Mercurial > hg > truffle
diff graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/TypeSystemCodeGenerator.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 | ae81dd154fb6 |
children | 9a83732f97eb |
line wrap: on
line diff
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/TypeSystemCodeGenerator.java Tue Apr 14 22:12:03 2015 +0200 +++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/TypeSystemCodeGenerator.java Tue Apr 14 15:12:48 2015 +0200 @@ -22,12 +22,13 @@ */ package com.oracle.truffle.dsl.processor.generator; +import static com.oracle.truffle.dsl.processor.generator.GeneratorUtils.*; import static com.oracle.truffle.dsl.processor.java.ElementUtils.*; -import static com.oracle.truffle.dsl.processor.generator.GeneratorUtils.*; import static javax.lang.model.element.Modifier.*; import java.util.*; +import javax.lang.model.element.*; import javax.lang.model.type.*; import com.oracle.truffle.api.nodes.*; @@ -38,64 +39,47 @@ public class TypeSystemCodeGenerator extends CodeTypeElementFactory<TypeSystemData> { - public static CodeTree cast(TypeData type, String content) { - return cast(type, CodeTreeBuilder.singleString(content)); + private static final String LOCAL_VALUE = "value"; + + public static CodeTree cast(TypeSystemData typeSystem, TypeMirror type, String content) { + return cast(typeSystem, type, CodeTreeBuilder.singleString(content)); } - public static CodeTree implicitType(TypeData type, CodeTree value) { - if (type.isGeneric()) { + public static CodeTree implicitType(TypeSystemData typeSystem, TypeMirror type, CodeTree value) { + if (ElementUtils.isObject(type)) { return value; } CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = type.getTypeSystem(); - builder.startStaticCall(createTypeSystemGen(typeSystem), getImplicitClass(type)).tree(value); + builder.startStaticCall(createTypeSystemGen(typeSystem), getImplicitClass(typeSystem, type)).tree(value); builder.end(); return builder.build(); } - public static CodeTree invokeImplicitCast(ImplicitCastData cast, CodeTree expression) { + public static CodeTree invokeImplicitCast(TypeSystemData typeSystem, ImplicitCastData cast, CodeTree expression) { CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = cast.getTargetType().getTypeSystem(); builder.startStaticCall(createTypeSystemGen(typeSystem), cast.getMethodName()).tree(expression); builder.end(); return builder.build(); } - public static CodeTree implicitCheck(TypeData type, CodeTree value, String typeHint) { - if (type.isGeneric()) { + public static CodeTree implicitCheck(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) { + return callImplictMethod(typeSystem, type, isImplicitTypeMethodName(typeSystem, type), value, typeHint); + } + + public static CodeTree implicitExpect(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) { + return callImplictMethod(typeSystem, type, expectImplicitTypeMethodName(typeSystem, type), value, typeHint); + } + + public static CodeTree implicitCast(TypeSystemData typeSystem, TypeMirror type, CodeTree value, String typeHint) { + return callImplictMethod(typeSystem, type, asImplicitTypeMethodName(typeSystem, type), value, typeHint); + } + + private static CodeTree callImplictMethod(TypeSystemData typeSystem, TypeMirror type, String methodName, CodeTree value, String typeHint) { + if (ElementUtils.isObject(type)) { return value; } CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = type.getTypeSystem(); - builder.startStaticCall(createTypeSystemGen(typeSystem), isImplicitTypeMethodName(type)).tree(value); - if (typeHint != null) { - builder.string(typeHint); - } - builder.end(); - return builder.build(); - } - - public static CodeTree implicitExpect(TypeData type, CodeTree value, String typeHint) { - if (type.isGeneric()) { - return value; - } - CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = type.getTypeSystem(); - builder.startStaticCall(createTypeSystemGen(typeSystem), expectImplicitTypeMethodName(type)).tree(value); - if (typeHint != null) { - builder.string(typeHint); - } - builder.end(); - return builder.build(); - } - - public static CodeTree implicitCast(TypeData type, CodeTree value, String typeHint) { - if (type.isGeneric()) { - return value; - } - CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = type.getTypeSystem(); - builder.startStaticCall(createTypeSystemGen(typeSystem), asImplicitTypeMethodName(type)).tree(value); + builder.startStaticCall(createTypeSystemGen(typeSystem), methodName).tree(value); if (typeHint != null) { builder.string(typeHint); } @@ -103,36 +87,57 @@ return builder.build(); } - public static CodeTree cast(TypeData type, CodeTree content) { - if (type.isGeneric()) { + public static CodeTree cast(TypeSystemData typeSystem, TypeMirror type, CodeTree content) { + if (ElementUtils.isObject(type)) { return content; } CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = type.getTypeSystem(); - if (type.isDefaultCast()) { - builder.cast(type.getPrimitiveType(), content); + TypeCastData cast = typeSystem.getCast(type); + if (cast == null) { + builder.cast(type, content); } else { - builder.startStaticCall(typeSystem.getTemplateType().asType(), type.getTypeCasts().get(0).getMethodName()).tree(content).end(); + builder.startStaticCall(typeSystem.getTemplateType().asType(), cast.getMethodName()).tree(content).end(); } return builder.build(); } - public static CodeTree expect(TypeData type, CodeTree content) { - if (type.isGeneric() || type.isVoid()) { + public static CodeTree expect(TypeSystemData typeSystem, TypeMirror type, CodeTree content) { + if (ElementUtils.isObject(type) || ElementUtils.isVoid(type)) { return content; } CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = type.getTypeSystem(); - builder.startStaticCall(createTypeSystemGen(typeSystem), expectTypeMethodName(type)).tree(content).end(); + if (typeSystem.hasType(type)) { + builder.startStaticCall(createTypeSystemGen(typeSystem), expectTypeMethodName(typeSystem, type)).tree(content).end(); + } else { + builder.startCall(expectTypeMethodName(typeSystem, type)).tree(content).end(); + } + return builder.build(); } - public static CodeTree expect(TypeData sourceType, TypeData targetType, CodeTree content) { - if (sourceType != null && !sourceType.needsCastTo(targetType)) { + public static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceType, TypeMirror expectedType) { + if (ElementUtils.isObject(expectedType) || ElementUtils.isVoid(expectedType)) { + return null; + } + CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), expectedType, TypeSystemCodeGenerator.expectTypeMethodName(typeSystem, expectedType)); + method.setVisibility(visibility); + method.addParameter(new CodeVariableElement(sourceType, LOCAL_VALUE)); + method.addThrownType(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException()); + + CodeTreeBuilder body = method.createBuilder(); + body.startIf().tree(check(typeSystem, expectedType, LOCAL_VALUE)).end().startBlock(); + body.startReturn().tree(cast(typeSystem, expectedType, LOCAL_VALUE)).end(); + body.end(); + body.startThrow().startNew(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end(); + return method; + } + + public static CodeTree expect(TypeSystemData typeSystem, TypeMirror sourceType, TypeMirror targetType, CodeTree content) { + if (sourceType != null && !ElementUtils.needsCastTo(sourceType, targetType)) { return content; } else { - return expect(targetType, content); + return expect(typeSystem, targetType, content); } } @@ -140,51 +145,55 @@ return new GeneratedTypeMirror(ElementUtils.getPackageName(typeSystem.getTemplateType()), typeName(typeSystem)); } - public static CodeTree check(TypeData type, String content) { - return check(type, CodeTreeBuilder.singleString(content)); + public static CodeTree check(TypeSystemData typeSystem, TypeMirror type, String content) { + return check(typeSystem, type, CodeTreeBuilder.singleString(content)); } - public static CodeTree check(TypeData type, CodeTree content) { - if (type.isGeneric()) { + public static CodeTree check(TypeSystemData typeSystem, TypeMirror type, CodeTree content) { + if (ElementUtils.isObject(type)) { return content; } CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); - TypeSystemData typeSystem = type.getTypeSystem(); - if (type.isDefaultCheck()) { - builder.instanceOf(content, type.getBoxedType()); + TypeCheckData check = typeSystem.getCheck(type); + if (check == null) { + builder.instanceOf(content, ElementUtils.boxType(typeSystem.getContext(), type)); } else { - builder.startStaticCall(typeSystem.getTemplateType().asType(), type.getTypeChecks().get(0).getMethodName()).tree(content).end(); + builder.startStaticCall(typeSystem.getTemplateType().asType(), check.getMethodName()).tree(content).end(); } return builder.build(); } - public static String isTypeMethodName(TypeData type) { - return "is" + ElementUtils.getTypeId(type.getBoxedType()); + public static String isTypeMethodName(TypeSystemData typeSystem, TypeMirror type) { + return "is" + getTypeId(typeSystem, type); } - static String isImplicitTypeMethodName(TypeData type) { - return "isImplicit" + ElementUtils.getTypeId(type.getBoxedType()); + private static String getTypeId(TypeSystemData typeSystem, TypeMirror type) { + return ElementUtils.getTypeId(typeSystem.boxType(type)); } - public static String asTypeMethodName(TypeData type) { - return "as" + ElementUtils.getTypeId(type.getBoxedType()); + static String isImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) { + return "isImplicit" + getTypeId(typeSystem, type); } - static String asImplicitTypeMethodName(TypeData type) { - return "asImplicit" + ElementUtils.getTypeId(type.getBoxedType()); + public static String asTypeMethodName(TypeSystemData typeSystem, TypeMirror type) { + return "as" + getTypeId(typeSystem, type); + } + + static String asImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) { + return "asImplicit" + getTypeId(typeSystem, type); } - static String expectImplicitTypeMethodName(TypeData type) { - return "expectImplicit" + ElementUtils.getTypeId(type.getBoxedType()); + static String expectImplicitTypeMethodName(TypeSystemData typeSystem, TypeMirror type) { + return "expectImplicit" + getTypeId(typeSystem, type); } - static String getImplicitClass(TypeData type) { - return "getImplicit" + ElementUtils.getTypeId(type.getBoxedType()) + "Class"; + static String getImplicitClass(TypeSystemData typeSystem, TypeMirror type) { + return "getImplicit" + getTypeId(typeSystem, type) + "Class"; } - public static String expectTypeMethodName(TypeData type) { - return "expect" + ElementUtils.getTypeId(type.getBoxedType()); + public static String expectTypeMethodName(TypeSystemData typeSystem, TypeMirror type) { + return "expect" + getTypeId(typeSystem, type); } static String typeName(TypeSystemData typeSystem) { @@ -203,11 +212,8 @@ clazz.add(new TypeSystemNodeFactory(context, typeSystem).create()); if (typeSystem.getOptions().implicitCastOptimization().isMergeCasts()) { - for (TypeData type : typeSystem.getTypes()) { - List<TypeData> sourceTypes = typeSystem.lookupSourceTypes(type); - if (sourceTypes.size() > 1) { - clazz.add(new ImplicitCastNodeFactory(context, type).create()); - } + for (TypeMirror type : typeSystem.lookupTargetTypes()) { + clazz.add(new ImplicitCastNodeFactory(context, typeSystem, type).create()); } } return clazz; @@ -215,8 +221,6 @@ private static class TypeClassFactory { - private static final String LOCAL_VALUE = "value"; - private final ProcessorContext context; private final TypeSystemData typeSystem; @@ -233,50 +237,35 @@ CodeVariableElement singleton = createSingleton(clazz); clazz.add(singleton); - for (TypeData type : typeSystem.getTypes()) { - if (type.isVoid() || type.isGeneric()) { + for (TypeMirror type : typeSystem.getLegacyTypes()) { + if (ElementUtils.isVoid(type) || ElementUtils.isObject(type)) { continue; } clazz.addOptional(createIsTypeMethod(type)); clazz.addOptional(createAsTypeMethod(type)); + clazz.addOptional(createExpectTypeMethod(type, context.getType(Object.class))); - for (TypeData sourceType : collectExpectSourceTypes(type)) { - clazz.addOptional(createExpectTypeMethod(type, sourceType)); - } + } - if (type.hasImplicitSourceTypes()) { - clazz.add(createAsImplicitTypeMethod(type, false)); - if (typeSystem.getOptions().implicitCastOptimization().isNone()) { - clazz.add(createExpectImplicitTypeMethod(type, false)); - } - clazz.add(createIsImplicitTypeMethod(type, false)); + List<TypeMirror> lookupTargetTypes = typeSystem.lookupTargetTypes(); + for (TypeMirror type : lookupTargetTypes) { + clazz.add(createAsImplicitTypeMethod(type, false)); + if (typeSystem.getOptions().implicitCastOptimization().isNone()) { + clazz.add(createExpectImplicitTypeMethod(type, false)); + } + clazz.add(createIsImplicitTypeMethod(type, false)); - if (typeSystem.getOptions().implicitCastOptimization().isDuplicateTail()) { - clazz.add(createAsImplicitTypeMethod(type, true)); - clazz.add(createExpectImplicitTypeMethod(type, true)); - clazz.add(createIsImplicitTypeMethod(type, true)); - clazz.add(createGetImplicitClass(type)); - } + if (typeSystem.getOptions().implicitCastOptimization().isDuplicateTail()) { + clazz.add(createAsImplicitTypeMethod(type, true)); + clazz.add(createExpectImplicitTypeMethod(type, true)); + clazz.add(createIsImplicitTypeMethod(type, true)); + clazz.add(createGetImplicitClass(type)); } } - return clazz; } - private static List<TypeData> collectExpectSourceTypes(TypeData type) { - Set<TypeData> sourceTypes = new HashSet<>(); - sourceTypes.add(type.getTypeSystem().getGenericTypeData()); - for (TypeCastData cast : type.getTypeCasts()) { - sourceTypes.add(cast.getSourceType()); - } - for (TypeCheckData cast : type.getTypeChecks()) { - sourceTypes.add(cast.getCheckedType()); - } - sourceTypes.remove(type); - return new ArrayList<>(sourceTypes); - } - private CodeVariableElement createSingleton(CodeTypeElement clazz) { CodeVariableElement field = new CodeVariableElement(modifiers(PUBLIC, STATIC, FINAL), clazz.asType(), singletonName(typeSystem)); field.createInitBuilder().startNew(clazz.asType()).end(); @@ -287,24 +276,24 @@ return field; } - private CodeExecutableElement createIsImplicitTypeMethod(TypeData type, boolean typed) { - CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isImplicitTypeMethodName(type)); + private CodeExecutableElement createIsImplicitTypeMethod(TypeMirror type, boolean typed) { + CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isImplicitTypeMethodName(typeSystem, type)); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); if (typed) { method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint")); } CodeTreeBuilder builder = method.createBuilder(); - List<TypeData> sourceTypes = typeSystem.lookupSourceTypes(type); + List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type); builder.startReturn(); String sep = ""; - for (TypeData sourceType : sourceTypes) { + for (TypeMirror sourceType : sourceTypes) { builder.string(sep); if (typed) { - builder.string("(typeHint == ").typeLiteral(sourceType.getPrimitiveType()).string(" && "); + builder.string("(typeHint == ").typeLiteral(sourceType).string(" && "); } - builder.tree(check(sourceType, LOCAL_VALUE)); + builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)); if (typed) { builder.string(")"); } @@ -321,24 +310,24 @@ return method; } - private CodeExecutableElement createAsImplicitTypeMethod(TypeData type, boolean useTypeHint) { - String name = asImplicitTypeMethodName(type); - CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type.getPrimitiveType(), name); + private CodeExecutableElement createAsImplicitTypeMethod(TypeMirror type, boolean useTypeHint) { + String name = asImplicitTypeMethodName(typeSystem, type); + CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, name); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); if (useTypeHint) { method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint")); } - List<TypeData> sourceTypes = typeSystem.lookupSourceTypes(type); + List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type); CodeTreeBuilder builder = method.createBuilder(); boolean elseIf = false; - for (TypeData sourceType : sourceTypes) { + for (TypeMirror sourceType : sourceTypes) { elseIf = builder.startIf(elseIf); if (useTypeHint) { - builder.string("typeHint == ").typeLiteral(sourceType.getPrimitiveType()); + builder.string("typeHint == ").typeLiteral(sourceType); } else { - builder.tree(check(sourceType, LOCAL_VALUE)); + builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)); } builder.end().startBlock(); @@ -348,7 +337,7 @@ if (cast != null) { builder.startCall(cast.getMethodName()); } - builder.tree(cast(sourceType, LOCAL_VALUE)).end(); + builder.tree(cast(typeSystem, sourceType, LOCAL_VALUE)).end(); if (cast != null) { builder.end(); } @@ -363,26 +352,26 @@ return method; } - private CodeExecutableElement createExpectImplicitTypeMethod(TypeData type, boolean useTypeHint) { - String name = expectImplicitTypeMethodName(type); - CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type.getPrimitiveType(), name); + private CodeExecutableElement createExpectImplicitTypeMethod(TypeMirror type, boolean useTypeHint) { + String name = expectImplicitTypeMethodName(typeSystem, type); + CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, name); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); if (useTypeHint) { method.addParameter(new CodeVariableElement(context.getType(Class.class), "typeHint")); } method.getThrownTypes().add(context.getType(UnexpectedResultException.class)); - List<TypeData> sourceTypes = typeSystem.lookupSourceTypes(type); + List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type); CodeTreeBuilder builder = method.createBuilder(); boolean elseIf = false; - for (TypeData sourceType : sourceTypes) { + for (TypeMirror sourceType : sourceTypes) { elseIf = builder.startIf(elseIf); if (useTypeHint) { - builder.string("typeHint == ").typeLiteral(sourceType.getPrimitiveType()); + builder.string("typeHint == ").typeLiteral(sourceType); builder.string(" && "); } - builder.tree(check(sourceType, LOCAL_VALUE)); + builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)); builder.end().startBlock(); @@ -391,7 +380,7 @@ if (cast != null) { builder.startCall(cast.getMethodName()); } - builder.tree(cast(sourceType, LOCAL_VALUE)).end(); + builder.tree(cast(typeSystem, sourceType, LOCAL_VALUE)).end(); if (cast != null) { builder.end(); } @@ -405,18 +394,18 @@ return method; } - private CodeExecutableElement createGetImplicitClass(TypeData type) { - CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(Class.class), TypeSystemCodeGenerator.getImplicitClass(type)); + private CodeExecutableElement createGetImplicitClass(TypeMirror type) { + CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(Class.class), TypeSystemCodeGenerator.getImplicitClass(typeSystem, type)); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); - List<TypeData> sourceTypes = typeSystem.lookupSourceTypes(type); + List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type); CodeTreeBuilder builder = method.createBuilder(); boolean elseIf = false; - for (TypeData sourceType : sourceTypes) { + for (TypeMirror sourceType : sourceTypes) { elseIf = builder.startIf(elseIf); - builder.tree(check(sourceType, LOCAL_VALUE)).end(); + builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)).end(); builder.end().startBlock(); - builder.startReturn().typeLiteral(sourceType.getPrimitiveType()).end(); + builder.startReturn().typeLiteral(sourceType).end(); builder.end(); } @@ -428,50 +417,39 @@ return method; } - private CodeExecutableElement createIsTypeMethod(TypeData type) { - if (!type.getTypeChecks().isEmpty()) { + private CodeExecutableElement createIsTypeMethod(TypeMirror type) { + if (typeSystem.getCheck(type) != null) { return null; } - CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isTypeMethodName(type)); + CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isTypeMethodName(typeSystem, type)); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); CodeTreeBuilder body = method.createBuilder(); - body.startReturn().tree(check(type, LOCAL_VALUE)).end(); + body.startReturn().tree(check(typeSystem, type, LOCAL_VALUE)).end(); return method; } - private CodeExecutableElement createAsTypeMethod(TypeData type) { - if (!type.getTypeCasts().isEmpty()) { + private CodeExecutableElement createAsTypeMethod(TypeMirror type) { + if (typeSystem.getCast(type) != null) { return null; } - CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type.getPrimitiveType(), TypeSystemCodeGenerator.asTypeMethodName(type)); + CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), type, TypeSystemCodeGenerator.asTypeMethodName(typeSystem, type)); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); CodeTreeBuilder body = method.createBuilder(); - String assertMessage = typeName(typeSystem) + "." + asTypeMethodName(type) + ": " + ElementUtils.getSimpleName(type.getBoxedType()) + " expected"; - body.startAssert().tree(check(type, LOCAL_VALUE)).string(" : ").doubleQuote(assertMessage).end(); - body.startReturn().tree(cast(type, LOCAL_VALUE)).end(); + String assertMessage = typeName(typeSystem) + "." + asTypeMethodName(typeSystem, type) + ": " + ElementUtils.getSimpleName(type) + " expected"; + body.startAssert().tree(check(typeSystem, type, LOCAL_VALUE)).string(" : ").doubleQuote(assertMessage).end(); + body.startReturn().tree(cast(typeSystem, type, LOCAL_VALUE)).end(); return method; } - private CodeExecutableElement createExpectTypeMethod(TypeData expectedType, TypeData sourceType) { - CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), expectedType.getPrimitiveType(), TypeSystemCodeGenerator.expectTypeMethodName(expectedType)); - method.addParameter(new CodeVariableElement(sourceType.getPrimitiveType(), LOCAL_VALUE)); - method.addThrownType(context.getTruffleTypes().getUnexpectedValueException()); - - CodeTreeBuilder body = method.createBuilder(); - body.startIf().tree(check(expectedType, LOCAL_VALUE)).end().startBlock(); - body.startReturn().tree(cast(expectedType, LOCAL_VALUE)).end().end(); - body.end(); // if-block - body.startThrow().startNew(context.getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end(); - - return method; + private CodeExecutableElement createExpectTypeMethod(TypeMirror expectedType, TypeMirror sourceType) { + return createExpectMethod(Modifier.PUBLIC, typeSystem, sourceType, expectedType); } - } }