DEADSOFTWARE

Remove batch
[gpcp-linux.git] / J2CPS / SymbolFile.java
1 /**********************************************************************/
2 /* Symbol File class for J2CPS */
3 /* */
4 /* (c) copyright QUT */
5 /**********************************************************************/
6 package J2CPS;
8 import java.io.*;
9 import java.util.ArrayList;
11 class SymbolFile {
13 /************************************************************************/
14 /* Symbol file reading/writing */
15 /************************************************************************/
16 // Collected syntax ---
17 //
18 // SymFile = Header [String (falSy | truSy)]
19 // {Import | Constant | Variable | Type | Procedure}
20 // TypeList Key.
21 // Header = magic modSy Name.
22 // Import = impSy Name [String] Key.
23 // Constant = conSy Name Literal.
24 // Variable = varSy Name TypeOrd.
25 // Type = typSy Name TypeOrd.
26 // Procedure = prcSy Name [String] [truSy] FormalType.
27 // Method = mthSy Name Byte Byte TypeOrd [String] FormalType.
28 // FormalType = [retSy TypeOrd] frmSy {parSy Byte TypeOrd} endFm.
29 // TypeOrd = ordinal.
30 // TypeHeader = tDefS Ord [fromS Ord Name].
31 // TypeList = start {Array | Record | Pointer | ProcType |
32 // NamedType | Enum} close.
33 // Array = TypeHeader arrSy TypeOrd (Byte | Number | ) endAr.
34 // Pointer = TypeHeader ptrSy TypeOrd.
35 // ProcType = TypeHeader pTpSy FormalType.
36 // EventType = TypeHeader evtSy FormalType.
37 // Record = TypeHeader recSy recAtt [truSy | falSy] [basSy TypeOrd]
38 // [iFcSy basSy TypeOrd {basSy TypeOrd}]
39 // {Name TypeOrd} {Method} {Statics} endRc.
40 // Statics = ( Constant | Variable | Procedure ).
41 // Enum = TypeHeader eTpSy { Constant } endRc.
42 // NamedType = TypeHeader.
43 // Name = namSy byte UTFstring.
44 // Literal = Number | String | Set | Char | Real | falSy | truSy.
45 // Byte = bytSy byte.
46 // String = strSy UTFstring.
47 // Number = numSy java.lang.long.
48 // Real = fltSy java.lang.double.
49 // Set = setSy java.lang.int.
50 // Key = keySy java.lang.int.
51 // Char = chrSy java.lang.char.
52 //
53 // Notes on the syntax:
54 // All record types must have a Name field, even though this is often
55 // redundant. The issue is that every record type (including those that
56 // are anonymous in CP) corresponds to a Java class, and the definer
57 // and the user of the class _must_ agree on the JVM name of the class.
58 // The same reasoning applies to procedure types, which must have equal
59 // interface names in all modules.
60 //
62 static final String[] mthAtt = {"", ",NEW", ",ABSTRACT", ",NEW,ABSTRACT",
63 ",EMPTY", ",NEW,EMPTY",
64 ",EXTENSIBLE", ",NEW,EXTENSIBLE"};
65 static final String[] recAtt = {"RECORD ", "ABSTRACT RECORD ",
66 "LIMITED RECORD ", "EXTENSIBLE RECORD "};
67 static final String[] mark = {"", "*", "-", "!"};
68 static final String[] varMark = {"", "IN", "OUT", "VAR"};
70 private static final String spaces = " ";
71 private static final String recEndSpace = " ";
72 private static final char qSepCh = '/';
74 static final int modSy = (int) 'H';
75 static final int namSy = (int) '$';
76 static final int bytSy = (int) '\\';
77 static final int numSy = (int) '#';
78 static final int chrSy = (int) 'c';
79 static final int strSy = (int) 's';
80 static final int fltSy = (int) 'r';
81 static final int falSy = (int) '0';
82 static final int truSy = (int) '1';
83 static final int impSy = (int) 'I';
84 static final int setSy = (int) 'S';
85 static final int keySy = (int) 'K';
86 static final int conSy = (int) 'C';
87 static final int typSy = (int) 'T';
88 static final int tDefS = (int) 't';
89 static final int prcSy = (int) 'P';
90 static final int retSy = (int) 'R';
91 static final int mthSy = (int) 'M';
92 static final int varSy = (int) 'V';
93 static final int parSy = (int) 'p';
94 static final int iFcSy = (int) '~';
96 static final int start = (int) '&';
97 static final int close = (int) '!';
99 static final int recSy = (int) '{';
100 static final int endRc = (int) '}';
101 static final int frmSy = (int) '(';
102 static final int fromS = (int) '@';
103 static final int endFm = (int) ')';
104 static final int arrSy = (int) '[';
105 static final int endAr = (int) ']';
106 static final int pTpSy = (int) '%';
107 static final int evtSy = (int) 'v';
108 static final int ptrSy = (int) '^';
109 static final int basSy = (int) '+';
110 static final int eTpSy = (int) 'e';
112 static final int magic = 0xdeadd0d0;
114 static final int prvMode = 0;
115 static final int pubMode = 1;
116 static final int rdoMode = 2;
117 static final int protect = 3;
119 private static final int initTypeListSize = 128;
120 public static TypeDesc[] typeList = new TypeDesc[initTypeListSize];
121 private static int nextType = TypeDesc.ordT;
122 private static int tListIx = 0;
123 private static int sSym = 0;
124 private static int acc = 0;
125 private static String name;
126 private static int iVal;
127 private static long lVal;
128 private static int tOrd;
129 private static char cVal;
130 private static double dVal;
131 private static DataInputStream in;
133 // Symbol file writing
135 static void writeName(DataOutputStream out,int access, String name)
136 throws IOException{
137 out.writeByte(namSy);
138 if (ConstantPool.isPublic(access)) { out.writeByte(pubMode); }
139 else if (ConstantPool.isProtected(access)) { out.writeByte(protect); }
140 else /* if (ConstantPool.isPrivate(access)) */ { out.writeByte(prvMode); }
141 out.writeUTF(name);
144 static void writeString(DataOutputStream out,String str) throws IOException {
145 out.writeByte(strSy);
146 out.writeUTF(str);
149 static void writeLiteral(DataOutputStream out,Object val) throws IOException {
150 if (val instanceof String) {
151 writeString(out,(String) val);
152 } else if (val instanceof Integer) {
153 out.writeByte(numSy);
154 out.writeLong(((Integer)val).longValue());
155 } else if (val instanceof Long) {
156 out.writeByte(numSy);
157 out.writeLong(((Long)val).longValue());
158 } else if (val instanceof Float) {
159 out.writeByte(fltSy);
160 out.writeDouble(((Float)val).doubleValue());
161 } else if (val instanceof Double) {
162 out.writeByte(fltSy);
163 out.writeDouble(((Double)val).doubleValue());
164 } else {
165 System.out.println("Unknown constant type");
166 System.exit(1);
170 public static void writeOrd(DataOutputStream out,int i) throws IOException {
171 // DIAGNOSTIC
172 if (i < 0)
173 throw new IOException();
174 // DIAGNOSTIC
175 if (i <= 0x7f) {
176 out.writeByte(i);
177 } else if (i <= 0x7fff) {
178 out.writeByte(128 + i % 128);
179 out.writeByte(i / 128);
180 } else {
181 throw new IOException();
185 private static void InsertType(TypeDesc ty) {
186 if (ty.outTypeNum > 0) { return; }
187 ty.outTypeNum = nextType++;
188 if (tListIx >= typeList.length) {
189 TypeDesc[] tmp = new TypeDesc[typeList.length + initTypeListSize];
190 System.arraycopy(typeList, 0, tmp, 0, typeList.length);
191 typeList = tmp;
193 typeList[tListIx++] = ty;
196 public static void AddType(TypeDesc ty) {
197 InsertType(ty);
198 if (!ty.writeDetails) { return; }
199 if (ty instanceof ClassDesc) {
200 ClassDesc aClass = (ClassDesc)ty;
201 if (aClass.outBaseTypeNum > 0) { return; }
202 aClass.outBaseTypeNum = nextType++;
203 if (aClass.superClass != null) {
204 aClass.superClass.writeDetails = true;
205 AddType(aClass.superClass);
207 if (aClass.isInterface) {
208 for (int i=0; i < aClass.interfaces.length; i++) {
209 aClass.interfaces[i].writeDetails = true;
210 AddType(aClass.interfaces[i]);
213 } else if (ty instanceof PtrDesc) {
214 ty = ((PtrDesc)ty).boundType;
215 if (ty.outTypeNum == 0) { AddType(ty); }
216 } else if (ty instanceof ArrayDesc) {
217 ty = ((ArrayDesc)ty).elemType;
218 while (ty instanceof ArrayDesc) {
219 ArrayDesc aTy = (ArrayDesc)ty;
220 if (aTy.ptrType.outTypeNum == 0) { InsertType(aTy.ptrType); }
221 if (aTy.outTypeNum == 0) { InsertType(aTy); }
222 ty = aTy.elemType;
223 }
224 if (ty.outTypeNum == 0) { InsertType(ty); }
228 static void writeTypeOrd(DataOutputStream out,TypeDesc ty)throws IOException {
229 if (ty.typeOrd < TypeDesc.ordT) {
230 out.writeByte(ty.typeOrd);
231 } else {
232 if (ty.outTypeNum == 0) { AddType(ty); }
233 if (ty.outTypeNum == 0) {
234 System.out.println("ERROR: type has number 0 for type " + ty.name);
235 System.exit(1);
237 writeOrd(out,ty.outTypeNum);
241 public static void WriteFormalType(MethodInfo m,DataOutputStream out)
242 throws IOException {
243 if ((m.retType != null) && (m.retType.typeOrd != 0)) {
244 out.writeByte(retSy);
245 writeTypeOrd(out,m.retType);
246 }
247 out.writeByte(frmSy);
248 for (int i=0; i < m.parTypes.length; i++) {
249 out.writeByte(parSy);
250 if (m.parTypes[i] instanceof ArrayDesc) {
251 out.writeByte(1); // array params are IN
252 } else {
253 out.writeByte(0); // all other java parameters are value
255 writeTypeOrd(out,m.parTypes[i]);
257 out.writeByte(endFm);
260 public static void WriteSymbolFile(PackageDesc thisPack) throws IOException{
261 ClearTypeList();
262 DataOutputStream out = J2CPSFiles.CreateSymFile(thisPack.cpName);
264 System.out.println("INFO: Creating symbol file " + thisPack.cpName);
266 out.writeInt(magic);
267 out.writeByte(modSy);
268 writeName(out,0,thisPack.cpName);
269 writeString(out,thisPack.javaName);
270 out.writeByte(falSy); /* package is not an interface */
271 for (int i=0; i < thisPack.imports.size(); i++) {
272 out.writeByte(impSy);
273 PackageDesc imp = (PackageDesc)thisPack.imports.get(i);
274 imp.impNum = i+1;
275 writeName(out,0,imp.cpName);
276 writeString(out,imp.javaName);
277 out.writeByte(keySy);
278 out.writeInt(0);
280 for (int cNum=0; cNum < thisPack.classes.length; cNum++) {
281 ClassDesc thisClass = thisPack.classes[cNum];
282 if ((thisClass != null) && ConstantPool.isPublic(thisClass.access)) {
283 thisClass.writeDetails = true;
284 out.writeByte(typSy);
285 writeName(out,thisClass.access,thisClass.objName);
286 writeTypeOrd(out,thisClass);
289 out.writeByte(start);
290 for (int i=0; i < tListIx; i++) {
291 out.writeByte(tDefS);
292 writeOrd(out,typeList[i].outTypeNum);
293 typeList[i].writeType(out,thisPack);
295 out.writeByte(close);
296 out.writeByte(keySy);
297 out.writeInt(0);
298 thisPack.ResetImports();
301 // Symbol file reading
303 private static void InsertType(int tNum,TypeDesc ty) {
304 if (tNum >= typeList.length) {
305 int newLen = 2 * typeList.length;
306 while (tNum >= newLen) { newLen += typeList.length; }
307 TypeDesc[] tmp = new TypeDesc[newLen];
308 System.arraycopy(typeList, 0, tmp, 0, typeList.length);
309 typeList = tmp;
311 typeList[tNum] = ty;
315 private static int readOrd() throws IOException {
316 int b1 = in.readUnsignedByte();
317 if (b1 <= 0x7f) { return b1; }
318 else { int b2 = in.readByte();
319 return b1 - 128 + b2 * 128; }
322 private static void GetSym() throws IOException {
323 sSym = in.readByte();
324 switch (sSym) {
325 case namSy : acc = in.readByte(); // fall through
326 case strSy : name = in.readUTF(); break;
327 case arrSy :
328 case ptrSy :
329 case retSy :
330 case fromS :
331 case tDefS :
332 case basSy : tOrd = readOrd(); break;
333 case bytSy : iVal = in.readByte(); break;
334 case keySy :
335 case setSy : iVal = in.readInt(); break;
336 case numSy : lVal = in.readLong(); break;
337 case fltSy : dVal = in.readDouble(); break;
338 case chrSy : cVal = in.readChar(); break;
339 case modSy :
340 case impSy :
341 case conSy :
342 case varSy :
343 case typSy :
344 case prcSy :
345 case mthSy :
346 case parSy :
347 case start :
348 case close :
349 case falSy :
350 case truSy :
351 case frmSy :
352 case endFm :
353 case recSy :
354 case endRc :
355 case endAr :
356 case eTpSy :
357 case iFcSy :
358 case evtSy :
359 case pTpSy : break;
360 default: char ch = (char) sSym;
361 System.out.println("Bad symbol file format." +ch+" "+sSym);
362 System.exit(1);
366 private static void Expect(int expSym) throws IOException {
367 if (expSym != sSym) {
368 System.out.println("Error in symbol file: expecting " +
369 String.valueOf((char) expSym) + " got " +
370 String.valueOf((char) sSym));
371 System.exit(1);
373 GetSym();
376 private static void Check(int expSym) {
377 if (expSym != sSym) {
378 System.out.println("Error in symbol file: checking " +
379 String.valueOf((char) expSym) + " got " +
380 String.valueOf((char) sSym));
381 System.exit(1);
385 private static void SkipToEndRec(DataInputStream in) throws IOException {
386 while (sSym != endRc) {
387 if (sSym == mthSy) {
388 GetSym(); // name
389 in.readByte();
390 in.readByte();
391 readOrd();
392 } else if (sSym == varSy) {
393 GetSym(); // name
394 readOrd();
395 } else if (sSym == conSy) {
396 GetSym(); // name
397 GetSym(); // Literal
398 } else if (sSym == prcSy) {
399 GetSym(); // name
400 } else if (sSym == parSy) {
401 in.readByte();
402 readOrd();
403 } else if (sSym == namSy) {
404 readOrd();
405 } else {
407 GetSym();
411 private static int GetAccess() {
412 if (acc == prvMode) { return ConstantPool.ACC_PRIVATE; }
413 else if (acc == pubMode) { return ConstantPool.ACC_PUBLIC; }
414 else if (acc == protect) { return ConstantPool.ACC_PROTECTED; }
415 return 0;
418 private static ClassDesc GetClassDesc(PackageDesc thisPack,String className) {
419 ClassDesc aClass = ClassDesc.GetClassDesc(thisPack.name + qSepCh +
420 className,thisPack);
421 if (aClass.fieldList == null){ aClass.fieldList = new ArrayList(); }
422 if (aClass.methodList == null){ aClass.methodList = new ArrayList(); }
423 return aClass;
426 private static void GetConstant(ClassDesc cClass) throws IOException {
427 // Constant = conSy Name Literal.
428 // Literal = Number | String | Set | Char | Real | falSy | truSy.
429 TypeDesc typ = null;
430 Object val = null;
431 Expect(conSy);
432 String constName = name;
433 int fAcc = GetAccess();
434 fAcc = fAcc + ConstantPool.ACC_STATIC + ConstantPool.ACC_FINAL;
435 Expect(namSy);
436 switch (sSym) {
437 case numSy : typ = TypeDesc.GetBasicType(TypeDesc.longT);
438 val = new Long(lVal); break;
439 case strSy : typ = TypeDesc.GetBasicType(TypeDesc.strT);
440 val = name;
441 case setSy : typ = TypeDesc.GetBasicType(TypeDesc.setT);
442 val = new Integer(iVal); break;
443 case chrSy : typ = TypeDesc.GetBasicType(TypeDesc.charT);
444 val = new Character(cVal); break;
445 case fltSy : typ = TypeDesc.GetBasicType(TypeDesc.dbleT);
446 val = new Double(dVal); break;
447 case falSy : typ = TypeDesc.GetBasicType(TypeDesc.boolT);
448 val = false; break;
449 case truSy : typ = TypeDesc.GetBasicType(TypeDesc.boolT);
450 val = true; break;
452 boolean ok = cClass.fieldList.add(new FieldInfo(cClass,fAcc,constName,typ,val));
453 GetSym();
456 private static void GetVar(ClassDesc vClass) throws IOException {
457 // Variable = varSy Name TypeOrd.
458 Expect(varSy);
459 String varName = name;
460 int fAcc = GetAccess();
461 Check(namSy);
462 FieldInfo f = new FieldInfo(vClass,fAcc,varName,null,null);
463 f.typeFixUp = readOrd();
464 vClass.fieldList.add(f);
465 GetSym();
468 private static void GetType(PackageDesc thisPack) throws IOException {
469 // Type = typSy Name TypeOrd.
470 Expect(typSy);
471 ClassDesc thisClass = GetClassDesc(thisPack,name);
472 thisClass.access = GetAccess();
473 Check(namSy);
474 int tNum = readOrd();
475 thisClass.inTypeNum = tNum;
476 InsertType(tNum,thisClass);
477 GetSym();
480 private static void GetFormalType(ClassDesc thisClass,MethodInfo thisMethod)
481 throws IOException {
482 // FormalType = [retSy TypeOrd] frmSy {parSy Byte TypeOrd} endFm.
483 int [] pars = new int[20];
484 int numPars = 0;
485 TypeDesc retType = TypeDesc.GetBasicType(TypeDesc.noTyp);
486 if (sSym == retSy) { thisMethod.retTypeFixUp = tOrd; GetSym();}
487 Expect(frmSy);
488 while (sSym != endFm) {
489 Check(parSy);
490 in.readByte(); /* ignore par mode */
491 pars[numPars++] = readOrd();
492 GetSym();
493 }
494 Expect(endFm);
495 thisMethod.parFixUps = new int[numPars];
496 System.arraycopy(pars, 0, thisMethod.parFixUps, 0, numPars);
500 private static void GetMethod(ClassDesc thisClass) throws IOException {
501 // Method = mthSy Name Byte Byte TypeOrd [String] FormalType.
502 String jName = null;
503 Expect(mthSy);
504 Check(namSy);
505 String nam = name;
506 int pAcc = GetAccess();
507 int attr = in.readByte();
508 int recMode = in.readByte();
509 int cNum = readOrd();
510 if (cNum != thisClass.inTypeNum) {
511 System.err.println("Method not part of THIS class!");
512 System.exit(1);
513 }
514 GetSym();
515 if (sSym == strSy) { jName = name; GetSym(); }
516 MethodInfo m = new MethodInfo(thisClass,nam,jName,pAcc);
517 switch (attr) {
518 case 1 : if (!m.isInitProc) {
519 m.accessFlags += ConstantPool.ACC_FINAL;
521 break;
522 case 2 : m.overridding = true;
523 m.accessFlags += (ConstantPool.ACC_ABSTRACT +
524 ConstantPool.ACC_FINAL);
525 break;
526 case 3 : m.accessFlags += (ConstantPool.ACC_ABSTRACT +
527 ConstantPool.ACC_FINAL);
528 break;
529 case 6 : m.overridding = true;
530 break;
531 case 7 : break;
533 GetFormalType(thisClass,m);
534 thisClass.methodList.add(m);
535 thisClass.scope.put(m.name,m);
538 private static void GetProc(ClassDesc pClass) throws IOException {
539 // Proc = prcSy Name [String] [truSy] FormalType.
540 String jName = null;
541 Expect(prcSy);
542 String procName = name;
543 int pAcc = GetAccess();
544 pAcc = pAcc + ConstantPool.ACC_STATIC;
545 Expect(namSy);
546 if (sSym == strSy) { jName = name; GetSym(); }
547 MethodInfo m = new MethodInfo(pClass,procName,jName,pAcc);
548 if (sSym == truSy) { m.isInitProc = true; GetSym(); }
549 GetFormalType(pClass,m);
550 pClass.methodList.add(m);
553 private static void ClearTypeList() {
554 for (int i=0; i < typeList.length; i++) {
555 if (typeList[i] != null) {
556 if (typeList[i].typeOrd >= TypeDesc.specT) {
557 typeList[i].inTypeNum = 0;
558 typeList[i].outTypeNum = 0;
560 if (typeList[i] instanceof ClassDesc) {
561 ((ClassDesc)typeList[i]).inBaseTypeNum = 0;
562 ((ClassDesc)typeList[i]).outBaseTypeNum = 0;
563 ((ClassDesc)typeList[i]).writeDetails = false;
564 } else if (typeList[i] instanceof ArrayDesc) {
565 ((ArrayDesc)typeList[i]).elemTypeFixUp = 0;
568 typeList[i] = null;
570 tListIx = 0;
571 nextType = TypeDesc.ordT;
574 private static void FixArrayElemType(ArrayDesc arr) {
575 if (arr.elemTypeFixUp == 0) { return; }
576 TypeDesc elem = GetFixUpType(arr.elemTypeFixUp);
577 if (elem instanceof ArrayDesc) {
578 FixArrayElemType((ArrayDesc)elem);
579 arr.dim = ((ArrayDesc)elem).dim + 1;
580 arr.ultimateElemType = ((ArrayDesc)elem).ultimateElemType;
581 } else {
582 arr.ultimateElemType = elem;
584 arr.elemType = elem;
587 private static TypeDesc GetFixUpType (int num) {
588 if (num < TypeDesc.specT) { return TypeDesc.GetBasicType(num); }
589 if (typeList[num] instanceof PtrDesc) {
590 return ((PtrDesc)typeList[num]).boundType;
592 return typeList[num];
593 }
595 public static void ReadSymbolFile(File symFile,PackageDesc thisPack)
596 throws FileNotFoundException, IOException {
598 if (ClassDesc.verbose)
599 System.out.println("INFO: Reading symbol file " + symFile.getName());
601 ClearTypeList();
602 ClassDesc aClass, impClass;
603 int maxInNum = 0;
604 FileInputStream fIn = new FileInputStream(symFile);
605 in = new DataInputStream(fIn);
606 if (in.readInt() != magic) {
607 System.out.println(symFile.getName() + " is not a valid symbol file.");
608 System.exit(1);
610 GetSym();
611 Expect(modSy);
612 if (!thisPack.cpName.equals(name)) {
613 System.out.println("ERROR: Symbol file " + symFile.getName() +
614 " does not contain MODULE " + thisPack.cpName + ", it contains MODULE " +
615 name);
616 System.exit(1);
618 Expect(namSy);
619 if (sSym == strSy) {
620 if (!name.equals(thisPack.javaName)) {
621 System.out.println("Wrong name in symbol file.");
622 System.exit(1);
624 GetSym();
625 if (sSym == truSy) {
626 System.out.println("ERROR: Java Package cannot be an interface.");
627 System.exit(1);
629 GetSym();
630 } else {
631 System.err.println("<" + symFile.getName() +
632 "> NOT A SYMBOL FILE FOR A JAVA PACKAGE!");
633 System.exit(1);
635 while (sSym != start) {
636 switch (sSym) {
637 case impSy : GetSym(); // name
638 String iName = name;
639 GetSym();
640 if (sSym == strSy) {
641 PackageDesc pack = PackageDesc.getPackage(name);
642 thisPack.imports.add(pack);
643 GetSym();
645 Expect(keySy);
646 break;
647 case conSy :
648 case varSy :
649 case prcSy : System.out.println("Symbol File is not from a java class");
650 System.exit(1);
651 break;
652 case typSy : GetType(thisPack); break;
655 Expect(start);
656 while (sSym != close) {
657 PackageDesc impPack;
658 impClass = null;
659 String impModName = null;
660 int impAcc = 0, impModAcc = 0;
661 Check(tDefS);
662 int tNum = tOrd; GetSym();
663 if (tNum > maxInNum) { maxInNum = tNum; }
664 if (sSym == fromS) {
665 int impNum = tOrd - 1;
666 GetSym();
667 Check(namSy);
668 String impName = name;
669 impAcc = acc;
670 if (impNum < 0) {
671 impPack = thisPack;
672 } else {
673 impPack = (PackageDesc)thisPack.imports.get(impNum);
675 impClass = GetClassDesc(impPack,impName);
676 GetSym();
678 switch (sSym) {
679 case arrSy : ArrayDesc newArr = null;
680 int elemOrd = tOrd;
681 GetSym();
682 Expect(endAr);
683 TypeDesc eTy = null;
684 if (elemOrd < typeList.length) {
685 if (elemOrd < TypeDesc.specT) {
686 eTy = TypeDesc.GetBasicType(elemOrd);
687 } else {
688 eTy = typeList[elemOrd];
690 if ((eTy != null) && (eTy instanceof PtrDesc) &&
691 (((PtrDesc)eTy).boundType != null) &&
692 (((PtrDesc)eTy).boundType instanceof ClassDesc)) {
693 eTy = ((PtrDesc)eTy).boundType;
694 }
696 if (eTy != null) {
697 newArr = ArrayDesc.FindArrayType(1,eTy,true);
698 } else {
699 newArr = new ArrayDesc(elemOrd);
701 if ((tNum < typeList.length) && (typeList[tNum] != null)) {
702 PtrDesc desc = (PtrDesc) typeList[tNum];
703 if (desc.inBaseTypeNum != tNum) {
704 System.out.println("WRONG BASE TYPE FOR POINTER!");
705 System.exit(1);
707 desc.Init(newArr);
708 newArr.SetPtrType(desc);
710 InsertType(tNum,newArr);
711 break;
712 case ptrSy : TypeDesc ty = null;
713 if (impClass != null) {
714 InsertType(tNum,impClass);
715 ty = impClass;
716 ty.inTypeNum = tNum;
717 ty.inBaseTypeNum = tOrd;
718 InsertType(tOrd,ty);
719 } else if ((tNum < typeList.length) &&
720 (typeList[tNum] != null) &&
721 (typeList[tNum] instanceof ClassDesc)) {
722 ty = typeList[tNum];
723 ty.inTypeNum = tNum;
724 ty.inBaseTypeNum = tOrd;
725 InsertType(tOrd,ty);
726 } else {
727 ty = new PtrDesc(tNum,tOrd);
728 InsertType(tNum,ty);
729 if ((tOrd < typeList.length) &&
730 (typeList[tOrd] != null)) {
731 ((PtrDesc)ty).Init(typeList[tOrd]);
734 GetSym();
735 break;
736 case recSy : if ((tNum >= typeList.length) || (typeList[tNum] == null)||
737 (!(typeList[tNum] instanceof ClassDesc))) {
738 /* cannot have record type that is not a base type
739 of a pointer in a java file */
740 System.err.println(
741 "RECORD TYPE " + tNum + " IS NOT POINTER BASE TYPE!");
742 System.exit(1);
744 aClass = (ClassDesc) typeList[tNum];
745 acc = in.readByte();
746 aClass.setRecAtt(acc);
747 if (aClass.read) {
748 GetSym();
749 SkipToEndRec(in);
750 GetSym();
751 } else {
752 GetSym();
753 if (sSym == truSy) {
754 aClass.isInterface = true;
755 GetSym();
756 } else if (sSym == falSy) {
757 GetSym();
759 if (sSym == basSy) {
760 aClass.superNum = tOrd;
761 GetSym();
763 if (sSym == iFcSy) {
764 GetSym();
765 aClass.intNums = new int[10];
766 aClass.numInts = 0;
767 while (sSym == basSy) {
768 if (aClass.numInts >= aClass.intNums.length) {
769 int tmp[] = new int[aClass.intNums.length*2];
770 System.arraycopy(aClass.intNums, 0, tmp, 0, aClass.intNums.length);
771 aClass.intNums = tmp;
773 aClass.intNums[aClass.numInts] = tOrd;
774 aClass.numInts++;
775 GetSym();
778 while (sSym == namSy) {
779 FieldInfo f = new FieldInfo(aClass,GetAccess(),name,
780 null,null);
781 f.typeFixUp = readOrd();
782 GetSym();
783 boolean ok = aClass.fieldList.add(f);
784 aClass.scope.put(f.name,f);
785 }
786 while ((sSym == mthSy) || (sSym == prcSy) ||
787 (sSym == varSy) || (sSym == conSy)) {
788 switch (sSym) {
789 case mthSy : GetMethod(aClass); break;
790 case prcSy : GetProc(aClass); break;
791 case varSy : GetVar(aClass); break;
792 case conSy : GetConstant(aClass); break;
795 Expect(endRc);
797 break;
798 case pTpSy : System.out.println("CANNOT HAVE PROC TYPE IN JAVA FILE!");
799 break;
800 case evtSy :System.out.println("CANNOT HAVE EVENT TYPE IN JAVA FILE!");
801 break;
802 case eTpSy : System.out.println("CANNOT HAVE ENUM TYPE IN JAVA FILE!");
803 break;
804 case tDefS :
805 case close : InsertType(tNum,impClass);
806 break;
807 default : char ch = (char) sSym;
808 System.out.println("UNRECOGNISED TYPE!" + sSym + " " + ch);
809 System.exit(1);
812 Expect(close);
813 Check(keySy);
814 fIn.close();
815 // do fix ups...
816 for (int i = TypeDesc.specT; i <= maxInNum; i++) {
817 if ((typeList[i] != null) && (typeList[i] instanceof ClassDesc)) {
818 if (!((ClassDesc)typeList[i]).read) {
819 aClass = (ClassDesc)typeList[i];
820 if (aClass.superNum != 0) {
821 aClass.superClass = (ClassDesc)typeList[aClass.superNum];
823 aClass.interfaces = new ClassDesc[aClass.numInts];
824 for (int j=0; j < aClass.numInts; j++) {
825 aClass.interfaces[j] = (ClassDesc) GetFixUpType(aClass.intNums[j]);
827 int size;
828 if (aClass.fieldList == null) {
829 size = 0;
830 } else {
831 size = aClass.fieldList.size();
833 aClass.fields = new FieldInfo[size];
834 for (int j=0; j < size; j++) {
835 aClass.fields[j] = (FieldInfo)aClass.fieldList.get(j);
836 aClass.fields[j].type = GetFixUpType(aClass.fields[j].typeFixUp);
837 if (aClass.fields[j].type instanceof ClassDesc) {
838 aClass.AddImport((ClassDesc)aClass.fields[j].type);
839 }
841 aClass.fieldList = null;
842 if (aClass.methodList == null) { size = 0;
843 } else { size = aClass.methodList.size(); }
844 aClass.methods = new MethodInfo[size];
845 for (int k=0; k < size; k++) {
846 aClass.methods[k] = (MethodInfo)aClass.methodList.get(k);
847 aClass.methods[k].retType = GetFixUpType(
848 aClass.methods[k].retTypeFixUp);
849 if (aClass.methods[k].retType instanceof ClassDesc) {
850 aClass.AddImport((ClassDesc)aClass.methods[k].retType);
851 }
852 aClass.methods[k].parTypes = new TypeDesc[
853 aClass.methods[k].parFixUps.length];
854 for (int j=0; j < aClass.methods[k].parFixUps.length; j++) {
855 aClass.methods[k].parTypes[j] = GetFixUpType(
856 aClass.methods[k].parFixUps[j]);
857 if (aClass.methods[k].parTypes[j] instanceof ClassDesc) {
858 aClass.AddImport((ClassDesc)aClass.methods[k].parTypes[j]);
859 }
862 aClass.methodList = null;
863 aClass.read = true;
864 aClass.done = true;
866 } else if ((typeList[i] != null) && (typeList[i] instanceof ArrayDesc)) {
867 FixArrayElemType((ArrayDesc)typeList[i]);
868 } else if ((typeList[i] != null) && (typeList[i] instanceof PtrDesc)) {
869 PtrDesc ptr = (PtrDesc)typeList[i];
870 if (ptr.typeOrd == TypeDesc.arrPtr) {
871 ptr.Init(typeList[ptr.inBaseTypeNum]);
873 } else if (typeList[i] != null) {
874 System.out.println("Type " + i + " " + typeList[i].name +
875 " is NOT array or class");
876 System.exit(0);