|
1 |
| package edu.rice.cs.dynamicjava.interpreter; |
|
2 |
| |
|
3 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
4 |
| |
|
5 |
| import edu.rice.cs.dynamicjava.symbol.*; |
|
6 |
| import edu.rice.cs.dynamicjava.symbol.type.ClassType; |
|
7 |
| import edu.rice.cs.dynamicjava.symbol.type.Type; |
|
8 |
| import edu.rice.cs.dynamicjava.symbol.type.VariableType; |
|
9 |
| |
|
10 |
| import static edu.rice.cs.plt.debug.DebugUtil.debug; |
|
11 |
| |
|
12 |
| |
|
13 |
| public class LocalContext extends DelegatingContext { |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| private ClassLoader _loader; |
|
18 |
| private Iterable<DJClass> _classes; |
|
19 |
| private Iterable<LocalVariable> _vars; |
|
20 |
| private Iterable<LocalFunction> _functions; |
|
21 |
| |
|
22 |
945
| public LocalContext(TypeContext next, ClassLoader loader, Iterable<DJClass> classes,
|
|
23 |
| Iterable<LocalVariable> vars, Iterable<LocalFunction> functions) { |
|
24 |
945
| super(next);
|
|
25 |
945
| _loader = loader;
|
|
26 |
945
| _classes = classes;
|
|
27 |
945
| _vars = vars;
|
|
28 |
945
| _functions = functions;
|
|
29 |
| } |
|
30 |
| |
|
31 |
0
| public LocalContext(TypeContext next, Iterable<LocalVariable> vars) {
|
|
32 |
0
| this(next, null, IterUtil.<DJClass>empty(), vars, IterUtil.<LocalFunction>empty());
|
|
33 |
| } |
|
34 |
| |
|
35 |
664
| public LocalContext(TypeContext next, LocalVariable var) {
|
|
36 |
664
| this(next, null, IterUtil.<DJClass>empty(), IterUtil.singleton(var),IterUtil.<LocalFunction>empty());
|
|
37 |
| } |
|
38 |
| |
|
39 |
89
| public LocalContext(TypeContext next, ClassLoader loader, DJClass c) {
|
|
40 |
89
| this(next, loader, IterUtil.singleton(c), IterUtil.<LocalVariable>empty(), IterUtil.<LocalFunction>empty());
|
|
41 |
| } |
|
42 |
| |
|
43 |
183
| public LocalContext(TypeContext next, LocalFunction f) {
|
|
44 |
183
| this(next, null, IterUtil.<DJClass>empty(), IterUtil.<LocalVariable>empty(), IterUtil.singleton(f));
|
|
45 |
| } |
|
46 |
| |
|
47 |
9
| protected LocalContext duplicate(TypeContext next) {
|
|
48 |
9
| return new LocalContext(next, _loader, _classes, _vars, _functions);
|
|
49 |
| } |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
119
| @Override public boolean typeExists(String name, TypeSystem ts) {
|
|
54 |
119
| return declaredClass(name) != null || super.typeExists(name, ts);
|
|
55 |
| } |
|
56 |
| |
|
57 |
0
| @Override public boolean topLevelClassExists(String name, TypeSystem ts) {
|
|
58 |
0
| return declaredClass(name) != null || super.topLevelClassExists(name, ts);
|
|
59 |
| } |
|
60 |
| |
|
61 |
4071
| @Override public DJClass getTopLevelClass(String name, TypeSystem ts) throws AmbiguousNameException {
|
|
62 |
4071
| DJClass result = declaredClass(name);
|
|
63 |
4071
| return result == null ? super.getTopLevelClass(name, ts) : result;
|
|
64 |
| } |
|
65 |
| |
|
66 |
0
| @Override public boolean memberClassExists(String name, TypeSystem ts) {
|
|
67 |
0
| return (declaredClass(name) == null) && super.memberClassExists(name, ts);
|
|
68 |
| } |
|
69 |
| |
|
70 |
108
| @Override public ClassType typeContainingMemberClass(String name, TypeSystem ts) throws AmbiguousNameException {
|
|
71 |
108
| return (declaredClass(name) == null) ? super.typeContainingMemberClass(name, ts) : null;
|
|
72 |
| } |
|
73 |
| |
|
74 |
0
| @Override public boolean typeVariableExists(String name, TypeSystem ts) {
|
|
75 |
0
| return (declaredClass(name) == null) && super.typeVariableExists(name, ts);
|
|
76 |
| } |
|
77 |
| |
|
78 |
108
| @Override public VariableType getTypeVariable(String name, TypeSystem ts) {
|
|
79 |
108
| return (declaredClass(name) == null) ? super.getTypeVariable(name, ts) : null;
|
|
80 |
| } |
|
81 |
| |
|
82 |
4406
| private DJClass declaredClass(String name) {
|
|
83 |
4406
| for (DJClass c : _classes) {
|
|
84 |
175
| if (!c.isAnonymous() && c.declaredName().equals(name)) { return c; }
|
|
85 |
| } |
|
86 |
4231
| return null;
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
362
| @Override public boolean variableExists(String name, TypeSystem ts) {
|
|
92 |
362
| return declaredVariable(name) != null || super.variableExists(name, ts);
|
|
93 |
| } |
|
94 |
| |
|
95 |
2150
| @Override public boolean localVariableExists(String name, TypeSystem ts) {
|
|
96 |
2150
| return declaredVariable(name) != null || super.localVariableExists(name, ts);
|
|
97 |
| } |
|
98 |
| |
|
99 |
2031
| @Override public LocalVariable getLocalVariable(String name, TypeSystem ts) {
|
|
100 |
2031
| LocalVariable result = declaredVariable(name);
|
|
101 |
2031
| return result == null ? super.getLocalVariable(name, ts) : result;
|
|
102 |
| } |
|
103 |
| |
|
104 |
119
| @Override public boolean fieldExists(String name, TypeSystem ts) {
|
|
105 |
119
| return (declaredVariable(name) == null) && super.fieldExists(name, ts);
|
|
106 |
| } |
|
107 |
| |
|
108 |
0
| @Override public ClassType typeContainingField(String name, TypeSystem ts) throws AmbiguousNameException {
|
|
109 |
0
| return (declaredVariable(name) == null) ? super.typeContainingField(name, ts) : null;
|
|
110 |
| } |
|
111 |
| |
|
112 |
4662
| private LocalVariable declaredVariable(String name) {
|
|
113 |
4662
| for (LocalVariable v : _vars) {
|
|
114 |
2281
| if (v.declaredName().equals(name)) { return v; }
|
|
115 |
| } |
|
116 |
2381
| return null;
|
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
0
| @Override public boolean functionExists(String name, TypeSystem ts) {
|
|
122 |
0
| return hasFunction(name) || super.functionExists(name, ts);
|
|
123 |
| } |
|
124 |
| |
|
125 |
382
| @Override public boolean localFunctionExists(String name, TypeSystem ts) {
|
|
126 |
382
| return hasFunction(name) || super.localFunctionExists(name, ts);
|
|
127 |
| } |
|
128 |
| |
|
129 |
382
| @Override public Iterable<LocalFunction> getLocalFunctions(String name, TypeSystem ts,
|
|
130 |
| Iterable<LocalFunction> partial) { |
|
131 |
382
| Iterable<LocalFunction> newPartial = partial;
|
|
132 |
382
| for (LocalFunction f : _functions) {
|
|
133 |
108
| if (f.declaredName().equals(name)) { newPartial = IterUtil.compose(partial, f); }
|
|
134 |
| } |
|
135 |
382
| return super.getLocalFunctions(name, ts, newPartial);
|
|
136 |
| } |
|
137 |
| |
|
138 |
0
| @Override public boolean methodExists(String name, TypeSystem ts) {
|
|
139 |
0
| return !hasFunction(name) && super.methodExists(name, ts);
|
|
140 |
| } |
|
141 |
| |
|
142 |
0
| @Override public Type typeContainingMethod(String name, TypeSystem ts) {
|
|
143 |
0
| return hasFunction(name) ? null : super.typeContainingMethod(name, ts);
|
|
144 |
| } |
|
145 |
| |
|
146 |
382
| private boolean hasFunction(String name) {
|
|
147 |
382
| for (LocalFunction f : _functions) {
|
|
148 |
108
| if (f.declaredName().equals(name)) { return true; }
|
|
149 |
| } |
|
150 |
274
| return false;
|
|
151 |
| } |
|
152 |
| |
|
153 |
89
| @Override public ClassLoader getClassLoader() {
|
|
154 |
43
| if (_loader == null) { return super.getClassLoader(); }
|
|
155 |
46
| else { return _loader; }
|
|
156 |
| } |
|
157 |
| |
|
158 |
| } |