@Override public String checkSignature(ExecutableElement method) { final List<? extends VariableElement> params = method.getParameters(); boolean hasFrameArgument = false; if (params.size() >= 1) { hasFrameArgument = ElementUtils.areTypesCompatible(params.get(0).asType(), Utils.getTypeMirror(processingEnv, VirtualFrame.class)); } int expectedNumberOfArguments = hasFrameArgument ? NUMBER_OF_UNARY + 1 : NUMBER_OF_UNARY; if (params.size() != expectedNumberOfArguments) { return "Wrong number of arguments. Expected signature: ([frame: VirtualFrame], receiverObject: TruffleObject)"; } return super.checkSignature(method); }
return 1; if (ElementUtils.typeEquals(signature1, signature2)) { return 0; if (isVoid(signature1)) { if (isVoid(signature2)) { return 0; } else if (isVoid(signature2)) { return -1; TypeMirror boxedType1 = ElementUtils.boxType(context, signature1); TypeMirror boxedType2 = ElementUtils.boxType(context, signature2); if (ElementUtils.isSubtype(boxedType1, boxedType2)) { if (ElementUtils.isSubtype(boxedType2, boxedType1)) { return 0; } else if (ElementUtils.isSubtype(boxedType2, boxedType1)) { return -1; } else { return ElementUtils.getSimpleName(signature1).compareTo(ElementUtils.getSimpleName(signature2));
public int compare(Element o1, Element o2) { TypeMirror e1 = o1.getEnclosingElement() != null ? o1.getEnclosingElement().asType() : null; TypeMirror e2 = o2.getEnclosingElement() != null ? o2.getEnclosingElement().asType() : null; Set<String> e1SuperTypes = getCachedSuperTypes(e1); Set<String> e2SuperTypes = getCachedSuperTypes(e2); return ElementUtils.compareByTypeHierarchy(e1, e1SuperTypes, e2, e2SuperTypes); }
public static boolean canThrowType(List<? extends TypeMirror> thrownTypes, TypeMirror exceptionType) { if (ElementUtils.containsType(thrownTypes, exceptionType)) { return true; } if (isRuntimeException(exceptionType)) { return true; } // search for any super types for (TypeElement typeElement : getSuperTypes(fromTypeMirror(exceptionType))) { if (ElementUtils.containsType(thrownTypes, typeElement.asType())) { return true; } } return false; }
private SpecializationData parseSpecialization(TemplateMethod method) { List<SpecializationThrowsData> exceptionData = new ArrayList<>(); if (method.getMethod() != null) { AnnotationValue rewriteValue = ElementUtils.getAnnotationValue(method.getMarkerAnnotation(), "rewriteOn"); List<TypeMirror> exceptionTypes = ElementUtils.getAnnotationValueList(TypeMirror.class, method.getMarkerAnnotation(), "rewriteOn"); List<TypeMirror> rewriteOnTypes = new ArrayList<>(); for (TypeMirror exceptionType : exceptionTypes) { SpecializationThrowsData throwsData = new SpecializationThrowsData(method.getMarkerAnnotation(), rewriteValue, exceptionType); if (!ElementUtils.canThrowType(method.getMethod().getThrownTypes(), exceptionType)) { method.addError("A rewriteOn checked exception was specified but not thrown in the method's throws clause. The @%s method must specify a throws clause with the exception type '%s'.", Specialization.class.getSimpleName(), ElementUtils.getQualifiedName(exceptionType)); if (!ElementUtils.canThrowType(rewriteOnTypes, typeMirror)) { method.addError(rewriteValue, "A checked exception '%s' is thrown but is not specified using the rewriteOn property. " + "Checked exceptions that are not used for rewriting are not handled by the DSL. Use RuntimeExceptions for this purpose instead.", ElementUtils.getQualifiedName(typeMirror)); String insertBeforeName = ElementUtils.getAnnotationValue(String.class, method.getMarkerAnnotation(), "insertBefore"); if (!insertBeforeName.equals("")) { specialization.setInsertBeforeName(insertBeforeName); List<String> containsDefs = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "contains"); Set<String> containsNames = specialization.getContainsNames(); containsNames.clear(); specialization.getContainsNames().add(include); } else { AnnotationValue value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "contains"); specialization.addError(value, "Duplicate contains declaration '%s'.", include);
private List<NodeChildData> parseChildren(final List<TypeElement> typeHierarchy, List<? extends Element> elements) { Set<String> shortCircuits = new HashSet<>(); for (ExecutableElement method : ElementFilter.methodsIn(elements)) { AnnotationMirror mirror = ElementUtils.findAnnotationMirror(processingEnv, method, ShortCircuit.class); if (mirror != null) { shortCircuits.add(ElementUtils.getAnnotationValue(String.class, mirror, "value")); AnnotationMirror mirror = ElementUtils.findAnnotationMirror(processingEnv, method, CreateCast.class); if (mirror != null) { List<String> children = (ElementUtils.getAnnotationValueList(String.class, mirror, "value")); if (children != null) { for (String child : children) { Collections.reverse(typeHierarchyReversed); for (TypeElement type : typeHierarchyReversed) { AnnotationMirror nodeChildrenMirror = ElementUtils.findAnnotationMirror(processingEnv, type, NodeChildren.class); if (!ElementUtils.isAssignable(nodeClassType, context.getTruffleTypes().getNode())) { nodeClassType = null; List<AnnotationMirror> children = ElementUtils.collectAnnotations(context, nodeChildrenMirror, "value", type, NodeChild.class); int index = 0; for (AnnotationMirror childMirror : children) { String name = ElementUtils.getAnnotationValue(String.class, childMirror, "value"); if (name.equals("")) { name = "child" + index;
public void visitBinary(Binary binary) { String operator = binary.getOperator(); TypeMirror leftType = binary.getLeft().getResolvedType(); TypeMirror rightType = binary.getRight().getResolvedType(); if (!ElementUtils.areTypesCompatible(leftType, rightType)) { throw new InvalidExpressionException(String.format("Incompatible operand types %s and %s.", ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } TypeMirror booleanType = context.getType(boolean.class); boolean valid; if (LOGIC_OPERATORS.contains(operator)) { valid = ElementUtils.typeEquals(leftType, booleanType); } else if (COMPARABLE_OPERATORS.contains(operator)) { valid = ElementUtils.isPrimitive(leftType); } else if (IDENTITY_OPERATORS.contains(operator)) { valid = leftType.getKind().isPrimitive() || leftType.getKind() == TypeKind.DECLARED || leftType.getKind() == TypeKind.ARRAY; } else { throw new InvalidExpressionException(String.format("The operator %s is undefined.", operator)); } binary.setResolvedType(booleanType); if (!valid) { throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type(s) %s %s.", operator, ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } }
Collections.reverse(reversedTypeHierarchy); for (TypeElement typeElement : reversedTypeHierarchy) { AnnotationMirror nodeChildrenMirror = ElementUtils.findAnnotationMirror(processingEnv, typeElement, NodeFields.class); List<AnnotationMirror> children = ElementUtils.collectAnnotations(context, nodeChildrenMirror, "value", typeElement, NodeField.class); String name = ElementUtils.firstLetterLowerCase(ElementUtils.getAnnotationValue(String.class, mirror, "name")); TypeMirror type = ElementUtils.getAnnotationValue(TypeMirror.class, mirror, "type"); field.addError(ElementUtils.getAnnotationValue(mirror, "name"), "Field name cannot be empty."); } else if (names.contains(name)) { field.addError(ElementUtils.getAnnotationValue(mirror, "name"), "Duplicate field name '%s'.", name);
private CodeExecutableElement createCreateNodeMethod() { CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), node.getNodeType(), "createNode"); CodeVariableElement arguments = new CodeVariableElement(context.getType(Object.class), "arguments"); method.setVarArgs(true); if (ElementUtils.isObject(param.asType())) { index++; continue; builder.type(ElementUtils.eraseGenericTypes(ElementUtils.boxType(context, param.asType()))); if (!param.asType().getKind().isPrimitive()) { builder.string(")"); for (VariableElement param : element.getParameters()) { builder.startGroup(); if (!ElementUtils.isObject(param.asType())) { builder.string("(").type(param.asType()).string(") ");
if (frameType == null) { frameType = resolvedFrameType; } else if (!ElementUtils.typeEquals(frameType, resolvedFrameType)) { inconsistentFrameTypes.add(ElementUtils.getSimpleName(frameType)); inconsistentFrameTypes.add(ElementUtils.getSimpleName(resolvedFrameType)); for (ExecutableTypeData type : allExecutes) { if (type.getEvaluatedCount() < nodeChildDeclarationsRequired) { requireNodeChildDeclarations.add(ElementUtils.createReferenceName(type.getMethod()));
private static String wrapText(Element element, AnnotationMirror mirror, String text) { StringBuilder b = new StringBuilder(); if (element != null) { if (element.getKind() == ElementKind.METHOD) { b.append("Method " + ElementUtils.createReferenceName((ExecutableElement) element)); } else { b.append("Element " + element.getSimpleName()); } } if (mirror != null) { b.append(" at annotation @" + ElementUtils.getSimpleName(mirror.getAnnotationType()).trim()); } if (b.length() > 0) { b.append(" is erroneous: ").append(text); return b.toString(); } else { return text; } }
private CodeExecutableElement createCreateNodeMethod() { CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), node.getNodeType(), "createNode"); CodeVariableElement arguments = new CodeVariableElement(context.getType(Object.class), "arguments"); method.setVarArgs(true); if (ElementUtils.isObject(param.asType())) { continue; builder.type(ElementUtils.boxType(context, param.asType())); if (!param.asType().getKind().isPrimitive()) { builder.string(")"); for (VariableElement param : element.getParameters()) { builder.startGroup(); if (!ElementUtils.isObject(param.asType())) { builder.string("(").type(param.asType()).string(") ");
public static CodeTree check(TypeSystemData typeSystem, TypeMirror type, CodeTree content) { if (ElementUtils.isObject(type)) { return content; } CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); TypeCheckData check = typeSystem.getCheck(type); if (check == null) { builder.instanceOf(content, ElementUtils.boxType(typeSystem.getContext(), type)); } else { builder.startStaticCall(typeSystem.getTemplateType().asType(), check.getMethodName()).tree(content).end(); } return builder.build(); }
static String singletonName(TypeSystemData type) { return createConstantName(getSimpleName(type.getTemplateType().asType())); }
public static int compareMethod(ExecutableElement method1, ExecutableElement method2) { List<? extends VariableElement> parameters1 = method1.getParameters(); List<? extends VariableElement> parameters2 = method2.getParameters(); if (parameters1.size() != parameters2.size()) { return Integer.compare(parameters1.size(), parameters2.size()); } int result = 0; for (int i = 0; i < parameters1.size(); i++) { VariableElement var1 = parameters1.get(i); VariableElement var2 = parameters2.get(i); result = compareType(var1.asType(), var2.asType()); if (result != 0) { return result; } } result = method1.getSimpleName().toString().compareTo(method2.getSimpleName().toString()); if (result == 0) { // if still no difference sort by enclosing type name TypeElement enclosingType1 = ElementUtils.findNearestEnclosingType(method1); TypeElement enclosingType2 = ElementUtils.findNearestEnclosingType(method2); result = enclosingType1.getQualifiedName().toString().compareTo(enclosingType2.getQualifiedName().toString()); } return result; }
private List<SpecializationData> filterImplementedSpecializations(ExecutableTypeData executable, List<SpecializationData> specializations) { List<SpecializationData> filteredSpecializations = new ArrayList<>(); TypeMirror returnType = ElementUtils.boxType(context, executable.getReturnType()); for (SpecializationData specialization : specializations) { TypeMirror specializationReturnType = ElementUtils.boxType(context, specialization.getReturnType().getType()); if (ElementUtils.typeEquals(specializationReturnType, returnType)) { filteredSpecializations.add(specialization); } } return filteredSpecializations; }
public int compare(TypeMirror o1, TypeMirror o2) { if (reverse) { return compareType(o2, o1); } else { return compareType(o1, o2); } } });
public int compare(ExecutableElement o1, ExecutableElement o2) { return ElementUtils.compareMethod(o1, o2); } });
public String createReferenceName() { if (getMethod() == null) { return "-"; } return ElementUtils.createReferenceName(getMethod()); }