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