Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/AbstractParser.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/AbstractParser.java@bd28da642eea |
children | 89f635cbd85e |
comparison
equal
deleted
inserted
replaced
16758:c5f8eeb3cbc8 | 16759:23415229349b |
---|---|
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.parser; | |
24 | |
25 import java.lang.annotation.*; | |
26 import java.util.*; | |
27 | |
28 import javax.annotation.processing.*; | |
29 import javax.lang.model.element.*; | |
30 import javax.tools.Diagnostic.Kind; | |
31 | |
32 import com.oracle.truffle.dsl.processor.*; | |
33 import com.oracle.truffle.dsl.processor.java.*; | |
34 import com.oracle.truffle.dsl.processor.model.*; | |
35 import com.oracle.truffle.dsl.processor.model.MessageContainer.Message; | |
36 | |
37 /** | |
38 * THIS IS NOT PUBLIC API. | |
39 */ | |
40 public abstract class AbstractParser<M extends Template> { | |
41 | |
42 protected final ProcessorContext context; | |
43 protected final ProcessingEnvironment processingEnv; | |
44 | |
45 protected final Log log; | |
46 | |
47 public AbstractParser() { | |
48 this.context = ProcessorContext.getInstance(); | |
49 this.processingEnv = context.getEnvironment(); | |
50 this.log = context.getLog(); | |
51 } | |
52 | |
53 public final M parse(Element element) { | |
54 M model = null; | |
55 try { | |
56 AnnotationMirror mirror = null; | |
57 if (getAnnotationType() != null) { | |
58 mirror = ElementUtils.findAnnotationMirror(processingEnv, element.getAnnotationMirrors(), getAnnotationType()); | |
59 } | |
60 | |
61 if (!context.getTruffleTypes().verify(context, element, mirror)) { | |
62 return null; | |
63 } | |
64 model = parse(element, mirror); | |
65 if (model == null) { | |
66 return null; | |
67 } | |
68 | |
69 redirectMessages(new HashSet<MessageContainer>(), model, model); | |
70 model.emitMessages(context, log); | |
71 return filterErrorElements(model); | |
72 } catch (CompileErrorException e) { | |
73 log.message(Kind.WARNING, element, null, null, "The truffle processor could not parse class due to error: %s", e.getMessage()); | |
74 return null; | |
75 } | |
76 } | |
77 | |
78 private void redirectMessages(Set<MessageContainer> visitedSinks, MessageContainer model, MessageContainer baseContainer) { | |
79 List<Message> messages = model.getMessages(); | |
80 for (int i = messages.size() - 1; i >= 0; i--) { | |
81 Message message = messages.get(i); | |
82 if (!ElementUtils.isEnclosedIn(baseContainer.getMessageElement(), message.getOriginalContainer().getMessageElement())) { | |
83 // redirect message | |
84 MessageContainer original = message.getOriginalContainer(); | |
85 String text = wrapText(original.getMessageElement(), original.getMessageAnnotation(), message.getText()); | |
86 Message redirectedMessage = new Message(null, baseContainer, text, message.getKind()); | |
87 model.getMessages().remove(i); | |
88 baseContainer.getMessages().add(redirectedMessage); | |
89 } | |
90 } | |
91 | |
92 for (MessageContainer childContainer : model) { | |
93 if (visitedSinks.contains(childContainer)) { | |
94 continue; | |
95 } | |
96 visitedSinks.add(childContainer); | |
97 | |
98 MessageContainer newBase = baseContainer; | |
99 if (childContainer.getBaseContainer() != null) { | |
100 newBase = childContainer.getBaseContainer(); | |
101 } | |
102 redirectMessages(visitedSinks, childContainer, newBase); | |
103 } | |
104 } | |
105 | |
106 private static String wrapText(Element element, AnnotationMirror mirror, String text) { | |
107 StringBuilder b = new StringBuilder(); | |
108 if (element != null) { | |
109 b.append("Element " + element.toString()); | |
110 } | |
111 if (mirror != null) { | |
112 b.append(" at annotation @" + ElementUtils.getSimpleName(mirror.getAnnotationType())); | |
113 } | |
114 | |
115 if (b.length() > 0) { | |
116 b.append(" is erroneous: ").append(text); | |
117 return b.toString(); | |
118 } else { | |
119 return text; | |
120 } | |
121 } | |
122 | |
123 protected M filterErrorElements(M model) { | |
124 return model.hasErrors() ? null : model; | |
125 } | |
126 | |
127 protected abstract M parse(Element element, AnnotationMirror mirror); | |
128 | |
129 public abstract Class<? extends Annotation> getAnnotationType(); | |
130 | |
131 public boolean isDelegateToRootDeclaredType() { | |
132 return false; | |
133 } | |
134 | |
135 public List<Class<? extends Annotation>> getAllAnnotationTypes() { | |
136 List<Class<? extends Annotation>> list = new ArrayList<>(); | |
137 if (getAnnotationType() != null) { | |
138 list.add(getAnnotationType()); | |
139 } | |
140 list.addAll(getTypeDelegatedAnnotationTypes()); | |
141 return list; | |
142 } | |
143 | |
144 public List<Class<? extends Annotation>> getTypeDelegatedAnnotationTypes() { | |
145 return Collections.emptyList(); | |
146 } | |
147 | |
148 } |