comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/template/TemplateMethod.java @ 11439:efe58aa92f86

Truffle-DSL: guards can now be declared using any base type or interface of the target value type.
author Christian Humer <christian.humer@gmail.com>
date Tue, 27 Aug 2013 22:08:26 +0200
parents 6879565ee10b
children 2868b55001d4
comparison
equal deleted inserted replaced
11438:b77721210bd6 11439:efe58aa92f86
276 } 276 }
277 return foundParameters; 277 return foundParameters;
278 } 278 }
279 279
280 public int compareBySignature(TemplateMethod compareMethod) { 280 public int compareBySignature(TemplateMethod compareMethod) {
281 TypeSystemData typeSystem = getTemplate().getTypeSystem(); 281 final TypeSystemData typeSystem = getTemplate().getTypeSystem();
282 if (typeSystem != compareMethod.getTemplate().getTypeSystem()) { 282 if (typeSystem != compareMethod.getTemplate().getTypeSystem()) {
283 throw new IllegalStateException("Cannot compare two methods with different type systems."); 283 throw new IllegalStateException("Cannot compare two methods with different type systems.");
284 } 284 }
285 285
286 Signature signature1 = getSignature(); 286 List<TypeMirror> signature1 = getSignatureTypes();
287 Signature signature2 = compareMethod.getSignature(); 287 List<TypeMirror> signature2 = compareMethod.getSignatureTypes();
288 if (signature1.size() != signature2.size()) { 288 if (signature1.size() != signature2.size()) {
289 return signature2.size() - signature1.size(); 289 return signature2.size() - signature1.size();
290 } 290 }
291 291
292 int result = 0; 292 int result = 0;
293 for (int i = 1; i < signature1.size(); i++) { 293 for (int i = 1; i < signature1.size(); i++) {
294 int typeResult = compareActualParameter(typeSystem, signature1.get(i), signature2.get(i)); 294 TypeMirror t1 = signature1.get(i);
295 TypeMirror t2 = signature2.get(i);
296
297 int typeResult = compareParameter(typeSystem, t1, t2);
295 if (result == 0) { 298 if (result == 0) {
296 result = typeResult; 299 result = typeResult;
297 } else if (typeResult != 0 && Math.signum(result) != Math.signum(typeResult)) { 300 } else if (typeResult != 0 && Math.signum(result) != Math.signum(typeResult)) {
298 // We cannot define an order. 301 // We cannot define an order.
299 return 0; 302 return 0;
300 } 303 }
301 } 304 }
302 if (result == 0 && signature1.size() > 0) { 305 if (result == 0 && signature1.size() > 0) {
303 result = compareActualParameter(typeSystem, signature1.get(0), signature2.get(0)); 306 result = compareParameter(typeSystem, signature1.get(0), signature2.get(0));
304 } 307 }
305 308
306 return result; 309 return result;
307 } 310 }
308 311
309 private static int compareActualParameter(TypeSystemData typeSystem, TypeData t1, TypeData t2) { 312 private static int compareParameter(TypeSystemData data, TypeMirror signature1, TypeMirror signature2) {
310 int index1 = typeSystem.findType(t1); 313 if (Utils.typeEquals(signature1, signature2)) {
311 int index2 = typeSystem.findType(t2); 314 return 0;
312 return index1 - index2; 315 }
316
317 int index1 = data.findType(signature1);
318 int index2 = data.findType(signature2);
319 if (index1 != -1 && index2 != -1) {
320 return index1 - index2;
321 }
322
323 if (signature1.getKind() == TypeKind.DECLARED && signature2.getKind() == TypeKind.DECLARED) {
324 TypeElement element1 = Utils.fromTypeMirror(signature1);
325 TypeElement element2 = Utils.fromTypeMirror(signature2);
326
327 if (Utils.getDirectSuperTypes(element1).contains(element2)) {
328 return -1;
329 } else if (Utils.getDirectSuperTypes(element2).contains(element1)) {
330 return 1;
331 }
332 }
333 return Utils.getSimpleName(signature1).compareTo(Utils.getSimpleName(signature2));
334 }
335
336 public List<TypeMirror> getSignatureTypes() {
337 List<TypeMirror> types = new ArrayList<>();
338 for (ActualParameter param : getReturnTypeAndParameters()) {
339 if (param.getSpecification().isSignature()) {
340 types.add(param.getType());
341 }
342 }
343 return types;
313 } 344 }
314 345
315 public static class Signature implements Iterable<TypeData>, Comparable<Signature> { 346 public static class Signature implements Iterable<TypeData>, Comparable<Signature> {
316 347
317 final List<TypeData> types; 348 final List<TypeData> types;