Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/ProcessorContext.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 | 64dcb92ee75a |
comparison
equal
deleted
inserted
replaced
10596:f43eb2f1bbbc | 10597:79041ab43660 |
---|---|
1 /* | |
2 * Copyright (c) 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; | |
24 | |
25 import static com.oracle.truffle.dsl.processor.Utils.*; | |
26 | |
27 import java.util.*; | |
28 | |
29 import javax.annotation.processing.*; | |
30 import javax.lang.model.element.*; | |
31 import javax.lang.model.type.*; | |
32 import javax.lang.model.util.*; | |
33 | |
34 import com.oracle.truffle.dsl.processor.ast.*; | |
35 import com.oracle.truffle.dsl.processor.ast.CodeTypeMirror.*; | |
36 import com.oracle.truffle.dsl.processor.template.*; | |
37 | |
38 /** | |
39 * THIS IS NOT PUBLIC API. | |
40 */ | |
41 public class ProcessorContext { | |
42 | |
43 private final ProcessingEnvironment environment; | |
44 | |
45 private final Map<String, Template> models = new HashMap<>(); | |
46 private final Map<String, Map<String, TypeMirror>> generatedClasses = new HashMap<>(); | |
47 | |
48 private final ProcessCallback callback; | |
49 private final Log log; | |
50 private final TruffleTypes truffleTypes; | |
51 | |
52 public ProcessorContext(ProcessingEnvironment env, ProcessCallback callback) { | |
53 this.environment = env; | |
54 this.callback = callback; | |
55 this.log = new Log(environment); | |
56 this.truffleTypes = new TruffleTypes(this); | |
57 } | |
58 | |
59 public TruffleTypes getTruffleTypes() { | |
60 return truffleTypes; | |
61 } | |
62 | |
63 public Log getLog() { | |
64 return log; | |
65 } | |
66 | |
67 public ProcessingEnvironment getEnvironment() { | |
68 return environment; | |
69 } | |
70 | |
71 public boolean containsTemplate(TypeElement element) { | |
72 return models.containsKey(Utils.getQualifiedName(element)); | |
73 } | |
74 | |
75 public void registerTemplate(TypeElement element, Template model) { | |
76 models.put(Utils.getQualifiedName(element), model); | |
77 } | |
78 | |
79 public void registerType(TypeElement templateType, TypeMirror generatedTypeMirror) { | |
80 String templateQualifiedName = getQualifiedName(templateType); | |
81 Map<String, TypeMirror> simpleNameToType = generatedClasses.get(templateQualifiedName); | |
82 if (simpleNameToType == null) { | |
83 simpleNameToType = new HashMap<>(); | |
84 generatedClasses.put(templateQualifiedName, simpleNameToType); | |
85 } | |
86 String generatedSimpleName = getSimpleName(generatedTypeMirror); | |
87 simpleNameToType.put(generatedSimpleName, generatedTypeMirror); | |
88 } | |
89 | |
90 public Template getTemplate(TypeMirror templateTypeMirror, boolean invokeCallback) { | |
91 String qualifiedName = Utils.getQualifiedName(templateTypeMirror); | |
92 Template model = models.get(qualifiedName); | |
93 if (model == null && invokeCallback) { | |
94 callback.callback(Utils.fromTypeMirror(templateTypeMirror)); | |
95 model = models.get(qualifiedName); | |
96 } | |
97 return model; | |
98 } | |
99 | |
100 public TypeMirror resolveNotYetCompiledType(TypeMirror mirror, Template templateHint) { | |
101 TypeMirror resolvedType = null; | |
102 if (mirror.getKind() == TypeKind.ARRAY) { | |
103 TypeMirror originalComponentType = ((ArrayType) mirror).getComponentType(); | |
104 TypeMirror resolvedComponent = resolveNotYetCompiledType(originalComponentType, templateHint); | |
105 if (resolvedComponent != originalComponentType) { | |
106 return new ArrayCodeTypeMirror(resolvedComponent); | |
107 } | |
108 } | |
109 | |
110 if (mirror.getKind() == TypeKind.ERROR) { | |
111 Element element = ((ErrorType) mirror).asElement(); | |
112 ElementKind kind = element.getKind(); | |
113 if (kind == ElementKind.CLASS || kind == ElementKind.PARAMETER || kind == ElementKind.ENUM) { | |
114 String simpleName = element.getSimpleName().toString(); | |
115 resolvedType = findGeneratedClassBySimpleName(simpleName, templateHint); | |
116 } | |
117 } else { | |
118 resolvedType = mirror; | |
119 } | |
120 | |
121 return resolvedType; | |
122 } | |
123 | |
124 public TypeMirror findGeneratedClassBySimpleName(String simpleName, Template templateHint) { | |
125 if (templateHint == null) { | |
126 // search all | |
127 for (String qualifiedTemplateName : generatedClasses.keySet()) { | |
128 Map<String, TypeMirror> mirrors = generatedClasses.get(qualifiedTemplateName); | |
129 if (mirrors.get(simpleName) != null) { | |
130 return mirrors.get(simpleName); | |
131 } | |
132 } | |
133 return null; | |
134 } else { | |
135 String templateQualifiedName = getQualifiedName(templateHint.getTemplateType()); | |
136 Map<String, TypeMirror> simpleNameToType = generatedClasses.get(templateQualifiedName); | |
137 if (simpleNameToType == null) { | |
138 return null; | |
139 } | |
140 return simpleNameToType.get(simpleName); | |
141 } | |
142 } | |
143 | |
144 public TypeMirror getType(String className) { | |
145 TypeElement element = environment.getElementUtils().getTypeElement(className); | |
146 if (element != null) { | |
147 return element.asType(); | |
148 } | |
149 return null; | |
150 } | |
151 | |
152 public TypeMirror getType(Class<?> element) { | |
153 TypeMirror mirror; | |
154 if (element.isPrimitive()) { | |
155 if (element == boolean.class) { | |
156 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.BOOLEAN); | |
157 } else if (element == byte.class) { | |
158 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.BYTE); | |
159 } else if (element == short.class) { | |
160 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.SHORT); | |
161 } else if (element == char.class) { | |
162 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.CHAR); | |
163 } else if (element == int.class) { | |
164 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.INT); | |
165 } else if (element == long.class) { | |
166 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.LONG); | |
167 } else if (element == float.class) { | |
168 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.FLOAT); | |
169 } else if (element == double.class) { | |
170 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.DOUBLE); | |
171 } else if (element == void.class) { | |
172 mirror = environment.getTypeUtils().getNoType(TypeKind.VOID); | |
173 } else { | |
174 assert false; | |
175 return null; | |
176 } | |
177 } else { | |
178 TypeElement typeElement = environment.getElementUtils().getTypeElement(element.getCanonicalName()); | |
179 mirror = typeElement.asType(); | |
180 } | |
181 return mirror; | |
182 } | |
183 | |
184 public interface ProcessCallback { | |
185 | |
186 void callback(TypeElement template); | |
187 | |
188 } | |
189 | |
190 public TypeMirror reloadTypeElement(TypeElement type) { | |
191 return getType(type.getQualifiedName().toString()); | |
192 } | |
193 | |
194 public TypeMirror reloadType(TypeMirror type) { | |
195 if (type instanceof CodeTypeMirror) { | |
196 return type; | |
197 } else if (type.getKind().isPrimitive()) { | |
198 return type; | |
199 } | |
200 Types types = getEnvironment().getTypeUtils(); | |
201 | |
202 switch (type.getKind()) { | |
203 case ARRAY: | |
204 return types.getArrayType(reloadType(((ArrayType) type).getComponentType())); | |
205 case WILDCARD: | |
206 return types.getWildcardType(((WildcardType) type).getExtendsBound(), ((WildcardType) type).getSuperBound()); | |
207 case DECLARED: | |
208 return reloadTypeElement((TypeElement) (((DeclaredType) type).asElement())); | |
209 } | |
210 return type; | |
211 } | |
212 } |