comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/node/NodeMethodParser.java @ 10597:79041ab43660

Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Jul 2013 20:58:32 +0200
parents
children 7a8835ec5e7d
comparison
equal deleted inserted replaced
10596:f43eb2f1bbbc 10597:79041ab43660
1 /*
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.dsl.processor.node;
24
25 import java.util.*;
26
27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*;
29
30 import com.oracle.truffle.dsl.processor.*;
31 import com.oracle.truffle.dsl.processor.node.NodeChildData.*;
32 import com.oracle.truffle.dsl.processor.template.*;
33
34 public abstract class NodeMethodParser<E extends TemplateMethod> extends TemplateMethodParser<NodeData, E> {
35
36 public NodeMethodParser(ProcessorContext context, NodeData node) {
37 super(context, node);
38 }
39
40 public NodeData getNode() {
41 return template;
42 }
43
44 @SuppressWarnings("unused")
45 protected ParameterSpec createValueParameterSpec(String valueName, NodeData nodeData, int evaluatedCount) {
46 ParameterSpec spec = new ParameterSpec(valueName, nodeTypeMirrors(nodeData));
47 spec.setSignature(true);
48 return spec;
49 }
50
51 protected List<TypeMirror> nodeTypeMirrors(NodeData nodeData) {
52 Set<TypeMirror> typeMirrors = new LinkedHashSet<>();
53
54 for (ExecutableTypeData typeData : nodeData.getExecutableTypes()) {
55 typeMirrors.add(typeData.getType().getPrimitiveType());
56 }
57
58 typeMirrors.add(nodeData.getTypeSystem().getGenericType());
59
60 return new ArrayList<>(typeMirrors);
61 }
62
63 protected ParameterSpec createReturnParameterSpec() {
64 return createValueParameterSpec("returnValue", getNode(), 0);
65 }
66
67 @Override
68 public boolean isParsable(ExecutableElement method) {
69 if (getAnnotationType() != null) {
70 return Utils.findAnnotationMirror(getContext().getEnvironment(), method, getAnnotationType()) != null;
71 }
72
73 return true;
74 }
75
76 @SuppressWarnings("unused")
77 protected final MethodSpec createDefaultMethodSpec(ExecutableElement method, AnnotationMirror mirror, boolean shortCircuitsEnabled, String shortCircuitName) {
78 MethodSpec methodSpec = new MethodSpec(createReturnParameterSpec());
79
80 addDefaultFrame(methodSpec);
81 addDefaultImplicitThis(method, methodSpec);
82 addDefaultFieldMethodSpec(method, methodSpec);
83 addDefaultChildren(shortCircuitsEnabled, shortCircuitName, methodSpec);
84
85 return methodSpec;
86 }
87
88 private void addDefaultChildren(boolean shortCircuitsEnabled, String shortCircuitName, MethodSpec methodSpec) {
89 // children are null when parsing executable types
90 if (getNode().getChildren() != null) {
91 for (NodeChildData child : getNode().getChildren()) {
92 if (child.getExecutionKind() == ExecutionKind.DEFAULT) {
93 ParameterSpec spec = createValueParameterSpec(child.getName(), child.getNodeData(), child.getExecuteWith().size());
94 if (child.getCardinality().isMany()) {
95 spec.setCardinality(Cardinality.MANY);
96 spec.setIndexed(true);
97 }
98 methodSpec.addRequired(spec);
99 } else if (child.getExecutionKind() == ExecutionKind.SHORT_CIRCUIT) {
100 String valueName = child.getName();
101 if (shortCircuitName != null && valueName.equals(shortCircuitName)) {
102 break;
103 }
104
105 if (shortCircuitsEnabled) {
106 methodSpec.addRequired(new ParameterSpec(shortCircuitValueName(valueName), getContext().getType(boolean.class)));
107 }
108 methodSpec.addRequired(createValueParameterSpec(valueName, child.getNodeData(), child.getExecuteWith().size()));
109 } else {
110 assert false;
111 }
112 }
113 }
114 }
115
116 private void addDefaultFrame(MethodSpec methodSpec) {
117 if (getNode().supportsFrame()) {
118 methodSpec.addOptional(new ParameterSpec("frame", getContext().getTruffleTypes().getFrame()));
119 }
120 }
121
122 protected void addDefaultFieldMethodSpec(ExecutableElement method, MethodSpec methodSpec) {
123 for (NodeFieldData field : getNode().getFields()) {
124 if (!Utils.isFieldAccessible(method, field.getVariable())) {
125 ParameterSpec spec = new ParameterSpec(field.getName(), field.getType());
126 spec.setLocal(true);
127 methodSpec.addOptional(spec);
128 }
129 }
130 }
131
132 protected void addDefaultImplicitThis(ExecutableElement method, MethodSpec methodSpec) {
133 TypeMirror declaredType = Utils.findNearestEnclosingType(method).asType();
134
135 if (!method.getModifiers().contains(Modifier.STATIC) && !Utils.isAssignable(getContext(), declaredType, getContext().getTruffleTypes().getNode())) {
136 methodSpec.addImplicitRequiredType(getNode().getTemplateType().asType());
137 }
138 }
139
140 private static String shortCircuitValueName(String valueName) {
141 return "has" + Utils.firstLetterUpperCase(valueName);
142 }
143
144 }