comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpression.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpression.java@ef292a5bb79d
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.dsl.processor.expression;
24
25 import java.util.*;
26 import java.util.concurrent.atomic.*;
27
28 import javax.lang.model.element.*;
29 import javax.lang.model.type.*;
30
31 public abstract class DSLExpression {
32
33 private TypeMirror resolvedTargetType;
34
35 private DSLExpression() {
36 }
37
38 public static DSLExpression parse(String input) {
39 return Parser.parse(input);
40 }
41
42 public final Set<VariableElement> findBoundVariableElements() {
43 final Set<VariableElement> variables = new HashSet<>();
44 this.accept(new AbstractDSLExpressionVisitor() {
45
46 @Override
47 public void visitVariable(Variable variable) {
48 if (variable.getReceiver() == null) {
49 variables.add(variable.getResolvedVariable());
50 }
51 }
52
53 });
54 return variables;
55 }
56
57 public final Set<Variable> findBoundVariables() {
58 final Set<Variable> variables = new HashSet<>();
59 this.accept(new AbstractDSLExpressionVisitor() {
60
61 @Override
62 public void visitVariable(Variable variable) {
63 if (variable.getReceiver() == null) {
64 variables.add(variable);
65 }
66 }
67
68 });
69 return variables;
70 }
71
72 public boolean containsComparisons() {
73 final AtomicBoolean found = new AtomicBoolean();
74 this.accept(new AbstractDSLExpressionVisitor() {
75 @Override
76 public void visitBinary(Binary binary) {
77 if (binary.isComparison()) {
78 found.set(true);
79 }
80 }
81 });
82 return found.get();
83 }
84
85 public void setResolvedTargetType(TypeMirror resolvedTargetType) {
86 this.resolvedTargetType = resolvedTargetType;
87 }
88
89 public TypeMirror getResolvedTargetType() {
90 return resolvedTargetType;
91 }
92
93 public abstract TypeMirror getResolvedType();
94
95 public abstract void accept(DSLExpressionVisitor visitor);
96
97 public static final class Negate extends DSLExpression {
98
99 private final DSLExpression receiver;
100
101 public Negate(DSLExpression receiver) {
102 this.receiver = receiver;
103 }
104
105 @Override
106 public void accept(DSLExpressionVisitor visitor) {
107 receiver.accept(visitor);
108 visitor.visitNegate(this);
109 }
110
111 public DSLExpression getReceiver() {
112 return receiver;
113 }
114
115 @Override
116 public TypeMirror getResolvedType() {
117 return receiver.getResolvedType();
118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 if (obj instanceof Negate) {
123 return receiver.equals(((Negate) obj).receiver);
124 }
125 return false;
126 }
127
128 @Override
129 public int hashCode() {
130 return receiver.hashCode();
131 }
132 }
133
134 public static final class Binary extends DSLExpression {
135
136 private final String operator;
137 private final DSLExpression left;
138 private final DSLExpression right;
139
140 private TypeMirror resolvedType;
141
142 public Binary(String operator, DSLExpression left, DSLExpression right) {
143 this.operator = operator;
144 this.left = left;
145 this.right = right;
146 }
147
148 public boolean isComparison() {
149 return DSLExpressionResolver.COMPARABLE_OPERATORS.contains(operator) || DSLExpressionResolver.IDENTITY_OPERATORS.contains(operator);
150 }
151
152 @Override
153 public boolean equals(Object obj) {
154 if (obj instanceof Binary) {
155 Binary other = (Binary) obj;
156 return operator.equals(other.operator) && left.equals(other.left) && right.equals(other.right);
157 }
158 return false;
159 }
160
161 @Override
162 public int hashCode() {
163 return Objects.hash(operator, left, right);
164 }
165
166 public String getOperator() {
167 return operator;
168 }
169
170 public DSLExpression getLeft() {
171 return left;
172 }
173
174 public DSLExpression getRight() {
175 return right;
176 }
177
178 @Override
179 public void accept(DSLExpressionVisitor visitor) {
180 left.accept(visitor);
181 right.accept(visitor);
182 visitor.visitBinary(this);
183 }
184
185 @Override
186 public TypeMirror getResolvedType() {
187 return resolvedType;
188 }
189
190 public void setResolvedType(TypeMirror resolvedType) {
191 this.resolvedType = resolvedType;
192 }
193
194 @Override
195 public String toString() {
196 return "Binary [left=" + left + ", operator=" + operator + ", right=" + right + ", resolvedType=" + resolvedType + "]";
197 }
198
199 }
200
201 public static final class Call extends DSLExpression {
202
203 private final DSLExpression receiver;
204 private final String name;
205 private final List<DSLExpression> parameters;
206
207 private ExecutableElement resolvedMethod;
208
209 public Call(DSLExpression receiver, String name, List<DSLExpression> parameters) {
210 this.receiver = receiver;
211 this.name = name;
212 this.parameters = parameters;
213 }
214
215 @Override
216 public boolean equals(Object obj) {
217 if (obj instanceof Call) {
218 Call other = (Call) obj;
219 return Objects.equals(receiver, other.receiver) && name.equals(other.name) && parameters.equals(other.parameters);
220 }
221 return false;
222 }
223
224 @Override
225 public int hashCode() {
226 return Objects.hash(receiver, name, parameters);
227 }
228
229 public DSLExpression getReceiver() {
230 return receiver;
231 }
232
233 public String getName() {
234 return name;
235 }
236
237 public List<DSLExpression> getParameters() {
238 return parameters;
239 }
240
241 @Override
242 public void accept(DSLExpressionVisitor visitor) {
243 if (receiver != null) {
244 receiver.accept(visitor);
245 }
246 for (DSLExpression parameter : getParameters()) {
247 parameter.accept(visitor);
248 }
249 visitor.visitCall(this);
250 }
251
252 @Override
253 public TypeMirror getResolvedType() {
254 if (resolvedMethod == null) {
255 return null;
256 }
257 if (resolvedMethod.getKind() == ElementKind.CONSTRUCTOR) {
258 return resolvedMethod.getEnclosingElement().asType();
259 } else {
260 return resolvedMethod.getReturnType();
261 }
262 }
263
264 public ExecutableElement getResolvedMethod() {
265 return resolvedMethod;
266 }
267
268 public void setResolvedMethod(ExecutableElement resolvedMethod) {
269 this.resolvedMethod = resolvedMethod;
270 }
271
272 @Override
273 public String toString() {
274 return "Call [receiver=" + receiver + ", name=" + name + ", parameters=" + parameters + ", resolvedMethod=" + resolvedMethod + "]";
275 }
276
277 }
278
279 public static final class Variable extends DSLExpression {
280
281 private final DSLExpression receiver;
282 private final String name;
283
284 private VariableElement resolvedVariable;
285
286 public Variable(DSLExpression receiver, String name) {
287 this.receiver = receiver;
288 this.name = name;
289 }
290
291 @Override
292 public boolean equals(Object obj) {
293 if (obj instanceof Variable) {
294 Variable other = (Variable) obj;
295 return Objects.equals(receiver, other.receiver) && name.equals(other.name);
296 }
297 return false;
298 }
299
300 @Override
301 public int hashCode() {
302 return Objects.hash(receiver, name);
303 }
304
305 public DSLExpression getReceiver() {
306 return receiver;
307 }
308
309 public String getName() {
310 return name;
311 }
312
313 @Override
314 public void accept(DSLExpressionVisitor visitor) {
315 if (receiver != null) {
316 receiver.accept(visitor);
317 }
318 visitor.visitVariable(this);
319 }
320
321 @Override
322 public TypeMirror getResolvedType() {
323 return resolvedVariable != null ? resolvedVariable.asType() : null;
324 }
325
326 public void setResolvedVariable(VariableElement resolvedVariable) {
327 this.resolvedVariable = resolvedVariable;
328 }
329
330 public VariableElement getResolvedVariable() {
331 return resolvedVariable;
332 }
333
334 @Override
335 public String toString() {
336 return "Variable [receiver=" + receiver + ", name=" + name + ", resolvedVariable=" + resolvedVariable + "]";
337 }
338
339 }
340
341 public static final class IntLiteral extends DSLExpression {
342
343 private final String literal;
344
345 private int resolvedValueInt;
346 private TypeMirror resolvedType;
347
348 public IntLiteral(String literal) {
349 this.literal = literal;
350 }
351
352 @Override
353 public boolean equals(Object obj) {
354 if (obj instanceof IntLiteral) {
355 IntLiteral other = (IntLiteral) obj;
356 return resolvedValueInt == other.resolvedValueInt;
357 }
358 return false;
359 }
360
361 @Override
362 public int hashCode() {
363 return resolvedValueInt;
364 }
365
366 public String getLiteral() {
367 return literal;
368 }
369
370 public int getResolvedValueInt() {
371 return resolvedValueInt;
372 }
373
374 public void setResolvedValueInt(int resolved) {
375 this.resolvedValueInt = resolved;
376 }
377
378 @Override
379 public TypeMirror getResolvedType() {
380 return resolvedType;
381 }
382
383 public void setResolvedType(TypeMirror resolvedType) {
384 this.resolvedType = resolvedType;
385 }
386
387 @Override
388 public void accept(DSLExpressionVisitor visitor) {
389 visitor.visitIntLiteral(this);
390 }
391
392 @Override
393 public String toString() {
394 return "IntLiteral [literal=" + literal + ", resolvedValueInt=" + resolvedValueInt + ", resolvedType=" + resolvedType + "]";
395 }
396
397 }
398
399 public abstract class AbstractDSLExpressionVisitor implements DSLExpressionVisitor {
400
401 public void visitBinary(Binary binary) {
402 }
403
404 public void visitCall(Call binary) {
405 }
406
407 public void visitIntLiteral(IntLiteral binary) {
408 }
409
410 public void visitNegate(Negate negate) {
411 }
412
413 public void visitVariable(Variable binary) {
414 }
415 }
416
417 public interface DSLExpressionVisitor {
418
419 void visitBinary(Binary binary);
420
421 void visitNegate(Negate negate);
422
423 void visitCall(Call binary);
424
425 void visitVariable(Variable binary);
426
427 void visitIntLiteral(IntLiteral binary);
428
429 }
430
431 }