# HG changeset patch # User Christian Humer # Date 1416846591 -3600 # Node ID 9e944c7eaded211d826f07e15ac77fcee071a185 # Parent 11bc91bcc525f2ee54a371e9b7065dd78574271f Truffle-DSL: fixed bug in source ordering when compiling with JDT. diff -r 11bc91bcc525 -r 9e944c7eaded graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/compiler/JDTCompiler.java --- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/compiler/JDTCompiler.java Mon Nov 24 16:33:02 2014 +0100 +++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/compiler/JDTCompiler.java Mon Nov 24 17:29:51 2014 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,7 +26,6 @@ import javax.annotation.processing.*; import javax.lang.model.element.*; -import javax.lang.model.type.*; import com.oracle.truffle.dsl.processor.java.*; @@ -43,6 +42,7 @@ public List getAllMembersInDeclarationOrder(ProcessingEnvironment environment, TypeElement type) { return sortBySourceOrder(new ArrayList<>(environment.getElementUtils().getAllMembers(type))); + } public List getEnclosedElementsInDeclarationOrder(TypeElement type) { @@ -50,66 +50,97 @@ } private static List sortBySourceOrder(List elements) { - final Map> declarationOrders = new HashMap<>(); + Map> groupedByEnclosing = new HashMap<>(); + for (Element element : elements) { + Element enclosing = element.getEnclosingElement(); + List grouped = groupedByEnclosing.get(enclosing); + if (grouped == null) { + grouped = new ArrayList<>(); + groupedByEnclosing.put((TypeElement) enclosing, grouped); + } + grouped.add(element); + } + + for (TypeElement enclosing : groupedByEnclosing.keySet()) { + Collections.sort(groupedByEnclosing.get(enclosing), createSourceOrderComparator(enclosing)); + } + + if (groupedByEnclosing.size() == 1) { + return groupedByEnclosing.get(groupedByEnclosing.keySet().iterator().next()); + } else { + List enclosingTypes = new ArrayList<>(groupedByEnclosing.keySet()); + + Collections.sort(enclosingTypes, new Comparator() { + public int compare(TypeElement o1, TypeElement o2) { + if (ElementUtils.isSubtype(o1.asType(), o2.asType())) { + return 1; + } else { + return -1; + } + } + }); + + List sourceOrderElements = new ArrayList<>(); + for (TypeElement typeElement : enclosingTypes) { + sourceOrderElements.addAll(groupedByEnclosing.get(typeElement)); + } + return sourceOrderElements; + } + + } + + private static Comparator createSourceOrderComparator(final TypeElement enclosing) { Comparator comparator = new Comparator() { + + final List declarationOrder = lookupDeclarationOrder(enclosing); + public int compare(Element o1, Element o2) { try { - TypeMirror enclosing1 = o1.getEnclosingElement().asType(); - TypeMirror enclosing2 = o2.getEnclosingElement().asType(); - - if (ElementUtils.typeEquals(enclosing1, enclosing2)) { - List declarationOrder = lookupDeclarationOrder(declarationOrders, (TypeElement) o1.getEnclosingElement()); + Element enclosing1Element = o1.getEnclosingElement(); + Element enclosing2Element = o2.getEnclosingElement(); - if (declarationOrder == null) { - return 0; - } - Object o1Binding = field(o1, "_binding"); - Object o2Binding = field(o2, "_binding"); + if (!ElementUtils.typeEquals(enclosing1Element.asType(), enclosing2Element.asType())) { + throw new AssertionError(); + } - int i1 = declarationOrder.indexOf(o1Binding); - int i2 = declarationOrder.indexOf(o2Binding); + Object o1Binding = field(o1, "_binding"); + Object o2Binding = field(o2, "_binding"); - if (i1 == -1 || i2 == -1) { - return 0; - } + int i1 = declarationOrder.indexOf(o1Binding); + int i2 = declarationOrder.indexOf(o2Binding); - return i1 - i2; - } else { - if (ElementUtils.isSubtype(enclosing1, enclosing2)) { - return 1; - } else { - return -1; - } + if (i1 == -1 || i2 == -1) { + return 0; } + + return i1 - i2; } catch (Exception e) { throw new RuntimeException(e); } } }; - - Collections.sort(elements, comparator); - return elements; + return comparator; } - private static List lookupDeclarationOrder(Map> declarationOrders, TypeElement type) throws Exception, ClassNotFoundException { - if (declarationOrders.containsKey(type)) { - return declarationOrders.get(type); - } + private static List lookupDeclarationOrder(TypeElement type) { - Object binding = field(type, "_binding"); - Class sourceTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding"); - Class binaryTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding"); + List declarationOrder; + try { + Object binding = field(type, "_binding"); + Class sourceTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding"); + Class binaryTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding"); - List declarationOrder = null; - if (sourceTypeBinding.isAssignableFrom(binding.getClass())) { - declarationOrder = findSourceTypeOrder(binding); - } else if (binaryTypeBinding.isAssignableFrom(binding.getClass())) { - declarationOrder = findBinaryTypeOrder(binding); + declarationOrder = null; + if (sourceTypeBinding.isAssignableFrom(binding.getClass())) { + declarationOrder = findSourceTypeOrder(binding); + } else if (binaryTypeBinding.isAssignableFrom(binding.getClass())) { + declarationOrder = findBinaryTypeOrder(binding); + } + } catch (Exception e) { + throw new RuntimeException(e); } - declarationOrders.put(type, declarationOrder); - return declarationOrder; }