comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/TypeSystemData.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/model/TypeSystemData.java@8e5f9310f3aa
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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.api.dsl.internal.*;
31 import com.oracle.truffle.dsl.processor.*;
32 import com.oracle.truffle.dsl.processor.java.*;
33
34 public class TypeSystemData extends Template {
35
36 private final List<ImplicitCastData> implicitCasts = new ArrayList<>();
37 private final List<TypeCastData> casts = new ArrayList<>();
38 private final List<TypeCheckData> checks = new ArrayList<>();
39 private final List<TypeMirror> legacyTypes = new ArrayList<>();
40
41 private Set<String> legacyTypeIds;
42
43 private final boolean isDefault;
44 private final DSLOptions options;
45
46 public TypeSystemData(ProcessorContext context, TypeElement templateType, AnnotationMirror annotation, DSLOptions options, boolean isDefault) {
47 super(context, templateType, annotation);
48 this.options = options;
49 this.isDefault = isDefault;
50 }
51
52 public boolean isDefault() {
53 return isDefault;
54 }
55
56 public DSLOptions getOptions() {
57 return options;
58 }
59
60 @Override
61 public TypeSystemData getTypeSystem() {
62 return this;
63 }
64
65 public List<TypeMirror> getLegacyTypes() {
66 return legacyTypes;
67 }
68
69 public TypeCastData getCast(TypeMirror targetType) {
70 for (TypeCastData cast : casts) {
71 if (ElementUtils.typeEquals(cast.getTargetType(), targetType)) {
72 return cast;
73 }
74 }
75 return null;
76 }
77
78 public TypeCheckData getCheck(TypeMirror type) {
79 for (TypeCheckData check : checks) {
80 if (ElementUtils.typeEquals(check.getCheckedType(), type)) {
81 return check;
82 }
83 }
84 return null;
85 }
86
87 public List<ImplicitCastData> getImplicitCasts() {
88 return implicitCasts;
89 }
90
91 public List<TypeCastData> getCasts() {
92 return casts;
93 }
94
95 public List<TypeCheckData> getChecks() {
96 return checks;
97 }
98
99 @Override
100 protected List<MessageContainer> findChildContainers() {
101 List<MessageContainer> sinks = new ArrayList<>();
102 if (checks != null) {
103 sinks.addAll(checks);
104 }
105 if (casts != null) {
106 sinks.addAll(casts);
107 }
108 if (implicitCasts != null) {
109 sinks.addAll(implicitCasts);
110 }
111 return sinks;
112 }
113
114 @Override
115 public String toString() {
116 return getClass().getSimpleName() + "[template = " + ElementUtils.getSimpleName(getTemplateType()) + "]";
117 }
118
119 public List<ImplicitCastData> lookupByTargetType(TypeMirror targetType) {
120 if (getImplicitCasts() == null) {
121 return Collections.emptyList();
122 }
123 List<ImplicitCastData> foundCasts = new ArrayList<>();
124 for (ImplicitCastData cast : getImplicitCasts()) {
125 if (ElementUtils.typeEquals(cast.getTargetType(), targetType)) {
126 foundCasts.add(cast);
127 }
128 }
129 return foundCasts;
130 }
131
132 public ImplicitCastData lookupCast(TypeMirror sourceType, TypeMirror targetType) {
133 if (getImplicitCasts() == null) {
134 return null;
135 }
136 for (ImplicitCastData cast : getImplicitCasts()) {
137 if (ElementUtils.typeEquals(cast.getSourceType(), sourceType) && ElementUtils.typeEquals(cast.getTargetType(), targetType)) {
138 return cast;
139 }
140 }
141 return null;
142 }
143
144 public boolean hasImplicitSourceTypes(TypeMirror targetType) {
145 if (getImplicitCasts() == null) {
146 return false;
147 }
148 for (ImplicitCastData cast : getImplicitCasts()) {
149 if (ElementUtils.typeEquals(cast.getTargetType(), targetType)) {
150 return true;
151 }
152 }
153 return false;
154 }
155
156 public List<TypeMirror> lookupTargetTypes() {
157 List<TypeMirror> sourceTypes = new ArrayList<>();
158 for (ImplicitCastData cast : getImplicitCasts()) {
159 sourceTypes.add(cast.getTargetType());
160 }
161 return ElementUtils.uniqueSortedTypes(sourceTypes, true);
162 }
163
164 public List<TypeMirror> lookupSourceTypes(TypeMirror targetType) {
165 List<TypeMirror> sourceTypes = new ArrayList<>();
166 sourceTypes.add(targetType);
167 for (ImplicitCastData cast : getImplicitCasts()) {
168 if (ElementUtils.typeEquals(cast.getTargetType(), targetType)) {
169 sourceTypes.add(cast.getSourceType());
170 }
171 }
172 return ElementUtils.uniqueSortedTypes(sourceTypes, true);
173 }
174
175 public boolean isImplicitSubtypeOf(TypeMirror source, TypeMirror target) {
176 List<ImplicitCastData> targetCasts = lookupByTargetType(target);
177 for (ImplicitCastData cast : targetCasts) {
178 if (ElementUtils.isSubtype(boxType(source), boxType(cast.getSourceType()))) {
179 return true;
180 }
181 }
182 return ElementUtils.isSubtype(boxType(source), boxType(target));
183 }
184
185 public TypeMirror boxType(TypeMirror type) {
186 return ElementUtils.boxType(getContext(), type);
187 }
188
189 public boolean hasType(TypeMirror type) {
190 if (legacyTypeIds == null) {
191 legacyTypeIds = new HashSet<>();
192 for (TypeMirror legacyType : legacyTypes) {
193 legacyTypeIds.add(ElementUtils.getTypeId(legacyType));
194 }
195 }
196 return legacyTypeIds.contains(ElementUtils.getTypeId(type));
197 }
198
199 }