Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/ElementUtils.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 | 3648df587223 |
children | f83fd99b2962 |
comparison
equal
deleted
inserted
replaced
20937:37ea76052733 | 20938:18c0f02fa4d2 |
---|---|
223 } | 223 } |
224 return ProcessorContext.getInstance().getEnvironment().getTypeUtils().isSubtype(type1, type2); | 224 return ProcessorContext.getInstance().getEnvironment().getTypeUtils().isSubtype(type1, type2); |
225 } | 225 } |
226 | 226 |
227 public static boolean isAssignable(TypeMirror from, TypeMirror to) { | 227 public static boolean isAssignable(TypeMirror from, TypeMirror to) { |
228 if (typeEquals(from, to)) { | |
229 return true; | |
230 } else if (isVoid(to)) { | |
231 return true; | |
232 } else if (isObject(to)) { | |
233 return true; | |
234 } | |
228 ProcessorContext context = ProcessorContext.getInstance(); | 235 ProcessorContext context = ProcessorContext.getInstance(); |
229 | |
230 if (!(from instanceof CodeTypeMirror) && !(to instanceof CodeTypeMirror)) { | 236 if (!(from instanceof CodeTypeMirror) && !(to instanceof CodeTypeMirror)) { |
231 return context.getEnvironment().getTypeUtils().isAssignable(context.reloadType(from), context.reloadType(to)); | 237 return context.getEnvironment().getTypeUtils().isAssignable(context.reloadType(from), context.reloadType(to)); |
232 } else { | 238 } else { |
233 return isAssignableImpl(from, to); | 239 return isAssignableImpl(from, to); |
234 } | 240 } |
1120 return element1.getSimpleName().equals(element2.getSimpleName()); | 1126 return element1.getSimpleName().equals(element2.getSimpleName()); |
1121 } else { | 1127 } else { |
1122 throw new AssertionError("unsupported element type"); | 1128 throw new AssertionError("unsupported element type"); |
1123 } | 1129 } |
1124 } | 1130 } |
1131 | |
1132 public static List<TypeMirror> sortTypes(List<TypeMirror> list) { | |
1133 Collections.sort(list, new Comparator<TypeMirror>() { | |
1134 public int compare(TypeMirror o1, TypeMirror o2) { | |
1135 return compareType(o1, o2); | |
1136 } | |
1137 }); | |
1138 return list; | |
1139 } | |
1140 | |
1141 public static int compareType(TypeMirror signature1, TypeMirror signature2) { | |
1142 if (signature1 == null) { | |
1143 return 1; | |
1144 } else if (signature2 == null) { | |
1145 return -1; | |
1146 } | |
1147 | |
1148 if (ElementUtils.typeEquals(signature1, signature2)) { | |
1149 return 0; | |
1150 } | |
1151 | |
1152 if (signature1.getKind() == TypeKind.DECLARED && signature2.getKind() == TypeKind.DECLARED) { | |
1153 TypeElement element1 = ElementUtils.fromTypeMirror(signature1); | |
1154 TypeElement element2 = ElementUtils.fromTypeMirror(signature2); | |
1155 | |
1156 if (ElementUtils.getDirectSuperTypes(element1).contains(element2)) { | |
1157 return -1; | |
1158 } else if (ElementUtils.getDirectSuperTypes(element2).contains(element1)) { | |
1159 return 1; | |
1160 } | |
1161 } | |
1162 return ElementUtils.getSimpleName(signature1).compareTo(ElementUtils.getSimpleName(signature2)); | |
1163 } | |
1164 | |
1165 public static List<TypeMirror> uniqueSortedTypes(Collection<TypeMirror> types) { | |
1166 if (types.isEmpty()) { | |
1167 return Collections.emptyList(); | |
1168 } else if (types.size() <= 1) { | |
1169 if (types instanceof List) { | |
1170 return (List<TypeMirror>) types; | |
1171 } else { | |
1172 return new ArrayList<>(types); | |
1173 } | |
1174 } | |
1175 Map<String, TypeMirror> sourceTypes = new HashMap<>(); | |
1176 for (TypeMirror type : types) { | |
1177 sourceTypes.put(ElementUtils.getTypeId(type), type); | |
1178 } | |
1179 return sortTypes(new ArrayList<>(sourceTypes.values())); | |
1180 } | |
1181 | |
1182 public static int compareMethod(ExecutableElement method1, ExecutableElement method2) { | |
1183 List<? extends VariableElement> parameters1 = method1.getParameters(); | |
1184 List<? extends VariableElement> parameters2 = method2.getParameters(); | |
1185 if (parameters1.size() != parameters2.size()) { | |
1186 return Integer.compare(parameters1.size(), parameters2.size()); | |
1187 } | |
1188 | |
1189 int result = 0; | |
1190 for (int i = 0; i < parameters1.size(); i++) { | |
1191 VariableElement var1 = parameters1.get(i); | |
1192 VariableElement var2 = parameters2.get(i); | |
1193 result = compareType(var1.asType(), var2.asType()); | |
1194 if (result != 0) { | |
1195 return result; | |
1196 } | |
1197 } | |
1198 | |
1199 result = method1.getSimpleName().toString().compareTo(method2.getSimpleName().toString()); | |
1200 if (result == 0) { | |
1201 // if still no difference sort by enclosing type name | |
1202 TypeElement enclosingType1 = ElementUtils.findNearestEnclosingType(method1); | |
1203 TypeElement enclosingType2 = ElementUtils.findNearestEnclosingType(method2); | |
1204 result = enclosingType1.getQualifiedName().toString().compareTo(enclosingType2.getQualifiedName().toString()); | |
1205 } | |
1206 return result; | |
1207 } | |
1208 | |
1125 } | 1209 } |