Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.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/DSLExpressionResolver.java@f83fd99b2962 |
children | 5bc7f7b867ab |
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 | |
27 import javax.lang.model.element.*; | |
28 import javax.lang.model.type.*; | |
29 import javax.lang.model.util.*; | |
30 | |
31 import com.oracle.truffle.dsl.processor.*; | |
32 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary; | |
33 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Call; | |
34 import com.oracle.truffle.dsl.processor.expression.DSLExpression.DSLExpressionVisitor; | |
35 import com.oracle.truffle.dsl.processor.expression.DSLExpression.IntLiteral; | |
36 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Negate; | |
37 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable; | |
38 import com.oracle.truffle.dsl.processor.java.*; | |
39 import com.oracle.truffle.dsl.processor.java.model.*; | |
40 | |
41 public class DSLExpressionResolver implements DSLExpressionVisitor { | |
42 | |
43 private static final List<String> LOGIC_OPERATORS = Arrays.asList("||"); | |
44 public static final List<String> COMPARABLE_OPERATORS = Arrays.asList("<", "<=", ">", ">="); | |
45 public static final List<String> IDENTITY_OPERATORS = Arrays.asList("==", "!="); | |
46 private static final String CONSTRUCTOR_KEYWORD = "new"; | |
47 | |
48 private final List<VariableElement> variables = new ArrayList<>(); | |
49 private final List<ExecutableElement> methods = new ArrayList<>(); | |
50 private final ProcessorContext context; | |
51 | |
52 private DSLExpressionResolver(ProcessorContext context) { | |
53 this.context = context; | |
54 } | |
55 | |
56 public DSLExpressionResolver(ProcessorContext context, List<? extends Element> lookupElements) { | |
57 this(context); | |
58 lookup(lookupElements); | |
59 } | |
60 | |
61 public DSLExpressionResolver copy(List<? extends Element> prefixElements) { | |
62 DSLExpressionResolver resolver = new DSLExpressionResolver(context); | |
63 resolver.lookup(prefixElements); | |
64 resolver.variables.addAll(variables); | |
65 resolver.methods.addAll(methods); | |
66 return resolver; | |
67 } | |
68 | |
69 private void lookup(List<? extends Element> lookupElements) { | |
70 variablesIn(variables, lookupElements, false); | |
71 methodsIn(lookupElements); | |
72 } | |
73 | |
74 private void methodsIn(List<? extends Element> lookupElements) { | |
75 for (Element variable : lookupElements) { | |
76 ElementKind kind = variable.getKind(); | |
77 if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) { | |
78 methods.add((ExecutableElement) variable); | |
79 } | |
80 } | |
81 } | |
82 | |
83 private static void variablesIn(List<VariableElement> variables, List<? extends Element> lookupElements, boolean publicOnly) { | |
84 for (Element variable : lookupElements) { | |
85 ElementKind kind = variable.getKind(); | |
86 if (kind == ElementKind.LOCAL_VARIABLE || kind == ElementKind.PARAMETER || kind == ElementKind.FIELD || kind == ElementKind.ENUM_CONSTANT) { | |
87 VariableElement variableElement = (VariableElement) variable; | |
88 if (!publicOnly || variableElement.getModifiers().contains(Modifier.PUBLIC)) { | |
89 variables.add(variableElement); | |
90 } | |
91 } | |
92 } | |
93 } | |
94 | |
95 private static String getMethodName(ExecutableElement method) { | |
96 if (method.getKind() == ElementKind.CONSTRUCTOR) { | |
97 return CONSTRUCTOR_KEYWORD; | |
98 } else { | |
99 return method.getSimpleName().toString(); | |
100 } | |
101 } | |
102 | |
103 public void visitBinary(Binary binary) { | |
104 String operator = binary.getOperator(); | |
105 TypeMirror leftType = binary.getLeft().getResolvedType(); | |
106 TypeMirror rightType = binary.getRight().getResolvedType(); | |
107 if (!ElementUtils.areTypesCompatible(leftType, rightType)) { | |
108 throw new InvalidExpressionException(String.format("Incompatible operand types %s and %s.", ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); | |
109 } | |
110 | |
111 TypeMirror booleanType = context.getType(boolean.class); | |
112 boolean valid; | |
113 if (LOGIC_OPERATORS.contains(operator)) { | |
114 valid = ElementUtils.typeEquals(leftType, booleanType); | |
115 } else if (COMPARABLE_OPERATORS.contains(operator)) { | |
116 valid = ElementUtils.isPrimitive(leftType); | |
117 } else if (IDENTITY_OPERATORS.contains(operator)) { | |
118 valid = leftType.getKind().isPrimitive() || leftType.getKind() == TypeKind.DECLARED || leftType.getKind() == TypeKind.ARRAY; | |
119 } else { | |
120 throw new InvalidExpressionException(String.format("The operator %s is undefined.", operator)); | |
121 } | |
122 binary.setResolvedType(booleanType); | |
123 | |
124 if (!valid) { | |
125 throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type(s) %s %s.", operator, ElementUtils.getSimpleName(leftType), | |
126 ElementUtils.getSimpleName(rightType))); | |
127 } | |
128 } | |
129 | |
130 public void visitNegate(Negate negate) { | |
131 TypeMirror booleanType = context.getType(boolean.class); | |
132 TypeMirror resolvedType = negate.getResolvedType(); | |
133 if (!ElementUtils.typeEquals(resolvedType, booleanType)) { | |
134 throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type %s.", "!", ElementUtils.getSimpleName(resolvedType))); | |
135 } | |
136 } | |
137 | |
138 public void visitCall(Call call) { | |
139 List<ExecutableElement> lookupMethods; | |
140 DSLExpression receiver = call.getReceiver(); | |
141 if (receiver == null) { | |
142 lookupMethods = this.methods; | |
143 } else { | |
144 TypeMirror type = receiver.getResolvedType(); | |
145 if (type.getKind() == TypeKind.DECLARED) { | |
146 type = context.reloadType(type); // ensure ECJ has the type loaded | |
147 lookupMethods = ElementFilter.methodsIn(context.getEnvironment().getElementUtils().getAllMembers((TypeElement) ((DeclaredType) type).asElement())); | |
148 } else { | |
149 lookupMethods = Collections.emptyList(); | |
150 } | |
151 } | |
152 | |
153 ExecutableElement foundWithName = null; | |
154 outer: for (ExecutableElement method : lookupMethods) { | |
155 if (getMethodName(method).equals(call.getName())) { | |
156 foundWithName = method; | |
157 | |
158 List<? extends VariableElement> parameters = method.getParameters(); | |
159 if (parameters.size() != call.getParameters().size()) { | |
160 continue outer; | |
161 } | |
162 | |
163 int parameterIndex = 0; | |
164 for (DSLExpression expression : call.getParameters()) { | |
165 TypeMirror sourceType = expression.getResolvedType(); | |
166 TypeMirror targetType = parameters.get(parameterIndex).asType(); | |
167 if (!ElementUtils.isAssignable(sourceType, targetType)) { | |
168 continue outer; | |
169 } | |
170 expression.setResolvedTargetType(targetType); | |
171 parameterIndex++; | |
172 } | |
173 | |
174 call.setResolvedMethod(method); | |
175 break; | |
176 } | |
177 } | |
178 if (call.getResolvedMethod() == null) { | |
179 if (foundWithName == null) { | |
180 // parameter mismatch | |
181 throw new InvalidExpressionException(String.format("The method %s is undefined for the enclosing scope.", call.getName())); | |
182 } else { | |
183 StringBuilder arguments = new StringBuilder(); | |
184 String sep = ""; | |
185 for (DSLExpression expression : call.getParameters()) { | |
186 arguments.append(sep).append(ElementUtils.getSimpleName(expression.getResolvedType())); | |
187 sep = ", "; | |
188 } | |
189 // name mismatch | |
190 throw new InvalidExpressionException(String.format("The method %s in the type %s is not applicable for the arguments %s.", // | |
191 ElementUtils.getReadableSignature(foundWithName), // | |
192 ElementUtils.getSimpleName((TypeElement) foundWithName.getEnclosingElement()), arguments.toString())); | |
193 } | |
194 } | |
195 } | |
196 | |
197 public void visitVariable(Variable variable) { | |
198 List<VariableElement> lookupVariables; | |
199 DSLExpression receiver = variable.getReceiver(); | |
200 if (variable.getName().equals("null")) { | |
201 variable.setResolvedVariable(new CodeVariableElement(new CodeTypeMirror(TypeKind.NULL), "null")); | |
202 } else { | |
203 if (receiver == null) { | |
204 lookupVariables = this.variables; | |
205 } else { | |
206 TypeMirror type = receiver.getResolvedType(); | |
207 if (type.getKind() == TypeKind.DECLARED) { | |
208 type = context.reloadType(type); // ensure ECJ has the type loaded | |
209 lookupVariables = new ArrayList<>(); | |
210 variablesIn(lookupVariables, context.getEnvironment().getElementUtils().getAllMembers((TypeElement) ((DeclaredType) type).asElement()), true); | |
211 } else if (type.getKind() == TypeKind.ARRAY) { | |
212 lookupVariables = Arrays.<VariableElement> asList(new CodeVariableElement(context.getType(int.class), "length")); | |
213 } else { | |
214 lookupVariables = Collections.emptyList(); | |
215 } | |
216 } | |
217 | |
218 for (VariableElement variableElement : lookupVariables) { | |
219 if (variableElement.getSimpleName().toString().equals(variable.getName())) { | |
220 variable.setResolvedVariable(variableElement); | |
221 break; | |
222 } | |
223 } | |
224 } | |
225 | |
226 if (variable.getResolvedVariable() == null) { | |
227 throw new InvalidExpressionException(String.format("%s cannot be resolved.", variable.getName())); | |
228 } | |
229 } | |
230 | |
231 public void visitIntLiteral(IntLiteral binary) { | |
232 try { | |
233 binary.setResolvedType(context.getType(int.class)); | |
234 | |
235 final int base; | |
236 final String literal; | |
237 | |
238 if (binary.getLiteral().startsWith("0x")) { | |
239 base = 16; | |
240 literal = binary.getLiteral().substring(2); | |
241 } else if (binary.getLiteral().startsWith("0b")) { | |
242 base = 2; | |
243 literal = binary.getLiteral().substring(2); | |
244 } else if (binary.getLiteral().startsWith("0")) { | |
245 base = 8; | |
246 literal = binary.getLiteral(); | |
247 } else { | |
248 base = 10; | |
249 literal = binary.getLiteral(); | |
250 } | |
251 | |
252 binary.setResolvedValueInt(Integer.parseInt(literal, base)); | |
253 } catch (NumberFormatException e) { | |
254 throw new InvalidExpressionException(String.format("Type mismatch: cannot convert from String '%s' to int", binary.getLiteral())); | |
255 } | |
256 } | |
257 | |
258 } |