Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/typesystem/TypeSystemCodeGenerator.java @ 10597:79041ab43660
Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 01 Jul 2013 20:58:32 +0200 |
parents | |
children | b7f90ff38d4b |
comparison
equal
deleted
inserted
replaced
10596:f43eb2f1bbbc | 10597:79041ab43660 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.dsl.processor.typesystem; | |
24 | |
25 import static com.oracle.truffle.dsl.processor.Utils.*; | |
26 import static javax.lang.model.element.Modifier.*; | |
27 | |
28 import java.util.*; | |
29 | |
30 import javax.lang.model.element.*; | |
31 import javax.lang.model.type.*; | |
32 | |
33 import com.oracle.truffle.dsl.processor.*; | |
34 import com.oracle.truffle.dsl.processor.ast.*; | |
35 import com.oracle.truffle.dsl.processor.template.*; | |
36 | |
37 public class TypeSystemCodeGenerator extends CompilationUnitFactory<TypeSystemData> { | |
38 | |
39 public TypeSystemCodeGenerator(ProcessorContext context) { | |
40 super(context); | |
41 } | |
42 | |
43 public static String isTypeMethodName(TypeData type) { | |
44 return "is" + Utils.getTypeId(type.getBoxedType()); | |
45 } | |
46 | |
47 public static String asTypeMethodName(TypeData type) { | |
48 return "as" + Utils.getTypeId(type.getBoxedType()); | |
49 } | |
50 | |
51 public static String expectTypeMethodName(TypeData type) { | |
52 return "expect" + Utils.getTypeId(type.getBoxedType()); | |
53 } | |
54 | |
55 /** | |
56 * Finds the generated singleton field for a TypeSytemData instance. TypeSystemCodeGenerator | |
57 * must be applied to the TypeSystemData model before use. | |
58 */ | |
59 public static VariableElement findSingleton(ProcessorContext context, TypeSystemData typeSystem) { | |
60 TypeMirror type = context.findGeneratedClassBySimpleName(TypeClassFactory.typeName(typeSystem), typeSystem); | |
61 return Utils.findDeclaredField(type, TypeClassFactory.singletonName(typeSystem.getTemplateType().asType())); | |
62 } | |
63 | |
64 @Override | |
65 protected void createChildren(TypeSystemData m) { | |
66 add(new TypeClassFactory(context), m); | |
67 } | |
68 | |
69 protected static class TypeClassFactory extends ClassElementFactory<TypeSystemData> { | |
70 | |
71 private static final String LOCAL_VALUE = "value"; | |
72 | |
73 public TypeClassFactory(ProcessorContext context) { | |
74 super(context); | |
75 } | |
76 | |
77 @Override | |
78 public CodeTypeElement create(TypeSystemData typeSystem) { | |
79 String name = typeName(typeSystem); | |
80 CodeTypeElement clazz = createClass(typeSystem, modifiers(PUBLIC), name, typeSystem.getTemplateType().asType(), false); | |
81 | |
82 clazz.add(createConstructorUsingFields(modifiers(PROTECTED), clazz)); | |
83 CodeVariableElement singleton = createSingleton(clazz); | |
84 clazz.add(singleton); | |
85 | |
86 for (TypeData type : typeSystem.getTypes()) { | |
87 if (!type.isGeneric()) { | |
88 CodeExecutableElement isType = createIsTypeMethod(type); | |
89 if (isType != null) { | |
90 clazz.add(isType); | |
91 } | |
92 CodeExecutableElement asType = createAsTypeMethod(type); | |
93 if (asType != null) { | |
94 clazz.add(asType); | |
95 } | |
96 | |
97 for (TypeData sourceType : collectExpectSourceTypes(type)) { | |
98 CodeExecutableElement expect = createExpectTypeMethod(type, sourceType); | |
99 if (expect != null) { | |
100 clazz.add(expect); | |
101 } | |
102 } | |
103 } | |
104 } | |
105 | |
106 return clazz; | |
107 } | |
108 | |
109 private static List<TypeData> collectExpectSourceTypes(TypeData type) { | |
110 Set<TypeData> sourceTypes = new HashSet<>(); | |
111 sourceTypes.add(type.getTypeSystem().getGenericTypeData()); | |
112 for (TypeCastData cast : type.getTypeCasts()) { | |
113 sourceTypes.add(cast.getSourceType()); | |
114 } | |
115 for (TypeCheckData cast : type.getTypeChecks()) { | |
116 sourceTypes.add(cast.getCheckedType()); | |
117 } | |
118 return new ArrayList<>(sourceTypes); | |
119 } | |
120 | |
121 private static String typeName(TypeSystemData typeSystem) { | |
122 String name = getSimpleName(typeSystem.getTemplateType()); | |
123 return name + "Gen"; | |
124 } | |
125 | |
126 private static String singletonName(TypeMirror type) { | |
127 return createConstantName(getSimpleName(type)); | |
128 } | |
129 | |
130 private CodeVariableElement createSingleton(CodeTypeElement clazz) { | |
131 CodeVariableElement field = new CodeVariableElement(modifiers(PUBLIC, STATIC, FINAL), clazz.asType(), singletonName(getModel().getTemplateType().asType())); | |
132 field.createInitBuilder().startNew(clazz.asType()).end(); | |
133 return field; | |
134 } | |
135 | |
136 private CodeExecutableElement createIsTypeMethod(TypeData type) { | |
137 if (!type.getTypeChecks().isEmpty()) { | |
138 return null; | |
139 } | |
140 | |
141 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), getContext().getType(boolean.class), TypeSystemCodeGenerator.isTypeMethodName(type)); | |
142 method.addParameter(new CodeVariableElement(getContext().getType(Object.class), LOCAL_VALUE)); | |
143 | |
144 CodeTreeBuilder body = method.createBuilder(); | |
145 body.startReturn().instanceOf(LOCAL_VALUE, type.getBoxedType()).end(); | |
146 | |
147 return method; | |
148 } | |
149 | |
150 private CodeExecutableElement createAsTypeMethod(TypeData type) { | |
151 if (!type.getTypeCasts().isEmpty()) { | |
152 return null; | |
153 } | |
154 | |
155 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), type.getPrimitiveType(), TypeSystemCodeGenerator.asTypeMethodName(type)); | |
156 method.addParameter(new CodeVariableElement(getContext().getType(Object.class), LOCAL_VALUE)); | |
157 | |
158 CodeTreeBuilder body = method.createBuilder(); | |
159 body.startAssert().startCall(isTypeMethodName(type)).string(LOCAL_VALUE).end().end(); | |
160 body.startReturn().cast(type.getPrimitiveType(), body.create().string(LOCAL_VALUE).getTree()).end(); | |
161 | |
162 return method; | |
163 } | |
164 | |
165 private CodeExecutableElement createExpectTypeMethod(TypeData expectedType, TypeData sourceType) { | |
166 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), expectedType.getPrimitiveType(), TypeSystemCodeGenerator.expectTypeMethodName(expectedType)); | |
167 method.addParameter(new CodeVariableElement(sourceType.getPrimitiveType(), LOCAL_VALUE)); | |
168 method.addThrownType(getContext().getTruffleTypes().getUnexpectedValueException()); | |
169 | |
170 CodeTreeBuilder body = method.createBuilder(); | |
171 body.startIf().startCall(null, TypeSystemCodeGenerator.isTypeMethodName(expectedType)).string(LOCAL_VALUE).end().end().startBlock(); | |
172 body.startReturn().startCall(null, TypeSystemCodeGenerator.asTypeMethodName(expectedType)).string(LOCAL_VALUE).end().end(); | |
173 body.end(); // if-block | |
174 body.startThrow().startNew(getContext().getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end(); | |
175 | |
176 return method; | |
177 } | |
178 | |
179 } | |
180 } |