public CodeVariableElement declareFields(CodeTypeElement clazz) { return clazz.add(createNodeField(PRIVATE, bitSetType, name + "_", CompilationFinal.class)); }
private CodeTypeElement createBaseSpecialization() { CodeTypeElement clazz = createClass(node, null, modifiers(PRIVATE, ABSTRACT, STATIC), specializationTypeName(null), typeSystem.getContext().getType(SpecializationNode.class)); clazz.addOptional(createSpecializationConstructor(clazz, null, null)); clazz.add(new CodeVariableElement(modifiers(PROTECTED, FINAL), nodeType(node), "root")); clazz.addOptional(createUnsupported()); clazz.add(createGetSuppliedChildrenMethod()); clazz.add(createAcceptAndExecute()); for (ExecutableTypeData type : usedTypes) { clazz.add(createFastPathExecuteMethod(null, type, usedTypes)); } return clazz; }
public void createFactoryMethods(CodeTypeElement clazz) { List<ExecutableElement> constructors = GeneratorUtils.findUserConstructors(createdFactoryElement.asType()); for (ExecutableElement constructor : constructors) { clazz.add(createCreateMethod(constructor)); if (constructor instanceof CodeExecutableElement) { ElementUtils.setVisibility(constructor.getModifiers(), Modifier.PRIVATE); } } }
private SpecializationData createSpecializations(CodeTypeElement clazz) { CodeTypeElement baseSpecialization = clazz.add(createBaseSpecialization()); TypeMirror baseSpecializationType = baseSpecialization.asType(); Map<SpecializationData, CodeTypeElement> generated = new LinkedHashMap<>(); List<SpecializationData> generateSpecializations = new ArrayList<>(); generateSpecializations.add(node.getUninitializedSpecialization()); if (needsPolymorphic()) { generateSpecializations.add(node.getPolymorphicSpecialization()); } generateSpecializations.addAll(reachableSpecializations); for (SpecializationData specialization : generateSpecializations) { generated.put(specialization, clazz.add(createSpecialization(specialization, baseSpecializationType))); } baseSpecialization.addOptional(createCreateNext(generated)); baseSpecialization.addOptional(createCreateFallback(generated)); baseSpecialization.addOptional(createCreatePolymorphic(generated)); baseSpecialization.addOptional(createGetNext(baseSpecialization)); for (NodeExecutionData execution : node.getChildExecutions()) { Collection<TypeMirror> specializedTypes = node.findSpecializedTypes(execution); specializedTypes.add(genericType); for (TypeMirror specializedType : specializedTypes) { if (isExecuteChildShared(execution, specializedType)) { baseSpecialization.addOptional(createExecuteChildMethod(execution, specializedType)); } } } return node.getUninitializedSpecialization(); }
public void createFactoryMethods(CodeTypeElement clazz) { List<ExecutableElement> constructors = GeneratorUtils.findUserConstructors(createdFactoryElement.asType()); for (ExecutableElement constructor : constructors) { clazz.add(createCreateMethod(constructor)); if (constructor instanceof CodeExecutableElement) { ElementUtils.setVisibility(constructor.getModifiers(), Modifier.PRIVATE); } } }
public CodeTypeElement create() { String name = typeName(typeSystem); CodeTypeElement clazz = GeneratorUtils.createClass(typeSystem, null, modifiers(PUBLIC, FINAL), name, typeSystem.getTemplateType().asType()); clazz.add(GeneratorUtils.createConstructorUsingFields(modifiers(PROTECTED), clazz)); CodeVariableElement singleton = createSingleton(clazz); clazz.add(singleton); 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))); } List<TypeMirror> lookupTargetTypes = typeSystem.lookupTargetTypes(); for (TypeMirror type : lookupTargetTypes) { clazz.add(createExpectImplicitTypeMethodFlat(type)); clazz.add(createIsImplicitTypeMethodFlat(type)); clazz.add(createAsImplicitTypeMethodFlat(type)); clazz.add(createSpecializeImplictTypeMethodFlat(type)); } return clazz; }
public CodeTypeElement create() { String typeName = typeName(forType); TypeMirror baseType = context.getType(Object.class); CodeTypeElement clazz = GeneratorUtils.createClass(typeSystem, null, modifiers(PUBLIC, FINAL, STATIC), typeName, baseType); for (TypeMirror sourceType : sourceTypes) { CodeVariableElement hasSeen = new CodeVariableElement(modifiers(PUBLIC), context.getType(boolean.class), seenFieldName(sourceType)); hasSeen.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(CompilationFinal.class))); clazz.add(hasSeen); } clazz.add(createConstructor(clazz)); if (isTypeBoxingOptimized(options.monomorphicTypeBoxingOptimization(), forType)) { clazz.add(createIsMonomorphic()); } clazz.add(createCast(false)); clazz.add(createCast(true)); clazz.add(createCheck()); clazz.add(createMerge(clazz)); clazz.add(createCreate(clazz)); return clazz; }
@Override public Void visitType(CodeTypeElement e, Void p) { List<TypeElement> superTypes = ElementUtils.getSuperTypes(e); for (TypeElement type : superTypes) { String qualifiedName = ElementUtils.getQualifiedName(type); if (qualifiedName.equals(Serializable.class.getCanonicalName())) { if (!e.containsField("serialVersionUID")) { e.add(new CodeVariableElement(modifiers(PRIVATE, STATIC, FINAL), ElementUtils.getType(processingEnv, long.class), "serialVersionUID", "1L")); } break; } } return super.visitType(e, p); }
@Override public Void visitType(CodeTypeElement e, Void p) { List<TypeElement> superTypes = ElementUtils.getSuperTypes(e); for (TypeElement type : superTypes) { String qualifiedName = ElementUtils.getQualifiedName(type); if (qualifiedName.equals(Serializable.class.getCanonicalName())) { if (!e.containsField("serialVersionUID")) { e.add(new CodeVariableElement(modifiers(PRIVATE, STATIC, FINAL), ElementUtils.getType(processingEnv, long.class), "serialVersionUID", "1L")); } break; } } return super.visitType(e, p); }
private static CodeTypeElement wrapGeneratedNodes(ProcessorContext context, NodeData node, List<CodeTypeElement> generatedNodes) { if (node.isGenerateFactory()) { // wrap all types into a generated factory CodeTypeElement factoryElement = new NodeFactoryFactory(context, node, generatedNodes.get(0)).create(); for (CodeTypeElement generatedNode : generatedNodes) { factoryElement.add(makeInnerClass(generatedNode)); } return factoryElement; } else { // wrap all types into the first node CodeTypeElement first = generatedNodes.get(0); CodeTypeElement second = first; if (generatedNodes.size() > 1) { second = generatedNodes.get(1); for (CodeTypeElement generatedNode : generatedNodes) { if (first != generatedNode) { first.add(makeInnerClass(generatedNode)); } } } new NodeFactoryFactory(context, node, second).createFactoryMethods(first); ElementUtils.setVisibility(first.getModifiers(), ElementUtils.getVisibility(node.getTemplateType().getModifiers())); return first; } }
private static CodeTypeElement wrapGeneratedNodes(ProcessorContext context, NodeData node, List<CodeTypeElement> generatedNodes) { if (node.isGenerateFactory()) { // wrap all types into a generated factory CodeTypeElement factoryElement = new NodeFactoryFactory(context, node, generatedNodes.get(0)).create(); for (CodeTypeElement generatedNode : generatedNodes) { factoryElement.add(makeInnerClass(generatedNode)); } return factoryElement; } else { // wrap all types into the first node CodeTypeElement first = generatedNodes.get(0); CodeTypeElement second = first; if (generatedNodes.size() > 1) { second = generatedNodes.get(1); for (CodeTypeElement generatedNode : generatedNodes) { if (first != generatedNode) { first.add(makeInnerClass(generatedNode)); } } } new NodeFactoryFactory(context, node, second).createFactoryMethods(first); ElementUtils.setVisibility(first.getModifiers(), ElementUtils.getVisibility(node.getTemplateType().getModifiers())); return first; } }
private static void generateErrorNode(ProcessorContext context, NodeData node, CodeTypeElement type) { for (ExecutableElement superConstructor : GeneratorUtils.findUserConstructors(node.getTemplateType().asType())) { CodeExecutableElement constructor = GeneratorUtils.createConstructorUsingFields(modifiers(), type, superConstructor); ElementUtils.setVisibility(constructor.getModifiers(), ElementUtils.getVisibility(superConstructor.getModifiers())); List<CodeVariableElement> childParameters = new ArrayList<>(); for (NodeChildData child : node.getChildren()) { childParameters.add(new CodeVariableElement(child.getOriginalType(), child.getName())); } constructor.getParameters().addAll(superConstructor.getParameters().size(), childParameters); type.add(constructor); } for (ExecutableElement method : ElementFilter.methodsIn(context.getEnvironment().getElementUtils().getAllMembers(node.getTemplateType()))) { if (method.getModifiers().contains(Modifier.ABSTRACT) && ElementUtils.getVisibility(method.getModifiers()) != Modifier.PRIVATE) { CodeExecutableElement overrideMethod = CodeExecutableElement.clone(context.getEnvironment(), method); overrideMethod.getModifiers().remove(Modifier.ABSTRACT); List<Message> messages = node.collectMessages(); String message = messages.toString(); message = message.replaceAll("\"", "\\\\\""); message = message.replaceAll("\n", "\\\\n"); overrideMethod.createBuilder().startThrow().startNew(context.getType(RuntimeException.class)).doubleQuote("Truffle DSL compiler errors: " + message).end().end(); type.add(overrideMethod); } } }
public CodeTypeElement create() { Modifier visibility = ElementUtils.getVisibility(node.getTemplateType().getModifiers()); TypeMirror nodeFactory = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getTruffleTypes().getNodeFactoryBase()), node.getNodeType()); CodeTypeElement clazz = GeneratorUtils.createClass(node, null, modifiers(), factoryClassName(node), null); if (visibility != null) { clazz.getModifiers().add(visibility); } clazz.getModifiers().add(Modifier.FINAL); if (createdFactoryElement != null) { clazz.setSuperClass(nodeFactory); clazz.add(createNodeFactoryConstructor()); clazz.add(createCreateNodeMethod()); clazz.add(createGetInstanceMethod(visibility)); clazz.add(createInstanceConstant(clazz.asType())); createFactoryMethods(clazz); } return clazz; }
public CodeTypeElement create() { Modifier visibility = ElementUtils.getVisibility(node.getTemplateType().getModifiers()); TypeMirror nodeFactory = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(NodeFactory.class)), node.getNodeType()); CodeTypeElement clazz = GeneratorUtils.createClass(node, null, modifiers(), factoryClassName(node), null); if (visibility != null) { clazz.getModifiers().add(visibility); } clazz.getModifiers().add(Modifier.FINAL); if (createdFactoryElement != null) { clazz.getImplements().add(nodeFactory); CodeAnnotationMirror supressWarnings = new CodeAnnotationMirror(context.getDeclaredType(SuppressWarnings.class)); supressWarnings.setElementValue(supressWarnings.findExecutableElement("value"), new CodeAnnotationValue(Arrays.asList(new CodeAnnotationValue("unchecked"), new CodeAnnotationValue("rawtypes")))); clazz.getAnnotationMirrors().add(supressWarnings); clazz.add(createNodeFactoryConstructor()); clazz.add(createCreateGetNodeClass()); clazz.add(createCreateGetExecutionSignature()); clazz.add(createCreateGetNodeSignatures()); clazz.add(createCreateNodeMethod()); clazz.add(createGetInstanceMethod(visibility)); clazz.add(createInstanceConstant(clazz.asType())); createFactoryMethods(clazz); } return clazz; }
private CodeTypeElement generateWrapperAndFactory(ProcessorContext context, Element e) { CodeTypeElement wrapper = generateWrapper(context, e, false); if (wrapper == null) { return null; } CodeTypeElement factory = generateFactory(context, e, wrapper); // add @SuppressWarnings("deprecation") DeclaredType suppressWarnings = context.getDeclaredType(SuppressWarnings.class); CodeAnnotationMirror suppressWarningsAnnotation = new CodeAnnotationMirror(suppressWarnings); suppressWarningsAnnotation.setElementValue(ElementUtils.findExecutableElement(suppressWarnings, "value"), new CodeAnnotationValue(Arrays.asList(new CodeAnnotationValue("deprecation")))); factory.getAnnotationMirrors().add(suppressWarningsAnnotation); wrapper.getModifiers().add(Modifier.STATIC); factory.add(wrapper); assertNoErrorExpected(e); return factory; }
@Override public CodeTypeElement create(ProcessorContext context, TypeSystemData typeSystem) { CodeTypeElement clazz = new TypeClassFactory(context, typeSystem).create(); if (typeSystem.getOptions().implicitCastOptimization().isMergeCasts()) { for (TypeMirror type : typeSystem.lookupTargetTypes()) { clazz.add(new ImplicitCastNodeFactory(context, typeSystem, type).create()); } } return clazz; }
private CodeTree wrapInAMethod(CodeTreeBuilder parent, SpecializationGroup group, FrameState frameState, String suffix, CodeTree codeTree) { CodeExecutableElement parentMethod = (CodeExecutableElement) parent.findMethod(); CodeTypeElement parentClass = (CodeTypeElement) parentMethod.getEnclosingElement(); String name = parentMethod.getSimpleName().toString() + "_" + suffix + (boxingSplitIndex++); CodeExecutableElement method = parentClass.add( frameState.createMethod(modifiers(Modifier.PRIVATE), parentMethod.getReturnType(), name, FRAME_VALUE, STATE_VALUE)); CodeTreeBuilder builder = method.createBuilder(); builder.tree(codeTree); method.getThrownTypes().addAll(parentMethod.getThrownTypes()); addExplodeLoop(builder, group); CodeTreeBuilder parentBuilder = parent.create(); parentBuilder.startReturn(); parentBuilder.startCall(method.getSimpleName().toString()); frameState.addReferencesTo(parentBuilder, FRAME_VALUE, STATE_VALUE); parentBuilder.end(); parentBuilder.end(); return parentBuilder.build(); }
private void addUnsupportedMethod(CodeTypeElement clazz) { CodeVariableElement seenUnsupportedField = new CodeVariableElement(modifiers(PRIVATE), getType(boolean.class), "seenUnsupported0"); seenUnsupportedField.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(CompilationFinal.class))); clazz.add(seenUnsupportedField); LocalContext locals = LocalContext.load(this); CodeExecutableElement method = locals.createMethod(modifiers(PRIVATE), getType(UnsupportedSpecializationException.class), "unsupported", varArgsThreshold); CodeTreeBuilder builder = method.createBuilder(); builder.startIf().string("!").string(seenUnsupportedField.getName()).end().startBlock(); builder.startStatement().startStaticCall(getType(CompilerDirectives.class), "transferToInterpreterAndInvalidate").end().end(); builder.startStatement().string(seenUnsupportedField.getName()).string(" = true").end(); builder.end(); builder.startReturn(); builder.startNew(getType(UnsupportedSpecializationException.class)); builder.string("this"); builder.tree(createGetSuppliedChildren()); locals.addReferencesTo(builder); builder.end(); builder.end(); clazz.add(method); }