annotate truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/DSLExpressionGenerator.java @ 22157:dc83cc1f94f2

Using fully qualified imports
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 16 Sep 2015 11:33:22 +0200
parents 9c8c0937da41
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1 /*
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
4 *
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
8 *
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
13 * accompanied this code).
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
14 *
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
18 *
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
21 * questions.
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
22 */
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
23 package com.oracle.truffle.dsl.processor.generator;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
24
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
25 import com.oracle.truffle.dsl.processor.expression.DSLExpression;
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
26 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
27 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Call;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
28 import com.oracle.truffle.dsl.processor.expression.DSLExpression.DSLExpressionVisitor;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
29 import com.oracle.truffle.dsl.processor.expression.DSLExpression.IntLiteral;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
30 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Negate;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
31 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
32 import com.oracle.truffle.dsl.processor.java.model.CodeTree;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
33 import com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
34 import java.util.ArrayDeque;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
35 import java.util.Deque;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
36 import java.util.Map;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
37 import javax.lang.model.element.Element;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
38 import javax.lang.model.element.ElementKind;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
39 import javax.lang.model.element.ExecutableElement;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
40 import javax.lang.model.element.Modifier;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
41 import javax.lang.model.element.VariableElement;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
42 import javax.lang.model.type.TypeKind;
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
43
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
44 public class DSLExpressionGenerator implements DSLExpressionVisitor {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
45
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
46 private final Map<Variable, CodeTree> bindings;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
47 private final CodeTree root;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
48 private final Deque<CodeTree> stack = new ArrayDeque<>();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
49
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
50 public DSLExpressionGenerator(CodeTree root, Map<Variable, CodeTree> bindings) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51 this.bindings = bindings;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52 this.root = root;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
53 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
54
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
55 public void visitBinary(Binary binary) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
56 CodeTree right = stack.pop();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
57 CodeTree left = stack.pop();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
58 stack.push(combine(left, string(" " + binary.getOperator() + " "), right));
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
59 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
60
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
61 public void visitCall(Call call) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
62 ExecutableElement method = call.getResolvedMethod();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
63 CodeTree[] parameters = new CodeTree[method.getParameters().size()];
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
64 for (int i = 0; i < parameters.length; i++) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
65 parameters[parameters.length - i - 1] = pop();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
66 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
67
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
68 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
69
19289
62c43fcf5be2 Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents: 19283
diff changeset
70 if (call.getResolvedMethod().getKind() == ElementKind.CONSTRUCTOR) {
62c43fcf5be2 Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents: 19283
diff changeset
71 builder.startNew(call.getResolvedType());
62c43fcf5be2 Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents: 19283
diff changeset
72 } else if (call.getReceiver() == null) {
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
73 if (isStatic(method)) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
74 builder.startStaticCall(method);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
75 } else {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
76 if (root != null) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
77 builder.tree(root).string(".");
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
78 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
79 builder.startCall(method.getSimpleName().toString());
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
80 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
81 } else {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
82 if (isStatic(method)) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
83 throw new AssertionError("Static calls must not have receivers.");
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
84 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
85 builder.startCall(pop(), method.getSimpleName().toString());
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
86 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
87 for (CodeTree parameter : parameters) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
88 builder.tree(parameter);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
89 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
90 builder.end();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
91
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
92 push(builder.build());
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
93 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
94
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
95 public void visitIntLiteral(IntLiteral binary) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
96 push(string(binary.getLiteral()));
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
97 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
98
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
99 public void visitNegate(Negate negate) {
19289
62c43fcf5be2 Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents: 19283
diff changeset
100 push(combine(string("!"), combine(string("("), pop(), string(")"))));
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
101 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
102
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
103 public void visitVariable(Variable variable) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
104 VariableElement resolvedVariable = variable.getResolvedVariable();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
105 CodeTree tree;
20939
f83fd99b2962 Truffle-DSL: add support for null literals.
Christian Humer <christian.humer@gmail.com>
parents: 19289
diff changeset
106 if (variable.getResolvedType().getKind() == TypeKind.NULL) {
f83fd99b2962 Truffle-DSL: add support for null literals.
Christian Humer <christian.humer@gmail.com>
parents: 19289
diff changeset
107 tree = CodeTreeBuilder.singleString("null");
f83fd99b2962 Truffle-DSL: add support for null literals.
Christian Humer <christian.humer@gmail.com>
parents: 19289
diff changeset
108 } else if (variable.getReceiver() == null) {
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
109
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
110 if (isStatic(resolvedVariable)) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
111 tree = staticReference(resolvedVariable);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
112 } else {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
113 tree = bindings.get(variable);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
114 boolean bound = true;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
115 if (tree == null) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
116 tree = string(resolvedVariable.getSimpleName().toString());
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
117 bound = false;
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
118 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
119 if (root != null && !bound) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
120 tree = combine(root, string("."), tree);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
121 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
122 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
123 } else {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
124 if (isStatic(resolvedVariable)) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
125 throw new AssertionError("Static variables cannot have receivers.");
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
126 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
127 tree = combine(pop(), string("."), string(resolvedVariable.getSimpleName().toString()));
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
128 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
129 push(tree);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
130 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
131
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
132 private static boolean isStatic(Element element) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
133 return element.getModifiers().contains(Modifier.STATIC);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
134 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
135
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
136 private static CodeTree combine(CodeTree tree1, CodeTree tree2) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
137 return new CodeTreeBuilder(null).startGroup().tree(tree1).tree(tree2).end().build();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
138 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
139
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
140 private static CodeTree combine(CodeTree tree1, CodeTree tree2, CodeTree tree3) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
141 return new CodeTreeBuilder(null).startGroup().tree(tree1).tree(tree2).tree(tree3).end().build();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
142 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
143
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
144 private static CodeTree string(String s) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
145 return CodeTreeBuilder.singleString(s);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
146 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
147
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
148 private static CodeTree staticReference(VariableElement var) {
19289
62c43fcf5be2 Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents: 19283
diff changeset
149 return CodeTreeBuilder.createBuilder().staticReference(var.getEnclosingElement().asType(), var.getSimpleName().toString()).build();
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
150 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
151
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
152 private void push(CodeTree tree) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
153 stack.push(tree);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
154 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
155
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
156 private CodeTree pop() {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
157 return stack.pop();
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
158 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
159
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
160 public static CodeTree write(DSLExpression expression, CodeTree root, Map<Variable, CodeTree> bindings) {
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
161 DSLExpressionGenerator writer = new DSLExpressionGenerator(root, bindings);
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
162 expression.accept(writer);
19289
62c43fcf5be2 Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents: 19283
diff changeset
163 return combine(string("("), writer.pop(), string(")"));
19283
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
164 }
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
165
08aa0372dad4 Truffle-DSL: implement new guard expression syntax.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
166 }