public CodeExecutableElement(Set<Modifier> modifiers, TypeMirror returnType, String name, CodeVariableElement... parameters) { super(modifiers); this.returnType = returnType; this.name = CodeNames.of(name); for (CodeVariableElement codeParameter : parameters) { addParameter(codeParameter); } }
public CodeExecutableElement(Set<Modifier> modifiers, TypeMirror returnType, String name, CodeVariableElement... parameters) { super(modifiers); this.returnType = returnType; this.name = CodeNames.of(name); for (CodeVariableElement codeParameter : parameters) { addParameter(codeParameter); } }
public void addParametersTo(CodeExecutableElement method, int varArgsThreshold, String... optionalNames) { for (String var : optionalNames) { LocalVariable local = values.get(var); if (local != null) { method.addParameter(local.createParameter()); } } if (needsVarargs(true, varArgsThreshold)) { method.addParameter(new CodeVariableElement(factory.getType(Object[].class), "args_")); method.setVarArgs(true); } else { for (NodeExecutionData execution : factory.node.getChildExecutions()) { LocalVariable var = getValue(execution); if (var != null) { method.addParameter(var.createParameter()); } } } }
public void addParametersTo(CodeExecutableElement method, int varArgsThreshold, String... optionalNames) { for (String var : optionalNames) { LocalVariable local = values.get(var); if (local != null) { method.addParameter(local.createParameter()); } } if (needsVarargs(true, varArgsThreshold)) { method.addParameter(new CodeVariableElement(factory.getType(Object[].class), "args_")); method.setVarArgs(true); } else { for (NodeExecutionData execution : factory.node.getChildExecutions()) { if (execution.isShortCircuit()) { LocalVariable shortCircuitVar = getShortCircuit(execution); if (shortCircuitVar != null) { method.addParameter(shortCircuitVar.createParameter()); } } LocalVariable var = getValue(execution); if (var != null) { method.addParameter(var.createParameter()); } } } }
public static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz, ExecutableElement constructor) { CodeExecutableElement method = new CodeExecutableElement(modifiers, null, clazz.getSimpleName().toString()); CodeTreeBuilder builder = method.createBuilder(); if (constructor != null && constructor.getParameters().size() > 0) { builder.startStatement(); builder.startSuperCall(); for (VariableElement parameter : constructor.getParameters()) { method.addParameter(new CodeVariableElement(parameter.asType(), parameter.getSimpleName().toString())); builder.string(parameter.getSimpleName().toString()); } builder.end(); // super builder.end(); // statement } for (VariableElement field : clazz.getFields()) { if (field.getModifiers().contains(STATIC)) { continue; } String fieldName = field.getSimpleName().toString(); method.addParameter(new CodeVariableElement(field.asType(), fieldName)); builder.startStatement(); builder.string("this."); builder.string(fieldName); builder.string(" = "); builder.string(fieldName); builder.end(); // statement } return method; }
public static CodeExecutableElement clone(@SuppressWarnings("unused") ProcessingEnvironment env, ExecutableElement method) { CodeExecutableElement copy = new CodeExecutableElement(method.getReturnType(), method.getSimpleName().toString()); for (TypeMirror thrownType : method.getThrownTypes()) { copy.addThrownType(thrownType); } copy.setDefaultValue(method.getDefaultValue()); for (AnnotationMirror mirror : method.getAnnotationMirrors()) { copy.addAnnotationMirror(mirror); } for (VariableElement var : method.getParameters()) { copy.addParameter(CodeVariableElement.clone(var)); } for (Element element : method.getEnclosedElements()) { copy.add(element); } copy.getModifiers().addAll(method.getModifiers()); copy.setVarArgs(method.isVarArgs()); return copy; }
public static CodeExecutableElement clone(@SuppressWarnings("unused") ProcessingEnvironment env, ExecutableElement method) { CodeExecutableElement copy = new CodeExecutableElement(method.getReturnType(), method.getSimpleName().toString()); for (TypeMirror thrownType : method.getThrownTypes()) { copy.addThrownType(thrownType); } copy.setDefaultValue(method.getDefaultValue()); for (AnnotationMirror mirror : method.getAnnotationMirrors()) { copy.addAnnotationMirror(mirror); } for (VariableElement var : method.getParameters()) { copy.addParameter(CodeVariableElement.clone(var)); } for (Element element : method.getEnclosedElements()) { copy.add(element); } copy.getModifiers().addAll(method.getModifiers()); copy.setVarArgs(method.isVarArgs()); return copy; }
private CodeExecutableElement createIsImplicitTypeMethodFlat(TypeMirror type) { CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(boolean.class), TypeSystemCodeGenerator.isImplicitTypeMethodName(typeSystem, type)); method.addParameter(new CodeVariableElement(context.getType(int.class), "state")); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); CodeTreeBuilder builder = method.createBuilder(); List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type); builder.startReturn(); String sep = ""; int mask = 1; for (TypeMirror sourceType : sourceTypes) { builder.string(sep); builder.string("((state & 0b").string(Integer.toBinaryString(mask)).string(") != 0 && "); builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)); builder.string(")"); if (sourceTypes.lastIndexOf(sourceType) != sourceTypes.size() - 1) { builder.newLine(); } if (sep.equals("")) { builder.startIndention(); } sep = " || "; mask = mask << 1; } builder.end(); builder.end(); return method; }
private Element createCheckForPolymorphicSpecialize() { final boolean requiresExclude = requiresExclude(); final boolean requiresCacheCheck = requiresCacheCheck(); TypeMirror returnType = getType(void.class); CodeExecutableElement executable = new CodeExecutableElement(modifiers(PRIVATE), returnType, CHECK_FOR_POLYMORPHIC_SPECIALIZE); executable.addParameter(new CodeVariableElement(state.bitSetType, OLD_STATE)); if (requiresExclude) { executable.addParameter(new CodeVariableElement(exclude.bitSetType, OLD_EXCLUDE)); } if (requiresCacheCheck) { executable.addParameter(new CodeVariableElement(getType(int.class), OLD_CACHE_COUNT)); } CodeTreeBuilder builder = executable.createBuilder(); builder.declaration(state.bitSetType, NEW_STATE, state.createMaskedReference(FrameState.load(this), reachableSpecializations.toArray())); if (requiresExclude) { builder.declaration(exclude.bitSetType, NEW_EXCLUDE, exclude.createReference(FrameState.load(this))); } builder.startIf().string("(" + OLD_STATE + " ^ " + NEW_STATE + ") != 0"); if (requiresExclude) { builder.string(" || "); builder.string("(" + OLD_EXCLUDE + " ^ " + NEW_EXCLUDE + ") != 0"); } if (requiresCacheCheck) { builder.string(" || " + OLD_CACHE_COUNT + " < " + COUNT_CACHES + "()"); } builder.end(); // if builder.startBlock().startStatement().startCall("this", REPORT_POLYMORPHIC_SPECIALIZE).end(2); builder.end(); // true block return executable; }
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(typeSystem, type)); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); CodeTreeBuilder body = method.createBuilder(); body.startReturn().tree(check(typeSystem, type, LOCAL_VALUE)).end(); return method; }
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(typeSystem, type)); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); CodeTreeBuilder body = method.createBuilder(); body.startReturn().tree(check(typeSystem, type, LOCAL_VALUE)).end(); return method; }
private Element createMerge(CodeTypeElement clazz) { String methodName = "merge"; CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), context.getType(void.class), methodName); method.addParameter(new CodeVariableElement(clazz.asType(), "otherCast")); CodeTreeBuilder builder = method.createBuilder(); for (TypeMirror sourceType : sourceTypes) { String seenField = seenFieldName(sourceType); builder.startStatement(); builder.string("this.").string(seenField).string(" |= ").string("otherCast.").string(seenField); builder.end(); } return method; }
private Element createIsValid(TypeMirror assumptionType) { CodeExecutableElement isValid = new CodeExecutableElement(modifiers(PRIVATE, STATIC), getType(boolean.class), "isValid_"); CodeTreeBuilder builder = isValid.createBuilder(); if (assumptionType.getKind() == TypeKind.ARRAY) { isValid.addAnnotationMirror(new CodeAnnotationMirror(context.getDeclaredType(ExplodeLoop.class))); isValid.addParameter(new CodeVariableElement(getType(Assumption[].class), "assumptions")); builder.startIf().string("assumptions == null").end().startBlock().returnFalse().end(); builder.startFor().startGroup().type(((ArrayType) assumptionType).getComponentType()).string(" assumption : assumptions").end().end(); builder.startBlock(); builder.startIf().string("assumption == null || !assumption.isValid()").end(); builder.startBlock(); builder.returnFalse(); builder.end(); builder.end(); builder.returnTrue(); } else { isValid.addParameter(new CodeVariableElement(getType(Assumption.class), "assumption")); builder.startReturn().string("assumption != null && assumption.isValid()").end(); } return isValid; }
private CodeExecutableElement createAsTypeMethod(TypeMirror type) { if (typeSystem.getCast(type) != null) { return null; } 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(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 Element createCheck() { String methodName = "check"; CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), context.getType(boolean.class), methodName); method.addParameter(new CodeVariableElement(context.getType(Object.class), "value")); CodeTreeBuilder builder = method.createBuilder(); boolean elseIf = false; for (TypeMirror sourceType : sourceTypes) { elseIf = builder.startIf(elseIf); builder.string(seenFieldName(sourceType)).string(" && ").tree(TypeSystemCodeGenerator.check(typeSystem, sourceType, "value")); builder.end(); builder.startBlock().returnTrue().end(); } builder.returnFalse(); return method; }
private CodeExecutableElement createAsTypeMethod(TypeMirror type) { if (typeSystem.getCast(type) != null) { return null; } 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(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 createSpecializeImplictTypeMethodFlat(TypeMirror type) { String name = specializeImplicitTypeMethodName(typeSystem, type); CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), context.getType(int.class), name); method.addParameter(new CodeVariableElement(context.getType(Object.class), LOCAL_VALUE)); List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(type); CodeTreeBuilder builder = method.createBuilder(); boolean elseIf = false; int mask = 1; for (TypeMirror sourceType : sourceTypes) { elseIf = builder.startIf(elseIf); builder.tree(check(typeSystem, sourceType, LOCAL_VALUE)); builder.end().startBlock(); builder.startReturn(); builder.string("0b", Integer.toBinaryString(mask)); builder.end(); builder.end(); mask = mask << 1; } builder.startElseBlock(); builder.startReturn().string("0").end(); builder.end(); return method; }
private Element createCreate(CodeTypeElement clazz) { String methodName = "create"; CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), clazz.asType(), methodName); method.addParameter(new CodeVariableElement(context.getType(Object.class), "value")); CodeTreeBuilder builder = method.createBuilder(); builder.declaration(clazz.asType(), "newCast", builder.create().startNew(clazz.asType()).end()); for (TypeMirror sourceType : sourceTypes) { String seenField = seenFieldName(sourceType); builder.startStatement(); builder.string("newCast.").string(seenField).string(" = ").tree(TypeSystemCodeGenerator.check(typeSystem, sourceType, "value")); builder.end(); } builder.startReturn().string("newCast").end(); return method; }
public static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceTypeOriginal, TypeMirror expectedTypeOriginal) { TypeMirror expectedType = ElementUtils.fillInGenericWildcards(expectedTypeOriginal); TypeMirror sourceType = ElementUtils.fillInGenericWildcards(sourceTypeOriginal); 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; }
static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceTypeOriginal, TypeMirror expectedTypeOriginal) { TypeMirror expectedType = ElementUtils.fillInGenericWildcards(expectedTypeOriginal); TypeMirror sourceType = ElementUtils.fillInGenericWildcards(sourceTypeOriginal); 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; }