TypeDesc.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**********************************************************************/
  2. /* Type Descriptor class for J2CPS */
  3. /* */
  4. /* (c) copyright QUT */
  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. public String name;
  36. public boolean writeDetails = false;
  37. public PackageDesc packageDesc = null;
  38. private static TypeDesc[] basicTypes = new TypeDesc[specT];
  39. int inTypeNum=0, outTypeNum=0, inBaseTypeNum = 0;
  40. int typeOrd = 0;
  41. static ArrayList<TypeDesc> types = new ArrayList<TypeDesc>();
  42. public TypeDesc() {
  43. inTypeNum = 0;
  44. outTypeNum = 0;
  45. typeOrd = 0;
  46. }
  47. private TypeDesc(int ix) {
  48. /* ONLY used for basic types */
  49. inTypeNum = ix;
  50. outTypeNum = ix;
  51. typeOrd = ix;
  52. }
  53. public String getTypeMnemonic() {
  54. return typeStrArr[typeOrd];
  55. }
  56. public static TypeDesc GetBasicType(int index) {
  57. return basicTypes[index];
  58. }
  59. public static TypeDesc GetType(String sig,int start) {
  60. int tOrd = GetTypeOrd(sig,start);
  61. if (tOrd == classT) {
  62. return ClassDesc.GetClassDesc(GetClassName(sig,start),null);
  63. } else if (tOrd == arrT) {
  64. return ArrayDesc.GetArrayType(sig,start,true);
  65. } else {
  66. return basicTypes[tOrd];
  67. }
  68. }
  69. private static String GetClassName(String sig,int start) {
  70. if (sig.charAt(start) != 'L') {
  71. System.out.println(sig.substring(0) + " is not a class name string!");
  72. System.exit(1);
  73. }
  74. int endCName = sig.indexOf(';',start);
  75. if (endCName == -1) {
  76. return sig.substring(start+1);
  77. } else {
  78. return sig.substring(start+1,endCName);
  79. }
  80. }
  81. private static int GetTypeOrd(String sig,int start) {
  82. switch (sig.charAt(start)) {
  83. case 'B' : return byteT;
  84. case 'C' : return charT;
  85. case 'D' : return dbleT;
  86. case 'F' : return floatT;
  87. case 'I' : return intT;
  88. case 'J' : return longT;
  89. case 'S' : return shortT;
  90. case 'Z' : return boolT;
  91. case 'V' : return noTyp;
  92. case 'L' : return classT;
  93. case '[' : return arrT;
  94. }
  95. return 0;
  96. }
  97. public static TypeDesc[] GetParTypes(String sig) {
  98. types.clear();
  99. TypeDesc[] typeArr;
  100. if (sig.charAt(0) != '(') {
  101. System.out.println(sig + " is not a parameter list!");
  102. System.exit(1);
  103. }
  104. int index = 1;
  105. while (sig.charAt(index) != ')') {
  106. if (sig.charAt(index) == '[') {
  107. types.add(ArrayDesc.GetArrayType(sig,index,false));
  108. } else {
  109. types.add(GetType(sig,index));
  110. }
  111. if (sig.charAt(index) == 'L') {
  112. index = sig.indexOf(';',index) + 1;
  113. } else if (sig.charAt(index) == '[') {
  114. while (sig.charAt(index) == '[') { index++; }
  115. if (sig.charAt(index) == 'L') { index = sig.indexOf(';',index) + 1;
  116. } else { index++; }
  117. } else { index++; }
  118. }
  119. typeArr = new TypeDesc[types.size()];
  120. for (int i=0; i < types.size(); i++) {
  121. typeArr[i] = types.get(i);
  122. }
  123. return typeArr;
  124. }
  125. public static void InitTypes() {
  126. for (int i=0; i < specT; i++) {
  127. basicTypes[i] = new TypeDesc(i);
  128. basicTypes[i].name = "BasicType" + i;
  129. SymbolFile.typeList[i] = basicTypes[i];
  130. }
  131. }
  132. public void writeType (DataOutputStream out, PackageDesc thisPack)
  133. throws IOException {
  134. System.err.println("TRYING TO WRITE A TYPEDESC! with ord " + typeOrd);
  135. System.exit(1);
  136. }
  137. }