comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/NodeMethodParser.java @ 16759:23415229349b

Truffle-DSL: new package structure.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Aug 2014 15:57:14 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/node/NodeMethodParser.java@e8ef44830b50
children 0370880ac9ce
comparison
equal deleted inserted replaced
16758:c5f8eeb3cbc8 16759:23415229349b
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.parser;
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.java.*;
32 import com.oracle.truffle.dsl.processor.model.*;
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 protected ParameterSpec createValueParameterSpec(NodeExecutionData execution) {
45 ParameterSpec spec = new ParameterSpec(execution.getName(), nodeTypeMirrors(execution.getChild().getNodeData()));
46 spec.setExecution(execution);
47 return spec;
48 }
49
50 protected List<TypeMirror> nodeTypeMirrors(NodeData nodeData) {
51 Set<TypeMirror> typeMirrors = new LinkedHashSet<>();
52
53 for (ExecutableTypeData typeData : nodeData.getExecutableTypes()) {
54 typeMirrors.add(typeData.getType().getPrimitiveType());
55 }
56
57 typeMirrors.add(nodeData.getTypeSystem().getGenericType());
58
59 return new ArrayList<>(typeMirrors);
60 }
61
62 protected ParameterSpec createReturnParameterSpec() {
63 ParameterSpec returnValue = new ParameterSpec("returnValue", nodeTypeMirrors(getNode()));
64 returnValue.setExecution(getNode().getThisExecution());
65 return returnValue;
66 }
67
68 @Override
69 public boolean isParsable(ExecutableElement method) {
70 if (getAnnotationType() != null) {
71 return ElementUtils.findAnnotationMirror(getContext().getEnvironment(), method, getAnnotationType()) != null;
72 }
73
74 return true;
75 }
76
77 @SuppressWarnings("unused")
78 protected final MethodSpec createDefaultMethodSpec(ExecutableElement method, AnnotationMirror mirror, boolean shortCircuitsEnabled, String shortCircuitName) {
79 MethodSpec methodSpec = new MethodSpec(createReturnParameterSpec());
80
81 addDefaultFrame(methodSpec);
82 addDefaultFieldMethodSpec(methodSpec);
83 addDefaultChildren(shortCircuitsEnabled, shortCircuitName, methodSpec);
84
85 return methodSpec;
86 }
87
88 private void addDefaultChildren(boolean shortCircuitsEnabled, String breakName, MethodSpec spec) {
89 if (getNode().getChildren() == null) {
90 // children are null when parsing executable types
91 return;
92 }
93
94 for (NodeExecutionData execution : getNode().getChildExecutions()) {
95 if (breakName != null && execution.getShortCircuitId().equals(breakName)) {
96 break;
97 }
98
99 if (execution.isShortCircuit() && shortCircuitsEnabled) {
100 spec.addRequired(new ParameterSpec(shortCircuitValueName(execution.getName()), getContext().getType(boolean.class)));
101 }
102 spec.addRequired(createValueParameterSpec(execution));
103 }
104 }
105
106 private void addDefaultFrame(MethodSpec methodSpec) {
107 if (getNode().supportsFrame()) {
108 methodSpec.addOptional(new ParameterSpec("frame", getContext().getTruffleTypes().getFrame()));
109 }
110 }
111
112 protected void addDefaultFieldMethodSpec(MethodSpec methodSpec) {
113 for (NodeFieldData field : getNode().getFields()) {
114 if (field.getGetter() == null) {
115 ParameterSpec spec = new ParameterSpec(field.getName(), field.getType());
116 spec.setLocal(true);
117 methodSpec.addOptional(spec);
118 }
119 }
120 }
121
122 private static String shortCircuitValueName(String valueName) {
123 return "has" + ElementUtils.firstLetterUpperCase(valueName);
124 }
125
126 }