1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.sun.tools.javafx.comp; |
27 | |
|
28 | |
import com.sun.tools.javac.tree.*; |
29 | |
import com.sun.tools.javac.tree.JCTree.*; |
30 | |
import com.sun.tools.javafx.tree.*; |
31 | |
import com.sun.tools.javafx.code.*; |
32 | |
import com.sun.tools.javac.code.Symbol; |
33 | |
import com.sun.tools.javac.code.Symbol.VarSymbol; |
34 | |
import com.sun.tools.javac.util.Context; |
35 | |
import com.sun.tools.javafx.comp.JavafxTypeMorpher.VarMorphInfo; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class JavafxVarUsageAnalysis extends JavafxTreeScanner { |
42 | 12 | protected static final Context.Key<JavafxVarUsageAnalysis> varUsageKey = |
43 | |
new Context.Key<JavafxVarUsageAnalysis>(); |
44 | |
|
45 | |
private final JavafxTypeMorpher typeMorpher; |
46 | |
private boolean inLHS; |
47 | |
private boolean inBindContext; |
48 | |
|
49 | |
public static JavafxVarUsageAnalysis instance(Context context) { |
50 | 399 | JavafxVarUsageAnalysis instance = context.get(varUsageKey); |
51 | 399 | if (instance == null) |
52 | 399 | instance = new JavafxVarUsageAnalysis(context); |
53 | 399 | return instance; |
54 | |
} |
55 | |
|
56 | 399 | JavafxVarUsageAnalysis(Context context) { |
57 | 399 | context.put(varUsageKey, this); |
58 | |
|
59 | 399 | this.typeMorpher = JavafxTypeMorpher.instance(context); |
60 | 399 | inLHS = false; |
61 | 399 | inBindContext = false; |
62 | 399 | } |
63 | |
|
64 | |
public void analyzeVarUse(JavafxEnv<JavafxAttrContext> attrEnv) { |
65 | 379 | scan(attrEnv.tree); |
66 | 379 | } |
67 | |
|
68 | |
private void markVarUse(Symbol sym) { |
69 | 19503 | if (sym instanceof VarSymbol) { |
70 | 13246 | VarSymbol vsym = (VarSymbol)sym; |
71 | 13246 | VarMorphInfo vmi = typeMorpher.varMorphInfo(vsym); |
72 | 13246 | if (inBindContext) { |
73 | 875 | if (inLHS) { |
74 | |
|
75 | |
} else { |
76 | 874 | vmi.markBoundTo(); |
77 | |
} |
78 | |
} else { |
79 | 12371 | if (inLHS) { |
80 | 928 | vmi.markAssignedTo(); |
81 | |
} |
82 | |
} |
83 | |
} |
84 | 19503 | } |
85 | |
|
86 | |
@Override |
87 | |
public void visitVar(JFXVar tree) { |
88 | 3263 | boolean wasInBindContext = inBindContext; |
89 | 3263 | inBindContext |= tree.isBound(); |
90 | 3263 | scan(tree.getInitializer()); |
91 | 3263 | markVarUse(tree.sym); |
92 | 3263 | inBindContext = wasInBindContext; |
93 | |
|
94 | 3263 | scan(tree.getOnReplace()); |
95 | 3263 | } |
96 | |
|
97 | |
@Override |
98 | |
public void visitFunctionDefinition(JFXFunctionDefinition tree) { |
99 | 1065 | boolean wasInBindContext = inBindContext; |
100 | 1065 | inBindContext = false; |
101 | 1065 | scan(tree.getFunctionValue()); |
102 | 1065 | inBindContext = wasInBindContext; |
103 | 1065 | } |
104 | |
|
105 | |
@Override |
106 | |
public void visitBindExpression(JFXBindExpression tree) { |
107 | 0 | boolean wasInBindContext = inBindContext; |
108 | 0 | inBindContext |= tree.getBindStatus().isBound(); |
109 | 0 | tree.getExpression().accept(this); |
110 | 0 | inBindContext = wasInBindContext; |
111 | 0 | } |
112 | |
|
113 | |
@Override |
114 | |
public void visitObjectLiteralPart(JFXObjectLiteralPart tree) { |
115 | 736 | boolean wasInBindContext = inBindContext; |
116 | 736 | inBindContext |= tree.isBound(); |
117 | 736 | markVarUse(tree.sym); |
118 | 736 | tree.getExpression().accept(this); |
119 | 736 | inBindContext = wasInBindContext; |
120 | 736 | } |
121 | |
|
122 | |
@Override |
123 | |
public void visitAssign(JCAssign tree) { |
124 | 910 | boolean wasLHS = inLHS; |
125 | 910 | inLHS = true; |
126 | 910 | scan(tree.lhs); |
127 | 910 | inLHS = wasLHS; |
128 | 910 | scan(tree.rhs); |
129 | 910 | } |
130 | |
|
131 | |
@Override |
132 | |
public void visitIdent(JCIdent tree) { |
133 | 10501 | markVarUse(tree.sym); |
134 | 10501 | } |
135 | |
|
136 | |
@Override |
137 | |
public void visitSelect(JCFieldAccess tree) { |
138 | |
|
139 | |
|
140 | 5003 | boolean wasLHS = inLHS; |
141 | 5003 | inLHS = false; |
142 | 5003 | scan(tree.selected); |
143 | 5003 | inLHS = wasLHS; |
144 | |
|
145 | 5003 | markVarUse(tree.sym); |
146 | 5003 | } |
147 | |
|
148 | |
public void visitBlockExpression(JFXBlockExpression tree) { |
149 | 1925 | scan(tree.stats); |
150 | 1925 | scan(tree.value); |
151 | 1925 | } |
152 | |
} |