comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/compiler/JDTCompiler.java @ 18494:9e944c7eaded

Truffle-DSL: fixed bug in source ordering when compiling with JDT.
author Christian Humer <christian.humer@gmail.com>
date Mon, 24 Nov 2014 17:29:51 +0100
parents ccb4e2bd894f
children
comparison
equal deleted inserted replaced
18493:11bc91bcc525 18494:9e944c7eaded
1 /* 1 /*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
24 24
25 import java.util.*; 25 import java.util.*;
26 26
27 import javax.annotation.processing.*; 27 import javax.annotation.processing.*;
28 import javax.lang.model.element.*; 28 import javax.lang.model.element.*;
29 import javax.lang.model.type.*;
30 29
31 import com.oracle.truffle.dsl.processor.java.*; 30 import com.oracle.truffle.dsl.processor.java.*;
32 31
33 public class JDTCompiler extends AbstractCompiler { 32 public class JDTCompiler extends AbstractCompiler {
34 33
41 } 40 }
42 } 41 }
43 42
44 public List<? extends Element> getAllMembersInDeclarationOrder(ProcessingEnvironment environment, TypeElement type) { 43 public List<? extends Element> getAllMembersInDeclarationOrder(ProcessingEnvironment environment, TypeElement type) {
45 return sortBySourceOrder(new ArrayList<>(environment.getElementUtils().getAllMembers(type))); 44 return sortBySourceOrder(new ArrayList<>(environment.getElementUtils().getAllMembers(type)));
45
46 } 46 }
47 47
48 public List<? extends Element> getEnclosedElementsInDeclarationOrder(TypeElement type) { 48 public List<? extends Element> getEnclosedElementsInDeclarationOrder(TypeElement type) {
49 return sortBySourceOrder(new ArrayList<>(type.getEnclosedElements())); 49 return sortBySourceOrder(new ArrayList<>(type.getEnclosedElements()));
50 } 50 }
51 51
52 private static List<? extends Element> sortBySourceOrder(List<Element> elements) { 52 private static List<? extends Element> sortBySourceOrder(List<Element> elements) {
53 final Map<TypeElement, List<Object>> declarationOrders = new HashMap<>(); 53 Map<TypeElement, List<Element>> groupedByEnclosing = new HashMap<>();
54 for (Element element : elements) {
55 Element enclosing = element.getEnclosingElement();
56 List<Element> grouped = groupedByEnclosing.get(enclosing);
57 if (grouped == null) {
58 grouped = new ArrayList<>();
59 groupedByEnclosing.put((TypeElement) enclosing, grouped);
60 }
61 grouped.add(element);
62 }
63
64 for (TypeElement enclosing : groupedByEnclosing.keySet()) {
65 Collections.sort(groupedByEnclosing.get(enclosing), createSourceOrderComparator(enclosing));
66 }
67
68 if (groupedByEnclosing.size() == 1) {
69 return groupedByEnclosing.get(groupedByEnclosing.keySet().iterator().next());
70 } else {
71 List<TypeElement> enclosingTypes = new ArrayList<>(groupedByEnclosing.keySet());
72
73 Collections.sort(enclosingTypes, new Comparator<TypeElement>() {
74 public int compare(TypeElement o1, TypeElement o2) {
75 if (ElementUtils.isSubtype(o1.asType(), o2.asType())) {
76 return 1;
77 } else {
78 return -1;
79 }
80 }
81 });
82
83 List<Element> sourceOrderElements = new ArrayList<>();
84 for (TypeElement typeElement : enclosingTypes) {
85 sourceOrderElements.addAll(groupedByEnclosing.get(typeElement));
86 }
87 return sourceOrderElements;
88 }
89
90 }
91
92 private static Comparator<Element> createSourceOrderComparator(final TypeElement enclosing) {
54 93
55 Comparator<Element> comparator = new Comparator<Element>() { 94 Comparator<Element> comparator = new Comparator<Element>() {
95
96 final List<Object> declarationOrder = lookupDeclarationOrder(enclosing);
97
56 public int compare(Element o1, Element o2) { 98 public int compare(Element o1, Element o2) {
57 try { 99 try {
58 TypeMirror enclosing1 = o1.getEnclosingElement().asType(); 100 Element enclosing1Element = o1.getEnclosingElement();
59 TypeMirror enclosing2 = o2.getEnclosingElement().asType(); 101 Element enclosing2Element = o2.getEnclosingElement();
60 102
61 if (ElementUtils.typeEquals(enclosing1, enclosing2)) { 103 if (!ElementUtils.typeEquals(enclosing1Element.asType(), enclosing2Element.asType())) {
62 List<Object> declarationOrder = lookupDeclarationOrder(declarationOrders, (TypeElement) o1.getEnclosingElement()); 104 throw new AssertionError();
63
64 if (declarationOrder == null) {
65 return 0;
66 }
67 Object o1Binding = field(o1, "_binding");
68 Object o2Binding = field(o2, "_binding");
69
70 int i1 = declarationOrder.indexOf(o1Binding);
71 int i2 = declarationOrder.indexOf(o2Binding);
72
73 if (i1 == -1 || i2 == -1) {
74 return 0;
75 }
76
77 return i1 - i2;
78 } else {
79 if (ElementUtils.isSubtype(enclosing1, enclosing2)) {
80 return 1;
81 } else {
82 return -1;
83 }
84 } 105 }
106
107 Object o1Binding = field(o1, "_binding");
108 Object o2Binding = field(o2, "_binding");
109
110 int i1 = declarationOrder.indexOf(o1Binding);
111 int i2 = declarationOrder.indexOf(o2Binding);
112
113 if (i1 == -1 || i2 == -1) {
114 return 0;
115 }
116
117 return i1 - i2;
85 } catch (Exception e) { 118 } catch (Exception e) {
86 throw new RuntimeException(e); 119 throw new RuntimeException(e);
87 } 120 }
88 } 121 }
89 }; 122 };
90 123 return comparator;
91 Collections.sort(elements, comparator); 124 }
92 return elements; 125
93 } 126 private static List<Object> lookupDeclarationOrder(TypeElement type) {
94 127
95 private static List<Object> lookupDeclarationOrder(Map<TypeElement, List<Object>> declarationOrders, TypeElement type) throws Exception, ClassNotFoundException { 128 List<Object> declarationOrder;
96 if (declarationOrders.containsKey(type)) { 129 try {
97 return declarationOrders.get(type); 130 Object binding = field(type, "_binding");
98 } 131 Class<?> sourceTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding");
99 132 Class<?> binaryTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding");
100 Object binding = field(type, "_binding"); 133
101 Class<?> sourceTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding"); 134 declarationOrder = null;
102 Class<?> binaryTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding"); 135 if (sourceTypeBinding.isAssignableFrom(binding.getClass())) {
103 136 declarationOrder = findSourceTypeOrder(binding);
104 List<Object> declarationOrder = null; 137 } else if (binaryTypeBinding.isAssignableFrom(binding.getClass())) {
105 if (sourceTypeBinding.isAssignableFrom(binding.getClass())) { 138 declarationOrder = findBinaryTypeOrder(binding);
106 declarationOrder = findSourceTypeOrder(binding); 139 }
107 } else if (binaryTypeBinding.isAssignableFrom(binding.getClass())) { 140 } catch (Exception e) {
108 declarationOrder = findBinaryTypeOrder(binding); 141 throw new RuntimeException(e);
109 } 142 }
110
111 declarationOrders.put(type, declarationOrder);
112 143
113 return declarationOrder; 144 return declarationOrder;
114 } 145 }
115 146
116 private static List<Object> findBinaryTypeOrder(Object binding) throws Exception { 147 private static List<Object> findBinaryTypeOrder(Object binding) throws Exception {