|
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 |
| |
|
9 |
| import static edu.rice.cs.plt.debug.DebugUtil.debug; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| public class FunctionContext extends DelegatingContext { |
|
16 |
| |
|
17 |
| private final Function _f; |
|
18 |
| |
|
19 |
410
| public FunctionContext(TypeContext next, Function f) {
|
|
20 |
410
| super(next);
|
|
21 |
410
| _f = f;
|
|
22 |
| } |
|
23 |
| |
|
24 |
0
| protected FunctionContext duplicate(TypeContext next) {
|
|
25 |
0
| return new FunctionContext(next, _f);
|
|
26 |
| } |
|
27 |
| |
|
28 |
46
| @Override public boolean variableExists(String name, TypeSystem ts) {
|
|
29 |
46
| return getParameter(name) != null || super.variableExists(name, ts);
|
|
30 |
| } |
|
31 |
| |
|
32 |
599
| @Override public boolean localVariableExists(String name, TypeSystem ts) {
|
|
33 |
599
| return getParameter(name) != null || super.localVariableExists(name, ts);
|
|
34 |
| } |
|
35 |
| |
|
36 |
527
| @Override public LocalVariable getLocalVariable(String name, TypeSystem ts) {
|
|
37 |
527
| LocalVariable result = getParameter(name);
|
|
38 |
527
| return result == null ? super.getLocalVariable(name, ts) : result;
|
|
39 |
| } |
|
40 |
| |
|
41 |
72
| @Override public boolean fieldExists(String name, TypeSystem ts) {
|
|
42 |
72
| return (getParameter(name) == null) ? super.fieldExists(name, ts) : false;
|
|
43 |
| } |
|
44 |
| |
|
45 |
69
| @Override public ClassType typeContainingField(String name, TypeSystem ts) throws AmbiguousNameException {
|
|
46 |
69
| return (getParameter(name) == null) ? super.typeContainingField(name, ts) : null;
|
|
47 |
| } |
|
48 |
| |
|
49 |
1313
| private LocalVariable getParameter(String name) {
|
|
50 |
1313
| for (LocalVariable v : _f.parameters()) {
|
|
51 |
1054
| if (v.declaredName().equals(name)) { return v; }
|
|
52 |
| } |
|
53 |
259
| return null;
|
|
54 |
| } |
|
55 |
| |
|
56 |
0
| @Override public boolean functionExists(String name, TypeSystem ts) {
|
|
57 |
0
| return isLocalFunction(name) || super.functionExists(name, ts);
|
|
58 |
| } |
|
59 |
| |
|
60 |
0
| @Override public boolean localFunctionExists(String name, TypeSystem ts) {
|
|
61 |
0
| return isLocalFunction(name) || super.localFunctionExists(name, ts);
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| @Override public Iterable<LocalFunction> getLocalFunctions(String name, TypeSystem ts,
|
|
65 |
| Iterable<LocalFunction> partial) { |
|
66 |
0
| if (IterUtil.isEmpty(partial)) {
|
|
67 |
0
| if (isLocalFunction(name)) { partial = IterUtil.singleton((LocalFunction) _f); }
|
|
68 |
0
| return super.getLocalFunctions(name, ts, partial);
|
|
69 |
| } |
|
70 |
0
| else { return partial; }
|
|
71 |
| } |
|
72 |
| |
|
73 |
0
| @Override public boolean methodExists(String name, TypeSystem ts) {
|
|
74 |
0
| return isLocalFunction(name) ? false : super.methodExists(name, ts);
|
|
75 |
| } |
|
76 |
| |
|
77 |
0
| @Override public Type typeContainingMethod(String name, TypeSystem ts) {
|
|
78 |
0
| return isLocalFunction(name) ? null : super.typeContainingMethod(name, ts);
|
|
79 |
| } |
|
80 |
| |
|
81 |
0
| private boolean isLocalFunction(String name) {
|
|
82 |
0
| return (_f instanceof LocalFunction) && ((LocalFunction) _f).declaredName().equals(name);
|
|
83 |
| } |
|
84 |
| |
|
85 |
0
| @Override public String makeClassName(String n) {
|
|
86 |
0
| return super.makeAnonymousClassName() + n;
|
|
87 |
| } |
|
88 |
| |
|
89 |
69
| @Override public DJClass getThis() {
|
|
90 |
0
| if (isStatic()) { return null; }
|
|
91 |
69
| else { return super.getThis(); }
|
|
92 |
| } |
|
93 |
| |
|
94 |
0
| @Override public DJClass getThis(String className) {
|
|
95 |
0
| if (isStatic()) { return null; }
|
|
96 |
0
| else { return super.getThis(className); }
|
|
97 |
| } |
|
98 |
| |
|
99 |
69
| @Override public DJClass getThis(Type expected, TypeSystem ts) {
|
|
100 |
0
| if (isStatic()) { return null; }
|
|
101 |
69
| else { return super.getThis(expected, ts); }
|
|
102 |
| } |
|
103 |
| |
|
104 |
0
| @Override public DJClass initializingClass() {
|
|
105 |
0
| return (_f instanceof DJConstructor) ? ((DJConstructor) _f).declaringClass() : null;
|
|
106 |
| } |
|
107 |
| |
|
108 |
135
| @Override public Type getReturnType() {
|
|
109 |
0
| if (_f instanceof DJConstructor) { return TypeSystem.VOID; }
|
|
110 |
135
| else { return _f.returnType(); }
|
|
111 |
| } |
|
112 |
| |
|
113 |
265
| @Override public Iterable<Type> getDeclaredThrownTypes() { return _f.thrownTypes(); }
|
|
114 |
| |
|
115 |
138
| private boolean isStatic() {
|
|
116 |
138
| return _f instanceof DJMethod && ((DJMethod)_f).isStatic();
|
|
117 |
| } |
|
118 |
| |
|
119 |
| } |