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 String varname=nd.toString();
270 Descriptor d=(Descriptor)nametable.get(varname);
272 throw new Error("Name "+varname+" undefined");
274 if (d instanceof VarDescriptor) {
275 nn.setVar((VarDescriptor)d);
276 } else if (d instanceof FieldDescriptor) {
277 nn.setField((FieldDescriptor)d);
278 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
281 if (!typeutil.isSuperorType(td,nn.getType()))
282 throw new Error("Field node returns "+nn.getType()+", but need "+td);
285 void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
286 checkExpressionNode(md, nametable, an.getSrc() ,td);
287 //TODO: Need check on validity of operation here
288 if (!((an.getDest() instanceof FieldAccessNode)||
289 (an.getDest() instanceof NameNode)))
290 throw new Error("Bad lside in "+an.printNode(0));
291 checkExpressionNode(md, nametable, an.getDest(), null);
292 if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
293 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
297 void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
298 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
299 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
300 checkBlockNode(md, nametable, ln.getBody());
303 /* Link in the initializer naming environment */
304 BlockNode bn=ln.getInitializer();
305 bn.getVarTable().setParent(nametable);
306 for(int i=0;i<bn.size();i++) {
307 BlockStatementNode bsn=bn.get(i);
308 checkBlockStatementNode(md, bn.getVarTable(),bsn);
310 //check the condition
311 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
312 checkBlockNode(md, bn.getVarTable(), ln.getBody());
313 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
318 void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
319 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
320 for(int i=0;i<con.numArgs();i++) {
321 ExpressionNode en=con.getArg(i);
322 checkExpressionNode(md,nametable,en,null);
323 tdarray[i]=en.getType();
326 TypeDescriptor typetolookin=con.getType();
327 checkTypeDescriptor(typetolookin);
328 if (!typetolookin.isClass())
331 ClassDescriptor classtolookin=typetolookin.getClassDesc();
332 System.out.println("Looking for "+typetolookin.getSymbol());
333 System.out.println(classtolookin.getMethodTable());
335 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
336 MethodDescriptor bestmd=null;
338 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
339 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
340 /* Need correct number of parameters */
341 System.out.println("Examining: "+currmd);
342 if (con.numArgs()!=currmd.numParameters())
344 for(int i=0;i<con.numArgs();i++) {
345 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
348 /* Method okay so far */
352 if (isMoreSpecific(currmd,bestmd)) {
354 } else if (!isMoreSpecific(bestmd, currmd))
355 throw new Error("No method is most specific");
357 /* Is this more specific than bestmd */
361 throw new Error("No method found for "+con.printNode(0));
362 con.setConstructor(bestmd);
368 /** Check to see if md1 is more specific than md2... Informally
369 if md2 could always be called given the arguments passed into
372 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
373 /* Checks if md1 is more specific than md2 */
374 if (md1.numParameters()!=md2.numParameters())
376 for(int i=0;i<md1.numParameters();i++) {
377 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
380 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
385 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
386 String id=nd.getIdentifier();
387 NameDescriptor base=nd.getBase();
389 return new NameNode(nd);
391 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
395 void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
396 /*Typecheck subexpressions
397 and get types for expressions*/
399 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
400 for(int i=0;i<min.numArgs();i++) {
401 ExpressionNode en=min.getArg(i);
402 checkExpressionNode(md,nametable,en,null);
403 tdarray[i]=en.getType();
405 TypeDescriptor typetolookin=null;
406 if (min.getExpression()!=null) {
407 checkExpressionNode(md,nametable,min.getExpression(),null);
408 typetolookin=min.getExpression().getType();
409 } else if (min.getBaseName()!=null) {
410 String rootname=min.getBaseName().getRoot();
411 if (nametable.get(rootname)!=null) {
412 //we have an expression
413 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
414 checkExpressionNode(md, nametable, min.getExpression(), null);
415 typetolookin=min.getExpression().getType();
418 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
420 throw new Error(min.getBaseName()+" undefined");
421 typetolookin=new TypeDescriptor(cd);
424 typetolookin=new TypeDescriptor(md.getClassDesc());
426 if (!typetolookin.isClass())
428 ClassDescriptor classtolookin=typetolookin.getClassDesc();
429 System.out.println("Method name="+min.getMethodName());
430 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
431 MethodDescriptor bestmd=null;
433 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
434 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
435 /* Need correct number of parameters */
436 if (min.numArgs()!=currmd.numParameters())
438 for(int i=0;i<min.numArgs();i++) {
439 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
442 /* Method okay so far */
446 if (isMoreSpecific(currmd,bestmd)) {
448 } else if (!isMoreSpecific(bestmd, currmd))
449 throw new Error("No method is most specific");
451 /* Is this more specific than bestmd */
455 throw new Error("No method found for :"+min.printNode(0));
456 min.setMethod(bestmd);
460 void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
461 checkExpressionNode(md, nametable, on.getLeft(), null);
462 if (on.getRight()!=null)
463 checkExpressionNode(md, nametable, on.getRight(), null);
464 TypeDescriptor ltd=on.getLeft().getType();
465 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
466 TypeDescriptor lefttype=null;
467 TypeDescriptor righttype=null;
468 Operation op=on.getOp();
471 case Operation.LOGIC_OR:
472 case Operation.LOGIC_AND:
473 if (!(ltd.isBoolean()&&rtd.isBoolean()))
477 on.setRightType(rtd);
478 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
481 case Operation.BIT_OR:
482 case Operation.BIT_XOR:
483 case Operation.BIT_AND:
484 // 5.6.2 Binary Numeric Promotion
485 //TODO unboxing of reference objects
486 if (ltd.isDouble()||rtd.isDouble())
488 else if (ltd.isFloat()||rtd.isFloat())
490 else if (ltd.isLong()||rtd.isLong())
491 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
493 lefttype=new TypeDescriptor(TypeDescriptor.INT);
496 on.setLeftType(lefttype);
497 on.setRightType(righttype);
498 on.setType(lefttype);
501 case Operation.EQUAL:
502 case Operation.NOTEQUAL:
503 // 5.6.2 Binary Numeric Promotion
504 //TODO unboxing of reference objects
505 if (ltd.isBoolean()||rtd.isBoolean()) {
506 if (!(ltd.isBoolean()&&rtd.isBoolean()))
508 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
509 } else if (ltd.isPtr()||rtd.isPtr()) {
510 if (!(ltd.isPtr()&&rtd.isPtr()))
514 } else if (ltd.isDouble()||rtd.isDouble())
515 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
516 else if (ltd.isFloat()||rtd.isFloat())
517 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
518 else if (ltd.isLong()||rtd.isLong())
519 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
521 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
523 on.setLeftType(lefttype);
524 on.setRightType(righttype);
525 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
534 // 5.6.2 Binary Numeric Promotion
535 //TODO unboxing of reference objects
536 if (!ltd.isNumber()||!rtd.isNumber())
539 if (ltd.isDouble()||rtd.isDouble())
540 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
541 else if (ltd.isFloat()||rtd.isFloat())
542 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
543 else if (ltd.isLong()||rtd.isLong())
544 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
546 lefttype=new TypeDescriptor(TypeDescriptor.INT);
548 on.setLeftType(lefttype);
549 on.setRightType(righttype);
550 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
554 //TODO: Need special case for strings eventually
561 // 5.6.2 Binary Numeric Promotion
562 //TODO unboxing of reference objects
563 if (!ltd.isNumber()||!rtd.isNumber())
564 throw new Error("Error in "+on.printNode(0));
566 if (ltd.isDouble()||rtd.isDouble())
567 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
568 else if (ltd.isFloat()||rtd.isFloat())
569 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
570 else if (ltd.isLong()||rtd.isLong())
571 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
573 lefttype=new TypeDescriptor(TypeDescriptor.INT);
575 on.setLeftType(lefttype);
576 on.setRightType(righttype);
577 on.setType(lefttype);
580 case Operation.LEFTSHIFT:
581 case Operation.RIGHTSHIFT:
582 if (!rtd.isIntegerType())
584 //5.6.1 Unary Numeric Promotion
585 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
586 righttype=new TypeDescriptor(TypeDescriptor.INT);
590 on.setRightType(righttype);
591 if (!ltd.isIntegerType())
593 case Operation.UNARYPLUS:
594 case Operation.UNARYMINUS:
595 case Operation.POSTINC:
596 case Operation.POSTDEC:
597 case Operation.PREINC:
598 case Operation.PREDEC:
601 //5.6.1 Unary Numeric Promotion
602 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
603 lefttype=new TypeDescriptor(TypeDescriptor.INT);
606 on.setLeftType(lefttype);
607 on.setType(lefttype);
616 if (!typeutil.isSuperorType(td, on.getType())) {
617 System.out.println(td);
618 System.out.println(on.getType());
619 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));