1 package Analysis.SSJava;
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
8 import java.util.StringTokenizer;
9 import java.util.Vector;
11 import IR.AnnotationDescriptor;
12 import IR.ClassDescriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.NameDescriptor;
19 import IR.SymbolTable;
20 import IR.TypeDescriptor;
21 import IR.VarDescriptor;
22 import IR.Tree.ArrayAccessNode;
23 import IR.Tree.ArrayInitializerNode;
24 import IR.Tree.AssignmentNode;
25 import IR.Tree.BlockExpressionNode;
26 import IR.Tree.BlockNode;
27 import IR.Tree.BlockStatementNode;
28 import IR.Tree.CreateObjectNode;
29 import IR.Tree.DeclarationNode;
30 import IR.Tree.ExpressionNode;
31 import IR.Tree.FieldAccessNode;
32 import IR.Tree.IfStatementNode;
34 import IR.Tree.LiteralNode;
35 import IR.Tree.LoopNode;
36 import IR.Tree.MethodInvokeNode;
37 import IR.Tree.NameNode;
38 import IR.Tree.OpNode;
39 import IR.Tree.SubBlockNode;
40 import IR.Tree.TertiaryNode;
43 public class FlowDownCheck {
47 Hashtable<TypeDescriptor, Location> td2loc; // mapping from 'type descriptor'
49 Hashtable<String, ClassDescriptor> id2cd; // mapping from 'locID' to 'class
52 public FlowDownCheck(State state) {
54 this.toanalyze = new HashSet();
55 this.td2loc = new Hashtable<TypeDescriptor, Location>();
60 id2cd = new Hashtable<String, ClassDescriptor>();
61 Hashtable cd2lattice = state.getCd2LocationOrder();
63 Set cdSet = cd2lattice.keySet();
64 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
65 ClassDescriptor cd = (ClassDescriptor) iterator.next();
66 Lattice<String> lattice = (Lattice<String>) cd2lattice.get(cd);
68 Set<String> locIdSet = lattice.getKeySet();
69 for (Iterator iterator2 = locIdSet.iterator(); iterator2.hasNext();) {
70 String locID = (String) iterator2.next();
77 public void flowDownCheck() {
78 SymbolTable classtable = state.getClassSymbolTable();
80 // phase 1 : checking declaration node and creating mapping of 'type
81 // desciptor' & 'location'
82 toanalyze.addAll(classtable.getValueSet());
83 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
84 while (!toanalyze.isEmpty()) {
85 Object obj = toanalyze.iterator().next();
86 ClassDescriptor cd = (ClassDescriptor) obj;
88 if (cd.isClassLibrary()) {
89 // doesn't care about class libraries now
92 checkDeclarationInClass(cd);
93 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
94 MethodDescriptor md = (MethodDescriptor) method_it.next();
96 checkDeclarationInMethodBody(cd, md);
98 System.out.println("Error in " + md);
104 // post-processing for delta location
105 // for a nested delta location, assigning a concrete reference to delta
107 Set<TypeDescriptor> tdSet = td2loc.keySet();
108 for (Iterator iterator = tdSet.iterator(); iterator.hasNext();) {
109 TypeDescriptor td = (TypeDescriptor) iterator.next();
110 Location loc = td2loc.get(td);
112 if (loc.getType() == Location.DELTA) {
113 // if it contains delta reference pointing to another location element
114 CompositeLocation compLoc = (CompositeLocation) loc;
116 Location locElement = compLoc.getTuple().at(0);
117 assert (locElement instanceof DeltaLocation);
119 DeltaLocation delta = (DeltaLocation) locElement;
120 TypeDescriptor refType = delta.getRefLocationId();
121 if (refType != null) {
122 Location refLoc = td2loc.get(refType);
124 assert (refLoc instanceof CompositeLocation);
125 CompositeLocation refCompLoc = (CompositeLocation) refLoc;
127 assert (refCompLoc.getTuple().at(0) instanceof DeltaLocation);
128 DeltaLocation refDelta = (DeltaLocation) refCompLoc.getTuple().at(0);
130 delta.addDeltaOperand(refDelta);
131 // compLoc.addLocation(refDelta);
137 // phase2 : checking assignments
138 toanalyze.addAll(classtable.getValueSet());
139 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
140 while (!toanalyze.isEmpty()) {
141 Object obj = toanalyze.iterator().next();
142 ClassDescriptor cd = (ClassDescriptor) obj;
143 toanalyze.remove(cd);
144 if (cd.isClassLibrary()) {
145 // doesn't care about class libraries now
149 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
150 MethodDescriptor md = (MethodDescriptor) method_it.next();
152 checkMethodBody(cd, md);
154 System.out.println("Error in " + md);
162 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
163 BlockNode bn = state.getMethodBody(md);
164 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
167 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
168 bn.getVarTable().setParent(nametable);
169 for (int i = 0; i < bn.size(); i++) {
170 BlockStatementNode bsn = bn.get(i);
171 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
175 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
176 BlockStatementNode bsn) {
178 switch (bsn.kind()) {
179 case Kind.SubBlockNode:
180 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
182 case Kind.DeclarationNode:
183 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
186 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
191 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
193 if (ln.getType() == LoopNode.FORLOOP) {
194 // check for loop case
195 ClassDescriptor cd = md.getClassDesc();
196 BlockNode bn = ln.getInitializer();
197 for (int i = 0; i < bn.size(); i++) {
198 BlockStatementNode bsn = bn.get(i);
199 checkDeclarationInBlockStatementNode(md, nametable, bsn);
204 checkDeclarationInBlockNode(md, nametable, ln.getBody());
207 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
208 BlockNode bn = state.getMethodBody(md);
209 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
212 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
214 // it will return the lowest location in the block node
215 CompositeLocation lowestLoc = null;
216 for (int i = 0; i < bn.size(); i++) {
217 BlockStatementNode bsn = bn.get(i);
218 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
220 if (lowestLoc == null) {
223 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc, md.getClassDesc())) {
231 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
232 SymbolTable nametable, BlockStatementNode bsn) {
234 switch (bsn.kind()) {
235 case Kind.BlockExpressionNode:
236 return checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
238 case Kind.DeclarationNode:
239 return checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
241 case Kind.IfStatementNode:
242 return checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
245 return checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
247 case Kind.ReturnNode:
248 // checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
251 case Kind.SubBlockNode:
252 return checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
254 // case Kind.ContinueBreakNode:
255 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
262 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
265 ClassDescriptor cd = md.getClassDesc();
266 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
268 CompositeLocation condLoc =
269 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation(
271 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
273 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc, cd)) {
274 // loop condition should be higher than loop body
276 "The location of the while-condition statement is lower than the loop body at "
277 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
283 // check for loop case
284 BlockNode bn = ln.getInitializer();
286 // calculate glb location of condition and update statements
287 CompositeLocation condLoc =
288 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
289 new CompositeLocation(cd));
290 CompositeLocation updateLoc =
291 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
293 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
294 glbInputSet.add(condLoc);
295 glbInputSet.add(updateLoc);
297 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
298 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
300 if (blockLoc == null) {
301 // when there is no statement in the loop body
302 return glbLocOfForLoopCond;
305 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc, cd)) {
307 "The location of the for-condition statement is lower than the for-loop body at "
308 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
315 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
316 SymbolTable nametable, SubBlockNode sbn) {
317 return checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
320 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
321 SymbolTable nametable, IfStatementNode isn) {
323 ClassDescriptor localCD = md.getClassDesc();
324 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
326 CompositeLocation condLoc = new CompositeLocation(localCD);
327 condLoc = checkLocationFromExpressionNode(md, nametable, isn.getCondition(), condLoc);
328 glbInputSet.add(condLoc);
330 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
331 glbInputSet.add(locTrueBlock);
333 // here, the location of conditional block should be higher than the
334 // location of true/false blocks
336 if (!CompositeLattice.isGreaterThan(condLoc, locTrueBlock, localCD)) {
339 "The location of the if-condition statement is lower than the conditional block at "
340 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
343 if (isn.getFalseBlock() != null) {
344 CompositeLocation locFalseBlock =
345 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
346 glbInputSet.add(locFalseBlock);
347 System.out.println(isn.getFalseBlock().printNode(0) + ":::falseLoc=" + locFalseBlock);
349 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock, localCD)) {
352 "The location of the if-condition statement is lower than the conditional block at "
353 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
358 // return GLB location of condition, true, and false block
359 CompositeLocation glbLoc = CompositeLattice.calculateGLB(localCD, glbInputSet, localCD);
364 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
365 SymbolTable nametable, DeclarationNode dn) {
366 VarDescriptor vd = dn.getVarDescriptor();
368 Location destLoc = td2loc.get(vd.getType());
370 ClassDescriptor localCD = md.getClassDesc();
371 if (dn.getExpression() != null) {
372 CompositeLocation expressionLoc = new CompositeLocation(localCD);
374 checkLocationFromExpressionNode(md, nametable, dn.getExpression(), expressionLoc);
376 if (expressionLoc != null) {
378 // checking location order
379 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) {
380 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
381 + " does not respect location hierarchy on the assignment " + dn.printNode(0));
384 return expressionLoc;
392 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
394 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
397 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
398 SymbolTable nametable, BlockExpressionNode ben) {
399 return checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
402 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
403 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
407 case Kind.AssignmentNode:
408 return checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
410 case Kind.FieldAccessNode:
411 return checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
414 return checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
417 return checkLocationFromOpNode(md, nametable, (OpNode) en);
419 case Kind.CreateObjectNode:
420 return checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
422 case Kind.ArrayAccessNode:
423 return checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
425 case Kind.LiteralNode:
426 return checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
428 case Kind.MethodInvokeNode:
429 return checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en);
431 case Kind.TertiaryNode:
432 return checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
434 // case Kind.InstanceOfNode:
435 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
438 // case Kind.ArrayInitializerNode:
439 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
443 // case Kind.ClassTypeNode:
444 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
447 // case Kind.OffsetNode:
448 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
458 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
459 SymbolTable nametable, TertiaryNode tn) {
460 ClassDescriptor cd = md.getClassDesc();
462 CompositeLocation condLoc =
463 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(cd));
464 CompositeLocation trueLoc =
465 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(cd));
466 CompositeLocation falseLoc =
467 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(cd));
469 // check if condLoc is higher than trueLoc & falseLoc
470 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc, cd)) {
472 "The location of the condition expression is lower than the true expression at "
473 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
476 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc, cd)) {
478 "The location of the condition expression is lower than the true expression at "
479 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
482 // then, return glb of trueLoc & falseLoc
483 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
484 glbInputSet.add(trueLoc);
485 glbInputSet.add(falseLoc);
487 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
490 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
491 SymbolTable nametable, MethodInvokeNode min) {
493 // all arguments should be higher than the location of return value
494 ClassDescriptor cd = md.getClassDesc();
496 // first, calculate glb of arguments
497 Set<CompositeLocation> argLocSet = new HashSet<CompositeLocation>();
498 for (int i = 0; i < min.numArgs(); i++) {
499 ExpressionNode en = min.getArg(i);
500 CompositeLocation argLoc =
501 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
502 argLocSet.add(argLoc);
505 if (argLocSet.size() > 0) {
506 CompositeLocation argGLBLoc = CompositeLattice.calculateGLB(cd, argLocSet, cd);
509 // if there are no arguments,
510 CompositeLocation returnLoc = new CompositeLocation(cd);
511 returnLoc.addLocation(Location.createTopLocation(cd));
516 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
517 SymbolTable nametable, ArrayAccessNode aan) {
519 // return glb location of array itself and index
521 ClassDescriptor cd = md.getClassDesc();
523 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
525 CompositeLocation arrayLoc =
526 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation(
528 glbInputSet.add(arrayLoc);
530 CompositeLocation indexLoc =
531 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(cd));
532 glbInputSet.add(indexLoc);
534 CompositeLocation glbLoc = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
538 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
539 SymbolTable nametable, CreateObjectNode con) {
541 ClassDescriptor cd = md.getClassDesc();
544 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
545 for (int i = 0; i < con.numArgs(); i++) {
546 ExpressionNode en = con.getArg(i);
547 CompositeLocation argLoc =
548 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
549 glbInputSet.add(argLoc);
552 // check array initializers
553 // if ((con.getArrayInitializer() != null)) {
554 // checkLocationFromArrayInitializerNode(md, nametable,
555 // con.getArrayInitializer());
558 if (glbInputSet.size() > 0) {
559 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
562 CompositeLocation compLoc = new CompositeLocation(cd);
563 compLoc.addLocation(Location.createTopLocation(cd));
568 private CompositeLocation checkLocationFromArrayInitializerNode(MethodDescriptor md,
569 SymbolTable nametable, ArrayInitializerNode ain) {
571 ClassDescriptor cd = md.getClassDesc();
572 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
573 for (int i = 0; i < ain.numVarInitializers(); ++i) {
574 checkLocationFromExpressionNode(md, nametable, ain.getVarInitializer(i),
575 new CompositeLocation(cd));
576 vec_type.add(ain.getVarInitializer(i).getType());
582 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
585 ClassDescriptor cd = md.getClassDesc();
586 CompositeLocation leftLoc = new CompositeLocation(cd);
587 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
589 CompositeLocation rightLoc = new CompositeLocation(cd);
590 if (on.getRight() != null) {
591 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
594 System.out.println("checking op node=" + on.printNode(0));
595 System.out.println("left loc=" + leftLoc + " from " + on.getLeft().getClass());
596 System.out.println("right loc=" + rightLoc);
598 Operation op = on.getOp();
600 switch (op.getOp()) {
602 case Operation.UNARYPLUS:
603 case Operation.UNARYMINUS:
604 case Operation.LOGIC_NOT:
606 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
609 case Operation.LOGIC_OR:
610 case Operation.LOGIC_AND:
612 case Operation.BIT_OR:
613 case Operation.BIT_XOR:
614 case Operation.BIT_AND:
615 case Operation.ISAVAILABLE:
616 case Operation.EQUAL:
617 case Operation.NOTEQUAL:
627 case Operation.LEFTSHIFT:
628 case Operation.RIGHTSHIFT:
629 case Operation.URIGHTSHIFT:
631 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
632 inputSet.add(leftLoc);
633 inputSet.add(rightLoc);
634 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(cd, inputSet, cd);
638 throw new Error(op.toString());
645 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
646 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
648 // literal value has the top location so that value can be flowed into any
650 Location literalLoc = Location.createTopLocation(md.getClassDesc());
651 loc.addLocation(literalLoc);
656 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
657 NameNode nn, CompositeLocation loc) {
659 NameDescriptor nd = nn.getName();
660 if (nd.getBase() != null) {
661 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
664 String varname = nd.toString();
665 Descriptor d = (Descriptor) nametable.get(varname);
667 Location localLoc = null;
668 if (d instanceof VarDescriptor) {
669 VarDescriptor vd = (VarDescriptor) d;
670 localLoc = td2loc.get(vd.getType());
671 } else if (d instanceof FieldDescriptor) {
672 FieldDescriptor fd = (FieldDescriptor) d;
673 localLoc = td2loc.get(fd.getType());
675 assert (localLoc != null);
677 if (localLoc instanceof CompositeLocation) {
678 loc = (CompositeLocation) localLoc;
680 loc.addLocation(localLoc);
687 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
688 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
689 FieldDescriptor fd = fan.getField();
690 Location fieldLoc = td2loc.get(fd.getType());
691 loc.addLocation(fieldLoc);
693 ExpressionNode left = fan.getExpression();
694 return checkLocationFromExpressionNode(md, nametable, left, loc);
697 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
698 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
700 ClassDescriptor cd = md.getClassDesc();
702 boolean postinc = true;
703 if (an.getOperation().getBaseOp() == null
704 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
705 .getBaseOp().getOp() != Operation.POSTDEC))
708 CompositeLocation destLocation =
709 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(cd));
711 CompositeLocation srcLocation = new CompositeLocation(cd);
713 srcLocation = new CompositeLocation(cd);
714 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
716 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, cd)) {
717 throw new Error("The value flow from " + srcLocation + " to " + destLocation
718 + " does not respect location hierarchy on the assignment " + an.printNode(0));
722 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
724 if (!((Set<String>) state.getCd2LocationPropertyMap().get(cd)).contains(destLocation
725 .getLocation(cd).getLocIdentifier())) {
726 throw new Error("Location " + destLocation + " is not allowed to have spinning values at "
727 + cd.getSourceFileName() + ":" + an.getNumLine());
735 private Location createCompositeLocation(FieldAccessNode fan, Location loc) {
737 FieldDescriptor field = fan.getField();
738 ClassDescriptor fieldCD = field.getClassDescriptor();
740 assert (field.getType().getAnnotationMarkers().size() == 1);
742 AnnotationDescriptor locAnnotation = field.getType().getAnnotationMarkers().elementAt(0);
743 if (locAnnotation.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
750 // Location localLoc=new Location(field.getClassDescriptor(),field.get)
752 // System.out.println("creatingComposite's field="+field+" localLoc="+localLoc);
753 ExpressionNode leftNode = fan.getExpression();
754 System.out.println("creatingComposite's leftnode=" + leftNode.printNode(0));
759 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
761 ClassDescriptor cd = md.getClassDesc();
762 VarDescriptor vd = dn.getVarDescriptor();
763 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
765 // currently enforce every variable to have corresponding location
766 if (annotationVec.size() == 0) {
767 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
768 + md.getSymbol() + " of the class " + cd.getSymbol());
771 if (annotationVec.size() > 1) {
772 // variable can have at most one location
773 throw new Error(vd.getSymbol() + " has more than one location.");
776 AnnotationDescriptor ad = annotationVec.elementAt(0);
778 if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) {
780 // check if location is defined
781 String locationID = ad.getMarker();
782 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
784 if (lattice == null || (!lattice.containsKey(locationID))) {
785 throw new Error("Location " + locationID
786 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
789 Location loc = new Location(cd, locationID);
790 td2loc.put(vd.getType(), loc);
792 } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
793 if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
795 CompositeLocation compLoc = new CompositeLocation(cd);
797 if (ad.getData().length() == 0) {
798 throw new Error("Delta function of " + vd.getSymbol() + " does not have any locations: "
799 + cd.getSymbol() + ".");
802 String deltaStr = ad.getData();
803 if (deltaStr.startsWith("LOC(")) {
805 if (!deltaStr.endsWith(")")) {
806 throw new Error("The declaration of the delta location is wrong at "
807 + cd.getSourceFileName() + ":" + dn.getNumLine());
809 String locationOperand = deltaStr.substring(4, deltaStr.length() - 1);
811 nametable.get(locationOperand);
812 Descriptor d = (Descriptor) nametable.get(locationOperand);
814 if (d instanceof VarDescriptor) {
815 VarDescriptor varDescriptor = (VarDescriptor) d;
816 DeltaLocation deltaLoc = new DeltaLocation(cd, varDescriptor.getType());
817 // td2loc.put(vd.getType(), compLoc);
818 compLoc.addLocation(deltaLoc);
819 } else if (d instanceof FieldDescriptor) {
820 throw new Error("Applying delta operation to the field " + locationOperand
821 + " is not allowed at " + cd.getSourceFileName() + ":" + dn.getNumLine());
824 StringTokenizer token = new StringTokenizer(deltaStr, ",");
825 DeltaLocation deltaLoc = new DeltaLocation(cd);
827 while (token.hasMoreTokens()) {
828 String deltaOperand = token.nextToken();
829 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
830 if (deltaCD == null) {
831 // delta operand is not defined in the location hierarchy
832 throw new Error("Delta operand '" + deltaOperand + "' of declaration node '" + vd
833 + "' is not defined by location hierarchies.");
836 Location loc = new Location(deltaCD, deltaOperand);
837 deltaLoc.addDeltaOperand(loc);
839 compLoc.addLocation(deltaLoc);
843 td2loc.put(vd.getType(), compLoc);
844 System.out.println("vd=" + vd + " is assigned by " + compLoc);
851 private void checkClass(ClassDescriptor cd) {
852 // Check to see that methods respects ss property
853 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
854 MethodDescriptor md = (MethodDescriptor) method_it.next();
855 checkMethodDeclaration(cd, md);
859 private void checkDeclarationInClass(ClassDescriptor cd) {
860 // Check to see that fields are okay
861 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
862 FieldDescriptor fd = (FieldDescriptor) field_it.next();
863 checkFieldDeclaration(cd, fd);
867 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
871 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
873 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
875 // currently enforce every variable to have corresponding location
876 if (annotationVec.size() == 0) {
877 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
881 if (annotationVec.size() > 1) {
882 // variable can have at most one location
883 throw new Error("Field " + fd.getSymbol() + " of class " + cd
884 + " has more than one location.");
887 // check if location is defined
888 AnnotationDescriptor ad = annotationVec.elementAt(0);
889 if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) {
890 String locationID = annotationVec.elementAt(0).getMarker();
891 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
893 if (lattice == null || (!lattice.containsKey(locationID))) {
894 throw new Error("Location " + locationID
895 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
898 Location localLoc = new Location(cd, locationID);
899 td2loc.put(fd.getType(), localLoc);
901 } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
902 if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
904 if (ad.getData().length() == 0) {
905 throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: "
906 + cd.getSymbol() + ".");
909 CompositeLocation compLoc = new CompositeLocation(cd);
910 DeltaLocation deltaLoc = new DeltaLocation(cd);
912 StringTokenizer token = new StringTokenizer(ad.getData(), ",");
913 while (token.hasMoreTokens()) {
914 String deltaOperand = token.nextToken();
915 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
916 if (deltaCD == null) {
917 // delta operand is not defined in the location hierarchy
918 throw new Error("Delta operand '" + deltaOperand + "' of field node '" + fd
919 + "' is not defined by location hierarchies.");
922 Location loc = new Location(deltaCD, deltaOperand);
923 deltaLoc.addDeltaOperand(loc);
925 compLoc.addLocation(deltaLoc);
926 td2loc.put(fd.getType(), compLoc);
933 static class CompositeLattice {
935 public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) {
937 // System.out.println("isGreaterThan=" + loc1 + " ? " + loc2);
938 CompositeLocation compLoc1;
939 CompositeLocation compLoc2;
941 if (loc1 instanceof CompositeLocation) {
942 compLoc1 = (CompositeLocation) loc1;
944 // create a bogus composite location for a single location
945 compLoc1 = new CompositeLocation(loc1.getClassDescriptor());
946 compLoc1.addLocation(loc1);
949 if (loc2 instanceof CompositeLocation) {
950 compLoc2 = (CompositeLocation) loc2;
952 // create a bogus composite location for a single location
953 compLoc2 = new CompositeLocation(loc2.getClassDescriptor());
954 compLoc2.addLocation(loc2);
957 // comparing two composite locations
958 // System.out.println("compare base location=" + compLoc1 + " ? " +
961 int baseCompareResult = compareBaseLocationSet(compLoc1, compLoc2, priorityCD);
962 if (baseCompareResult == ComparisonResult.EQUAL) {
963 if (compareDelta(compLoc1, compLoc2) == ComparisonResult.GREATER) {
968 } else if (baseCompareResult == ComparisonResult.GREATER) {
976 private static int compareDelta(CompositeLocation compLoc1, CompositeLocation compLoc2) {
977 if (compLoc1.getNumofDelta() < compLoc2.getNumofDelta()) {
978 return ComparisonResult.GREATER;
980 return ComparisonResult.LESS;
984 private static int compareBaseLocationSet(CompositeLocation compLoc1,
985 CompositeLocation compLoc2, ClassDescriptor priorityCD) {
987 // if compLoc1 is greater than compLoc2, return true
988 // else return false;
990 Map<ClassDescriptor, Location> cd2loc1 = compLoc1.getCd2Loc();
991 Map<ClassDescriptor, Location> cd2loc2 = compLoc2.getCd2Loc();
993 // compare first the priority loc elements
994 Location priorityLoc1 = cd2loc1.get(priorityCD);
995 Location priorityLoc2 = cd2loc2.get(priorityCD);
997 assert (priorityLoc1.getClassDescriptor().equals(priorityLoc2.getClassDescriptor()));
999 ClassDescriptor cd = priorityLoc1.getClassDescriptor();
1000 Lattice<String> locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd);
1002 if (priorityLoc1.getLocIdentifier().equals(priorityLoc2.getLocIdentifier())) {
1003 // have the same level of local hierarchy
1005 if (((Set<String>) state.getCd2LocationPropertyMap().get(cd)).contains(priorityLoc1
1006 .getLocIdentifier())) {
1007 // this location can be spinning
1008 return ComparisonResult.GREATER;
1011 } else if (locationOrder.isGreaterThan(priorityLoc1.getLocIdentifier(),
1012 priorityLoc2.getLocIdentifier())) {
1013 // if priority loc of compLoc1 is higher than compLoc2
1014 // then, compLoc 1 is higher than compLoc2
1015 return ComparisonResult.GREATER;
1017 // if priority loc of compLoc1 is NOT higher than compLoc2
1018 // then, compLoc 1 is NOT higher than compLoc2
1019 return ComparisonResult.LESS;
1022 // compare base locations except priority by class descriptor
1023 Set<ClassDescriptor> keySet1 = cd2loc1.keySet();
1024 int numEqualLoc = 0;
1026 for (Iterator iterator = keySet1.iterator(); iterator.hasNext();) {
1027 ClassDescriptor cd1 = (ClassDescriptor) iterator.next();
1029 Location loc1 = cd2loc1.get(cd1);
1030 Location loc2 = cd2loc2.get(cd1);
1032 if (priorityLoc1.equals(loc1)) {
1037 // if comploc2 doesn't have corresponding location,
1038 // then we determines that comploc1 is lower than comploc 2
1039 return ComparisonResult.LESS;
1042 System.out.println("lattice comparison:" + loc1.getLocIdentifier() + " ? "
1043 + loc2.getLocIdentifier());
1044 locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd1);
1045 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1046 // have the same level of local hierarchy
1048 } else if (!locationOrder.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1049 // if one element of composite location 1 is not higher than composite
1051 // then, composite loc 1 is not higher than composite loc 2
1053 System.out.println(compLoc1 + " < " + compLoc2);
1054 return ComparisonResult.LESS;
1059 if (numEqualLoc == (compLoc1.getBaseLocationSize() - 1)) {
1060 return ComparisonResult.EQUAL;
1063 System.out.println(compLoc1 + " > " + compLoc2);
1064 return ComparisonResult.GREATER;
1067 public static CompositeLocation calculateGLB(ClassDescriptor cd,
1068 Set<CompositeLocation> inputSet, ClassDescriptor priorityCD) {
1070 CompositeLocation glbCompLoc = new CompositeLocation(cd);
1071 int maxDeltaFunction = 0;
1073 // calculate GLB of priority element first
1075 Hashtable<ClassDescriptor, Set<Location>> cd2locSet =
1076 new Hashtable<ClassDescriptor, Set<Location>>();
1078 // creating mapping from class to set of locations
1079 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1080 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1082 int numOfDelta = compLoc.getNumofDelta();
1083 if (numOfDelta > maxDeltaFunction) {
1084 maxDeltaFunction = numOfDelta;
1087 Set<Location> baseLocationSet = compLoc.getBaseLocationSet();
1088 for (Iterator iterator2 = baseLocationSet.iterator(); iterator2.hasNext();) {
1089 Location locElement = (Location) iterator2.next();
1090 ClassDescriptor locCD = locElement.getClassDescriptor();
1092 Set<Location> locSet = cd2locSet.get(locCD);
1093 if (locSet == null) {
1094 locSet = new HashSet<Location>();
1096 locSet.add(locElement);
1098 cd2locSet.put(locCD, locSet);
1103 Set<Location> locSetofClass = cd2locSet.get(priorityCD);
1104 Set<String> locIdentifierSet = new HashSet<String>();
1106 for (Iterator<Location> locIterator = locSetofClass.iterator(); locIterator.hasNext();) {
1107 Location locElement = locIterator.next();
1108 locIdentifierSet.add(locElement.getLocIdentifier());
1111 Lattice<String> locOrder = (Lattice<String>) state.getCd2LocationOrder().get(priorityCD);
1112 String glbLocIdentifer = locOrder.getGLB(locIdentifierSet);
1114 Location priorityGLB = new Location(priorityCD, glbLocIdentifer);
1116 Set<CompositeLocation> sameGLBLoc = new HashSet<CompositeLocation>();
1118 for (Iterator<CompositeLocation> iterator = inputSet.iterator(); iterator.hasNext();) {
1119 CompositeLocation inputComploc = iterator.next();
1120 Location locElement = inputComploc.getLocation(priorityCD);
1122 if (locElement.equals(priorityGLB)) {
1123 sameGLBLoc.add(inputComploc);
1126 glbCompLoc.addLocation(priorityGLB);
1127 if (sameGLBLoc.size() > 0) {
1128 // if more than one location shares the same priority GLB
1129 // need to calculate the rest of GLB loc
1131 Set<Location> glbElementSet = new HashSet<Location>();
1133 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1134 ClassDescriptor localCD = iterator.next();
1135 if (!localCD.equals(priorityCD)) {
1136 Set<Location> localLocSet = cd2locSet.get(localCD);
1137 Set<String> LocalLocIdSet = new HashSet<String>();
1139 for (Iterator<Location> locIterator = localLocSet.iterator(); locIterator.hasNext();) {
1140 Location locElement = locIterator.next();
1141 LocalLocIdSet.add(locElement.getLocIdentifier());
1144 Lattice<String> localOrder = (Lattice<String>) state.getCd2LocationOrder().get(localCD);
1145 Location localGLBLoc = new Location(localCD, localOrder.getGLB(LocalLocIdSet));
1146 glbCompLoc.addLocation(localGLBLoc);
1150 // if priority glb loc is lower than all of input loc
1151 // assign top location to the rest of loc element
1153 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1154 ClassDescriptor localCD = iterator.next();
1155 if (!localCD.equals(priorityCD)) {
1156 Location localGLBLoc = Location.createTopLocation(localCD);
1157 glbCompLoc.addLocation(localGLBLoc);
1170 class ComparisonResult {
1172 public static final int GREATER = 0;
1173 public static final int EQUAL = 1;
1174 public static final int LESS = 2;