Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/ProcessorContext.java @ 7291:a748e4d44694
Truffle API to specify type-specalized Node classes; annotation processor for automatic code generation of the type-specialized Node classes during the build process
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Fri, 21 Dec 2012 10:44:31 -0800 |
parents | |
children | 57edf6b07d36 |
comparison
equal
deleted
inserted
replaced
7290:a81db08fe930 | 7291:a748e4d44694 |
---|---|
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.codegen.processor; | |
24 | |
25 import static com.oracle.truffle.codegen.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 | |
33 import com.oracle.truffle.codegen.processor.ast.CodeTypeMirror.ArrayCodeTypeMirror; | |
34 import com.oracle.truffle.codegen.processor.template.*; | |
35 | |
36 /** | |
37 * THIS IS NOT PUBLIC API. | |
38 */ | |
39 public class ProcessorContext { | |
40 | |
41 private final ProcessingEnvironment environment; | |
42 | |
43 private final Map<String, Template> models = new HashMap<>(); | |
44 private final Map<String, Map<String, TypeMirror>> generatedClasses = new HashMap<>(); | |
45 | |
46 private final ProcessCallback callback; | |
47 private final Log log; | |
48 private final TruffleTypes truffleTypes; | |
49 | |
50 public ProcessorContext(ProcessingEnvironment env, ProcessCallback callback) { | |
51 this.environment = env; | |
52 this.callback = callback; | |
53 this.log = new Log(environment); | |
54 this.truffleTypes = new TruffleTypes(this); | |
55 } | |
56 | |
57 public TruffleTypes getTruffleTypes() { | |
58 return truffleTypes; | |
59 } | |
60 | |
61 public Log getLog() { | |
62 return log; | |
63 } | |
64 | |
65 public ProcessingEnvironment getEnvironment() { | |
66 return environment; | |
67 } | |
68 | |
69 public boolean containsTemplate(TypeElement element) { | |
70 return models.containsKey(Utils.getQualifiedName(element)); | |
71 } | |
72 | |
73 public void registerTemplate(TypeElement element, Template model) { | |
74 models.put(Utils.getQualifiedName(element), model); | |
75 } | |
76 | |
77 public void registerType(TypeElement templateType, TypeMirror generatedTypeMirror) { | |
78 String templateQualifiedName = getQualifiedName(templateType); | |
79 Map<String, TypeMirror> simpleNameToType = generatedClasses.get(templateQualifiedName); | |
80 if (simpleNameToType == null) { | |
81 simpleNameToType = new HashMap<>(); | |
82 generatedClasses.put(templateQualifiedName, simpleNameToType); | |
83 } | |
84 String generatedSimpleName = getSimpleName(generatedTypeMirror); | |
85 simpleNameToType.put(generatedSimpleName, generatedTypeMirror); | |
86 } | |
87 | |
88 public Template getTemplate(TypeMirror templateTypeMirror, boolean invokeCallback) { | |
89 String qualifiedName = Utils.getQualifiedName(templateTypeMirror); | |
90 Template model = models.get(qualifiedName); | |
91 if (model == null && invokeCallback) { | |
92 callback.callback(Utils.fromTypeMirror(templateTypeMirror)); | |
93 model = models.get(qualifiedName); | |
94 } | |
95 return model; | |
96 } | |
97 | |
98 public TypeMirror resolveNotYetCompiledType(TypeMirror mirror, Template templateHint) { | |
99 TypeMirror resolvedType = null; | |
100 if (mirror.getKind() == TypeKind.ARRAY) { | |
101 TypeMirror originalComponentType = ((ArrayType) mirror).getComponentType(); | |
102 TypeMirror resolvedComponent = resolveNotYetCompiledType(originalComponentType, templateHint); | |
103 if (resolvedComponent != originalComponentType) { | |
104 return new ArrayCodeTypeMirror(resolvedComponent); | |
105 } | |
106 } | |
107 | |
108 if (mirror.getKind() == TypeKind.ERROR) { | |
109 Element element = ((ErrorType) mirror).asElement(); | |
110 ElementKind kind = element.getKind(); | |
111 if (kind == ElementKind.CLASS || kind == ElementKind.PARAMETER || kind == ElementKind.ENUM) { | |
112 String simpleName = element.getSimpleName().toString(); | |
113 resolvedType = findGeneratedClassBySimpleName(simpleName, templateHint); | |
114 } | |
115 } else { | |
116 resolvedType = mirror; | |
117 } | |
118 | |
119 return resolvedType; | |
120 } | |
121 | |
122 public TypeMirror findGeneratedClassBySimpleName(String simpleName, Template templateHint) { | |
123 if (templateHint == null) { | |
124 // search all | |
125 for (String qualifiedTemplateName : generatedClasses.keySet()) { | |
126 Map<String, TypeMirror> mirrors = generatedClasses.get(qualifiedTemplateName); | |
127 if (mirrors.get(simpleName) != null) { | |
128 return mirrors.get(simpleName); | |
129 } | |
130 } | |
131 return null; | |
132 } else { | |
133 String templateQualifiedName = getQualifiedName(templateHint.getTemplateType()); | |
134 Map<String, TypeMirror> simpleNameToType = generatedClasses.get(templateQualifiedName); | |
135 if (simpleNameToType == null) { | |
136 return null; | |
137 } | |
138 return simpleNameToType.get(simpleName); | |
139 } | |
140 } | |
141 | |
142 public TypeMirror getType(String className) { | |
143 TypeElement element = environment.getElementUtils().getTypeElement(className); | |
144 if (element != null) { | |
145 return element.asType(); | |
146 } | |
147 return null; | |
148 } | |
149 | |
150 public TypeMirror getType(Class< ? > element) { | |
151 TypeMirror mirror; | |
152 if (element.isPrimitive()) { | |
153 if (element == boolean.class) { | |
154 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.BOOLEAN); | |
155 } else if (element == byte.class) { | |
156 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.BYTE); | |
157 } else if (element == short.class) { | |
158 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.SHORT); | |
159 } else if (element == char.class) { | |
160 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.CHAR); | |
161 } else if (element == int.class) { | |
162 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.INT); | |
163 } else if (element == long.class) { | |
164 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.LONG); | |
165 } else if (element == float.class) { | |
166 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.FLOAT); | |
167 } else if (element == double.class) { | |
168 mirror = environment.getTypeUtils().getPrimitiveType(TypeKind.DOUBLE); | |
169 } else if (element == void.class) { | |
170 mirror = environment.getTypeUtils().getNoType(TypeKind.VOID); | |
171 } else { | |
172 assert false; | |
173 return null; | |
174 } | |
175 } else { | |
176 TypeElement typeElement = environment.getElementUtils().getTypeElement(element.getCanonicalName()); | |
177 mirror = typeElement.asType(); | |
178 } | |
179 return mirror; | |
180 } | |
181 | |
182 public interface ProcessCallback { | |
183 | |
184 void callback(TypeElement template); | |
185 | |
186 } | |
187 | |
188 } |