annotate graal/com.oracle.graal.compiler.match.processor/src/com/oracle/graal/compiler/match/processor/MatchProcessor.java @ 21543:93c50cefb9e8

moved GraalInternalError to com.oracle.jvmci.common and renamed it to JVMCIError (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Mon, 25 May 2015 23:30:34 +0200
parents 391f94d4d23f
children 6a93800d10f1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
1 /*
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
4 *
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
8 *
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
13 * accompanied this code).
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
14 *
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
18 *
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
21 * questions.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
22 */
21097
391f94d4d23f Move MatchProcessor and HotSpotVMConfigPorcessor to their own projects
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 19405
diff changeset
23 package com.oracle.graal.compiler.match.processor;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
24
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
25 import java.io.*;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
26 import java.util.*;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
27 import java.util.regex.*;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
28
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
29 import javax.annotation.processing.*;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
30 import javax.lang.model.*;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
31 import javax.lang.model.element.*;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
32 import javax.lang.model.type.*;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
33 import javax.lang.model.util.*;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
34 import javax.tools.Diagnostic.Kind;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
35 import javax.tools.*;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
36
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
37 import com.oracle.graal.compiler.gen.*;
21097
391f94d4d23f Move MatchProcessor and HotSpotVMConfigPorcessor to their own projects
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 19405
diff changeset
38 import com.oracle.graal.compiler.match.*;
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
39 import com.oracle.graal.graph.*;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
40 import com.oracle.graal.nodes.*;
21543
93c50cefb9e8 moved GraalInternalError to com.oracle.jvmci.common and renamed it to JVMCIError (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21097
diff changeset
41 import com.oracle.jvmci.common.*;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
42
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
43 /**
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
44 * Processes classes annotated with {@link MatchRule}. A {@link MatchStatementSet} service is
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
45 * generated for each top level class containing at least one such field. These service objects can
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
46 * be retrieved as follows:
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
47 *
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
48 * <pre>
15966
27ff0792b048 made more services implement com.oracle.graal.api.runtime.Service for faster loading
Doug Simon <doug.simon@oracle.com>
parents: 15844
diff changeset
49 * Iterable<MatchStatementSet> sl = Services.load(MatchStatementSet.class);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
50 * for (MatchStatementSet rules : sl) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
51 * ...
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
52 * }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
53 * </pre>
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
54 */
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
55 @SupportedAnnotationTypes({"com.oracle.graal.compiler.match.MatchRule", "com.oracle.graal.compiler.match.MatchRules", "com.oracle.graal.compiler.match.MatchableNode",
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
56 "com.oracle.graal.compiler.match.MatchableNodes"})
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
57 public class MatchProcessor extends AbstractProcessor {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
58
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
59 public MatchProcessor() {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
60 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
61
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
62 @Override
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
63 public SourceVersion getSupportedSourceVersion() {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
64 return SourceVersion.latest();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
65 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
66
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
67 private final Set<Element> processedMatchRule = new HashSet<>();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
68 private final Set<Element> processedMatchableNode = new HashSet<>();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
69
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
70 private static class RuleParseError extends RuntimeException {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
71 private static final long serialVersionUID = 6456128283609257490L;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
72
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
73 RuleParseError(String format, Object... args) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
74 super(String.format(format, args));
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
75 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
76 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
77
19358
5ea169a3bf81 make various fields final
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18163
diff changeset
78 private static final Pattern tokenizer = Pattern.compile("\\s*([()=]|[A-Za-z][A-Za-z0-9]*)\\s*");
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
79
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
80 private class RuleParser {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
81 private ArrayList<TypeDescriptor> capturedTypes = new ArrayList<>();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
82
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
83 private ArrayList<String> capturedNames = new ArrayList<>();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
84
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
85 private final String[] tokens;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
86
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
87 private int current;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
88
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
89 private MatchDescriptor matchDescriptor;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
90
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
91 private final Set<Element> originatingElements = new HashSet<>();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
92
17242
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
93 private Set<String> requiredPackages = new HashSet<>();
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
94
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
95 RuleParser(String rule) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
96 Matcher m = tokenizer.matcher(rule);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
97 List<String> list = new ArrayList<>();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
98 int end = 0;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
99 while (m.lookingAt()) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
100 list.add(m.group(1));
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
101 end = m.end();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
102 m.region(m.end(), m.regionEnd());
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
103 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
104 if (end != m.regionEnd()) {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
105 throw new RuleParseError("Unexpected tokens :" + rule.substring(m.end(), m.regionEnd()));
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
106 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
107 tokens = list.toArray(new String[0]);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
108
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
109 matchDescriptor = parseExpression();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
110 if (!done()) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
111 throw new RuleParseError("didn't consume all tokens");
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
112 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
113 capturedNames.add(0, "root");
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
114 capturedTypes.add(0, matchDescriptor.nodeType);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
115 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
116
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
117 String next() {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
118 return tokens[current++];
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
119 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
120
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
121 String peek(String name) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
122 if (current >= tokens.length) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
123 if (name == null) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
124 throw new RuleParseError("Out of tokens");
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
125 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
126 throw new RuleParseError("Out of tokens looking for %s", name);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
127 }
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
128 return tokens[current];
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
129 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
130
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
131 boolean done() {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
132 return current == tokens.length;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
133 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
134
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
135 private MatchDescriptor parseExpression() {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
136 if (peek("(").equals("(")) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
137 next();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
138 MatchDescriptor descriptor = parseType(true);
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
139 for (int n = 0; n < descriptor.nodeType.inputs.length; n++) {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
140 if (peek("(").equals("(")) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
141 descriptor.inputs[n] = parseExpression();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
142 } else {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
143 descriptor.inputs[n] = parseType(false);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
144 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
145 }
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
146 for (int n = 0; n < descriptor.nodeType.inputs.length; n++) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
147 if (descriptor.inputs[n] == null) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
148 throw new RuleParseError("not enough inputs for " + descriptor.name);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
149 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
150 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
151 if (peek(")").equals(")")) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
152 next();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
153 return descriptor;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
154 }
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
155 throw new RuleParseError("Too many arguments to " + descriptor.nodeType.nodeClass);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
156 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
157 throw new RuleParseError("Extra tokens following match pattern: " + peek(null));
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
158 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
159
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
160 private MatchDescriptor parseType(boolean forExpression) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
161 TypeDescriptor type = null;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
162 String name = null;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
163 if (Character.isUpperCase(peek("node type or name").charAt(0))) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
164 String token = next();
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
165 type = knownTypes.get(token);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
166 if (type == null) {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
167 throw new RuleParseError("Unknown node type: " + token);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
168 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
169 if (peek("=").equals("=")) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
170 next();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
171 name = next();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
172 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
173 originatingElements.addAll(type.originatingElements);
17242
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
174 requiredPackages.add(type.nodePackage);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
175 } else if (Character.isLowerCase(peek("name").charAt(0))) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
176 name = next();
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
177 type = valueType;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
178 } else {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
179 throw new RuleParseError("Unexpected token \"%s\" when looking for name or node type", peek(null));
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
180 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
181 if (name != null) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
182 if (!capturedNames.contains(name)) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
183 capturedNames.add(name);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
184 capturedTypes.add(type);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
185 } else {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
186 int index = capturedNames.indexOf(name);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
187 if (capturedTypes.get(index) != type) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
188 throw new RuleParseError("Captured node \"%s\" has differing types", name);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
189 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
190 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
191 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
192 return new MatchDescriptor(type, name, forExpression);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
193 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
194
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
195 List<String> generateVariants() {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
196 return matchDescriptor.generateVariants();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
197 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
198
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
199 /**
16897
f90dcdbbb75e switched to using new NodeFieldIterator and NodeFieldIterable for traversing Node inputs and successors
Doug Simon <doug.simon@oracle.com>
parents: 16045
diff changeset
200 * Recursively accumulate any required Position declarations.
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
201 */
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
202 void generatePositionDeclarations(Set<String> declarations) {
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
203 matchDescriptor.generatePositionDeclarations(declarations);
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
204 }
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
205
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
206 /**
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
207 *
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
208 * @return the list of node types which are captured by name
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
209 */
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
210 public ArrayList<TypeDescriptor> capturedTypes() {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
211 return capturedTypes;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
212 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
213
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
214 public ArrayList<String> capturedNames() {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
215 return capturedNames;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
216 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
217 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
218
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
219 /**
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
220 * Set to true to enable logging to a local file during annotation processing. There's no normal
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
221 * channel for any debug messages and debugging annotation processors requires some special
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
222 * setup.
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
223 */
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
224 private static final boolean DEBUG = false;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
225
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
226 private PrintWriter log;
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
227
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
228 /**
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
229 * Logging facility for debugging the annotation processor.
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
230 */
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
231
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
232 private PrintWriter getLog() {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
233 if (log == null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
234 try {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
235 // Create the log file within the generated source directory so it's easy to find.
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
236 // /tmp isn't platform independent and java.io.tmpdir can map anywhere, particularly
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
237 // on the mac.
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
238 FileObject file = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", getClass().getSimpleName() + "log");
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
239 log = new PrintWriter(new FileWriter(file.toUri().getPath(), true));
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
240 } catch (IOException e) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
241 // Do nothing
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
242 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
243 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
244 return log;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
245 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
246
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
247 private void logMessage(String format, Object... args) {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
248 if (!DEBUG) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
249 return;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
250 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
251 PrintWriter bw = getLog();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
252 if (bw != null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
253 bw.printf(format, args);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
254 bw.flush();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
255 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
256 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
257
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
258 private void logException(Throwable t) {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
259 if (!DEBUG) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
260 return;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
261 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
262 PrintWriter bw = getLog();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
263 if (bw != null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
264 t.printStackTrace(bw);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
265 bw.flush();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
266 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
267 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
268
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
269 /**
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
270 * Bugs in an annotation processor can cause silent failure so try to report any exception
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
271 * throws as errors.
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
272 */
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
273 private void reportExceptionThrow(Element element, Throwable t) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
274 if (element != null) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
275 logMessage("throw for %s:\n", element);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
276 }
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
277 logException(t);
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
278 errorMessage(element, "Exception throw during processing: %s %s", t, Arrays.toString(Arrays.copyOf(t.getStackTrace(), 4)));
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
279 }
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
280
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
281 static class TypeDescriptor {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
282 final TypeMirror mirror;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
283
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
284 /**
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
285 * The name uses in match expressions to refer to this type.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
286 */
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
287 final String shortName;
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
288
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
289 /**
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
290 * The simple name of the {@link ValueNode} class represented by this type.
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
291 */
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
292 final String nodeClass;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
293
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
294 /**
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
295 * The package of {@link ValueNode} class represented by this type.
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
296 */
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
297 final String nodePackage;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
298
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
299 /**
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
300 * The matchable inputs of the node.
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
301 */
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
302 final String[] inputs;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
303
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
304 /**
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
305 * Should swapped variants of this match be generated. The user of the match is expected to
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
306 * compensate for any ordering differences in compare which are commutative but require
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
307 * reinterpreting the condition in that case.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
308 */
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
309 final boolean commutative;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
310
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
311 /**
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
312 * Can multiple users of this node subsume it. Constants can be swallowed into a match even
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
313 * if there are multiple users.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
314 */
15520
4cdc787681d4 add support for more nodes inputs
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15458
diff changeset
315 final boolean shareable;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
316
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
317 final Set<Element> originatingElements = new HashSet<>();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
318
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
319 TypeDescriptor(TypeMirror mirror, String shortName, String nodeClass, String nodePackage, String[] inputs, boolean commutative, boolean shareable) {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
320 this.mirror = mirror;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
321 this.shortName = shortName;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
322 this.nodeClass = nodeClass;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
323 this.nodePackage = nodePackage;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
324 this.inputs = inputs;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
325 this.commutative = commutative;
15520
4cdc787681d4 add support for more nodes inputs
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15458
diff changeset
326 this.shareable = shareable;
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
327 assert !commutative || inputs.length == 2;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
328 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
329 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
330
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
331 /**
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
332 * The types which are know for purpose of parsing MatchRule expressions.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
333 */
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
334 Map<String, TypeDescriptor> knownTypes = new HashMap<>();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
335
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
336 /**
15699
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
337 * The mapping between elements with MatchRules and the wrapper class used invoke the code
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
338 * generation after the match.
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
339 */
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
340 private Map<String, MethodInvokerItem> invokers = new LinkedHashMap<>();
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
341
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
342 private TypeDescriptor valueType;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
343
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
344 private TypeMirror matchRulesTypeMirror;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
345
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
346 private TypeMirror matchRuleTypeMirror;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
347
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
348 private TypeMirror matchableNodeTypeMirror;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
349
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
350 private TypeMirror matchableNodesTypeMirror;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
351
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
352 private void declareType(TypeMirror mirror, String shortName, String nodeClass, String nodePackage, String[] inputs, boolean commutative, boolean shareable, Element element) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
353 TypeDescriptor descriptor = new TypeDescriptor(mirror, shortName, nodeClass, nodePackage, inputs, commutative, shareable);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
354 descriptor.originatingElements.add(element);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
355 knownTypes.put(shortName, descriptor);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
356 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
357
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
358 private String findPackage(Element type) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
359 PackageElement p = processingEnv.getElementUtils().getPackageOf(type);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
360 if (p != null) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
361 return p.getQualifiedName().toString();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
362 }
21543
93c50cefb9e8 moved GraalInternalError to com.oracle.jvmci.common and renamed it to JVMCIError (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21097
diff changeset
363 throw new JVMCIError("can't find package for %s", type);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
364 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
365
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
366 class MatchDescriptor {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
367 TypeDescriptor nodeType;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
368 String name;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
369 MatchDescriptor[] inputs;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
370
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
371 MatchDescriptor(TypeDescriptor nodeType, String name, boolean forExpression) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
372 this.nodeType = nodeType;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
373 this.name = name;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
374 if (forExpression) {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
375 this.inputs = new MatchDescriptor[nodeType.inputs.length];
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
376 } else {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
377 this.inputs = new MatchDescriptor[0];
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
378 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
379 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
380
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
381 public void generatePositionDeclarations(Set<String> declarations) {
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
382 if (inputs.length == 0) {
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
383 return;
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
384 }
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
385 declarations.add(generatePositionDeclaration());
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
386 for (MatchDescriptor desc : inputs) {
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
387 desc.generatePositionDeclarations(declarations);
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
388 }
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
389 }
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
390
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
391 List<String> recurseVariants(int index) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
392 if (inputs.length == 0) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
393 return new ArrayList<>();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
394 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
395 List<String> currentVariants = inputs[index].generateVariants();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
396 if (index == inputs.length - 1) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
397 return currentVariants;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
398 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
399 List<String> subVariants = recurseVariants(index + 1);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
400 List<String> result = new ArrayList<>();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
401 for (String current : currentVariants) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
402 for (String sub : subVariants) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
403 result.add(current + ", " + sub);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
404 if (nodeType.commutative) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
405 result.add(sub + ", " + current);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
406 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
407 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
408 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
409 return result;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
410 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
411
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
412 /**
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
413 * Recursively generate all the variants of this rule pattern. Currently that just means to
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
414 * swap the inputs for commutative rules, producing all possible permutations.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
415 *
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
416 * @return a list of Strings which will construct pattern matchers for this rule.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
417 */
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
418 List<String> generateVariants() {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
419 String prefix = formatPrefix();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
420 String suffix = formatSuffix();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
421 ArrayList<String> variants = new ArrayList<>();
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
422 if (inputs.length > 0) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
423 for (String var : recurseVariants(0)) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
424 variants.add(prefix + ", " + var + suffix);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
425 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
426 } else {
15520
4cdc787681d4 add support for more nodes inputs
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15458
diff changeset
427 assert inputs.length == 0;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
428 variants.add(prefix + suffix);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
429 }
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
430
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
431 return variants;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
432 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
433
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
434 private String formatPrefix() {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
435 if (nodeType == valueType) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
436 return String.format("new MatchPattern(%s, false", name != null ? ("\"" + name + "\"") : "null");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
437 } else {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
438 return String.format("new MatchPattern(%s.class, %s", nodeType.nodeClass, name != null ? ("\"" + name + "\"") : "null");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
439 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
440 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
441
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
442 private String formatSuffix() {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
443 if (nodeType != null) {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
444 if (inputs.length != nodeType.inputs.length) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
445 return ", true)";
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
446 } else {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
447 if (nodeType.inputs.length > 0) {
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
448 return ", " + nodeType.nodeClass + "_positions, " + !nodeType.shareable + ")";
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
449 }
15520
4cdc787681d4 add support for more nodes inputs
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15458
diff changeset
450 if (nodeType.shareable) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
451 return ", false)";
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
452 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
453 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
454 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
455 return ")";
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
456 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
457
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
458 String generatePositionDeclaration() {
19405
e6637891a870 Simplify NodeClass management.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 19358
diff changeset
459 return String.format("Position[] %s_positions = MatchRuleRegistry.findPositions(%s.TYPE, new String[]{\"%s\"});", nodeType.nodeClass, nodeType.nodeClass,
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
460 String.join("\", \"", nodeType.inputs));
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
461 }
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
462 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
463
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
464 /**
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
465 * Strip the package off a class name leaving the full class name including any outer classes.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
466 */
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
467 private String fullClassName(Element element) {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
468 assert element.getKind() == ElementKind.CLASS || element.getKind() == ElementKind.INTERFACE : element;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
469 String pkg = findPackage(element);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
470 return ((TypeElement) element).getQualifiedName().toString().substring(pkg.length() + 1);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
471 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
472
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
473 private void createFiles(MatchRuleDescriptor info) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
474 String pkg = ((PackageElement) info.topDeclaringType.getEnclosingElement()).getQualifiedName().toString();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
475 Name topDeclaringClass = info.topDeclaringType.getSimpleName();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
476
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
477 String matchStatementClassName = topDeclaringClass + "_" + MatchStatementSet.class.getSimpleName();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
478 Element[] originatingElements = info.originatingElements.toArray(new Element[info.originatingElements.size()]);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
479
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
480 Types typeUtils = typeUtils();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
481 Filer filer = processingEnv.getFiler();
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
482 try (PrintWriter out = createSourceFile(pkg, matchStatementClassName, filer, originatingElements)) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
483
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
484 out.println("// CheckStyle: stop header check");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
485 out.println("// GENERATED CONTENT - DO NOT EDIT");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
486 out.println("// Source: " + topDeclaringClass + ".java");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
487 out.println("package " + pkg + ";");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
488 out.println("");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
489 out.println("import java.util.*;");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
490 out.println("import " + MatchStatementSet.class.getPackage().getName() + ".*;");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
491 out.println("import " + NodeLIRBuilder.class.getName() + ";");
16897
f90dcdbbb75e switched to using new NodeFieldIterator and NodeFieldIterable for traversing Node inputs and successors
Doug Simon <doug.simon@oracle.com>
parents: 16045
diff changeset
492 out.println("import " + Position.class.getName() + ";");
17242
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
493 for (String p : info.requiredPackages) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
494 out.println("import " + p + ".*;");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
495 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
496 out.println("");
17223
5b8f316bdb9e Using suppress warning for SPARCNodeLIRBuilder
Stefan Anzinger <stefan.anzinger@oracle.com>
parents: 17222
diff changeset
497
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
498 out.println("public class " + matchStatementClassName + " implements " + MatchStatementSet.class.getSimpleName() + " {");
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
499
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
500 out.println();
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
501
15699
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
502 // Generate declarations for the wrapper class to invoke the code generation methods.
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
503 for (MethodInvokerItem invoker : invokers.values()) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
504 StringBuilder args = new StringBuilder();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
505 StringBuilder types = new StringBuilder();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
506 int count = invoker.fields.size();
15699
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
507 int index = 0;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
508 for (VariableElement arg : invoker.fields) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
509 args.append('"');
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
510 args.append(arg.getSimpleName());
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
511 args.append('"');
15699
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
512 types.append(String.format("(%s) args[%s]", fullClassName(typeUtils.asElement(arg.asType())), index++));
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
513 if (count-- > 1) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
514 args.append(", ");
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
515 types.append(", ");
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
516 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
517 }
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
518 out.printf(" private static final String[] %s = new String[] {%s};\n", invoker.argumentsListName(), args);
15699
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
519 out.printf(" private static final class %s implements MatchGenerator {\n", invoker.wrapperClass());
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
520 out.printf(" static MatchGenerator instance = new %s();\n", invoker.wrapperClass());
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
521 out.printf(" public ComplexMatchResult match(NodeLIRBuilder builder, Object...args) {\n");
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
522 out.printf(" return ((%s) builder).%s(%s);\n", invoker.nodeLIRBuilderClass, invoker.methodName, types);
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
523 out.printf(" }\n");
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
524 out.printf(" public String getName() {\n");
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
525 out.printf(" return \"%s\";\n", invoker.methodName);
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
526 out.printf(" }\n");
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
527 out.printf(" }\n");
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
528 out.println();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
529
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
530 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
531
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
532 String desc = MatchStatement.class.getSimpleName();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
533
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
534 out.println(" public Class<? extends NodeLIRBuilder> forClass() {");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
535 out.println(" return " + topDeclaringClass + ".class;");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
536 out.println(" }");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
537 out.println();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
538 out.println(" @Override");
19405
e6637891a870 Simplify NodeClass management.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 19358
diff changeset
539 out.println(" public List<" + desc + "> statements() {");
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17242
diff changeset
540 out.println(" // Checkstyle: stop ");
15700
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
541
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
542 for (String positionDeclaration : info.positionDeclarations) {
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
543 out.println(" " + positionDeclaration);
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
544 }
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
545 out.println();
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
546
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
547 out.println(" List<" + desc + "> statements = Collections.unmodifiableList(Arrays.asList(");
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
548
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
549 int i = 0;
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
550 for (MatchRuleItem matchRule : info.matchRules) {
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
551 String comma = i == info.matchRules.size() - 1 ? "" : ",";
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
552 out.printf(" %s%s\n", matchRule.ruleBuilder(), comma);
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
553 i++;
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
554 }
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
555 out.println(" ));");
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17242
diff changeset
556 out.println(" // Checkstyle: resume");
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
557 out.println(" return statements;");
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
558 out.println(" }");
15700
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
559
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
560 out.println();
98423229008c allow overriding the NodeClass lookup when building MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15699
diff changeset
561
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
562 out.println("}");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
563 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
564
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
565 try {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
566 createProviderFile(pkg, matchStatementClassName, originatingElements);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
567 } catch (IOException e) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
568 reportExceptionThrow(info.topDeclaringType, e);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
569 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
570 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
571
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
572 private void createProviderFile(String pkg, String providerClassName, Element... originatingElements) throws IOException {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
573 String filename = "META-INF/providers/" + pkg + "." + providerClassName;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
574 FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, originatingElements);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
575 PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
576 writer.println(MatchStatementSet.class.getName());
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
577 writer.close();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
578 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
579
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
580 protected PrintWriter createSourceFile(String pkg, String relativeName, Filer filer, Element... originatingElements) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
581 try {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
582 // Ensure Unix line endings to comply with Graal code style guide checked by Checkstyle
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
583 JavaFileObject sourceFile = filer.createSourceFile(pkg + "." + relativeName, originatingElements);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
584 return new PrintWriter(sourceFile.openWriter()) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
585
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
586 @Override
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
587 public void println() {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
588 print("\n");
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
589 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
590 };
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
591 } catch (IOException e) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
592 throw new RuntimeException(e);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
593 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
594 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
595
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
596 /**
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
597 * Used to generate the MatchStatement constructor invocation.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
598 */
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
599 static class MatchRuleItem {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
600 private final String matchPattern;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
601 private final MethodInvokerItem invoker;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
602
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
603 public MatchRuleItem(String matchPattern, MethodInvokerItem invoker) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
604 this.matchPattern = matchPattern;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
605 this.invoker = invoker;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
606 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
607
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
608 /**
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
609 * @return a string which will construct the MatchStatement instance to match this pattern.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
610 */
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
611 public String ruleBuilder() {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
612 return String.format("new MatchStatement(\"%s\", %s, %s.instance, %s)", invoker.methodName, matchPattern, invoker.wrapperClass(), invoker.argumentsListName());
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
613 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
614 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
615
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
616 /**
15699
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
617 * Used to generate the wrapper class to invoke the code generation method.
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
618 */
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
619 static class MethodInvokerItem {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
620 final String methodName;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
621 final String nodeLIRBuilderClass;
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
622 final ExecutableElement method;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
623 final List<? extends VariableElement> fields;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
624
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
625 MethodInvokerItem(String methodName, String nodeLIRBuilderClass, ExecutableElement method, List<? extends VariableElement> fields) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
626 this.methodName = methodName;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
627 this.nodeLIRBuilderClass = nodeLIRBuilderClass;
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
628 this.method = method;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
629 this.fields = fields;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
630 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
631
15699
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
632 String wrapperClass() {
423bf61c9c32 use inner classes instead of reflection during matching
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15548
diff changeset
633 return "MatchGenerator_" + methodName;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
634 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
635
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
636 String argumentsListName() {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
637 return methodName + "_arguments";
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
638 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
639 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
640
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
641 static class MatchRuleDescriptor {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
642
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
643 final TypeElement topDeclaringType;
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
644 final List<MatchRuleItem> matchRules = new ArrayList<>();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
645 private final Set<Element> originatingElements = new HashSet<>();
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
646 public Set<String> positionDeclarations = new LinkedHashSet<>();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
647
17242
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
648 /**
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
649 * The set of packages which must be imported to refer the classes mention in matchRules.
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
650 */
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
651 Set<String> requiredPackages = new HashSet<>();
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
652
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
653 public MatchRuleDescriptor(TypeElement topDeclaringType) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
654 this.topDeclaringType = topDeclaringType;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
655 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
656 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
657
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
658 private static TypeElement topDeclaringType(Element element) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
659 Element enclosing = element.getEnclosingElement();
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
660 if (enclosing == null || enclosing.getKind() == ElementKind.PACKAGE) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
661 assert element.getKind() == ElementKind.CLASS || element.getKind() == ElementKind.INTERFACE;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
662 return (TypeElement) element;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
663 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
664 return topDeclaringType(enclosing);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
665 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
666
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
667 private AnnotationMirror findAnnotationMirror(Element element, TypeMirror typeMirror) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
668 for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
669 if (typeUtils().isSameType(mirror.getAnnotationType(), typeMirror)) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
670 return mirror;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
671 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
672 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
673 return null;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
674 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
675
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
676 @Override
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
677 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
678 if (roundEnv.processingOver()) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
679 return true;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
680 }
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
681
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
682 logMessage("Starting round %s\n", roundEnv);
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
683 matchRulesTypeMirror = processingEnv.getElementUtils().getTypeElement(MatchRules.class.getCanonicalName()).asType();
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
684 matchRuleTypeMirror = processingEnv.getElementUtils().getTypeElement(MatchRule.class.getCanonicalName()).asType();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
685
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
686 matchableNodeTypeMirror = processingEnv.getElementUtils().getTypeElement(MatchableNode.class.getCanonicalName()).asType();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
687 matchableNodesTypeMirror = processingEnv.getElementUtils().getTypeElement(MatchableNodes.class.getCanonicalName()).asType();
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
688
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
689 Element currentElement = null;
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
690 try {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
691 for (Element element : roundEnv.getElementsAnnotatedWith(MatchableNode.class)) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
692 logMessage("%s\n", element);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
693 processMatchableNode(element);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
694 }
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
695 for (Element element : roundEnv.getElementsAnnotatedWith(MatchableNodes.class)) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
696 logMessage("%s\n", element);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
697 processMatchableNode(element);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
698 }
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
699 // Define a TypeDescriptor for the generic node but don't enter it into the nodeTypes
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
700 // table since it shouldn't be mentioned in match rules.
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
701 TypeMirror valueTypeMirror = processingEnv.getElementUtils().getTypeElement(ValueNode.class.getName()).asType();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
702 valueType = new TypeDescriptor(valueTypeMirror, "Value", ValueNode.class.getSimpleName(), ValueNode.class.getPackage().getName(), new String[0], false, false);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
703
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
704 Map<TypeElement, MatchRuleDescriptor> map = new LinkedHashMap<>();
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
705
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
706 for (Element element : roundEnv.getElementsAnnotatedWith(MatchRule.class)) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
707 currentElement = element;
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
708 processMatchRule(map, element, findAnnotationMirror(element, matchRuleTypeMirror));
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
709 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
710 for (Element element : roundEnv.getElementsAnnotatedWith(MatchRules.class)) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
711 currentElement = element;
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
712 processMatchRule(map, element, findAnnotationMirror(element, matchRulesTypeMirror));
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
713 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
714
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
715 currentElement = null;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
716 for (MatchRuleDescriptor info : map.values()) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
717 createFiles(info);
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
718 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
719
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
720 } catch (Throwable t) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
721 reportExceptionThrow(currentElement, t);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
722 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
723
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
724 return true;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
725 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
726
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
727 /**
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
728 * Build up the type table to be used during parsing of the MatchRule.
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
729 */
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
730 private void processMatchableNode(Element element) {
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
731 if (!processedMatchableNode.contains(element)) {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
732 try {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
733 processedMatchableNode.add(element);
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
734
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
735 AnnotationMirror mirror = findAnnotationMirror(element, matchableNodesTypeMirror);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
736 if (mirror == null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
737 mirror = findAnnotationMirror(element, matchableNodeTypeMirror);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
738 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
739 if (mirror == null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
740 return;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
741 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
742 TypeElement topDeclaringType = topDeclaringType(element);
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
743 List<AnnotationMirror> mirrors = null;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
744 if (typeUtils().isSameType(mirror.getAnnotationType(), matchableNodesTypeMirror)) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
745 // Unpack the mirrors for a repeatable annotation
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
746 mirrors = getAnnotationValueList(AnnotationMirror.class, mirror, "value");
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
747 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
748 int i = 0;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
749 for (MatchableNode matchableNode : element.getAnnotationsByType(MatchableNode.class)) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
750 processMatchableNode(element, topDeclaringType, matchableNode, mirrors != null ? mirrors.get(i++) : mirror);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
751 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
752 } catch (Throwable t) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
753 reportExceptionThrow(element, t);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
754 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
755 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
756 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
757
21543
93c50cefb9e8 moved GraalInternalError to com.oracle.jvmci.common and renamed it to JVMCIError (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21097
diff changeset
758 private void processMatchableNode(Element element, TypeElement topDeclaringType, MatchableNode matchable, AnnotationMirror mirror) throws JVMCIError {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
759 logMessage("processMatchableNode %s %s %s\n", topDeclaringType, element, matchable);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
760 String nodeClass;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
761 String nodePackage;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
762 TypeMirror nodeClassMirror = null;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
763 try {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
764 matchable.nodeClass();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
765 } catch (MirroredTypeException e) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
766 nodeClassMirror = e.getTypeMirror();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
767 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
768 if (nodeClassMirror == null) {
21543
93c50cefb9e8 moved GraalInternalError to com.oracle.jvmci.common and renamed it to JVMCIError (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21097
diff changeset
769 throw new JVMCIError("Can't get mirror for node class %s", element);
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
770 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
771 if (nodeClassMirror.toString().equals(MatchableNode.class.getName())) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
772 nodeClass = topDeclaringType.getQualifiedName().toString();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
773 } else {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
774 nodeClass = nodeClassMirror.toString();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
775 }
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
776 TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(nodeClass);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
777 if (typeElement == null) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
778 errorMessage(element, mirror, "Class \"%s\" cannot be resolved to a type", nodeClass);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
779 return;
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
780 }
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
781 nodePackage = findPackage(typeElement);
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
782 assert nodeClass.startsWith(nodePackage);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
783 nodeClass = nodeClass.substring(nodePackage.length() + 1);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
784 assert nodeClass.endsWith("Node");
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
785 String shortName = nodeClass.substring(0, nodeClass.length() - 4);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
786
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
787 Types typeUtils = processingEnv.getTypeUtils();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
788 TypeElement nodeClassElement = (TypeElement) typeUtils.asElement(nodeClassMirror);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
789 for (String input : matchable.inputs()) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
790 boolean ok = false;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
791 TypeElement current = nodeClassElement;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
792 while (!ok && current != null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
793 for (Element fieldElement : ElementFilter.fieldsIn(current.getEnclosedElements())) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
794 if (fieldElement.getSimpleName().toString().equals(input)) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
795 ok = true;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
796 break;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
797 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
798 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
799 TypeMirror theSuper = current.getSuperclass();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
800 current = (TypeElement) typeUtils.asElement(theSuper);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
801 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
802 if (!ok) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
803 errorMessage(element, mirror, "Input named \"%s\" doesn't exist in %s", input, nodeClassElement.getSimpleName());
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
804 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
805 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
806
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
807 declareType(nodeClassMirror, shortName, nodeClass, nodePackage, matchable.inputs(), matchable.commutative(), matchable.shareable(), element);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
808 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
809
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
810 private void processMatchRule(Map<TypeElement, MatchRuleDescriptor> map, Element element, AnnotationMirror mirror) {
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
811 if (!processedMatchRule.contains(element)) {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
812 try {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
813 processedMatchRule.add(element);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
814
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
815 // The annotation element type should ensure this is true.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
816 assert element instanceof ExecutableElement;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
817
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
818 findMatchableNodes(element);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
819
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
820 TypeElement topDeclaringType = topDeclaringType(element);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
821 MatchRuleDescriptor info = map.get(topDeclaringType);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
822 if (info == null) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
823 info = new MatchRuleDescriptor(topDeclaringType);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
824 map.put(topDeclaringType, info);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
825 }
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
826 List<AnnotationMirror> mirrors = null;
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
827 if (typeUtils().isSameType(mirror.getAnnotationType(), matchRulesTypeMirror)) {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
828 // Unpack the mirrors for a repeatable annotation
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
829 mirrors = getAnnotationValueList(AnnotationMirror.class, mirror, "value");
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
830 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
831 int i = 0;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
832 for (MatchRule matchRule : element.getAnnotationsByType(MatchRule.class)) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
833 processMethodMatchRule((ExecutableElement) element, info, matchRule, mirrors != null ? mirrors.get(i++) : mirror);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
834 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
835 } catch (Throwable t) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
836 reportExceptionThrow(element, t);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
837 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
838 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
839 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
840
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
841 /**
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
842 * Search the super types of element for MatchableNode definitions. Any superclass or super
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
843 * interface can contain definitions of matchable nodes.
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
844 *
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
845 * @param element
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
846 */
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
847 private void findMatchableNodes(Element element) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
848 processMatchableNode(element);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
849 Element enclosing = element.getEnclosingElement();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
850 while (enclosing != null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
851 if (enclosing.getKind() == ElementKind.CLASS || enclosing.getKind() == ElementKind.INTERFACE) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
852 TypeElement current = (TypeElement) enclosing;
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
853 while (current != null) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
854 processMatchableNode(current);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
855 for (TypeMirror intf : current.getInterfaces()) {
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
856 Element interfaceElement = typeUtils().asElement(intf);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
857 processMatchableNode(interfaceElement);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
858 // Recurse
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
859 findMatchableNodes(interfaceElement);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
860 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
861 TypeMirror theSuper = current.getSuperclass();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
862 current = (TypeElement) typeUtils().asElement(theSuper);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
863 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
864 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
865 enclosing = enclosing.getEnclosingElement();
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
866 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
867 }
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
868
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
869 private Types typeUtils() {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
870 return processingEnv.getTypeUtils();
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
871 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
872
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
873 private void processMethodMatchRule(ExecutableElement method, MatchRuleDescriptor info, MatchRule matchRule, AnnotationMirror mirror) {
15547
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
874 logMessage("processMethodMatchRule %s %s\n", method, mirror);
cab432461b8b use NodeClass.Position when matching graphs, rearrange MatchableNode annotations, improve error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15521
diff changeset
875
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
876 Types typeUtils = typeUtils();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
877
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
878 if (!method.getModifiers().contains(Modifier.PUBLIC)) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
879 errorMessage(method, "MatchRule method %s must be public", method.getSimpleName());
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
880 return;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
881 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
882 if (method.getModifiers().contains(Modifier.STATIC)) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
883 errorMessage(method, "MatchRule method %s must be non-static", method.getSimpleName());
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
884 return;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
885 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
886
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
887 try {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
888 TypeMirror returnType = method.getReturnType();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
889 if (!typeUtils.isSameType(returnType, processingEnv.getElementUtils().getTypeElement(ComplexMatchResult.class.getName()).asType())) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
890 errorMessage(method, "MatchRule method return type must be %s", ComplexMatchResult.class.getName());
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
891 return;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
892 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
893
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
894 String rule = matchRule.value();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
895 RuleParser parser = new RuleParser(rule);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
896 ArrayList<TypeDescriptor> expectedTypes = parser.capturedTypes();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
897 ArrayList<String> expectedNames = parser.capturedNames();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
898 List<? extends VariableElement> actualParameters = method.getParameters();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
899 if (expectedTypes.size() + 1 < actualParameters.size()) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
900 errorMessage(method, "Too many arguments for match method %s != %s", expectedTypes.size() + 1, actualParameters.size());
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
901 return;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
902 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
903
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
904 // Walk through the parameters to the method and see if they exist in the match rule.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
905 // The order doesn't matter but only names mentioned in the rule can be used and they
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
906 // must be assignment compatible.
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
907 for (VariableElement parameter : actualParameters) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
908 String name = parameter.getSimpleName().toString();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
909 int nameIndex = expectedNames.indexOf(name);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
910 if (nameIndex == -1) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
911 errorMessage(method, "Argument \"%s\" isn't captured in the match rule", name);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
912 return;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
913 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
914 TypeMirror type = parameter.asType();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
915 if (!typeUtils.isAssignable(expectedTypes.get(nameIndex).mirror, type)) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
916 errorMessage(method, "Captured value \"%s\" of type %s is not assignable to argument of type %s", name, expectedTypes.get(nameIndex).mirror, type);
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
917 return;
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
918 }
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
919 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
920
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
921 String methodName = method.getSimpleName().toString();
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
922 MethodInvokerItem invoker = invokers.get(methodName);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
923 if (invoker == null) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
924 invoker = new MethodInvokerItem(methodName, topDeclaringType(method).getSimpleName().toString(), method, actualParameters);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
925 invokers.put(methodName, invoker);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
926 } else if (invoker.method != method) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
927 // This could be supported but it's easier if they are unique since the names
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
928 // are used in log output and snippet counters.
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
929 errorMessage(method, "Use unique method names for match methods.");
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
930 return;
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
931 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
932
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
933 Element enclosing = method.getEnclosingElement();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
934 String declaringClass = "";
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
935 String separator = "";
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
936 Set<Element> originatingElementsList = info.originatingElements;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
937 originatingElementsList.add(method);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
938 while (enclosing != null) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
939 if (enclosing.getKind() == ElementKind.CLASS || enclosing.getKind() == ElementKind.INTERFACE) {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
940 if (enclosing.getModifiers().contains(Modifier.PRIVATE)) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
941 errorMessage(method, "MatchRule cannot be declared in a private %s %s", enclosing.getKind().name().toLowerCase(), enclosing);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
942 return;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
943 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
944 originatingElementsList.add(enclosing);
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
945 declaringClass = enclosing.getSimpleName() + separator + declaringClass;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
946 separator = ".";
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
947 } else {
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
948 assert enclosing.getKind() == ElementKind.PACKAGE;
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
949 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
950 enclosing = enclosing.getEnclosingElement();
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
951 }
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
952
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
953 originatingElementsList.addAll(parser.originatingElements);
17242
15c8e24f027a Limit imports in generated matchers to only the used packages
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 17223
diff changeset
954 info.requiredPackages.addAll(parser.requiredPackages);
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
955
15548
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
956 // Accumulate any position declarations.
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
957 parser.generatePositionDeclarations(info.positionDeclarations);
23dbc25b10a8 share position computations in MatchStatements
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15547
diff changeset
958
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
959 List<String> matches = parser.generateVariants();
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
960 for (String match : matches) {
15453
100306ae985b switch MatchRule from class to method annotation and fix review feedback
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15370
diff changeset
961 info.matchRules.add(new MatchRuleItem(match, invoker));
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
962 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
963 } catch (RuleParseError e) {
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
964 errorMessage(method, mirror, e.getMessage());
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
965 }
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
966 }
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
967
16045
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
968 private void errorMessage(Element element, String format, Object... args) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
969 processingEnv.getMessager().printMessage(Kind.ERROR, String.format(format, args), element);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
970 }
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
971
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
972 private void errorMessage(Element element, AnnotationMirror mirror, String format, Object... args) {
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
973 processingEnv.getMessager().printMessage(Kind.ERROR, String.format(format, args), element, mirror);
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
974 }
a8322288e838 improve error reporting in MatchProcessor
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15966
diff changeset
975
15521
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
976 // TODO borrowed from com.oracle.truffle.dsl.processor.Utils
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
977 @SuppressWarnings("unchecked")
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
978 private static <T> List<T> getAnnotationValueList(Class<T> expectedListType, AnnotationMirror mirror, String name) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
979 List<? extends AnnotationValue> values = getAnnotationValue(List.class, mirror, name);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
980 List<T> result = new ArrayList<>();
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
981
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
982 if (values != null) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
983 for (AnnotationValue value : values) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
984 T annotationValue = resolveAnnotationValue(expectedListType, value);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
985 if (annotationValue != null) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
986 result.add(annotationValue);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
987 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
988 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
989 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
990 return result;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
991 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
992
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
993 private static <T> T getAnnotationValue(Class<T> expectedType, AnnotationMirror mirror, String name) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
994 return resolveAnnotationValue(expectedType, getAnnotationValue(mirror, name));
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
995 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
996
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
997 @SuppressWarnings({"unchecked"})
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
998 private static <T> T resolveAnnotationValue(Class<T> expectedType, AnnotationValue value) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
999 if (value == null) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1000 return null;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1001 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1002
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1003 Object unboxedValue = value.accept(new AnnotationValueVisitorImpl(), null);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1004 if (unboxedValue != null) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1005 if (expectedType == TypeMirror.class && unboxedValue instanceof String) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1006 return null;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1007 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1008 if (!expectedType.isAssignableFrom(unboxedValue.getClass())) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1009 throw new ClassCastException(unboxedValue.getClass().getName() + " not assignable from " + expectedType.getName());
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1010 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1011 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1012 return (T) unboxedValue;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1013 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1014
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1015 private static AnnotationValue getAnnotationValue(AnnotationMirror mirror, String name) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1016 ExecutableElement valueMethod = null;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1017 for (ExecutableElement method : ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1018 if (method.getSimpleName().toString().equals(name)) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1019 valueMethod = method;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1020 break;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1021 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1022 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1023
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1024 if (valueMethod == null) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1025 return null;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1026 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1027
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1028 AnnotationValue value = mirror.getElementValues().get(valueMethod);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1029 if (value == null) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1030 value = valueMethod.getDefaultValue();
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1031 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1032
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1033 return value;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1034 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1035
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1036 private static class AnnotationValueVisitorImpl extends AbstractAnnotationValueVisitor7<Object, Void> {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1037
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1038 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1039 public Object visitBoolean(boolean b, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1040 return Boolean.valueOf(b);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1041 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1042
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1043 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1044 public Object visitByte(byte b, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1045 return Byte.valueOf(b);
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1046 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1047
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1048 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1049 public Object visitChar(char c, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1050 return c;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1051 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1052
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1053 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1054 public Object visitDouble(double d, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1055 return d;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1056 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1057
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1058 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1059 public Object visitFloat(float f, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1060 return f;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1061 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1062
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1063 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1064 public Object visitInt(int i, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1065 return i;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1066 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1067
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1068 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1069 public Object visitLong(long i, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1070 return i;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1071 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1072
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1073 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1074 public Object visitShort(short s, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1075 return s;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1076 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1077
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1078 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1079 public Object visitString(String s, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1080 return s;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1081 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1082
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1083 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1084 public Object visitType(TypeMirror t, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1085 return t;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1086 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1087
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1088 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1089 public Object visitEnumConstant(VariableElement c, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1090 return c;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1091 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1092
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1093 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1094 public Object visitAnnotation(AnnotationMirror a, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1095 return a;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1096 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1097
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1098 @Override
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1099 public Object visitArray(List<? extends AnnotationValue> vals, Void p) {
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1100 return vals;
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1101 }
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1102
76213c9350ad improve annotation error reporting
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 15520
diff changeset
1103 }
15370
319deee16746 add support for matching multiple HIR nodes when lowering to LIR
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
diff changeset
1104 }