-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParMembers.jrag
256 lines (204 loc) · 8.07 KB
/
ParMembers.jrag
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
aspect ParFields {
class Pair<A, B> {
private A first;
private B second;
public Pair(A first, B second) {
super();
this.first = first;
this.second = second;
}
public int hashCode() {
int hashFirst = first != null ? first.hashCode() : 0;
int hashSecond = second != null ? second.hashCode() : 0;
return (hashFirst + hashSecond) * hashSecond + hashFirst;
}
public boolean equals(Object other) {
if (other instanceof Pair) {
Pair<A, B> otherPair = (Pair<A, B>) other;
return ((this.first == otherPair.first || (this.first != null
&& otherPair.first != null && this.first
.equals(otherPair.first))) && (this.second == otherPair.second || (this.second != null
&& otherPair.second != null && this.second
.equals(otherPair.second))));
}
return false;
}
public String toString() {
return "(" + first + ", " + second + ")";
}
public A getFirst() {
return first;
}
public void setFirst(A first) {
this.first = first;
}
public B getSecond() {
return second;
}
public void setSecond(B second) {
this.second = second;
}
}
syn lazy HashMap TypeDecl.localParFieldsMap() = new HashMap();
eq L2ClassDecl.localParFieldsMap() {
HashMap parFields = new HashMap();
// for (FieldVariable fieldVar : getFieldPars()) {
// parFields.put(new Pair(fieldVar.getReceiverTypeAccess().type(), fieldVar.getID()), fieldVar); //NOT SURE ABOUT THIS LINE
// }
for (MemberVariable pm : getMemberParameterList()) {
if (pm instanceof FieldVariable) {
FieldVariable fieldVar = (FieldVariable) pm;
parFields.put(new Pair(fieldVar.getReceiverTypeAccess().type(), fieldVar.getID()), fieldVar); //NOT SURE ABOUT THIS LINE
}
}
return parFields;
}
eq L2GenericClassDecl.localParFieldsMap() { //todo simplify it
HashMap parFields = new HashMap();
// for (FieldVariable fieldVar : getFieldPars()) {
// parFields.put(new Pair(fieldVar.getReceiverTypeAccess().type(), fieldVar.getID()), fieldVar); //NOT SURE ABOUT THIS LINE
// }
for (MemberVariable pm : getMemberParameterList()) {
if (pm instanceof FieldVariable) {
FieldVariable fieldVar = (FieldVariable) pm;
parFields.put(new Pair(fieldVar.getReceiverTypeAccess().type(), fieldVar.getID()), fieldVar); //NOT SURE ABOUT THIS LINE
}
}
return parFields;
}
syn lazy FieldVariable TypeDecl.memberParFields(TypeDecl type, String name) = null;
// member fields
eq ClassDecl.memberParFields(TypeDecl type, String name) {
//TODO SIMPLIFY
FieldVariable field = localParFieldsMap().containsKey(new Pair(type, name)) ? (FieldVariable)localParFieldsMap().get(new Pair(type, name)) : null;
if (field != null)
return field; // this causes hiding of fields in superclass and interfaces
if (hasSuperclass()) {
//TODO
}
return field;
}
//////////////////////////////////////
syn lazy HashMap<Pair, Collection> TypeDecl.localParMethodsMap() = new HashMap();
eq L2ClassDecl.localParMethodsMap() {
HashMap<Pair, Collection> parMethods = new HashMap();
for (MemberVariable pm : getMemberParameterList()) {
if (pm instanceof MethodVariable) {
MethodVariable methodVar = (MethodVariable) pm;
Pair key = new Pair(methodVar.getReceiverTypeAccess().type(), methodVar.getID());
Collection val = parMethods.get(key);
if (val == null) {
val = new ArrayList();
parMethods.put(key, val);
}
val.add(methodVar);
}
}
return parMethods;
}
eq L2GenericClassDecl.localParMethodsMap() { //todo simplify it
HashMap<Pair, Collection> parMethods = new HashMap();
for (MemberVariable pm : getMemberParameterList()) {
if (pm instanceof MethodVariable) {
MethodVariable methodVar = (MethodVariable) pm;
Pair key = new Pair(methodVar.getReceiverTypeAccess().type(), methodVar.getID());
Collection val = parMethods.get(key);
if (val == null) {
val = new ArrayList();
parMethods.put(key, val);
}
val.add(methodVar);
}
}
return parMethods;
}
syn lazy MethodVariable TypeDecl.memberParMethods(TypeDecl type, String name, List<Expr> argTypes) = null;
// member methods
eq ClassDecl.memberParMethods(TypeDecl type, String name, List<Expr> argTypes) {
Pair key = new Pair(type, name);
Collection<MethodVariable> methods = localParMethodsMap().get(key);
if (methods == null) return null;
for (MethodVariable methodVar : methods) {
if (methodVar.getNumArgumentTypes() == argTypes.getNumChild()) {
boolean flag = true;
for (int j = 0; j < methodVar.getNumArgumentTypes(); j++) {
TypeDecl parType = methodVar.getArgumentTypes(j).type();
TypeDecl argType = argTypes.getChild(j).type();
// if (!parType.subtype(argType)) {
// flag = false;
// break;
// }
if (!argType.methodInvocationConversionTo(parType) && !argType.isUnknown() && !parType.isUnknown()) {
flag = false;
break;
}
}
if (flag)
return methodVar;
}
}
return null;
}
syn lazy boolean TypeDecl.hasMemberParMethods(TypeDecl type, String name) = false;
eq ClassDecl.hasMemberParMethods(TypeDecl type, String name) {
Pair key = new Pair(type, name);
Collection<MethodVariable> methods = localParMethodsMap().get(key);
return methods != null;
}
////////////////////////////////////////////////////////////
inh lazy MemberVariable ParMemberAccess.getDeclVariable();
eq ParMemberClassInstanceExpr.getMemberArgument(int i).getDeclVariable() {
TypeDecl ty = type().original();
if (ty instanceof L2ClassDecl) {
return ((L2ClassDecl)ty).getMemberParameter(i);
} else if (ty instanceof L2GenericClassDecl) {
return ((L2GenericClassDecl)ty).getMemberParameter(i);
} else return null; //impossible
}
inh lazy MemberVariable ParMemberAccess.getParVarAccess();
eq ParMemberClassInstanceExpr.getMemberArgument(int i).getParVarAccess() {
ParMemberAccess fa = getMemberArgument(i);
if ( fa.getDeclVariable() instanceof FieldVariable ) {
SimpleSet decls = fa.getReceiverTypeAccess().type().memberFields(fa.getMemberName());
if (decls.isEmpty())
return hostType().memberParFields(fa.getReceiverTypeAccess().type(), fa.getMemberName());
} else {
Collection<MethodDecl> decls = fa.getReceiverTypeAccess().type().memberMethods(fa.getMemberName());
if (decls.isEmpty()) {
MethodVariable declmv = (MethodVariable) fa.getDeclVariable();
List declArgs = declmv.getArgumentTypesList();
return hostType().memberParMethods(fa.getReceiverTypeAccess().type(), fa.getMemberName(), declArgs);
}
}
return null;
}
inh lazy TypeDecl ParMemberAccess.getFieldType(); //if we use an par filed access to instantaite it again
eq ParMemberClassInstanceExpr.getMemberArgument(int i).getFieldType() {
ParMemberAccess fa = getMemberArgument(i);
if ( fa.getDeclVariable() instanceof MethodVariable ) return null;
FieldVariable declfv = (FieldVariable) fa.getDeclVariable();
SimpleSet decls = fa.getReceiverTypeAccess().type().memberFields(fa.getMemberName());
if (decls.isEmpty()) {
FieldVariable fv = hostType().memberParFields(fa.getReceiverTypeAccess().type(), fa.getMemberName());
if (fv != null) {
return fv.getTypeAccess().type();
} else return null;
}
else return ((FieldDeclaration) decls.iterator().next()).getTypeAccess().type(); //TODO check more
}
inh lazy TypeDecl ParMemberAccess.getMethodType();
eq ParMemberClassInstanceExpr.getMemberArgument(int i).getMethodType() {
ParMemberAccess fa = getMemberArgument(i);
if ( fa.getDeclVariable() instanceof FieldVariable ) return null;
MethodVariable declmv = (MethodVariable) fa.getDeclVariable();
List declArgs = declmv.getArgumentTypesList();
Collection<MethodDecl> decls = fa.getReceiverTypeAccess().type().memberMethods(fa.getMemberName());
if (decls.isEmpty()) {
MethodVariable mv = hostType().memberParMethods(fa.getReceiverTypeAccess().type(), fa.getMemberName(), declArgs);
if (mv != null)
return mv.getTypeAccess().type();
else return null;
}
else return ((MethodDecl) decls.iterator().next()).getTypeAccess().type(); //no need check more, override method must return the same type
}
}