6d22825dcaa312f6bc8e87711300cf25fedfe4c6
[IRC.git] / Robust / src / Analysis / SSJava / FlowDownCheck.java
1 package Analysis.SSJava;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashSet;
7 import java.util.Hashtable;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Set;
11 import java.util.StringTokenizer;
12 import java.util.Vector;
13
14 import Analysis.SSJava.FlowDownCheck.ComparisonResult;
15 import Analysis.SSJava.FlowDownCheck.CompositeLattice;
16 import IR.AnnotationDescriptor;
17 import IR.ClassDescriptor;
18 import IR.Descriptor;
19 import IR.FieldDescriptor;
20 import IR.MethodDescriptor;
21 import IR.NameDescriptor;
22 import IR.Operation;
23 import IR.State;
24 import IR.SymbolTable;
25 import IR.TypeDescriptor;
26 import IR.TypeExtension;
27 import IR.VarDescriptor;
28 import IR.Flat.FlatNode;
29 import IR.Tree.ArrayAccessNode;
30 import IR.Tree.AssignmentNode;
31 import IR.Tree.BlockExpressionNode;
32 import IR.Tree.BlockNode;
33 import IR.Tree.BlockStatementNode;
34 import IR.Tree.CastNode;
35 import IR.Tree.CreateObjectNode;
36 import IR.Tree.DeclarationNode;
37 import IR.Tree.ExpressionNode;
38 import IR.Tree.FieldAccessNode;
39 import IR.Tree.IfStatementNode;
40 import IR.Tree.Kind;
41 import IR.Tree.LiteralNode;
42 import IR.Tree.LoopNode;
43 import IR.Tree.MethodInvokeNode;
44 import IR.Tree.NameNode;
45 import IR.Tree.OpNode;
46 import IR.Tree.ReturnNode;
47 import IR.Tree.SubBlockNode;
48 import IR.Tree.SwitchBlockNode;
49 import IR.Tree.SwitchStatementNode;
50 import IR.Tree.SynchronizedNode;
51 import IR.Tree.TertiaryNode;
52 import IR.Tree.TreeNode;
53 import Util.Pair;
54
55 public class FlowDownCheck {
56
57   State state;
58   static SSJavaAnalysis ssjava;
59
60   Set<ClassDescriptor> toanalyze;
61   List<ClassDescriptor> toanalyzeList;
62
63   Set<MethodDescriptor> toanalyzeMethod;
64   List<MethodDescriptor> toanalyzeMethodList;
65
66   // mapping from 'descriptor' to 'composite location'
67   Hashtable<Descriptor, CompositeLocation> d2loc;
68
69   Hashtable<MethodDescriptor, CompositeLocation> md2ReturnLoc;
70   Hashtable<MethodDescriptor, ReturnLocGenerator> md2ReturnLocGen;
71
72   // mapping from 'locID' to 'class descriptor'
73   Hashtable<String, ClassDescriptor> fieldLocName2cd;
74
75   boolean deterministic = true;
76
77   public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
78     this.ssjava = ssjava;
79     this.state = state;
80     if (deterministic) {
81       this.toanalyzeList = new ArrayList<ClassDescriptor>();
82     } else {
83       this.toanalyze = new HashSet<ClassDescriptor>();
84     }
85     if (deterministic) {
86       this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
87     } else {
88       this.toanalyzeMethod = new HashSet<MethodDescriptor>();
89     }
90     this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
91     this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
92     this.md2ReturnLoc = new Hashtable<MethodDescriptor, CompositeLocation>();
93     this.md2ReturnLocGen = new Hashtable<MethodDescriptor, ReturnLocGenerator>();
94   }
95
96   public void init() {
97
98     // construct mapping from the location name to the class descriptor
99     // assume that the location name is unique through the whole program
100
101     Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
102     for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
103       ClassDescriptor cd = (ClassDescriptor) iterator.next();
104       SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
105       Set<String> fieldLocNameSet = lattice.getKeySet();
106
107       for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
108         String fieldLocName = (String) iterator2.next();
109         fieldLocName2cd.put(fieldLocName, cd);
110       }
111
112     }
113
114   }
115
116   public boolean toAnalyzeIsEmpty() {
117     if (deterministic) {
118       return toanalyzeList.isEmpty();
119     } else {
120       return toanalyze.isEmpty();
121     }
122   }
123
124   public ClassDescriptor toAnalyzeNext() {
125     if (deterministic) {
126       return toanalyzeList.remove(0);
127     } else {
128       ClassDescriptor cd = toanalyze.iterator().next();
129       toanalyze.remove(cd);
130       return cd;
131     }
132   }
133
134   public void setupToAnalyze() {
135     SymbolTable classtable = state.getClassSymbolTable();
136     if (deterministic) {
137       toanalyzeList.clear();
138       toanalyzeList.addAll(classtable.getValueSet());
139       Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
140         public int compare(ClassDescriptor o1, ClassDescriptor o2) {
141           return o1.getClassName().compareToIgnoreCase(o2.getClassName());
142         }
143       });
144     } else {
145       toanalyze.clear();
146       toanalyze.addAll(classtable.getValueSet());
147     }
148   }
149
150   public void setupToAnalazeMethod(ClassDescriptor cd) {
151
152     SymbolTable methodtable = cd.getMethodTable();
153     if (deterministic) {
154       toanalyzeMethodList.clear();
155       toanalyzeMethodList.addAll(methodtable.getValueSet());
156       Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
157         public int compare(MethodDescriptor o1, MethodDescriptor o2) {
158           return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
159         }
160       });
161     } else {
162       toanalyzeMethod.clear();
163       toanalyzeMethod.addAll(methodtable.getValueSet());
164     }
165   }
166
167   public boolean toAnalyzeMethodIsEmpty() {
168     if (deterministic) {
169       return toanalyzeMethodList.isEmpty();
170     } else {
171       return toanalyzeMethod.isEmpty();
172     }
173   }
174
175   public MethodDescriptor toAnalyzeMethodNext() {
176     if (deterministic) {
177       return toanalyzeMethodList.remove(0);
178     } else {
179       MethodDescriptor md = toanalyzeMethod.iterator().next();
180       toanalyzeMethod.remove(md);
181       return md;
182     }
183   }
184
185   public void flowDownCheck() {
186
187     // phase 1 : checking declaration node and creating mapping of 'type
188     // desciptor' & 'location'
189     setupToAnalyze();
190
191     while (!toAnalyzeIsEmpty()) {
192       ClassDescriptor cd = toAnalyzeNext();
193
194       if (ssjava.needToBeAnnoated(cd)) {
195
196         ClassDescriptor superDesc = cd.getSuperDesc();
197
198         if (superDesc != null && (!superDesc.getSymbol().equals("Object"))) {
199           checkOrderingInheritance(superDesc, cd);
200         }
201
202         checkDeclarationInClass(cd);
203
204         setupToAnalazeMethod(cd);
205         while (!toAnalyzeMethodIsEmpty()) {
206           MethodDescriptor md = toAnalyzeMethodNext();
207           if (ssjava.needTobeAnnotated(md)) {
208             checkDeclarationInMethodBody(cd, md);
209           }
210         }
211
212       }
213
214     }
215
216     // phase2 : checking assignments
217     setupToAnalyze();
218
219     while (!toAnalyzeIsEmpty()) {
220       ClassDescriptor cd = toAnalyzeNext();
221
222       setupToAnalazeMethod(cd);
223       while (!toAnalyzeMethodIsEmpty()) {
224         MethodDescriptor md = toAnalyzeMethodNext();
225         if (ssjava.needTobeAnnotated(md)) {
226           if (state.SSJAVADEBUG) {
227             System.out.println("SSJAVA: Checking Flow-down Rules: " + md);
228           }
229           CompositeLocation calleePCLOC = ssjava.getPCLocation(md);
230           checkMethodBody(cd, md, calleePCLOC);
231         }
232       }
233     }
234
235   }
236
237   private void checkOrderingInheritance(ClassDescriptor superCd, ClassDescriptor cd) {
238     // here, we're going to check that sub class keeps same relative orderings
239     // in respect to super class
240
241     SSJavaLattice<String> superLattice = ssjava.getClassLattice(superCd);
242     SSJavaLattice<String> subLattice = ssjava.getClassLattice(cd);
243
244     if (superLattice != null) {
245       // if super class doesn't define lattice, then we don't need to check its
246       // subclass
247       if (subLattice == null) {
248         throw new Error("If a parent class '" + superCd
249             + "' has a ordering lattice, its subclass '" + cd + "' should have one.");
250       }
251
252       Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
253       Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
254
255       for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
256         Pair<String, String> pair = (Pair<String, String>) iterator.next();
257
258         if (!subPairSet.contains(pair)) {
259           throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
260               + pair.getSecond() + " < " + pair.getFirst()
261               + "' that is defined by its superclass '" + superCd + "'.");
262         }
263       }
264     }
265
266     MethodLattice<String> superMethodDefaultLattice = ssjava.getMethodDefaultLattice(superCd);
267     MethodLattice<String> subMethodDefaultLattice = ssjava.getMethodDefaultLattice(cd);
268
269     if (superMethodDefaultLattice != null) {
270       if (subMethodDefaultLattice == null) {
271         throw new Error("When a parent class '" + superCd
272             + "' defines a default method lattice, its subclass '" + cd + "' should define one.");
273       }
274
275       Set<Pair<String, String>> superPairSet = superMethodDefaultLattice.getOrderingPairSet();
276       Set<Pair<String, String>> subPairSet = subMethodDefaultLattice.getOrderingPairSet();
277
278       for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
279         Pair<String, String> pair = (Pair<String, String>) iterator.next();
280
281         if (!subPairSet.contains(pair)) {
282           throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
283               + pair.getSecond() + " < " + pair.getFirst()
284               + "' that is defined by its superclass '" + superCd
285               + "' in the method default lattice.");
286         }
287       }
288
289     }
290
291   }
292
293   public Hashtable getMap() {
294     return d2loc;
295   }
296
297   private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
298     BlockNode bn = state.getMethodBody(md);
299
300     // first, check annotations on method parameters
301     List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
302     for (int i = 0; i < md.numParameters(); i++) {
303       // process annotations on method parameters
304       VarDescriptor vd = (VarDescriptor) md.getParameter(i);
305       assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), null);
306       paramList.add(d2loc.get(vd));
307     }
308     Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
309
310     CompositeLocation returnLocComp = null;
311
312     boolean hasReturnLocDeclaration = false;
313     if (methodAnnotations != null) {
314       for (int i = 0; i < methodAnnotations.size(); i++) {
315         AnnotationDescriptor an = methodAnnotations.elementAt(i);
316         if (an.getMarker().equals(ssjava.RETURNLOC)) {
317           // this case, developer explicitly defines method lattice
318           String returnLocDeclaration = an.getValue();
319           returnLocComp = parseLocationDeclaration(md, null, returnLocDeclaration);
320           hasReturnLocDeclaration = true;
321         } else if (an.getMarker().equals(ssjava.THISLOC)) {
322           String thisLoc = an.getValue();
323           ssjava.getMethodLattice(md).setThisLoc(thisLoc);
324         } else if (an.getMarker().equals(ssjava.GLOBALLOC)) {
325           String globalLoc = an.getValue();
326           ssjava.getMethodLattice(md).setGlobalLoc(globalLoc);
327         } else if (an.getMarker().equals(ssjava.PCLOC)) {
328           String pcLocDeclaration = an.getValue();
329           ssjava.setPCLocation(md, parseLocationDeclaration(md, null, pcLocDeclaration));
330         }
331       }
332     }
333
334     // second, check return location annotation
335     if (!md.getReturnType().isVoid() && !ssjava.getMethodContainingSSJavaLoop().equals(md)) {
336       if (!hasReturnLocDeclaration) {
337         // if developer does not define method lattice
338         // search return location in the method default lattice
339         String rtrStr = ssjava.getMethodLattice(md).getReturnLoc();
340         if (rtrStr != null) {
341           returnLocComp = new CompositeLocation(new Location(md, rtrStr));
342         }
343       }
344
345       if (returnLocComp == null) {
346         throw new Error("Return location is not specified for the method " + md + " at "
347             + cd.getSourceFileName());
348       }
349
350       md2ReturnLoc.put(md, returnLocComp);
351
352     }
353
354     if (!md.getReturnType().isVoid() && !ssjava.getMethodContainingSSJavaLoop().equals(md)) {
355       MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
356       String thisLocId = methodLattice.getThisLoc();
357       if ((!md.isStatic())) {
358
359         if (thisLocId == null) {
360           throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
361               + md.getClassDesc().getSourceFileName());
362         }
363
364         CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
365         paramList.add(0, thisLoc);
366
367       }
368
369       md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), md, paramList, md
370           + " of " + cd.getSourceFileName()));
371
372     }
373
374     // fourth, check declarations inside of method
375
376     checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
377
378   }
379
380   private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
381     bn.getVarTable().setParent(nametable);
382     for (int i = 0; i < bn.size(); i++) {
383       BlockStatementNode bsn = bn.get(i);
384       checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
385     }
386   }
387
388   private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
389       BlockStatementNode bsn) {
390
391     switch (bsn.kind()) {
392     case Kind.SubBlockNode:
393       checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
394       return;
395
396     case Kind.DeclarationNode:
397       checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
398       break;
399
400     case Kind.LoopNode:
401       checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
402       break;
403
404     case Kind.IfStatementNode:
405       checkDeclarationInIfStatementNode(md, nametable, (IfStatementNode) bsn);
406       return;
407
408     case Kind.SwitchStatementNode:
409       checkDeclarationInSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
410       return;
411
412     case Kind.SynchronizedNode:
413       checkDeclarationInSynchronizedNode(md, nametable, (SynchronizedNode) bsn);
414       return;
415
416     }
417   }
418
419   private void checkDeclarationInSynchronizedNode(MethodDescriptor md, SymbolTable nametable,
420       SynchronizedNode sbn) {
421     checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
422   }
423
424   private void checkDeclarationInSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
425       SwitchStatementNode ssn) {
426     BlockNode sbn = ssn.getSwitchBody();
427     for (int i = 0; i < sbn.size(); i++) {
428       SwitchBlockNode node = (SwitchBlockNode) sbn.get(i);
429       checkDeclarationInBlockNode(md, nametable, node.getSwitchBlockStatement());
430     }
431   }
432
433   private void checkDeclarationInIfStatementNode(MethodDescriptor md, SymbolTable nametable,
434       IfStatementNode isn) {
435     checkDeclarationInBlockNode(md, nametable, isn.getTrueBlock());
436     if (isn.getFalseBlock() != null)
437       checkDeclarationInBlockNode(md, nametable, isn.getFalseBlock());
438   }
439
440   private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
441
442     if (ln.getType() == LoopNode.FORLOOP) {
443       // check for loop case
444       ClassDescriptor cd = md.getClassDesc();
445       BlockNode bn = ln.getInitializer();
446       for (int i = 0; i < bn.size(); i++) {
447         BlockStatementNode bsn = bn.get(i);
448         checkDeclarationInBlockStatementNode(md, nametable, bsn);
449       }
450     }
451
452     // check loop body
453     checkDeclarationInBlockNode(md, nametable, ln.getBody());
454   }
455
456   private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md,
457       CompositeLocation constraints) {
458     BlockNode bn = state.getMethodBody(md);
459     checkLocationFromBlockNode(md, md.getParameterTable(), bn, constraints);
460   }
461
462   private String generateErrorMessage(ClassDescriptor cd, TreeNode tn) {
463     if (tn != null) {
464       return cd.getSourceFileName() + "::" + tn.getNumLine();
465     } else {
466       return cd.getSourceFileName();
467     }
468
469   }
470
471   private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
472       BlockNode bn, CompositeLocation constraint) {
473
474     bn.getVarTable().setParent(nametable);
475     for (int i = 0; i < bn.size(); i++) {
476       BlockStatementNode bsn = bn.get(i);
477       checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
478     }
479     return new CompositeLocation();
480
481   }
482
483   private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
484       SymbolTable nametable, BlockStatementNode bsn, CompositeLocation constraint) {
485
486     CompositeLocation compLoc = null;
487     switch (bsn.kind()) {
488     case Kind.BlockExpressionNode:
489       compLoc =
490           checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, constraint);
491       break;
492
493     case Kind.DeclarationNode:
494       compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn, constraint);
495       break;
496
497     case Kind.IfStatementNode:
498       compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn, constraint);
499       break;
500
501     case Kind.LoopNode:
502       compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn, constraint);
503       break;
504
505     case Kind.ReturnNode:
506       compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn, constraint);
507       break;
508
509     case Kind.SubBlockNode:
510       compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn, constraint);
511       break;
512
513     case Kind.ContinueBreakNode:
514       compLoc = new CompositeLocation();
515       break;
516
517     case Kind.SwitchStatementNode:
518       compLoc =
519           checkLocationFromSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, constraint);
520
521     }
522     return compLoc;
523   }
524
525   private CompositeLocation checkLocationFromSwitchStatementNode(MethodDescriptor md,
526       SymbolTable nametable, SwitchStatementNode ssn, CompositeLocation constraint) {
527
528     ClassDescriptor cd = md.getClassDesc();
529     CompositeLocation condLoc =
530         checkLocationFromExpressionNode(md, nametable, ssn.getCondition(), new CompositeLocation(),
531             constraint, false);
532     BlockNode sbn = ssn.getSwitchBody();
533
534     constraint = generateNewConstraint(constraint, condLoc);
535
536     for (int i = 0; i < sbn.size(); i++) {
537       checkLocationFromSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), constraint);
538     }
539     return new CompositeLocation();
540   }
541
542   private CompositeLocation checkLocationFromSwitchBlockNode(MethodDescriptor md,
543       SymbolTable nametable, SwitchBlockNode sbn, CompositeLocation constraint) {
544
545     CompositeLocation blockLoc =
546         checkLocationFromBlockNode(md, nametable, sbn.getSwitchBlockStatement(), constraint);
547
548     return blockLoc;
549
550   }
551
552   private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
553       ReturnNode rn, CompositeLocation constraint) {
554
555     if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
556       return new CompositeLocation();
557     }
558
559     ExpressionNode returnExp = rn.getReturnExpression();
560
561     CompositeLocation declaredReturnLoc = md2ReturnLoc.get(md);
562
563     CompositeLocation returnValueLoc;
564     if (returnExp != null) {
565       returnValueLoc =
566           checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation(),
567               constraint, false);
568
569       // System.out.println("# RETURN VALUE LOC=" + returnValueLoc +
570       // " with constraint=" + constraint);
571
572       // TODO: do we need to check here?
573       // if this return statement is inside branch, return value has an implicit
574       // flow from conditional location
575       // if (constraint != null) {
576       // Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
577       // inputGLB.add(returnValueLoc);
578       // inputGLB.add(constraint);
579       // returnValueLoc =
580       // CompositeLattice.calculateGLB(inputGLB,
581       // generateErrorMessage(md.getClassDesc(), rn));
582       // }
583
584       if (constraint != null) {
585
586         // Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
587         // inputGLB.add(returnValueLoc);
588         // inputGLB.add(constraint);
589         // returnValueLoc =
590         // CompositeLattice.calculateGLB(inputGLB,
591         // generateErrorMessage(md.getClassDesc(), rn));
592
593         // if (!returnValueLoc.get(returnValueLoc.getSize() - 1).isTop()) {
594         // if (!CompositeLattice.isGreaterThan(constraint, returnValueLoc,
595         // generateErrorMessage(md.getClassDesc(), rn))) {
596         // System.out.println("returnValueLoc.get(returnValueLoc.getSize() - 1).isTop()="
597         // + returnValueLoc.get(returnValueLoc.getSize() - 1).isTop());
598         // throw new Error("The value flow from " + constraint + " to " +
599         // returnValueLoc
600         // + " does not respect location hierarchy on the assignment " +
601         // rn.printNode(0)
602         // + " at " + md.getClassDesc().getSourceFileName() + "::" +
603         // rn.getNumLine());
604         // }
605         // }
606
607         if (!CompositeLattice.isGreaterThan(constraint, declaredReturnLoc,
608             generateErrorMessage(md.getClassDesc(), rn))) {
609           throw new Error("The value flow from " + constraint + " to " + declaredReturnLoc
610               + " does not respect location hierarchy on the assignment " + rn.printNode(0)
611               + " at " + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
612         }
613
614       }
615
616       // check if return value is equal or higher than RETRUNLOC of method
617       // declaration annotation
618
619       int compareResult =
620           CompositeLattice.compare(returnValueLoc, declaredReturnLoc, false,
621               generateErrorMessage(md.getClassDesc(), rn));
622
623       if (compareResult == ComparisonResult.LESS || compareResult == ComparisonResult.INCOMPARABLE) {
624         throw new Error(
625             "Return value location is not equal or higher than the declaraed return location at "
626                 + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
627       }
628     }
629
630     return new CompositeLocation();
631   }
632
633   private boolean hasOnlyLiteralValue(ExpressionNode en) {
634     if (en.kind() == Kind.LiteralNode) {
635       return true;
636     } else {
637       return false;
638     }
639   }
640
641   private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
642       LoopNode ln, CompositeLocation constraint) {
643
644     ClassDescriptor cd = md.getClassDesc();
645     if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
646
647       CompositeLocation condLoc =
648           checkLocationFromExpressionNode(md, nametable, ln.getCondition(),
649               new CompositeLocation(), constraint, false);
650       // addLocationType(ln.getCondition().getType(), (condLoc));
651
652       constraint = generateNewConstraint(constraint, condLoc);
653       checkLocationFromBlockNode(md, nametable, ln.getBody(), constraint);
654
655       return new CompositeLocation();
656
657     } else {
658       // check 'for loop' case
659       BlockNode bn = ln.getInitializer();
660       bn.getVarTable().setParent(nametable);
661       // need to check initialization node
662       // checkLocationFromBlockNode(md, bn.getVarTable(), bn, constraint);
663       for (int i = 0; i < bn.size(); i++) {
664         BlockStatementNode bsn = bn.get(i);
665         checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
666       }
667
668       // calculate glb location of condition and update statements
669       CompositeLocation condLoc =
670           checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
671               new CompositeLocation(), constraint, false);
672       // addLocationType(ln.getCondition().getType(), condLoc);
673
674       constraint = generateNewConstraint(constraint, condLoc);
675
676       checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate(), constraint);
677       checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody(), constraint);
678
679       return new CompositeLocation();
680
681     }
682
683   }
684
685   private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
686       SymbolTable nametable, SubBlockNode sbn, CompositeLocation constraint) {
687     CompositeLocation compLoc =
688         checkLocationFromBlockNode(md, nametable, sbn.getBlockNode(), constraint);
689     return compLoc;
690   }
691
692   private CompositeLocation generateNewConstraint(CompositeLocation currentCon,
693       CompositeLocation newCon) {
694
695     if (currentCon == null) {
696       return newCon;
697     } else {
698       // compute GLB of current constraint and new constraint
699       Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
700       inputSet.add(currentCon);
701       inputSet.add(newCon);
702       return CompositeLattice.calculateGLB(inputSet, "");
703     }
704
705   }
706
707   private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
708       SymbolTable nametable, IfStatementNode isn, CompositeLocation constraint) {
709
710     System.out.println("\n\ncheckLocationFromIfStatementNode=" + isn.printNode(0));
711     CompositeLocation condLoc =
712         checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(),
713             constraint, false);
714
715     System.out.println("-######old constraint=" + constraint);
716     constraint = generateNewConstraint(constraint, condLoc);
717     System.out.println("-######new constraint=" + constraint);
718     checkLocationFromBlockNode(md, nametable, isn.getTrueBlock(), constraint);
719
720     if (isn.getFalseBlock() != null) {
721       checkLocationFromBlockNode(md, nametable, isn.getFalseBlock(), constraint);
722     }
723
724     return new CompositeLocation();
725   }
726
727   private void checkOwnership(MethodDescriptor md, TreeNode tn, ExpressionNode srcExpNode) {
728
729     if (srcExpNode.kind() == Kind.NameNode || srcExpNode.kind() == Kind.FieldAccessNode) {
730       if (srcExpNode.getType().isPtr() && !srcExpNode.getType().isNull()) {
731         // first, check the linear type
732         // RHS reference should be owned by the current method
733         FieldDescriptor fd = getFieldDescriptorFromExpressionNode(srcExpNode);
734         boolean isOwned;
735         if (fd == null) {
736           // local var case
737           isOwned = ((SSJavaType) srcExpNode.getType().getExtension()).isOwned();
738         } else {
739           // field case
740           isOwned = ssjava.isOwnedByMethod(md, fd);
741         }
742         if (!isOwned) {
743           throw new Error(
744               "It is not allowed to create the reference alias from the reference not owned by the method at "
745                   + generateErrorMessage(md.getClassDesc(), tn));
746         }
747
748       }
749     }
750
751   }
752
753   private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
754       SymbolTable nametable, DeclarationNode dn, CompositeLocation constraint) {
755
756     VarDescriptor vd = dn.getVarDescriptor();
757
758     CompositeLocation destLoc = d2loc.get(vd);
759
760     if (dn.getExpression() != null) {
761
762       checkOwnership(md, dn, dn.getExpression());
763
764       CompositeLocation expressionLoc =
765           checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
766               new CompositeLocation(), constraint, false);
767       // addTypeLocation(dn.getExpression().getType(), expressionLoc);
768
769       if (expressionLoc != null) {
770
771         // checking location order
772         if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc,
773             generateErrorMessage(md.getClassDesc(), dn))) {
774           throw new Error("The value flow from " + expressionLoc + " to " + destLoc
775               + " does not respect location hierarchy on the assignment " + dn.printNode(0)
776               + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
777         }
778       }
779       return expressionLoc;
780
781     } else {
782
783       return new CompositeLocation();
784
785     }
786
787   }
788
789   private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
790       SubBlockNode sbn) {
791     checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
792   }
793
794   private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
795       SymbolTable nametable, BlockExpressionNode ben, CompositeLocation constraint) {
796
797     CompositeLocation compLoc =
798         checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null, constraint, false);
799     // addTypeLocation(ben.getExpression().getType(), compLoc);
800     return compLoc;
801   }
802
803   private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
804       SymbolTable nametable, ExpressionNode en, CompositeLocation loc,
805       CompositeLocation constraint, boolean isLHS) {
806
807     CompositeLocation compLoc = null;
808     switch (en.kind()) {
809
810     case Kind.AssignmentNode:
811       compLoc =
812           checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc, constraint);
813       break;
814
815     case Kind.FieldAccessNode:
816       compLoc =
817           checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc, constraint);
818       break;
819
820     case Kind.NameNode:
821       compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc, constraint);
822       break;
823
824     case Kind.OpNode:
825       compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en, constraint);
826       break;
827
828     case Kind.CreateObjectNode:
829       compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
830       break;
831
832     case Kind.ArrayAccessNode:
833       compLoc =
834           checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en, constraint, isLHS);
835       break;
836
837     case Kind.LiteralNode:
838       compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
839       break;
840
841     case Kind.MethodInvokeNode:
842       compLoc =
843           checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc, constraint);
844       break;
845
846     case Kind.TertiaryNode:
847       compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en, constraint);
848       break;
849
850     case Kind.CastNode:
851       compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en, constraint);
852       break;
853
854     // case Kind.InstanceOfNode:
855     // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
856     // return null;
857
858     // case Kind.ArrayInitializerNode:
859     // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
860     // td);
861     // return null;
862
863     // case Kind.ClassTypeNode:
864     // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
865     // return null;
866
867     // case Kind.OffsetNode:
868     // checkOffsetNode(md, nametable, (OffsetNode)en, td);
869     // return null;
870
871     default:
872       return null;
873
874     }
875     // addTypeLocation(en.getType(), compLoc);
876     return compLoc;
877
878   }
879
880   private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
881       CastNode cn, CompositeLocation constraint) {
882
883     ExpressionNode en = cn.getExpression();
884     return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
885         false);
886
887   }
888
889   private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
890       SymbolTable nametable, TertiaryNode tn, CompositeLocation constraint) {
891     ClassDescriptor cd = md.getClassDesc();
892
893     CompositeLocation condLoc =
894         checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(),
895             constraint, false);
896     // addLocationType(tn.getCond().getType(), condLoc);
897     CompositeLocation trueLoc =
898         checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(),
899             constraint, false);
900     // addLocationType(tn.getTrueExpr().getType(), trueLoc);
901     CompositeLocation falseLoc =
902         checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(),
903             constraint, false);
904     // addLocationType(tn.getFalseExpr().getType(), falseLoc);
905
906     // locations from true/false branches can be TOP when there are only literal
907     // values
908     // in this case, we don't need to check flow down rule!
909
910     // System.out.println("\n#tertiary cond=" + tn.getCond().printNode(0) +
911     // " Loc=" + condLoc);
912     // System.out.println("# true=" + tn.getTrueExpr().printNode(0) + " Loc=" +
913     // trueLoc);
914     // System.out.println("# false=" + tn.getFalseExpr().printNode(0) + " Loc="
915     // + falseLoc);
916
917     // we don't need to check that condLoc is higher than trueLoc & falseLoc
918     // if (!trueLoc.get(0).isTop()
919     // && !CompositeLattice.isGreaterThan(condLoc, trueLoc, generateErrorMessage(cd, tn))) {
920     // throw new Error(
921     // "The location of the condition expression is lower than the true expression at "
922     // + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
923     // }
924     //
925     // if (!falseLoc.get(0).isTop()
926     // && !CompositeLattice.isGreaterThan(condLoc, falseLoc,
927     // generateErrorMessage(cd, tn.getCond()))) {
928     // throw new Error(
929     // "The location of the condition expression is lower than the false expression at "
930     // + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
931     // }
932
933     // then, return glb of trueLoc & falseLoc
934     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
935     glbInputSet.add(condLoc);
936     glbInputSet.add(trueLoc);
937     glbInputSet.add(falseLoc);
938
939     if (glbInputSet.size() == 1) {
940       return trueLoc;
941     } else {
942       return CompositeLattice.calculateGLB(glbInputSet, generateErrorMessage(cd, tn));
943     }
944
945   }
946
947   private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
948       SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc,
949       CompositeLocation constraint) {
950
951     ClassDescriptor cd = md.getClassDesc();
952     MethodDescriptor calleeMethodDesc = min.getMethod();
953
954     NameDescriptor baseName = min.getBaseName();
955     boolean isSystemout = false;
956     if (baseName != null) {
957       isSystemout = baseName.getSymbol().equals("System.out");
958     }
959
960     if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
961         && !ssjava.isTrustMethod(calleeMethodDesc) && !calleeMethodDesc.getModifiers().isNative()
962         && !isSystemout) {
963
964       CompositeLocation baseLocation = null;
965       if (min.getExpression() != null) {
966         baseLocation =
967             checkLocationFromExpressionNode(md, nametable, min.getExpression(),
968                 new CompositeLocation(), constraint, false);
969       } else {
970         if (min.getMethod().isStatic()) {
971           String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
972           if (globalLocId == null) {
973             throw new Error("Method lattice does not define global variable location at "
974                 + generateErrorMessage(md.getClassDesc(), min));
975           }
976           baseLocation = new CompositeLocation(new Location(md, globalLocId));
977         } else {
978           String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
979           baseLocation = new CompositeLocation(new Location(md, thisLocId));
980         }
981       }
982
983       // System.out.println("\n#checkLocationFromMethodInvokeNode=" +
984       // min.printNode(0)
985       // + " baseLocation=" + baseLocation + " constraint=" + constraint);
986
987       // setup the location list of caller's arguments
988       List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
989
990       // setup the location list of callee's parameters
991       MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleeMethodDesc);
992       List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
993
994       if (min.numArgs() > 0) {
995         if (!calleeMethodDesc.isStatic()) {
996           callerArgList.add(baseLocation);
997         }
998         for (int i = 0; i < min.numArgs(); i++) {
999           ExpressionNode en = min.getArg(i);
1000           CompositeLocation callerArgLoc =
1001               checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(),
1002                   constraint, false);
1003           callerArgList.add(callerArgLoc);
1004         }
1005
1006         if (!calleeMethodDesc.isStatic()) {
1007           CompositeLocation calleeThisLoc =
1008               new CompositeLocation(new Location(calleeMethodDesc, calleeLattice.getThisLoc()));
1009           calleeParamList.add(calleeThisLoc);
1010         }
1011
1012         for (int i = 0; i < calleeMethodDesc.numParameters(); i++) {
1013           VarDescriptor calleevd = (VarDescriptor) calleeMethodDesc.getParameter(i);
1014           CompositeLocation calleeLoc = d2loc.get(calleevd);
1015           calleeParamList.add(calleeLoc);
1016         }
1017       }
1018
1019       if (constraint != null) {
1020         // check whether the PC location is lower than one of the
1021         // argument locations. If it is lower, the callee has to have @PCLOC
1022         // annotation that declares the program counter that is higher than
1023         // corresponding parameter
1024
1025         CompositeLocation calleePCLOC = ssjava.getPCLocation(calleeMethodDesc);
1026
1027         for (int idx = 0; idx < callerArgList.size(); idx++) {
1028           CompositeLocation argLocation = callerArgList.get(idx);
1029
1030           // if the PC location is lower than an argument location
1031           // then, need to check that the callee's the initial location for the PC is also lower
1032           // than the corresponding parameter location
1033
1034           if (!argLocation.get(0).isTop()
1035               && CompositeLattice.compare(argLocation, constraint, true,
1036                   generateErrorMessage(cd, min)) == ComparisonResult.GREATER) {
1037
1038             CompositeLocation paramLocation = calleeParamList.get(idx);
1039
1040             int paramCompareResult =
1041                 CompositeLattice.compare(paramLocation, calleePCLOC, true,
1042                     generateErrorMessage(cd, min));
1043             // CompositeLattice.compare(calleePCLOC, paramLocation, true,
1044             // generateErrorMessage(cd, min));
1045
1046             System.out.println("-CHECKPCLOC:");
1047             System.out.println("---ARG LOC="
1048                 + argLocation
1049                 + " callerPCLOC="
1050                 + constraint
1051                 + " result="
1052                 + CompositeLattice.compare(argLocation, constraint, true,
1053                     generateErrorMessage(cd, min)));
1054             System.out.println("---PARAM LOC=" + paramLocation + " calleePCLOC=" + calleePCLOC
1055                 + " paramCompareResult=" + paramCompareResult);
1056
1057             if (!(paramLocation.get(0).equals(calleePCLOC.get(0)) && calleePCLOC.getSize() > 1)
1058                 && paramCompareResult == ComparisonResult.LESS) {
1059               throw new Error(
1060                   "The argument(idx="
1061                       + idx
1062                       + ") location "
1063                       + argLocation
1064                       + "is higher than the caller program counter location "
1065                       + constraint
1066                       + ". Need to specify that the initial PC location of the callee, which is currently set to "
1067                       + calleePCLOC + ", is not higher than " + paramLocation + " in the method "
1068                       + calleeMethodDesc.getSymbol() + ":" + min.getNumLine());
1069             }
1070
1071           }
1072
1073         }
1074
1075       }
1076
1077       checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
1078
1079       checkCallerArgumentLocationConstraints(md, nametable, min, baseLocation, constraint);
1080
1081       if (!min.getMethod().getReturnType().isVoid()) {
1082         // If method has a return value, compute the highest possible return
1083         // location in the caller's perspective
1084         CompositeLocation ceilingLoc =
1085             computeCeilingLocationForCaller(md, nametable, min, baseLocation, constraint);
1086
1087         if (ceilingLoc == null) {
1088           return new CompositeLocation(Location.createTopLocation(md));
1089         }
1090         return ceilingLoc;
1091       }
1092     }
1093
1094     return new CompositeLocation(Location.createTopLocation(md));
1095
1096   }
1097
1098   private CompositeLocation translateCallerLocToCalleeLoc(MethodDescriptor calleeMD,
1099       CompositeLocation calleeBaseLoc, CompositeLocation constraint) {
1100
1101     CompositeLocation calleeConstraint = new CompositeLocation();
1102
1103     // if (constraint.startsWith(calleeBaseLoc)) {
1104     // if the first part of constraint loc is matched with callee base loc
1105     Location thisLoc = new Location(calleeMD, ssjava.getMethodLattice(calleeMD).getThisLoc());
1106     calleeConstraint.addLocation(thisLoc);
1107     for (int i = calleeBaseLoc.getSize(); i < constraint.getSize(); i++) {
1108       calleeConstraint.addLocation(constraint.get(i));
1109     }
1110
1111     // }
1112
1113     return calleeConstraint;
1114   }
1115
1116   private void checkCallerArgumentLocationConstraints(MethodDescriptor md, SymbolTable nametable,
1117       MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
1118     // if parameter location consists of THIS and FIELD location,
1119     // caller should pass an argument that is comparable to the declared
1120     // parameter location
1121     // and is not lower than the declared parameter location in the field
1122     // lattice.
1123
1124     MethodDescriptor calleemd = min.getMethod();
1125
1126     if (calleemd.isStatic()) {
1127       return;
1128     }
1129
1130     List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
1131     List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
1132
1133     MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
1134     Location calleeThisLoc = new Location(calleemd, calleeLattice.getThisLoc());
1135
1136     for (int i = 0; i < min.numArgs(); i++) {
1137       ExpressionNode en = min.getArg(i);
1138       CompositeLocation callerArgLoc =
1139           checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1140               false);
1141       callerArgList.add(callerArgLoc);
1142     }
1143
1144     // setup callee params set
1145     for (int i = 0; i < calleemd.numParameters(); i++) {
1146       VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1147       CompositeLocation calleeLoc = d2loc.get(calleevd);
1148       calleeParamList.add(calleeLoc);
1149     }
1150
1151     String errorMsg = generateErrorMessage(md.getClassDesc(), min);
1152
1153     // System.out.println("checkCallerArgumentLocationConstraints=" +
1154     // min.printNode(0));
1155     // System.out.println("base location=" + callerBaseLoc + " constraint=" +
1156     // constraint);
1157
1158     for (int i = 0; i < calleeParamList.size(); i++) {
1159       CompositeLocation calleeParamLoc = calleeParamList.get(i);
1160       if (calleeParamLoc.get(0).equals(calleeThisLoc) && calleeParamLoc.getSize() > 1) {
1161
1162         // callee parameter location has field information
1163         CompositeLocation callerArgLoc = callerArgList.get(i);
1164
1165         CompositeLocation paramLocation =
1166             translateCalleeParamLocToCaller(md, calleeParamLoc, callerBaseLoc, errorMsg);
1167
1168         Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1169         if (constraint != null) {
1170           inputGLBSet.add(callerArgLoc);
1171           inputGLBSet.add(constraint);
1172           callerArgLoc =
1173               CompositeLattice.calculateGLB(inputGLBSet,
1174                   generateErrorMessage(md.getClassDesc(), min));
1175         }
1176
1177         Location argLastLoc = callerArgLoc.get(callerArgLoc.getSize() - 1);
1178         Location paramLastLoc = paramLocation.get(paramLocation.getSize() - 1);
1179
1180         if (argLastLoc.equals(paramLastLoc) && ssjava.isSharedLocation(argLastLoc)
1181             && ssjava.isSharedLocation(paramLastLoc)) {
1182           continue;
1183         }
1184
1185         // if (!CompositeLattice.isGreaterThan(callerArgLoc, paramLocation, errorMsg)) {
1186         if (CompositeLattice.compare(callerArgLoc, paramLocation, true, errorMsg) == ComparisonResult.LESS) {
1187           throw new Error("Caller argument '" + min.getArg(i).printNode(0) + " : " + callerArgLoc
1188               + "' should be higher than corresponding callee's parameter : " + paramLocation
1189               + " at " + errorMsg);
1190         }
1191
1192       }
1193     }
1194
1195   }
1196
1197   private CompositeLocation translateCalleeParamLocToCaller(MethodDescriptor md,
1198       CompositeLocation calleeParamLoc, CompositeLocation callerBaseLocation, String errorMsg) {
1199
1200     CompositeLocation translate = new CompositeLocation();
1201
1202     for (int i = 0; i < callerBaseLocation.getSize(); i++) {
1203       translate.addLocation(callerBaseLocation.get(i));
1204     }
1205
1206     for (int i = 1; i < calleeParamLoc.getSize(); i++) {
1207       translate.addLocation(calleeParamLoc.get(i));
1208     }
1209
1210     // System.out.println("TRANSLATED=" + translate + " from calleeParamLoc=" +
1211     // calleeParamLoc);
1212
1213     return translate;
1214   }
1215
1216   private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
1217       SymbolTable nametable, MethodInvokeNode min, CompositeLocation baseLocation,
1218       CompositeLocation constraint) {
1219     List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
1220
1221     // by default, method has a THIS parameter
1222     if (!min.getMethod().isStatic()) {
1223       argList.add(baseLocation);
1224     }
1225
1226     for (int i = 0; i < min.numArgs(); i++) {
1227       ExpressionNode en = min.getArg(i);
1228       CompositeLocation callerArg =
1229           checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1230               false);
1231       argList.add(callerArg);
1232     }
1233
1234     // System.out.println("\n## computeReturnLocation=" + min.getMethod() + " argList=" + argList);
1235     CompositeLocation ceilLoc = md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
1236     // System.out.println("## ReturnLocation=" + ceilLoc);
1237
1238     return ceilLoc;
1239
1240   }
1241
1242   private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
1243       MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
1244
1245     MethodDescriptor calleemd = min.getMethod();
1246
1247     MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
1248
1249     System.out.println("checkCalleeConstraints=" + calleemd + " calleeLattice.getThisLoc()="
1250         + calleeLattice.getThisLoc());
1251
1252     List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
1253     List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
1254
1255     if (min.numArgs() > 0) {
1256       // caller needs to guarantee that it passes arguments in regarding to
1257       // callee's hierarchy
1258
1259       // setup caller args set
1260       // first, add caller's base(this) location
1261       if (!calleemd.isStatic())
1262         callerArgList.add(callerBaseLoc);
1263       // second, add caller's arguments
1264       for (int i = 0; i < min.numArgs(); i++) {
1265         ExpressionNode en = min.getArg(i);
1266         CompositeLocation callerArgLoc =
1267             checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1268                 false);
1269         callerArgList.add(callerArgLoc);
1270       }
1271
1272       // setup callee params set
1273       // first, add callee's this location
1274       if (!calleemd.isStatic()) {
1275         CompositeLocation calleeThisLoc =
1276             new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
1277         calleeParamList.add(calleeThisLoc);
1278       }
1279       // second, add callee's parameters
1280       for (int i = 0; i < calleemd.numParameters(); i++) {
1281         VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1282         CompositeLocation calleeLoc = d2loc.get(calleevd);
1283         // System.out.println("calleevd=" + calleevd + " loc=" + calleeLoc);
1284         calleeParamList.add(calleeLoc);
1285       }
1286
1287       // here, check if ordering relations among caller's args respect
1288       // ordering relations in-between callee's args
1289       CHECK: for (int i = 0; i < calleeParamList.size(); i++) {
1290         CompositeLocation calleeLoc1 = calleeParamList.get(i);
1291         CompositeLocation callerLoc1 = callerArgList.get(i);
1292
1293         for (int j = 0; j < calleeParamList.size(); j++) {
1294           if (i != j) {
1295             CompositeLocation calleeLoc2 = calleeParamList.get(j);
1296             CompositeLocation callerLoc2 = callerArgList.get(j);
1297
1298             if (callerLoc1.get(callerLoc1.getSize() - 1).isTop()
1299                 || callerLoc2.get(callerLoc2.getSize() - 1).isTop()) {
1300               continue CHECK;
1301             }
1302
1303             // System.out.println("calleeLoc1=" + calleeLoc1);
1304             // System.out.println("calleeLoc2=" + calleeLoc2 +
1305             // "calleeParamList=" + calleeParamList);
1306
1307             int callerResult =
1308                 CompositeLattice.compare(callerLoc1, callerLoc2, true,
1309                     generateErrorMessage(md.getClassDesc(), min));
1310             // System.out.println("callerResult=" + callerResult);
1311             int calleeResult =
1312                 CompositeLattice.compare(calleeLoc1, calleeLoc2, true,
1313                     generateErrorMessage(md.getClassDesc(), min));
1314             // System.out.println("calleeResult=" + calleeResult);
1315
1316             if (callerResult == ComparisonResult.EQUAL) {
1317               if (ssjava.isSharedLocation(callerLoc1.get(callerLoc1.getSize() - 1))
1318                   && ssjava.isSharedLocation(callerLoc2.get(callerLoc2.getSize() - 1))) {
1319                 // if both of them are shared locations, promote them to
1320                 // "GREATER relation"
1321                 callerResult = ComparisonResult.GREATER;
1322               }
1323             }
1324
1325             if (calleeResult == ComparisonResult.GREATER
1326                 && callerResult != ComparisonResult.GREATER) {
1327               // If calleeLoc1 is higher than calleeLoc2
1328               // then, caller should have same ordering relation in-bet
1329               // callerLoc1 & callerLoc2
1330
1331               String paramName1, paramName2;
1332
1333               if (!calleemd.isStatic()) {
1334                 if (i == 0) {
1335                   paramName1 = "'THIS'";
1336                 } else {
1337                   paramName1 = "'parameter " + calleemd.getParamName(i - 1) + "'";
1338                 }
1339               } else {
1340                 paramName1 = "'parameter " + calleemd.getParamName(i) + "'";
1341               }
1342
1343               if (!calleemd.isStatic()) {
1344                 if (j == 0 && !calleemd.isStatic()) {
1345                   paramName2 = "'THIS'";
1346                 } else {
1347                   paramName2 = "'parameter " + calleemd.getParamName(j - 1) + "'";
1348                 }
1349               } else {
1350                 paramName2 = "'parameter " + calleemd.getParamName(j) + "'";
1351               }
1352
1353               throw new Error(
1354                   "Caller doesn't respect an ordering relation among method arguments: callee expects that "
1355                       + paramName1 + " should be higher than " + paramName2 + " in " + calleemd
1356                       + " at " + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
1357             }
1358           }
1359
1360         }
1361       }
1362
1363     }
1364
1365   }
1366
1367   private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
1368       SymbolTable nametable, ArrayAccessNode aan, CompositeLocation constraint, boolean isLHS) {
1369     System.out.println("aan=" + aan.printNode(0) + "  line#=" + aan.getNumLine());
1370     ClassDescriptor cd = md.getClassDesc();
1371     System.out.println("aan.getExpression()=" + aan.getExpression().getClass());
1372     CompositeLocation arrayLoc =
1373         checkLocationFromExpressionNode(md, nametable, aan.getExpression(),
1374             new CompositeLocation(), constraint, isLHS);
1375     System.out.println("HERE?");
1376     // addTypeLocation(aan.getExpression().getType(), arrayLoc);
1377     CompositeLocation indexLoc =
1378         checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(),
1379             constraint, isLHS);
1380     // addTypeLocation(aan.getIndex().getType(), indexLoc);
1381     System.out.println("HERE2?");
1382
1383     if (isLHS) {
1384       if (!CompositeLattice.isGreaterThan(indexLoc, arrayLoc, generateErrorMessage(cd, aan))) {
1385         throw new Error("Array index value is not higher than array location at "
1386             + generateErrorMessage(cd, aan));
1387       }
1388       return arrayLoc;
1389     } else {
1390       Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
1391       inputGLB.add(arrayLoc);
1392       inputGLB.add(indexLoc);
1393       System.out.println("arrayLoc=" + arrayLoc + "   indexLoc=" + indexLoc);
1394       CompositeLocation comp =
1395           CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(cd, aan));
1396       System.out.println("---aan=" + aan.printNode(0) + "  compLoc=" + comp);
1397       return comp;
1398     }
1399
1400   }
1401
1402   private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
1403       SymbolTable nametable, CreateObjectNode con) {
1404
1405     ClassDescriptor cd = md.getClassDesc();
1406
1407     CompositeLocation compLoc = new CompositeLocation();
1408     compLoc.addLocation(Location.createTopLocation(md));
1409     return compLoc;
1410
1411   }
1412
1413   private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
1414       OpNode on, CompositeLocation constraint) {
1415
1416     ClassDescriptor cd = md.getClassDesc();
1417     CompositeLocation leftLoc = new CompositeLocation();
1418     leftLoc =
1419         checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc, constraint, false);
1420     // addTypeLocation(on.getLeft().getType(), leftLoc);
1421
1422     CompositeLocation rightLoc = new CompositeLocation();
1423     if (on.getRight() != null) {
1424       rightLoc =
1425           checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc, constraint, false);
1426       // addTypeLocation(on.getRight().getType(), rightLoc);
1427     }
1428
1429     System.out.println("\n# OP NODE=" + on.printNode(0));
1430     // System.out.println("# left loc=" + leftLoc + " from " +
1431     // on.getLeft().getClass());
1432     // if (on.getRight() != null) {
1433     // System.out.println("# right loc=" + rightLoc + " from " +
1434     // on.getRight().getClass());
1435     // }
1436
1437     Operation op = on.getOp();
1438
1439     switch (op.getOp()) {
1440
1441     case Operation.UNARYPLUS:
1442     case Operation.UNARYMINUS:
1443     case Operation.LOGIC_NOT:
1444       // single operand
1445       return leftLoc;
1446
1447     case Operation.LOGIC_OR:
1448     case Operation.LOGIC_AND:
1449     case Operation.COMP:
1450     case Operation.BIT_OR:
1451     case Operation.BIT_XOR:
1452     case Operation.BIT_AND:
1453     case Operation.ISAVAILABLE:
1454     case Operation.EQUAL:
1455     case Operation.NOTEQUAL:
1456     case Operation.LT:
1457     case Operation.GT:
1458     case Operation.LTE:
1459     case Operation.GTE:
1460     case Operation.ADD:
1461     case Operation.SUB:
1462     case Operation.MULT:
1463     case Operation.DIV:
1464     case Operation.MOD:
1465     case Operation.LEFTSHIFT:
1466     case Operation.RIGHTSHIFT:
1467     case Operation.URIGHTSHIFT:
1468
1469       Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
1470       inputSet.add(leftLoc);
1471       inputSet.add(rightLoc);
1472       CompositeLocation glbCompLoc =
1473           CompositeLattice.calculateGLB(inputSet, generateErrorMessage(cd, on));
1474       return glbCompLoc;
1475
1476     default:
1477       throw new Error(op.toString());
1478     }
1479
1480   }
1481
1482   private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
1483       SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
1484
1485     // literal value has the top location so that value can be flowed into any
1486     // location
1487     Location literalLoc = Location.createTopLocation(md);
1488     loc.addLocation(literalLoc);
1489     return loc;
1490
1491   }
1492
1493   private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
1494       NameNode nn, CompositeLocation loc, CompositeLocation constraint) {
1495
1496     // System.out.println("checkLocationFromNameNode nn=" + nn.printNode(0));
1497     NameDescriptor nd = nn.getName();
1498     if (nd.getBase() != null) {
1499       loc =
1500           checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc, constraint, false);
1501     } else {
1502       String varname = nd.toString();
1503       if (varname.equals("this")) {
1504         // 'this' itself!
1505         MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
1506         String thisLocId = methodLattice.getThisLoc();
1507         if (thisLocId == null) {
1508           throw new Error("The location for 'this' is not defined at "
1509               + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
1510         }
1511         Location locElement = new Location(md, thisLocId);
1512         loc.addLocation(locElement);
1513         return loc;
1514
1515       }
1516
1517       Descriptor d = (Descriptor) nametable.get(varname);
1518
1519       // CompositeLocation localLoc = null;
1520       if (d instanceof VarDescriptor) {
1521         VarDescriptor vd = (VarDescriptor) d;
1522         // localLoc = d2loc.get(vd);
1523         // the type of var descriptor has a composite location!
1524         loc = ((SSJavaType) vd.getType().getExtension()).getCompLoc().clone();
1525       } else if (d instanceof FieldDescriptor) {
1526         // the type of field descriptor has a location!
1527         FieldDescriptor fd = (FieldDescriptor) d;
1528         // System.out.println("fd=" + fd);
1529         if (fd.isStatic()) {
1530           if (fd.isFinal()) {
1531             // if it is 'static final', the location has TOP since no one can
1532             // change its value
1533             loc.addLocation(Location.createTopLocation(md));
1534             return loc;
1535           } else {
1536
1537             // if 'static', the location has pre-assigned global loc
1538             MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1539             String globalLocId = localLattice.getGlobalLoc();
1540             if (globalLocId == null) {
1541               throw new Error("Global location element is not defined in the method " + md);
1542             }
1543             Location globalLoc = new Location(md, globalLocId);
1544             System.out.println("static=" + globalLoc);
1545             loc.addLocation(globalLoc);
1546           }
1547         } else {
1548           // the location of field access starts from this, followed by field
1549           // location
1550           MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1551           Location thisLoc = new Location(md, localLattice.getThisLoc());
1552           loc.addLocation(thisLoc);
1553         }
1554
1555         Location fieldLoc = (Location) fd.getType().getExtension();
1556         loc.addLocation(fieldLoc);
1557       } else if (d == null) {
1558         // access static field
1559         FieldDescriptor fd = nn.getField();
1560
1561         MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1562         String globalLocId = localLattice.getGlobalLoc();
1563         if (globalLocId == null) {
1564           throw new Error("Method lattice does not define global variable location at "
1565               + generateErrorMessage(md.getClassDesc(), nn));
1566         }
1567         loc.addLocation(new Location(md, globalLocId));
1568
1569         Location fieldLoc = (Location) fd.getType().getExtension();
1570         loc.addLocation(fieldLoc);
1571
1572         return loc;
1573
1574       }
1575     }
1576
1577     return loc;
1578   }
1579
1580   private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
1581       SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc,
1582       CompositeLocation constraint) {
1583
1584     ExpressionNode left = fan.getExpression();
1585     TypeDescriptor ltd = left.getType();
1586
1587     FieldDescriptor fd = fan.getField();
1588
1589     String varName = null;
1590     if (left.kind() == Kind.NameNode) {
1591       NameDescriptor nd = ((NameNode) left).getName();
1592       varName = nd.toString();
1593     }
1594
1595     if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
1596       // using a class name directly or access using this
1597       if (fd.isStatic() && fd.isFinal()) {
1598         loc.addLocation(Location.createTopLocation(md));
1599         return loc;
1600       }
1601     }
1602
1603     Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
1604     if (left instanceof ArrayAccessNode) {
1605       ArrayAccessNode aan = (ArrayAccessNode) left;
1606       CompositeLocation indexLoc =
1607           checkLocationFromExpressionNode(md, nametable, aan.getIndex(), loc, constraint, false);
1608       inputGLB.add(indexLoc);
1609     }
1610
1611     loc = checkLocationFromExpressionNode(md, nametable, left, loc, constraint, false);
1612
1613     if (!left.getType().isPrimitive()) {
1614
1615       if (!fd.getSymbol().equals("length")) {
1616         // array.length access, return the location of the array
1617         Location fieldLoc = getFieldLocation(fd);
1618         loc.addLocation(fieldLoc);
1619       }
1620
1621     }
1622
1623     inputGLB.add(loc);
1624     loc = CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(md.getClassDesc(), fan));
1625
1626     return loc;
1627   }
1628
1629   private Location getFieldLocation(FieldDescriptor fd) {
1630
1631     // System.out.println("### getFieldLocation=" + fd);
1632     // System.out.println("### fd.getType().getExtension()=" +
1633     // fd.getType().getExtension());
1634
1635     Location fieldLoc = (Location) fd.getType().getExtension();
1636
1637     // handle the case that method annotation checking skips checking field
1638     // declaration
1639     if (fieldLoc == null) {
1640       fieldLoc = checkFieldDeclaration(fd.getClassDescriptor(), fd);
1641     }
1642
1643     return fieldLoc;
1644
1645   }
1646
1647   private FieldDescriptor getFieldDescriptorFromExpressionNode(ExpressionNode en) {
1648
1649     if (en.kind() == Kind.NameNode) {
1650       NameNode nn = (NameNode) en;
1651       if (nn.getField() != null) {
1652         return nn.getField();
1653       }
1654
1655       if (nn.getName() != null && nn.getName().getBase() != null) {
1656         return getFieldDescriptorFromExpressionNode(nn.getExpression());
1657       }
1658
1659     } else if (en.kind() == Kind.FieldAccessNode) {
1660       FieldAccessNode fan = (FieldAccessNode) en;
1661       return fan.getField();
1662     }
1663
1664     return null;
1665   }
1666
1667   private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
1668       SymbolTable nametable, AssignmentNode an, CompositeLocation loc, CompositeLocation constraint) {
1669
1670     System.out.println("\n\ncheckLocationFromAssignmentNode=" + an.printNode(0));
1671
1672     ClassDescriptor cd = md.getClassDesc();
1673
1674     Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1675
1676     boolean postinc = true;
1677     if (an.getOperation().getBaseOp() == null
1678         || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
1679             .getBaseOp().getOp() != Operation.POSTDEC))
1680       postinc = false;
1681
1682     // if LHS is array access node, need to check if array index is higher
1683     // than array itself
1684     CompositeLocation destLocation =
1685         checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1686             constraint, true);
1687
1688     CompositeLocation rhsLocation;
1689     CompositeLocation srcLocation;
1690
1691     if (!postinc) {
1692
1693       checkOwnership(md, an, an.getSrc());
1694
1695       rhsLocation =
1696           checkLocationFromExpressionNode(md, nametable, an.getSrc(), new CompositeLocation(),
1697               constraint, false);
1698
1699       if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
1700         // if assignment contains OP+EQ operator, need to merge location types
1701         // of LHS & RHS into the RHS
1702         Set<CompositeLocation> srcGLBSet = new HashSet<CompositeLocation>();
1703         srcGLBSet.add(rhsLocation);
1704         srcGLBSet.add(destLocation);
1705         srcLocation = CompositeLattice.calculateGLB(srcGLBSet, generateErrorMessage(cd, an));
1706       } else {
1707         srcLocation = rhsLocation;
1708       }
1709
1710       if (constraint != null) {
1711
1712         if (!CompositeLattice.isGreaterThan(constraint, destLocation, generateErrorMessage(cd, an))) {
1713           throw new Error("The value flow from " + constraint + " to " + destLocation
1714               + " does not respect location hierarchy on the assignment " + an.printNode(0)
1715               + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1716         }
1717         // inputGLBSet.add(srcLocation);
1718         // inputGLBSet.add(constraint);
1719         // srcLocation = CompositeLattice.calculateGLB(inputGLBSet,
1720         // generateErrorMessage(cd, an));
1721       }
1722
1723       System.out.println("src=" + srcLocation + "  dest=" + destLocation + "  const=" + constraint);
1724
1725       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1726
1727         String context = "";
1728         if (constraint != null) {
1729           context = " and the current context constraint is " + constraint;
1730         }
1731
1732         throw new Error("The value flow from " + srcLocation + " to " + destLocation
1733             + " does not respect location hierarchy on the assignment " + an.printNode(0) + context
1734             + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1735       }
1736
1737       if (srcLocation.equals(destLocation)) {
1738         // keep it for definitely written analysis
1739         Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1740         for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1741           FlatNode fn = (FlatNode) iterator.next();
1742           ssjava.addSameHeightWriteFlatNode(fn);
1743         }
1744
1745       }
1746
1747     } else {
1748       destLocation =
1749           rhsLocation =
1750               checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1751                   constraint, false);
1752
1753       if (constraint != null) {
1754
1755         if (!CompositeLattice.isGreaterThan(constraint, destLocation, generateErrorMessage(cd, an))) {
1756           throw new Error("The value flow from " + constraint + " to " + destLocation
1757               + " does not respect location hierarchy on the assignment " + an.printNode(0)
1758               + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1759         }
1760         // inputGLBSet.add(rhsLocation);
1761         // inputGLBSet.add(constraint);
1762         // srcLocation = CompositeLattice.calculateGLB(inputGLBSet,
1763         // generateErrorMessage(cd, an));
1764         srcLocation = rhsLocation;
1765       } else {
1766         srcLocation = rhsLocation;
1767       }
1768
1769       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1770
1771         if (srcLocation.equals(destLocation)) {
1772           throw new Error("Location " + srcLocation
1773               + " is not allowed to have the value flow that moves within the same location at '"
1774               + an.printNode(0) + "' of " + cd.getSourceFileName() + "::" + an.getNumLine());
1775         } else {
1776           throw new Error("The value flow from " + srcLocation + " to " + destLocation
1777               + " does not respect location hierarchy on the assignment " + an.printNode(0)
1778               + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1779         }
1780
1781       }
1782
1783       if (srcLocation.equals(destLocation)) {
1784         // keep it for definitely written analysis
1785         Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1786         for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1787           FlatNode fn = (FlatNode) iterator.next();
1788           ssjava.addSameHeightWriteFlatNode(fn);
1789         }
1790       }
1791
1792     }
1793
1794     return destLocation;
1795   }
1796
1797   private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
1798       SymbolTable nametable, TreeNode n) {
1799
1800     ClassDescriptor cd = md.getClassDesc();
1801     Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
1802
1803     // currently enforce every variable to have corresponding location
1804     if (annotationVec.size() == 0) {
1805       throw new Error("Location is not assigned to variable '" + vd.getSymbol()
1806           + "' in the method '" + md + "' of the class " + cd.getSymbol() + " at "
1807           + generateErrorMessage(cd, n));
1808     }
1809
1810     int locDecCount = 0;
1811     for (int i = 0; i < annotationVec.size(); i++) {
1812       AnnotationDescriptor ad = annotationVec.elementAt(i);
1813
1814       if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1815
1816         if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1817           locDecCount++;
1818           if (locDecCount > 1) {// variable can have at most one location
1819             throw new Error(vd.getSymbol() + " has more than one location declaration.");
1820           }
1821           String locDec = ad.getValue(); // check if location is defined
1822
1823           if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
1824             DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
1825             d2loc.put(vd, deltaLoc);
1826             addLocationType(vd.getType(), deltaLoc);
1827           } else {
1828             CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1829
1830             Location lastElement = compLoc.get(compLoc.getSize() - 1);
1831             if (ssjava.isSharedLocation(lastElement)) {
1832               ssjava.mapSharedLocation2Descriptor(lastElement, vd);
1833             }
1834
1835             d2loc.put(vd, compLoc);
1836             addLocationType(vd.getType(), compLoc);
1837           }
1838
1839         }
1840       }
1841     }
1842
1843   }
1844
1845   private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1846
1847     int deltaCount = 0;
1848     int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1849     while (dIdx >= 0) {
1850       deltaCount++;
1851       int beginIdx = dIdx + 6;
1852       locDec = locDec.substring(beginIdx, locDec.length() - 1);
1853       dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1854     }
1855
1856     CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1857     DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
1858
1859     return deltaLoc;
1860   }
1861
1862   private Location parseFieldLocDeclaraton(String decl, String msg) throws Exception {
1863
1864     int idx = decl.indexOf(".");
1865
1866     String className = decl.substring(0, idx);
1867     String fieldName = decl.substring(idx + 1);
1868
1869     className.replaceAll(" ", "");
1870     fieldName.replaceAll(" ", "");
1871
1872     Descriptor d = state.getClassSymbolTable().get(className);
1873
1874     if (d == null) {
1875       // System.out.println("state.getClassSymbolTable()=" +
1876       // state.getClassSymbolTable());
1877       throw new Error("The class in the location declaration '" + decl + "' does not exist at "
1878           + msg);
1879     }
1880
1881     assert (d instanceof ClassDescriptor);
1882     SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1883     if (!lattice.containsKey(fieldName)) {
1884       throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1885           + className + "' at " + msg);
1886     }
1887
1888     return new Location(d, fieldName);
1889   }
1890
1891   private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1892
1893     CompositeLocation compLoc = new CompositeLocation();
1894
1895     StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1896     List<String> locIdList = new ArrayList<String>();
1897     while (tokenizer.hasMoreTokens()) {
1898       String locId = tokenizer.nextToken();
1899       locIdList.add(locId);
1900     }
1901
1902     // at least,one location element needs to be here!
1903     assert (locIdList.size() > 0);
1904
1905     // assume that loc with idx 0 comes from the local lattice
1906     // loc with idx 1 comes from the field lattice
1907
1908     String localLocId = locIdList.get(0);
1909     SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1910     Location localLoc = new Location(md, localLocId);
1911     if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1912       throw new Error("Location " + localLocId
1913           + " is not defined in the local variable lattice at "
1914           + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : md) + ".");
1915     }
1916     compLoc.addLocation(localLoc);
1917
1918     for (int i = 1; i < locIdList.size(); i++) {
1919       String locName = locIdList.get(i);
1920       try {
1921         Location fieldLoc =
1922             parseFieldLocDeclaraton(locName, generateErrorMessage(md.getClassDesc(), n));
1923         compLoc.addLocation(fieldLoc);
1924       } catch (Exception e) {
1925         throw new Error("The location declaration '" + locName + "' is wrong  at "
1926             + generateErrorMessage(md.getClassDesc(), n));
1927       }
1928     }
1929
1930     return compLoc;
1931
1932   }
1933
1934   private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1935     VarDescriptor vd = dn.getVarDescriptor();
1936     assignLocationOfVarDescriptor(vd, md, nametable, dn);
1937   }
1938
1939   private void checkDeclarationInClass(ClassDescriptor cd) {
1940     // Check to see that fields are okay
1941     for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1942       FieldDescriptor fd = (FieldDescriptor) field_it.next();
1943
1944       if (!(fd.isFinal() && fd.isStatic())) {
1945         checkFieldDeclaration(cd, fd);
1946       } else {
1947         // for static final, assign top location by default
1948         Location loc = Location.createTopLocation(cd);
1949         addLocationType(fd.getType(), loc);
1950       }
1951     }
1952   }
1953
1954   private Location checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1955
1956     Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1957
1958     // currently enforce every field to have corresponding location
1959     if (annotationVec.size() == 0) {
1960       throw new Error("Location is not assigned to the field '" + fd.getSymbol()
1961           + "' of the class " + cd.getSymbol() + " at " + cd.getSourceFileName());
1962     }
1963
1964     if (annotationVec.size() > 1) {
1965       // variable can have at most one location
1966       throw new Error("Field " + fd.getSymbol() + " of class " + cd
1967           + " has more than one location.");
1968     }
1969
1970     AnnotationDescriptor ad = annotationVec.elementAt(0);
1971     Location loc = null;
1972
1973     if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1974       if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1975         String locationID = ad.getValue();
1976         // check if location is defined
1977         SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1978         if (lattice == null || (!lattice.containsKey(locationID))) {
1979           throw new Error("Location " + locationID
1980               + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1981               + cd.getSourceFileName() + ".");
1982         }
1983         loc = new Location(cd, locationID);
1984
1985         if (ssjava.isSharedLocation(loc)) {
1986           ssjava.mapSharedLocation2Descriptor(loc, fd);
1987         }
1988
1989         addLocationType(fd.getType(), loc);
1990
1991       }
1992     }
1993
1994     return loc;
1995   }
1996
1997   private void addLocationType(TypeDescriptor type, CompositeLocation loc) {
1998     if (type != null) {
1999       TypeExtension te = type.getExtension();
2000       SSJavaType ssType;
2001       if (te != null) {
2002         ssType = (SSJavaType) te;
2003         ssType.setCompLoc(loc);
2004       } else {
2005         ssType = new SSJavaType(loc);
2006         type.setExtension(ssType);
2007       }
2008     }
2009   }
2010
2011   private void addLocationType(TypeDescriptor type, Location loc) {
2012     if (type != null) {
2013       type.setExtension(loc);
2014     }
2015   }
2016
2017   static class CompositeLattice {
2018
2019     public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2, String msg) {
2020
2021       // System.out.println("\nisGreaterThan=" + loc1 + " " + loc2 + " msg=" +
2022       // msg);
2023       int baseCompareResult = compareBaseLocationSet(loc1, loc2, true, false, msg);
2024       if (baseCompareResult == ComparisonResult.EQUAL) {
2025         if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
2026           return true;
2027         } else {
2028           return false;
2029         }
2030       } else if (baseCompareResult == ComparisonResult.GREATER) {
2031         return true;
2032       } else {
2033         return false;
2034       }
2035
2036     }
2037
2038     public static int compare(CompositeLocation loc1, CompositeLocation loc2, boolean ignore,
2039         String msg) {
2040
2041       // System.out.println("compare=" + loc1 + " " + loc2);
2042       int baseCompareResult = compareBaseLocationSet(loc1, loc2, false, ignore, msg);
2043
2044       if (baseCompareResult == ComparisonResult.EQUAL) {
2045         return compareDelta(loc1, loc2);
2046       } else {
2047         return baseCompareResult;
2048       }
2049
2050     }
2051
2052     private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
2053
2054       int deltaCount1 = 0;
2055       int deltaCount2 = 0;
2056       if (dLoc1 instanceof DeltaLocation) {
2057         deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
2058       }
2059
2060       if (dLoc2 instanceof DeltaLocation) {
2061         deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
2062       }
2063       if (deltaCount1 < deltaCount2) {
2064         return ComparisonResult.GREATER;
2065       } else if (deltaCount1 == deltaCount2) {
2066         return ComparisonResult.EQUAL;
2067       } else {
2068         return ComparisonResult.LESS;
2069       }
2070
2071     }
2072
2073     private static int compareBaseLocationSet(CompositeLocation compLoc1,
2074         CompositeLocation compLoc2, boolean awareSharedLoc, boolean ignore, String msg) {
2075
2076       // if compLoc1 is greater than compLoc2, return true
2077       // else return false;
2078
2079       // compare one by one in according to the order of the tuple
2080       int numOfTie = 0;
2081       for (int i = 0; i < compLoc1.getSize(); i++) {
2082         Location loc1 = compLoc1.get(i);
2083         if (i >= compLoc2.getSize()) {
2084           if (ignore) {
2085             return ComparisonResult.INCOMPARABLE;
2086           } else {
2087             throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
2088                 + " because they are not comparable at " + msg);
2089           }
2090         }
2091         Location loc2 = compLoc2.get(i);
2092
2093         Descriptor descriptor = getCommonParentDescriptor(loc1, loc2, msg);
2094         SSJavaLattice<String> lattice = getLatticeByDescriptor(descriptor);
2095
2096         // check if the shared location is appeared only at the end of the
2097         // composite location
2098         if (lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
2099           if (i != (compLoc1.getSize() - 1)) {
2100             throw new Error("The shared location " + loc1.getLocIdentifier()
2101                 + " cannot be appeared in the middle of composite location at" + msg);
2102           }
2103         }
2104
2105         if (lattice.getSharedLocSet().contains(loc2.getLocIdentifier())) {
2106           if (i != (compLoc2.getSize() - 1)) {
2107             throw new Error("The shared location " + loc2.getLocIdentifier()
2108                 + " cannot be appeared in the middle of composite location at " + msg);
2109           }
2110         }
2111
2112         // if (!lattice1.equals(lattice2)) {
2113         // throw new Error("Failed to compare two locations of " + compLoc1 +
2114         // " and " + compLoc2
2115         // + " because they are not comparable at " + msg);
2116         // }
2117
2118         if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
2119           numOfTie++;
2120           // check if the current location is the spinning location
2121           // note that the spinning location only can be appeared in the last
2122           // part of the composite location
2123           if (awareSharedLoc && numOfTie == compLoc1.getSize()
2124               && lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
2125             return ComparisonResult.GREATER;
2126           }
2127           continue;
2128         } else if (lattice.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
2129           return ComparisonResult.GREATER;
2130         } else {
2131           if (lattice.isComparable(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
2132             return ComparisonResult.LESS;
2133           } else {
2134             return ComparisonResult.INCOMPARABLE;
2135           }
2136         }
2137
2138       }
2139
2140       if (numOfTie == compLoc1.getSize()) {
2141
2142         if (numOfTie != compLoc2.getSize()) {
2143
2144           if (ignore) {
2145             return ComparisonResult.INCOMPARABLE;
2146           } else {
2147             throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
2148                 + " because they are not comparable at " + msg);
2149           }
2150
2151         }
2152
2153         return ComparisonResult.EQUAL;
2154       }
2155
2156       return ComparisonResult.LESS;
2157
2158     }
2159
2160     public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet, String errMsg) {
2161
2162       System.out.println("Calculating GLB=" + inputSet);
2163       CompositeLocation glbCompLoc = new CompositeLocation();
2164
2165       // calculate GLB of the first(priority) element
2166       Set<String> priorityLocIdentifierSet = new HashSet<String>();
2167       Descriptor priorityDescriptor = null;
2168
2169       Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
2170           new Hashtable<String, Set<CompositeLocation>>();
2171       // mapping from the priority loc ID to its full representation by the
2172       // composite location
2173
2174       int maxTupleSize = 0;
2175       CompositeLocation maxCompLoc = null;
2176
2177       Location prevPriorityLoc = null;
2178       for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
2179         CompositeLocation compLoc = (CompositeLocation) iterator.next();
2180         if (compLoc.getSize() > maxTupleSize) {
2181           maxTupleSize = compLoc.getSize();
2182           maxCompLoc = compLoc;
2183         }
2184         Location priorityLoc = compLoc.get(0);
2185         String priorityLocId = priorityLoc.getLocIdentifier();
2186         priorityLocIdentifierSet.add(priorityLocId);
2187
2188         if (locId2CompLocSet.containsKey(priorityLocId)) {
2189           locId2CompLocSet.get(priorityLocId).add(compLoc);
2190         } else {
2191           Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
2192           newSet.add(compLoc);
2193           locId2CompLocSet.put(priorityLocId, newSet);
2194         }
2195
2196         // check if priority location are coming from the same lattice
2197         if (priorityDescriptor == null) {
2198           priorityDescriptor = priorityLoc.getDescriptor();
2199         } else {
2200           priorityDescriptor = getCommonParentDescriptor(priorityLoc, prevPriorityLoc, errMsg);
2201         }
2202         prevPriorityLoc = priorityLoc;
2203         // else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
2204         // throw new Error("Failed to calculate GLB of " + inputSet
2205         // + " because they are from different lattices.");
2206         // }
2207       }
2208
2209       SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
2210       System.out.println("priorityDescriptor=" + priorityDescriptor);
2211       System.out.println("GLB INPUT=" + priorityLocIdentifierSet);
2212       String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
2213       System.out.println("GLB OUTPUT="+glbOfPriorityLoc);
2214       glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
2215       Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
2216
2217       if (compSet == null) {
2218         // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
2219         // mean that the result is already lower than <x1,y1> and <x2,y2>
2220         // assign TOP to the rest of the location elements
2221
2222         // in this case, do not take care about delta
2223         // CompositeLocation inputComp = inputSet.iterator().next();
2224         for (int i = 1; i < maxTupleSize; i++) {
2225           glbCompLoc.addLocation(Location.createTopLocation(maxCompLoc.get(i).getDescriptor()));
2226         }
2227       } else {
2228
2229         // here find out composite location that has a maximum length tuple
2230         // if we have three input set: [A], [A,B], [A,B,C]
2231         // maximum length tuple will be [A,B,C]
2232         int max = 0;
2233         CompositeLocation maxFromCompSet = null;
2234         for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2235           CompositeLocation c = (CompositeLocation) iterator.next();
2236           if (c.getSize() > max) {
2237             max = c.getSize();
2238             maxFromCompSet = c;
2239           }
2240         }
2241
2242         if (compSet.size() == 1) {
2243           // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
2244           CompositeLocation comp = compSet.iterator().next();
2245           for (int i = 1; i < comp.getSize(); i++) {
2246             glbCompLoc.addLocation(comp.get(i));
2247           }
2248
2249           // if input location corresponding to glb is a delta, need to apply
2250           // delta to glb result
2251           if (comp instanceof DeltaLocation) {
2252             glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2253           }
2254
2255         } else {
2256           // when GLB(x1,x2)==x1 and x2 : GLB case 1
2257           // if more than one location shares the same priority GLB
2258           // need to calculate the rest of GLB loc
2259
2260           // setup input set starting from the second tuple item
2261           Set<CompositeLocation> innerGLBInput = new HashSet<CompositeLocation>();
2262           for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2263             CompositeLocation compLoc = (CompositeLocation) iterator.next();
2264             CompositeLocation innerCompLoc = new CompositeLocation();
2265             for (int idx = 1; idx < compLoc.getSize(); idx++) {
2266               innerCompLoc.addLocation(compLoc.get(idx));
2267             }
2268             if (innerCompLoc.getSize() > 0) {
2269               innerGLBInput.add(innerCompLoc);
2270             }
2271           }
2272
2273           if (innerGLBInput.size() > 0) {
2274             // System.out.println("######innerGLBInput=" + innerGLBInput);
2275             CompositeLocation innerGLB = CompositeLattice.calculateGLB(innerGLBInput, errMsg);
2276             for (int idx = 0; idx < innerGLB.getSize(); idx++) {
2277               glbCompLoc.addLocation(innerGLB.get(idx));
2278             }
2279           }
2280
2281           // if input location corresponding to glb is a delta, need to apply
2282           // delta to glb result
2283
2284           for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2285             CompositeLocation compLoc = (CompositeLocation) iterator.next();
2286             if (compLoc instanceof DeltaLocation) {
2287               if (glbCompLoc.equals(compLoc)) {
2288                 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2289                 break;
2290               }
2291             }
2292           }
2293
2294         }
2295       }
2296
2297       System.out.println("GLB=" + glbCompLoc);
2298       return glbCompLoc;
2299
2300     }
2301
2302     static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
2303
2304       SSJavaLattice<String> lattice = null;
2305
2306       if (d instanceof ClassDescriptor) {
2307         lattice = ssjava.getCd2lattice().get(d);
2308       } else if (d instanceof MethodDescriptor) {
2309         if (ssjava.getMd2lattice().containsKey(d)) {
2310           lattice = ssjava.getMd2lattice().get(d);
2311         } else {
2312           // use default lattice for the method
2313           lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
2314         }
2315       }
2316
2317       return lattice;
2318     }
2319
2320     static Descriptor getCommonParentDescriptor(Location loc1, Location loc2, String msg) {
2321
2322       Descriptor d1 = loc1.getDescriptor();
2323       Descriptor d2 = loc2.getDescriptor();
2324
2325       Descriptor descriptor;
2326
2327       if (d1 instanceof ClassDescriptor && d2 instanceof ClassDescriptor) {
2328
2329         if (d1.equals(d2)) {
2330           descriptor = d1;
2331         } else {
2332           // identifying which one is parent class
2333           Set<Descriptor> d1SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d1);
2334           Set<Descriptor> d2SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d2);
2335
2336           if (d1 == null && d2 == null) {
2337             throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2338                 + " because they are not comparable at " + msg);
2339           } else if (d1SubClassesSet != null && d1SubClassesSet.contains(d2)) {
2340             descriptor = d1;
2341           } else if (d2SubClassesSet != null && d2SubClassesSet.contains(d1)) {
2342             descriptor = d2;
2343           } else {
2344             throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2345                 + " because they are not comparable at " + msg);
2346           }
2347         }
2348
2349       } else if (d1 instanceof MethodDescriptor && d2 instanceof MethodDescriptor) {
2350
2351         if (d1.equals(d2)) {
2352           descriptor = d1;
2353         } else {
2354
2355           // identifying which one is parent class
2356           MethodDescriptor md1 = (MethodDescriptor) d1;
2357           MethodDescriptor md2 = (MethodDescriptor) d2;
2358
2359           if (!md1.matches(md2)) {
2360             throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2361                 + " because they are not comparable at " + msg);
2362           }
2363
2364           Set<Descriptor> d1SubClassesSet =
2365               ssjava.tu.getSubClasses(((MethodDescriptor) d1).getClassDesc());
2366           Set<Descriptor> d2SubClassesSet =
2367               ssjava.tu.getSubClasses(((MethodDescriptor) d2).getClassDesc());
2368
2369           if (d1 == null && d2 == null) {
2370             throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2371                 + " because they are not comparable at " + msg);
2372           } else if (d1 != null && d1SubClassesSet.contains(d2)) {
2373             descriptor = d1;
2374           } else if (d2 != null && d2SubClassesSet.contains(d1)) {
2375             descriptor = d2;
2376           } else {
2377             throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2378                 + " because they are not comparable at " + msg);
2379           }
2380         }
2381
2382       } else {
2383         throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2384             + " because they are not comparable at " + msg);
2385       }
2386
2387       return descriptor;
2388
2389     }
2390
2391   }
2392
2393   class ComparisonResult {
2394
2395     public static final int GREATER = 0;
2396     public static final int EQUAL = 1;
2397     public static final int LESS = 2;
2398     public static final int INCOMPARABLE = 3;
2399     int result;
2400
2401   }
2402
2403 }
2404
2405 class ReturnLocGenerator {
2406
2407   public static final int PARAMISHIGHER = 0;
2408   public static final int PARAMISSAME = 1;
2409   public static final int IGNORE = 2;
2410
2411   private Hashtable<Integer, Integer> paramIdx2paramType;
2412
2413   private CompositeLocation declaredReturnLoc = null;
2414
2415   public ReturnLocGenerator(CompositeLocation returnLoc, MethodDescriptor md,
2416       List<CompositeLocation> params, String msg) {
2417
2418     CompositeLocation thisLoc = params.get(0);
2419     if (returnLoc.get(0).equals(thisLoc.get(0)) && returnLoc.getSize() > 1) {
2420       // if the declared return location consists of THIS and field location,
2421       // return location for the caller's side has to have same field element
2422       this.declaredReturnLoc = returnLoc;
2423     } else {
2424       // creating mappings
2425       paramIdx2paramType = new Hashtable<Integer, Integer>();
2426       for (int i = 0; i < params.size(); i++) {
2427         CompositeLocation param = params.get(i);
2428         int compareResult = CompositeLattice.compare(param, returnLoc, true, msg);
2429
2430         int type;
2431         if (compareResult == ComparisonResult.GREATER) {
2432           type = 0;
2433         } else if (compareResult == ComparisonResult.EQUAL) {
2434           type = 1;
2435         } else {
2436           type = 2;
2437         }
2438         paramIdx2paramType.put(new Integer(i), new Integer(type));
2439       }
2440     }
2441
2442   }
2443
2444   public CompositeLocation computeReturnLocation(List<CompositeLocation> args) {
2445
2446     if (declaredReturnLoc != null) {
2447       // when developer specify that the return value is [THIS,field]
2448       // needs to translate to the caller's location
2449       CompositeLocation callerLoc = new CompositeLocation();
2450       CompositeLocation callerBaseLocation = args.get(0);
2451
2452       for (int i = 0; i < callerBaseLocation.getSize(); i++) {
2453         callerLoc.addLocation(callerBaseLocation.get(i));
2454       }
2455       for (int i = 1; i < declaredReturnLoc.getSize(); i++) {
2456         callerLoc.addLocation(declaredReturnLoc.get(i));
2457       }
2458       return callerLoc;
2459     } else {
2460       // compute the highest possible location in caller's side
2461       assert paramIdx2paramType.keySet().size() == args.size();
2462
2463       Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
2464       for (int i = 0; i < args.size(); i++) {
2465         int type = (paramIdx2paramType.get(new Integer(i))).intValue();
2466         CompositeLocation argLoc = args.get(i);
2467         if (type == PARAMISHIGHER || type == PARAMISSAME) {
2468           // return loc is equal to or lower than param
2469           inputGLB.add(argLoc);
2470         }
2471       }
2472
2473       // compute GLB of arguments subset that are same or higher than return
2474       // location
2475       if (inputGLB.isEmpty()) {
2476         if (args.size() == 0) {
2477           return null;
2478         }
2479         CompositeLocation rtr =
2480             new CompositeLocation(Location.createTopLocation(args.get(0).get(0).getDescriptor()));
2481         return rtr;
2482       } else {
2483         CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB, "");
2484         return glb;
2485       }
2486     }
2487
2488   }
2489 }