comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/template/MethodSpec.java @ 8595:8a1115c92271

Implemented codegen guard definitions can now omit unused parameters.
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Apr 2013 21:43:20 +0200
parents 703c09f8640c
children 61ba6fc21ba4
comparison
equal deleted inserted replaced
8594:ce6e8672f798 8595:8a1115c92271
24 24
25 import java.util.*; 25 import java.util.*;
26 26
27 import javax.lang.model.type.*; 27 import javax.lang.model.type.*;
28 28
29 import com.oracle.truffle.codegen.processor.*;
30 import com.oracle.truffle.codegen.processor.template.ParameterSpec.Cardinality;
31
29 public class MethodSpec { 32 public class MethodSpec {
30 33
31 private final List<TypeMirror> implicitTypes; 34 private final List<TypeMirror> implicitRequiredTypes = new ArrayList<>();
32 35
33 private final ParameterSpec returnType; 36 private final ParameterSpec returnType;
34 private final List<ParameterSpec> parameters; 37 private final List<ParameterSpec> optional = new ArrayList<>();
35 38 private final List<ParameterSpec> required = new ArrayList<>();
36 public MethodSpec(List<TypeMirror> prefixTypes, ParameterSpec returnType, List<ParameterSpec> parameters) { 39
37 this.implicitTypes = prefixTypes; 40 private boolean variableRequiredArguments;
41 private List<TypeDef> typeDefinitions;
42
43 public MethodSpec(ParameterSpec returnType) {
38 this.returnType = returnType; 44 this.returnType = returnType;
39 this.parameters = parameters; 45 }
40 } 46
41 47 public void setVariableRequiredArguments(boolean variableArguments) {
42 public List<TypeMirror> getImplicitTypes() { 48 this.variableRequiredArguments = variableArguments;
43 return implicitTypes; 49 }
50
51 public boolean isVariableRequiredArguments() {
52 return variableRequiredArguments;
53 }
54
55 public void addImplicitRequiredType(TypeMirror type) {
56 this.implicitRequiredTypes.add(type);
57 }
58
59 public void addOptional(ParameterSpec spec) {
60 optional.add(spec);
61 }
62
63 public void addRequired(ParameterSpec spec) {
64 required.add(spec);
65 }
66
67 public List<TypeMirror> getImplicitRequiredTypes() {
68 return implicitRequiredTypes;
44 } 69 }
45 70
46 public ParameterSpec getReturnType() { 71 public ParameterSpec getReturnType() {
47 return returnType; 72 return returnType;
48 } 73 }
49 74
50 public List<ParameterSpec> getParameters() { 75 public List<ParameterSpec> getRequired() {
51 return parameters; 76 return required;
77 }
78
79 public List<ParameterSpec> getOptional() {
80 return optional;
81 }
82
83 public void makeTypeDefinitions() {
84
85 }
86
87 public List<ParameterSpec> getAll() {
88 List<ParameterSpec> specs = new ArrayList<>();
89 specs.add(getReturnType());
90 specs.addAll(getOptional());
91 specs.addAll(getRequired());
92 return specs;
52 } 93 }
53 94
54 public ParameterSpec findParameterSpec(String name) { 95 public ParameterSpec findParameterSpec(String name) {
55 for (ParameterSpec spec : parameters) { 96 for (ParameterSpec spec : getAll()) {
56 if (spec.getName().equals(name)) { 97 if (spec.getName().equals(name)) {
57 return spec; 98 return spec;
58 } 99 }
59 } 100 }
60 return null; 101 return null;
61 } 102 }
62 103
104 public void applyTypeDefinitions(String prefix) {
105 this.typeDefinitions = createTypeDefinitions(prefix);
106 }
107
108 private List<TypeDef> createTypeDefinitions(String prefix) {
109 List<TypeDef> typeDefs = new ArrayList<>();
110
111 int defIndex = 0;
112 for (ParameterSpec spec : getAll()) {
113 List<TypeMirror> allowedTypes = spec.getAllowedTypes();
114 List<TypeMirror> types = spec.getAllowedTypes();
115 if (types != null && allowedTypes.size() > 1) {
116 TypeDef foundDef = null;
117 for (TypeDef def : typeDefs) {
118 if (allowedTypes.equals(def.getTypes())) {
119 foundDef = def;
120 break;
121 }
122 }
123 if (foundDef == null) {
124 foundDef = new TypeDef(types, prefix + defIndex);
125 typeDefs.add(foundDef);
126 defIndex++;
127 }
128
129 spec.setTypeDefinition(foundDef);
130 }
131 }
132
133 return typeDefs;
134 }
135
136 public String toSignatureString(String methodName) {
137 StringBuilder b = new StringBuilder();
138 b.append(" ");
139 b.append(createTypeSignature(returnType, true));
140
141 b.append(" ");
142 b.append(methodName);
143 b.append("(");
144
145 String sep = "";
146
147 for (ParameterSpec optionalSpec : getOptional()) {
148 b.append(sep);
149 b.append("[");
150 b.append(createTypeSignature(optionalSpec, false));
151 b.append("]");
152 sep = ", ";
153 }
154
155 for (ParameterSpec requiredSpec : getRequired()) {
156 b.append(sep);
157 if (requiredSpec.getCardinality() == Cardinality.MULTIPLE) {
158 b.append("{");
159 }
160 b.append(createTypeSignature(requiredSpec, false));
161 if (requiredSpec.getCardinality() == Cardinality.MULTIPLE) {
162 b.append("}");
163 }
164 sep = ", ";
165 }
166
167 b.append(")");
168
169 if (typeDefinitions != null && !typeDefinitions.isEmpty()) {
170 b.append("\n\n");
171
172 String lineSep = "";
173 for (TypeDef def : typeDefinitions) {
174 b.append(lineSep);
175 b.append(" <").append(def.getName()).append(">");
176 b.append(" = {");
177 String separator = "";
178 for (TypeMirror type : def.getTypes()) {
179 b.append(separator).append(Utils.getSimpleName(type));
180 separator = ", ";
181 }
182 b.append("}");
183 lineSep = "\n";
184
185 }
186 }
187 return b.toString();
188 }
189
190 private static String createTypeSignature(ParameterSpec spec, boolean typeOnly) {
191 StringBuilder builder = new StringBuilder();
192 TypeDef foundTypeDef = spec.getTypeDefinition();
193 if (foundTypeDef != null) {
194 builder.append("<" + foundTypeDef.getName() + ">");
195 } else if (spec.getAllowedTypes().size() >= 1) {
196 builder.append(Utils.getSimpleName(spec.getAllowedTypes().get(0)));
197 } else {
198 builder.append("void");
199 }
200 if (!typeOnly) {
201 builder.append(" ");
202 builder.append(spec.getName());
203 }
204 return builder.toString();
205 }
206
207 @Override
208 public String toString() {
209 return toSignatureString("methodName");
210 }
211
212 static class TypeDef {
213
214 private final List<TypeMirror> types;
215 private final String name;
216
217 public TypeDef(List<TypeMirror> types, String name) {
218 this.types = types;
219 this.name = name;
220 }
221
222 public List<TypeMirror> getTypes() {
223 return types;
224 }
225
226 public String getName() {
227 return name;
228 }
229 }
230
63 } 231 }