comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/ProcessorContext.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/ProcessorContext.java@18c0f02fa4d2
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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 java.util.*;
26
27 import javax.annotation.processing.*;
28 import javax.lang.model.element.*;
29 import javax.lang.model.type.*;
30 import javax.lang.model.util.*;
31
32 import com.oracle.truffle.api.frame.*;
33 import com.oracle.truffle.dsl.processor.java.*;
34 import com.oracle.truffle.dsl.processor.java.model.*;
35 import com.oracle.truffle.dsl.processor.model.*;
36
37 /**
38 * THIS IS NOT PUBLIC API.
39 */
40 public class ProcessorContext {
41
42 private final ProcessingEnvironment environment;
43
44 private final Map<String, Template> models = new HashMap<>();
45
46 private final ProcessCallback callback;
47 private final Log log;
48 private final TruffleTypes truffleTypes;
49
50 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(ElementUtils.getQualifiedName(element));
71 }
72
73 public void registerTemplate(TypeElement element, Template model) {
74 models.put(ElementUtils.getQualifiedName(element), model);
75 }
76
77 public Template getTemplate(TypeMirror templateTypeMirror, boolean invokeCallback) {
78 String qualifiedName = ElementUtils.getQualifiedName(templateTypeMirror);
79 Template model = models.get(qualifiedName);
80 if (model == null && invokeCallback) {
81 callback.callback(ElementUtils.fromTypeMirror(templateTypeMirror));
82 model = models.get(qualifiedName);
83 }
84 return model;
85 }
86
87 public DeclaredType getDeclaredType(Class<?> element) {
88 return (DeclaredType) ElementUtils.getType(environment, element);
89 }
90
91 public boolean isType(TypeMirror type, Class<?> clazz) {
92 return ElementUtils.typeEquals(type, getType(clazz));
93 }
94
95 public TypeMirror getType(Class<?> element) {
96 return ElementUtils.getType(environment, element);
97 }
98
99 public interface ProcessCallback {
100
101 void callback(TypeElement template);
102
103 }
104
105 public TypeMirror reloadTypeElement(TypeElement type) {
106 return getType(type.getQualifiedName().toString());
107 }
108
109 private TypeMirror getType(String className) {
110 TypeElement element = environment.getElementUtils().getTypeElement(className);
111 if (element != null) {
112 return element.asType();
113 }
114 return null;
115 }
116
117 public TypeMirror reloadType(TypeMirror type) {
118 if (type instanceof CodeTypeMirror) {
119 return type;
120 } else if (type.getKind().isPrimitive()) {
121 return type;
122 }
123 Types types = getEnvironment().getTypeUtils();
124
125 switch (type.getKind()) {
126 case ARRAY:
127 return types.getArrayType(reloadType(((ArrayType) type).getComponentType()));
128 case WILDCARD:
129 return types.getWildcardType(((WildcardType) type).getExtendsBound(), ((WildcardType) type).getSuperBound());
130 case DECLARED:
131 return reloadTypeElement((TypeElement) (((DeclaredType) type).asElement()));
132 }
133 return type;
134 }
135
136 private static final ThreadLocal<ProcessorContext> instance = new ThreadLocal<>();
137
138 public static void setThreadLocalInstance(ProcessorContext context) {
139 instance.set(context);
140 }
141
142 public static ProcessorContext getInstance() {
143 return instance.get();
144 }
145
146 public List<TypeMirror> getFrameTypes() {
147 return Arrays.asList(getType(VirtualFrame.class), getType(MaterializedFrame.class), getType(Frame.class));
148 }
149 }