annotate truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/TypeSystemCodeGenerator.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
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1 /*
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
4 *
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
8 *
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
13 * accompanied this code).
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
14 *
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
18 *
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
21 * questions.
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
22 */
16759
23415229349b Truffle-DSL: new package structure.
Christian Humer <christian.humer@gmail.com>
parents: 16755
diff changeset
23 package com.oracle.truffle.dsl.processor.generator;
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
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.api.nodes.UnexpectedResultException;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
26 import com.oracle.truffle.dsl.processor.ProcessorContext;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
27 import static com.oracle.truffle.dsl.processor.generator.GeneratorUtils.createTransferToInterpreterAndInvalidate;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
28 import com.oracle.truffle.dsl.processor.java.ElementUtils;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
29 import static com.oracle.truffle.dsl.processor.java.ElementUtils.createConstantName;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
30 import static com.oracle.truffle.dsl.processor.java.ElementUtils.getSimpleName;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
31 import static com.oracle.truffle.dsl.processor.java.ElementUtils.modifiers;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
32 import com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
33 import com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
34 import com.oracle.truffle.dsl.processor.java.model.CodeTree;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
35 import com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
36 import com.oracle.truffle.dsl.processor.java.model.CodeTypeElement;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
37 import com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
38 import com.oracle.truffle.dsl.processor.java.model.CodeVariableElement;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
39 import com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
40 import com.oracle.truffle.dsl.processor.model.ImplicitCastData;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
41 import com.oracle.truffle.dsl.processor.model.TypeCastData;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
42 import com.oracle.truffle.dsl.processor.model.TypeCheckData;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
43 import com.oracle.truffle.dsl.processor.model.TypeSystemData;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
44 import java.util.List;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
45 import javax.lang.model.element.Modifier;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
46 import static javax.lang.model.element.Modifier.FINAL;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
47 import static javax.lang.model.element.Modifier.PROTECTED;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
48 import static javax.lang.model.element.Modifier.PUBLIC;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
49 import static javax.lang.model.element.Modifier.STATIC;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
50 import javax.lang.model.type.DeclaredType;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
51 import javax.lang.model.type.TypeMirror;
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
53 public class TypeSystemCodeGenerator extends CodeTypeElementFactory<TypeSystemData> {
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
54
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
55 private static final String LOCAL_VALUE = "value";
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
56
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
57 public static CodeTree cast(TypeSystemData typeSystem, TypeMirror type, String content) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
58 return cast(typeSystem, type, CodeTreeBuilder.singleString(content));
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
59 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
60
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
61 public static CodeTree implicitType(TypeSystemData typeSystem, TypeMirror type, CodeTree value) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
62 if (ElementUtils.isObject(type)) {
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
63 return value;
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
64 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
65 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
66 builder.startStaticCall(createTypeSystemGen(typeSystem), getImplicitClass(typeSystem, type)).tree(value);
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
67 builder.end();
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
68 return builder.build();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
69 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
70
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
71 public static CodeTree invokeImplicitCast(TypeSystemData typeSystem, ImplicitCastData cast, CodeTree expression) {
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
72 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
73 builder.startStaticCall(createTypeSystemGen(typeSystem), cast.getMethodName()).tree(expression);
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
74 builder.end();
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
75 return builder.build();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
76 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
77
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
78 public static CodeTree implicitCheck(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
79 return callImplictMethod(typeSystem, type, isImplicitTypeMethodName(typeSystem, type), value, typeHint);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
80 }
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
81
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
82 public static CodeTree implicitExpect(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
83 return callImplictMethod(typeSystem, type, expectImplicitTypeMethodName(typeSystem, type), value, typeHint);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
84 }
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
85
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
86 public static CodeTree implicitCast(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
87 return callImplictMethod(typeSystem, type, asImplicitTypeMethodName(typeSystem, type), value, typeHint);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
88 }
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
89
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
90 private static CodeTree callImplictMethod(TypeSystemData typeSystem, TypeMirror type, String methodName, CodeTree value, String typeHint) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
91 if (ElementUtils.isObject(type)) {
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
92 return value;
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
93 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
94 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
95 builder.startStaticCall(createTypeSystemGen(typeSystem), methodName).tree(value);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
96 if (typeHint != null) {
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
97 builder.string(typeHint);
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
98 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
99 builder.end();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
100 return builder.build();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
101 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
102
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
103 public static CodeTree cast(TypeSystemData typeSystem, TypeMirror type, CodeTree content) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
104 if (ElementUtils.isObject(type)) {
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
105 return content;
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
106 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
107 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
108
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
109 TypeCastData cast = typeSystem.getCast(type);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
110 if (cast == null) {
20949
9a83732f97eb Truffle-DSL: ensure proper wildcards for generics are filled in; remove unused type system node code generator.
Christian Humer <christian.humer@gmail.com>
parents: 20938
diff changeset
111 builder.cast(ElementUtils.fillInGenericWildcards(type), content);
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
112 } else {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
113 builder.startStaticCall(typeSystem.getTemplateType().asType(), cast.getMethodName()).tree(content).end();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
114 }
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
115 return builder.build();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
116 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
117
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
118 public static CodeTree expect(TypeSystemData typeSystem, TypeMirror type, CodeTree content) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
119 if (ElementUtils.isObject(type) || ElementUtils.isVoid(type)) {
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
120 return content;
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
121 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
122 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
123 if (typeSystem.hasType(type)) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
124 builder.startStaticCall(createTypeSystemGen(typeSystem), expectTypeMethodName(typeSystem, type)).tree(content).end();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
125 } else {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
126 builder.startCall(expectTypeMethodName(typeSystem, type)).tree(content).end();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
127 }
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
128
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
129 return builder.build();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
130 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
131
20949
9a83732f97eb Truffle-DSL: ensure proper wildcards for generics are filled in; remove unused type system node code generator.
Christian Humer <christian.humer@gmail.com>
parents: 20938
diff changeset
132 public static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceTypeOriginal, TypeMirror expectedTypeOriginal) {
9a83732f97eb Truffle-DSL: ensure proper wildcards for generics are filled in; remove unused type system node code generator.
Christian Humer <christian.humer@gmail.com>
parents: 20938
diff changeset
133 TypeMirror expectedType = ElementUtils.fillInGenericWildcards(expectedTypeOriginal);
9a83732f97eb Truffle-DSL: ensure proper wildcards for generics are filled in; remove unused type system node code generator.
Christian Humer <christian.humer@gmail.com>
parents: 20938
diff changeset
134 TypeMirror sourceType = ElementUtils.fillInGenericWildcards(sourceTypeOriginal);
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
135 if (ElementUtils.isObject(expectedType) || ElementUtils.isVoid(expectedType)) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
136 return null;
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
137 }
20949
9a83732f97eb Truffle-DSL: ensure proper wildcards for generics are filled in; remove unused type system node code generator.
Christian Humer <christian.humer@gmail.com>
parents: 20938
diff changeset
138
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
139 CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), expectedType, TypeSystemCodeGenerator.expectTypeMethodName(typeSystem, expectedType));
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
140 method.setVisibility(visibility);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
141 method.addParameter(new CodeVariableElement(sourceType, LOCAL_VALUE));
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
142 method.addThrownType(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException());
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
143
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
144 CodeTreeBuilder body = method.createBuilder();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
145 body.startIf().tree(check(typeSystem, expectedType, LOCAL_VALUE)).end().startBlock();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
146 body.startReturn().tree(cast(typeSystem, expectedType, LOCAL_VALUE)).end();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
147 body.end();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
148 body.startThrow().startNew(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
149 return method;
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
150 }
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
151
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
152 public static CodeTree expect(TypeSystemData typeSystem, TypeMirror sourceType, TypeMirror targetType, CodeTree content) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
153 if (sourceType != null && !ElementUtils.needsCastTo(sourceType, targetType)) {
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
154 return content;
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
155 } else {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
156 return expect(typeSystem, targetType, content);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
157 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
158 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
159
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
160 public static CodeTypeMirror createTypeSystemGen(TypeSystemData typeSystem) {
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
161 return new GeneratedTypeMirror(ElementUtils.getPackageName(typeSystem.getTemplateType()), typeName(typeSystem));
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
162 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
163
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
164 public static CodeTree check(TypeSystemData typeSystem, TypeMirror type, String content) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
165 return check(typeSystem, type, CodeTreeBuilder.singleString(content));
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
166 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
167
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
168 public static CodeTree check(TypeSystemData typeSystem, TypeMirror type, CodeTree content) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
169 if (ElementUtils.isObject(type)) {
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
170 return content;
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
171 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
172 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
173
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
174 TypeCheckData check = typeSystem.getCheck(type);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
175 if (check == null) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
176 builder.instanceOf(content, ElementUtils.boxType(typeSystem.getContext(), type));
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
177 } else {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
178 builder.startStaticCall(typeSystem.getTemplateType().asType(), check.getMethodName()).tree(content).end();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
179 }
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
180 return builder.build();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
181 }
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
182
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
183 public static String isTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
184 return "is" + getTypeId(typeSystem, type);
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
185 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
186
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
187 private static String getTypeId(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
188 return ElementUtils.getTypeId(typeSystem.boxType(type));
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
189 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
190
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
191 static String isImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
192 return "isImplicit" + getTypeId(typeSystem, type);
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
193 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
194
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
195 public static String asTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
196 return "as" + getTypeId(typeSystem, type);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
197 }
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
198
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
199 static String asImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
200 return "asImplicit" + getTypeId(typeSystem, type);
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
201 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
202
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
203 static String expectImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
204 return "expectImplicit" + getTypeId(typeSystem, type);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
205 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
206
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
207 static String getImplicitClass(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
208 return "getImplicit" + getTypeId(typeSystem, type) + "Class";
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
209 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
210
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
211 public static String expectTypeMethodName(TypeSystemData typeSystem, TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
212 return "expect" + getTypeId(typeSystem, type);
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
213 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
214
18752
1acaa69ff61b Truffle-DSL: refactor generator classes
Christian Humer <christian.humer@gmail.com>
parents: 16817
diff changeset
215 static String typeName(TypeSystemData typeSystem) {
16755
bd28da642eea Truffle-DSL: Several new features implemented:
Christian Humer <christian.humer@gmail.com>
parents: 13275
diff changeset
216 String name = getSimpleName(typeSystem.getTemplateType());
bd28da642eea Truffle-DSL: Several new features implemented:
Christian Humer <christian.humer@gmail.com>
parents: 13275
diff changeset
217 return name + "Gen";
bd28da642eea Truffle-DSL: Several new features implemented:
Christian Humer <christian.humer@gmail.com>
parents: 13275
diff changeset
218 }
bd28da642eea Truffle-DSL: Several new features implemented:
Christian Humer <christian.humer@gmail.com>
parents: 13275
diff changeset
219
18752
1acaa69ff61b Truffle-DSL: refactor generator classes
Christian Humer <christian.humer@gmail.com>
parents: 16817
diff changeset
220 static String singletonName(TypeSystemData type) {
16755
bd28da642eea Truffle-DSL: Several new features implemented:
Christian Humer <christian.humer@gmail.com>
parents: 13275
diff changeset
221 return createConstantName(getSimpleName(type.getTemplateType().asType()));
bd28da642eea Truffle-DSL: Several new features implemented:
Christian Humer <christian.humer@gmail.com>
parents: 13275
diff changeset
222 }
bd28da642eea Truffle-DSL: Several new features implemented:
Christian Humer <christian.humer@gmail.com>
parents: 13275
diff changeset
223
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
224 @Override
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
225 public CodeTypeElement create(ProcessorContext context, TypeSystemData typeSystem) {
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
226 CodeTypeElement clazz = new TypeClassFactory(context, typeSystem).create();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
227
19282
ae81dd154fb6 Truffle-DSL: remove old DSL layout; Make new layout the default.
Christian Humer <christian.humer@gmail.com>
parents: 18776
diff changeset
228 if (typeSystem.getOptions().implicitCastOptimization().isMergeCasts()) {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
229 for (TypeMirror type : typeSystem.lookupTargetTypes()) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
230 clazz.add(new ImplicitCastNodeFactory(context, typeSystem, type).create());
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
231 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
232 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
233 return clazz;
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
234 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
235
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
236 private static class TypeClassFactory {
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
237
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
238 private final ProcessorContext context;
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
239 private final TypeSystemData typeSystem;
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
240
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
241 public TypeClassFactory(ProcessorContext context, TypeSystemData typeSystem) {
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
242 this.context = context;
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
243 this.typeSystem = typeSystem;
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
244 }
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
245
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
246 public CodeTypeElement create() {
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
247 String name = typeName(typeSystem);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
248 CodeTypeElement clazz = GeneratorUtils.createClass(typeSystem, null, modifiers(PUBLIC, FINAL), name, typeSystem.getTemplateType().asType());
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
249
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
250 clazz.add(GeneratorUtils.createConstructorUsingFields(modifiers(PROTECTED), clazz));
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
251 CodeVariableElement singleton = createSingleton(clazz);
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
252 clazz.add(singleton);
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
253
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
254 for (TypeMirror type : typeSystem.getLegacyTypes()) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
255 if (ElementUtils.isVoid(type) || ElementUtils.isObject(type)) {
16817
0370880ac9ce Truffle-DSL: better caching for type checks.
Christian Humer <christian.humer@gmail.com>
parents: 16759
diff changeset
256 continue;
0370880ac9ce Truffle-DSL: better caching for type checks.
Christian Humer <christian.humer@gmail.com>
parents: 16759
diff changeset
257 }
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
258
16817
0370880ac9ce Truffle-DSL: better caching for type checks.
Christian Humer <christian.humer@gmail.com>
parents: 16759
diff changeset
259 clazz.addOptional(createIsTypeMethod(type));
0370880ac9ce Truffle-DSL: better caching for type checks.
Christian Humer <christian.humer@gmail.com>
parents: 16759
diff changeset
260 clazz.addOptional(createAsTypeMethod(type));
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
261 clazz.addOptional(createExpectTypeMethod(type, context.getType(Object.class)));
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
262
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
263 }
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
264
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
265 List<TypeMirror> lookupTargetTypes = typeSystem.lookupTargetTypes();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
266 for (TypeMirror type : lookupTargetTypes) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
267 clazz.add(createAsImplicitTypeMethod(type, false));
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
268 if (typeSystem.getOptions().implicitCastOptimization().isNone()) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
269 clazz.add(createExpectImplicitTypeMethod(type, false));
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
270 }
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
271 clazz.add(createIsImplicitTypeMethod(type, false));
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
272
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
273 if (typeSystem.getOptions().implicitCastOptimization().isDuplicateTail()) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
274 clazz.add(createAsImplicitTypeMethod(type, true));
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
275 clazz.add(createExpectImplicitTypeMethod(type, true));
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
276 clazz.add(createIsImplicitTypeMethod(type, true));
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
277 clazz.add(createGetImplicitClass(type));
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
278 }
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
279 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
280 return clazz;
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
281 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
282
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
283 private CodeVariableElement createSingleton(CodeTypeElement clazz) {
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
284 CodeVariableElement field = new CodeVariableElement(modifiers(PUBLIC, STATIC, FINAL), clazz.asType(), singletonName(typeSystem));
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
285 field.createInitBuilder().startNew(clazz.asType()).end();
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
286
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
287 CodeAnnotationMirror annotationMirror = new CodeAnnotationMirror((DeclaredType) context.getType(Deprecated.class));
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
288 field.getAnnotationMirrors().add(annotationMirror);
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
289
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
290 return field;
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
291 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
292
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
293 private CodeExecutableElement createIsImplicitTypeMethod(TypeMirror type, boolean typed) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
294 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isImplicitTypeMethodName(typeSystem, type));
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
295 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
296 if (typed) {
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
297 method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint"));
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
298 }
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
299 CodeTreeBuilder builder = method.createBuilder();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
300
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
301 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
302
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
303 builder.startReturn();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
304 String sep = "";
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
305 for (TypeMirror sourceType : sourceTypes) {
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
306 builder.string(sep);
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
307 if (typed) {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
308 builder.string("(typeHint == ").typeLiteral(sourceType).string(" && ");
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
309 }
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
310 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
311 if (typed) {
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
312 builder.string(")");
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
313 }
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
314 if (sourceTypes.lastIndexOf(sourceType) != sourceTypes.size() - 1) {
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
315 builder.newLine();
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
316 }
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
317 if (sep.equals("")) {
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
318 builder.startIndention();
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
319 }
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
320 sep = " || ";
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
321 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
322 builder.end();
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
323 builder.end();
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
324 return method;
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
325 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
326
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
327 private CodeExecutableElement createAsImplicitTypeMethod(TypeMirror type, boolean useTypeHint) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
328 String name = asImplicitTypeMethodName(typeSystem, type);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
329 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, name);
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
330 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
331 if (useTypeHint) {
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
332 method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint"));
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
333 }
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
334
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
335 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
336
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
337 CodeTreeBuilder builder = method.createBuilder();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
338 boolean elseIf = false;
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
339 for (TypeMirror sourceType : sourceTypes) {
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
340 elseIf = builder.startIf(elseIf);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
341 if (useTypeHint) {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
342 builder.string("typeHint == ").typeLiteral(sourceType);
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
343 } else {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
344 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
12395
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
345 }
8e8347ecabbc Truffle-DSL: implemented new polymorphic more compact generation strategy
Christian Humer <christian.humer@gmail.com>
parents: 11545
diff changeset
346
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
347 builder.end().startBlock();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
348
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
349 builder.startReturn();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
350 ImplicitCastData cast = typeSystem.lookupCast(sourceType, type);
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
351 if (cast != null) {
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
352 builder.startCall(cast.getMethodName());
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
353 }
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
354 builder.tree(cast(typeSystem, sourceType, LOCAL_VALUE)).end();
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
355 if (cast != null) {
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
356 builder.end();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
357 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
358 builder.end();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
359 builder.end();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
360 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
361
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
362 builder.startElseBlock();
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
363 builder.tree(createTransferToInterpreterAndInvalidate());
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
364 builder.startThrow().startNew(context.getType(IllegalArgumentException.class)).doubleQuote("Illegal type ").end().end();
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
365 builder.end();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
366 return method;
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
367 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
368
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
369 private CodeExecutableElement createExpectImplicitTypeMethod(TypeMirror type, boolean useTypeHint) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
370 String name = expectImplicitTypeMethodName(typeSystem, type);
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
371 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, name);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
372 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
373 if (useTypeHint) {
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
374 method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint"));
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
375 }
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
376 method.getThrownTypes().add(context.getType(UnexpectedResultException.class));
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
377
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
378 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
379
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
380 CodeTreeBuilder builder = method.createBuilder();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
381 boolean elseIf = false;
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
382 for (TypeMirror sourceType : sourceTypes) {
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
383 elseIf = builder.startIf(elseIf);
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
384 if (useTypeHint) {
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
385 builder.string("typeHint == ").typeLiteral(sourceType);
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
386 builder.string(" && ");
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
387 }
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
388 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
389
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
390 builder.end().startBlock();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
391
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
392 builder.startReturn();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
393 ImplicitCastData cast = typeSystem.lookupCast(sourceType, type);
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
394 if (cast != null) {
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
395 builder.startCall(cast.getMethodName());
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
396 }
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
397 builder.tree(cast(typeSystem, sourceType, LOCAL_VALUE)).end();
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
398 if (cast != null) {
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
399 builder.end();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
400 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
401 builder.end();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
402 builder.end();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
403 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
404
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
405 builder.startElseBlock();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
406 builder.startThrow().startNew(context.getType(UnexpectedResultException.class)).string(LOCAL_VALUE).end().end();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
407 builder.end();
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
408 return method;
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
409 }
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
410
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
411 private CodeExecutableElement createGetImplicitClass(TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
412 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(Class.class), TypeSystemCodeGenerator.getImplicitClass(typeSystem, type));
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
413 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
414
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
415 List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type);
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
416 CodeTreeBuilder builder = method.createBuilder();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
417 boolean elseIf = false;
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
418 for (TypeMirror sourceType : sourceTypes) {
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
419 elseIf = builder.startIf(elseIf);
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
420 builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)).end();
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
421 builder.end().startBlock();
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
422 builder.startReturn().typeLiteral(sourceType).end();
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
423 builder.end();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
424 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
425
21742
2e850dbf82ae Truffle-DSL: fix NPE in getImplicitCastType if an implicitely casted type is short circuited. (reported-by: Mohaned Qunaibit)
Christian Humer <christian.humer@gmail.com>
parents: 20949
diff changeset
426 builder.startElseIf().string(LOCAL_VALUE).string(" == ").nullLiteral().end();
2e850dbf82ae Truffle-DSL: fix NPE in getImplicitCastType if an implicitely casted type is short circuited. (reported-by: Mohaned Qunaibit)
Christian Humer <christian.humer@gmail.com>
parents: 20949
diff changeset
427 builder.startBlock();
2e850dbf82ae Truffle-DSL: fix NPE in getImplicitCastType if an implicitely casted type is short circuited. (reported-by: Mohaned Qunaibit)
Christian Humer <christian.humer@gmail.com>
parents: 20949
diff changeset
428 builder.startReturn().typeLiteral(context.getType(Object.class)).end();
2e850dbf82ae Truffle-DSL: fix NPE in getImplicitCastType if an implicitely casted type is short circuited. (reported-by: Mohaned Qunaibit)
Christian Humer <christian.humer@gmail.com>
parents: 20949
diff changeset
429 builder.end();
2e850dbf82ae Truffle-DSL: fix NPE in getImplicitCastType if an implicitely casted type is short circuited. (reported-by: Mohaned Qunaibit)
Christian Humer <christian.humer@gmail.com>
parents: 20949
diff changeset
430
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
431 builder.startElseBlock();
18761
a665483c3881 Truffle-DSL: new node layout implementation.
Christian Humer <christian.humer@gmail.com>
parents: 18758
diff changeset
432 builder.tree(createTransferToInterpreterAndInvalidate());
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
433 builder.startThrow().startNew(context.getType(IllegalArgumentException.class)).doubleQuote("Illegal type ").end().end();
11545
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
434 builder.end();
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
435
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
436 return method;
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
437 }
2fb276f5e3e9 Truffle-DSL: implemented implicit casts.
Christian Humer <christian.humer@gmail.com>
parents: 11184
diff changeset
438
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
439 private CodeExecutableElement createIsTypeMethod(TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
440 if (typeSystem.getCheck(type) != null) {
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
441 return null;
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
442 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
443
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
444 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isTypeMethodName(typeSystem, type));
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
445 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
446
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
447 CodeTreeBuilder body = method.createBuilder();
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
448 body.startReturn().tree(check(typeSystem, type, LOCAL_VALUE)).end();
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
449
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
450 return method;
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
451 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
452
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
453 private CodeExecutableElement createAsTypeMethod(TypeMirror type) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
454 if (typeSystem.getCast(type) != null) {
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
455 return null;
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
456 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
457
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
458 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, TypeSystemCodeGenerator.asTypeMethodName(typeSystem, type));
18753
f6b8787dc113 Truffle-DSL: replace complex factory system with a much simpler version
Christian Humer <christian.humer@gmail.com>
parents: 18752
diff changeset
459 method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE));
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
460
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
461 CodeTreeBuilder body = method.createBuilder();
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
462 String assertMessage = typeName(typeSystem) + "." + asTypeMethodName(typeSystem, type) + ": " + ElementUtils.getSimpleName(type) + " expected";
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
463 body.startAssert().tree(check(typeSystem, type, LOCAL_VALUE)).string(" : ").doubleQuote(assertMessage).end();
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
464 body.startReturn().tree(cast(typeSystem, type, LOCAL_VALUE)).end();
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
465
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
466 return method;
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
467 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
468
20938
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
469 private CodeExecutableElement createExpectTypeMethod(TypeMirror expectedType, TypeMirror sourceType) {
18c0f02fa4d2 Truffle-DSL: make type systems optional.
Christian Humer <christian.humer@gmail.com>
parents: 19282
diff changeset
470 return createExpectMethod(Modifier.PUBLIC, typeSystem, sourceType, expectedType);
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
471 }
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
472 }
18758
3912400fc33a Truffle-DSL: remove type system singleton
Christian Humer <christian.humer@gmail.com>
parents: 18753
diff changeset
473
10597
79041ab43660 Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
474 }