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 if (rn.getReturnExpression()!=null)
157 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
159 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
160 throw new Error("Need to return something for "+md);
163 void checkIfStatementNode(MethodDescriptor md, SymbolTable nametable, IfStatementNode isn) {
164 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
165 checkBlockNode(md, nametable, isn.getTrueBlock());
166 if (isn.getFalseBlock()!=null)
167 checkBlockNode(md, nametable, isn.getFalseBlock());
170 void checkExpressionNode(MethodDescriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
172 case Kind.AssignmentNode:
173 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
176 checkCastNode(md,nametable,(CastNode)en,td);
178 case Kind.CreateObjectNode:
179 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
181 case Kind.FieldAccessNode:
182 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
184 case Kind.ArrayAccessNode:
185 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
187 case Kind.LiteralNode:
188 checkLiteralNode(md,nametable,(LiteralNode)en,td);
190 case Kind.MethodInvokeNode:
191 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
194 checkNameNode(md,nametable,(NameNode)en,td);
197 checkOpNode(md,nametable,(OpNode)en,td);
203 void checkCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
204 /* Get type descriptor */
205 if (cn.getType()==null) {
206 NameDescriptor typenamed=cn.getTypeName().getName();
207 String typename=typenamed.toString();
208 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
212 /* Check the type descriptor */
213 TypeDescriptor cast_type=cn.getType();
214 checkTypeDescriptor(cast_type);
218 if (!typeutil.isSuperorType(td,cast_type))
219 throw new Error("Cast node returns "+cast_type+", but need "+td);
222 ExpressionNode en=cn.getExpression();
223 checkExpressionNode(md, nametable, en, null);
224 TypeDescriptor etd=en.getType();
225 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
228 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
231 /* Different branches */
232 /* TODO: change if add interfaces */
233 throw new Error("Cast will always fail\n"+cn.printNode(0));
236 void checkFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
237 ExpressionNode left=fan.getExpression();
238 checkExpressionNode(md,nametable,left,null);
239 TypeDescriptor ltd=left.getType();
240 String fieldname=fan.getFieldName();
241 FieldDescriptor fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
243 throw new Error("Unknown field "+fieldname);
246 if (!typeutil.isSuperorType(td,fan.getType()))
247 throw new Error("Field node returns "+fan.getType()+", but need "+td);
250 void checkArrayAccessNode(MethodDescriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
251 ExpressionNode left=aan.getExpression();
252 checkExpressionNode(md,nametable,left,null);
254 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
255 TypeDescriptor ltd=left.getType();
258 if (!typeutil.isSuperorType(td,aan.getType()))
259 throw new Error("Field node returns "+aan.getType()+", but need "+td);
262 void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
263 /* Resolve the type */
264 Object o=ln.getValue();
265 if (ln.getTypeString().equals("null")) {
266 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
267 } else if (o instanceof Integer) {
268 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
269 } else if (o instanceof Long) {
270 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
271 } else if (o instanceof Float) {
272 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
273 } else if (o instanceof Boolean) {
274 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
275 } else if (o instanceof Double) {
276 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
277 } else if (o instanceof Character) {
278 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
279 } else if (o instanceof String) {
280 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
284 if (!typeutil.isSuperorType(td,ln.getType()))
285 throw new Error("Field node returns "+ln.getType()+", but need "+td);
288 void checkNameNode(MethodDescriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
289 NameDescriptor nd=nn.getName();
290 if (nd.getBase()!=null) {
292 /* Rewrite NameNode */
293 ExpressionNode en=translateNameDescriptorintoExpression(nd);
294 nn.setExpression(en);
295 checkExpressionNode(md,nametable,en,td);
297 String varname=nd.toString();
298 Descriptor d=(Descriptor)nametable.get(varname);
300 throw new Error("Name "+varname+" undefined");
302 if (d instanceof VarDescriptor) {
303 nn.setVar((VarDescriptor)d);
304 } else if (d instanceof FieldDescriptor) {
305 nn.setField((FieldDescriptor)d);
306 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
309 if (!typeutil.isSuperorType(td,nn.getType()))
310 throw new Error("Field node returns "+nn.getType()+", but need "+td);
314 void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
315 checkExpressionNode(md, nametable, an.getSrc() ,td);
316 //TODO: Need check on validity of operation here
317 if (!((an.getDest() instanceof FieldAccessNode)||
318 (an.getDest() instanceof ArrayAccessNode)||
319 (an.getDest() instanceof NameNode)))
320 throw new Error("Bad lside in "+an.printNode(0));
321 checkExpressionNode(md, nametable, an.getDest(), null);
322 if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
323 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
327 void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
328 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
329 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
330 checkBlockNode(md, nametable, ln.getBody());
333 /* Link in the initializer naming environment */
334 BlockNode bn=ln.getInitializer();
335 bn.getVarTable().setParent(nametable);
336 for(int i=0;i<bn.size();i++) {
337 BlockStatementNode bsn=bn.get(i);
338 checkBlockStatementNode(md, bn.getVarTable(),bsn);
340 //check the condition
341 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
342 checkBlockNode(md, bn.getVarTable(), ln.getBody());
343 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
348 void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
349 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
350 for(int i=0;i<con.numArgs();i++) {
351 ExpressionNode en=con.getArg(i);
352 checkExpressionNode(md,nametable,en,null);
353 tdarray[i]=en.getType();
356 TypeDescriptor typetolookin=con.getType();
357 checkTypeDescriptor(typetolookin);
358 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
359 throw new Error("Can't allocate primitive type:"+con.printNode(0));
361 if (!typetolookin.isArray()) {
362 //Array's don't need constructor calls
363 ClassDescriptor classtolookin=typetolookin.getClassDesc();
364 System.out.println("Looking for "+typetolookin.getSymbol());
365 System.out.println(classtolookin.getMethodTable());
367 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
368 MethodDescriptor bestmd=null;
370 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
371 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
372 /* Need correct number of parameters */
373 System.out.println("Examining: "+currmd);
374 if (con.numArgs()!=currmd.numParameters())
376 for(int i=0;i<con.numArgs();i++) {
377 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
380 /* Method okay so far */
384 if (isMoreSpecific(currmd,bestmd)) {
386 } else if (!isMoreSpecific(bestmd, currmd))
387 throw new Error("No method is most specific");
389 /* Is this more specific than bestmd */
393 throw new Error("No method found for "+con.printNode(0));
394 con.setConstructor(bestmd);
399 /** Check to see if md1 is more specific than md2... Informally
400 if md2 could always be called given the arguments passed into
403 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
404 /* Checks if md1 is more specific than md2 */
405 if (md1.numParameters()!=md2.numParameters())
407 for(int i=0;i<md1.numParameters();i++) {
408 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
411 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
416 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
417 String id=nd.getIdentifier();
418 NameDescriptor base=nd.getBase();
420 return new NameNode(nd);
422 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
426 void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
427 /*Typecheck subexpressions
428 and get types for expressions*/
430 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
431 for(int i=0;i<min.numArgs();i++) {
432 ExpressionNode en=min.getArg(i);
433 checkExpressionNode(md,nametable,en,null);
434 tdarray[i]=en.getType();
436 TypeDescriptor typetolookin=null;
437 if (min.getExpression()!=null) {
438 checkExpressionNode(md,nametable,min.getExpression(),null);
439 typetolookin=min.getExpression().getType();
440 } else if (min.getBaseName()!=null) {
441 String rootname=min.getBaseName().getRoot();
442 if (nametable.get(rootname)!=null) {
443 //we have an expression
444 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
445 checkExpressionNode(md, nametable, min.getExpression(), null);
446 typetolookin=min.getExpression().getType();
449 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
451 throw new Error(min.getBaseName()+" undefined");
452 typetolookin=new TypeDescriptor(cd);
455 typetolookin=new TypeDescriptor(md.getClassDesc());
457 if (!typetolookin.isClass())
459 ClassDescriptor classtolookin=typetolookin.getClassDesc();
460 System.out.println("Method name="+min.getMethodName());
461 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
462 MethodDescriptor bestmd=null;
464 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
465 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
466 /* Need correct number of parameters */
467 if (min.numArgs()!=currmd.numParameters())
469 for(int i=0;i<min.numArgs();i++) {
470 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
473 /* Method okay so far */
477 if (isMoreSpecific(currmd,bestmd)) {
479 } else if (!isMoreSpecific(bestmd, currmd))
480 throw new Error("No method is most specific");
482 /* Is this more specific than bestmd */
486 throw new Error("No method found for :"+min.printNode(0));
487 min.setMethod(bestmd);
488 /* Check whether we need to set this parameter to implied this */
489 if (!bestmd.isStatic()) {
490 if (min.getExpression()==null) {
491 ExpressionNode en=new NameNode(new NameDescriptor("this"));
492 min.setExpression(en);
493 checkExpressionNode(md, nametable, min.getExpression(), null);
500 void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
501 checkExpressionNode(md, nametable, on.getLeft(), null);
502 if (on.getRight()!=null)
503 checkExpressionNode(md, nametable, on.getRight(), null);
504 TypeDescriptor ltd=on.getLeft().getType();
505 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
506 TypeDescriptor lefttype=null;
507 TypeDescriptor righttype=null;
508 Operation op=on.getOp();
511 case Operation.LOGIC_OR:
512 case Operation.LOGIC_AND:
513 if (!(ltd.isBoolean()&&rtd.isBoolean()))
517 on.setRightType(rtd);
518 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
521 case Operation.BIT_OR:
522 case Operation.BIT_XOR:
523 case Operation.BIT_AND:
524 // 5.6.2 Binary Numeric Promotion
525 //TODO unboxing of reference objects
526 if (ltd.isDouble()||rtd.isDouble())
528 else if (ltd.isFloat()||rtd.isFloat())
530 else if (ltd.isLong()||rtd.isLong())
531 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
533 lefttype=new TypeDescriptor(TypeDescriptor.INT);
536 on.setLeftType(lefttype);
537 on.setRightType(righttype);
538 on.setType(lefttype);
541 case Operation.EQUAL:
542 case Operation.NOTEQUAL:
543 // 5.6.2 Binary Numeric Promotion
544 //TODO unboxing of reference objects
545 if (ltd.isBoolean()||rtd.isBoolean()) {
546 if (!(ltd.isBoolean()&&rtd.isBoolean()))
548 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
549 } else if (ltd.isPtr()||rtd.isPtr()) {
550 if (!(ltd.isPtr()&&rtd.isPtr()))
554 } else if (ltd.isDouble()||rtd.isDouble())
555 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
556 else if (ltd.isFloat()||rtd.isFloat())
557 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
558 else if (ltd.isLong()||rtd.isLong())
559 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
561 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
563 on.setLeftType(lefttype);
564 on.setRightType(righttype);
565 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
574 // 5.6.2 Binary Numeric Promotion
575 //TODO unboxing of reference objects
576 if (!ltd.isNumber()||!rtd.isNumber())
579 if (ltd.isDouble()||rtd.isDouble())
580 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
581 else if (ltd.isFloat()||rtd.isFloat())
582 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
583 else if (ltd.isLong()||rtd.isLong())
584 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
586 lefttype=new TypeDescriptor(TypeDescriptor.INT);
588 on.setLeftType(lefttype);
589 on.setRightType(righttype);
590 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
594 //TODO: Need special case for strings eventually
601 // 5.6.2 Binary Numeric Promotion
602 //TODO unboxing of reference objects
603 if (!ltd.isNumber()||!rtd.isNumber())
604 throw new Error("Error in "+on.printNode(0));
606 if (ltd.isDouble()||rtd.isDouble())
607 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
608 else if (ltd.isFloat()||rtd.isFloat())
609 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
610 else if (ltd.isLong()||rtd.isLong())
611 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
613 lefttype=new TypeDescriptor(TypeDescriptor.INT);
615 on.setLeftType(lefttype);
616 on.setRightType(righttype);
617 on.setType(lefttype);
620 case Operation.LEFTSHIFT:
621 case Operation.RIGHTSHIFT:
622 if (!rtd.isIntegerType())
624 //5.6.1 Unary Numeric Promotion
625 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
626 righttype=new TypeDescriptor(TypeDescriptor.INT);
630 on.setRightType(righttype);
631 if (!ltd.isIntegerType())
633 case Operation.UNARYPLUS:
634 case Operation.UNARYMINUS:
635 case Operation.POSTINC:
636 case Operation.POSTDEC:
637 case Operation.PREINC:
638 case Operation.PREDEC:
641 //5.6.1 Unary Numeric Promotion
642 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
643 lefttype=new TypeDescriptor(TypeDescriptor.INT);
646 on.setLeftType(lefttype);
647 on.setType(lefttype);
656 if (!typeutil.isSuperorType(td, on.getType())) {
657 System.out.println(td);
658 System.out.println(on.getType());
659 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));