comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/TypeSystemData.java @ 16759:23415229349b

Truffle-DSL: new package structure.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Aug 2014 15:57:14 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/typesystem/TypeSystemData.java@bd28da642eea
children e8d9b3827d4b
comparison
equal deleted inserted replaced
16758:c5f8eeb3cbc8 16759:23415229349b
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.model;
24
25 import java.util.*;
26
27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*;
29
30 import com.oracle.truffle.dsl.processor.*;
31 import com.oracle.truffle.dsl.processor.java.*;
32
33 public class TypeSystemData extends Template {
34
35 private List<TypeData> types;
36 private List<TypeMirror> primitiveTypeMirrors = new ArrayList<>();
37 private List<TypeMirror> boxedTypeMirrors = new ArrayList<>();
38
39 private List<ImplicitCastData> implicitCasts;
40 private List<TypeCastData> casts;
41 private List<TypeCheckData> checks;
42
43 private TypeMirror genericType;
44 private TypeData voidType;
45
46 public TypeSystemData(ProcessorContext context, TypeElement templateType, AnnotationMirror annotation) {
47 super(context, templateType, null, annotation);
48 }
49
50 @Override
51 public TypeSystemData getTypeSystem() {
52 return this;
53 }
54
55 public void setTypes(List<TypeData> types) {
56 this.types = types;
57 if (types != null) {
58 for (TypeData typeData : types) {
59 primitiveTypeMirrors.add(typeData.getPrimitiveType());
60 boxedTypeMirrors.add(typeData.getBoxedType());
61 }
62 }
63 }
64
65 public void setImplicitCasts(List<ImplicitCastData> implicitCasts) {
66 this.implicitCasts = implicitCasts;
67 }
68
69 public List<ImplicitCastData> getImplicitCasts() {
70 return implicitCasts;
71 }
72
73 public void setCasts(List<TypeCastData> casts) {
74 this.casts = casts;
75 }
76
77 public void setChecks(List<TypeCheckData> checks) {
78 this.checks = checks;
79 }
80
81 public void setGenericType(TypeMirror genericType) {
82 this.genericType = genericType;
83 }
84
85 public void setVoidType(TypeData voidType) {
86 this.voidType = voidType;
87 }
88
89 @Override
90 protected List<MessageContainer> findChildContainers() {
91 List<MessageContainer> sinks = new ArrayList<>();
92 if (types != null) {
93 sinks.addAll(types);
94 }
95 if (checks != null) {
96 sinks.addAll(checks);
97 }
98 if (casts != null) {
99 sinks.addAll(casts);
100 }
101 if (implicitCasts != null) {
102 sinks.addAll(implicitCasts);
103 }
104 return sinks;
105 }
106
107 public TypeData getVoidType() {
108 return voidType;
109 }
110
111 public List<TypeMirror> getBoxedTypeMirrors() {
112 return boxedTypeMirrors;
113 }
114
115 public List<TypeMirror> getPrimitiveTypeMirrors() {
116 return primitiveTypeMirrors;
117 }
118
119 public List<TypeData> getTypes() {
120 return types;
121 }
122
123 public TypeMirror getGenericType() {
124 return genericType;
125 }
126
127 public TypeData getGenericTypeData() {
128 TypeData result = types.get(types.size() - 1);
129 assert result.getBoxedType() == genericType;
130 return result;
131 }
132
133 public TypeData findType(String simpleName) {
134 for (TypeData type : types) {
135 if (ElementUtils.getSimpleName(type.getBoxedType()).equals(simpleName)) {
136 return type;
137 }
138 }
139 return null;
140 }
141
142 public TypeData findTypeData(TypeMirror type) {
143 if (ElementUtils.typeEquals(voidType.getPrimitiveType(), type)) {
144 return voidType;
145 }
146
147 int index = findType(type);
148 if (index == -1) {
149 return null;
150 }
151 return types.get(index);
152 }
153
154 public int findType(TypeMirror type) {
155 for (int i = 0; i < types.size(); i++) {
156 if (ElementUtils.typeEquals(types.get(i).getPrimitiveType(), type)) {
157 return i;
158 }
159 }
160 return -1;
161 }
162
163 @Override
164 public String toString() {
165 return getClass().getSimpleName() + "[template = " + ElementUtils.getSimpleName(getTemplateType()) + ", types = " + types + "]";
166 }
167
168 public List<ImplicitCastData> lookupByTargetType(TypeData targetType) {
169 if (getImplicitCasts() == null) {
170 return Collections.emptyList();
171 }
172 List<ImplicitCastData> foundCasts = new ArrayList<>();
173 for (ImplicitCastData cast : getImplicitCasts()) {
174 if (cast.getTargetType().equals(targetType)) {
175 foundCasts.add(cast);
176 }
177 }
178 return foundCasts;
179 }
180
181 public ImplicitCastData lookupCast(TypeData sourceType, TypeData targetType) {
182 if (getImplicitCasts() == null) {
183 return null;
184 }
185 for (ImplicitCastData cast : getImplicitCasts()) {
186 if (cast.getSourceType().equals(sourceType) && cast.getTargetType().equals(targetType)) {
187 return cast;
188 }
189 }
190 return null;
191 }
192
193 public List<TypeData> lookupSourceTypes(TypeData type) {
194 List<TypeData> sourceTypes = new ArrayList<>();
195 sourceTypes.add(type);
196 if (getImplicitCasts() != null) {
197 for (ImplicitCastData cast : getImplicitCasts()) {
198 if (cast.getTargetType() == type) {
199 sourceTypes.add(cast.getSourceType());
200 }
201 }
202 }
203 Collections.sort(sourceTypes);
204 return sourceTypes;
205 }
206
207 }