extends the grammar to have a way to define a new type of location that is allowed...
[IRC.git] / Robust / src / Analysis / SSJava / FlowDownCheck.java
1 package Analysis.SSJava;
2
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.StringTokenizer;
9 import java.util.Vector;
10
11 import IR.AnnotationDescriptor;
12 import IR.ClassDescriptor;
13 import IR.Descriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.NameDescriptor;
17 import IR.Operation;
18 import IR.State;
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.ContinueBreakNode;
29 import IR.Tree.CreateObjectNode;
30 import IR.Tree.DeclarationNode;
31 import IR.Tree.ExpressionNode;
32 import IR.Tree.FieldAccessNode;
33 import IR.Tree.IfStatementNode;
34 import IR.Tree.Kind;
35 import IR.Tree.LiteralNode;
36 import IR.Tree.LoopNode;
37 import IR.Tree.MethodInvokeNode;
38 import IR.Tree.NameNode;
39 import IR.Tree.OpNode;
40 import IR.Tree.SubBlockNode;
41 import IR.Tree.TertiaryNode;
42 import Util.Lattice;
43
44 public class FlowDownCheck {
45
46   static State state;
47   HashSet toanalyze;
48   Hashtable<TypeDescriptor, Location> td2loc; // mapping from 'type descriptor'
49   // to 'location'
50   Hashtable<String, ClassDescriptor> id2cd; // mapping from 'locID' to 'class
51
52   // descriptor'
53
54   public FlowDownCheck(State state) {
55     this.state = state;
56     this.toanalyze = new HashSet();
57     this.td2loc = new Hashtable<TypeDescriptor, Location>();
58     init();
59   }
60
61   public void init() {
62     id2cd = new Hashtable<String, ClassDescriptor>();
63     Hashtable cd2lattice = state.getCd2LocationOrder();
64
65     Set cdSet = cd2lattice.keySet();
66     for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
67       ClassDescriptor cd = (ClassDescriptor) iterator.next();
68       Lattice<String> lattice = (Lattice<String>) cd2lattice.get(cd);
69
70       Set<String> locIdSet = lattice.getKeySet();
71       for (Iterator iterator2 = locIdSet.iterator(); iterator2.hasNext();) {
72         String locID = (String) iterator2.next();
73         id2cd.put(locID, cd);
74       }
75     }
76
77   }
78
79   public void flowDownCheck() {
80     SymbolTable classtable = state.getClassSymbolTable();
81
82     // phase 1 : checking declaration node and creating mapping of 'type
83     // desciptor' & 'location'
84     toanalyze.addAll(classtable.getValueSet());
85     toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
86     while (!toanalyze.isEmpty()) {
87       Object obj = toanalyze.iterator().next();
88       ClassDescriptor cd = (ClassDescriptor) obj;
89       toanalyze.remove(cd);
90       if (cd.isClassLibrary()) {
91         // doesn't care about class libraries now
92         continue;
93       }
94       checkDeclarationInClass(cd);
95       for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
96         MethodDescriptor md = (MethodDescriptor) method_it.next();
97         try {
98           checkDeclarationInMethodBody(cd, md);
99         } catch (Error e) {
100           System.out.println("Error in " + md);
101           throw e;
102         }
103       }
104     }
105
106     // post-processing for delta location
107     // for a nested delta location, assigning a concrete reference to delta
108     // operand
109     Set<TypeDescriptor> tdSet = td2loc.keySet();
110     for (Iterator iterator = tdSet.iterator(); iterator.hasNext();) {
111       TypeDescriptor td = (TypeDescriptor) iterator.next();
112       Location loc = td2loc.get(td);
113
114       if (loc.getType() == Location.DELTA) {
115         // if it contains delta reference pointing to another location element
116         CompositeLocation compLoc = (CompositeLocation) loc;
117
118         Location locElement = compLoc.getTuple().at(0);
119         assert (locElement instanceof DeltaLocation);
120
121         DeltaLocation delta = (DeltaLocation) locElement;
122         TypeDescriptor refType = delta.getRefLocationId();
123         if (refType != null) {
124           Location refLoc = td2loc.get(refType);
125
126           assert (refLoc instanceof CompositeLocation);
127           CompositeLocation refCompLoc = (CompositeLocation) refLoc;
128
129           assert (refCompLoc.getTuple().at(0) instanceof DeltaLocation);
130           DeltaLocation refDelta = (DeltaLocation) refCompLoc.getTuple().at(0);
131
132           delta.addDeltaOperand(refDelta);
133           // compLoc.addLocation(refDelta);
134         }
135
136       }
137     }
138
139     // phase2 : checking assignments
140     toanalyze.addAll(classtable.getValueSet());
141     toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
142     while (!toanalyze.isEmpty()) {
143       Object obj = toanalyze.iterator().next();
144       ClassDescriptor cd = (ClassDescriptor) obj;
145       toanalyze.remove(cd);
146       if (cd.isClassLibrary()) {
147         // doesn't care about class libraries now
148         continue;
149       }
150       checkClass(cd);
151       for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
152         MethodDescriptor md = (MethodDescriptor) method_it.next();
153         try {
154           checkMethodBody(cd, md);
155         } catch (Error e) {
156           System.out.println("Error in " + md);
157           throw e;
158         }
159       }
160     }
161
162   }
163
164   private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
165     BlockNode bn = state.getMethodBody(md);
166     checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
167   }
168
169   private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
170     bn.getVarTable().setParent(nametable);
171     for (int i = 0; i < bn.size(); i++) {
172       BlockStatementNode bsn = bn.get(i);
173       checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
174     }
175   }
176
177   private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
178       BlockStatementNode bsn) {
179
180     switch (bsn.kind()) {
181     case Kind.SubBlockNode:
182       checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
183       return;
184     case Kind.DeclarationNode:
185       checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
186       break;
187     case Kind.LoopNode:
188       checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
189       break;
190     }
191   }
192
193   private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
194
195     if (ln.getType() == LoopNode.FORLOOP) {
196       // check for loop case
197       ClassDescriptor cd = md.getClassDesc();
198       BlockNode bn = ln.getInitializer();
199       for (int i = 0; i < bn.size(); i++) {
200         BlockStatementNode bsn = bn.get(i);
201         checkDeclarationInBlockStatementNode(md, nametable, bsn);
202       }
203     }
204
205     // check loop body
206     checkDeclarationInBlockNode(md, nametable, ln.getBody());
207   }
208
209   private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
210     BlockNode bn = state.getMethodBody(md);
211     checkLocationFromBlockNode(md, md.getParameterTable(), bn);
212   }
213
214   private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
215       BlockNode bn) {
216     // it will return the lowest location in the block node
217     CompositeLocation lowestLoc = null;
218     for (int i = 0; i < bn.size(); i++) {
219       BlockStatementNode bsn = bn.get(i);
220       CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
221
222       if (lowestLoc == null) {
223         lowestLoc = bLoc;
224       } else {
225         if (CompositeLattice.isGreaterThan(lowestLoc, bLoc, md.getClassDesc())) {
226           lowestLoc = bLoc;
227         }
228       }
229     }
230     return lowestLoc;
231   }
232
233   private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
234       SymbolTable nametable, BlockStatementNode bsn) {
235
236     switch (bsn.kind()) {
237     case Kind.BlockExpressionNode:
238       return checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
239
240     case Kind.DeclarationNode:
241       return checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
242
243     case Kind.IfStatementNode:
244       return checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
245
246     case Kind.LoopNode:
247       return checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
248
249     case Kind.ReturnNode:
250       // checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
251       return null;
252
253     case Kind.SubBlockNode:
254       return checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
255
256       // case Kind.ContinueBreakNode:
257       // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
258       // bsn);
259       // return null;
260     }
261     return null;
262   }
263
264   private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
265       LoopNode ln) {
266
267     ClassDescriptor cd = md.getClassDesc();
268     if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
269
270       CompositeLocation condLoc =
271           checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation(
272               cd));
273       CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
274
275       if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc, cd)) {
276         // loop condition should be higher than loop body
277         throw new Error(
278             "The location of the while-condition statement is lower than the loop body at "
279                 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
280       }
281
282       return bodyLoc;
283
284     } else {
285       // check for loop case
286       BlockNode bn = ln.getInitializer();
287
288       // calculate glb location of condition and update statements
289       CompositeLocation condLoc =
290           checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
291               new CompositeLocation(cd));
292       CompositeLocation updateLoc =
293           checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
294
295       Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
296       glbInputSet.add(condLoc);
297       glbInputSet.add(updateLoc);
298
299       CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
300       CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
301
302       if (blockLoc == null) {
303         // when there is no statement in the loop body
304         return glbLocOfForLoopCond;
305       }
306
307       if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc, cd)) {
308         throw new Error(
309             "The location of the for-condition statement is lower than the for-loop body at "
310                 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
311       }
312       return blockLoc;
313     }
314
315   }
316
317   private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
318       SymbolTable nametable, SubBlockNode sbn) {
319     return checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
320   }
321
322   private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
323       SymbolTable nametable, IfStatementNode isn) {
324
325     ClassDescriptor localCD = md.getClassDesc();
326     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
327
328     CompositeLocation condLoc = new CompositeLocation(localCD);
329     condLoc = checkLocationFromExpressionNode(md, nametable, isn.getCondition(), condLoc);
330     glbInputSet.add(condLoc);
331
332     System.out.println(isn.getCondition().printNode(0) + ":::condLoc=" + condLoc);
333
334     CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
335     glbInputSet.add(locTrueBlock);
336     System.out.println(isn.getTrueBlock().printNode(0) + ":::trueLoc=" + locTrueBlock);
337
338     // here, the location of conditional block should be higher than the
339     // location of true/false blocks
340
341     if (!CompositeLattice.isGreaterThan(condLoc, locTrueBlock, localCD)) {
342       // error
343       throw new Error(
344           "The location of the if-condition statement is lower than the conditional block at "
345               + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
346     }
347
348     if (isn.getFalseBlock() != null) {
349       CompositeLocation locFalseBlock =
350           checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
351       glbInputSet.add(locFalseBlock);
352       System.out.println(isn.getFalseBlock().printNode(0) + ":::falseLoc=" + locFalseBlock);
353
354       if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock, localCD)) {
355         // error
356         throw new Error(
357             "The location of the if-condition statement is lower than the conditional block at "
358                 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
359       }
360
361     }
362
363     // return GLB location of condition, true, and false block
364     CompositeLocation glbLoc = CompositeLattice.calculateGLB(localCD, glbInputSet, localCD);
365
366     return glbLoc;
367   }
368
369   private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
370       SymbolTable nametable, DeclarationNode dn) {
371     VarDescriptor vd = dn.getVarDescriptor();
372
373     Location destLoc = td2loc.get(vd.getType());
374
375     ClassDescriptor localCD = md.getClassDesc();
376     if (dn.getExpression() != null) {
377       CompositeLocation expressionLoc = new CompositeLocation(localCD);
378       expressionLoc =
379           checkLocationFromExpressionNode(md, nametable, dn.getExpression(), expressionLoc);
380
381       if (expressionLoc != null) {
382
383         // checking location order
384         if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) {
385           throw new Error("The value flow from " + expressionLoc + " to " + destLoc
386               + " does not respect location hierarchy on the assignment " + dn.printNode(0));
387         }
388       }
389       return expressionLoc;
390
391     } else {
392       return null;
393     }
394
395   }
396
397   private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
398       SubBlockNode sbn) {
399     checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
400   }
401
402   private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
403       SymbolTable nametable, BlockExpressionNode ben) {
404     return checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
405   }
406
407   private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
408       SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
409
410     switch (en.kind()) {
411
412     case Kind.AssignmentNode:
413       return checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
414
415     case Kind.FieldAccessNode:
416       return checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
417
418     case Kind.NameNode:
419       return checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
420
421     case Kind.OpNode:
422       return checkLocationFromOpNode(md, nametable, (OpNode) en);
423
424     case Kind.CreateObjectNode:
425       return checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
426
427     case Kind.ArrayAccessNode:
428       return checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
429
430     case Kind.LiteralNode:
431       return checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
432
433     case Kind.MethodInvokeNode:
434       return checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en);
435
436     case Kind.TertiaryNode:
437       return checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
438
439       // case Kind.InstanceOfNode:
440       // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
441       // return null;
442
443       // case Kind.ArrayInitializerNode:
444       // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
445       // td);
446       // return null;
447
448       // case Kind.ClassTypeNode:
449       // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
450       // return null;
451
452       // case Kind.OffsetNode:
453       // checkOffsetNode(md, nametable, (OffsetNode)en, td);
454       // return null;
455
456     default:
457       return null;
458
459     }
460
461   }
462
463   private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
464       SymbolTable nametable, TertiaryNode tn) {
465     ClassDescriptor cd = md.getClassDesc();
466
467     CompositeLocation condLoc =
468         checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(cd));
469     CompositeLocation trueLoc =
470         checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(cd));
471     CompositeLocation falseLoc =
472         checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(cd));
473
474     // check if condLoc is higher than trueLoc & falseLoc
475     if (!CompositeLattice.isGreaterThan(condLoc, trueLoc, cd)) {
476       throw new Error(
477           "The location of the condition expression is lower than the true expression at "
478               + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
479     }
480
481     if (!CompositeLattice.isGreaterThan(condLoc, falseLoc, cd)) {
482       throw new Error(
483           "The location of the condition expression is lower than the true expression at "
484               + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
485     }
486
487     // then, return glb of trueLoc & falseLoc
488     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
489     glbInputSet.add(trueLoc);
490     glbInputSet.add(falseLoc);
491
492     return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
493   }
494
495   private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
496       SymbolTable nametable, MethodInvokeNode min) {
497
498     // all arguments should be higher than the location of return value
499     ClassDescriptor cd = md.getClassDesc();
500
501     // first, calculate glb of arguments
502     Set<CompositeLocation> argLocSet = new HashSet<CompositeLocation>();
503     for (int i = 0; i < min.numArgs(); i++) {
504       ExpressionNode en = min.getArg(i);
505       CompositeLocation argLoc =
506           checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
507       argLocSet.add(argLoc);
508     }
509
510     if (argLocSet.size() > 0) {
511       CompositeLocation argGLBLoc = CompositeLattice.calculateGLB(cd, argLocSet, cd);
512       return argGLBLoc;
513     } else {
514       // if there are no arguments,
515       CompositeLocation returnLoc = new CompositeLocation(cd);
516       returnLoc.addLocation(Location.createTopLocation(cd));
517       return returnLoc;
518     }
519   }
520
521   private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
522       SymbolTable nametable, ArrayAccessNode aan) {
523
524     // return glb location of array itself and index
525
526     ClassDescriptor cd = md.getClassDesc();
527
528     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
529
530     CompositeLocation arrayLoc =
531         checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation(
532             cd));
533     glbInputSet.add(arrayLoc);
534
535     CompositeLocation indexLoc =
536         checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(cd));
537     glbInputSet.add(indexLoc);
538
539     CompositeLocation glbLoc = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
540     return glbLoc;
541   }
542
543   private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
544       SymbolTable nametable, CreateObjectNode con) {
545
546     ClassDescriptor cd = md.getClassDesc();
547
548     // check arguments
549     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
550     for (int i = 0; i < con.numArgs(); i++) {
551       ExpressionNode en = con.getArg(i);
552       CompositeLocation argLoc =
553           checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
554       glbInputSet.add(argLoc);
555     }
556
557     // check array initializers
558     // if ((con.getArrayInitializer() != null)) {
559     // checkLocationFromArrayInitializerNode(md, nametable,
560     // con.getArrayInitializer());
561     // }
562
563     if (glbInputSet.size() > 0) {
564       return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
565     }
566
567     CompositeLocation compLoc = new CompositeLocation(cd);
568     compLoc.addLocation(Location.createTopLocation(cd));
569     return compLoc;
570
571   }
572
573   private CompositeLocation checkLocationFromArrayInitializerNode(MethodDescriptor md,
574       SymbolTable nametable, ArrayInitializerNode ain) {
575
576     ClassDescriptor cd = md.getClassDesc();
577     Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
578     for (int i = 0; i < ain.numVarInitializers(); ++i) {
579       checkLocationFromExpressionNode(md, nametable, ain.getVarInitializer(i),
580           new CompositeLocation(cd));
581       vec_type.add(ain.getVarInitializer(i).getType());
582     }
583
584     return null;
585   }
586
587   private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
588       OpNode on) {
589
590     ClassDescriptor cd = md.getClassDesc();
591     CompositeLocation leftLoc = new CompositeLocation(cd);
592     leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
593
594     CompositeLocation rightLoc = new CompositeLocation(cd);
595     if (on.getRight() != null) {
596       rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
597     }
598
599     System.out.println("checking op node=" + on.printNode(0));
600     System.out.println("left loc=" + leftLoc + " from " + on.getLeft().getClass());
601     System.out.println("right loc=" + rightLoc);
602
603     Operation op = on.getOp();
604
605     switch (op.getOp()) {
606
607     case Operation.UNARYPLUS:
608     case Operation.UNARYMINUS:
609     case Operation.LOGIC_NOT:
610       // single operand
611       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
612       break;
613
614     case Operation.LOGIC_OR:
615     case Operation.LOGIC_AND:
616     case Operation.COMP:
617     case Operation.BIT_OR:
618     case Operation.BIT_XOR:
619     case Operation.BIT_AND:
620     case Operation.ISAVAILABLE:
621     case Operation.EQUAL:
622     case Operation.NOTEQUAL:
623     case Operation.LT:
624     case Operation.GT:
625     case Operation.LTE:
626     case Operation.GTE:
627     case Operation.ADD:
628     case Operation.SUB:
629     case Operation.MULT:
630     case Operation.DIV:
631     case Operation.MOD:
632     case Operation.LEFTSHIFT:
633     case Operation.RIGHTSHIFT:
634     case Operation.URIGHTSHIFT:
635
636       Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
637       inputSet.add(leftLoc);
638       inputSet.add(rightLoc);
639       CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(cd, inputSet, cd);
640       return glbCompLoc;
641
642     default:
643       throw new Error(op.toString());
644     }
645
646     return null;
647
648   }
649
650   private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
651       SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
652
653     // literal value has the top location so that value can be flowed into any
654     // location
655     Location literalLoc = Location.createTopLocation(md.getClassDesc());
656     loc.addLocation(literalLoc);
657     return loc;
658
659   }
660
661   private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
662       NameNode nn, CompositeLocation loc) {
663
664     NameDescriptor nd = nn.getName();
665     if (nd.getBase() != null) {
666       loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
667     } else {
668
669       String varname = nd.toString();
670       Descriptor d = (Descriptor) nametable.get(varname);
671
672       Location localLoc = null;
673       if (d instanceof VarDescriptor) {
674         VarDescriptor vd = (VarDescriptor) d;
675         localLoc = td2loc.get(vd.getType());
676       } else if (d instanceof FieldDescriptor) {
677         FieldDescriptor fd = (FieldDescriptor) d;
678         localLoc = td2loc.get(fd.getType());
679       }
680       assert (localLoc != null);
681
682       if (localLoc instanceof CompositeLocation) {
683         loc = (CompositeLocation) localLoc;
684       } else {
685         loc.addLocation(localLoc);
686       }
687     }
688
689     return loc;
690   }
691
692   private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
693       SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
694     FieldDescriptor fd = fan.getField();
695     Location fieldLoc = td2loc.get(fd.getType());
696     loc.addLocation(fieldLoc);
697
698     ExpressionNode left = fan.getExpression();
699     return checkLocationFromExpressionNode(md, nametable, left, loc);
700   }
701
702   private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
703       SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
704
705     ClassDescriptor localCD = md.getClassDesc();
706
707     boolean postinc = true;
708     if (an.getOperation().getBaseOp() == null
709         || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
710             .getBaseOp().getOp() != Operation.POSTDEC))
711       postinc = false;
712
713     CompositeLocation destLocation =
714         checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(localCD));
715
716     CompositeLocation srcLocation = new CompositeLocation(localCD);
717     if (!postinc) {
718       srcLocation = new CompositeLocation(localCD);
719       srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
720       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, localCD)) {
721         throw new Error("The value flow from " + srcLocation + " to " + destLocation
722             + " does not respect location hierarchy on the assignment " + an.printNode(0));
723       }
724     }
725
726     return destLocation;
727   }
728
729   private Location createCompositeLocation(FieldAccessNode fan, Location loc) {
730
731     FieldDescriptor field = fan.getField();
732     ClassDescriptor fieldCD = field.getClassDescriptor();
733
734     assert (field.getType().getAnnotationMarkers().size() == 1);
735
736     AnnotationDescriptor locAnnotation = field.getType().getAnnotationMarkers().elementAt(0);
737     if (locAnnotation.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
738       // single location
739
740     } else {
741       // delta location
742     }
743
744     // Location localLoc=new Location(field.getClassDescriptor(),field.get)
745
746     // System.out.println("creatingComposite's field="+field+" localLoc="+localLoc);
747     ExpressionNode leftNode = fan.getExpression();
748     System.out.println("creatingComposite's leftnode=" + leftNode.printNode(0));
749
750     return loc;
751   }
752
753   private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
754
755     ClassDescriptor cd = md.getClassDesc();
756     VarDescriptor vd = dn.getVarDescriptor();
757     Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
758
759     // currently enforce every variable to have corresponding location
760     if (annotationVec.size() == 0) {
761       throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
762           + md.getSymbol() + " of the class " + cd.getSymbol());
763     }
764
765     if (annotationVec.size() > 1) {
766       // variable can have at most one location
767       throw new Error(vd.getSymbol() + " has more than one location.");
768     }
769
770     AnnotationDescriptor ad = annotationVec.elementAt(0);
771
772     if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) {
773
774       // check if location is defined
775       String locationID = ad.getMarker();
776       Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
777
778       if (lattice == null || (!lattice.containsKey(locationID))) {
779         throw new Error("Location " + locationID
780             + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
781       }
782
783       Location loc = new Location(cd, locationID);
784       td2loc.put(vd.getType(), loc);
785
786     } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
787       if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
788
789         CompositeLocation compLoc = new CompositeLocation(cd);
790
791         if (ad.getData().length() == 0) {
792           throw new Error("Delta function of " + vd.getSymbol() + " does not have any locations: "
793               + cd.getSymbol() + ".");
794         }
795
796         String deltaStr = ad.getData();
797         if (deltaStr.startsWith("LOC(")) {
798
799           if (!deltaStr.endsWith(")")) {
800             throw new Error("The declaration of the delta location is wrong at "
801                 + cd.getSourceFileName() + ":" + dn.getNumLine());
802           }
803           String locationOperand = deltaStr.substring(4, deltaStr.length() - 1);
804
805           nametable.get(locationOperand);
806           Descriptor d = (Descriptor) nametable.get(locationOperand);
807
808           if (d instanceof VarDescriptor) {
809             VarDescriptor varDescriptor = (VarDescriptor) d;
810             DeltaLocation deltaLoc = new DeltaLocation(cd, varDescriptor.getType());
811             // td2loc.put(vd.getType(), compLoc);
812             compLoc.addLocation(deltaLoc);
813           } else if (d instanceof FieldDescriptor) {
814             throw new Error("Applying delta operation to the field " + locationOperand
815                 + " is not allowed at " + cd.getSourceFileName() + ":" + dn.getNumLine());
816           }
817         } else {
818           StringTokenizer token = new StringTokenizer(deltaStr, ",");
819           DeltaLocation deltaLoc = new DeltaLocation(cd);
820
821           while (token.hasMoreTokens()) {
822             String deltaOperand = token.nextToken();
823             ClassDescriptor deltaCD = id2cd.get(deltaOperand);
824             if (deltaCD == null) {
825               // delta operand is not defined in the location hierarchy
826               throw new Error("Delta operand '" + deltaOperand + "' of declaration node '" + vd
827                   + "' is not defined by location hierarchies.");
828             }
829
830             Location loc = new Location(deltaCD, deltaOperand);
831             deltaLoc.addDeltaOperand(loc);
832           }
833           compLoc.addLocation(deltaLoc);
834
835         }
836
837         td2loc.put(vd.getType(), compLoc);
838         System.out.println("vd=" + vd + " is assigned by " + compLoc);
839
840       }
841     }
842
843   }
844
845   private void checkClass(ClassDescriptor cd) {
846     // Check to see that methods respects ss property
847     for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
848       MethodDescriptor md = (MethodDescriptor) method_it.next();
849       checkMethodDeclaration(cd, md);
850     }
851   }
852
853   private void checkDeclarationInClass(ClassDescriptor cd) {
854     // Check to see that fields are okay
855     for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
856       FieldDescriptor fd = (FieldDescriptor) field_it.next();
857       checkFieldDeclaration(cd, fd);
858     }
859   }
860
861   private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
862     // TODO
863   }
864
865   private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
866
867     Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
868
869     // currently enforce every variable to have corresponding location
870     if (annotationVec.size() == 0) {
871       throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
872           + cd.getSymbol());
873     }
874
875     if (annotationVec.size() > 1) {
876       // variable can have at most one location
877       throw new Error("Field " + fd.getSymbol() + " of class " + cd
878           + " has more than one location.");
879     }
880
881     // check if location is defined
882     AnnotationDescriptor ad = annotationVec.elementAt(0);
883     if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) {
884       String locationID = annotationVec.elementAt(0).getMarker();
885       Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
886
887       if (lattice == null || (!lattice.containsKey(locationID))) {
888         throw new Error("Location " + locationID
889             + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
890       }
891
892       Location localLoc = new Location(cd, locationID);
893       td2loc.put(fd.getType(), localLoc);
894
895     } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
896       if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
897
898         if (ad.getData().length() == 0) {
899           throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: "
900               + cd.getSymbol() + ".");
901         }
902
903         CompositeLocation compLoc = new CompositeLocation(cd);
904         DeltaLocation deltaLoc = new DeltaLocation(cd);
905
906         StringTokenizer token = new StringTokenizer(ad.getData(), ",");
907         while (token.hasMoreTokens()) {
908           String deltaOperand = token.nextToken();
909           ClassDescriptor deltaCD = id2cd.get(deltaOperand);
910           if (deltaCD == null) {
911             // delta operand is not defined in the location hierarchy
912             throw new Error("Delta operand '" + deltaOperand + "' of field node '" + fd
913                 + "' is not defined by location hierarchies.");
914           }
915
916           Location loc = new Location(deltaCD, deltaOperand);
917           deltaLoc.addDeltaOperand(loc);
918         }
919         compLoc.addLocation(deltaLoc);
920         td2loc.put(fd.getType(), compLoc);
921
922       }
923     }
924
925   }
926
927   static class CompositeLattice {
928
929     public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) {
930
931       // System.out.println("isGreaterThan=" + loc1 + " ? " + loc2);
932       CompositeLocation compLoc1;
933       CompositeLocation compLoc2;
934
935       if (loc1 instanceof CompositeLocation) {
936         compLoc1 = (CompositeLocation) loc1;
937       } else {
938         // create a bogus composite location for a single location
939         compLoc1 = new CompositeLocation(loc1.getClassDescriptor());
940         compLoc1.addLocation(loc1);
941       }
942
943       if (loc2 instanceof CompositeLocation) {
944         compLoc2 = (CompositeLocation) loc2;
945       } else {
946         // create a bogus composite location for a single location
947         compLoc2 = new CompositeLocation(loc2.getClassDescriptor());
948         compLoc2.addLocation(loc2);
949       }
950
951       // comparing two composite locations
952       // System.out.println("compare base location=" + compLoc1 + " ? " +
953       // compLoc2);
954
955       int baseCompareResult = compareBaseLocationSet(compLoc1, compLoc2, priorityCD);
956       if (baseCompareResult == ComparisonResult.EQUAL) {
957         if (compareDelta(compLoc1, compLoc2) == ComparisonResult.GREATER) {
958           return true;
959         } else {
960           return false;
961         }
962       } else if (baseCompareResult == ComparisonResult.GREATER) {
963         return true;
964       } else {
965         return false;
966       }
967
968     }
969
970     private static int compareDelta(CompositeLocation compLoc1, CompositeLocation compLoc2) {
971       if (compLoc1.getNumofDelta() < compLoc2.getNumofDelta()) {
972         return ComparisonResult.GREATER;
973       } else {
974         return ComparisonResult.LESS;
975       }
976     }
977
978     private static int compareBaseLocationSet(CompositeLocation compLoc1,
979         CompositeLocation compLoc2, ClassDescriptor priorityCD) {
980
981       // if compLoc1 is greater than compLoc2, return true
982       // else return false;
983
984       Map<ClassDescriptor, Location> cd2loc1 = compLoc1.getCd2Loc();
985       Map<ClassDescriptor, Location> cd2loc2 = compLoc2.getCd2Loc();
986
987       // compare first the priority loc elements
988       Location priorityLoc1 = cd2loc1.get(priorityCD);
989       Location priorityLoc2 = cd2loc2.get(priorityCD);
990
991       assert (priorityLoc1.getClassDescriptor().equals(priorityLoc2.getClassDescriptor()));
992
993       ClassDescriptor cd = priorityLoc1.getClassDescriptor();
994       Lattice<String> locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd);
995
996       if (priorityLoc1.getLocIdentifier().equals(priorityLoc2.getLocIdentifier())) {
997         // have the same level of local hierarchy
998       } else if (locationOrder.isGreaterThan(priorityLoc1.getLocIdentifier(),
999           priorityLoc2.getLocIdentifier())) {
1000         // if priority loc of compLoc1 is higher than compLoc2
1001         // then, compLoc 1 is higher than compLoc2
1002         return ComparisonResult.GREATER;
1003       } else {
1004         // if priority loc of compLoc1 is NOT higher than compLoc2
1005         // then, compLoc 1 is NOT higher than compLoc2
1006         return ComparisonResult.LESS;
1007       }
1008
1009       // compare base locations except priority by class descriptor
1010       Set<ClassDescriptor> keySet1 = cd2loc1.keySet();
1011       int numEqualLoc = 0;
1012
1013       for (Iterator iterator = keySet1.iterator(); iterator.hasNext();) {
1014         ClassDescriptor cd1 = (ClassDescriptor) iterator.next();
1015
1016         Location loc1 = cd2loc1.get(cd1);
1017         Location loc2 = cd2loc2.get(cd1);
1018
1019         if (priorityLoc1.equals(loc1)) {
1020           continue;
1021         }
1022
1023         if (loc2 == null) {
1024           // if comploc2 doesn't have corresponding location,
1025           // then we determines that comploc1 is lower than comploc 2
1026           return ComparisonResult.LESS;
1027         }
1028
1029         System.out.println("lattice comparison:" + loc1.getLocIdentifier() + " ? "
1030             + loc2.getLocIdentifier());
1031         locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd1);
1032         if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1033           // have the same level of local hierarchy
1034           numEqualLoc++;
1035         } else if (!locationOrder.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1036           // if one element of composite location 1 is not higher than composite
1037           // location 2
1038           // then, composite loc 1 is not higher than composite loc 2
1039
1040           System.out.println(compLoc1 + " < " + compLoc2);
1041           return ComparisonResult.LESS;
1042         }
1043
1044       }
1045
1046       if (numEqualLoc == (compLoc1.getBaseLocationSize() - 1)) {
1047         return ComparisonResult.EQUAL;
1048       }
1049
1050       System.out.println(compLoc1 + " > " + compLoc2);
1051       return ComparisonResult.GREATER;
1052     }
1053
1054     public static CompositeLocation calculateGLB(ClassDescriptor cd,
1055         Set<CompositeLocation> inputSet, ClassDescriptor priorityCD) {
1056
1057       CompositeLocation glbCompLoc = new CompositeLocation(cd);
1058       int maxDeltaFunction = 0;
1059
1060       // calculate GLB of priority element first
1061
1062       Hashtable<ClassDescriptor, Set<Location>> cd2locSet =
1063           new Hashtable<ClassDescriptor, Set<Location>>();
1064
1065       // creating mapping from class to set of locations
1066       for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1067         CompositeLocation compLoc = (CompositeLocation) iterator.next();
1068
1069         int numOfDelta = compLoc.getNumofDelta();
1070         if (numOfDelta > maxDeltaFunction) {
1071           maxDeltaFunction = numOfDelta;
1072         }
1073
1074         Set<Location> baseLocationSet = compLoc.getBaseLocationSet();
1075         for (Iterator iterator2 = baseLocationSet.iterator(); iterator2.hasNext();) {
1076           Location locElement = (Location) iterator2.next();
1077           ClassDescriptor locCD = locElement.getClassDescriptor();
1078
1079           Set<Location> locSet = cd2locSet.get(locCD);
1080           if (locSet == null) {
1081             locSet = new HashSet<Location>();
1082           }
1083           locSet.add(locElement);
1084
1085           cd2locSet.put(locCD, locSet);
1086
1087         }
1088       }
1089
1090       Set<Location> locSetofClass = cd2locSet.get(priorityCD);
1091       Set<String> locIdentifierSet = new HashSet<String>();
1092
1093       for (Iterator<Location> locIterator = locSetofClass.iterator(); locIterator.hasNext();) {
1094         Location locElement = locIterator.next();
1095         locIdentifierSet.add(locElement.getLocIdentifier());
1096       }
1097
1098       Lattice<String> locOrder = (Lattice<String>) state.getCd2LocationOrder().get(priorityCD);
1099       String glbLocIdentifer = locOrder.getGLB(locIdentifierSet);
1100
1101       Location priorityGLB = new Location(priorityCD, glbLocIdentifer);
1102
1103       Set<CompositeLocation> sameGLBLoc = new HashSet<CompositeLocation>();
1104
1105       for (Iterator<CompositeLocation> iterator = inputSet.iterator(); iterator.hasNext();) {
1106         CompositeLocation inputComploc = iterator.next();
1107         Location locElement = inputComploc.getLocation(priorityCD);
1108
1109         if (locElement.equals(priorityGLB)) {
1110           sameGLBLoc.add(inputComploc);
1111         }
1112       }
1113       glbCompLoc.addLocation(priorityGLB);
1114       if (sameGLBLoc.size() > 0) {
1115         // if more than one location shares the same priority GLB
1116         // need to calculate the rest of GLB loc
1117
1118         Set<Location> glbElementSet = new HashSet<Location>();
1119
1120         for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1121           ClassDescriptor localCD = iterator.next();
1122           if (!localCD.equals(priorityCD)) {
1123             Set<Location> localLocSet = cd2locSet.get(localCD);
1124             Set<String> LocalLocIdSet = new HashSet<String>();
1125
1126             for (Iterator<Location> locIterator = localLocSet.iterator(); locIterator.hasNext();) {
1127               Location locElement = locIterator.next();
1128               LocalLocIdSet.add(locElement.getLocIdentifier());
1129             }
1130
1131             Lattice<String> localOrder = (Lattice<String>) state.getCd2LocationOrder().get(localCD);
1132             Location localGLBLoc = new Location(localCD, localOrder.getGLB(LocalLocIdSet));
1133             glbCompLoc.addLocation(localGLBLoc);
1134           }
1135         }
1136       } else {
1137         // if priority glb loc is lower than all of input loc
1138         // assign top location to the rest of loc element
1139
1140         for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1141           ClassDescriptor localCD = iterator.next();
1142           if (!localCD.equals(priorityCD)) {
1143             Location localGLBLoc = Location.createTopLocation(localCD);
1144             glbCompLoc.addLocation(localGLBLoc);
1145           }
1146
1147         }
1148
1149       }
1150
1151       return glbCompLoc;
1152
1153     }
1154
1155   }
1156
1157   class ComparisonResult {
1158
1159     public static final int GREATER = 0;
1160     public static final int EQUAL = 1;
1161     public static final int LESS = 2;
1162     int result;
1163
1164   }
1165
1166 }