TypeDesc.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**********************************************************************/
  2. /* Type Descriptor class for j2cps */
  3. /* */
  4. /* (c) copyright QUT, John Gough 2000-2012, John Gough, 2012-2017 */
  5. /**********************************************************************/
  6. package j2cps;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. public class TypeDesc {
  11. public static final int noTyp = 0;
  12. public static final int boolT = 1;
  13. public static final int sCharT = 2;
  14. public static final int charT = 3;
  15. public static final int byteT = 4;
  16. public static final int shortT = 5;
  17. public static final int intT = 6;
  18. public static final int longT = 7;
  19. public static final int floatT = 8;
  20. public static final int dbleT = 9;
  21. public static final int setT = 10;
  22. public static final int anyRT = 11;
  23. public static final int anyPT = 12;
  24. public static final int strT = 13;
  25. public static final int sStrT = 14;
  26. public static final int specT = 15;
  27. public static final int ordT = 16;
  28. public static final int arrT = 17;
  29. public static final int classT = 18;
  30. public static final int arrPtr = 19;
  31. public int typeFixUp = 0;
  32. private static final String[] typeStrArr =
  33. { "?","B","c","C","b","i","I","L","r","R",
  34. "?","?","?","?","?","?","?","a","O","?"};
  35. /**
  36. * Component Pascal version of java language name
  37. */
  38. public String name;
  39. /**
  40. * If this boolean is true then the type is
  41. * dumped to the symbol file with full
  42. * member information. If false just the
  43. * name and type ordinal number is emitted.
  44. */
  45. public boolean writeDetails = false;
  46. /**
  47. * This field holds the package descriptor
  48. * to which this type belongs.
  49. */
  50. public PackageDesc parentPkg = null;
  51. // ###########################
  52. // Temporary code for finding rogue elements in imports lists.
  53. // MethodInfo or FieldInfo that caused addition to import list.
  54. Object blame = null;
  55. // ###########################
  56. private static TypeDesc[] basicTypes = new TypeDesc[specT];
  57. int inTypeNum=0;
  58. int outTypeNum=0;
  59. int inBaseTypeNum = 0;
  60. int typeOrd = 0;
  61. static ArrayList<TypeDesc> types = new ArrayList<>();
  62. public TypeDesc() {
  63. inTypeNum = 0;
  64. outTypeNum = 0;
  65. typeOrd = 0;
  66. }
  67. private TypeDesc(int ix) {
  68. /* ONLY used for basic types */
  69. inTypeNum = ix;
  70. outTypeNum = ix;
  71. typeOrd = ix;
  72. }
  73. public String getTypeMnemonic() {
  74. return typeStrArr[typeOrd];
  75. }
  76. public static TypeDesc GetBasicType(int index) {
  77. return basicTypes[index];
  78. }
  79. public static TypeDesc GetType(String sig,int start) {
  80. int tOrd = GetTypeOrd(sig,start);
  81. switch (tOrd) {
  82. case classT:
  83. return ClassDesc.GetClassDesc(GetClassName(sig,start),null);
  84. case arrT:
  85. return ArrayDesc.GetArrayTypeFromSig(sig,start,true);
  86. default:
  87. return basicTypes[tOrd];
  88. }
  89. }
  90. private static String GetClassName(String sig,int start) {
  91. if (sig.charAt(start) != 'L') {
  92. System.out.println(sig.substring(0) + " is not a class name string!");
  93. System.exit(1);
  94. }
  95. int endCName = sig.indexOf(';',start);
  96. if (endCName == -1) {
  97. return sig.substring(start+1);
  98. } else {
  99. return sig.substring(start+1,endCName);
  100. }
  101. }
  102. private static int GetTypeOrd(String sig,int start) {
  103. switch (sig.charAt(start)) {
  104. case 'B' : return byteT;
  105. case 'C' : return charT;
  106. case 'D' : return dbleT;
  107. case 'F' : return floatT;
  108. case 'I' : return intT;
  109. case 'J' : return longT;
  110. case 'S' : return shortT;
  111. case 'Z' : return boolT;
  112. case 'V' : return noTyp;
  113. case 'L' : return classT;
  114. case '[' : return arrT;
  115. }
  116. return 0;
  117. }
  118. public static TypeDesc[] GetParTypes(String sig) {
  119. types.clear();
  120. TypeDesc[] typeArr;
  121. if (sig.charAt(0) != '(') {
  122. System.out.println(sig + " is not a parameter list!");
  123. System.exit(1);
  124. }
  125. int index = 1;
  126. while (sig.charAt(index) != ')') {
  127. if (sig.charAt(index) == '[') {
  128. types.add(ArrayDesc.GetArrayTypeFromSig(sig,index,false));
  129. } else {
  130. types.add(GetType(sig,index));
  131. }
  132. switch (sig.charAt(index)) {
  133. case 'L':
  134. index = sig.indexOf(';',index) + 1;
  135. break;
  136. case '[':
  137. while (sig.charAt(index) == '[') {
  138. index++;
  139. }
  140. if (sig.charAt(index) == 'L') {
  141. index = sig.indexOf(';',index) + 1;
  142. } else {
  143. index++;
  144. }
  145. break;
  146. default:
  147. index++;
  148. break;
  149. }
  150. }
  151. typeArr = new TypeDesc[types.size()];
  152. for (int i=0; i < types.size(); i++) {
  153. typeArr[i] = types.get(i);
  154. }
  155. return typeArr;
  156. }
  157. /**
  158. * Initialize <code>typeList</code> to begin with
  159. * the basic, predeclared types.
  160. */
  161. public static void InitTypes() {
  162. for (int i=0; i < specT; i++) {
  163. basicTypes[i] = new TypeDesc(i);
  164. basicTypes[i].name = "BasicType" + i;
  165. SymbolFile.typeList[i] = basicTypes[i];
  166. }
  167. }
  168. public void writeType (DataOutputStream out, PackageDesc thisPack)
  169. throws IOException {
  170. System.err.println("TRYING TO WRITE A TYPEDESC! with ord " + typeOrd);
  171. System.exit(1);
  172. }
  173. }