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.AssignmentNode;
24 import IR.Tree.BlockExpressionNode;
25 import IR.Tree.BlockNode;
26 import IR.Tree.BlockStatementNode;
27 import IR.Tree.CreateObjectNode;
28 import IR.Tree.DeclarationNode;
29 import IR.Tree.ExpressionNode;
30 import IR.Tree.FieldAccessNode;
31 import IR.Tree.IfStatementNode;
33 import IR.Tree.LiteralNode;
34 import IR.Tree.LoopNode;
35 import IR.Tree.MethodInvokeNode;
36 import IR.Tree.NameNode;
37 import IR.Tree.OpNode;
38 import IR.Tree.SubBlockNode;
39 import IR.Tree.TertiaryNode;
40 import IR.Tree.TreeNode;
43 public class FlowDownCheck {
47 Hashtable<Descriptor, 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<Descriptor, 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;
89 if(!cd.isInterface()){
90 checkDeclarationInClass(cd);
91 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
92 MethodDescriptor md = (MethodDescriptor) method_it.next();
94 checkDeclarationInMethodBody(cd, md);
96 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<Descriptor> tdSet = td2loc.keySet();
108 for (Iterator iterator = tdSet.iterator(); iterator.hasNext();) {
109 Descriptor td = (Descriptor) 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 Descriptor 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);
146 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
147 MethodDescriptor md = (MethodDescriptor) method_it.next();
149 checkMethodBody(cd, md);
151 System.out.println("Error in " + md);
159 public Hashtable getMap() {
163 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
164 BlockNode bn = state.getMethodBody(md);
165 for (int i = 0; i < md.numParameters(); i++) {
166 // process annotations on method parameters
167 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
168 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
170 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
173 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
174 bn.getVarTable().setParent(nametable);
175 for (int i = 0; i < bn.size(); i++) {
176 BlockStatementNode bsn = bn.get(i);
177 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
181 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
182 BlockStatementNode bsn) {
184 switch (bsn.kind()) {
185 case Kind.SubBlockNode:
186 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
189 case Kind.DeclarationNode:
190 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
194 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
199 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
201 if (ln.getType() == LoopNode.FORLOOP) {
202 // check for loop case
203 ClassDescriptor cd = md.getClassDesc();
204 BlockNode bn = ln.getInitializer();
205 for (int i = 0; i < bn.size(); i++) {
206 BlockStatementNode bsn = bn.get(i);
207 checkDeclarationInBlockStatementNode(md, nametable, bsn);
212 checkDeclarationInBlockNode(md, nametable, ln.getBody());
215 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
216 BlockNode bn = state.getMethodBody(md);
217 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
220 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
222 // it will return the lowest location in the block node
223 CompositeLocation lowestLoc = null;
224 for (int i = 0; i < bn.size(); i++) {
225 BlockStatementNode bsn = bn.get(i);
226 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
228 if (lowestLoc == null) {
231 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc, md.getClassDesc())) {
239 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
240 SymbolTable nametable, BlockStatementNode bsn) {
242 CompositeLocation compLoc = null;
243 switch (bsn.kind()) {
244 case Kind.BlockExpressionNode:
245 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
248 case Kind.DeclarationNode:
249 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
252 case Kind.IfStatementNode:
253 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
257 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
260 case Kind.ReturnNode:
261 // checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
264 case Kind.SubBlockNode:
265 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
268 // case Kind.ContinueBreakNode:
269 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
276 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
279 ClassDescriptor cd = md.getClassDesc();
280 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
282 CompositeLocation condLoc =
283 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation(
285 addTypeLocation(ln.getCondition().getType(), (condLoc));
287 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
289 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc, cd)) {
290 // loop condition should be higher than loop body
292 "The location of the while-condition statement is lower than the loop body at "
293 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
299 // check for loop case
300 BlockNode bn = ln.getInitializer();
302 // calculate glb location of condition and update statements
303 CompositeLocation condLoc =
304 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
305 new CompositeLocation(cd));
306 addTypeLocation(ln.getCondition().getType(), condLoc);
308 CompositeLocation updateLoc =
309 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
311 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
312 glbInputSet.add(condLoc);
313 glbInputSet.add(updateLoc);
315 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
316 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
318 if (blockLoc == null) {
319 // when there is no statement in the loop body
320 return glbLocOfForLoopCond;
323 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc, cd)) {
325 "The location of the for-condition statement is lower than the for-loop body at "
326 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
333 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
334 SymbolTable nametable, SubBlockNode sbn) {
335 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
339 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
340 SymbolTable nametable, IfStatementNode isn) {
342 ClassDescriptor localCD = md.getClassDesc();
343 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
345 CompositeLocation condLoc =
346 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(
348 addTypeLocation(isn.getCondition().getType(), condLoc);
349 glbInputSet.add(condLoc);
351 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
352 glbInputSet.add(locTrueBlock);
354 // here, the location of conditional block should be higher than the
355 // location of true/false blocks
357 if (!CompositeLattice.isGreaterThan(condLoc, locTrueBlock, localCD)) {
360 "The location of the if-condition statement is lower than the conditional block at "
361 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
364 if (isn.getFalseBlock() != null) {
365 CompositeLocation locFalseBlock =
366 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
367 glbInputSet.add(locFalseBlock);
369 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock, localCD)) {
372 "The location of the if-condition statement is lower than the conditional block at "
373 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
378 // return GLB location of condition, true, and false block
379 CompositeLocation glbLoc = CompositeLattice.calculateGLB(localCD, glbInputSet, localCD);
384 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
385 SymbolTable nametable, DeclarationNode dn) {
386 VarDescriptor vd = dn.getVarDescriptor();
388 Location destLoc = td2loc.get(vd);
390 ClassDescriptor localCD = md.getClassDesc();
391 if (dn.getExpression() != null) {
392 CompositeLocation expressionLoc =
393 checkLocationFromExpressionNode(md, nametable, dn.getExpression(), new CompositeLocation(
395 addTypeLocation(dn.getExpression().getType(), expressionLoc);
397 if (expressionLoc != null) {
398 // checking location order
399 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) {
400 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
401 + " does not respect location hierarchy on the assignment " + dn.printNode(0));
404 return expressionLoc;
412 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
414 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
417 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
418 SymbolTable nametable, BlockExpressionNode ben) {
419 CompositeLocation compLoc =
420 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
421 addTypeLocation(ben.getExpression().getType(), compLoc);
425 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
426 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
428 CompositeLocation compLoc = null;
431 case Kind.AssignmentNode:
432 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
435 case Kind.FieldAccessNode:
436 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
440 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
444 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
447 case Kind.CreateObjectNode:
448 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
451 case Kind.ArrayAccessNode:
452 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
455 case Kind.LiteralNode:
456 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
459 case Kind.MethodInvokeNode:
460 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en);
463 case Kind.TertiaryNode:
464 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
467 // case Kind.InstanceOfNode:
468 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
471 // case Kind.ArrayInitializerNode:
472 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
476 // case Kind.ClassTypeNode:
477 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
480 // case Kind.OffsetNode:
481 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
489 addTypeLocation(en.getType(), compLoc);
494 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
495 SymbolTable nametable, TertiaryNode tn) {
496 ClassDescriptor cd = md.getClassDesc();
498 CompositeLocation condLoc =
499 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(cd));
500 addTypeLocation(tn.getCond().getType(), condLoc);
501 CompositeLocation trueLoc =
502 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(cd));
503 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
504 CompositeLocation falseLoc =
505 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(cd));
506 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
508 // check if condLoc is higher than trueLoc & falseLoc
509 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc, cd)) {
511 "The location of the condition expression is lower than the true expression at "
512 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
515 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc, cd)) {
517 "The location of the condition expression is lower than the true expression at "
518 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
521 // then, return glb of trueLoc & falseLoc
522 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
523 glbInputSet.add(trueLoc);
524 glbInputSet.add(falseLoc);
526 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
529 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
530 SymbolTable nametable, MethodInvokeNode min) {
532 ClassDescriptor cd = md.getClassDesc();
534 if (min.numArgs() > 1) {
536 // caller needs to guarantee that it passes arguments in regarding to
537 // callee's hierarchy
539 for (int i = 0; i < min.numArgs(); i++) {
540 ExpressionNode en = min.getArg(i);
541 CompositeLocation callerArg1 =
542 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
544 ClassDescriptor calleecd = min.getMethod().getClassDesc();
545 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
546 Location calleeLoc1 = td2loc.get(calleevd);
548 if (!callerArg1.getLocation(cd).isTop()) {
549 // here, check if ordering relations among caller's args respect
550 // ordering relations in-between callee's args
551 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
552 if (currentIdx != i) { // skip itself
553 ExpressionNode argExp = min.getArg(currentIdx);
554 CompositeLocation callerArg2 =
555 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation(cd));
557 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
558 Location calleeLoc2 = td2loc.get(calleevd2);
559 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2, cd);
560 boolean calleeResult =
561 CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2, calleecd);
563 if (calleeResult && !callerResult) {
564 // in callee, calleeLoc1 is higher than calleeLoc2
565 // then, caller should have same ordering relation in-bet
566 // callerLoc1 & callerLoc2
568 throw new Error("Caller doesn't respect ordering relations among method arguments:"
569 + cd.getSourceFileName() + ":" + min.getNumLine());
580 // all arguments should be higher than the location of return value
582 // first, calculate glb of arguments
583 Set<CompositeLocation> argLocSet = new HashSet<CompositeLocation>();
584 for (int i = 0; i < min.numArgs(); i++) {
585 ExpressionNode en = min.getArg(i);
586 CompositeLocation argLoc =
587 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
588 addTypeLocation(en.getType(), argLoc);
589 argLocSet.add(argLoc);
592 if (argLocSet.size() > 0) {
593 CompositeLocation argGLBLoc = CompositeLattice.calculateGLB(cd, argLocSet, cd);
596 // if there are no arguments,
597 CompositeLocation returnLoc = new CompositeLocation(cd);
598 returnLoc.addLocation(Location.createTopLocation(cd));
603 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
604 SymbolTable nametable, ArrayAccessNode aan) {
606 // return glb location of array itself and index
608 ClassDescriptor cd = md.getClassDesc();
610 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
612 CompositeLocation arrayLoc =
613 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation(
615 addTypeLocation(aan.getExpression().getType(), arrayLoc);
616 glbInputSet.add(arrayLoc);
618 CompositeLocation indexLoc =
619 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(cd));
620 glbInputSet.add(indexLoc);
621 addTypeLocation(aan.getIndex().getType(), indexLoc);
623 CompositeLocation glbLoc = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
627 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
628 SymbolTable nametable, CreateObjectNode con) {
630 ClassDescriptor cd = md.getClassDesc();
633 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
634 for (int i = 0; i < con.numArgs(); i++) {
635 ExpressionNode en = con.getArg(i);
636 CompositeLocation argLoc =
637 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
638 glbInputSet.add(argLoc);
639 addTypeLocation(en.getType(), argLoc);
642 // check array initializers
643 // if ((con.getArrayInitializer() != null)) {
644 // checkLocationFromArrayInitializerNode(md, nametable,
645 // con.getArrayInitializer());
648 if (glbInputSet.size() > 0) {
649 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
652 CompositeLocation compLoc = new CompositeLocation(cd);
653 compLoc.addLocation(Location.createTopLocation(cd));
658 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
661 ClassDescriptor cd = md.getClassDesc();
662 CompositeLocation leftLoc = new CompositeLocation(cd);
663 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
664 addTypeLocation(on.getLeft().getType(), leftLoc);
666 CompositeLocation rightLoc = new CompositeLocation(cd);
667 if (on.getRight() != null) {
668 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
669 addTypeLocation(on.getRight().getType(), rightLoc);
672 System.out.println("checking op node=" + on.printNode(0));
673 System.out.println("left loc=" + leftLoc + " from " + on.getLeft().getClass());
674 System.out.println("right loc=" + rightLoc);
676 Operation op = on.getOp();
678 switch (op.getOp()) {
680 case Operation.UNARYPLUS:
681 case Operation.UNARYMINUS:
682 case Operation.LOGIC_NOT:
684 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
687 case Operation.LOGIC_OR:
688 case Operation.LOGIC_AND:
690 case Operation.BIT_OR:
691 case Operation.BIT_XOR:
692 case Operation.BIT_AND:
693 case Operation.ISAVAILABLE:
694 case Operation.EQUAL:
695 case Operation.NOTEQUAL:
705 case Operation.LEFTSHIFT:
706 case Operation.RIGHTSHIFT:
707 case Operation.URIGHTSHIFT:
709 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
710 inputSet.add(leftLoc);
711 inputSet.add(rightLoc);
712 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(cd, inputSet, cd);
716 throw new Error(op.toString());
723 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
724 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
726 // literal value has the top location so that value can be flowed into any
728 Location literalLoc = Location.createTopLocation(md.getClassDesc());
729 loc.addLocation(literalLoc);
734 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
735 NameNode nn, CompositeLocation loc) {
737 NameDescriptor nd = nn.getName();
738 if (nd.getBase() != null) {
739 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
740 addTypeLocation(nn.getExpression().getType(), loc);
743 String varname = nd.toString();
744 Descriptor d = (Descriptor) nametable.get(varname);
746 Location localLoc = null;
747 if (d instanceof VarDescriptor) {
748 VarDescriptor vd = (VarDescriptor) d;
749 localLoc = td2loc.get(vd);
750 } else if (d instanceof FieldDescriptor) {
751 FieldDescriptor fd = (FieldDescriptor) d;
752 localLoc = td2loc.get(fd);
754 assert (localLoc != null);
756 if (localLoc instanceof CompositeLocation) {
757 loc = (CompositeLocation) localLoc;
759 loc.addLocation(localLoc);
766 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
767 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
769 FieldDescriptor fd = fan.getField();
770 Location fieldLoc = td2loc.get(fd);
771 loc.addLocation(fieldLoc);
773 ExpressionNode left = fan.getExpression();
774 CompositeLocation compLoc = checkLocationFromExpressionNode(md, nametable, left, loc);
775 addTypeLocation(left.getType(), compLoc);
779 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
780 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
781 ClassDescriptor cd = md.getClassDesc();
783 boolean postinc = true;
784 if (an.getOperation().getBaseOp() == null
785 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
786 .getBaseOp().getOp() != Operation.POSTDEC))
789 CompositeLocation destLocation =
790 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(cd));
792 CompositeLocation srcLocation = new CompositeLocation(cd);
794 srcLocation = new CompositeLocation(cd);
795 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
797 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, cd)) {
798 throw new Error("The value flow from " + srcLocation + " to " + destLocation
799 + " does not respect location hierarchy on the assignment " + an.printNode(0));
803 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
805 if (!((Set<String>) state.getCd2LocationPropertyMap().get(cd)).contains(destLocation
806 .getLocation(cd).getLocIdentifier())) {
807 throw new Error("Location " + destLocation + " is not allowed to have spinning values at "
808 + cd.getSourceFileName() + ":" + an.getNumLine());
812 if (an.getSrc() != null) {
813 addTypeLocation(an.getSrc().getType(), srcLocation);
815 addTypeLocation(an.getDest().getType(), destLocation);
820 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
821 SymbolTable nametable, TreeNode n) {
823 ClassDescriptor cd = md.getClassDesc();
824 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
826 // currently enforce every variable to have corresponding location
827 if (annotationVec.size() == 0) {
828 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
829 + md.getSymbol() + " of the class " + cd.getSymbol());
832 if (annotationVec.size() > 1) {
833 // variable can have at most one location
834 throw new Error(vd.getSymbol() + " has more than one location.");
837 AnnotationDescriptor ad = annotationVec.elementAt(0);
839 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
841 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
842 String locationID = ad.getValue();
843 // check if location is defined
844 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
845 if (lattice == null || (!lattice.containsKey(locationID))) {
846 throw new Error("Location " + locationID
847 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
849 Location loc = new Location(cd, locationID);
851 addTypeLocation(vd.getType(), loc);
853 } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
855 CompositeLocation compLoc = new CompositeLocation(cd);
857 if (ad.getValue().length() == 0) {
858 throw new Error("Delta function of " + vd.getSymbol() + " does not have any locations: "
859 + cd.getSymbol() + ".");
862 String deltaStr = ad.getValue();
863 if (deltaStr.startsWith("LOC(")) {
865 if (!deltaStr.endsWith(")")) {
866 throw new Error("The declaration of the delta location is wrong at "
867 + cd.getSourceFileName() + ":" + n.getNumLine());
869 String locationOperand = deltaStr.substring(4, deltaStr.length() - 1);
871 nametable.get(locationOperand);
872 Descriptor d = (Descriptor) nametable.get(locationOperand);
874 if (d instanceof VarDescriptor) {
875 VarDescriptor varDescriptor = (VarDescriptor) d;
876 DeltaLocation deltaLoc = new DeltaLocation(cd, varDescriptor);
877 // td2loc.put(vd.getType(), compLoc);
878 compLoc.addLocation(deltaLoc);
879 } else if (d instanceof FieldDescriptor) {
880 throw new Error("Applying delta operation to the field " + locationOperand
881 + " is not allowed at " + cd.getSourceFileName() + ":" + n.getNumLine());
884 StringTokenizer token = new StringTokenizer(deltaStr, ",");
885 DeltaLocation deltaLoc = new DeltaLocation(cd);
887 while (token.hasMoreTokens()) {
888 String deltaOperand = token.nextToken();
889 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
890 if (deltaCD == null) {
891 // delta operand is not defined in the location hierarchy
892 throw new Error("Delta operand '" + deltaOperand + "' of declaration node '" + vd
893 + "' is not defined by location hierarchies.");
896 Location loc = new Location(deltaCD, deltaOperand);
897 deltaLoc.addDeltaOperand(loc);
899 compLoc.addLocation(deltaLoc);
903 td2loc.put(vd, compLoc);
904 addTypeLocation(vd.getType(), compLoc);
911 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
912 VarDescriptor vd = dn.getVarDescriptor();
913 assignLocationOfVarDescriptor(vd, md, nametable, dn);
916 private void checkClass(ClassDescriptor cd) {
917 // Check to see that methods respects ss property
918 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
919 MethodDescriptor md = (MethodDescriptor) method_it.next();
920 checkMethodDeclaration(cd, md);
924 private void checkDeclarationInClass(ClassDescriptor cd) {
925 // Check to see that fields are okay
926 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
927 FieldDescriptor fd = (FieldDescriptor) field_it.next();
928 checkFieldDeclaration(cd, fd);
932 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
936 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
938 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
940 // currently enforce every variable to have corresponding location
941 if (annotationVec.size() == 0) {
942 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
946 if (annotationVec.size() > 1) {
947 // variable can have at most one location
948 throw new Error("Field " + fd.getSymbol() + " of class " + cd
949 + " has more than one location.");
952 // check if location is defined
953 AnnotationDescriptor ad = annotationVec.elementAt(0);
954 if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) {
955 String locationID = annotationVec.elementAt(0).getMarker();
956 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
958 if (lattice == null || (!lattice.containsKey(locationID))) {
959 throw new Error("Location " + locationID
960 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
963 Location localLoc = new Location(cd, locationID);
964 td2loc.put(fd, localLoc);
965 addTypeLocation(fd.getType(), localLoc);
967 } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
968 if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
970 if (ad.getValue().length() == 0) {
971 throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: "
972 + cd.getSymbol() + ".");
975 CompositeLocation compLoc = new CompositeLocation(cd);
976 DeltaLocation deltaLoc = new DeltaLocation(cd);
978 StringTokenizer token = new StringTokenizer(ad.getValue(), ",");
979 while (token.hasMoreTokens()) {
980 String deltaOperand = token.nextToken();
981 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
982 if (deltaCD == null) {
983 // delta operand is not defined in the location hierarchy
984 throw new Error("Delta operand '" + deltaOperand + "' of field node '" + fd
985 + "' is not defined by location hierarchies.");
988 Location loc = new Location(deltaCD, deltaOperand);
989 deltaLoc.addDeltaOperand(loc);
991 compLoc.addLocation(deltaLoc);
992 td2loc.put(fd, compLoc);
993 addTypeLocation(fd.getType(), compLoc);
1000 private void addTypeLocation(TypeDescriptor type, Location loc) {
1002 type.setExtension(loc);
1006 static class CompositeLattice {
1008 public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) {
1010 // System.out.println("isGreaterThan=" + loc1 + " ? " + loc2);
1011 CompositeLocation compLoc1;
1012 CompositeLocation compLoc2;
1014 if (loc1 instanceof CompositeLocation) {
1015 compLoc1 = (CompositeLocation) loc1;
1017 // create a bogus composite location for a single location
1018 compLoc1 = new CompositeLocation(loc1.getClassDescriptor());
1019 compLoc1.addLocation(loc1);
1022 if (loc2 instanceof CompositeLocation) {
1023 compLoc2 = (CompositeLocation) loc2;
1025 // create a bogus composite location for a single location
1026 compLoc2 = new CompositeLocation(loc2.getClassDescriptor());
1027 compLoc2.addLocation(loc2);
1030 // comparing two composite locations
1031 // System.out.println("compare base location=" + compLoc1 + " ? " +
1034 int baseCompareResult = compareBaseLocationSet(compLoc1, compLoc2, priorityCD);
1035 if (baseCompareResult == ComparisonResult.EQUAL) {
1036 if (compareDelta(compLoc1, compLoc2) == ComparisonResult.GREATER) {
1041 } else if (baseCompareResult == ComparisonResult.GREATER) {
1049 private static int compareDelta(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1050 if (compLoc1.getNumofDelta() < compLoc2.getNumofDelta()) {
1051 return ComparisonResult.GREATER;
1053 return ComparisonResult.LESS;
1057 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1058 CompositeLocation compLoc2, ClassDescriptor priorityCD) {
1060 // if compLoc1 is greater than compLoc2, return true
1061 // else return false;
1063 Map<ClassDescriptor, Location> cd2loc1 = compLoc1.getCd2Loc();
1064 Map<ClassDescriptor, Location> cd2loc2 = compLoc2.getCd2Loc();
1066 // compare first the priority loc elements
1067 Location priorityLoc1 = cd2loc1.get(priorityCD);
1068 Location priorityLoc2 = cd2loc2.get(priorityCD);
1070 assert (priorityLoc1.getClassDescriptor().equals(priorityLoc2.getClassDescriptor()));
1072 ClassDescriptor cd = priorityLoc1.getClassDescriptor();
1073 Lattice<String> locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd);
1075 if (priorityLoc1.getLocIdentifier().equals(priorityLoc2.getLocIdentifier())) {
1076 // have the same level of local hierarchy
1078 Set<String> spinSet = (Set<String>) state.getCd2LocationPropertyMap().get(cd);
1079 if (spinSet != null && spinSet.contains(priorityLoc1.getLocIdentifier())) {
1080 // this location can be spinning
1081 return ComparisonResult.GREATER;
1084 } else if (locationOrder.isGreaterThan(priorityLoc1.getLocIdentifier(),
1085 priorityLoc2.getLocIdentifier())) {
1086 // if priority loc of compLoc1 is higher than compLoc2
1087 // then, compLoc 1 is higher than compLoc2
1088 return ComparisonResult.GREATER;
1090 // if priority loc of compLoc1 is NOT higher than compLoc2
1091 // then, compLoc 1 is NOT higher than compLoc2
1092 return ComparisonResult.LESS;
1095 // compare base locations except priority by class descriptor
1096 Set<ClassDescriptor> keySet1 = cd2loc1.keySet();
1097 int numEqualLoc = 0;
1099 for (Iterator iterator = keySet1.iterator(); iterator.hasNext();) {
1100 ClassDescriptor cd1 = (ClassDescriptor) iterator.next();
1102 Location loc1 = cd2loc1.get(cd1);
1103 Location loc2 = cd2loc2.get(cd1);
1105 if (priorityLoc1.equals(loc1)) {
1110 // if comploc2 doesn't have corresponding location,
1111 // then we determines that comploc1 is lower than comploc 2
1112 return ComparisonResult.LESS;
1115 System.out.println("lattice comparison:" + loc1.getLocIdentifier() + " ? "
1116 + loc2.getLocIdentifier());
1117 locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd1);
1118 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1119 // have the same level of local hierarchy
1121 } else if (!locationOrder.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1122 // if one element of composite location 1 is not higher than composite
1124 // then, composite loc 1 is not higher than composite loc 2
1126 System.out.println(compLoc1 + " < " + compLoc2);
1127 return ComparisonResult.LESS;
1132 if (numEqualLoc == (compLoc1.getBaseLocationSize() - 1)) {
1133 return ComparisonResult.EQUAL;
1136 System.out.println(compLoc1 + " > " + compLoc2);
1137 return ComparisonResult.GREATER;
1140 public static CompositeLocation calculateGLB(ClassDescriptor cd,
1141 Set<CompositeLocation> inputSet, ClassDescriptor priorityCD) {
1143 CompositeLocation glbCompLoc = new CompositeLocation(cd);
1144 int maxDeltaFunction = 0;
1146 // calculate GLB of priority element first
1148 Hashtable<ClassDescriptor, Set<Location>> cd2locSet =
1149 new Hashtable<ClassDescriptor, Set<Location>>();
1151 // creating mapping from class to set of locations
1152 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1153 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1155 int numOfDelta = compLoc.getNumofDelta();
1156 if (numOfDelta > maxDeltaFunction) {
1157 maxDeltaFunction = numOfDelta;
1160 Set<Location> baseLocationSet = compLoc.getBaseLocationSet();
1161 for (Iterator iterator2 = baseLocationSet.iterator(); iterator2.hasNext();) {
1162 Location locElement = (Location) iterator2.next();
1163 ClassDescriptor locCD = locElement.getClassDescriptor();
1165 Set<Location> locSet = cd2locSet.get(locCD);
1166 if (locSet == null) {
1167 locSet = new HashSet<Location>();
1169 locSet.add(locElement);
1171 cd2locSet.put(locCD, locSet);
1176 Set<Location> locSetofClass = cd2locSet.get(priorityCD);
1177 Set<String> locIdentifierSet = new HashSet<String>();
1179 for (Iterator<Location> locIterator = locSetofClass.iterator(); locIterator.hasNext();) {
1180 Location locElement = locIterator.next();
1181 locIdentifierSet.add(locElement.getLocIdentifier());
1184 Lattice<String> locOrder = (Lattice<String>) state.getCd2LocationOrder().get(priorityCD);
1185 String glbLocIdentifer = locOrder.getGLB(locIdentifierSet);
1187 Location priorityGLB = new Location(priorityCD, glbLocIdentifer);
1189 Set<CompositeLocation> sameGLBLoc = new HashSet<CompositeLocation>();
1191 for (Iterator<CompositeLocation> iterator = inputSet.iterator(); iterator.hasNext();) {
1192 CompositeLocation inputComploc = iterator.next();
1193 Location locElement = inputComploc.getLocation(priorityCD);
1195 if (locElement.equals(priorityGLB)) {
1196 sameGLBLoc.add(inputComploc);
1199 glbCompLoc.addLocation(priorityGLB);
1200 if (sameGLBLoc.size() > 0) {
1201 // if more than one location shares the same priority GLB
1202 // need to calculate the rest of GLB loc
1204 Set<Location> glbElementSet = new HashSet<Location>();
1206 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1207 ClassDescriptor localCD = iterator.next();
1208 if (!localCD.equals(priorityCD)) {
1209 Set<Location> localLocSet = cd2locSet.get(localCD);
1210 Set<String> LocalLocIdSet = new HashSet<String>();
1212 for (Iterator<Location> locIterator = localLocSet.iterator(); locIterator.hasNext();) {
1213 Location locElement = locIterator.next();
1214 LocalLocIdSet.add(locElement.getLocIdentifier());
1217 Lattice<String> localOrder = (Lattice<String>) state.getCd2LocationOrder().get(localCD);
1218 Location localGLBLoc = new Location(localCD, localOrder.getGLB(LocalLocIdSet));
1219 glbCompLoc.addLocation(localGLBLoc);
1223 // if priority glb loc is lower than all of input loc
1224 // assign top location to the rest of loc element
1226 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1227 ClassDescriptor localCD = iterator.next();
1228 if (!localCD.equals(priorityCD)) {
1229 Location localGLBLoc = Location.createTopLocation(localCD);
1230 glbCompLoc.addLocation(localGLBLoc);
1243 class ComparisonResult {
1245 public static final int GREATER = 0;
1246 public static final int EQUAL = 1;
1247 public static final int LESS = 2;