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