add one more checking, class inheritance: If a class B has a super class A, all relat...
[IRC.git] / Robust / src / Analysis / SSJava / FlowDownCheck.java
1 package Analysis.SSJava;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Hashtable;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Set;
9 import java.util.StringTokenizer;
10 import java.util.Vector;
11
12 import IR.AnnotationDescriptor;
13 import IR.ClassDescriptor;
14 import IR.Descriptor;
15 import IR.FieldDescriptor;
16 import IR.MethodDescriptor;
17 import IR.NameDescriptor;
18 import IR.Operation;
19 import IR.State;
20 import IR.SymbolTable;
21 import IR.TypeDescriptor;
22 import IR.VarDescriptor;
23 import IR.Tree.ArrayAccessNode;
24 import IR.Tree.AssignmentNode;
25 import IR.Tree.BlockExpressionNode;
26 import IR.Tree.BlockNode;
27 import IR.Tree.BlockStatementNode;
28 import IR.Tree.CastNode;
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.ReturnNode;
41 import IR.Tree.SubBlockNode;
42 import IR.Tree.TertiaryNode;
43 import IR.Tree.TreeNode;
44 import Util.Pair;
45
46 public class FlowDownCheck {
47
48   static State state;
49   static SSJavaAnalysis ssjava;
50
51   HashSet toanalyze;
52
53   // mapping from 'descriptor' to 'composite location'
54   Hashtable<Descriptor, CompositeLocation> d2loc;
55
56   // mapping from 'locID' to 'class descriptor'
57   Hashtable<String, ClassDescriptor> fieldLocName2cd;
58
59   public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
60     this.ssjava = ssjava;
61     this.state = state;
62     this.toanalyze = new HashSet();
63     this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
64     this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
65     init();
66   }
67
68   public void init() {
69
70     // construct mapping from the location name to the class descriptor
71     // assume that the location name is unique through the whole program
72
73     Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
74     for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
75       ClassDescriptor cd = (ClassDescriptor) iterator.next();
76       SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
77       Set<String> fieldLocNameSet = lattice.getKeySet();
78
79       for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
80         String fieldLocName = (String) iterator2.next();
81         fieldLocName2cd.put(fieldLocName, cd);
82       }
83
84     }
85
86   }
87
88   public void flowDownCheck() {
89     SymbolTable classtable = state.getClassSymbolTable();
90
91     // phase 1 : checking declaration node and creating mapping of 'type
92     // desciptor' & 'location'
93     toanalyze.addAll(classtable.getValueSet());
94     toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
95     while (!toanalyze.isEmpty()) {
96       Object obj = toanalyze.iterator().next();
97       ClassDescriptor cd = (ClassDescriptor) obj;
98       toanalyze.remove(cd);
99
100       if (!cd.isInterface()) {
101
102         ClassDescriptor superDesc = cd.getSuperDesc();
103         if (superDesc != null && (!superDesc.isInterface())
104             && (!superDesc.getSymbol().equals("Object"))) {
105           checkOrderingInheritance(superDesc, cd);
106         }
107
108         checkDeclarationInClass(cd);
109         for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
110           MethodDescriptor md = (MethodDescriptor) method_it.next();
111           try {
112             checkDeclarationInMethodBody(cd, md);
113           } catch (Error e) {
114             System.out.println("Error in " + md);
115             throw e;
116           }
117         }
118       }
119
120     }
121
122     // phase2 : checking assignments
123     toanalyze.addAll(classtable.getValueSet());
124     toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
125     while (!toanalyze.isEmpty()) {
126       Object obj = toanalyze.iterator().next();
127       ClassDescriptor cd = (ClassDescriptor) obj;
128       toanalyze.remove(cd);
129
130       checkClass(cd);
131       for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
132         MethodDescriptor md = (MethodDescriptor) method_it.next();
133         try {
134           checkMethodBody(cd, md);
135         } catch (Error e) {
136           System.out.println("Error in " + md);
137           throw e;
138         }
139       }
140     }
141
142   }
143
144   private void checkOrderingInheritance(ClassDescriptor superCd, ClassDescriptor cd) {
145     // here, we're going to check that sub class keeps same relative orderings
146     // in respect to super class
147
148     SSJavaLattice<String> superLattice = ssjava.getClassLattice(superCd);
149     SSJavaLattice<String> subLattice = ssjava.getClassLattice(cd);
150
151     Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
152     Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
153
154     for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
155       Pair<String, String> pair = (Pair<String, String>) iterator.next();
156
157       if (!subPairSet.contains(pair)) {
158         throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
159             + pair.getSecond() + " < " + pair.getFirst() + "' that is defined by its superclass '"
160             + superCd + "'.");
161       }
162     }
163
164   }
165
166   public Hashtable getMap() {
167     return d2loc;
168   }
169
170   private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
171     BlockNode bn = state.getMethodBody(md);
172     for (int i = 0; i < md.numParameters(); i++) {
173       // process annotations on method parameters
174       VarDescriptor vd = (VarDescriptor) md.getParameter(i);
175       assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
176     }
177     checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
178   }
179
180   private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
181     bn.getVarTable().setParent(nametable);
182     for (int i = 0; i < bn.size(); i++) {
183       BlockStatementNode bsn = bn.get(i);
184       checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
185     }
186   }
187
188   private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
189       BlockStatementNode bsn) {
190
191     switch (bsn.kind()) {
192     case Kind.SubBlockNode:
193       checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
194       return;
195
196     case Kind.DeclarationNode:
197       checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
198       break;
199
200     case Kind.LoopNode:
201       checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
202       break;
203     }
204   }
205
206   private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
207
208     if (ln.getType() == LoopNode.FORLOOP) {
209       // check for loop case
210       ClassDescriptor cd = md.getClassDesc();
211       BlockNode bn = ln.getInitializer();
212       for (int i = 0; i < bn.size(); i++) {
213         BlockStatementNode bsn = bn.get(i);
214         checkDeclarationInBlockStatementNode(md, nametable, bsn);
215       }
216     }
217
218     // check loop body
219     checkDeclarationInBlockNode(md, nametable, ln.getBody());
220   }
221
222   private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
223     BlockNode bn = state.getMethodBody(md);
224     checkLocationFromBlockNode(md, md.getParameterTable(), bn);
225   }
226
227   private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
228       BlockNode bn) {
229
230     bn.getVarTable().setParent(nametable);
231     // it will return the lowest location in the block node
232     CompositeLocation lowestLoc = null;
233     for (int i = 0; i < bn.size(); i++) {
234       BlockStatementNode bsn = bn.get(i);
235       CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
236       if (lowestLoc == null) {
237         lowestLoc = bLoc;
238       } else {
239         if (!bLoc.isEmpty()) {
240           if (CompositeLattice.isGreaterThan(lowestLoc, bLoc)) {
241             lowestLoc = bLoc;
242           }
243         }
244       }
245     }
246     return lowestLoc;
247   }
248
249   private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
250       SymbolTable nametable, BlockStatementNode bsn) {
251
252     CompositeLocation compLoc = null;
253     switch (bsn.kind()) {
254     case Kind.BlockExpressionNode:
255       compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
256       break;
257
258     case Kind.DeclarationNode:
259       compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
260       break;
261
262     case Kind.IfStatementNode:
263       compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
264       break;
265
266     case Kind.LoopNode:
267       compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
268       break;
269
270     case Kind.ReturnNode:
271       compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
272       break;
273
274     case Kind.SubBlockNode:
275       compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
276       break;
277
278     // case Kind.ContinueBreakNode:
279     // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
280     // bsn);
281     // return null;
282     }
283     return compLoc;
284   }
285
286   private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
287       ReturnNode rn) {
288
289     ExpressionNode returnExp = rn.getReturnExpression();
290
291     CompositeLocation expLoc =
292         checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation());
293
294     // callee should have a relative ordering in-between parameters and return
295     // value, which is required by caller's constraint
296     for (int i = 0; i < md.numParameters(); i++) {
297       Descriptor calleevd = md.getParameter(i);
298       CompositeLocation calleeParamLoc = d2loc.get(calleevd);
299
300       // here, parameter(input value) should be higher than result(output)
301       if ((!expLoc.get(0).isTop()) && !CompositeLattice.isGreaterThan(calleeParamLoc, expLoc)) {
302         throw new Error("Callee " + md + " doesn't have the ordering relation between parameter '"
303             + calleevd + "' and its return value '" + returnExp.printNode(0) + "'.");
304       }
305     }
306
307     // by default, return node has "bottom" location
308     CompositeLocation loc = new CompositeLocation();
309     loc.addLocation(Location.createBottomLocation(md));
310     return loc;
311   }
312
313   private boolean hasOnlyLiteralValue(ExpressionNode en) {
314     if (en.kind() == Kind.LiteralNode) {
315       return true;
316     } else {
317       return false;
318     }
319   }
320
321   private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
322       LoopNode ln) {
323
324     ClassDescriptor cd = md.getClassDesc();
325     if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
326
327       CompositeLocation condLoc =
328           checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation());
329       addTypeLocation(ln.getCondition().getType(), (condLoc));
330
331       CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
332
333       if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc)) {
334         // loop condition should be higher than loop body
335         throw new Error(
336             "The location of the while-condition statement is lower than the loop body at "
337                 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
338       }
339
340       return bodyLoc;
341
342     } else {
343       // check for loop case
344       BlockNode bn = ln.getInitializer();
345       bn.getVarTable().setParent(nametable);
346
347       // calculate glb location of condition and update statements
348       CompositeLocation condLoc =
349           checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
350               new CompositeLocation());
351       addTypeLocation(ln.getCondition().getType(), condLoc);
352
353       CompositeLocation updateLoc =
354           checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
355
356       Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
357       glbInputSet.add(condLoc);
358       glbInputSet.add(updateLoc);
359
360       CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(glbInputSet);
361
362       // check location of 'forloop' body
363       CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
364
365       if (blockLoc == null) {
366         // when there is no statement in the loop body
367         return glbLocOfForLoopCond;
368       }
369
370       if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc)) {
371         throw new Error(
372             "The location of the for-condition statement is lower than the for-loop body at "
373                 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
374       }
375       return blockLoc;
376     }
377
378   }
379
380   private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
381       SymbolTable nametable, SubBlockNode sbn) {
382     CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
383     return compLoc;
384   }
385
386   private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
387       SymbolTable nametable, IfStatementNode isn) {
388
389     ClassDescriptor localCD = md.getClassDesc();
390     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
391
392     CompositeLocation condLoc =
393         checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation());
394
395     addTypeLocation(isn.getCondition().getType(), condLoc);
396     glbInputSet.add(condLoc);
397
398     CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
399     if (locTrueBlock != null) {
400       glbInputSet.add(locTrueBlock);
401       // here, the location of conditional block should be higher than the
402       // location of true/false blocks
403       if (locTrueBlock != null && !CompositeLattice.isGreaterThan(condLoc, locTrueBlock)) {
404         // error
405         throw new Error(
406             "The location of the if-condition statement is lower than the conditional block at "
407                 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
408       }
409     }
410
411     if (isn.getFalseBlock() != null) {
412       CompositeLocation locFalseBlock =
413           checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
414
415       if (locFalseBlock != null) {
416         glbInputSet.add(locFalseBlock);
417
418         if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock)) {
419           // error
420           throw new Error(
421               "The location of the if-condition statement is lower than the conditional block at "
422                   + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
423         }
424       }
425
426     }
427
428     // return GLB location of condition, true, and false block
429     CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
430
431     return glbLoc;
432   }
433
434   private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
435       SymbolTable nametable, DeclarationNode dn) {
436
437     VarDescriptor vd = dn.getVarDescriptor();
438
439     CompositeLocation destLoc = d2loc.get(vd);
440
441     if (dn.getExpression() != null) {
442       CompositeLocation expressionLoc =
443           checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
444               new CompositeLocation());
445       // addTypeLocation(dn.getExpression().getType(), expressionLoc);
446
447       if (expressionLoc != null) {
448         // checking location order
449         if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
450           throw new Error("The value flow from " + expressionLoc + " to " + destLoc
451               + " does not respect location hierarchy on the assignment " + dn.printNode(0)
452               + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
453         }
454       }
455       return expressionLoc;
456
457     } else {
458
459       // if (destLoc instanceof Location) {
460       // CompositeLocation comp = new CompositeLocation();
461       // comp.addLocation(destLoc);
462       // return comp;
463       // } else {
464       // return (CompositeLocation) destLoc;
465       // }
466       return destLoc;
467
468     }
469
470   }
471
472   private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
473       SubBlockNode sbn) {
474     checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
475   }
476
477   private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
478       SymbolTable nametable, BlockExpressionNode ben) {
479     CompositeLocation compLoc =
480         checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
481     // addTypeLocation(ben.getExpression().getType(), compLoc);
482     return compLoc;
483   }
484
485   private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
486       SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
487
488     CompositeLocation compLoc = null;
489     switch (en.kind()) {
490
491     case Kind.AssignmentNode:
492       compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
493       break;
494
495     case Kind.FieldAccessNode:
496       compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
497       break;
498
499     case Kind.NameNode:
500       compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
501       break;
502
503     case Kind.OpNode:
504       compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
505       break;
506
507     case Kind.CreateObjectNode:
508       compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
509       break;
510
511     case Kind.ArrayAccessNode:
512       compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
513       break;
514
515     case Kind.LiteralNode:
516       compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
517       break;
518
519     case Kind.MethodInvokeNode:
520       compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
521       break;
522
523     case Kind.TertiaryNode:
524       compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
525       break;
526
527     case Kind.CastNode:
528       compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
529       break;
530
531     // case Kind.InstanceOfNode:
532     // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
533     // return null;
534
535     // case Kind.ArrayInitializerNode:
536     // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
537     // td);
538     // return null;
539
540     // case Kind.ClassTypeNode:
541     // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
542     // return null;
543
544     // case Kind.OffsetNode:
545     // checkOffsetNode(md, nametable, (OffsetNode)en, td);
546     // return null;
547
548     default:
549       return null;
550
551     }
552     // addTypeLocation(en.getType(), compLoc);
553     return compLoc;
554
555   }
556
557   private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
558       CastNode cn) {
559
560     ExpressionNode en = cn.getExpression();
561     return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
562
563   }
564
565   private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
566       SymbolTable nametable, TertiaryNode tn) {
567     ClassDescriptor cd = md.getClassDesc();
568
569     CompositeLocation condLoc =
570         checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
571     addTypeLocation(tn.getCond().getType(), condLoc);
572     CompositeLocation trueLoc =
573         checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
574     addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
575     CompositeLocation falseLoc =
576         checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
577     addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
578
579     // check if condLoc is higher than trueLoc & falseLoc
580     if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
581       throw new Error(
582           "The location of the condition expression is lower than the true expression at "
583               + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
584     }
585
586     if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
587       throw new Error(
588           "The location of the condition expression is lower than the true expression at "
589               + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
590     }
591
592     // then, return glb of trueLoc & falseLoc
593     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
594     glbInputSet.add(trueLoc);
595     glbInputSet.add(falseLoc);
596
597     return CompositeLattice.calculateGLB(glbInputSet);
598   }
599
600   private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
601       SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
602
603     checkCalleeConstraints(md, nametable, min);
604
605     // all we need to care about is that
606     // method output(return value) should be lower than input values(method
607     // parameters)
608     Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
609     for (int i = 0; i < min.numArgs(); i++) {
610       ExpressionNode en = min.getArg(i);
611       CompositeLocation callerArg =
612           checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
613       inputGLBSet.add(callerArg);
614     }
615
616     if (inputGLBSet.size() > 0) {
617       return CompositeLattice.calculateGLB(inputGLBSet);
618     } else {
619       // if there are no arguments, just return TOP location
620       CompositeLocation compLoc = new CompositeLocation();
621       compLoc.addLocation(Location.createTopLocation(md));
622       return compLoc;
623     }
624
625   }
626
627   private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
628       MethodInvokeNode min) {
629
630     if (min.numArgs() > 1) {
631       // caller needs to guarantee that it passes arguments in regarding to
632       // callee's hierarchy
633       for (int i = 0; i < min.numArgs(); i++) {
634         ExpressionNode en = min.getArg(i);
635         CompositeLocation callerArg1 =
636             checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
637
638         ClassDescriptor calleecd = min.getMethod().getClassDesc();
639         VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
640         CompositeLocation calleeLoc1 = d2loc.get(calleevd);
641
642         if (!callerArg1.get(0).isTop()) {
643           // here, check if ordering relations among caller's args respect
644           // ordering relations in-between callee's args
645           for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
646             if (currentIdx != i) { // skip itself
647               ExpressionNode argExp = min.getArg(currentIdx);
648
649               CompositeLocation callerArg2 =
650                   checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
651
652               VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
653               CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
654
655               boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2);
656               boolean calleeResult = CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2);
657
658               if (calleeResult && !callerResult) {
659                 // If calleeLoc1 is higher than calleeLoc2
660                 // then, caller should have same ordering relation in-bet
661                 // callerLoc1 & callerLoc2
662
663                 throw new Error("Caller doesn't respect ordering relations among method arguments:"
664                     + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
665               }
666
667             }
668           }
669         }
670
671       }
672
673     }
674
675   }
676
677   private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
678       SymbolTable nametable, ArrayAccessNode aan) {
679
680     // return glb location of array itself and index
681
682     ClassDescriptor cd = md.getClassDesc();
683
684     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
685
686     CompositeLocation arrayLoc =
687         checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation());
688     // addTypeLocation(aan.getExpression().getType(), arrayLoc);
689     glbInputSet.add(arrayLoc);
690     CompositeLocation indexLoc =
691         checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation());
692     glbInputSet.add(indexLoc);
693     // addTypeLocation(aan.getIndex().getType(), indexLoc);
694
695     CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
696     return glbLoc;
697   }
698
699   private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
700       SymbolTable nametable, CreateObjectNode con) {
701
702     ClassDescriptor cd = md.getClassDesc();
703
704     // check arguments
705     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
706     for (int i = 0; i < con.numArgs(); i++) {
707       ExpressionNode en = con.getArg(i);
708       CompositeLocation argLoc =
709           checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
710       glbInputSet.add(argLoc);
711       addTypeLocation(en.getType(), argLoc);
712     }
713
714     // check array initializers
715     // if ((con.getArrayInitializer() != null)) {
716     // checkLocationFromArrayInitializerNode(md, nametable,
717     // con.getArrayInitializer());
718     // }
719
720     if (glbInputSet.size() > 0) {
721       return CompositeLattice.calculateGLB(glbInputSet);
722     }
723
724     CompositeLocation compLoc = new CompositeLocation();
725     compLoc.addLocation(Location.createTopLocation(md));
726     return compLoc;
727
728   }
729
730   private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
731       OpNode on) {
732
733     ClassDescriptor cd = md.getClassDesc();
734     CompositeLocation leftLoc = new CompositeLocation();
735     leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
736     // addTypeLocation(on.getLeft().getType(), leftLoc);
737
738     CompositeLocation rightLoc = new CompositeLocation();
739     if (on.getRight() != null) {
740       rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
741       // addTypeLocation(on.getRight().getType(), rightLoc);
742     }
743
744     // System.out.println("checking op node=" + on.printNode(0));
745     // System.out.println("left loc=" + leftLoc + " from " +
746     // on.getLeft().getClass());
747     // System.out.println("right loc=" + rightLoc + " from " +
748     // on.getRight().getClass());
749
750     Operation op = on.getOp();
751
752     switch (op.getOp()) {
753
754     case Operation.UNARYPLUS:
755     case Operation.UNARYMINUS:
756     case Operation.LOGIC_NOT:
757       // single operand
758       return leftLoc;
759
760     case Operation.LOGIC_OR:
761     case Operation.LOGIC_AND:
762     case Operation.COMP:
763     case Operation.BIT_OR:
764     case Operation.BIT_XOR:
765     case Operation.BIT_AND:
766     case Operation.ISAVAILABLE:
767     case Operation.EQUAL:
768     case Operation.NOTEQUAL:
769     case Operation.LT:
770     case Operation.GT:
771     case Operation.LTE:
772     case Operation.GTE:
773     case Operation.ADD:
774     case Operation.SUB:
775     case Operation.MULT:
776     case Operation.DIV:
777     case Operation.MOD:
778     case Operation.LEFTSHIFT:
779     case Operation.RIGHTSHIFT:
780     case Operation.URIGHTSHIFT:
781
782       Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
783       inputSet.add(leftLoc);
784       inputSet.add(rightLoc);
785       CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
786       return glbCompLoc;
787
788     default:
789       throw new Error(op.toString());
790     }
791
792   }
793
794   private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
795       SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
796
797     // literal value has the top location so that value can be flowed into any
798     // location
799     Location literalLoc = Location.createTopLocation(md);
800     loc.addLocation(literalLoc);
801     return loc;
802
803   }
804
805   private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
806       NameNode nn, CompositeLocation loc) {
807
808     NameDescriptor nd = nn.getName();
809     if (nd.getBase() != null) {
810
811       loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
812       // addTypeLocation(nn.getExpression().getType(), loc);
813     } else {
814       String varname = nd.toString();
815
816       if (varname.equals("this")) {
817         // 'this' itself!
818         MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
819         String thisLocId = methodLattice.getThisLoc();
820         if (thisLocId == null) {
821           throw new Error("The location for 'this' is not defined at "
822               + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
823         }
824         Location locElement = new Location(md, thisLocId);
825         loc.addLocation(locElement);
826         return loc;
827       }
828       Descriptor d = (Descriptor) nametable.get(varname);
829
830       // CompositeLocation localLoc = null;
831       if (d instanceof VarDescriptor) {
832         VarDescriptor vd = (VarDescriptor) d;
833         // localLoc = d2loc.get(vd);
834         // the type of var descriptor has a composite location!
835         loc = ((CompositeLocation) vd.getType().getExtension()).clone();
836       } else if (d instanceof FieldDescriptor) {
837         // the type of field descriptor has a location!
838         FieldDescriptor fd = (FieldDescriptor) d;
839
840         if (fd.isStatic()) {
841           if (fd.isFinal()) {
842             // if it is 'static final', the location has TOP since no one can
843             // change its value
844             loc.addLocation(Location.createTopLocation(md));
845           } else {
846             // if 'static', the location has pre-assigned global loc
847             MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
848             String globalLocId = localLattice.getGlobalLoc();
849             if (globalLocId == null) {
850               throw new Error("Global location element is not defined in the method " + md);
851             }
852             Location globalLoc = new Location(md, globalLocId);
853
854             loc.addLocation(globalLoc);
855           }
856         } else {
857           // the location of field access starts from this, followed by field
858           // location
859           MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
860           Location thisLoc = new Location(md, localLattice.getThisLoc());
861           loc.addLocation(thisLoc);
862         }
863
864         Location fieldLoc = (Location) fd.getType().getExtension();
865         loc.addLocation(fieldLoc);
866       }
867     }
868     return loc;
869   }
870
871   private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
872       SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
873
874     ExpressionNode left = fan.getExpression();
875     loc = checkLocationFromExpressionNode(md, nametable, left, loc);
876     // addTypeLocation(left.getType(), loc);
877
878     if (!left.getType().isPrimitive()) {
879       FieldDescriptor fd = fan.getField();
880       Location fieldLoc = (Location) fd.getType().getExtension();
881       loc.addLocation(fieldLoc);
882     }
883
884     return loc;
885   }
886
887   private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
888       SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
889
890     ClassDescriptor cd = md.getClassDesc();
891
892     boolean postinc = true;
893     if (an.getOperation().getBaseOp() == null
894         || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
895             .getBaseOp().getOp() != Operation.POSTDEC))
896       postinc = false;
897
898     CompositeLocation destLocation =
899         checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
900
901     CompositeLocation srcLocation = new CompositeLocation();
902
903     if (!postinc) {
904       if (hasOnlyLiteralValue(an.getSrc())) {
905         // if source is literal value, src location is TOP. so do not need to
906         // compare!
907         return destLocation;
908       }
909       srcLocation = new CompositeLocation();
910       srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
911       // System.out.println(" an= " + an.printNode(0) + " an.getSrc()=" +
912       // an.getSrc().getClass()
913       // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
914       // System.out.println("srcLocation=" + srcLocation);
915       // System.out.println("dstLocation=" + destLocation);
916       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
917         throw new Error("The value flow from " + srcLocation + " to " + destLocation
918             + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
919             + cd.getSourceFileName() + "::" + an.getNumLine());
920       }
921     } else {
922       destLocation =
923           srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
924
925       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
926         throw new Error("Location " + destLocation
927             + " is not allowed to have the value flow that moves within the same location at "
928             + cd.getSourceFileName() + "::" + an.getNumLine());
929       }
930
931     }
932
933     return destLocation;
934   }
935
936   private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
937       SymbolTable nametable, TreeNode n) {
938
939     ClassDescriptor cd = md.getClassDesc();
940     Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
941
942     // currently enforce every variable to have corresponding location
943     if (annotationVec.size() == 0) {
944       throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
945           + md.getSymbol() + " of the class " + cd.getSymbol());
946     }
947
948     if (annotationVec.size() > 1) { // variable can have at most one location
949       throw new Error(vd.getSymbol() + " has more than one location.");
950     }
951
952     AnnotationDescriptor ad = annotationVec.elementAt(0);
953
954     if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
955
956       if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
957         String locDec = ad.getValue(); // check if location is defined
958
959         if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
960           DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
961           d2loc.put(vd, deltaLoc);
962           addTypeLocation(vd.getType(), deltaLoc);
963         } else {
964           CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
965           d2loc.put(vd, compLoc);
966           addTypeLocation(vd.getType(), compLoc);
967         }
968
969       }
970     }
971
972   }
973
974   private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
975
976     int deltaCount = 0;
977     int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
978     while (dIdx >= 0) {
979       deltaCount++;
980       int beginIdx = dIdx + 6;
981       locDec = locDec.substring(beginIdx, locDec.length() - 1);
982       dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
983     }
984
985     CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
986     DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
987
988     return deltaLoc;
989   }
990
991   private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
992
993     CompositeLocation compLoc = new CompositeLocation();
994
995     StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
996     List<String> locIdList = new ArrayList<String>();
997     while (tokenizer.hasMoreTokens()) {
998       String locId = tokenizer.nextToken();
999       locIdList.add(locId);
1000     }
1001
1002     // at least,one location element needs to be here!
1003     assert (locIdList.size() > 0);
1004
1005     // assume that loc with idx 0 comes from the local lattice
1006     // loc with idx 1 comes from the field lattice
1007
1008     String localLocId = locIdList.get(0);
1009     SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1010     Location localLoc = new Location(md, localLocId);
1011     if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1012       throw new Error("Location " + localLocId
1013           + " is not defined in the local variable lattice at "
1014           + md.getClassDesc().getSourceFileName() + "::" + n.getNumLine() + ".");
1015     }
1016     compLoc.addLocation(localLoc);
1017
1018     for (int i = 1; i < locIdList.size(); i++) {
1019       String locName = locIdList.get(i);
1020       ClassDescriptor cd = fieldLocName2cd.get(locName);
1021
1022       SSJavaLattice<String> fieldLattice = CompositeLattice.getLatticeByDescriptor(cd);
1023
1024       if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
1025         throw new Error("Location " + locName + " is not defined in the field lattice at "
1026             + cd.getSourceFileName() + ".");
1027       }
1028
1029       Location fieldLoc = new Location(cd, locName);
1030       compLoc.addLocation(fieldLoc);
1031     }
1032
1033     return compLoc;
1034
1035   }
1036
1037   private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1038     VarDescriptor vd = dn.getVarDescriptor();
1039     assignLocationOfVarDescriptor(vd, md, nametable, dn);
1040   }
1041
1042   private void checkClass(ClassDescriptor cd) {
1043     // Check to see that methods respects ss property
1044     for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1045       MethodDescriptor md = (MethodDescriptor) method_it.next();
1046       checkMethodDeclaration(cd, md);
1047     }
1048   }
1049
1050   private void checkDeclarationInClass(ClassDescriptor cd) {
1051     // Check to see that fields are okay
1052     for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1053       FieldDescriptor fd = (FieldDescriptor) field_it.next();
1054       checkFieldDeclaration(cd, fd);
1055     }
1056   }
1057
1058   private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1059     // TODO
1060   }
1061
1062   private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1063
1064     Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1065
1066     // currently enforce every field to have corresponding location
1067     if (annotationVec.size() == 0) {
1068       throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1069           + cd.getSymbol());
1070     }
1071
1072     if (annotationVec.size() > 1) {
1073       // variable can have at most one location
1074       throw new Error("Field " + fd.getSymbol() + " of class " + cd
1075           + " has more than one location.");
1076     }
1077
1078     AnnotationDescriptor ad = annotationVec.elementAt(0);
1079
1080     if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1081
1082       if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1083         String locationID = ad.getValue();
1084         // check if location is defined
1085         SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1086         if (lattice == null || (!lattice.containsKey(locationID))) {
1087           throw new Error("Location " + locationID
1088               + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1089               + cd.getSourceFileName() + ".");
1090         }
1091         Location loc = new Location(cd, locationID);
1092         // d2loc.put(fd, loc);
1093         addTypeLocation(fd.getType(), loc);
1094
1095       }
1096     }
1097
1098   }
1099
1100   private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1101     if (type != null) {
1102       type.setExtension(loc);
1103     }
1104   }
1105
1106   private void addTypeLocation(TypeDescriptor type, Location loc) {
1107     if (type != null) {
1108       type.setExtension(loc);
1109     }
1110   }
1111
1112   static class CompositeLattice {
1113
1114     public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1115
1116       // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1117
1118       int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1119       if (baseCompareResult == ComparisonResult.EQUAL) {
1120         if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1121           return true;
1122         } else {
1123           return false;
1124         }
1125       } else if (baseCompareResult == ComparisonResult.GREATER) {
1126         return true;
1127       } else {
1128         return false;
1129       }
1130
1131     }
1132
1133     private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1134
1135       int deltaCount1 = 0;
1136       int deltaCount2 = 0;
1137       if (dLoc1 instanceof DeltaLocation) {
1138         deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1139       }
1140
1141       if (dLoc2 instanceof DeltaLocation) {
1142         deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1143       }
1144       if (deltaCount1 < deltaCount2) {
1145         return ComparisonResult.GREATER;
1146       } else if (deltaCount1 == deltaCount2) {
1147         return ComparisonResult.EQUAL;
1148       } else {
1149         return ComparisonResult.LESS;
1150       }
1151
1152     }
1153
1154     private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1155
1156       // if compLoc1 is greater than compLoc2, return true
1157       // else return false;
1158
1159       // compare one by one in according to the order of the tuple
1160       int numOfTie = 0;
1161       for (int i = 0; i < compLoc1.getSize(); i++) {
1162         Location loc1 = compLoc1.get(i);
1163         if (i >= compLoc2.getSize()) {
1164           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1165               + " because they are not comparable.");
1166         }
1167         Location loc2 = compLoc2.get(i);
1168
1169         if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1170           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1171               + " because they are not comparable.");
1172         }
1173
1174         Descriptor d1 = loc1.getDescriptor();
1175         Descriptor d2 = loc2.getDescriptor();
1176
1177         SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1178         SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1179
1180         // check if the spin location is appeared only at the end of the
1181         // composite location
1182         if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1183           if (i != (compLoc1.getSize() - 1)) {
1184             throw new Error("The spin location " + loc1.getLocIdentifier()
1185                 + " cannot be appeared in the middle of composite location.");
1186           }
1187         }
1188
1189         if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1190           if (i != (compLoc2.getSize() - 1)) {
1191             throw new Error("The spin location " + loc2.getLocIdentifier()
1192                 + " cannot be appeared in the middle of composite location.");
1193           }
1194         }
1195
1196         if (!lattice1.equals(lattice2)) {
1197           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1198               + " because they are not comparable.");
1199         }
1200
1201         if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1202           numOfTie++;
1203           // check if the current location is the spinning location
1204           // note that the spinning location only can be appeared in the last
1205           // part of the composite location
1206           if (numOfTie == compLoc1.getSize()
1207               && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1208             return ComparisonResult.GREATER;
1209           }
1210           continue;
1211         } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1212           return ComparisonResult.GREATER;
1213         } else {
1214           return ComparisonResult.LESS;
1215         }
1216
1217       }
1218
1219       if (numOfTie == compLoc1.getSize()) {
1220
1221         if (numOfTie != compLoc2.getSize()) {
1222           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1223               + " because they are not comparable.");
1224         }
1225
1226         return ComparisonResult.EQUAL;
1227       }
1228
1229       return ComparisonResult.LESS;
1230
1231     }
1232
1233     public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1234
1235       // System.out.println("Calculating GLB=" + inputSet);
1236       CompositeLocation glbCompLoc = new CompositeLocation();
1237
1238       // calculate GLB of the first(priority) element
1239       Set<String> priorityLocIdentifierSet = new HashSet<String>();
1240       Descriptor priorityDescriptor = null;
1241
1242       Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1243           new Hashtable<String, Set<CompositeLocation>>();
1244       // mapping from the priority loc ID to its full representation by the
1245       // composite location
1246
1247       for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1248         CompositeLocation compLoc = (CompositeLocation) iterator.next();
1249         Location priorityLoc = compLoc.get(0);
1250         String priorityLocId = priorityLoc.getLocIdentifier();
1251         priorityLocIdentifierSet.add(priorityLocId);
1252
1253         if (locId2CompLocSet.contains(priorityLocId)) {
1254           locId2CompLocSet.get(priorityLocId).add(compLoc);
1255         } else {
1256           Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1257           newSet.add(compLoc);
1258           locId2CompLocSet.put(priorityLocId, newSet);
1259         }
1260
1261         // check if priority location are coming from the same lattice
1262         if (priorityDescriptor == null) {
1263           priorityDescriptor = priorityLoc.getDescriptor();
1264         } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1265           throw new Error("Failed to calculate GLB of " + inputSet
1266               + " because they are from different lattices.");
1267         }
1268       }
1269
1270       SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1271       String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1272
1273       glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1274       Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1275
1276       if (compSet == null) {
1277         // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1278         // mean that the result is already lower than <x1,y1> and <x2,y2>
1279         // assign TOP to the rest of the location elements
1280         CompositeLocation inputComp = inputSet.iterator().next();
1281         for (int i = 1; i < inputComp.getSize(); i++) {
1282           glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1283         }
1284       } else {
1285         if (compSet.size() == 1) {
1286           // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1287           CompositeLocation comp = compSet.iterator().next();
1288           for (int i = 1; i < comp.getSize(); i++) {
1289             glbCompLoc.addLocation(comp.get(i));
1290           }
1291         } else {
1292           // when GLB(x1,x2)==x1 and x2 : GLB case 1
1293           // if more than one location shares the same priority GLB
1294           // need to calculate the rest of GLB loc
1295
1296           int compositeLocSize = compSet.iterator().next().getSize();
1297
1298           Set<String> glbInputSet = new HashSet<String>();
1299           Descriptor currentD = null;
1300           for (int i = 1; i < compositeLocSize; i++) {
1301             for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1302               CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1303               Location currentLoc = compositeLocation.get(i);
1304               currentD = currentLoc.getDescriptor();
1305               // making set of the current location sharing the same idx
1306               glbInputSet.add(currentLoc.getLocIdentifier());
1307             }
1308             // calculate glb for the current lattice
1309
1310             SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1311             String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1312             glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1313           }
1314
1315         }
1316       }
1317
1318       return glbCompLoc;
1319
1320     }
1321
1322     static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1323
1324       SSJavaLattice<String> lattice = null;
1325
1326       if (d instanceof ClassDescriptor) {
1327         lattice = ssjava.getCd2lattice().get(d);
1328       } else if (d instanceof MethodDescriptor) {
1329         if (ssjava.getMd2lattice().containsKey(d)) {
1330           lattice = ssjava.getMd2lattice().get(d);
1331         } else {
1332           // use default lattice for the method
1333           lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1334         }
1335       }
1336
1337       return lattice;
1338     }
1339
1340   }
1341
1342   class ComparisonResult {
1343
1344     public static final int GREATER = 0;
1345     public static final int EQUAL = 1;
1346     public static final int LESS = 2;
1347     int result;
1348
1349   }
1350
1351 }