comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/TypeSystemCodeGenerator.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/generator/TypeSystemCodeGenerator.java@2e850dbf82ae
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2012, 2012, 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.generator;
24
25 import static com.oracle.truffle.dsl.processor.generator.GeneratorUtils.*;
26 import static com.oracle.truffle.dsl.processor.java.ElementUtils.*;
27 import static javax.lang.model.element.Modifier.*;
28
29 import java.util.*;
30
31 import javax.lang.model.element.*;
32 import javax.lang.model.type.*;
33
34 import com.oracle.truffle.api.nodes.*;
35 import com.oracle.truffle.dsl.processor.*;
36 import com.oracle.truffle.dsl.processor.java.*;
37 import com.oracle.truffle.dsl.processor.java.model.*;
38 import com.oracle.truffle.dsl.processor.model.*;
39
40 public class TypeSystemCodeGenerator extends CodeTypeElementFactory<TypeSystemData> {
41
42 private static final String LOCAL_VALUE = "value";
43
44 public static CodeTree cast(TypeSystemData typeSystem, TypeMirror type, String content) {
45 return cast(typeSystem, type, CodeTreeBuilder.singleString(content));
46 }
47
48 public static CodeTree implicitType(TypeSystemData typeSystem, TypeMirror type, CodeTree value) {
49 if (ElementUtils.isObject(type)) {
50 return value;
51 }
52 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
53 builder.startStaticCall(createTypeSystemGen(typeSystem), getImplicitClass(typeSystem, type)).tree(value);
54 builder.end();
55 return builder.build();
56 }
57
58 public static CodeTree invokeImplicitCast(TypeSystemData typeSystem, ImplicitCastData cast, CodeTree expression) {
59 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
60 builder.startStaticCall(createTypeSystemGen(typeSystem), cast.getMethodName()).tree(expression);
61 builder.end();
62 return builder.build();
63 }
64
65 public static CodeTree implicitCheck(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) {
66 return callImplictMethod(typeSystem, type, isImplicitTypeMethodName(typeSystem, type), value, typeHint);
67 }
68
69 public static CodeTree implicitExpect(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) {
70 return callImplictMethod(typeSystem, type, expectImplicitTypeMethodName(typeSystem, type), value, typeHint);
71 }
72
73 public static CodeTree implicitCast(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) {
74 return callImplictMethod(typeSystem, type, asImplicitTypeMethodName(typeSystem, type), value, typeHint);
75 }
76
77 private static CodeTree callImplictMethod(TypeSystemData typeSystem, TypeMirror type, String methodName, CodeTree value, String typeHint) {
78 if (ElementUtils.isObject(type)) {
79 return value;
80 }
81 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
82 builder.startStaticCall(createTypeSystemGen(typeSystem), methodName).tree(value);
83 if (typeHint != null) {
84 builder.string(typeHint);
85 }
86 builder.end();
87 return builder.build();
88 }
89
90 public static CodeTree cast(TypeSystemData typeSystem, TypeMirror type, CodeTree content) {
91 if (ElementUtils.isObject(type)) {
92 return content;
93 }
94 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
95
96 TypeCastData cast = typeSystem.getCast(type);
97 if (cast == null) {
98 builder.cast(ElementUtils.fillInGenericWildcards(type), content);
99 } else {
100 builder.startStaticCall(typeSystem.getTemplateType().asType(), cast.getMethodName()).tree(content).end();
101 }
102 return builder.build();
103 }
104
105 public static CodeTree expect(TypeSystemData typeSystem, TypeMirror type, CodeTree content) {
106 if (ElementUtils.isObject(type) || ElementUtils.isVoid(type)) {
107 return content;
108 }
109 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
110 if (typeSystem.hasType(type)) {
111 builder.startStaticCall(createTypeSystemGen(typeSystem), expectTypeMethodName(typeSystem, type)).tree(content).end();
112 } else {
113 builder.startCall(expectTypeMethodName(typeSystem, type)).tree(content).end();
114 }
115
116 return builder.build();
117 }
118
119 public static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceTypeOriginal, TypeMirror expectedTypeOriginal) {
120 TypeMirror expectedType = ElementUtils.fillInGenericWildcards(expectedTypeOriginal);
121 TypeMirror sourceType = ElementUtils.fillInGenericWildcards(sourceTypeOriginal);
122 if (ElementUtils.isObject(expectedType) || ElementUtils.isVoid(expectedType)) {
123 return null;
124 }
125
126 CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), expectedType, TypeSystemCodeGenerator.expectTypeMethodName(typeSystem, expectedType));
127 method.setVisibility(visibility);
128 method.addParameter(new CodeVariableElement(sourceType, LOCAL_VALUE));
129 method.addThrownType(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException());
130
131 CodeTreeBuilder body = method.createBuilder();
132 body.startIf().tree(check(typeSystem, expectedType, LOCAL_VALUE)).end().startBlock();
133 body.startReturn().tree(cast(typeSystem, expectedType, LOCAL_VALUE)).end();
134 body.end();
135 body.startThrow().startNew(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end();
136 return method;
137 }
138
139 public static CodeTree expect(TypeSystemData typeSystem, TypeMirror sourceType, TypeMirror targetType, CodeTree content) {
140 if (sourceType != null && !ElementUtils.needsCastTo(sourceType, targetType)) {
141 return content;
142 } else {
143 return expect(typeSystem, targetType, content);
144 }
145 }
146
147 public static CodeTypeMirror createTypeSystemGen(TypeSystemData typeSystem) {
148 return new GeneratedTypeMirror(ElementUtils.getPackageName(typeSystem.getTemplateType()), typeName(typeSystem));
149 }
150
151 public static CodeTree check(TypeSystemData typeSystem, TypeMirror type, String content) {
152 return check(typeSystem, type, CodeTreeBuilder.singleString(content));
153 }
154
155 public static CodeTree check(TypeSystemData typeSystem, TypeMirror type, CodeTree content) {
156 if (ElementUtils.isObject(type)) {
157 return content;
158 }
159 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
160
161 TypeCheckData check = typeSystem.getCheck(type);
162 if (check == null) {
163 builder.instanceOf(content, ElementUtils.boxType(typeSystem.getContext(), type));
164 } else {
165 builder.startStaticCall(typeSystem.getTemplateType().asType(), check.getMethodName()).tree(content).end();
166 }
167 return builder.build();
168 }
169
170 public static String isTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
171 return "is" + getTypeId(typeSystem, type);
172 }
173
174 private static String getTypeId(TypeSystemData typeSystem, TypeMirror type) {
175 return ElementUtils.getTypeId(typeSystem.boxType(type));
176 }
177
178 static String isImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
179 return "isImplicit" + getTypeId(typeSystem, type);
180 }
181
182 public static String asTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
183 return "as" + getTypeId(typeSystem, type);
184 }
185
186 static String asImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
187 return "asImplicit" + getTypeId(typeSystem, type);
188 }
189
190 static String expectImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
191 return "expectImplicit" + getTypeId(typeSystem, type);
192 }
193
194 static String getImplicitClass(TypeSystemData typeSystem, TypeMirror type) {
195 return "getImplicit" + getTypeId(typeSystem, type) + "Class";
196 }
197
198 public static String expectTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
199 return "expect" + getTypeId(typeSystem, type);
200 }
201
202 static String typeName(TypeSystemData typeSystem) {
203 String name = getSimpleName(typeSystem.getTemplateType());
204 return name + "Gen";
205 }
206
207 static String singletonName(TypeSystemData type) {
208 return createConstantName(getSimpleName(type.getTemplateType().asType()));
209 }
210
211 @Override
212 public CodeTypeElement create(ProcessorContext context, TypeSystemData typeSystem) {
213 CodeTypeElement clazz = new TypeClassFactory(context, typeSystem).create();
214
215 if (typeSystem.getOptions().implicitCastOptimization().isMergeCasts()) {
216 for (TypeMirror type : typeSystem.lookupTargetTypes()) {
217 clazz.add(new ImplicitCastNodeFactory(context, typeSystem, type).create());
218 }
219 }
220 return clazz;
221 }
222
223 private static class TypeClassFactory {
224
225 private final ProcessorContext context;
226 private final TypeSystemData typeSystem;
227
228 public TypeClassFactory(ProcessorContext context, TypeSystemData typeSystem) {
229 this.context = context;
230 this.typeSystem = typeSystem;
231 }
232
233 public CodeTypeElement create() {
234 String name = typeName(typeSystem);
235 CodeTypeElement clazz = GeneratorUtils.createClass(typeSystem, null, modifiers(PUBLIC, FINAL), name, typeSystem.getTemplateType().asType());
236
237 clazz.add(GeneratorUtils.createConstructorUsingFields(modifiers(PROTECTED), clazz));
238 CodeVariableElement singleton = createSingleton(clazz);
239 clazz.add(singleton);
240
241 for (TypeMirror type : typeSystem.getLegacyTypes()) {
242 if (ElementUtils.isVoid(type) || ElementUtils.isObject(type)) {
243 continue;
244 }
245
246 clazz.addOptional(createIsTypeMethod(type));
247 clazz.addOptional(createAsTypeMethod(type));
248 clazz.addOptional(createExpectTypeMethod(type, context.getType(Object.class)));
249
250 }
251
252 List<TypeMirror> lookupTargetTypes = typeSystem.lookupTargetTypes();
253 for (TypeMirror type : lookupTargetTypes) {
254 clazz.add(createAsImplicitTypeMethod(type, false));
255 if (typeSystem.getOptions().implicitCastOptimization().isNone()) {
256 clazz.add(createExpectImplicitTypeMethod(type, false));
257 }
258 clazz.add(createIsImplicitTypeMethod(type, false));
259
260 if (typeSystem.getOptions().implicitCastOptimization().isDuplicateTail()) {
261 clazz.add(createAsImplicitTypeMethod(type, true));
262 clazz.add(createExpectImplicitTypeMethod(type, true));
263 clazz.add(createIsImplicitTypeMethod(type, true));
264 clazz.add(createGetImplicitClass(type));
265 }
266 }
267 return clazz;
268 }
269
270 private CodeVariableElement createSingleton(CodeTypeElement clazz) {
271 CodeVariableElement field = new CodeVariableElement(modifiers(PUBLIC, STATIC, FINAL), clazz.asType(), singletonName(typeSystem));
272 field.createInitBuilder().startNew(clazz.asType()).end();
273
274 CodeAnnotationMirror annotationMirror = new CodeAnnotationMirror((DeclaredType) context.getType(Deprecated.class));
275 field.getAnnotationMirrors().add(annotationMirror);
276
277 return field;
278 }
279
280 private CodeExecutableElement createIsImplicitTypeMethod(TypeMirror type, boolean typed) {
281 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isImplicitTypeMethodName(typeSystem, type));
282 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
283 if (typed) {
284 method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint"));
285 }
286 CodeTreeBuilder builder = method.createBuilder();
287
288 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
289
290 builder.startReturn();
291 String sep = "";
292 for (TypeMirror sourceType : sourceTypes) {
293 builder.string(sep);
294 if (typed) {
295 builder.string("(typeHint == ").typeLiteral(sourceType).string(" && ");
296 }
297 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
298 if (typed) {
299 builder.string(")");
300 }
301 if (sourceTypes.lastIndexOf(sourceType) != sourceTypes.size() - 1) {
302 builder.newLine();
303 }
304 if (sep.equals("")) {
305 builder.startIndention();
306 }
307 sep = " || ";
308 }
309 builder.end();
310 builder.end();
311 return method;
312 }
313
314 private CodeExecutableElement createAsImplicitTypeMethod(TypeMirror type, boolean useTypeHint) {
315 String name = asImplicitTypeMethodName(typeSystem, type);
316 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, name);
317 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
318 if (useTypeHint) {
319 method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint"));
320 }
321
322 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
323
324 CodeTreeBuilder builder = method.createBuilder();
325 boolean elseIf = false;
326 for (TypeMirror sourceType : sourceTypes) {
327 elseIf = builder.startIf(elseIf);
328 if (useTypeHint) {
329 builder.string("typeHint == ").typeLiteral(sourceType);
330 } else {
331 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
332 }
333
334 builder.end().startBlock();
335
336 builder.startReturn();
337 ImplicitCastData cast = typeSystem.lookupCast(sourceType, type);
338 if (cast != null) {
339 builder.startCall(cast.getMethodName());
340 }
341 builder.tree(cast(typeSystem, sourceType, LOCAL_VALUE)).end();
342 if (cast != null) {
343 builder.end();
344 }
345 builder.end();
346 builder.end();
347 }
348
349 builder.startElseBlock();
350 builder.tree(createTransferToInterpreterAndInvalidate());
351 builder.startThrow().startNew(context.getType(IllegalArgumentException.class)).doubleQuote("Illegal type ").end().end();
352 builder.end();
353 return method;
354 }
355
356 private CodeExecutableElement createExpectImplicitTypeMethod(TypeMirror type, boolean useTypeHint) {
357 String name = expectImplicitTypeMethodName(typeSystem, type);
358 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, name);
359 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
360 if (useTypeHint) {
361 method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint"));
362 }
363 method.getThrownTypes().add(context.getType(UnexpectedResultException.class));
364
365 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
366
367 CodeTreeBuilder builder = method.createBuilder();
368 boolean elseIf = false;
369 for (TypeMirror sourceType : sourceTypes) {
370 elseIf = builder.startIf(elseIf);
371 if (useTypeHint) {
372 builder.string("typeHint == ").typeLiteral(sourceType);
373 builder.string(" && ");
374 }
375 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
376
377 builder.end().startBlock();
378
379 builder.startReturn();
380 ImplicitCastData cast = typeSystem.lookupCast(sourceType, type);
381 if (cast != null) {
382 builder.startCall(cast.getMethodName());
383 }
384 builder.tree(cast(typeSystem, sourceType, LOCAL_VALUE)).end();
385 if (cast != null) {
386 builder.end();
387 }
388 builder.end();
389 builder.end();
390 }
391
392 builder.startElseBlock();
393 builder.startThrow().startNew(context.getType(UnexpectedResultException.class)).string(LOCAL_VALUE).end().end();
394 builder.end();
395 return method;
396 }
397
398 private CodeExecutableElement createGetImplicitClass(TypeMirror type) {
399 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(Class.class), TypeSystemCodeGenerator.getImplicitClass(typeSystem, type));
400 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
401
402 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
403 CodeTreeBuilder builder = method.createBuilder();
404 boolean elseIf = false;
405 for (TypeMirror sourceType : sourceTypes) {
406 elseIf = builder.startIf(elseIf);
407 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)).end();
408 builder.end().startBlock();
409 builder.startReturn().typeLiteral(sourceType).end();
410 builder.end();
411 }
412
413 builder.startElseIf().string(LOCAL_VALUE).string(" == ").nullLiteral().end();
414 builder.startBlock();
415 builder.startReturn().typeLiteral(context.getType(Object.class)).end();
416 builder.end();
417
418 builder.startElseBlock();
419 builder.tree(createTransferToInterpreterAndInvalidate());
420 builder.startThrow().startNew(context.getType(IllegalArgumentException.class)).doubleQuote("Illegal type ").end().end();
421 builder.end();
422
423 return method;
424 }
425
426 private CodeExecutableElement createIsTypeMethod(TypeMirror type) {
427 if (typeSystem.getCheck(type) != null) {
428 return null;
429 }
430
431 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isTypeMethodName(typeSystem, type));
432 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
433
434 CodeTreeBuilder body = method.createBuilder();
435 body.startReturn().tree(check(typeSystem, type, LOCAL_VALUE)).end();
436
437 return method;
438 }
439
440 private CodeExecutableElement createAsTypeMethod(TypeMirror type) {
441 if (typeSystem.getCast(type) != null) {
442 return null;
443 }
444
445 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, TypeSystemCodeGenerator.asTypeMethodName(typeSystem, type));
446 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
447
448 CodeTreeBuilder body = method.createBuilder();
449 String assertMessage = typeName(typeSystem) + "." + asTypeMethodName(typeSystem, type) + ": " + ElementUtils.getSimpleName(type) + " expected";
450 body.startAssert().tree(check(typeSystem, type, LOCAL_VALUE)).string(" : ").doubleQuote(assertMessage).end();
451 body.startReturn().tree(cast(typeSystem, type, LOCAL_VALUE)).end();
452
453 return method;
454 }
455
456 private CodeExecutableElement createExpectTypeMethod(TypeMirror expectedType, TypeMirror sourceType) {
457 return createExpectMethod(Modifier.PUBLIC, typeSystem, sourceType, expectedType);
458 }
459 }
460
461 }