annotate graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/node/NodeCodeGenerator.java @ 9216:8b9ea2f5c36e

Removed guards from NodeData.
author Christian Humer <christian.humer@gmail.com>
date Mon, 08 Apr 2013 18:28:41 +0200
parents bd067a48a9c2
children 61ba6fc21ba4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1 /*
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
4 *
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
8 *
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
13 * accompanied this code).
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
14 *
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
18 *
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
21 * questions.
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
22 */
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
23 package com.oracle.truffle.codegen.processor.node;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
24
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
25 import static com.oracle.truffle.codegen.processor.Utils.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
26 import static javax.lang.model.element.Modifier.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
27
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
28 import java.util.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
29
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
30 import javax.lang.model.element.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
31 import javax.lang.model.type.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
32 import javax.lang.model.util.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
33
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
34 import com.oracle.truffle.api.codegen.*;
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
35 import com.oracle.truffle.codegen.processor.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
36 import com.oracle.truffle.codegen.processor.ast.*;
8251
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
37 import com.oracle.truffle.codegen.processor.node.NodeFieldData.ExecutionKind;
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
38 import com.oracle.truffle.codegen.processor.node.NodeFieldData.FieldKind;
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
39 import com.oracle.truffle.codegen.processor.template.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
40 import com.oracle.truffle.codegen.processor.typesystem.*;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
41
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
42 public class NodeCodeGenerator extends CompilationUnitFactory<NodeData> {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
43
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
44 private static final String THIS_NODE_LOCAL_VAR_NAME = "thisNode";
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
45
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
46 public NodeCodeGenerator(ProcessorContext context) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
47 super(context);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
48 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
49
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
50 private TypeMirror getUnexpectedValueException() {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51 return getContext().getTruffleTypes().getUnexpectedValueException();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
53
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
54 private static String factoryClassName(NodeData node) {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
55 return node.getNodeId() + "Factory";
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
56 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
57
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
58 private static String nodeSpecializationClassName(SpecializationData specialization) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
59 String nodeid = specialization.getNode().getNodeId();
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
60 if (nodeid.endsWith("Node") && !nodeid.equals("Node")) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
61 nodeid = nodeid.substring(0, nodeid.length() - 4);
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
62 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
63
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
64 String name = Utils.firstLetterUpperCase(nodeid);
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
65 name += Utils.firstLetterUpperCase(specialization.getId());
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
66 name += "Node";
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
67 return name;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
68 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
69
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
70 private static String valueName(ActualParameter param) {
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
71 return param.getLocalName();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
72 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
73
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
74 private static String castValueName(ActualParameter parameter) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
75 return valueName(parameter) + "Cast";
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
76 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
77
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
78 private void addInternalValueParameters(CodeExecutableElement method, TemplateMethod specialization, boolean forceFrame) {
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
79 if (forceFrame && specialization.getSpecification().findParameterSpec("frame") != null) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
80 method.addParameter(new CodeVariableElement(getContext().getTruffleTypes().getFrame(), "frameValue"));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
81 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
82 for (ActualParameter parameter : specialization.getParameters()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
83 ParameterSpec spec = parameter.getSpecification();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
84 if (forceFrame && spec.getName().equals("frame")) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
85 continue;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
86 }
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
87 if (spec.isLocal()) {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
88 continue;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
89 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
90
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
91 method.addParameter(new CodeVariableElement(parameter.getActualType(), valueName(parameter)));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
92 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
93 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
94
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
95 private static void addInternalValueParameterNames(CodeTreeBuilder builder, TemplateMethod specialization, String unexpectedValueName, boolean forceFrame, boolean includeImplicit) {
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
96 if (forceFrame && specialization.getSpecification().findParameterSpec("frame") != null) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
97 builder.string("frameValue");
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
98 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
99 for (ActualParameter parameter : specialization.getParameters()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
100 ParameterSpec spec = parameter.getSpecification();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
101 if (forceFrame && spec.getName().equals("frame")) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
102 continue;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
103 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
104
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
105 if (!includeImplicit && (parameter.isImplicit())) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
106 continue;
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
107 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
108 if (parameter.getSpecification().isLocal()) {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
109 continue;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
110 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
111
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
112 if (unexpectedValueName != null && parameter.getLocalName().equals(unexpectedValueName)) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
113 builder.cast(parameter.getActualType(), CodeTreeBuilder.singleString("ex.getResult()"));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
114 } else {
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
115 builder.string(valueName(parameter));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
116 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
117 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
118 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
119
8662
5eeade940236 Fixed user generic signature must not match generated generic signature.
Christian Humer <christian.humer@gmail.com>
parents: 8661
diff changeset
120 private static CodeTree createTemplateMethodCall(CodeTreeBuilder parent, TemplateMethod sourceMethod, TemplateMethod targetMethod, String unexpectedValueName) {
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
121 CodeTreeBuilder builder = parent.create();
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
122
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
123 boolean castedValues = sourceMethod != targetMethod;
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
124
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
125 builder.startGroup();
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
126 ExecutableElement method = targetMethod.getMethod();
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
127 if (method == null) {
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
128 throw new IllegalStateException("Cannot call synthetic operation methods.");
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
129 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
130 TypeElement targetClass = Utils.findNearestEnclosingType(method.getEnclosingElement());
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
131 NodeData node = (NodeData) targetMethod.getTemplate();
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
132 TypeSystemData typeSystem = node.getTypeSystem();
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
133
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
134 boolean accessible = targetMethod.canBeAccessedByInstanceOf(node.getNodeType());
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
135 if (accessible) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
136 if (builder.findMethod().getModifiers().contains(STATIC)) {
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
137 if (method.getModifiers().contains(STATIC)) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
138 builder.type(targetClass.asType());
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
139 } else {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
140 builder.string(THIS_NODE_LOCAL_VAR_NAME);
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
141 }
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
142 } else {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
143 builder.string("super");
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
144 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
145 } else {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
146 if (method.getModifiers().contains(STATIC)) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
147 builder.type(targetClass.asType());
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
148 } else {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
149 ActualParameter parameter = null;
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
150 for (ActualParameter searchParameter : targetMethod.getParameters()) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
151 if (searchParameter.getSpecification().isSignature()) {
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
152 parameter = searchParameter;
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
153 break;
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
154 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
155 }
8662
5eeade940236 Fixed user generic signature must not match generated generic signature.
Christian Humer <christian.humer@gmail.com>
parents: 8661
diff changeset
156 ActualParameter sourceParameter = sourceMethod.findParameter(parameter.getLocalName());
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
157 assert parameter != null;
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
158
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
159 if (castedValues) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
160 NodeFieldData field = node.findField(parameter.getSpecification().getName());
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
161 if (field == null) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
162 builder.string(valueName(parameter));
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
163 } else {
8662
5eeade940236 Fixed user generic signature must not match generated generic signature.
Christian Humer <christian.humer@gmail.com>
parents: 8661
diff changeset
164 if (Utils.typeEquals(sourceParameter.getActualType(), parameter.getActualType())) {
5eeade940236 Fixed user generic signature must not match generated generic signature.
Christian Humer <christian.humer@gmail.com>
parents: 8661
diff changeset
165 builder.string(valueName(parameter));
5eeade940236 Fixed user generic signature must not match generated generic signature.
Christian Humer <christian.humer@gmail.com>
parents: 8661
diff changeset
166 } else {
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
167 builder.string(castValueName(parameter));
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
168 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
169 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
170 } else {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
171 builder.string(valueName(parameter));
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
172 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
173 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
174 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
175 builder.string(".");
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
176 builder.startCall(method.getSimpleName().toString());
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
177
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
178 for (ActualParameter targetParameter : targetMethod.getParameters()) {
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
179 ActualParameter valueParameter = sourceMethod.findParameter(targetParameter.getLocalName());
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
180 if (valueParameter == null) {
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
181 valueParameter = targetParameter;
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
182 }
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
183 TypeData targetType = targetParameter.getActualTypeData(typeSystem);
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
184
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
185 if (targetParameter.isImplicit() || valueParameter.isImplicit()) {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
186 continue;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
187 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
188
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
189 TypeData valueType = null;
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
190 if (valueParameter != null) {
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
191 valueType = valueParameter.getActualTypeData(typeSystem);
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
192 }
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
193
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
194 if (targetParameter.getSpecification().isLocal()) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
195 builder.startGroup();
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
196 if (builder.findMethod().getModifiers().contains(Modifier.STATIC)) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
197 builder.string(THIS_NODE_LOCAL_VAR_NAME).string(".");
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
198 } else {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
199 builder.string("this.");
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
200 }
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
201 builder.string(targetParameter.getSpecification().getName());
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
202 builder.end();
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
203 } else if (unexpectedValueName != null && targetParameter.getLocalName().equals(unexpectedValueName)) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
204 builder.string("ex.getResult()");
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
205 } else if (targetType == null || targetType.isGeneric() || (valueType != null && valueType.equalsType(targetType))) {
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
206 builder.string(valueName(targetParameter));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
207 } else {
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
208 builder.string(castValueName(targetParameter));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
209 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
210 }
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
211
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
212 builder.end().end();
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
213
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
214 return builder.getRoot();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
215 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
216
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
217 private static String genClassName(Template operation) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
218 return getSimpleName(operation.getTemplateType()) + "Gen";
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
219 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
220
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
221 private String generatedGenericMethodName(SpecializationData specialization) {
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
222 final String prefix = "generic";
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
223
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
224 if (specialization == null) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
225 return prefix;
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
226 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
227
8277
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
228 if (!specialization.getNode().needsRewrites(context)) {
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
229 return prefix;
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
230 }
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
231
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
232 SpecializationData prev = null;
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
233 for (SpecializationData current : specialization.getNode().getSpecializations()) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
234 if (specialization == current) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
235 if (prev == null || prev.isUninitialized()) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
236 return prefix;
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
237 } else {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
238 return prefix + current.getId();
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
239 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
240 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
241 prev = current;
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
242 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
243 return prefix;
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
244 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
245
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
246 private static CodeTree createCallTypeSystemMethod(ProcessorContext context, CodeTreeBuilder parent, NodeData node, String methodName, String value) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
247 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
248 startCallTypeSystemMethod(context, builder, node, methodName);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
249 builder.string(value);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
250 builder.end().end();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
251 return builder.getRoot();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
252 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
253
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7502
diff changeset
254 private static void startCallTypeSystemMethod(ProcessorContext context, CodeTreeBuilder body, NodeData node, String methodName) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
255 VariableElement singleton = TypeSystemCodeGenerator.findSingleton(context, node.getTypeSystem());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
256 assert singleton != null;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
257
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
258 body.startGroup();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
259 body.staticReference(singleton.getEnclosingElement().asType(), singleton.getSimpleName().toString());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
260 body.string(".").startCall(methodName);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
261 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
262
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
263 private CodeTree createGuardAndCast(CodeTreeBuilder parent, String conditionPrefix, SpecializationData sourceSpecialization, SpecializationData targetSpecialization, boolean castValues,
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
264 CodeTree guardedStatements, CodeTree elseStatements) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
265
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
266 NodeData node = targetSpecialization.getNode();
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
267 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
268 CodeTree implicitGuards = createImplicitGuards(parent, conditionPrefix, sourceSpecialization, targetSpecialization);
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
269 CodeTree explicitGuards = createExplicitGuards(parent, implicitGuards == null ? conditionPrefix : null, sourceSpecialization, targetSpecialization);
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
270
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
271 Set<String> valuesNeedsCast;
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
272 if (castValues) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
273 // cast all
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
274 valuesNeedsCast = null;
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
275 } else {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
276 // find out which values needs a cast
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
277 valuesNeedsCast = new HashSet<>();
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
278 for (GuardData guard : targetSpecialization.getGuards()) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
279 for (ActualParameter parameter : guard.getParameters()) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
280 NodeFieldData field = node.findField(parameter.getSpecification().getName());
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
281 if (field == null) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
282 continue;
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
283 }
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
284 TypeData typeData = parameter.getActualTypeData(node.getTypeSystem());
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
285 if (typeData != null && !typeData.isGeneric()) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
286 valuesNeedsCast.add(parameter.getLocalName());
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
287 }
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
288 }
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
289 }
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
290 }
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
291
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
292 int ifCount = 0;
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
293
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
294 if (implicitGuards != null) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
295 builder.startIf();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
296 builder.tree(implicitGuards);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
297 builder.end();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
298 builder.startBlock();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
299 ifCount++;
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
300 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
301
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
302 builder.tree(createCasts(parent, valuesNeedsCast, sourceSpecialization, targetSpecialization));
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
303
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
304 if (explicitGuards != null) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
305 builder.startIf();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
306 builder.tree(explicitGuards);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
307 builder.end();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
308 builder.startBlock();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
309 ifCount++;
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
310 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
311
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
312 if (implicitGuards == null && explicitGuards == null && conditionPrefix != null && !conditionPrefix.isEmpty()) {
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
313 builder.startIf();
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
314 builder.string(conditionPrefix);
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
315 builder.end().startBlock();
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
316 ifCount++;
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
317 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
318
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
319 builder.tree(guardedStatements);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
320
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
321 builder.end(ifCount);
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
322 if (elseStatements != null && ifCount > 0) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
323 builder.tree(elseStatements);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
324 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
325 return builder.getRoot();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
326 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
327
8662
5eeade940236 Fixed user generic signature must not match generated generic signature.
Christian Humer <christian.humer@gmail.com>
parents: 8661
diff changeset
328 private static CodeTree createExplicitGuards(CodeTreeBuilder parent, String conditionPrefix, SpecializationData valueSpecialization, SpecializationData guardedSpecialization) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
329 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
330 String andOperator = conditionPrefix != null ? conditionPrefix + " && " : "";
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
331 if (guardedSpecialization.getGuards().size() > 0) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
332 // Explicitly specified guards
8592
a80bf36c6a1e Refactor to shared template method signature comparison.
Christian Humer <christian.humer@gmail.com>
parents: 8361
diff changeset
333 for (GuardData guard : guardedSpecialization.getGuards()) {
a80bf36c6a1e Refactor to shared template method signature comparison.
Christian Humer <christian.humer@gmail.com>
parents: 8361
diff changeset
334 builder.string(andOperator);
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
335 builder.tree(createTemplateMethodCall(parent, valueSpecialization, guard, null));
8592
a80bf36c6a1e Refactor to shared template method signature comparison.
Christian Humer <christian.humer@gmail.com>
parents: 8361
diff changeset
336 andOperator = " && ";
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
337 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
338 }
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
339
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
340 return builder.isEmpty() ? null : builder.getRoot();
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
341 }
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
342
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
343 private CodeTree createCasts(CodeTreeBuilder parent, Set<String> castWhiteList, SpecializationData valueSpecialization, SpecializationData guardedSpecialization) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
344 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
345 // Implict guards based on method signature
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
346 for (ActualParameter guardedParam : guardedSpecialization.getParameters()) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
347 NodeFieldData field = guardedSpecialization.getNode().findField(guardedParam.getSpecification().getName());
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
348 if (field == null || field.getKind() == FieldKind.FIELD) {
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
349 continue;
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
350 }
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
351 ActualParameter valueParam = valueSpecialization.findParameter(guardedParam.getLocalName());
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
352
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
353 if (castWhiteList != null && !castWhiteList.contains(guardedParam.getLocalName())) {
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
354 continue;
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
355 }
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
356
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
357 CodeTree cast = createCast(parent, field, valueParam, guardedParam);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
358 if (cast == null) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
359 continue;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
360 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
361 builder.tree(cast);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
362 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
363
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
364 return builder.getRoot();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
365 }
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
366
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
367 private CodeTree createImplicitGuards(CodeTreeBuilder parent, String conditionPrefix, SpecializationData valueSpecialization, SpecializationData guardedSpecialization) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
368 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
369 // Implict guards based on method signature
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
370 String andOperator = conditionPrefix != null ? conditionPrefix + " && " : "";
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
371 for (ActualParameter guardedParam : guardedSpecialization.getParameters()) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
372 NodeFieldData field = guardedSpecialization.getNode().findField(guardedParam.getSpecification().getName());
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
373 if (field == null || field.getKind() == FieldKind.FIELD) {
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
374 continue;
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
375 }
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
376 ActualParameter valueParam = valueSpecialization.findParameter(guardedParam.getLocalName());
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
377
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
378 CodeTree implicitGuard = createImplicitGuard(builder, field, valueParam, guardedParam);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
379 if (implicitGuard == null) {
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
380 continue;
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
381 }
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
382
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
383 builder.string(andOperator);
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
384 builder.tree(implicitGuard);
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
385 andOperator = " && ";
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
386 }
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
387
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
388 return builder.isEmpty() ? null : builder.getRoot();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
389 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
390
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
391 private CodeTree createImplicitGuard(CodeTreeBuilder parent, NodeFieldData field, ActualParameter source, ActualParameter target) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
392 NodeData node = field.getNodeData();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
393 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
394
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
395 TypeData targetType = target.getActualTypeData(node.getTypeSystem());
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
396 TypeData sourceType = source.getActualTypeData(node.getTypeSystem());
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
397
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
398 if (targetType.equalsType(sourceType) || targetType.isGeneric()) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
399 return null;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
400 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
401
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
402 builder.startGroup();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
403
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
404 if (field.isShortCircuit()) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
405 ActualParameter shortCircuit = target.getPreviousParameter();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
406 assert shortCircuit != null;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
407 builder.string("(");
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
408 builder.string("!").string(valueName(shortCircuit));
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
409 builder.string(" || ");
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
410 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
411
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
412 startCallTypeSystemMethod(getContext(), builder, node, TypeSystemCodeGenerator.isTypeMethodName(target.getActualTypeData(node.getTypeSystem())));
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
413 builder.string(valueName(source));
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
414 builder.end().end(); // call
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
415
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
416 if (field.isShortCircuit()) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
417 builder.string(")");
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
418 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
419
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
420 builder.end(); // group
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
421
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
422 return builder.getRoot();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
423 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
424
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
425 private CodeTree createCast(CodeTreeBuilder parent, NodeFieldData field, ActualParameter source, ActualParameter target) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
426 NodeData node = field.getNodeData();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
427 TypeSystemData typeSystem = node.getTypeSystem();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
428
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
429 TypeData sourceType = source.getActualTypeData(typeSystem);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
430 TypeData targetType = target.getActualTypeData(typeSystem);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
431
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
432 if (targetType.equalsType(sourceType) || targetType.isGeneric()) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
433 return null;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
434 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
435
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
436 CodeTree condition = null;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
437 if (field.isShortCircuit()) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
438 ActualParameter shortCircuit = target.getPreviousParameter();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
439 assert shortCircuit != null;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
440 condition = CodeTreeBuilder.singleString(valueName(shortCircuit));
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
441 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
442
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
443 CodeTree value = createCallTypeSystemMethod(context, parent, node, TypeSystemCodeGenerator.asTypeMethodName(targetType), valueName(target));
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
444
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
445 return createLazyAssignment(parent, castValueName(target), target.getActualType(), condition, value);
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
446 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
447
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
448 /**
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
449 * <pre>
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
450 * variant1 $condition != null
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
451 *
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
452 * $type $name = defaultValue($type);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
453 * if ($condition) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
454 * $name = $value;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
455 * }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
456 *
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
457 * variant2 $condition != null
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
458 * $type $name = $value;
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
459 * </pre>
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
460 *
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
461 * .
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
462 */
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
463 private static CodeTree createLazyAssignment(CodeTreeBuilder parent, String name, TypeMirror type, CodeTree condition, CodeTree value) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
464 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
465 if (condition == null) {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
466 builder.declaration(type, name, value);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
467 } else {
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
468 builder.declaration(type, name, new CodeTreeBuilder(parent).defaultValue(type).getRoot());
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
469
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
470 builder.startIf().tree(condition).end();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
471 builder.startBlock();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
472 builder.startStatement();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
473 builder.string(name);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
474 builder.string(" = ");
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
475 builder.tree(value);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
476 builder.end(); // statement
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
477 builder.end(); // block
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
478 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
479 return builder.getRoot();
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
480 }
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
481
8251
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
482 private void emitEncounteredSynthetic(CodeTreeBuilder builder) {
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
483 builder.startThrow().startNew(getContext().getType(UnsupportedOperationException.class)).end().end();
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
484 }
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
485
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
486 @Override
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
487 protected void createChildren(NodeData node) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
488 Map<NodeData, List<TypeElement>> childTypes = new LinkedHashMap<>();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
489 if (node.getDeclaredChildren() != null && !node.getDeclaredChildren().isEmpty()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
490 for (NodeData nodeChild : node.getDeclaredChildren()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
491 NodeCodeGenerator generator = new NodeCodeGenerator(getContext());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
492 childTypes.put(nodeChild, generator.process(null, nodeChild).getEnclosedElements());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
493 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
494 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
495
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
496 if (node.getExtensionElements() != null && !node.getExtensionElements().isEmpty()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
497 NodeGenFactory factory = new NodeGenFactory(context);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
498 add(factory, node);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
499 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
500
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
501 if (node.needsFactory() || node.getNodeChildren().size() > 0) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
502 add(new NodeFactoryFactory(context, childTypes), node);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
503 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
504 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
505
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
506 private class NodeGenFactory extends ClassElementFactory<NodeData> {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
507
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
508 public NodeGenFactory(ProcessorContext context) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
509 super(context);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
510 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
511
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
512 @Override
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
513 protected CodeTypeElement create(NodeData node) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
514 CodeTypeElement clazz = createClass(node, modifiers(PUBLIC, ABSTRACT), genClassName(node), node.getTemplateType().asType(), false);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
515
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
516 for (ExecutableElement executable : ElementFilter.constructorsIn(node.getTemplateType().getEnclosedElements())) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
517 CodeExecutableElement superConstructor = createSuperConstructor(clazz, executable);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
518
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
519 if (superConstructor != null) {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7502
diff changeset
520 if (superConstructor.getParameters().size() == 1 && Utils.typeEquals(superConstructor.getParameters().get(0).asType(), node.getTemplateType().asType())) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
521 String originalName = superConstructor.getParameters().get(0).getSimpleName().toString();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
522 superConstructor.getParameters().clear();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
523 superConstructor.getParameters().add(new CodeVariableElement(clazz.asType(), originalName));
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
524 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
525 clazz.add(superConstructor);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
526 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
527 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
528
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
529 if (node.getExtensionElements() != null) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
530 clazz.getEnclosedElements().addAll(node.getExtensionElements());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
531 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
532
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
533 node.setNodeType(clazz.asType());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
534
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
535 return clazz;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
536 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
537
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
538 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
539
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
540 private class NodeFactoryFactory extends ClassElementFactory<NodeData> {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
541
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
542 private final Map<NodeData, List<TypeElement>> childTypes;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
543
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
544 public NodeFactoryFactory(ProcessorContext context, Map<NodeData, List<TypeElement>> childElements) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
545 super(context);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
546 this.childTypes = childElements;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
547 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
548
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
549 @Override
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
550 protected CodeTypeElement create(NodeData node) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
551 Modifier visibility = Utils.getVisibility(node.getTemplateType().getModifiers());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
552 CodeTypeElement clazz = createClass(node, modifiers(), factoryClassName(node), null, false);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
553 if (visibility != null) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
554 clazz.getModifiers().add(visibility);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
555 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
556 clazz.getModifiers().add(Modifier.FINAL);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
557 clazz.add(createConstructorUsingFields(modifiers(PRIVATE), clazz));
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
558 return clazz;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
559 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
560
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
561 @Override
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
562 protected void createChildren(NodeData node) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
563 CodeTypeElement clazz = getElement();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
564
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
565 Modifier createVisibility = Utils.getVisibility(clazz.getModifiers());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
566
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
567 if (node.needsFactory()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
568 createFactoryMethods(node, clazz, createVisibility);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
569
8242
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
570 if (node.getSpecializations().size() > 1) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
571 clazz.add(createCreateSpecializedMethod(node, createVisibility));
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
572 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
573
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
574 if (node.needsRewrites(context)) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
575 clazz.add(createSpecializeMethod(node));
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
576 }
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
577
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
578 if (node.getGenericSpecialization() != null) {
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
579 List<CodeExecutableElement> genericMethods = createGeneratedGenericMethod(node);
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
580 for (CodeExecutableElement method : genericMethods) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
581 clazz.add(method);
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
582 }
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
583 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
584
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
585 for (SpecializationData specialization : node.getSpecializations()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
586 add(new SpecializedNodeFactory(context), specialization);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
587 }
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
588
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
589 TypeMirror nodeFactory = getContext().getEnvironment().getTypeUtils().getDeclaredType(Utils.fromTypeMirror(getContext().getType(NodeFactory.class)), node.getNodeType());
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
590 clazz.getImplements().add(nodeFactory);
7859
3c68170fc9b0 Fixed a visibility bug in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7858
diff changeset
591 clazz.add(createCreateNodeMethod(node));
3c68170fc9b0 Fixed a visibility bug in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7858
diff changeset
592 clazz.add(createCreateNodeSpecializedMethod(node));
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
593 clazz.add(createGetNodeClassMethod(node));
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
594 clazz.add(createGetNodeSignaturesMethod(node));
8248
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
595 clazz.add(createGetChildrenSignatureMethod(node));
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
596 clazz.add(createGetInstanceMethod(node, createVisibility));
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
597 clazz.add(createInstanceConstant(node, clazz.asType()));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
598 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
599
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
600 for (NodeData childNode : childTypes.keySet()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
601 if (childNode.getTemplateType().getModifiers().contains(Modifier.PRIVATE)) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
602 continue;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
603 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
604
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
605 for (TypeElement type : childTypes.get(childNode)) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
606 Set<Modifier> typeModifiers = ((CodeTypeElement) type).getModifiers();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
607 Modifier visibility = Utils.getVisibility(type.getModifiers());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
608 typeModifiers.clear();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
609 if (visibility != null) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
610 typeModifiers.add(visibility);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
611 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
612
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
613 typeModifiers.add(Modifier.STATIC);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
614 typeModifiers.add(Modifier.FINAL);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
615 clazz.add(type);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
616 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
617 }
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
618
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
619 List<NodeData> children = node.getNodeChildren();
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
620 if (node.getParent() == null && children.size() > 0) {
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
621 clazz.add(createGetFactories(node));
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
622 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
623
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
624 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
625
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
626 private CodeExecutableElement createGetNodeClassMethod(NodeData node) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
627 Types types = getContext().getEnvironment().getTypeUtils();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
628 TypeMirror returnType = types.getDeclaredType(Utils.fromTypeMirror(getContext().getType(Class.class)), node.getNodeType());
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
629 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnType, "getNodeClass");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
630 CodeTreeBuilder builder = method.createBuilder();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
631 builder.startReturn().typeLiteral(node.getNodeType()).end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
632 return method;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
633 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
634
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
635 private CodeExecutableElement createGetNodeSignaturesMethod(NodeData node) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
636 Types types = getContext().getEnvironment().getTypeUtils();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
637 TypeElement listType = Utils.fromTypeMirror(getContext().getType(List.class));
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
638 TypeMirror classType = getContext().getType(Class.class);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
639 TypeMirror returnType = types.getDeclaredType(listType, types.getDeclaredType(listType, classType));
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
640 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnType, "getNodeSignatures");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
641 CodeTreeBuilder builder = method.createBuilder();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
642 builder.startReturn();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
643 builder.startStaticCall(getContext().getType(Arrays.class), "asList");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
644 List<ExecutableElement> constructors = findUserConstructors(node);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
645 for (ExecutableElement constructor : constructors) {
8248
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
646 builder.tree(createAsList(builder, Utils.asTypeMirrors(constructor.getParameters()), classType));
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
647 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
648 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
649 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
650 return method;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
651 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
652
8248
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
653 private CodeExecutableElement createGetChildrenSignatureMethod(NodeData node) {
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
654 Types types = getContext().getEnvironment().getTypeUtils();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
655 TypeElement listType = Utils.fromTypeMirror(getContext().getType(List.class));
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
656 TypeMirror classType = getContext().getType(Class.class);
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
657 TypeMirror nodeType = getContext().getTruffleTypes().getNode();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
658 TypeMirror wildcardNodeType = types.getWildcardType(nodeType, null);
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
659 classType = types.getDeclaredType(Utils.fromTypeMirror(classType), wildcardNodeType);
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
660 TypeMirror returnType = types.getDeclaredType(listType, classType);
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
661
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
662 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnType, "getExecutionSignature");
8248
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
663 CodeTreeBuilder builder = method.createBuilder();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
664
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
665 List<TypeMirror> signatureTypes = new ArrayList<>();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
666 assert !node.getSpecializations().isEmpty();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
667 SpecializationData data = node.getSpecializations().get(0);
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
668 for (ActualParameter parameter : data.getParameters()) {
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
669 ParameterSpec spec = parameter.getSpecification();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
670 NodeFieldData field = node.findField(spec.getName());
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
671 if (field == null || field.getKind() == FieldKind.FIELD) {
8248
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
672 continue;
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
673 }
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
674
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
675 TypeMirror type;
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
676 if (field.getKind() == FieldKind.CHILDREN && field.getType().getKind() == TypeKind.ARRAY) {
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
677 type = ((ArrayType) field.getType()).getComponentType();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
678 } else {
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
679 type = field.getType();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
680 }
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
681
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
682 signatureTypes.add(type);
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
683 }
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
684
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
685 builder.startReturn().tree(createAsList(builder, signatureTypes, classType)).end();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
686 return method;
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
687 }
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
688
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
689 private CodeTree createAsList(CodeTreeBuilder parent, List<TypeMirror> types, TypeMirror elementClass) {
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
690 CodeTreeBuilder builder = parent.create();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
691 builder.startGroup();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
692 builder.type(getContext().getType(Arrays.class));
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
693 builder.string(".<").type(elementClass).string(">");
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
694 builder.startCall("asList");
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
695 for (TypeMirror typeMirror : types) {
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
696 builder.typeLiteral(typeMirror);
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
697 }
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
698 builder.end().end();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
699 return builder.getRoot();
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
700 }
c4c3f50fa9c2 Fixes for codegen builtins support.
Christian Humer <christian.humer@gmail.com>
parents: 8245
diff changeset
701
7859
3c68170fc9b0 Fixed a visibility bug in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7858
diff changeset
702 private CodeExecutableElement createCreateNodeMethod(NodeData node) {
3c68170fc9b0 Fixed a visibility bug in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7858
diff changeset
703 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), node.getNodeType(), "createNode");
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
704 CodeVariableElement arguments = new CodeVariableElement(getContext().getType(Object.class), "arguments");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
705 method.setVarArgs(true);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
706 method.addParameter(arguments);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
707
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
708 CodeTreeBuilder builder = method.createBuilder();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
709 List<ExecutableElement> signatures = findUserConstructors(node);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
710 boolean ifStarted = false;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
711
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
712 for (ExecutableElement element : signatures) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
713 ifStarted = builder.startIf(ifStarted);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
714 builder.string("arguments.length == " + element.getParameters().size());
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
715
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
716 int index = 0;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
717 for (VariableElement param : element.getParameters()) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
718 builder.string(" && ");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
719 if (!param.asType().getKind().isPrimitive()) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
720 builder.string("(arguments[" + index + "] == null || ");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
721 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
722 builder.string("arguments[" + index + "] instanceof ");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
723 builder.type(Utils.boxType(getContext(), param.asType()));
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
724 if (!param.asType().getKind().isPrimitive()) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
725 builder.string(")");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
726 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
727 index++;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
728 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
729 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
730 builder.startBlock();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
731
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
732 builder.startReturn().startCall("create");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
733 index = 0;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
734 for (VariableElement param : element.getParameters()) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
735 builder.startGroup();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
736 builder.string("(").type(param.asType()).string(") ");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
737 builder.string("arguments[").string(String.valueOf(index)).string("]");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
738 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
739 index++;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
740 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
741 builder.end().end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
742
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
743 builder.end(); // block
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
744 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
745
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
746 builder.startElseBlock();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
747 builder.startThrow().startNew(getContext().getType(IllegalArgumentException.class));
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
748 builder.doubleQuote("Invalid create signature.");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
749 builder.end().end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
750 builder.end(); // else block
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
751 return method;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
752 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
753
7859
3c68170fc9b0 Fixed a visibility bug in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7858
diff changeset
754 private CodeExecutableElement createCreateNodeSpecializedMethod(NodeData node) {
3c68170fc9b0 Fixed a visibility bug in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7858
diff changeset
755 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), node.getNodeType(), "createNodeSpecialized");
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
756 CodeVariableElement nodeParam = new CodeVariableElement(node.getNodeType(), "thisNode");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
757 CodeVariableElement arguments = new CodeVariableElement(getContext().getType(Class.class), "types");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
758 method.addParameter(nodeParam);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
759 method.addParameter(arguments);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
760 method.setVarArgs(true);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
761
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
762 CodeTreeBuilder builder = method.createBuilder();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
763 if (!node.needsRewrites(getContext())) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
764 builder.startThrow().startNew(getContext().getType(UnsupportedOperationException.class)).end().end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
765 } else {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
766 builder.startIf();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
767 builder.string("types.length == 1");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
768 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
769 builder.startBlock();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
770
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
771 builder.startReturn().startCall("createSpecialized");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
772 builder.string("thisNode");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
773 builder.string("types[0]");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
774 builder.end().end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
775
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
776 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
777 builder.startElseBlock();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
778 builder.startThrow().startNew(getContext().getType(IllegalArgumentException.class));
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
779 builder.doubleQuote("Invalid createSpecialized signature.");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
780 builder.end().end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
781 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
782 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
783
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
784 return method;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
785 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
786
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
787 private ExecutableElement createGetInstanceMethod(NodeData node, Modifier visibility) {
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
788 Types types = getContext().getEnvironment().getTypeUtils();
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
789 TypeElement nodeFactoryType = Utils.fromTypeMirror(getContext().getType(NodeFactory.class));
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
790 TypeMirror returnType = types.getDeclaredType(nodeFactoryType, node.getNodeType());
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
791
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
792 CodeExecutableElement method = new CodeExecutableElement(modifiers(), returnType, "getInstance");
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
793 if (visibility != null) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
794 method.getModifiers().add(visibility);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
795 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
796 method.getModifiers().add(Modifier.STATIC);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
797
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
798 String varName = instanceVarName(node);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
799
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
800 CodeTreeBuilder builder = method.createBuilder();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
801 builder.startIf();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
802 builder.string(varName).string(" == null");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
803 builder.end().startBlock();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
804
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
805 builder.startStatement();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
806 builder.string(varName);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
807 builder.string(" = ");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
808 builder.startNew(factoryClassName(node)).end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
809 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
810
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
811 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
812 builder.startReturn().string(varName).end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
813 return method;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
814 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
815
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
816 private String instanceVarName(NodeData node) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
817 if (node.getParent() != null) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
818 return Utils.firstLetterLowerCase(factoryClassName(node)) + "Instance";
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
819 } else {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
820 return "instance";
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
821 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
822 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
823
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
824 private CodeVariableElement createInstanceConstant(NodeData node, TypeMirror factoryType) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
825 String varName = instanceVarName(node);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
826 CodeVariableElement var = new CodeVariableElement(modifiers(), factoryType, varName);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
827 var.getModifiers().add(Modifier.PRIVATE);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
828 var.getModifiers().add(Modifier.STATIC);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
829 return var;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
830 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
831
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
832 private ExecutableElement createGetFactories(NodeData node) {
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
833 List<NodeData> children = node.getNodeChildren();
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
834 if (node.needsFactory()) {
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
835 children.add(node);
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
836 }
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
837
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
838 List<TypeMirror> nodeTypesList = new ArrayList<>();
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
839 TypeMirror prev = null;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
840 boolean allSame = true;
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
841 for (NodeData child : children) {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
842 nodeTypesList.add(child.getNodeType());
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
843 if (prev != null && !Utils.typeEquals(child.getNodeType(), prev)) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
844 allSame = false;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
845 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
846 prev = child.getNodeType();
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
847 }
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
848 TypeMirror commonNodeSuperType = Utils.getCommonSuperType(getContext(), nodeTypesList.toArray(new TypeMirror[nodeTypesList.size()]));
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
849
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
850 Types types = getContext().getEnvironment().getTypeUtils();
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
851 TypeMirror factoryType = getContext().getType(NodeFactory.class);
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
852 TypeMirror baseType;
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
853 if (allSame) {
7858
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
854 baseType = types.getDeclaredType(Utils.fromTypeMirror(factoryType), commonNodeSuperType);
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
855 } else {
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
856 baseType = types.getDeclaredType(Utils.fromTypeMirror(factoryType), types.getWildcardType(commonNodeSuperType, null));
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
857 }
4958cbdbf360 Fixed minor bugs in NodeFactory generation.
Christian Humer <christian.humer@gmail.com>
parents: 7855
diff changeset
858 TypeMirror listType = types.getDeclaredType(Utils.fromTypeMirror(getContext().getType(List.class)), baseType);
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
859
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
860 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), listType, "getFactories");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
861
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
862 CodeTreeBuilder builder = method.createBuilder();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
863 builder.startReturn();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
864 builder.startStaticCall(getContext().getType(Arrays.class), "asList");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
865
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
866 for (NodeData child : children) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
867 builder.startGroup();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
868 NodeData childNode = child;
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
869 List<NodeData> factories = new ArrayList<>();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
870 while (childNode.getParent() != null) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
871 factories.add(childNode);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
872 childNode = childNode.getParent();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
873 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
874 Collections.reverse(factories);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
875 for (NodeData nodeData : factories) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
876 builder.string(factoryClassName(nodeData)).string(".");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
877 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
878 builder.string("getInstance()");
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
879 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
880 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
881 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
882 builder.end();
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
883 return method;
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
884 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
885
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
886 private void createFactoryMethods(NodeData node, CodeTypeElement clazz, Modifier createVisibility) {
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
887 List<ExecutableElement> constructors = findUserConstructors(node);
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
888 for (ExecutableElement constructor : constructors) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
889 clazz.add(createCreateMethod(node, createVisibility, constructor));
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
890 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
891 }
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
892
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
893 private List<ExecutableElement> findUserConstructors(NodeData node) {
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
894 List<ExecutableElement> constructors = new ArrayList<>();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
895 for (ExecutableElement constructor : ElementFilter.constructorsIn(Utils.fromTypeMirror(node.getNodeType()).getEnclosedElements())) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
896 if (constructor.getModifiers().contains(PRIVATE)) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
897 continue;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
898 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
899
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
900 // skip node rewrite constructor
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7502
diff changeset
901 if (constructor.getParameters().size() == 1 && typeEquals(constructor.getParameters().get(0).asType(), node.getNodeType())) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
902 continue;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
903 }
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
904 constructors.add(constructor);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
905 }
7855
6e4fb0ccebb1 Generated factories implement the new NodeFactory interface.
Christian Humer <christian.humer@gmail.com>
parents: 7847
diff changeset
906 return constructors;
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
907 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
908
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
909 private CodeExecutableElement createCreateMethod(NodeData node, Modifier visibility, ExecutableElement constructor) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
910 CodeExecutableElement method = CodeExecutableElement.clone(getContext().getEnvironment(), constructor);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
911 method.setSimpleName(CodeNames.of("create"));
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
912 method.getModifiers().clear();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
913 if (visibility != null) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
914 method.getModifiers().add(visibility);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
915 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
916 method.getModifiers().add(Modifier.STATIC);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
917 method.setReturnType(node.getNodeType());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
918
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
919 CodeTreeBuilder body = method.createBuilder();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
920 body.startReturn();
8242
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
921 if (node.getSpecializations().isEmpty()) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
922 body.null_();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
923 } else {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
924 body.startNew(nodeSpecializationClassName(node.getSpecializations().get(0)));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
925 for (VariableElement var : method.getParameters()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
926 body.string(var.getSimpleName().toString());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
927 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
928 body.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
929 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
930 body.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
931 return method;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
932 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
933
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
934 private CodeExecutableElement createCreateSpecializedMethod(NodeData node, Modifier visibility) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
935 CodeExecutableElement method = new CodeExecutableElement(modifiers(), node.getNodeType(), "createSpecialized");
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
936 if (visibility != null) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
937 method.getModifiers().add(visibility);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
938 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
939 method.getModifiers().add(Modifier.STATIC);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
940
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
941 method.addParameter(new CodeVariableElement(node.getNodeType(), THIS_NODE_LOCAL_VAR_NAME));
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
942 method.addParameter(new CodeVariableElement(getContext().getType(Class.class), "specializationClass"));
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
943
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
944 CodeTreeBuilder body = method.createBuilder();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
945 boolean first = true;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
946 for (TypeData type : node.getTypeSystem().getTypes()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
947 SpecializationData specialization = node.findUniqueSpecialization(type);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
948 if (specialization != null && !type.isGeneric()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
949 if (first) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
950 body.startIf();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
951 first = false;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
952 } else {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
953 body.startElseIf();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
954 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
955 body.string("specializationClass == ").type(type.getBoxedType()).string(".class").end().startBlock();
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
956 body.startReturn().startNew(nodeSpecializationClassName(specialization));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
957 body.string(THIS_NODE_LOCAL_VAR_NAME);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
958 body.end().end(); // new, return
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
959
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
960 body.end(); // if
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
961 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
962 }
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
963 body.startReturn().startNew(nodeSpecializationClassName(node.getGenericSpecialization()));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
964 body.string(THIS_NODE_LOCAL_VAR_NAME);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
965 body.end().end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
966 return method;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
967 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
968
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
969 private CodeExecutableElement createSpecializeMethod(NodeData node) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
970 CodeExecutableElement method = new CodeExecutableElement(modifiers(PRIVATE, STATIC), node.getNodeType(), "specialize");
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
971 method.addParameter(new CodeVariableElement(node.getNodeType(), THIS_NODE_LOCAL_VAR_NAME));
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
972 method.addParameter(new CodeVariableElement(getContext().getType(Class.class), "minimumState"));
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
973 addInternalValueParameters(method, node.getGenericSpecialization(), true);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
974
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
975 CodeTreeBuilder body = method.createBuilder();
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
976 body.startStatement().string("boolean allowed = (minimumState == ").string(nodeSpecializationClassName(node.getSpecializations().get(0))).string(".class)").end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
977
8242
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
978 for (int i = 1; i < node.getSpecializations().size(); i++) {
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
979 SpecializationData specialization = node.getSpecializations().get(i);
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
980 body.startStatement().string("allowed = allowed || (minimumState == ").string(nodeSpecializationClassName(specialization)).string(".class)").end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
981
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
982 CodeTreeBuilder guarded = new CodeTreeBuilder(body);
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
983 guarded.startReturn().startNew(nodeSpecializationClassName(specialization));
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
984 guarded.string(THIS_NODE_LOCAL_VAR_NAME);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
985 guarded.end().end();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
986
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
987 body.tree(createGuardAndCast(body, "allowed", node.getGenericSpecialization(), specialization, false, guarded.getRoot(), null));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
988 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
989 body.startThrow().startNew(getContext().getType(IllegalArgumentException.class)).end().end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
990
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
991 return method;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
992 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
993
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
994 private List<CodeExecutableElement> createGeneratedGenericMethod(NodeData node) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
995 TypeMirror genericReturnType = node.getGenericSpecialization().getReturnType().getActualType();
8277
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
996 if (node.needsRewrites(context)) {
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
997 List<CodeExecutableElement> methods = new ArrayList<>();
7751
ef1b41ea0a90 Implemented an option to turn off generated generic generation using specializations in @Generic.
Christian Humer <christian.humer@gmail.com>
parents: 7750
diff changeset
998
8242
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
999 List<SpecializationData> specializations = node.getSpecializations();
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1000 SpecializationData prev = null;
8242
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
1001 for (int i = 0; i < specializations.size(); i++) {
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
1002 SpecializationData current = specializations.get(i);
ac4e8c16ffdf Added new codegen api classes NodeId, NodeClass to codegen along with some refactorings.
Christian Humer <christian.humer@gmail.com>
parents: 8237
diff changeset
1003 SpecializationData next = i + 1 < specializations.size() ? specializations.get(i + 1) : null;
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1004 if (prev == null || current.isUninitialized()) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1005 prev = current;
7751
ef1b41ea0a90 Implemented an option to turn off generated generic generation using specializations in @Generic.
Christian Humer <christian.humer@gmail.com>
parents: 7750
diff changeset
1006 continue;
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1007 } else {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1008 String methodName = generatedGenericMethodName(current);
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1009 CodeExecutableElement method = new CodeExecutableElement(modifiers(PRIVATE, STATIC), genericReturnType, methodName);
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1010 method.addParameter(new CodeVariableElement(node.getNodeType(), THIS_NODE_LOCAL_VAR_NAME));
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1011 addInternalValueParameters(method, node.getGenericSpecialization(), true);
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1012
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1013 emitGeneratedGenericSpecialization(method.createBuilder(), current, next);
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1014
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1015 methods.add(method);
7751
ef1b41ea0a90 Implemented an option to turn off generated generic generation using specializations in @Generic.
Christian Humer <christian.humer@gmail.com>
parents: 7750
diff changeset
1016 }
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1017 prev = current;
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1018 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1019
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1020 return methods;
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1021 } else {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1022 CodeExecutableElement method = new CodeExecutableElement(modifiers(PRIVATE, STATIC), genericReturnType, generatedGenericMethodName(null));
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1023 method.addParameter(new CodeVariableElement(node.getNodeType(), THIS_NODE_LOCAL_VAR_NAME));
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1024 addInternalValueParameters(method, node.getGenericSpecialization(), true);
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1025 emitInvokeDoMethod(method.createBuilder(), node.getGenericSpecialization(), 0);
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1026 return Arrays.asList(method);
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1027 }
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1028 }
7751
ef1b41ea0a90 Implemented an option to turn off generated generic generation using specializations in @Generic.
Christian Humer <christian.humer@gmail.com>
parents: 7750
diff changeset
1029
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1030 private void emitGeneratedGenericSpecialization(CodeTreeBuilder builder, SpecializationData current, SpecializationData next) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1031 CodeTreeBuilder invokeMethodBuilder = new CodeTreeBuilder(builder);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1032 emitInvokeDoMethod(invokeMethodBuilder, current, 0);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1033 CodeTree invokeMethod = invokeMethodBuilder.getRoot();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1034
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1035 if (next != null) {
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
1036 CodeTreeBuilder nextBuilder = builder.create();
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
1037
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
1038 nextBuilder.startReturn().startCall(generatedGenericMethodName(next));
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
1039 nextBuilder.string(THIS_NODE_LOCAL_VAR_NAME);
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1040 addInternalValueParameterNames(nextBuilder, next, null, true, true);
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
1041 nextBuilder.end().end();
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
1042
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1043 invokeMethod = createGuardAndCast(builder, null, current.getNode().getGenericSpecialization(), current, true, invokeMethod, nextBuilder.getRoot());
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1044 }
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1045
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1046 builder.tree(invokeMethod);
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1047
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1048 if (next != null) {
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1049 builder.end();
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1050 }
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1051 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1052
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1053 private void emitInvokeDoMethod(CodeTreeBuilder builder, SpecializationData specialization, int level) {
8243
d81ff782fa1a Removed @SpecializationThrows from codegen API. Replaced it by a simplier version in @Specialization.
Christian Humer <christian.humer@gmail.com>
parents: 8242
diff changeset
1054 if (!specialization.getExceptions().isEmpty()) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1055 builder.startTryBlock();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1056 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1057
8256
4dc7034317ec Cleanup.
Christian Humer <christian.humer@gmail.com>
parents: 8252
diff changeset
1058 if (specialization.getMethod() == null) {
8251
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
1059 emitEncounteredSynthetic(builder);
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
1060 } else {
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
1061 builder.startReturn();
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1062 builder.tree(createTemplateMethodCall(builder, specialization.getNode().getGenericSpecialization(), specialization, null));
8251
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
1063 builder.end(); // return
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
1064 }
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1065
8243
d81ff782fa1a Removed @SpecializationThrows from codegen API. Replaced it by a simplier version in @Specialization.
Christian Humer <christian.humer@gmail.com>
parents: 8242
diff changeset
1066 if (!specialization.getExceptions().isEmpty()) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1067 for (SpecializationThrowsData exception : specialization.getExceptions()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1068 builder.end().startCatchBlock(exception.getJavaClass(), "ex" + level);
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1069
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1070 builder.startReturn().startCall(generatedGenericMethodName(exception.getTransitionTo()));
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1071 builder.string(THIS_NODE_LOCAL_VAR_NAME);
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1072 addInternalValueParameterNames(builder, exception.getTransitionTo(), null, true, true);
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1073 builder.end().end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1074 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1075 builder.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1076 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1077 }
8251
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
1078
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1079 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1080
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1081 private class SpecializedNodeFactory extends ClassElementFactory<SpecializationData> {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1082
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1083 public SpecializedNodeFactory(ProcessorContext context) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1084 super(context);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1085 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1086
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1087 @Override
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1088 public CodeTypeElement create(SpecializationData specialization) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1089 NodeData node = specialization.getNode();
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1090 CodeTypeElement clazz = createClass(node, modifiers(PRIVATE, STATIC, FINAL), nodeSpecializationClassName(specialization), node.getNodeType(), false);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1091 return clazz;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1092 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1093
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1094 @Override
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1095 protected void createChildren(SpecializationData specialization) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1096 CodeTypeElement clazz = getElement();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1097 NodeData node = specialization.getNode();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1098
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1099 TypeElement superTypeElement = Utils.fromTypeMirror(clazz.getSuperclass());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1100 for (ExecutableElement constructor : ElementFilter.constructorsIn(superTypeElement.getEnclosedElements())) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1101 ExecutableElement superConstructor = createSuperConstructor(clazz, constructor);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1102 if (superConstructor != null) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1103 clazz.add(superConstructor);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1104 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1105 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1106
9215
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1107 TypeData primaryType = specialization.getReturnType().getActualTypeData(node.getTypeSystem());
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1108
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1109 for (ExecutableTypeData execType : node.getExecutableTypes()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1110 if (execType.isFinal()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1111 continue;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1112 }
9215
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1113
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1114 if (primaryType == execType.getType()) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1115 CodeExecutableElement executeMethod = createExecutableTypeOverride(execType);
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1116 clazz.add(executeMethod);
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1117 executeMethod.setBodyTree(createFunctionalExecute(executeMethod.createBuilder(), specialization));
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1118 } else if (needsCastingExecuteMethod(execType, primaryType)) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1119 CodeExecutableElement executeMethod = createExecutableTypeOverride(execType);
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1120 clazz.add(executeMethod);
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1121 executeMethod.setBodyTree(createCastingExecute(executeMethod.createBuilder(), specialization, execType.getType()));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1122 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1123
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1124 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1125
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1126 if (node.needsRewrites(getContext()) && !specialization.isGeneric() && !specialization.isUninitialized()) {
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1127 buildSpecializeAndExecute(clazz, specialization);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1128 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1129 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1130
9215
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1131 private CodeExecutableElement createExecutableTypeOverride(ExecutableTypeData execType) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1132 CodeExecutableElement method = CodeExecutableElement.clone(getContext().getEnvironment(), execType.getMethod());
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1133 if (method.getParameters().size() == 1) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1134 CodeVariableElement var = CodeVariableElement.clone(method.getParameters().get(0));
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1135 var.setName("frameValue");
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1136 method.getParameters().set(0, var);
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1137 }
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1138 method.getModifiers().remove(Modifier.ABSTRACT);
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1139 return method;
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1140 }
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1141
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1142 private boolean needsCastingExecuteMethod(ExecutableTypeData execType, TypeData primaryType) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1143 if (execType.isAbstract()) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1144 return true;
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1145 }
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1146 if (Utils.isPrimitiveOrVoid(primaryType.getPrimitiveType()) && Utils.isPrimitiveOrVoid(execType.getType().getPrimitiveType())) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1147 return true;
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1148 }
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1149 if (execType.getType().isGeneric()) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1150 return true;
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1151 }
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1152 return false;
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1153 }
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1154
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1155 private CodeTree createCastingExecute(CodeTreeBuilder parent, SpecializationData specialization, TypeData type) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1156 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1157 NodeData node = specialization.getNode();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1158 TypeSystemData typeSystem = node.getTypeSystem();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1159
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1160 ExecutableTypeData castedType = node.findExecutableType(type);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1161 TypeData primaryType = specialization.getReturnType().getActualTypeData(typeSystem);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1162 ExecutableTypeData execType = specialization.getNode().findExecutableType(primaryType);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1163
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1164 boolean needsTry = execType.hasUnexpectedValue(getContext());
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1165 boolean returnVoid = type.isVoid();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1166
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1167 CodeTree primaryExecuteCall = null;
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1168
9215
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1169 CodeTreeBuilder executeBuilder = new CodeTreeBuilder(builder);
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1170 buildExecute(executeBuilder, null, null, execType);
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1171 primaryExecuteCall = executeBuilder.getRoot();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1172
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1173 if (needsTry) {
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1174 if (!returnVoid) {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1175 builder.declaration(primaryType.getPrimitiveType(), "value");
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1176 }
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1177 builder.startTryBlock();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1178
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1179 if (returnVoid) {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1180 builder.statement(primaryExecuteCall);
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1181 } else {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1182 builder.startStatement();
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1183 builder.string("value = ");
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1184 builder.tree(primaryExecuteCall);
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1185 builder.end();
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1186 }
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1187
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1188 builder.end().startCatchBlock(getUnexpectedValueException(), "ex");
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1189 if (returnVoid) {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1190 builder.string("// ignore").newLine();
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1191 } else {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1192 builder.startReturn();
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1193 builder.tree(createExpectType(node, castedType, CodeTreeBuilder.singleString("ex.getResult()")));
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1194 builder.end();
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1195 }
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1196 builder.end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1197
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1198 if (!returnVoid) {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1199 builder.startReturn();
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1200 builder.tree(createExpectType(node, castedType, CodeTreeBuilder.singleString("value")));
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1201 builder.end();
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1202 }
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1203 } else {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1204 if (returnVoid) {
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1205 builder.statement(primaryExecuteCall);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1206 } else {
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1207 builder.startReturn();
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1208 builder.tree(createExpectType(node, castedType, primaryExecuteCall));
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1209 builder.end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1210 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1211 }
9215
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1212
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1213 return builder.getRoot();
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1214 }
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1215
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1216 private CodeTree createExpectType(NodeData node, ExecutableTypeData castedType, CodeTree value) {
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1217 if (castedType == null) {
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1218 return value;
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1219 } else if (castedType.getType().isVoid()) {
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1220 return value;
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1221 } else if (castedType.getType().isGeneric()) {
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1222 return value;
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1223 }
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1224
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1225 CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1226 String targetMethodName;
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1227 if (castedType.hasUnexpectedValue(getContext())) {
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1228 targetMethodName = TypeSystemCodeGenerator.expectTypeMethodName(castedType.getType());
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1229 } else {
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1230 targetMethodName = TypeSystemCodeGenerator.asTypeMethodName(castedType.getType());
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1231 }
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1232 startCallTypeSystemMethod(getContext(), builder, node, targetMethodName);
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1233
7750
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1234 builder.tree(value);
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1235 builder.end().end();
bdcb3cc47e16 Generated nodes can now handle the case were a non generic execute methods does not throw an UVE.
Christian Humer <christian.humer@gmail.com>
parents: 7679
diff changeset
1236 return builder.getRoot();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1237 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1238
9215
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1239 private CodeTree createFunctionalExecute(CodeTreeBuilder parent, SpecializationData specialization) {
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1240 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1241 if (specialization.isUninitialized()) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1242 builder.tree(createDeoptimize(builder));
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1243 }
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1244
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1245 builder.tree(createExecuteChildren(builder, specialization));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1246
8361
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1247 CodeTree executeNode;
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1248 if (specialization.isUninitialized()) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1249 builder.tree(createSpecializeCall(builder, specialization));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1250 }
8361
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1251 executeNode = createExecute(builder, specialization);
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1252
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1253 SpecializationData next = specialization.findNextSpecialization();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1254 CodeTree returnSpecialized = null;
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1255 if (next != null) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1256 returnSpecialized = createReturnSpecializeAndExecute(builder, next, null);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1257 }
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1258 builder.tree(createGuardAndCast(builder, null, specialization, specialization, true, executeNode, returnSpecialized));
9215
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1259
bd067a48a9c2 Changed execute method generation strategy. Limited it for primitive execute methods.
Christian Humer <christian.humer@gmail.com>
parents: 8662
diff changeset
1260 return builder.getRoot();
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1261 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1262
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1263 private CodeTree createDeoptimize(CodeTreeBuilder parent) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1264 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1265 builder.startStatement();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1266 builder.startStaticCall(getContext().getTruffleTypes().getTruffleIntrinsics(), "deoptimize").end();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1267 builder.end();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1268 return builder.getRoot();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1269 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1270
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1271 private CodeTree createSpecializeCall(CodeTreeBuilder parent, SpecializationData specialization) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1272 NodeData node = specialization.getNode();
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1273
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1274 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1275 emitSpecializationListeners(builder, node);
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1276
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1277 builder.startStatement();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1278 builder.startCall("replace");
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1279 if (node.needsRewrites(getContext())) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1280 builder.startCall(factoryClassName(node), "specialize");
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1281 builder.string("this");
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1282 builder.typeLiteral(builder.findMethod().getEnclosingElement().asType());
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1283 addInternalValueParameterNames(builder, specialization, null, true, true);
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1284 builder.end(); // call replace, call specialize
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1285 } else {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1286 builder.startCall(factoryClassName(node), "createSpecialized").string("this").string("null").end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1287 }
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1288 builder.end().end();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1289 return builder.getRoot();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1290 }
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1291
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1292 private CodeTree createExecute(CodeTreeBuilder parent, SpecializationData specialization) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1293 NodeData node = specialization.getNode();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1294 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
8243
d81ff782fa1a Removed @SpecializationThrows from codegen API. Replaced it by a simplier version in @Specialization.
Christian Humer <christian.humer@gmail.com>
parents: 8242
diff changeset
1295 if (!specialization.getExceptions().isEmpty()) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1296 builder.startTryBlock();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1297 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1298
8361
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1299 if (specialization.isUninitialized()) {
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1300 String genericMethodName = generatedGenericMethodName(null);
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1301 builder.startReturn().startCall(factoryClassName(node), genericMethodName);
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1302 builder.string("this");
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1303 addInternalValueParameterNames(builder, specialization, null, true, true);
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1304 builder.end().end();
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1305 } else if (specialization.getMethod() == null && !node.needsRewrites(context)) {
8251
cb70ed101b5f Added automatic generation of generic specialization which throws unsupported operation if reached.
Christian Humer <christian.humer@gmail.com>
parents: 8248
diff changeset
1306 emitEncounteredSynthetic(builder);
8361
676fa31bd3f0 Uninitialized cases produced wrong call to throw new UnsuportedOperationException.
Christian Humer <christian.humer@gmail.com>
parents: 8310
diff changeset
1307 } else if (specialization.isGeneric()) {
8277
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1308 String genericMethodName;
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1309 if (!specialization.isUseSpecializationsForGeneric()) {
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1310 genericMethodName = generatedGenericMethodName(specialization);
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1311 } else {
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1312 genericMethodName = generatedGenericMethodName(null);
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1313 }
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1314
97ee911c4c74 Fixed behaviour of useSpecializations for Generic specializations.
Christian Humer <christian.humer@gmail.com>
parents: 8256
diff changeset
1315 builder.startReturn().startCall(factoryClassName(node), genericMethodName);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1316 builder.string("this");
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1317 addInternalValueParameterNames(builder, specialization, null, true, true);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1318 builder.end().end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1319 } else {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1320 builder.startReturn();
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1321 builder.tree(createTemplateMethodCall(builder, specialization, specialization, null));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1322 builder.end(); // return
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1323 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1324
8243
d81ff782fa1a Removed @SpecializationThrows from codegen API. Replaced it by a simplier version in @Specialization.
Christian Humer <christian.humer@gmail.com>
parents: 8242
diff changeset
1325 if (!specialization.getExceptions().isEmpty()) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1326 for (SpecializationThrowsData exception : specialization.getExceptions()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1327 builder.end().startCatchBlock(exception.getJavaClass(), "ex");
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1328 builder.tree(createReturnSpecializeAndExecute(parent, exception.getTransitionTo(), null));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1329 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1330 builder.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1331 }
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1332 return builder.getRoot();
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1333 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1334
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1335 private CodeTree createExecuteChildren(CodeTreeBuilder parent, SpecializationData specialization) {
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1336 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1337
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1338 for (ActualParameter parameter : specialization.getParameters()) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1339 NodeFieldData field = specialization.getNode().findField(parameter.getSpecification().getName());
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1340 if (field == null || field.getKind() == FieldKind.FIELD) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1341 continue;
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1342 }
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1343
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1344 buildFieldExecute(builder, specialization, parameter, field, null);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1345 }
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1346 return builder.getRoot();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1347 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1348
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1349 private void emitSpecializationListeners(CodeTreeBuilder builder, NodeData node) {
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1350 for (TemplateMethod listener : node.getSpecializationListeners()) {
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1351 builder.startStatement();
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1352 builder.tree(createTemplateMethodCall(builder, listener, listener, null));
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1353 builder.end(); // statement
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1354 }
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1355 }
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1356
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1357 private void buildFieldExecute(CodeTreeBuilder builder, SpecializationData specialization, ActualParameter param, NodeFieldData field, ActualParameter exceptionParam) {
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1358 boolean shortCircuit = startShortCircuit(builder, specialization, param, exceptionParam);
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1359 ExecutableTypeData execType = field.getNodeData().findExecutableType(param.getActualTypeData(field.getNodeData().getTypeSystem()));
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1360 boolean unexpected = execType.hasUnexpectedValue(getContext());
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1361
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1362 if (!shortCircuit && unexpected) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1363 builder.startStatement().type(param.getActualType()).string(" ").string(valueName(param)).end();
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1364 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1365
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1366 if (unexpected) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1367 builder.startTryBlock();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1368 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1369
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1370 if (!shortCircuit && !unexpected) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1371 builder.startStatement().type(param.getActualType()).string(" ").string(valueName(param)).string(" = ");
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1372 } else {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1373 builder.startStatement().string(valueName(param)).string(" = ");
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1374 }
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1375 buildExecute(builder, param, field, execType);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1376 builder.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1377
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1378 if (unexpected) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1379 builder.end().startCatchBlock(getUnexpectedValueException(), "ex");
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1380 SpecializationData generic = specialization.getNode().getGenericSpecialization();
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1381 boolean execute = false;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1382 for (ActualParameter exParam : generic.getParameters()) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1383 NodeFieldData exField = generic.getNode().findField(exParam.getSpecification().getName());
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1384 if (exField == null || field.getKind() == FieldKind.FIELD) {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1385 continue;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1386 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1387 if (execute) {
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1388 buildFieldExecute(builder, specialization.getNode().getGenericSpecialization(), exParam, exField, param);
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1389 } else if (exParam.getLocalName().equals(param.getLocalName())) {
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1390 execute = true;
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1391 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1392 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1393 builder.tree(createReturnSpecializeAndExecute(builder, specialization.findNextSpecialization(), param));
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1394 builder.end(); // catch block
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1395 }
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1396
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1397 endShortCircuit(builder, shortCircuit);
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1398 builder.newLine();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1399 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1400
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1401 private void buildExecute(CodeTreeBuilder builder, ActualParameter parameter, NodeFieldData field, ExecutableTypeData execType) {
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1402 if (field != null) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1403 Element accessElement = field.getAccessElement();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1404 if (accessElement.getKind() == ElementKind.METHOD) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1405 builder.startCall(accessElement.getSimpleName().toString()).end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1406 } else if (accessElement.getKind() == ElementKind.FIELD) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1407 builder.string("this.").string(accessElement.getSimpleName().toString());
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1408 } else {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1409 throw new AssertionError();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1410 }
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1411 if (parameter.getSpecification().isIndexed()) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1412 builder.string("[" + parameter.getIndex() + "]");
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1413 }
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1414 builder.string(".");
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1415 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1416 builder.startCall(execType.getMethodName());
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1417 if (execType.getParameters().size() == 1) {
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1418 builder.string("frameValue");
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1419 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1420 builder.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1421 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1422
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1423 private boolean startShortCircuit(CodeTreeBuilder builder, SpecializationData specialization, ActualParameter parameter, ActualParameter exceptionParam) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1424 NodeFieldData forField = specialization.getNode().findField(parameter.getSpecification().getName());
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1425 if (forField == null) {
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1426 return false;
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1427 }
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1428
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1429 if (forField.getExecutionKind() != ExecutionKind.SHORT_CIRCUIT) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1430 return false;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1431 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1432
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1433 ActualParameter shortCircuitParam = specialization.getPreviousParam(parameter);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1434
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1435 int shortCircuitIndex = 0;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1436 for (NodeFieldData field : specialization.getNode().getFields()) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1437 if (field.getExecutionKind() == ExecutionKind.SHORT_CIRCUIT) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1438 if (field == forField) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1439 break;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1440 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1441 shortCircuitIndex++;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1442 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1443 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1444
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1445 builder.startStatement().type(shortCircuitParam.getActualType()).string(" ").string(valueName(shortCircuitParam)).string(" = ");
8252
0905d796944a Refactored codegen error model to make error redirection a lot easier.
Christian Humer <christian.humer@gmail.com>
parents: 8251
diff changeset
1446 ShortCircuitData shortCircuitData = specialization.getShortCircuits().get(shortCircuitIndex);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1447
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1448 builder.tree(createTemplateMethodCall(builder, shortCircuitData, shortCircuitData, exceptionParam != null ? exceptionParam.getLocalName() : null));
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1449
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1450 builder.end(); // statement
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1451
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1452 builder.declaration(parameter.getActualType(), valueName(parameter), CodeTreeBuilder.createBuilder().defaultValue(parameter.getActualType()));
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1453 builder.startIf().string(shortCircuitParam.getLocalName()).end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1454 builder.startBlock();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1455
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1456 return true;
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1457 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1458
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1459 private void endShortCircuit(CodeTreeBuilder builder, boolean shortCircuit) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1460 if (shortCircuit) {
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1461 builder.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1462 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1463 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1464
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1465 private CodeTree createReturnSpecializeAndExecute(CodeTreeBuilder parent, SpecializationData nextSpecialization, ActualParameter exceptionParam) {
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1466 CodeTreeBuilder specializeCall = new CodeTreeBuilder(parent);
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1467 specializeCall.startCall("specializeAndExecute");
8245
703c09f8640c Implemented support for @NodeClass annotation to support builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8243
diff changeset
1468 specializeCall.string(nodeSpecializationClassName(nextSpecialization) + ".class");
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1469 addInternalValueParameterNames(specializeCall, nextSpecialization.getNode().getGenericSpecialization(), exceptionParam != null ? exceptionParam.getLocalName() : null, true, true);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1470 specializeCall.end().end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1471
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1472 CodeTreeBuilder builder = new CodeTreeBuilder(parent);
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1473 builder.startReturn();
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1474 builder.tree(specializeCall.getRoot());
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1475 builder.end();
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1476 return builder.getRoot();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1477 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1478
8237
6b74ffe38183 Implemented support for executing nodes in @Children fields.
Christian Humer <christian.humer@gmail.com>
parents: 7859
diff changeset
1479 private void buildSpecializeAndExecute(CodeTypeElement clazz, SpecializationData specialization) {
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1480 NodeData node = specialization.getNode();
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1481 TypeData returnType = specialization.getReturnType().getActualTypeData(node.getTypeSystem());
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1482 ExecutableTypeData returnExecutableType = node.findExecutableType(returnType);
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1483 boolean canThrowUnexpected = returnExecutableType == null ? true : returnExecutableType.hasUnexpectedValue(getContext());
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1484
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1485 CodeExecutableElement method = new CodeExecutableElement(modifiers(PRIVATE), returnType.getPrimitiveType(), "specializeAndExecute");
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1486 method.addParameter(new CodeVariableElement(getContext().getType(Class.class), "minimumState"));
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1487 if (canThrowUnexpected) {
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1488 method.addThrownType(getUnexpectedValueException());
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1489 }
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1490 addInternalValueParameters(method, specialization.getNode().getGenericSpecialization(), true);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1491 clazz.add(method);
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1492
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1493 CodeTreeBuilder builder = method.createBuilder();
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1494
7846
91cc98eae8ee Refactor guard creation methods are not flexible enough to handle two if guards.
Christian Humer <christian.humer@gmail.com>
parents: 7845
diff changeset
1495 builder.tree(createDeoptimize(builder));
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1496 emitSpecializationListeners(builder, specialization.getNode());
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1497
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1498 builder.startStatement();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1499 builder.startCall("replace");
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1500 builder.startCall(factoryClassName(specialization.getNode()), "specialize").string("this").string("minimumState");
8595
8a1115c92271 Implemented codegen guard definitions can now omit unused parameters.
Christian Humer <christian.humer@gmail.com>
parents: 8592
diff changeset
1501 addInternalValueParameterNames(builder, specialization.getNode().getGenericSpecialization(), null, true, true);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1502 builder.end();
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1503 builder.end(); // call replace
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1504 builder.end(); // statement
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1505
8661
e0ff5cf358a4 Fixed when specializing nodes must always call the full generic case.
Christian Humer <christian.humer@gmail.com>
parents: 8595
diff changeset
1506 String generatedMethodName;
e0ff5cf358a4 Fixed when specializing nodes must always call the full generic case.
Christian Humer <christian.humer@gmail.com>
parents: 8595
diff changeset
1507 if (specialization.getNode().getGenericSpecialization().isUseSpecializationsForGeneric()) {
e0ff5cf358a4 Fixed when specializing nodes must always call the full generic case.
Christian Humer <christian.humer@gmail.com>
parents: 8595
diff changeset
1508 generatedMethodName = generatedGenericMethodName(null);
e0ff5cf358a4 Fixed when specializing nodes must always call the full generic case.
Christian Humer <christian.humer@gmail.com>
parents: 8595
diff changeset
1509 } else {
e0ff5cf358a4 Fixed when specializing nodes must always call the full generic case.
Christian Humer <christian.humer@gmail.com>
parents: 8595
diff changeset
1510 generatedMethodName = generatedGenericMethodName(specialization.findNextSpecialization());
e0ff5cf358a4 Fixed when specializing nodes must always call the full generic case.
Christian Humer <christian.humer@gmail.com>
parents: 8595
diff changeset
1511 }
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1512 ExecutableElement generatedGeneric = clazz.getEnclosingClass().getMethod(generatedMethodName);
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1513
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1514 CodeTreeBuilder genericExecute = CodeTreeBuilder.createBuilder();
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1515 genericExecute.startCall(factoryClassName(specialization.getNode()), generatedMethodName);
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1516 genericExecute.string("this");
8310
89006c76f737 Final fields of base node can be optionally passed to builtin specialization method. And a few fixes.
Christian Humer <christian.humer@gmail.com>
parents: 8277
diff changeset
1517 addInternalValueParameterNames(genericExecute, specialization.getNode().getGenericSpecialization(), null, true, true);
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1518 genericExecute.end(); // call generated generic
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1519
7847
06a7cd6aaf00 Casting is now done on demand using local variables for explicit guards.
Christian Humer <christian.humer@gmail.com>
parents: 7846
diff changeset
1520 CodeTree genericInvocation = createExpectType(node, returnExecutableType, genericExecute.getRoot());
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1521
7777
ca51efac4d57 Fixed rewrite in generated generic did not invoke guards.
Christian Humer <christian.humer@gmail.com>
parents: 7751
diff changeset
1522 if (generatedGeneric != null && Utils.isVoid(generatedGeneric.getReturnType())) {
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1523 builder.statement(genericInvocation);
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1524
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1525 if (!Utils.isVoid(builder.findMethod().asType())) {
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1526 builder.startReturn().defaultValue(returnType.getPrimitiveType()).end();
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1527 }
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1528 } else {
7843
4969921f57b7 Renamed generated specialize to specializeAndExecute.
Christian Humer <christian.humer@gmail.com>
parents: 7799
diff changeset
1529 builder.startReturn().tree(genericInvocation).end();
7502
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1530 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1531 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1532
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1533 }
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1534
6343a09b2ec1 Codegen operation generation is inferred from the node type hierarchy.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1535 }