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