1 package Analysis.SSJava;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.Iterator;
9 import IR.ClassDescriptor;
10 import IR.FieldDescriptor;
11 import IR.MethodDescriptor;
13 import IR.SymbolTable;
14 import IR.VarDescriptor;
15 import IR.Tree.BlockNode;
16 import IR.Tree.BlockStatementNode;
17 import IR.Tree.DeclarationNode;
20 public class LocationInference {
23 SSJavaAnalysis ssjava;
25 List<ClassDescriptor> toanalyzeList;
26 List<MethodDescriptor> toanalyzeMethodList;
28 public LocationInference(SSJavaAnalysis ssjava, State state) {
31 this.toanalyzeList = new ArrayList<ClassDescriptor>();
32 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
35 public void setupToAnalyze() {
36 SymbolTable classtable = state.getClassSymbolTable();
37 toanalyzeList.clear();
38 toanalyzeList.addAll(classtable.getValueSet());
39 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
40 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
41 return o1.getClassName().compareToIgnoreCase(o2.getClassName());
46 public void setupToAnalazeMethod(ClassDescriptor cd) {
48 SymbolTable methodtable = cd.getMethodTable();
49 toanalyzeMethodList.clear();
50 toanalyzeMethodList.addAll(methodtable.getValueSet());
51 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
52 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
53 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
58 public boolean toAnalyzeMethodIsEmpty() {
59 return toanalyzeMethodList.isEmpty();
62 public boolean toAnalyzeIsEmpty() {
63 return toanalyzeList.isEmpty();
66 public ClassDescriptor toAnalyzeNext() {
67 return toanalyzeList.remove(0);
70 public MethodDescriptor toAnalyzeMethodNext() {
71 return toanalyzeMethodList.remove(0);
74 private void checkDeclarationInClass(ClassDescriptor cd) {
75 // Check to see that fields are okay
76 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
77 FieldDescriptor fd = (FieldDescriptor) field_it.next();
79 if (!(fd.isFinal() && fd.isStatic())) {
80 analyzeFieldDeclaration(cd, fd);
82 // for static final, assign top location by default
87 private void analyzeFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
88 // assign a unique ID to field
91 public void inference() {
95 while (!toAnalyzeIsEmpty()) {
96 ClassDescriptor cd = toAnalyzeNext();
98 checkDeclarationInClass(cd);
100 setupToAnalazeMethod(cd);
101 while (!toAnalyzeMethodIsEmpty()) {
102 MethodDescriptor md = toAnalyzeMethodNext();
104 // need to analyze method declaration for assigning unique id to
105 // parameters(including 'this' variable)
107 if (ssjava.needTobeAnnotated(md)) {
108 if (state.SSJAVADEBUG) {
109 System.out.println("SSJAVA: Location Inference: " + md);
111 analyzeMethodBody(cd, md, null);
118 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md,
119 CompositeLocation constraints) {
120 BlockNode bn = state.getMethodBody(md);
121 analyzeBlockNode(md, md.getParameterTable(), bn);
124 private CompositeLocation analyzeBlockNode(MethodDescriptor md, SymbolTable nametable,
127 bn.getVarTable().setParent(nametable);
128 for (int i = 0; i < bn.size(); i++) {
129 BlockStatementNode bsn = bn.get(i);
130 analyzeBlockStatementNode(md, bn.getVarTable(), bsn);
132 return new CompositeLocation();
136 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
137 BlockStatementNode bsn) {
139 switch (bsn.kind()) {
140 case Kind.BlockExpressionNode:
141 // checkBlockExpressionNode(md,(BlockExpressionNode)bsn);
144 case Kind.DeclarationNode:
145 analyzeDeclarationNode(md, nametable, (DeclarationNode) bsn);
148 case Kind.IfStatementNode:
154 case Kind.ReturnNode:
157 case Kind.SubBlockNode:
160 case Kind.ContinueBreakNode:
163 case Kind.SwitchStatementNode:
169 private void analyzeDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
171 VarDescriptor vd = dn.getVarDescriptor();