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 Double) {
255 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
256 } else if (o instanceof Character) {
257 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
258 } else if (o instanceof String) {
259 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
263 if (!typeutil.isSuperorType(td,ln.getType()))
264 throw new Error("Field node returns "+ln.getType()+", but need "+td);
267 void checkNameNode(MethodDescriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
268 NameDescriptor nd=nn.getName();
269 if (nd.getBase()!=null) {
271 /* Rewrite NameNode */
272 ExpressionNode en=translateNameDescriptorintoExpression(nd);
273 nn.setExpression(en);
274 checkExpressionNode(md,nametable,en,td);
276 String varname=nd.toString();
277 Descriptor d=(Descriptor)nametable.get(varname);
279 throw new Error("Name "+varname+" undefined");
281 if (d instanceof VarDescriptor) {
282 nn.setVar((VarDescriptor)d);
283 } else if (d instanceof FieldDescriptor) {
284 nn.setField((FieldDescriptor)d);
285 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
288 if (!typeutil.isSuperorType(td,nn.getType()))
289 throw new Error("Field node returns "+nn.getType()+", but need "+td);
293 void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
294 checkExpressionNode(md, nametable, an.getSrc() ,td);
295 //TODO: Need check on validity of operation here
296 if (!((an.getDest() instanceof FieldAccessNode)||
297 (an.getDest() instanceof NameNode)))
298 throw new Error("Bad lside in "+an.printNode(0));
299 checkExpressionNode(md, nametable, an.getDest(), null);
300 if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
301 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
305 void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
306 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
307 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
308 checkBlockNode(md, nametable, ln.getBody());
311 /* Link in the initializer naming environment */
312 BlockNode bn=ln.getInitializer();
313 bn.getVarTable().setParent(nametable);
314 for(int i=0;i<bn.size();i++) {
315 BlockStatementNode bsn=bn.get(i);
316 checkBlockStatementNode(md, bn.getVarTable(),bsn);
318 //check the condition
319 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
320 checkBlockNode(md, bn.getVarTable(), ln.getBody());
321 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
326 void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
327 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
328 for(int i=0;i<con.numArgs();i++) {
329 ExpressionNode en=con.getArg(i);
330 checkExpressionNode(md,nametable,en,null);
331 tdarray[i]=en.getType();
334 TypeDescriptor typetolookin=con.getType();
335 checkTypeDescriptor(typetolookin);
336 if (!typetolookin.isClass())
339 ClassDescriptor classtolookin=typetolookin.getClassDesc();
340 System.out.println("Looking for "+typetolookin.getSymbol());
341 System.out.println(classtolookin.getMethodTable());
343 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
344 MethodDescriptor bestmd=null;
346 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
347 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
348 /* Need correct number of parameters */
349 System.out.println("Examining: "+currmd);
350 if (con.numArgs()!=currmd.numParameters())
352 for(int i=0;i<con.numArgs();i++) {
353 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
356 /* Method okay so far */
360 if (isMoreSpecific(currmd,bestmd)) {
362 } else if (!isMoreSpecific(bestmd, currmd))
363 throw new Error("No method is most specific");
365 /* Is this more specific than bestmd */
369 throw new Error("No method found for "+con.printNode(0));
370 con.setConstructor(bestmd);
376 /** Check to see if md1 is more specific than md2... Informally
377 if md2 could always be called given the arguments passed into
380 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
381 /* Checks if md1 is more specific than md2 */
382 if (md1.numParameters()!=md2.numParameters())
384 for(int i=0;i<md1.numParameters();i++) {
385 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
388 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
393 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
394 String id=nd.getIdentifier();
395 NameDescriptor base=nd.getBase();
397 return new NameNode(nd);
399 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
403 void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
404 /*Typecheck subexpressions
405 and get types for expressions*/
407 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
408 for(int i=0;i<min.numArgs();i++) {
409 ExpressionNode en=min.getArg(i);
410 checkExpressionNode(md,nametable,en,null);
411 tdarray[i]=en.getType();
413 TypeDescriptor typetolookin=null;
414 if (min.getExpression()!=null) {
415 checkExpressionNode(md,nametable,min.getExpression(),null);
416 typetolookin=min.getExpression().getType();
417 } else if (min.getBaseName()!=null) {
418 String rootname=min.getBaseName().getRoot();
419 if (nametable.get(rootname)!=null) {
420 //we have an expression
421 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
422 checkExpressionNode(md, nametable, min.getExpression(), null);
423 typetolookin=min.getExpression().getType();
426 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
428 throw new Error(min.getBaseName()+" undefined");
429 typetolookin=new TypeDescriptor(cd);
432 typetolookin=new TypeDescriptor(md.getClassDesc());
434 if (!typetolookin.isClass())
436 ClassDescriptor classtolookin=typetolookin.getClassDesc();
437 System.out.println("Method name="+min.getMethodName());
438 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
439 MethodDescriptor bestmd=null;
441 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
442 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
443 /* Need correct number of parameters */
444 if (min.numArgs()!=currmd.numParameters())
446 for(int i=0;i<min.numArgs();i++) {
447 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
450 /* Method okay so far */
454 if (isMoreSpecific(currmd,bestmd)) {
456 } else if (!isMoreSpecific(bestmd, currmd))
457 throw new Error("No method is most specific");
459 /* Is this more specific than bestmd */
463 throw new Error("No method found for :"+min.printNode(0));
464 min.setMethod(bestmd);
465 /* Check whether we need to set this parameter to implied this */
466 if (!bestmd.isStatic()) {
467 if (min.getExpression()==null) {
468 ExpressionNode en=new NameNode(new NameDescriptor("this"));
469 min.setExpression(en);
470 checkExpressionNode(md, nametable, min.getExpression(), null);
477 void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
478 checkExpressionNode(md, nametable, on.getLeft(), null);
479 if (on.getRight()!=null)
480 checkExpressionNode(md, nametable, on.getRight(), null);
481 TypeDescriptor ltd=on.getLeft().getType();
482 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
483 TypeDescriptor lefttype=null;
484 TypeDescriptor righttype=null;
485 Operation op=on.getOp();
488 case Operation.LOGIC_OR:
489 case Operation.LOGIC_AND:
490 if (!(ltd.isBoolean()&&rtd.isBoolean()))
494 on.setRightType(rtd);
495 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
498 case Operation.BIT_OR:
499 case Operation.BIT_XOR:
500 case Operation.BIT_AND:
501 // 5.6.2 Binary Numeric Promotion
502 //TODO unboxing of reference objects
503 if (ltd.isDouble()||rtd.isDouble())
505 else if (ltd.isFloat()||rtd.isFloat())
507 else if (ltd.isLong()||rtd.isLong())
508 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
510 lefttype=new TypeDescriptor(TypeDescriptor.INT);
513 on.setLeftType(lefttype);
514 on.setRightType(righttype);
515 on.setType(lefttype);
518 case Operation.EQUAL:
519 case Operation.NOTEQUAL:
520 // 5.6.2 Binary Numeric Promotion
521 //TODO unboxing of reference objects
522 if (ltd.isBoolean()||rtd.isBoolean()) {
523 if (!(ltd.isBoolean()&&rtd.isBoolean()))
525 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
526 } else if (ltd.isPtr()||rtd.isPtr()) {
527 if (!(ltd.isPtr()&&rtd.isPtr()))
531 } else if (ltd.isDouble()||rtd.isDouble())
532 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
533 else if (ltd.isFloat()||rtd.isFloat())
534 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
535 else if (ltd.isLong()||rtd.isLong())
536 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
538 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
540 on.setLeftType(lefttype);
541 on.setRightType(righttype);
542 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
551 // 5.6.2 Binary Numeric Promotion
552 //TODO unboxing of reference objects
553 if (!ltd.isNumber()||!rtd.isNumber())
556 if (ltd.isDouble()||rtd.isDouble())
557 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
558 else if (ltd.isFloat()||rtd.isFloat())
559 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
560 else if (ltd.isLong()||rtd.isLong())
561 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
563 lefttype=new TypeDescriptor(TypeDescriptor.INT);
565 on.setLeftType(lefttype);
566 on.setRightType(righttype);
567 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
571 //TODO: Need special case for strings eventually
578 // 5.6.2 Binary Numeric Promotion
579 //TODO unboxing of reference objects
580 if (!ltd.isNumber()||!rtd.isNumber())
581 throw new Error("Error in "+on.printNode(0));
583 if (ltd.isDouble()||rtd.isDouble())
584 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
585 else if (ltd.isFloat()||rtd.isFloat())
586 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
587 else if (ltd.isLong()||rtd.isLong())
588 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
590 lefttype=new TypeDescriptor(TypeDescriptor.INT);
592 on.setLeftType(lefttype);
593 on.setRightType(righttype);
594 on.setType(lefttype);
597 case Operation.LEFTSHIFT:
598 case Operation.RIGHTSHIFT:
599 if (!rtd.isIntegerType())
601 //5.6.1 Unary Numeric Promotion
602 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
603 righttype=new TypeDescriptor(TypeDescriptor.INT);
607 on.setRightType(righttype);
608 if (!ltd.isIntegerType())
610 case Operation.UNARYPLUS:
611 case Operation.UNARYMINUS:
612 case Operation.POSTINC:
613 case Operation.POSTDEC:
614 case Operation.PREINC:
615 case Operation.PREDEC:
618 //5.6.1 Unary Numeric Promotion
619 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
620 lefttype=new TypeDescriptor(TypeDescriptor.INT);
623 on.setLeftType(lefttype);
624 on.setType(lefttype);
633 if (!typeutil.isSuperorType(td, on.getType())) {
634 System.out.println(td);
635 System.out.println(on.getType());
636 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));