1 package Analysis.SSJava;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
8 import IR.ClassDescriptor;
9 import IR.MethodDescriptor;
11 import IR.SymbolTable;
12 import IR.VarDescriptor;
13 import IR.Tree.BlockNode;
14 import IR.Tree.BlockStatementNode;
15 import IR.Tree.DeclarationNode;
18 public class LocationInference {
21 SSJavaAnalysis ssjava;
23 List<ClassDescriptor> toanalyzeList;
24 List<MethodDescriptor> toanalyzeMethodList;
26 public LocationInference(SSJavaAnalysis ssjava, State state) {
29 this.toanalyzeList = new ArrayList<ClassDescriptor>();
30 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
33 public void setupToAnalyze() {
34 SymbolTable classtable = state.getClassSymbolTable();
35 toanalyzeList.clear();
36 toanalyzeList.addAll(classtable.getValueSet());
37 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
38 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
39 return o1.getClassName().compareToIgnoreCase(o2.getClassName());
44 public void setupToAnalazeMethod(ClassDescriptor cd) {
46 SymbolTable methodtable = cd.getMethodTable();
47 toanalyzeMethodList.clear();
48 toanalyzeMethodList.addAll(methodtable.getValueSet());
49 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
50 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
51 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
56 public boolean toAnalyzeMethodIsEmpty() {
57 return toanalyzeMethodList.isEmpty();
60 public boolean toAnalyzeIsEmpty() {
61 return toanalyzeList.isEmpty();
64 public ClassDescriptor toAnalyzeNext() {
65 return toanalyzeList.remove(0);
68 public MethodDescriptor toAnalyzeMethodNext() {
69 return toanalyzeMethodList.remove(0);
72 public void inference() {
76 while (!toAnalyzeIsEmpty()) {
77 ClassDescriptor cd = toAnalyzeNext();
79 setupToAnalazeMethod(cd);
80 while (!toAnalyzeMethodIsEmpty()) {
81 MethodDescriptor md = toAnalyzeMethodNext();
82 if (ssjava.needTobeAnnotated(md)) {
83 if (state.SSJAVADEBUG) {
84 System.out.println("SSJAVA: Location Inference: " + md);
86 analyzeMethodBody(cd, md, null);
93 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md,
94 CompositeLocation constraints) {
95 BlockNode bn = state.getMethodBody(md);
96 analyzeBlockNode(md, md.getParameterTable(), bn);
99 private CompositeLocation analyzeBlockNode(MethodDescriptor md, SymbolTable nametable,
102 bn.getVarTable().setParent(nametable);
103 for (int i = 0; i < bn.size(); i++) {
104 BlockStatementNode bsn = bn.get(i);
105 analyzeBlockStatementNode(md, bn.getVarTable(), bsn);
107 return new CompositeLocation();
111 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
112 BlockStatementNode bsn) {
114 switch (bsn.kind()) {
115 case Kind.BlockExpressionNode:
116 // checkBlockExpressionNode(md,(BlockExpressionNode)bsn);
119 case Kind.DeclarationNode:
120 analyzeDeclarationNode(md, nametable, (DeclarationNode) bsn);
123 case Kind.IfStatementNode:
129 case Kind.ReturnNode:
132 case Kind.SubBlockNode:
135 case Kind.ContinueBreakNode:
138 case Kind.SwitchStatementNode:
144 private void analyzeDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
146 VarDescriptor vd = dn.getVarDescriptor();