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