Search in sources :

Example 1 with NodeMatchRules

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("}");
    }
}
Also used : SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) Position(org.graalvm.compiler.graph.Position) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) MatchStatementSet(org.graalvm.compiler.core.match.MatchStatementSet) VariableElement(javax.lang.model.element.VariableElement) Name(javax.lang.model.element.Name) ServiceProvider(org.graalvm.compiler.serviceprovider.ServiceProvider) Filer(javax.annotation.processing.Filer) PrintWriter(java.io.PrintWriter) NodeMatchRules(org.graalvm.compiler.core.gen.NodeMatchRules)

Aggregations

PrintWriter (java.io.PrintWriter)1 Filer (javax.annotation.processing.Filer)1 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 Name (javax.lang.model.element.Name)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 Types (javax.lang.model.util.Types)1 NodeMatchRules (org.graalvm.compiler.core.gen.NodeMatchRules)1 MatchStatementSet (org.graalvm.compiler.core.match.MatchStatementSet)1 Position (org.graalvm.compiler.graph.Position)1 ServiceProvider (org.graalvm.compiler.serviceprovider.ServiceProvider)1