6 public class SemanticCheck {
10 public SemanticCheck(State state, TypeUtil tu) {
15 public void semanticCheck() {
16 SymbolTable classtable=state.getClassSymbolTable();
17 Iterator it=classtable.getDescriptorsIterator();
18 // Do descriptors first
20 ClassDescriptor cd=(ClassDescriptor)it.next();
21 System.out.println("Checking class: "+cd);
22 //Set superclass link up
23 if (cd.getSuper()!=null) {
24 cd.setSuper(typeutil.getClass(cd.getSuper()));
25 // Link together Field and Method tables
26 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
27 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
30 for(Iterator field_it=cd.getFields();field_it.hasNext();) {
31 FieldDescriptor fd=(FieldDescriptor)field_it.next();
32 System.out.println("Checking field: "+fd);
35 for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
36 MethodDescriptor md=(MethodDescriptor)method_it.next();
41 it=classtable.getDescriptorsIterator();
42 // Do descriptors first
44 ClassDescriptor cd=(ClassDescriptor)it.next();
45 for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
46 MethodDescriptor md=(MethodDescriptor)method_it.next();
47 checkMethodBody(cd,md);
52 public void checkTypeDescriptor(TypeDescriptor td) {
56 String name=td.toString();
57 ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
59 throw new Error("Undefined class "+name);
60 td.setClassDescriptor(field_cd);
66 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
67 checkTypeDescriptor(fd.getType());
70 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
71 /* Check return type */
72 if (!md.isConstructor())
73 if (!md.getReturnType().isVoid())
74 checkTypeDescriptor(md.getReturnType());
76 for(int i=0;i<md.numParameters();i++) {
77 TypeDescriptor param_type=md.getParamType(i);
78 checkTypeDescriptor(param_type);
80 /* Link the naming environments */
81 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
82 md.getParameterTable().setParent(cd.getFieldTable());
85 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
90 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
91 System.out.println("Processing method:"+md);
92 BlockNode bn=state.getMethodBody(md);
93 checkBlockNode(md, md.getParameterTable(),bn);
96 public void checkBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
97 /* Link in the naming environment */
98 bn.getVarTable().setParent(nametable);
99 for(int i=0;i<bn.size();i++) {
100 BlockStatementNode bsn=bn.get(i);
101 checkBlockStatementNode(md, bn.getVarTable(),bsn);
105 public void checkBlockStatementNode(MethodDescriptor md, SymbolTable nametable, BlockStatementNode bsn) {
107 case Kind.BlockExpressionNode:
108 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
111 case Kind.DeclarationNode:
112 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
115 case Kind.IfStatementNode:
116 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
120 checkLoopNode(md, nametable, (LoopNode)bsn);
123 case Kind.ReturnNode:
124 checkReturnNode(md, nametable, (ReturnNode)bsn);
127 case Kind.SubBlockNode:
128 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
134 void checkBlockExpressionNode(MethodDescriptor md, SymbolTable nametable, BlockExpressionNode ben) {
135 checkExpressionNode(md, nametable, ben.getExpression(), null);
138 void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
139 VarDescriptor vd=dn.getVarDescriptor();
140 checkTypeDescriptor(vd.getType());
141 Descriptor d=nametable.get(vd.getSymbol());
143 (d instanceof FieldDescriptor)) {
146 throw new Error(vd.getSymbol()+" defined a second time");
147 if (dn.getExpression()!=null)
148 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
151 void checkSubBlockNode(MethodDescriptor md, SymbolTable nametable, SubBlockNode sbn) {
152 checkBlockNode(md, nametable, sbn.getBlockNode());
155 void checkReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn) {
156 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
159 void checkIfStatementNode(MethodDescriptor md, SymbolTable nametable, IfStatementNode isn) {
160 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
161 checkBlockNode(md, nametable, isn.getTrueBlock());
162 if (isn.getFalseBlock()!=null)
163 checkBlockNode(md, nametable, isn.getFalseBlock());
166 void checkExpressionNode(MethodDescriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
168 case Kind.AssignmentNode:
169 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
172 checkCastNode(md,nametable,(CastNode)en,td);
174 case Kind.CreateObjectNode:
175 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
177 case Kind.FieldAccessNode:
178 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
180 case Kind.LiteralNode:
181 checkLiteralNode(md,nametable,(LiteralNode)en,td);
183 case Kind.MethodInvokeNode:
184 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
187 checkNameNode(md,nametable,(NameNode)en,td);
190 checkOpNode(md,nametable,(OpNode)en,td);
196 void checkCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
197 /* Get type descriptor */
198 if (cn.getType()==null) {
199 NameDescriptor typenamed=cn.getTypeName().getName();
200 String typename=typenamed.toString();
201 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
205 /* Check the type descriptor */
206 TypeDescriptor cast_type=cn.getType();
207 checkTypeDescriptor(cast_type);
211 if (!typeutil.isSuperorType(td,cast_type))
212 throw new Error("Cast node returns "+cast_type+", but need "+td);
215 ExpressionNode en=cn.getExpression();
216 checkExpressionNode(md, nametable, en, null);
217 TypeDescriptor etd=en.getType();
218 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
221 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
224 /* Different branches */
225 /* TODO: change if add interfaces */
226 throw new Error("Cast will always fail\n"+cn.printNode(0));
229 void checkFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
230 ExpressionNode left=fan.getExpression();
231 checkExpressionNode(md,nametable,left,null);
232 TypeDescriptor ltd=left.getType();
233 String fieldname=fan.getFieldName();
234 FieldDescriptor fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
236 throw new Error("Unknown field "+fieldname);
239 if (!typeutil.isSuperorType(td,fan.getType()))
240 throw new Error("Field node returns "+fan.getType()+", but need "+td);
243 void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
244 /* Resolve the type */
245 Object o=ln.getValue();
246 if (ln.getTypeString().equals("null")) {
247 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
248 } else if (o instanceof Integer) {
249 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
250 } else if (o instanceof Long) {
251 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
252 } else if (o instanceof Float) {
253 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
254 } else if (o instanceof Boolean) {
255 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
256 } else if (o instanceof Double) {
257 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
258 } else if (o instanceof Character) {
259 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
260 } else if (o instanceof String) {
261 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
265 if (!typeutil.isSuperorType(td,ln.getType()))
266 throw new Error("Field node returns "+ln.getType()+", but need "+td);
269 void checkNameNode(MethodDescriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
270 NameDescriptor nd=nn.getName();
271 if (nd.getBase()!=null) {
273 /* Rewrite NameNode */
274 ExpressionNode en=translateNameDescriptorintoExpression(nd);
275 nn.setExpression(en);
276 checkExpressionNode(md,nametable,en,td);
278 String varname=nd.toString();
279 Descriptor d=(Descriptor)nametable.get(varname);
281 throw new Error("Name "+varname+" undefined");
283 if (d instanceof VarDescriptor) {
284 nn.setVar((VarDescriptor)d);
285 } else if (d instanceof FieldDescriptor) {
286 nn.setField((FieldDescriptor)d);
287 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
290 if (!typeutil.isSuperorType(td,nn.getType()))
291 throw new Error("Field node returns "+nn.getType()+", but need "+td);
295 void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
296 checkExpressionNode(md, nametable, an.getSrc() ,td);
297 //TODO: Need check on validity of operation here
298 if (!((an.getDest() instanceof FieldAccessNode)||
299 (an.getDest() instanceof NameNode)))
300 throw new Error("Bad lside in "+an.printNode(0));
301 checkExpressionNode(md, nametable, an.getDest(), null);
302 if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
303 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
307 void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
308 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
309 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
310 checkBlockNode(md, nametable, ln.getBody());
313 /* Link in the initializer naming environment */
314 BlockNode bn=ln.getInitializer();
315 bn.getVarTable().setParent(nametable);
316 for(int i=0;i<bn.size();i++) {
317 BlockStatementNode bsn=bn.get(i);
318 checkBlockStatementNode(md, bn.getVarTable(),bsn);
320 //check the condition
321 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
322 checkBlockNode(md, bn.getVarTable(), ln.getBody());
323 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
328 void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
329 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
330 for(int i=0;i<con.numArgs();i++) {
331 ExpressionNode en=con.getArg(i);
332 checkExpressionNode(md,nametable,en,null);
333 tdarray[i]=en.getType();
336 TypeDescriptor typetolookin=con.getType();
337 checkTypeDescriptor(typetolookin);
338 if (!typetolookin.isClass())
341 ClassDescriptor classtolookin=typetolookin.getClassDesc();
342 System.out.println("Looking for "+typetolookin.getSymbol());
343 System.out.println(classtolookin.getMethodTable());
345 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
346 MethodDescriptor bestmd=null;
348 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
349 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
350 /* Need correct number of parameters */
351 System.out.println("Examining: "+currmd);
352 if (con.numArgs()!=currmd.numParameters())
354 for(int i=0;i<con.numArgs();i++) {
355 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
358 /* Method okay so far */
362 if (isMoreSpecific(currmd,bestmd)) {
364 } else if (!isMoreSpecific(bestmd, currmd))
365 throw new Error("No method is most specific");
367 /* Is this more specific than bestmd */
371 throw new Error("No method found for "+con.printNode(0));
372 con.setConstructor(bestmd);
378 /** Check to see if md1 is more specific than md2... Informally
379 if md2 could always be called given the arguments passed into
382 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
383 /* Checks if md1 is more specific than md2 */
384 if (md1.numParameters()!=md2.numParameters())
386 for(int i=0;i<md1.numParameters();i++) {
387 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
390 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
395 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
396 String id=nd.getIdentifier();
397 NameDescriptor base=nd.getBase();
399 return new NameNode(nd);
401 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
405 void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
406 /*Typecheck subexpressions
407 and get types for expressions*/
409 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
410 for(int i=0;i<min.numArgs();i++) {
411 ExpressionNode en=min.getArg(i);
412 checkExpressionNode(md,nametable,en,null);
413 tdarray[i]=en.getType();
415 TypeDescriptor typetolookin=null;
416 if (min.getExpression()!=null) {
417 checkExpressionNode(md,nametable,min.getExpression(),null);
418 typetolookin=min.getExpression().getType();
419 } else if (min.getBaseName()!=null) {
420 String rootname=min.getBaseName().getRoot();
421 if (nametable.get(rootname)!=null) {
422 //we have an expression
423 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
424 checkExpressionNode(md, nametable, min.getExpression(), null);
425 typetolookin=min.getExpression().getType();
428 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
430 throw new Error(min.getBaseName()+" undefined");
431 typetolookin=new TypeDescriptor(cd);
434 typetolookin=new TypeDescriptor(md.getClassDesc());
436 if (!typetolookin.isClass())
438 ClassDescriptor classtolookin=typetolookin.getClassDesc();
439 System.out.println("Method name="+min.getMethodName());
440 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
441 MethodDescriptor bestmd=null;
443 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
444 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
445 /* Need correct number of parameters */
446 if (min.numArgs()!=currmd.numParameters())
448 for(int i=0;i<min.numArgs();i++) {
449 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
452 /* Method okay so far */
456 if (isMoreSpecific(currmd,bestmd)) {
458 } else if (!isMoreSpecific(bestmd, currmd))
459 throw new Error("No method is most specific");
461 /* Is this more specific than bestmd */
465 throw new Error("No method found for :"+min.printNode(0));
466 min.setMethod(bestmd);
467 /* Check whether we need to set this parameter to implied this */
468 if (!bestmd.isStatic()) {
469 if (min.getExpression()==null) {
470 ExpressionNode en=new NameNode(new NameDescriptor("this"));
471 min.setExpression(en);
472 checkExpressionNode(md, nametable, min.getExpression(), null);
479 void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
480 checkExpressionNode(md, nametable, on.getLeft(), null);
481 if (on.getRight()!=null)
482 checkExpressionNode(md, nametable, on.getRight(), null);
483 TypeDescriptor ltd=on.getLeft().getType();
484 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
485 TypeDescriptor lefttype=null;
486 TypeDescriptor righttype=null;
487 Operation op=on.getOp();
490 case Operation.LOGIC_OR:
491 case Operation.LOGIC_AND:
492 if (!(ltd.isBoolean()&&rtd.isBoolean()))
496 on.setRightType(rtd);
497 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
500 case Operation.BIT_OR:
501 case Operation.BIT_XOR:
502 case Operation.BIT_AND:
503 // 5.6.2 Binary Numeric Promotion
504 //TODO unboxing of reference objects
505 if (ltd.isDouble()||rtd.isDouble())
507 else if (ltd.isFloat()||rtd.isFloat())
509 else if (ltd.isLong()||rtd.isLong())
510 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
512 lefttype=new TypeDescriptor(TypeDescriptor.INT);
515 on.setLeftType(lefttype);
516 on.setRightType(righttype);
517 on.setType(lefttype);
520 case Operation.EQUAL:
521 case Operation.NOTEQUAL:
522 // 5.6.2 Binary Numeric Promotion
523 //TODO unboxing of reference objects
524 if (ltd.isBoolean()||rtd.isBoolean()) {
525 if (!(ltd.isBoolean()&&rtd.isBoolean()))
527 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
528 } else if (ltd.isPtr()||rtd.isPtr()) {
529 if (!(ltd.isPtr()&&rtd.isPtr()))
533 } else if (ltd.isDouble()||rtd.isDouble())
534 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
535 else if (ltd.isFloat()||rtd.isFloat())
536 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
537 else if (ltd.isLong()||rtd.isLong())
538 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
540 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
542 on.setLeftType(lefttype);
543 on.setRightType(righttype);
544 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
553 // 5.6.2 Binary Numeric Promotion
554 //TODO unboxing of reference objects
555 if (!ltd.isNumber()||!rtd.isNumber())
558 if (ltd.isDouble()||rtd.isDouble())
559 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
560 else if (ltd.isFloat()||rtd.isFloat())
561 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
562 else if (ltd.isLong()||rtd.isLong())
563 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
565 lefttype=new TypeDescriptor(TypeDescriptor.INT);
567 on.setLeftType(lefttype);
568 on.setRightType(righttype);
569 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
573 //TODO: Need special case for strings eventually
580 // 5.6.2 Binary Numeric Promotion
581 //TODO unboxing of reference objects
582 if (!ltd.isNumber()||!rtd.isNumber())
583 throw new Error("Error in "+on.printNode(0));
585 if (ltd.isDouble()||rtd.isDouble())
586 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
587 else if (ltd.isFloat()||rtd.isFloat())
588 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
589 else if (ltd.isLong()||rtd.isLong())
590 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
592 lefttype=new TypeDescriptor(TypeDescriptor.INT);
594 on.setLeftType(lefttype);
595 on.setRightType(righttype);
596 on.setType(lefttype);
599 case Operation.LEFTSHIFT:
600 case Operation.RIGHTSHIFT:
601 if (!rtd.isIntegerType())
603 //5.6.1 Unary Numeric Promotion
604 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
605 righttype=new TypeDescriptor(TypeDescriptor.INT);
609 on.setRightType(righttype);
610 if (!ltd.isIntegerType())
612 case Operation.UNARYPLUS:
613 case Operation.UNARYMINUS:
614 case Operation.POSTINC:
615 case Operation.POSTDEC:
616 case Operation.PREINC:
617 case Operation.PREDEC:
620 //5.6.1 Unary Numeric Promotion
621 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
622 lefttype=new TypeDescriptor(TypeDescriptor.INT);
625 on.setLeftType(lefttype);
626 on.setType(lefttype);
635 if (!typeutil.isSuperorType(td, on.getType())) {
636 System.out.println(td);
637 System.out.println(on.getType());
638 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));