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