f213b81b8dbd59e3257db8e40d3654bfa9597863
[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 Location parseFieldLocDeclaraton(String decl) {
992
993     int idx = decl.indexOf(".");
994     String className = decl.substring(0, idx);
995     String fieldName = decl.substring(idx + 1);
996
997     Descriptor d = state.getClassSymbolTable().get(className);
998
999     assert (d instanceof ClassDescriptor);
1000     SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1001     if (!lattice.containsKey(fieldName)) {
1002       throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1003           + className + "'.");
1004     }
1005
1006     return new Location(d, fieldName);
1007   }
1008
1009   private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1010
1011     CompositeLocation compLoc = new CompositeLocation();
1012
1013     StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1014     List<String> locIdList = new ArrayList<String>();
1015     while (tokenizer.hasMoreTokens()) {
1016       String locId = tokenizer.nextToken();
1017       locIdList.add(locId);
1018     }
1019
1020     // at least,one location element needs to be here!
1021     assert (locIdList.size() > 0);
1022
1023     // assume that loc with idx 0 comes from the local lattice
1024     // loc with idx 1 comes from the field lattice
1025
1026     String localLocId = locIdList.get(0);
1027     SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1028     Location localLoc = new Location(md, localLocId);
1029     if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1030       throw new Error("Location " + localLocId
1031           + " is not defined in the local variable lattice at "
1032           + md.getClassDesc().getSourceFileName() + "::" + n.getNumLine() + ".");
1033     }
1034     compLoc.addLocation(localLoc);
1035
1036     for (int i = 1; i < locIdList.size(); i++) {
1037       String locName = locIdList.get(i);
1038
1039       Location fieldLoc = parseFieldLocDeclaraton(locName);
1040       // ClassDescriptor cd = fieldLocName2cd.get(locName);
1041       // SSJavaLattice<String> fieldLattice =
1042       // CompositeLattice.getLatticeByDescriptor(cd);
1043       //
1044       // if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
1045       // throw new Error("Location " + locName +
1046       // " is not defined in the field lattice at "
1047       // + cd.getSourceFileName() + ".");
1048       // }
1049       // Location fieldLoc = new Location(cd, locName);
1050       compLoc.addLocation(fieldLoc);
1051     }
1052
1053     return compLoc;
1054
1055   }
1056
1057   private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1058     VarDescriptor vd = dn.getVarDescriptor();
1059     assignLocationOfVarDescriptor(vd, md, nametable, dn);
1060   }
1061
1062   private void checkClass(ClassDescriptor cd) {
1063     // Check to see that methods respects ss property
1064     for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1065       MethodDescriptor md = (MethodDescriptor) method_it.next();
1066       checkMethodDeclaration(cd, md);
1067     }
1068   }
1069
1070   private void checkDeclarationInClass(ClassDescriptor cd) {
1071     // Check to see that fields are okay
1072     for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1073       FieldDescriptor fd = (FieldDescriptor) field_it.next();
1074       checkFieldDeclaration(cd, fd);
1075     }
1076   }
1077
1078   private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1079     // TODO
1080   }
1081
1082   private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1083
1084     Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1085
1086     // currently enforce every field to have corresponding location
1087     if (annotationVec.size() == 0) {
1088       throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1089           + cd.getSymbol());
1090     }
1091
1092     if (annotationVec.size() > 1) {
1093       // variable can have at most one location
1094       throw new Error("Field " + fd.getSymbol() + " of class " + cd
1095           + " has more than one location.");
1096     }
1097
1098     AnnotationDescriptor ad = annotationVec.elementAt(0);
1099
1100     if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1101
1102       if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1103         String locationID = ad.getValue();
1104         // check if location is defined
1105         SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1106         if (lattice == null || (!lattice.containsKey(locationID))) {
1107           throw new Error("Location " + locationID
1108               + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1109               + cd.getSourceFileName() + ".");
1110         }
1111         Location loc = new Location(cd, locationID);
1112         // d2loc.put(fd, loc);
1113         addTypeLocation(fd.getType(), loc);
1114
1115       }
1116     }
1117
1118   }
1119
1120   private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1121     if (type != null) {
1122       type.setExtension(loc);
1123     }
1124   }
1125
1126   private void addTypeLocation(TypeDescriptor type, Location loc) {
1127     if (type != null) {
1128       type.setExtension(loc);
1129     }
1130   }
1131
1132   static class CompositeLattice {
1133
1134     public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1135
1136       // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1137
1138       int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1139       if (baseCompareResult == ComparisonResult.EQUAL) {
1140         if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1141           return true;
1142         } else {
1143           return false;
1144         }
1145       } else if (baseCompareResult == ComparisonResult.GREATER) {
1146         return true;
1147       } else {
1148         return false;
1149       }
1150
1151     }
1152
1153     private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1154
1155       int deltaCount1 = 0;
1156       int deltaCount2 = 0;
1157       if (dLoc1 instanceof DeltaLocation) {
1158         deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1159       }
1160
1161       if (dLoc2 instanceof DeltaLocation) {
1162         deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1163       }
1164       if (deltaCount1 < deltaCount2) {
1165         return ComparisonResult.GREATER;
1166       } else if (deltaCount1 == deltaCount2) {
1167         return ComparisonResult.EQUAL;
1168       } else {
1169         return ComparisonResult.LESS;
1170       }
1171
1172     }
1173
1174     private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1175
1176       // if compLoc1 is greater than compLoc2, return true
1177       // else return false;
1178
1179       // compare one by one in according to the order of the tuple
1180       int numOfTie = 0;
1181       for (int i = 0; i < compLoc1.getSize(); i++) {
1182         Location loc1 = compLoc1.get(i);
1183         if (i >= compLoc2.getSize()) {
1184           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1185               + " because they are not comparable.");
1186         }
1187         Location loc2 = compLoc2.get(i);
1188
1189         if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1190           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1191               + " because they are not comparable.");
1192         }
1193
1194         Descriptor d1 = loc1.getDescriptor();
1195         Descriptor d2 = loc2.getDescriptor();
1196
1197         SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1198         SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1199
1200         // check if the spin location is appeared only at the end of the
1201         // composite location
1202         if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1203           if (i != (compLoc1.getSize() - 1)) {
1204             throw new Error("The spin location " + loc1.getLocIdentifier()
1205                 + " cannot be appeared in the middle of composite location.");
1206           }
1207         }
1208
1209         if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1210           if (i != (compLoc2.getSize() - 1)) {
1211             throw new Error("The spin location " + loc2.getLocIdentifier()
1212                 + " cannot be appeared in the middle of composite location.");
1213           }
1214         }
1215
1216         if (!lattice1.equals(lattice2)) {
1217           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1218               + " because they are not comparable.");
1219         }
1220
1221         if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1222           numOfTie++;
1223           // check if the current location is the spinning location
1224           // note that the spinning location only can be appeared in the last
1225           // part of the composite location
1226           if (numOfTie == compLoc1.getSize()
1227               && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1228             return ComparisonResult.GREATER;
1229           }
1230           continue;
1231         } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1232           return ComparisonResult.GREATER;
1233         } else {
1234           return ComparisonResult.LESS;
1235         }
1236
1237       }
1238
1239       if (numOfTie == compLoc1.getSize()) {
1240
1241         if (numOfTie != compLoc2.getSize()) {
1242           throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1243               + " because they are not comparable.");
1244         }
1245
1246         return ComparisonResult.EQUAL;
1247       }
1248
1249       return ComparisonResult.LESS;
1250
1251     }
1252
1253     public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1254
1255       // System.out.println("Calculating GLB=" + inputSet);
1256       CompositeLocation glbCompLoc = new CompositeLocation();
1257
1258       // calculate GLB of the first(priority) element
1259       Set<String> priorityLocIdentifierSet = new HashSet<String>();
1260       Descriptor priorityDescriptor = null;
1261
1262       Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1263           new Hashtable<String, Set<CompositeLocation>>();
1264       // mapping from the priority loc ID to its full representation by the
1265       // composite location
1266
1267       for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1268         CompositeLocation compLoc = (CompositeLocation) iterator.next();
1269         Location priorityLoc = compLoc.get(0);
1270         String priorityLocId = priorityLoc.getLocIdentifier();
1271         priorityLocIdentifierSet.add(priorityLocId);
1272
1273         if (locId2CompLocSet.contains(priorityLocId)) {
1274           locId2CompLocSet.get(priorityLocId).add(compLoc);
1275         } else {
1276           Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1277           newSet.add(compLoc);
1278           locId2CompLocSet.put(priorityLocId, newSet);
1279         }
1280
1281         // check if priority location are coming from the same lattice
1282         if (priorityDescriptor == null) {
1283           priorityDescriptor = priorityLoc.getDescriptor();
1284         } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1285           throw new Error("Failed to calculate GLB of " + inputSet
1286               + " because they are from different lattices.");
1287         }
1288       }
1289
1290       SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1291       String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1292
1293       glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1294       Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1295
1296       if (compSet == null) {
1297         // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1298         // mean that the result is already lower than <x1,y1> and <x2,y2>
1299         // assign TOP to the rest of the location elements
1300         CompositeLocation inputComp = inputSet.iterator().next();
1301         for (int i = 1; i < inputComp.getSize(); i++) {
1302           glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1303         }
1304       } else {
1305         if (compSet.size() == 1) {
1306           // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1307           CompositeLocation comp = compSet.iterator().next();
1308           for (int i = 1; i < comp.getSize(); i++) {
1309             glbCompLoc.addLocation(comp.get(i));
1310           }
1311         } else {
1312           // when GLB(x1,x2)==x1 and x2 : GLB case 1
1313           // if more than one location shares the same priority GLB
1314           // need to calculate the rest of GLB loc
1315
1316           int compositeLocSize = compSet.iterator().next().getSize();
1317
1318           Set<String> glbInputSet = new HashSet<String>();
1319           Descriptor currentD = null;
1320           for (int i = 1; i < compositeLocSize; i++) {
1321             for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1322               CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1323               Location currentLoc = compositeLocation.get(i);
1324               currentD = currentLoc.getDescriptor();
1325               // making set of the current location sharing the same idx
1326               glbInputSet.add(currentLoc.getLocIdentifier());
1327             }
1328             // calculate glb for the current lattice
1329
1330             SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1331             String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1332             glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1333           }
1334
1335         }
1336       }
1337
1338       return glbCompLoc;
1339
1340     }
1341
1342     static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1343
1344       SSJavaLattice<String> lattice = null;
1345
1346       if (d instanceof ClassDescriptor) {
1347         lattice = ssjava.getCd2lattice().get(d);
1348       } else if (d instanceof MethodDescriptor) {
1349         if (ssjava.getMd2lattice().containsKey(d)) {
1350           lattice = ssjava.getMd2lattice().get(d);
1351         } else {
1352           // use default lattice for the method
1353           lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1354         }
1355       }
1356
1357       return lattice;
1358     }
1359
1360   }
1361
1362   class ComparisonResult {
1363
1364     public static final int GREATER = 0;
1365     public static final int EQUAL = 1;
1366     public static final int LESS = 2;
1367     int result;
1368
1369   }
1370
1371 }