Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/ImplicitCastNodeFactory.java @ 20938:18c0f02fa4d2
Truffle-DSL: make type systems optional.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 14 Apr 2015 15:12:48 +0200 |
parents | a665483c3881 |
children | b1530a6cce8c |
rev | line source |
---|---|
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
1 /* |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
4 * |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
8 * |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
13 * accompanied this code). |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
14 * |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
18 * |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
21 * questions. |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
22 */ |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
23 package com.oracle.truffle.dsl.processor.generator; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
24 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
25 import static com.oracle.truffle.dsl.processor.java.ElementUtils.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
26 import static javax.lang.model.element.Modifier.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
27 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
28 import java.util.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
29 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
30 import javax.lang.model.element.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
31 import javax.lang.model.type.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
32 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
33 import com.oracle.truffle.api.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
34 import com.oracle.truffle.api.CompilerDirectives.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
35 import com.oracle.truffle.api.dsl.internal.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
36 import com.oracle.truffle.api.nodes.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
37 import com.oracle.truffle.dsl.processor.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 import com.oracle.truffle.dsl.processor.java.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 import com.oracle.truffle.dsl.processor.java.model.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 import com.oracle.truffle.dsl.processor.model.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 import static com.oracle.truffle.dsl.processor.generator.GeneratorUtils.*; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 public class ImplicitCastNodeFactory { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 private final ProcessorContext context; |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
47 private final TypeMirror forType; |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 private final TypeSystemData typeSystem; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 private final DSLOptions options; |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
50 private final List<TypeMirror> sourceTypes; |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
52 public ImplicitCastNodeFactory(ProcessorContext context, TypeSystemData typeSystem, TypeMirror forType) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 this.context = context; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 this.forType = forType; |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
55 this.typeSystem = typeSystem; |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 this.options = typeSystem.getOptions(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 this.sourceTypes = typeSystem.lookupSourceTypes(forType); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
60 public static String typeName(TypeMirror type) { |
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
61 return "Implicit" + getTypeId(type) + "Cast"; |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
64 public static TypeMirror type(TypeSystemData typeSystem, TypeMirror type) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 String typeSystemName = TypeSystemCodeGenerator.typeName(typeSystem); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 return new GeneratedTypeMirror(ElementUtils.getPackageName(typeSystem.getTemplateType()) + "." + typeSystemName, typeName(type)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
69 public static CodeTree create(TypeSystemData typeSystem, TypeMirror type, CodeTree value) { |
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
70 return CodeTreeBuilder.createBuilder().startStaticCall(type(typeSystem, type), "create").tree(value).end().build(); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 public static CodeTree cast(String nodeName, CodeTree value) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 return CodeTreeBuilder.createBuilder().startCall(nodeName, "cast").tree(value).end().build(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 public static CodeTree check(String nodeName, CodeTree value) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 return CodeTreeBuilder.createBuilder().startCall(nodeName, "check").tree(value).end().build(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
81 private static String seenFieldName(TypeMirror type) { |
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
82 return "seen" + getTypeId(type); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 public CodeTypeElement create() { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 String typeName = typeName(forType); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
87 TypeMirror baseType = context.getType(Object.class); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 CodeTypeElement clazz = GeneratorUtils.createClass(typeSystem, null, modifiers(PUBLIC, FINAL, STATIC), typeName, baseType); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
90 for (TypeMirror sourceType : sourceTypes) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 CodeVariableElement hasSeen = new CodeVariableElement(modifiers(PUBLIC), context.getType(boolean.class), seenFieldName(sourceType)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 hasSeen.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(CompilationFinal.class))); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 clazz.add(hasSeen); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 clazz.add(createConstructor(clazz)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
97 if (isTypeBoxingOptimized(options.monomorphicTypeBoxingOptimization(), forType)) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
98 clazz.add(createIsMonomorphic()); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
99 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
100 clazz.add(createCast(false)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
101 clazz.add(createCast(true)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
102 clazz.add(createCheck()); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 clazz.add(createMerge(clazz)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
104 clazz.add(createCreate(clazz)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
105 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
106 return clazz; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
107 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
108 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
109 private Element createIsMonomorphic() { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
110 String methodName = "isMonomorphic"; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
111 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), context.getType(boolean.class), methodName); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
112 CodeTreeBuilder builder = method.createBuilder(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
113 builder.startReturn(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
114 String operator = ""; |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
115 for (TypeMirror sourceType : sourceTypes) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
116 builder.string(operator); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
117 builder.string(seenFieldName(sourceType)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
118 operator = " ^ "; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
119 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
120 builder.end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
121 return method; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
122 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
123 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
124 private static Element createConstructor(CodeTypeElement clazz) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
125 return new CodeExecutableElement(modifiers(PRIVATE), null, clazz.getSimpleName().toString()); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
126 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
127 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
128 private Element createCreate(CodeTypeElement clazz) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
129 String methodName = "create"; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
130 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), clazz.asType(), methodName); |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
131 method.addParameter(new CodeVariableElement(context.getType(Object.class), "value")); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
132 CodeTreeBuilder builder = method.createBuilder(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
133 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
134 builder.declaration(clazz.asType(), "newCast", builder.create().startNew(clazz.asType()).end()); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
135 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
136 for (TypeMirror sourceType : sourceTypes) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
137 String seenField = seenFieldName(sourceType); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
138 builder.startStatement(); |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
139 builder.string("newCast.").string(seenField).string(" = ").tree(TypeSystemCodeGenerator.check(typeSystem, sourceType, "value")); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
140 builder.end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
141 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
142 builder.startReturn().string("newCast").end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
143 return method; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
144 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
145 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
146 private Element createMerge(CodeTypeElement clazz) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
147 String methodName = "merge"; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
148 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), context.getType(void.class), methodName); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
149 method.addParameter(new CodeVariableElement(clazz.asType(), "otherCast")); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
150 CodeTreeBuilder builder = method.createBuilder(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
151 |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
152 for (TypeMirror sourceType : sourceTypes) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
153 String seenField = seenFieldName(sourceType); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
154 builder.startStatement(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
155 builder.string("this.").string(seenField).string(" |= ").string("otherCast.").string(seenField); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
156 builder.end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
157 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
158 return method; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
159 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
160 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
161 private Element createCheck() { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
162 String methodName = "check"; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
163 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), context.getType(boolean.class), methodName); |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
164 method.addParameter(new CodeVariableElement(context.getType(Object.class), "value")); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
165 CodeTreeBuilder builder = method.createBuilder(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
166 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
167 boolean elseIf = false; |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
168 for (TypeMirror sourceType : sourceTypes) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
169 elseIf = builder.startIf(elseIf); |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
170 builder.string(seenFieldName(sourceType)).string(" && ").tree(TypeSystemCodeGenerator.check(typeSystem, sourceType, "value")); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
171 builder.end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
172 builder.startBlock().returnTrue().end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
173 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
174 builder.returnFalse(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
175 return method; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
176 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
177 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
178 private Element createCast(boolean expect) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
179 String methodName = expect ? "expect" : "cast"; |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
180 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), forType, methodName); |
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
181 method.addParameter(new CodeVariableElement(context.getType(Object.class), "value")); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
182 if (expect) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
183 method.getThrownTypes().add(context.getType(UnexpectedResultException.class)); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
184 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
185 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
186 CodeTreeBuilder builder = method.createBuilder(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
187 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
188 boolean elseIf = false; |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
189 for (TypeMirror sourceType : sourceTypes) { |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
190 elseIf = builder.startIf(elseIf); |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
191 builder.string(seenFieldName(sourceType)).string(" && ").tree(TypeSystemCodeGenerator.check(typeSystem, sourceType, "value")); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
192 builder.end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
193 builder.startBlock(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
194 builder.startReturn(); |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
195 CodeTree castTree = TypeSystemCodeGenerator.cast(typeSystem, sourceType, "value"); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
196 ImplicitCastData cast = typeSystem.lookupCast(sourceType, forType); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
197 if (cast != null) { |
20938
18c0f02fa4d2
Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents:
18761
diff
changeset
|
198 builder.tree(TypeSystemCodeGenerator.invokeImplicitCast(typeSystem, cast, castTree)); |
18761
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
199 } else { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
200 builder.tree(castTree); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
201 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
202 builder.end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
203 builder.end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
204 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
205 if (expect) { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
206 builder.startThrow().startNew(context.getType(UnexpectedResultException.class)).string("value").end().end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
207 } else { |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
208 builder.startStatement().startStaticCall(context.getType(CompilerDirectives.class), "transferToInterpreter").end().end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
209 builder.startThrow().startNew(context.getType(AssertionError.class)).end().end(); |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
210 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
211 return method; |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
212 } |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
213 |
a665483c3881
Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
214 } |