use of org.graalvm.compiler.core.gen.NodeMatchRules in project graal by oracle.
the class MatchProcessor method createFiles.
private void createFiles(MatchRuleDescriptor info) {
String pkg = ((PackageElement) info.topDeclaringType.getEnclosingElement()).getQualifiedName().toString();
Name topDeclaringClass = info.topDeclaringType.getSimpleName();
String matchStatementClassName = topDeclaringClass + "_" + MatchStatementSet.class.getSimpleName();
Element[] originatingElements = info.originatingElements.toArray(new Element[info.originatingElements.size()]);
Types typeUtils = typeUtils();
Filer filer = processingEnv.getFiler();
try (PrintWriter out = createSourceFile(pkg, matchStatementClassName, filer, originatingElements)) {
out.println("// CheckStyle: stop header check");
out.println("// CheckStyle: stop line length check");
out.println("// GENERATED CONTENT - DO NOT EDIT");
out.println("// Source: " + topDeclaringClass + ".java");
out.println("package " + pkg + ";");
out.println("");
out.println("import java.util.*;");
out.println("import " + MatchStatementSet.class.getPackage().getName() + ".*;");
out.println("import " + NodeMatchRules.class.getName() + ";");
out.println("import " + Position.class.getName() + ";");
out.println("import " + ServiceProvider.class.getName() + ";");
for (String p : info.requiredPackages) {
out.println("import " + p + ".*;");
}
out.println("");
out.println("@" + ServiceProvider.class.getSimpleName() + "(" + MatchStatementSet.class.getSimpleName() + ".class)");
out.println("public class " + matchStatementClassName + " implements " + MatchStatementSet.class.getSimpleName() + " {");
out.println();
// Generate declarations for the wrapper class to invoke the code generation methods.
for (MethodInvokerItem invoker : info.invokers.getValues()) {
StringBuilder args = new StringBuilder();
StringBuilder types = new StringBuilder();
int count = invoker.fields.size();
int index = 0;
for (VariableElement arg : invoker.fields) {
args.append('"');
args.append(arg.getSimpleName());
args.append('"');
types.append(String.format("(%s) args[%s]", fullClassName(typeUtils.asElement(arg.asType())), index++));
if (count-- > 1) {
args.append(", ");
types.append(", ");
}
}
out.printf(" private static final String[] %s = new String[] {%s};\n", invoker.argumentsListName(), args);
out.printf(" private static final class %s implements MatchGenerator {\n", invoker.wrapperClass());
out.printf(" static MatchGenerator instance = new %s();\n", invoker.wrapperClass());
out.printf(" @Override\n");
out.printf(" public ComplexMatchResult match(NodeMatchRules nodeMatchRules, Object...args) {\n");
out.printf(" return ((%s) nodeMatchRules).%s(%s);\n", invoker.nodeLIRBuilderClass, invoker.methodName, types);
out.printf(" }\n");
out.printf(" @Override\n");
out.printf(" public String getName() {\n");
out.printf(" return \"%s\";\n", invoker.methodName);
out.printf(" }\n");
out.printf(" }\n");
out.println();
}
String desc = MatchStatement.class.getSimpleName();
out.println(" @Override");
out.println(" public Class<? extends NodeMatchRules> forClass() {");
out.println(" return " + topDeclaringClass + ".class;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public List<" + desc + "> statements() {");
out.println(" // Checkstyle: stop ");
for (String positionDeclaration : info.positionDeclarations) {
out.println(" " + positionDeclaration);
}
out.println();
out.println(" List<" + desc + "> statements = Collections.unmodifiableList(Arrays.asList(");
int i = 0;
for (MatchRuleItem matchRule : info.matchRules) {
String comma = i == info.matchRules.size() - 1 ? "" : ",";
out.printf(" %s%s\n", matchRule.ruleBuilder(), comma);
i++;
}
out.println(" ));");
out.println(" // Checkstyle: resume");
out.println(" return statements;");
out.println(" }");
out.println();
out.println("}");
}
}
Aggregations