50330
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2018, 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 org.graalvm.compiler.replacements.processor;
|
|
24 |
|
|
25 |
import java.io.PrintWriter;
|
|
26 |
import java.util.Set;
|
|
27 |
|
|
28 |
import javax.lang.model.element.ExecutableElement;
|
|
29 |
import javax.lang.model.element.Modifier;
|
|
30 |
import javax.lang.model.element.TypeElement;
|
|
31 |
import javax.lang.model.element.VariableElement;
|
|
32 |
import javax.lang.model.type.ArrayType;
|
|
33 |
import javax.lang.model.type.DeclaredType;
|
|
34 |
import javax.lang.model.type.TypeMirror;
|
|
35 |
import javax.lang.model.type.TypeVariable;
|
|
36 |
import javax.lang.model.type.WildcardType;
|
|
37 |
|
|
38 |
import org.graalvm.compiler.processor.AbstractProcessor;
|
|
39 |
import org.graalvm.compiler.replacements.processor.InjectedDependencies.Dependency;
|
|
40 |
import org.graalvm.compiler.replacements.processor.InjectedDependencies.WellKnownDependency;
|
|
41 |
|
|
42 |
public abstract class GeneratedPlugin {
|
|
43 |
|
|
44 |
protected final ExecutableElement intrinsicMethod;
|
|
45 |
private boolean needInjectionProvider;
|
|
46 |
|
|
47 |
private String pluginName;
|
|
48 |
|
|
49 |
public GeneratedPlugin(ExecutableElement intrinsicMethod) {
|
|
50 |
this.intrinsicMethod = intrinsicMethod;
|
|
51 |
this.needInjectionProvider = false;
|
|
52 |
this.pluginName = intrinsicMethod.getEnclosingElement().getSimpleName() + "_" + intrinsicMethod.getSimpleName();
|
|
53 |
}
|
|
54 |
|
|
55 |
protected abstract TypeElement getAnnotationClass(AbstractProcessor processor);
|
|
56 |
|
|
57 |
public String getPluginName() {
|
|
58 |
return pluginName;
|
|
59 |
}
|
|
60 |
|
|
61 |
public void setPluginName(String pluginName) {
|
|
62 |
this.pluginName = pluginName;
|
|
63 |
}
|
|
64 |
|
|
65 |
public void generate(AbstractProcessor processor, PrintWriter out) {
|
|
66 |
out.printf(" // class: %s\n", intrinsicMethod.getEnclosingElement());
|
|
67 |
out.printf(" // method: %s\n", intrinsicMethod);
|
|
68 |
out.printf(" // generated-by: %s\n", getClass().getName());
|
|
69 |
out.printf(" private static final class %s extends GeneratedInvocationPlugin {\n", pluginName);
|
|
70 |
out.printf("\n");
|
|
71 |
out.printf(" @Override\n");
|
|
72 |
out.printf(" public boolean execute(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode[] args) {\n");
|
|
73 |
InjectedDependencies deps = createExecute(processor, out);
|
|
74 |
out.printf(" }\n");
|
|
75 |
out.printf(" @Override\n");
|
|
76 |
out.printf(" public Class<? extends Annotation> getSource() {\n");
|
|
77 |
out.printf(" return %s.class;\n", getAnnotationClass(processor).getQualifiedName().toString().replace('$', '.'));
|
|
78 |
out.printf(" }\n");
|
|
79 |
|
|
80 |
createPrivateMembers(processor, out, deps);
|
|
81 |
|
|
82 |
out.printf(" }\n");
|
|
83 |
}
|
|
84 |
|
|
85 |
public void register(PrintWriter out) {
|
|
86 |
out.printf(" plugins.register(new %s(", pluginName);
|
|
87 |
if (needInjectionProvider) {
|
|
88 |
out.printf("injection");
|
|
89 |
}
|
|
90 |
out.printf("), %s.class, \"%s\"", intrinsicMethod.getEnclosingElement(), intrinsicMethod.getSimpleName());
|
|
91 |
if (!intrinsicMethod.getModifiers().contains(Modifier.STATIC)) {
|
|
92 |
out.printf(", InvocationPlugin.Receiver.class");
|
|
93 |
}
|
|
94 |
for (VariableElement arg : intrinsicMethod.getParameters()) {
|
|
95 |
out.printf(", %s.class", getErasedType(arg.asType()));
|
|
96 |
}
|
|
97 |
out.printf(");\n");
|
|
98 |
}
|
|
99 |
|
|
100 |
public abstract void extraImports(Set<String> imports);
|
|
101 |
|
|
102 |
protected abstract InjectedDependencies createExecute(AbstractProcessor processor, PrintWriter out);
|
|
103 |
|
|
104 |
static String getErasedType(TypeMirror type) {
|
|
105 |
switch (type.getKind()) {
|
|
106 |
case DECLARED:
|
|
107 |
DeclaredType declared = (DeclaredType) type;
|
|
108 |
TypeElement element = (TypeElement) declared.asElement();
|
|
109 |
return element.getQualifiedName().toString();
|
|
110 |
case TYPEVAR:
|
|
111 |
return getErasedType(((TypeVariable) type).getUpperBound());
|
|
112 |
case WILDCARD:
|
|
113 |
return getErasedType(((WildcardType) type).getExtendsBound());
|
|
114 |
case ARRAY:
|
|
115 |
return getErasedType(((ArrayType) type).getComponentType()) + "[]";
|
|
116 |
default:
|
|
117 |
return type.toString();
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
static boolean hasRawtypeWarning(TypeMirror type) {
|
|
122 |
switch (type.getKind()) {
|
|
123 |
case DECLARED:
|
|
124 |
DeclaredType declared = (DeclaredType) type;
|
|
125 |
return declared.getTypeArguments().size() > 0;
|
|
126 |
case TYPEVAR:
|
|
127 |
return false;
|
|
128 |
case WILDCARD:
|
|
129 |
return false;
|
|
130 |
case ARRAY:
|
|
131 |
return hasRawtypeWarning(((ArrayType) type).getComponentType());
|
|
132 |
default:
|
|
133 |
return false;
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
static boolean hasUncheckedWarning(TypeMirror type) {
|
|
138 |
switch (type.getKind()) {
|
|
139 |
case DECLARED:
|
|
140 |
DeclaredType declared = (DeclaredType) type;
|
|
141 |
for (TypeMirror typeParam : declared.getTypeArguments()) {
|
|
142 |
if (hasUncheckedWarning(typeParam)) {
|
|
143 |
return true;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
return false;
|
|
147 |
case TYPEVAR:
|
|
148 |
return true;
|
|
149 |
case WILDCARD:
|
|
150 |
return ((WildcardType) type).getExtendsBound() != null;
|
|
151 |
case ARRAY:
|
|
152 |
return hasUncheckedWarning(((ArrayType) type).getComponentType());
|
|
153 |
default:
|
|
154 |
return false;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
private void createPrivateMembers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
|
|
159 |
if (!deps.isEmpty()) {
|
|
160 |
out.printf("\n");
|
|
161 |
for (Dependency dep : deps) {
|
|
162 |
out.printf(" private final %s %s;\n", dep.type, dep.name);
|
|
163 |
}
|
|
164 |
|
|
165 |
out.printf("\n");
|
|
166 |
out.printf(" private %s(InjectionProvider injection) {\n", pluginName);
|
|
167 |
for (Dependency dep : deps) {
|
|
168 |
out.printf(" this.%s = %s;\n", dep.name, dep.inject(processor, intrinsicMethod));
|
|
169 |
}
|
|
170 |
out.printf(" }\n");
|
|
171 |
|
|
172 |
needInjectionProvider = true;
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
protected static String getReturnKind(ExecutableElement method) {
|
|
177 |
switch (method.getReturnType().getKind()) {
|
|
178 |
case BOOLEAN:
|
|
179 |
case BYTE:
|
|
180 |
case SHORT:
|
|
181 |
case CHAR:
|
|
182 |
case INT:
|
|
183 |
return "Int";
|
|
184 |
case LONG:
|
|
185 |
return "Long";
|
|
186 |
case FLOAT:
|
|
187 |
return "Float";
|
|
188 |
case DOUBLE:
|
|
189 |
return "Double";
|
|
190 |
case VOID:
|
|
191 |
return "Void";
|
|
192 |
case ARRAY:
|
|
193 |
case TYPEVAR:
|
|
194 |
case DECLARED:
|
|
195 |
return "Object";
|
|
196 |
default:
|
|
197 |
throw new IllegalArgumentException(method.getReturnType().toString());
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
protected static void constantArgument(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, int argIdx, TypeMirror type, int nodeIdx) {
|
|
202 |
if (hasRawtypeWarning(type)) {
|
|
203 |
out.printf(" @SuppressWarnings({\"rawtypes\"})\n");
|
|
204 |
}
|
|
205 |
out.printf(" %s arg%d;\n", getErasedType(type), argIdx);
|
|
206 |
out.printf(" if (args[%d].isConstant()) {\n", nodeIdx);
|
|
207 |
if (type.equals(processor.getType("jdk.vm.ci.meta.ResolvedJavaType"))) {
|
|
208 |
out.printf(" arg%d = %s.asJavaType(args[%d].asConstant());\n", argIdx, deps.use(WellKnownDependency.CONSTANT_REFLECTION), nodeIdx);
|
|
209 |
} else {
|
|
210 |
switch (type.getKind()) {
|
|
211 |
case BOOLEAN:
|
|
212 |
out.printf(" arg%d = args[%d].asJavaConstant().asInt() != 0;\n", argIdx, nodeIdx);
|
|
213 |
break;
|
|
214 |
case BYTE:
|
|
215 |
out.printf(" arg%d = (byte) args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
|
|
216 |
break;
|
|
217 |
case CHAR:
|
|
218 |
out.printf(" arg%d = (char) args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
|
|
219 |
break;
|
|
220 |
case SHORT:
|
|
221 |
out.printf(" arg%d = (short) args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
|
|
222 |
break;
|
|
223 |
case INT:
|
|
224 |
out.printf(" arg%d = args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
|
|
225 |
break;
|
|
226 |
case LONG:
|
|
227 |
out.printf(" arg%d = args[%d].asJavaConstant().asLong();\n", argIdx, nodeIdx);
|
|
228 |
break;
|
|
229 |
case FLOAT:
|
|
230 |
out.printf(" arg%d = args[%d].asJavaConstant().asFloat();\n", argIdx, nodeIdx);
|
|
231 |
break;
|
|
232 |
case DOUBLE:
|
|
233 |
out.printf(" arg%d = args[%d].asJavaConstant().asDouble();\n", argIdx, nodeIdx);
|
|
234 |
break;
|
|
235 |
case ARRAY:
|
|
236 |
case DECLARED:
|
|
237 |
out.printf(" arg%d = %s.asObject(%s.class, args[%d].asJavaConstant());\n", argIdx, deps.use(WellKnownDependency.SNIPPET_REFLECTION), getErasedType(type), nodeIdx);
|
|
238 |
break;
|
|
239 |
default:
|
|
240 |
throw new IllegalArgumentException(type.toString());
|
|
241 |
}
|
|
242 |
}
|
|
243 |
out.printf(" } else {\n");
|
|
244 |
out.printf(" assert b.canDeferPlugin(this) : b.getClass().toString();\n");
|
|
245 |
out.printf(" return false;\n");
|
|
246 |
out.printf(" }\n");
|
|
247 |
}
|
|
248 |
}
|