Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/ast/CodeElement.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 | 6343a09b2ec1 |
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.ast; | |
24 | |
25 import java.io.*; | |
26 import java.lang.annotation.*; | |
27 import java.util.*; | |
28 | |
29 import javax.lang.model.element.*; | |
30 | |
31 import com.oracle.truffle.codegen.processor.api.element.*; | |
32 import com.oracle.truffle.codegen.processor.codewriter.*; | |
33 | |
34 public abstract class CodeElement<E extends Element> implements WritableElement, GeneratedElement { | |
35 | |
36 private final Set<Modifier> modifiers; | |
37 private List<AnnotationMirror> annotations; | |
38 private List<E> enclosedElements; | |
39 | |
40 private Element enclosingElement; | |
41 | |
42 private Element generatorElement; | |
43 private AnnotationMirror generatorAnnotationMirror; | |
44 | |
45 public CodeElement() { | |
46 this.modifiers = new LinkedHashSet<>(); | |
47 } | |
48 | |
49 | |
50 @Override | |
51 public void setGeneratorAnnotationMirror(AnnotationMirror mirror) { | |
52 this.generatorAnnotationMirror = mirror; | |
53 } | |
54 | |
55 @Override | |
56 public void setGeneratorElement(Element element) { | |
57 this.generatorElement = element; | |
58 } | |
59 | |
60 @Override | |
61 public AnnotationMirror getGeneratorAnnotationMirror() { | |
62 return generatorAnnotationMirror; | |
63 } | |
64 | |
65 @Override | |
66 public Element getGeneratorElement() { | |
67 return generatorElement; | |
68 } | |
69 | |
70 public CodeElement(Set<Modifier> modifiers) { | |
71 this.modifiers = new LinkedHashSet<>(modifiers); | |
72 } | |
73 | |
74 public E add(E element) { | |
75 if (element == null) { | |
76 throw new NullPointerException(); | |
77 } | |
78 getEnclosedElements().add(element); | |
79 return element; | |
80 } | |
81 | |
82 public void remove(E element) { | |
83 getEnclosedElements().remove(element); | |
84 } | |
85 | |
86 @Override | |
87 public Set<Modifier> getModifiers() { | |
88 return modifiers; | |
89 } | |
90 | |
91 @Override | |
92 public List<E> getEnclosedElements() { | |
93 if (enclosedElements == null) { | |
94 enclosedElements = parentableList(this, new ArrayList<E>()); | |
95 } | |
96 return enclosedElements; | |
97 } | |
98 | |
99 @Override | |
100 public List<AnnotationMirror> getAnnotationMirrors() { | |
101 if (annotations == null) { | |
102 annotations = parentableList(this, new ArrayList<AnnotationMirror>()); | |
103 } | |
104 return annotations; | |
105 } | |
106 | |
107 @Override | |
108 public <A extends Annotation> A getAnnotation(Class<A> annotationType) { | |
109 throw new UnsupportedOperationException(); | |
110 } | |
111 | |
112 @Override | |
113 public void addAnnotationMirror(AnnotationMirror annotationMirror) { | |
114 getAnnotationMirrors().add(annotationMirror); | |
115 } | |
116 | |
117 public void removeAnnotationMirror(AnnotationMirror annotationMirror) { | |
118 getAnnotationMirrors().remove(annotationMirror); | |
119 } | |
120 | |
121 void setEnclosingElement(Element parent) { | |
122 if (this.enclosingElement != null && parent != null) { | |
123 throw new IllegalStateException("Element already added to " + parent); | |
124 } | |
125 this.enclosingElement = parent; | |
126 } | |
127 | |
128 public Element getEnclosingElement() { | |
129 return enclosingElement; | |
130 } | |
131 | |
132 public CodeTypeElement getEnclosingClass() { | |
133 Element p = enclosingElement; | |
134 while (p != null && p.getKind() != ElementKind.CLASS | |
135 && p.getKind() != ElementKind.ENUM) { | |
136 p = p.getEnclosingElement(); | |
137 } | |
138 return (CodeTypeElement) p; | |
139 } | |
140 | |
141 <T> List<T> parentableList(Element parent, List<T> list) { | |
142 return new ParentableList<>(parent, list); | |
143 } | |
144 | |
145 | |
146 @Override | |
147 public String toString() { | |
148 StringBuilderCodeWriter codeWriter = new StringBuilderCodeWriter(); | |
149 accept(codeWriter, null); | |
150 return codeWriter.getString(); | |
151 } | |
152 | |
153 private static class StringBuilderCodeWriter extends AbstractCodeWriter { | |
154 | |
155 public StringBuilderCodeWriter() { | |
156 this.writer = new CharArrayWriter(); | |
157 } | |
158 | |
159 @Override | |
160 protected Writer createWriter(CodeTypeElement clazz) throws IOException { | |
161 return writer; | |
162 } | |
163 public String getString() { | |
164 return new String(((CharArrayWriter) writer).toCharArray()); | |
165 } | |
166 | |
167 } | |
168 | |
169 | |
170 private static class ParentableList<T> implements List<T> { | |
171 | |
172 private final Element parent; | |
173 private final List<T> delegate; | |
174 | |
175 public ParentableList(Element parent, List<T> delegate) { | |
176 this.parent = parent; | |
177 this.delegate = delegate; | |
178 } | |
179 | |
180 private void addImpl(T element) { | |
181 if (element != null) { | |
182 if (element instanceof CodeElement<?>) { | |
183 ((CodeElement<?>) element).setEnclosingElement(parent); | |
184 } | |
185 } | |
186 } | |
187 | |
188 private static void removeImpl(Object element) { | |
189 if (element instanceof CodeElement<?>) { | |
190 ((CodeElement<?>) element).setEnclosingElement(null); | |
191 } | |
192 } | |
193 | |
194 @Override | |
195 public int size() { | |
196 return delegate.size(); | |
197 } | |
198 | |
199 @Override | |
200 public boolean isEmpty() { | |
201 return delegate.isEmpty(); | |
202 } | |
203 | |
204 @Override | |
205 public boolean contains(Object o) { | |
206 return delegate.contains(o); | |
207 } | |
208 | |
209 @Override | |
210 public Iterator<T> iterator() { | |
211 return delegate.iterator(); | |
212 } | |
213 | |
214 @Override | |
215 public Object[] toArray() { | |
216 return delegate.toArray(); | |
217 } | |
218 | |
219 @Override | |
220 public <E> E[] toArray(E[] a) { | |
221 return delegate.toArray(a); | |
222 } | |
223 | |
224 @Override | |
225 public boolean add(T e) { | |
226 addImpl(e); | |
227 return delegate.add(e); | |
228 } | |
229 | |
230 @Override | |
231 public boolean remove(Object o) { | |
232 boolean removed = delegate.remove(o); | |
233 if (removed) { | |
234 removeImpl(o); | |
235 } | |
236 return removed; | |
237 } | |
238 | |
239 @Override | |
240 public boolean containsAll(Collection< ? > c) { | |
241 return delegate.containsAll(c); | |
242 } | |
243 | |
244 @Override | |
245 public boolean addAll(Collection< ? extends T> c) { | |
246 if (c != null) { | |
247 for (T t : c) { | |
248 addImpl(t); | |
249 } | |
250 } | |
251 return delegate.addAll(c); | |
252 } | |
253 | |
254 @Override | |
255 public boolean addAll(int index, Collection< ? extends T> c) { | |
256 if (c != null) { | |
257 for (T t : c) { | |
258 addImpl(t); | |
259 } | |
260 } | |
261 return delegate.addAll(index, c); | |
262 } | |
263 | |
264 @Override | |
265 public boolean removeAll(Collection< ? > c) { | |
266 if (c != null) { | |
267 for (Object t : c) { | |
268 removeImpl(t); | |
269 } | |
270 } | |
271 return delegate.removeAll(c); | |
272 } | |
273 | |
274 @Override | |
275 public String toString() { | |
276 return delegate.toString(); | |
277 } | |
278 | |
279 @Override | |
280 public boolean retainAll(Collection< ? > c) { | |
281 throw new UnsupportedOperationException("Not supported by parentable list"); | |
282 } | |
283 | |
284 @Override | |
285 public void clear() { | |
286 for (Object e : this) { | |
287 removeImpl(e); | |
288 } | |
289 delegate.clear(); | |
290 } | |
291 | |
292 @Override | |
293 public T get(int index) { | |
294 return delegate.get(index); | |
295 } | |
296 | |
297 @Override | |
298 public T set(int index, T element) { | |
299 removeImpl(delegate.get(index)); | |
300 addImpl(element); | |
301 return delegate.set(index, element); | |
302 } | |
303 | |
304 @Override | |
305 public void add(int index, T element) { | |
306 addImpl(element); | |
307 delegate.add(index, element); | |
308 } | |
309 | |
310 @Override | |
311 public T remove(int index) { | |
312 T element = delegate.remove(index); | |
313 removeImpl(element); | |
314 return element; | |
315 } | |
316 | |
317 @Override | |
318 public int indexOf(Object o) { | |
319 return delegate.indexOf(o); | |
320 } | |
321 | |
322 @Override | |
323 public int lastIndexOf(Object o) { | |
324 return delegate.lastIndexOf(o); | |
325 } | |
326 | |
327 @Override | |
328 public ListIterator<T> listIterator() { | |
329 return delegate.listIterator(); | |
330 } | |
331 | |
332 @Override | |
333 public ListIterator<T> listIterator(int index) { | |
334 return delegate.listIterator(index); | |
335 } | |
336 | |
337 @Override | |
338 public List<T> subList(int fromIndex, int toIndex) { | |
339 return new ParentableList<>(parent, delegate.subList(fromIndex, toIndex)); | |
340 } | |
341 | |
342 } | |
343 | |
344 | |
345 } |