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