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.LiteralNode:
185 checkLiteralNode(md,nametable,(LiteralNode)en,td);
187 case Kind.MethodInvokeNode:
188 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
191 checkNameNode(md,nametable,(NameNode)en,td);
194 checkOpNode(md,nametable,(OpNode)en,td);
200 void checkCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
201 /* Get type descriptor */
202 if (cn.getType()==null) {
203 NameDescriptor typenamed=cn.getTypeName().getName();
204 String typename=typenamed.toString();
205 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
209 /* Check the type descriptor */
210 TypeDescriptor cast_type=cn.getType();
211 checkTypeDescriptor(cast_type);
215 if (!typeutil.isSuperorType(td,cast_type))
216 throw new Error("Cast node returns "+cast_type+", but need "+td);
219 ExpressionNode en=cn.getExpression();
220 checkExpressionNode(md, nametable, en, null);
221 TypeDescriptor etd=en.getType();
222 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
225 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
228 /* Different branches */
229 /* TODO: change if add interfaces */
230 throw new Error("Cast will always fail\n"+cn.printNode(0));
233 void checkFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
234 ExpressionNode left=fan.getExpression();
235 checkExpressionNode(md,nametable,left,null);
236 TypeDescriptor ltd=left.getType();
237 String fieldname=fan.getFieldName();
238 FieldDescriptor fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
240 throw new Error("Unknown field "+fieldname);
243 if (!typeutil.isSuperorType(td,fan.getType()))
244 throw new Error("Field node returns "+fan.getType()+", but need "+td);
247 void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
248 /* Resolve the type */
249 Object o=ln.getValue();
250 if (ln.getTypeString().equals("null")) {
251 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
252 } else if (o instanceof Integer) {
253 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
254 } else if (o instanceof Long) {
255 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
256 } else if (o instanceof Float) {
257 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
258 } else if (o instanceof Boolean) {
259 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
260 } else if (o instanceof Double) {
261 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
262 } else if (o instanceof Character) {
263 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
264 } else if (o instanceof String) {
265 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
269 if (!typeutil.isSuperorType(td,ln.getType()))
270 throw new Error("Field node returns "+ln.getType()+", but need "+td);
273 void checkNameNode(MethodDescriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
274 NameDescriptor nd=nn.getName();
275 if (nd.getBase()!=null) {
277 /* Rewrite NameNode */
278 ExpressionNode en=translateNameDescriptorintoExpression(nd);
279 nn.setExpression(en);
280 checkExpressionNode(md,nametable,en,td);
282 String varname=nd.toString();
283 Descriptor d=(Descriptor)nametable.get(varname);
285 throw new Error("Name "+varname+" undefined");
287 if (d instanceof VarDescriptor) {
288 nn.setVar((VarDescriptor)d);
289 } else if (d instanceof FieldDescriptor) {
290 nn.setField((FieldDescriptor)d);
291 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
294 if (!typeutil.isSuperorType(td,nn.getType()))
295 throw new Error("Field node returns "+nn.getType()+", but need "+td);
299 void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
300 checkExpressionNode(md, nametable, an.getSrc() ,td);
301 //TODO: Need check on validity of operation here
302 if (!((an.getDest() instanceof FieldAccessNode)||
303 (an.getDest() instanceof NameNode)))
304 throw new Error("Bad lside in "+an.printNode(0));
305 checkExpressionNode(md, nametable, an.getDest(), null);
306 if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
307 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
311 void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
312 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
313 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
314 checkBlockNode(md, nametable, ln.getBody());
317 /* Link in the initializer naming environment */
318 BlockNode bn=ln.getInitializer();
319 bn.getVarTable().setParent(nametable);
320 for(int i=0;i<bn.size();i++) {
321 BlockStatementNode bsn=bn.get(i);
322 checkBlockStatementNode(md, bn.getVarTable(),bsn);
324 //check the condition
325 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
326 checkBlockNode(md, bn.getVarTable(), ln.getBody());
327 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
332 void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
333 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
334 for(int i=0;i<con.numArgs();i++) {
335 ExpressionNode en=con.getArg(i);
336 checkExpressionNode(md,nametable,en,null);
337 tdarray[i]=en.getType();
340 TypeDescriptor typetolookin=con.getType();
341 checkTypeDescriptor(typetolookin);
342 if (!typetolookin.isClass())
345 ClassDescriptor classtolookin=typetolookin.getClassDesc();
346 System.out.println("Looking for "+typetolookin.getSymbol());
347 System.out.println(classtolookin.getMethodTable());
349 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
350 MethodDescriptor bestmd=null;
352 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
353 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
354 /* Need correct number of parameters */
355 System.out.println("Examining: "+currmd);
356 if (con.numArgs()!=currmd.numParameters())
358 for(int i=0;i<con.numArgs();i++) {
359 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
362 /* Method okay so far */
366 if (isMoreSpecific(currmd,bestmd)) {
368 } else if (!isMoreSpecific(bestmd, currmd))
369 throw new Error("No method is most specific");
371 /* Is this more specific than bestmd */
375 throw new Error("No method found for "+con.printNode(0));
376 con.setConstructor(bestmd);
382 /** Check to see if md1 is more specific than md2... Informally
383 if md2 could always be called given the arguments passed into
386 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
387 /* Checks if md1 is more specific than md2 */
388 if (md1.numParameters()!=md2.numParameters())
390 for(int i=0;i<md1.numParameters();i++) {
391 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
394 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
399 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
400 String id=nd.getIdentifier();
401 NameDescriptor base=nd.getBase();
403 return new NameNode(nd);
405 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
409 void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
410 /*Typecheck subexpressions
411 and get types for expressions*/
413 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
414 for(int i=0;i<min.numArgs();i++) {
415 ExpressionNode en=min.getArg(i);
416 checkExpressionNode(md,nametable,en,null);
417 tdarray[i]=en.getType();
419 TypeDescriptor typetolookin=null;
420 if (min.getExpression()!=null) {
421 checkExpressionNode(md,nametable,min.getExpression(),null);
422 typetolookin=min.getExpression().getType();
423 } else if (min.getBaseName()!=null) {
424 String rootname=min.getBaseName().getRoot();
425 if (nametable.get(rootname)!=null) {
426 //we have an expression
427 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
428 checkExpressionNode(md, nametable, min.getExpression(), null);
429 typetolookin=min.getExpression().getType();
432 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
434 throw new Error(min.getBaseName()+" undefined");
435 typetolookin=new TypeDescriptor(cd);
438 typetolookin=new TypeDescriptor(md.getClassDesc());
440 if (!typetolookin.isClass())
442 ClassDescriptor classtolookin=typetolookin.getClassDesc();
443 System.out.println("Method name="+min.getMethodName());
444 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
445 MethodDescriptor bestmd=null;
447 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
448 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
449 /* Need correct number of parameters */
450 if (min.numArgs()!=currmd.numParameters())
452 for(int i=0;i<min.numArgs();i++) {
453 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
456 /* Method okay so far */
460 if (isMoreSpecific(currmd,bestmd)) {
462 } else if (!isMoreSpecific(bestmd, currmd))
463 throw new Error("No method is most specific");
465 /* Is this more specific than bestmd */
469 throw new Error("No method found for :"+min.printNode(0));
470 min.setMethod(bestmd);
471 /* Check whether we need to set this parameter to implied this */
472 if (!bestmd.isStatic()) {
473 if (min.getExpression()==null) {
474 ExpressionNode en=new NameNode(new NameDescriptor("this"));
475 min.setExpression(en);
476 checkExpressionNode(md, nametable, min.getExpression(), null);
483 void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
484 checkExpressionNode(md, nametable, on.getLeft(), null);
485 if (on.getRight()!=null)
486 checkExpressionNode(md, nametable, on.getRight(), null);
487 TypeDescriptor ltd=on.getLeft().getType();
488 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
489 TypeDescriptor lefttype=null;
490 TypeDescriptor righttype=null;
491 Operation op=on.getOp();
494 case Operation.LOGIC_OR:
495 case Operation.LOGIC_AND:
496 if (!(ltd.isBoolean()&&rtd.isBoolean()))
500 on.setRightType(rtd);
501 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
504 case Operation.BIT_OR:
505 case Operation.BIT_XOR:
506 case Operation.BIT_AND:
507 // 5.6.2 Binary Numeric Promotion
508 //TODO unboxing of reference objects
509 if (ltd.isDouble()||rtd.isDouble())
511 else if (ltd.isFloat()||rtd.isFloat())
513 else if (ltd.isLong()||rtd.isLong())
514 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
516 lefttype=new TypeDescriptor(TypeDescriptor.INT);
519 on.setLeftType(lefttype);
520 on.setRightType(righttype);
521 on.setType(lefttype);
524 case Operation.EQUAL:
525 case Operation.NOTEQUAL:
526 // 5.6.2 Binary Numeric Promotion
527 //TODO unboxing of reference objects
528 if (ltd.isBoolean()||rtd.isBoolean()) {
529 if (!(ltd.isBoolean()&&rtd.isBoolean()))
531 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
532 } else if (ltd.isPtr()||rtd.isPtr()) {
533 if (!(ltd.isPtr()&&rtd.isPtr()))
537 } else if (ltd.isDouble()||rtd.isDouble())
538 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
539 else if (ltd.isFloat()||rtd.isFloat())
540 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
541 else if (ltd.isLong()||rtd.isLong())
542 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
544 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
546 on.setLeftType(lefttype);
547 on.setRightType(righttype);
548 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
557 // 5.6.2 Binary Numeric Promotion
558 //TODO unboxing of reference objects
559 if (!ltd.isNumber()||!rtd.isNumber())
562 if (ltd.isDouble()||rtd.isDouble())
563 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
564 else if (ltd.isFloat()||rtd.isFloat())
565 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
566 else if (ltd.isLong()||rtd.isLong())
567 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
569 lefttype=new TypeDescriptor(TypeDescriptor.INT);
571 on.setLeftType(lefttype);
572 on.setRightType(righttype);
573 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
577 //TODO: Need special case for strings eventually
584 // 5.6.2 Binary Numeric Promotion
585 //TODO unboxing of reference objects
586 if (!ltd.isNumber()||!rtd.isNumber())
587 throw new Error("Error in "+on.printNode(0));
589 if (ltd.isDouble()||rtd.isDouble())
590 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
591 else if (ltd.isFloat()||rtd.isFloat())
592 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
593 else if (ltd.isLong()||rtd.isLong())
594 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
596 lefttype=new TypeDescriptor(TypeDescriptor.INT);
598 on.setLeftType(lefttype);
599 on.setRightType(righttype);
600 on.setType(lefttype);
603 case Operation.LEFTSHIFT:
604 case Operation.RIGHTSHIFT:
605 if (!rtd.isIntegerType())
607 //5.6.1 Unary Numeric Promotion
608 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
609 righttype=new TypeDescriptor(TypeDescriptor.INT);
613 on.setRightType(righttype);
614 if (!ltd.isIntegerType())
616 case Operation.UNARYPLUS:
617 case Operation.UNARYMINUS:
618 case Operation.POSTINC:
619 case Operation.POSTDEC:
620 case Operation.PREINC:
621 case Operation.PREDEC:
624 //5.6.1 Unary Numeric Promotion
625 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
626 lefttype=new TypeDescriptor(TypeDescriptor.INT);
629 on.setLeftType(lefttype);
630 on.setType(lefttype);
639 if (!typeutil.isSuperorType(td, on.getType())) {
640 System.out.println(td);
641 System.out.println(on.getType());
642 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));