private void organizeImpl() { ImportTypeReferenceVisitor reference = new ImportTypeReferenceVisitor(); topLevelClass.accept(reference, null); }
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; }
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(); }
@Override public Void visitType(CodeTypeElement e, Void p) { visitAnnotations(e, e.getAnnotationMirrors()); visitTypeReference(e, e.getSuperclass()); for (TypeMirror type : e.getImplements()) { visitTypeReference(e, type); } return super.visitType(e, p); }
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 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; }
private void writeClassImpl(CodeTypeElement e) { for (AnnotationMirror annotation : e.getAnnotationMirrors()) { visitAnnotation(e, annotation); writeLn(); writeModifiers(e.getModifiers(), true); if (e.getKind() == ElementKind.ENUM) { write("enum "); } else { write("class "); write(e.getSimpleName()); if (e.getSuperclass() != null && !getQualifiedName(e.getSuperclass()).equals("java.lang.Object")) { write(" extends ").write(useImport(e, e.getSuperclass())); if (e.getImplements().size() > 0) { write(" implements "); for (int i = 0; i < e.getImplements().size(); i++) { write(useImport(e, e.getImplements().get(i))); if (i < e.getImplements().size() - 1) { write(", "); VariableElement field = staticFields.get(i); field.accept(this, null); if (e.getKind() == ElementKind.ENUM && i < staticFields.size() - 1) { write(","); writeLn(); for (ExecutableElement method : ElementFilter.constructorsIn(e.getEnclosedElements())) { method.accept(this, null);
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; }
PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(sourceType); Set<Modifier> typeModifiers = ElementUtils.modifiers(Modifier.PUBLIC, Modifier.FINAL); CodeTypeElement factory = new CodeTypeElement(typeModifiers, ElementKind.CLASS, pack, createWrapperClassName(sourceType)); factory.getImplements().add(new CodeTypeMirror.DeclaredCodeTypeMirror(ElementUtils.fromTypeMirror(factoryType), Arrays.asList(sourceType.asType()))); ExecutableElement constructor = ElementFilter.constructorsIn(wrapper.getEnclosedElements()).iterator().next(); builder.startReturn().startNew(wrapper.asType()); if (firstParameterReference != null) { builder.string(firstParameterReference); builder.end().end(); factory.add(createMethod);
public CodeTypeElement create() { CodeTypeElement clazz = GeneratorUtils.createClass(node, null, modifiers(FINAL), nodeTypeName(node), node.getTemplateType().asType()); ElementUtils.setVisibility(clazz.getModifiers(), ElementUtils.getVisibility(node.getTemplateType().getModifiers())); clazz.addOptional(createAccessChildMethod(child)); clazz.add(new CodeVariableElement(modifiers(PRIVATE, FINAL), field.getType(), field.getName())); if (field.getGetter() != null && field.getGetter().getModifiers().contains(Modifier.ABSTRACT)) { CodeExecutableElement method = CodeExecutableElement.clone(context.getEnvironment(), field.getGetter()); method.getModifiers().remove(Modifier.ABSTRACT); method.createBuilder().startReturn().string("this.").string(field.getName()).end(); clazz.add(method); clazz.add(createNodeConstructor(clazz, superConstructor)); clazz.add(createNodeField(PRIVATE, execution.getNodeType(), nodeFieldName(execution), Child.class)); clazz.add(createNodeField(PRIVATE, getType(Class.class), polymorphicTypeProfileFieldName(execution), CompilationFinal.class)); clazz.add(createNodeField(PRIVATE, getType(boolean.class), excludedFieldName(specialization), CompilationFinal.class)); clazz.add(createGetCostMethod()); clazz.add(createExecutableTypeOverride(usedTypes, execType)); clazz.add(createExecutableTypeOverride(usedTypes, execType)); clazz.getImplements().add(getType(SpecializedNode.class)); clazz.add(createMethodGetSpecializationNode()); clazz.add(createDeepCopyMethod());
CodeTypeElement wrapperType = new CodeTypeElement(typeModifiers, ElementKind.CLASS, pack, wrapperClassName); TypeMirror resolvedSuperType = sourceType.asType(); wrapperType.setSuperClass(resolvedSuperType); if (topLevelClass) { wrapperType.getImplements().add(context.getType(InstrumentableNode.WrapperNode.class)); } else { wrapperType.getImplements().add(context.getType(com.oracle.truffle.api.instrumentation.InstrumentableFactory.WrapperNode.class)); wrapperType.add(createNodeChild(context, sourceType.asType(), FIELD_DELEGATE)); wrapperType.add(createNodeChild(context, context.getType(ProbeNode.class), FIELD_PROBE)); wrapperType.add(wrappedConstructor); for (VariableElement field : wrapperType.getFields()) { CodeExecutableElement getter = new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), field.asType(), "get" + ElementUtils.firstLetterUpperCase(field.getSimpleName().toString())); getter.createBuilder().startReturn().string(field.getSimpleName().toString()).end(); wrapperType.add(getter); CodeExecutableElement getInstrumentationTags = new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), returnType, METHOD_GET_NODE_COST); getInstrumentationTags.createBuilder().startReturn().staticReference(returnType, "NONE").end(); wrapperType.add(getInstrumentationTags); wrapperType.add(wrappedExecute); wrapperType.add(generatedMethod);
static CodeTypeElement createClass(Template sourceModel, TemplateMethod sourceMethod, Set<Modifier> modifiers, String simpleName, TypeMirror superType) { TypeElement templateType = sourceModel.getTemplateType(); ProcessorContext context = ProcessorContext.getInstance(); PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(templateType); CodeTypeElement clazz = new CodeTypeElement(modifiers, ElementKind.CLASS, pack, simpleName); TypeMirror resolvedSuperType = superType; if (resolvedSuperType == null) { resolvedSuperType = context.getType(Object.class); } clazz.setSuperClass(resolvedSuperType); CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) context.getType(GeneratedBy.class)); generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType())); if (sourceMethod != null && sourceMethod.getMethod() != null) { generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(sourceMethod.createReferenceName())); } clazz.addAnnotationMirror(generatedByAnnotation); return clazz; }
return; unit.setGeneratorAnnotationMirror(model.getTemplateTypeAnnotation()); unit.setGeneratorElement(model.getTemplateType()); unit.accept(new GenerateOverrideVisitor(overrideType), null); unit.accept(new FixWarningsVisitor(context.getEnvironment(), unusedType, overrideType), null); unit.accept(new CodeWriter(context.getEnvironment(), element), null);
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; }
public CodeTypeElement create(CodeTypeElement clazz) { for (NodeChildData child : node.getChildren()) { clazz.addOptional(createAccessChildMethod(child)); clazz.add(new CodeVariableElement(modifiers(PRIVATE, FINAL), field.getType(), field.getName())); if (field.getGetter() != null && field.getGetter().getModifiers().contains(Modifier.ABSTRACT)) { CodeExecutableElement method = CodeExecutableElement.clone(context.getEnvironment(), method.getModifiers().remove(Modifier.ABSTRACT); method.createBuilder().startReturn().string("this.").string(field.getName()).end(); clazz.add(method); clazz.add(createNodeConstructor(clazz, superConstructor)); clazz.add(createNodeField(PRIVATE, execution.getNodeType(), nodeFieldName(execution), Child.class)); clazz.add(createFallbackGuard()); clazz.addOptional(createExecuteAndSpecialize()); if (shouldReportPolymorphism(node, reachableSpecializations)) { clazz.addOptional(createCheckForPolymorphicSpecialize()); if (requiresCacheCheck()) { clazz.addOptional(createCountCaches()); clazz.add(createGetCostMethod()); clazz.addOptional(TypeSystemCodeGenerator.createExpectMethod(PRIVATE, typeSystem, context.getType(Object.class), type));
annotationType = Child.class; if (specialization.getMaximumNumberOfInstances() > 1) { cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class)); getNodeCost.createBuilder().startReturn().staticReference(context.getType(NodeCost.class), "NONE").end(); cacheType.add(getNodeCost); cacheType.add(createNodeField(null, cacheType.asType(), "next_", annotationType)); cacheType.add(GeneratorUtils.createConstructorUsingFields(modifiers(), cacheType)); cacheType.getEnclosedElements().addAll(fields); clazz.add(createNodeField(PRIVATE, cacheType.asType(), createSpecializationFieldName(specialization), annotationType)); clazz.add(cacheType); clazz.getEnclosedElements().addAll(fields);
PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(sourceType); Set<Modifier> typeModifiers = ElementUtils.modifiers(Modifier.FINAL); CodeTypeElement descriptors = new CodeTypeElement(typeModifiers, ElementKind.CLASS, pack, optionsClassName); DeclaredType optionDescriptorsType = context.getDeclaredType(OptionDescriptors.class); descriptors.getImplements().add(optionDescriptorsType); descriptors.add(getMethod); descriptors.add(iteratorMethod);
private Element createGetNext(CodeTypeElement type) { if (!nextUsed) { return null; } CodeExecutableElement method = new CodeExecutableElement(modifiers(PROTECTED, FINAL), type.asType(), "getNext"); CodeTreeBuilder builder = method.createBuilder(); builder.startReturn().cast(type.asType(), CodeTreeBuilder.singleString("this.next")).end(); return method; }
public CodeVariableElement declareFields(CodeTypeElement clazz) { return clazz.add(createNodeField(PRIVATE, bitSetType, name + "_", CompilationFinal.class)); }
@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); }