Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/TemplateMethod.java @ 20938:18c0f02fa4d2
Truffle-DSL: make type systems optional.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 14 Apr 2015 15:12:48 +0200 |
parents | 62c43fcf5be2 |
children | 476374f3fe9a |
comparison
equal
deleted
inserted
replaced
20937:37ea76052733 | 20938:18c0f02fa4d2 |
---|---|
226 return signatureSize; | 226 return signatureSize; |
227 } | 227 } |
228 | 228 |
229 public TypeSignature getTypeSignature() { | 229 public TypeSignature getTypeSignature() { |
230 TypeSignature signature = new TypeSignature(); | 230 TypeSignature signature = new TypeSignature(); |
231 signature.types.add(getReturnType().getTypeSystemType()); | 231 signature.types.add(getReturnType().getType()); |
232 for (Parameter parameter : getSignatureParameters()) { | 232 for (Parameter parameter : getSignatureParameters()) { |
233 TypeData typeData = parameter.getTypeSystemType(); | 233 TypeMirror typeData = parameter.getType(); |
234 if (typeData != null) { | 234 if (typeData != null) { |
235 signature.types.add(typeData); | 235 signature.types.add(typeData); |
236 } | 236 } |
237 } | 237 } |
238 return signature; | 238 return signature; |
248 continue; | 248 continue; |
249 } | 249 } |
250 if (signatureIndex >= signature.size()) { | 250 if (signatureIndex >= signature.size()) { |
251 break; | 251 break; |
252 } | 252 } |
253 TypeData newType = signature.get(signatureIndex++); | 253 TypeMirror newType = signature.get(signatureIndex++); |
254 if (!parameter.getTypeSystemType().equals(newType)) { | 254 if (!ElementUtils.typeEquals(newType, parameter.getType())) { |
255 replaceParameter(parameter.getLocalName(), new Parameter(parameter, newType)); | 255 replaceParameter(parameter.getLocalName(), new Parameter(parameter, newType)); |
256 } | 256 } |
257 } | 257 } |
258 } | 258 } |
259 | 259 |
276 } | 276 } |
277 return compare; | 277 return compare; |
278 } | 278 } |
279 | 279 |
280 public int compareBySignature(TemplateMethod compareMethod) { | 280 public int compareBySignature(TemplateMethod compareMethod) { |
281 final TypeSystemData typeSystem = getTemplate().getTypeSystem(); | |
282 if (typeSystem != compareMethod.getTemplate().getTypeSystem()) { | |
283 throw new IllegalStateException("Cannot compare two methods with different type systems."); | |
284 } | |
285 | |
286 List<TypeMirror> signature1 = getSignatureTypes(this); | 281 List<TypeMirror> signature1 = getSignatureTypes(this); |
287 List<TypeMirror> signature2 = getSignatureTypes(compareMethod); | 282 List<TypeMirror> signature2 = getSignatureTypes(compareMethod); |
288 | 283 |
289 int result = 0; | 284 int result = 0; |
290 for (int i = 0; i < Math.max(signature1.size(), signature2.size()); i++) { | 285 for (int i = 0; i < Math.max(signature1.size(), signature2.size()); i++) { |
291 TypeMirror t1 = i < signature1.size() ? signature1.get(i) : null; | 286 TypeMirror t1 = i < signature1.size() ? signature1.get(i) : null; |
292 TypeMirror t2 = i < signature2.size() ? signature2.get(i) : null; | 287 TypeMirror t2 = i < signature2.size() ? signature2.get(i) : null; |
293 result = compareParameter(typeSystem, t1, t2); | 288 result = ElementUtils.compareType(t1, t2); |
294 if (result != 0) { | 289 if (result != 0) { |
295 break; | 290 break; |
296 } | 291 } |
297 } | 292 } |
298 | 293 |
299 return result; | 294 return result; |
300 } | |
301 | |
302 protected static int compareParameter(TypeSystemData data, TypeMirror signature1, TypeMirror signature2) { | |
303 if (signature1 == null) { | |
304 return 1; | |
305 } else if (signature2 == null) { | |
306 return -1; | |
307 } | |
308 | |
309 if (ElementUtils.typeEquals(signature1, signature2)) { | |
310 return 0; | |
311 } | |
312 | |
313 int index1 = data.findType(signature1); | |
314 int index2 = data.findType(signature2); | |
315 if (index1 != -1 && index2 != -1) { | |
316 return index1 - index2; | |
317 } | |
318 | |
319 // TODO this version if subclass of should be improved. | |
320 if (signature1.getKind() == TypeKind.DECLARED && signature2.getKind() == TypeKind.DECLARED) { | |
321 TypeElement element1 = ElementUtils.fromTypeMirror(signature1); | |
322 TypeElement element2 = ElementUtils.fromTypeMirror(signature2); | |
323 | |
324 if (ElementUtils.getDirectSuperTypes(element1).contains(element2)) { | |
325 return -1; | |
326 } else if (ElementUtils.getDirectSuperTypes(element2).contains(element1)) { | |
327 return 1; | |
328 } | |
329 } | |
330 return ElementUtils.getSimpleName(signature1).compareTo(ElementUtils.getSimpleName(signature2)); | |
331 } | 295 } |
332 | 296 |
333 public static List<TypeMirror> getSignatureTypes(TemplateMethod method) { | 297 public static List<TypeMirror> getSignatureTypes(TemplateMethod method) { |
334 List<TypeMirror> types = new ArrayList<>(); | 298 List<TypeMirror> types = new ArrayList<>(); |
335 for (Parameter param : method.getSignatureParameters()) { | 299 for (Parameter param : method.getSignatureParameters()) { |
336 types.add(param.getType()); | 300 types.add(param.getType()); |
337 } | 301 } |
338 return types; | 302 return types; |
339 } | 303 } |
340 | 304 |
341 public static class TypeSignature implements Iterable<TypeData>, Comparable<TypeSignature> { | 305 public static class TypeSignature implements Iterable<TypeMirror> { |
342 | 306 |
343 private final List<TypeData> types; | 307 private final List<TypeMirror> types; |
344 | 308 |
345 public TypeSignature() { | 309 public TypeSignature() { |
346 this.types = new ArrayList<>(); | 310 this.types = new ArrayList<>(); |
347 } | 311 } |
348 | 312 |
349 public TypeSignature(List<TypeData> signature) { | 313 public TypeSignature(List<TypeMirror> signature) { |
350 this.types = signature; | 314 this.types = signature; |
351 } | 315 } |
352 | 316 |
353 @Override | 317 @Override |
354 public int hashCode() { | 318 public int hashCode() { |
357 | 321 |
358 public int size() { | 322 public int size() { |
359 return types.size(); | 323 return types.size(); |
360 } | 324 } |
361 | 325 |
362 public TypeData get(int index) { | 326 public TypeMirror get(int index) { |
363 return types.get(index); | 327 return types.get(index); |
364 } | |
365 | |
366 public int compareTo(TypeSignature other) { | |
367 if (this == other) { | |
368 return 0; | |
369 } else if (types.size() != other.types.size()) { | |
370 return types.size() - other.types.size(); | |
371 } else if (types.isEmpty()) { | |
372 return 0; | |
373 } | |
374 | |
375 for (int i = 0; i < types.size(); i++) { | |
376 TypeData type1 = types.get(i); | |
377 TypeData type2 = other.types.get(i); | |
378 | |
379 int comparison = type1.compareTo(type2); | |
380 if (comparison != 0) { | |
381 return comparison; | |
382 } | |
383 } | |
384 | |
385 return 0; | |
386 } | 328 } |
387 | 329 |
388 @Override | 330 @Override |
389 public boolean equals(Object obj) { | 331 public boolean equals(Object obj) { |
390 if (obj instanceof TypeSignature) { | 332 if (obj instanceof TypeSignature) { |
391 return ((TypeSignature) obj).types.equals(types); | 333 return ((TypeSignature) obj).types.equals(types); |
392 } | 334 } |
393 return super.equals(obj); | 335 return super.equals(obj); |
394 } | 336 } |
395 | 337 |
396 public Iterator<TypeData> iterator() { | 338 public Iterator<TypeMirror> iterator() { |
397 return types.iterator(); | 339 return types.iterator(); |
398 } | 340 } |
399 | 341 |
400 @Override | 342 @Override |
401 public String toString() { | 343 public String toString() { |