Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/MethodSpec.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/template/MethodSpec.java@e8ef44830b50 |
children | c88ab4f1f04a |
comparison
equal
deleted
inserted
replaced
16758:c5f8eeb3cbc8 | 16759:23415229349b |
---|---|
1 /* | |
2 * Copyright (c) 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.model; | |
24 | |
25 import java.util.*; | |
26 | |
27 import javax.lang.model.type.*; | |
28 | |
29 import com.oracle.truffle.dsl.processor.java.*; | |
30 | |
31 public class MethodSpec { | |
32 | |
33 private final ParameterSpec returnType; | |
34 private final List<ParameterSpec> optional = new ArrayList<>(); | |
35 private final List<ParameterSpec> required = new ArrayList<>(); | |
36 | |
37 private boolean ignoreAdditionalParameters; | |
38 private boolean ignoreAdditionalSpecifications; | |
39 private boolean variableRequiredParameters; | |
40 | |
41 private List<TypeDef> typeDefinitions; | |
42 | |
43 public MethodSpec(ParameterSpec returnType) { | |
44 this.returnType = returnType; | |
45 } | |
46 | |
47 public void setVariableRequiredParameters(boolean variableRequiredParameters) { | |
48 this.variableRequiredParameters = variableRequiredParameters; | |
49 } | |
50 | |
51 public boolean isVariableRequiredParameters() { | |
52 return variableRequiredParameters; | |
53 } | |
54 | |
55 public void setIgnoreAdditionalParameters(boolean ignoreAdditionalParameter) { | |
56 this.ignoreAdditionalParameters = ignoreAdditionalParameter; | |
57 } | |
58 | |
59 public boolean isIgnoreAdditionalParameters() { | |
60 return ignoreAdditionalParameters; | |
61 } | |
62 | |
63 public void addOptional(ParameterSpec spec) { | |
64 optional.add(spec); | |
65 } | |
66 | |
67 public ParameterSpec addRequired(ParameterSpec spec) { | |
68 required.add(spec); | |
69 return spec; | |
70 } | |
71 | |
72 public ParameterSpec getReturnType() { | |
73 return returnType; | |
74 } | |
75 | |
76 public List<ParameterSpec> getRequired() { | |
77 return required; | |
78 } | |
79 | |
80 public List<ParameterSpec> getOptional() { | |
81 return optional; | |
82 } | |
83 | |
84 public List<ParameterSpec> getAll() { | |
85 List<ParameterSpec> specs = new ArrayList<>(); | |
86 specs.add(getReturnType()); | |
87 specs.addAll(getOptional()); | |
88 specs.addAll(getRequired()); | |
89 return specs; | |
90 } | |
91 | |
92 public ParameterSpec findParameterSpec(String name) { | |
93 for (ParameterSpec spec : getAll()) { | |
94 if (spec.getName().equals(name)) { | |
95 return spec; | |
96 } | |
97 } | |
98 return null; | |
99 } | |
100 | |
101 public void applyTypeDefinitions(String prefix) { | |
102 this.typeDefinitions = createTypeDefinitions(prefix); | |
103 } | |
104 | |
105 private List<TypeDef> createTypeDefinitions(String prefix) { | |
106 List<TypeDef> typeDefs = new ArrayList<>(); | |
107 | |
108 int defIndex = 0; | |
109 for (ParameterSpec spec : getAll()) { | |
110 List<TypeMirror> allowedTypes = spec.getAllowedTypes(); | |
111 List<TypeMirror> types = spec.getAllowedTypes(); | |
112 if (types != null && allowedTypes.size() > 1) { | |
113 TypeDef foundDef = null; | |
114 for (TypeDef def : typeDefs) { | |
115 if (allowedTypes.equals(def.getTypes())) { | |
116 foundDef = def; | |
117 break; | |
118 } | |
119 } | |
120 if (foundDef == null) { | |
121 foundDef = new TypeDef(types, prefix + defIndex); | |
122 typeDefs.add(foundDef); | |
123 defIndex++; | |
124 } | |
125 | |
126 spec.setTypeDefinition(foundDef); | |
127 } | |
128 } | |
129 | |
130 return typeDefs; | |
131 } | |
132 | |
133 public String toSignatureString(String methodName) { | |
134 StringBuilder b = new StringBuilder(); | |
135 b.append(" "); | |
136 b.append(createTypeSignature(returnType, true)); | |
137 | |
138 b.append(" "); | |
139 b.append(methodName); | |
140 b.append("("); | |
141 | |
142 String sep = ""; | |
143 | |
144 for (ParameterSpec optionalSpec : getOptional()) { | |
145 b.append(sep); | |
146 b.append("["); | |
147 b.append(createTypeSignature(optionalSpec, false)); | |
148 b.append("]"); | |
149 sep = ", "; | |
150 } | |
151 | |
152 for (int i = 0; i < getRequired().size(); i++) { | |
153 ParameterSpec requiredSpec = getRequired().get(i); | |
154 b.append(sep); | |
155 | |
156 if (isVariableRequiredParameters() && i == getRequired().size() - 1) { | |
157 b.append(("{")); | |
158 } | |
159 b.append(createTypeSignature(requiredSpec, false)); | |
160 if (isVariableRequiredParameters() && i == getRequired().size() - 1) { | |
161 b.append(("}")); | |
162 } | |
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(ElementUtils.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(ElementUtils.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 private 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 | |
231 public void setIgnoreAdditionalSpecifications(boolean ignoreAdditoinalSpecifications) { | |
232 this.ignoreAdditionalSpecifications = ignoreAdditoinalSpecifications; | |
233 } | |
234 | |
235 public boolean isIgnoreAdditionalSpecifications() { | |
236 return ignoreAdditionalSpecifications; | |
237 } | |
238 | |
239 } |