changes.
[IRC.git] / Robust / src / Analysis / SSJava / LocationInference.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.Iterator;
7 import java.util.List;
8
9 import IR.ClassDescriptor;
10 import IR.FieldDescriptor;
11 import IR.MethodDescriptor;
12 import IR.State;
13 import IR.SymbolTable;
14 import IR.VarDescriptor;
15 import IR.Tree.BlockNode;
16 import IR.Tree.BlockStatementNode;
17 import IR.Tree.DeclarationNode;
18 import IR.Tree.Kind;
19
20 public class LocationInference {
21
22   State state;
23   SSJavaAnalysis ssjava;
24
25   List<ClassDescriptor> toanalyzeList;
26   List<MethodDescriptor> toanalyzeMethodList;
27
28   public LocationInference(SSJavaAnalysis ssjava, State state) {
29     this.ssjava = ssjava;
30     this.state = state;
31     this.toanalyzeList = new ArrayList<ClassDescriptor>();
32     this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
33   }
34
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());
42       }
43     });
44   }
45
46   public void setupToAnalazeMethod(ClassDescriptor cd) {
47
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());
54       }
55     });
56   }
57
58   public boolean toAnalyzeMethodIsEmpty() {
59     return toanalyzeMethodList.isEmpty();
60   }
61
62   public boolean toAnalyzeIsEmpty() {
63     return toanalyzeList.isEmpty();
64   }
65
66   public ClassDescriptor toAnalyzeNext() {
67     return toanalyzeList.remove(0);
68   }
69
70   public MethodDescriptor toAnalyzeMethodNext() {
71     return toanalyzeMethodList.remove(0);
72   }
73
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();
78
79       if (!(fd.isFinal() && fd.isStatic())) {
80         analyzeFieldDeclaration(cd, fd);
81       } else {
82         // for static final, assign top location by default
83       }
84     }
85   }
86
87   private void analyzeFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
88     // assign a unique ID to field
89   }
90
91   public void inference() {
92
93     setupToAnalyze();
94
95     while (!toAnalyzeIsEmpty()) {
96       ClassDescriptor cd = toAnalyzeNext();
97
98       checkDeclarationInClass(cd);
99
100       setupToAnalazeMethod(cd);
101       while (!toAnalyzeMethodIsEmpty()) {
102         MethodDescriptor md = toAnalyzeMethodNext();
103
104         // need to analyze method declaration for assigning unique id to
105         // parameters(including 'this' variable)
106
107         if (ssjava.needTobeAnnotated(md)) {
108           if (state.SSJAVADEBUG) {
109             System.out.println("SSJAVA: Location Inference: " + md);
110           }
111           analyzeMethodBody(cd, md, null);
112         }
113       }
114     }
115
116   }
117
118   private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md,
119       CompositeLocation constraints) {
120     BlockNode bn = state.getMethodBody(md);
121     analyzeBlockNode(md, md.getParameterTable(), bn);
122   }
123
124   private CompositeLocation analyzeBlockNode(MethodDescriptor md, SymbolTable nametable,
125       BlockNode bn) {
126
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);
131     }
132     return new CompositeLocation();
133
134   }
135
136   private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
137       BlockStatementNode bsn) {
138
139     switch (bsn.kind()) {
140     case Kind.BlockExpressionNode:
141       // checkBlockExpressionNode(md,(BlockExpressionNode)bsn);
142       break;
143
144     case Kind.DeclarationNode:
145       analyzeDeclarationNode(md, nametable, (DeclarationNode) bsn);
146       break;
147
148     case Kind.IfStatementNode:
149       break;
150
151     case Kind.LoopNode:
152       break;
153
154     case Kind.ReturnNode:
155       break;
156
157     case Kind.SubBlockNode:
158       break;
159
160     case Kind.ContinueBreakNode:
161       break;
162
163     case Kind.SwitchStatementNode:
164       break;
165
166     }
167   }
168
169   private void analyzeDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
170
171     VarDescriptor vd = dn.getVarDescriptor();
172
173   }
174
175 }