Search in sources :

Example 6 with PropertyModel

use of com.oracle.truffle.object.dsl.processor.model.PropertyModel in project graal by oracle.

the class LayoutParser method addConstructorProperties.

private void addConstructorProperties(ExecutableElement methodElement, List<? extends VariableElement> parameters) {
    List<? extends VariableElement> ownParameters = parameters;
    if (superLayout != null) {
        final List<PropertyModel> superProperties = superLayout.getAllInstanceProperties();
        checkSharedParameters(methodElement, parameters, superProperties);
        ownParameters = parameters.subList(superProperties.size(), parameters.size());
    }
    for (VariableElement element : ownParameters) {
        final String parameterName = element.getSimpleName().toString();
        if (parameterName.equals("factory")) {
            processor.reportError(methodElement, "Factory is a confusing name for a property");
        }
        if (constructorProperties.contains(parameterName)) {
            processor.reportError(methodElement, "The property %s is duplicated");
        } else {
            constructorProperties.add(parameterName);
            final PropertyBuilder property = getProperty(parameterName);
            setPropertyType(element, property, element.asType());
            parseConstructorParameterAnnotations(property, element);
        }
    }
}
Also used : PropertyModel(com.oracle.truffle.object.dsl.processor.model.PropertyModel) VariableElement(javax.lang.model.element.VariableElement) PropertyBuilder(com.oracle.truffle.object.dsl.processor.model.PropertyBuilder)

Example 7 with PropertyModel

use of com.oracle.truffle.object.dsl.processor.model.PropertyModel in project graal by oracle.

the class LayoutParser method parseShapeConstructor.

private void parseShapeConstructor(ExecutableElement methodElement) {
    List<? extends VariableElement> parameters = methodElement.getParameters();
    if (!parameters.isEmpty()) {
        hasShapeProperties = true;
    }
    if (superLayout != null) {
        final List<PropertyModel> superShapeProperties = superLayout.getAllShapeProperties();
        checkSharedParameters(methodElement, parameters, superShapeProperties);
        parameters = parameters.subList(superShapeProperties.size(), parameters.size());
    }
    for (VariableElement element : parameters) {
        final String parameterName = element.getSimpleName().toString();
        constructorProperties.add(parameterName);
        final PropertyBuilder property = getProperty(parameterName);
        setPropertyType(element, property, element.asType());
        parseConstructorParameterAnnotations(property, element);
        property.setIsShapeProperty(true);
    }
}
Also used : PropertyModel(com.oracle.truffle.object.dsl.processor.model.PropertyModel) VariableElement(javax.lang.model.element.VariableElement) PropertyBuilder(com.oracle.truffle.object.dsl.processor.model.PropertyBuilder)

Example 8 with PropertyModel

use of com.oracle.truffle.object.dsl.processor.model.PropertyModel in project graal by oracle.

the class LayoutGenerator method generateFactory.

private void generateFactory(final PrintStream stream) {
    // The shortcut factory when there are no shape properties
    if (!layout.hasShapeProperties()) {
        stream.println("    @Override");
        stream.printf("    public DynamicObject create%s(", layout.getName());
        if (layout.getAllProperties().isEmpty()) {
            stream.println(") {");
        } else {
            stream.println();
            for (PropertyModel property : layout.getAllProperties()) {
                stream.printf("            %s %s", property.getType().toString(), property.getName());
                if (property == layout.getAllProperties().get(layout.getAllProperties().size() - 1)) {
                    stream.println(") {");
                } else {
                    stream.println(",");
                }
            }
        }
        stream.printf("        return create%s(%s_FACTORY", layout.getName(), NameUtils.identifierToConstant(layout.getName()));
        if (layout.getAllProperties().isEmpty()) {
            stream.println(");");
        } else {
            stream.println(",");
            for (PropertyModel property : layout.getAllProperties()) {
                stream.printf("            %s", property.getName());
                if (property == layout.getAllProperties().get(layout.getAllProperties().size() - 1)) {
                    stream.println(");");
                } else {
                    stream.println(",");
                }
            }
        }
        stream.println("    }");
        stream.println("    ");
    }
    // The full factory
    final boolean builder = layout.hasBuilder();
    final String methodName = builder ? "build" : "create" + layout.getName();
    final String returnType = builder ? "Object[]" : "DynamicObject";
    if (layout.hasShapeProperties()) {
        stream.println("    @Override");
        stream.print("    public");
    } else {
        stream.print("    private");
        if (!layout.hasObjectTypeGuard()) {
            stream.printf(" static");
        }
    }
    if (layout.hasInstanceProperties()) {
        stream.printf(" %s %s(%n", returnType, methodName);
        if (!builder) {
            stream.println("            DynamicObjectFactory factory,");
        }
        for (PropertyModel property : layout.getAllInstanceProperties()) {
            stream.printf("            %s %s", property.getType().toString(), property.getName());
            if (property == layout.getAllProperties().get(layout.getAllProperties().size() - 1)) {
                stream.println(") {");
            } else {
                stream.println(",");
            }
        }
    } else {
        String factoryArg = builder ? "" : "DynamicObjectFactory factory";
        stream.printf(" %s %s(%s) {%n", returnType, methodName, factoryArg);
    }
    if (!builder) {
        stream.println("        assert factory != null;");
        stream.println("        CompilerAsserts.partialEvaluationConstant(factory);");
        stream.printf("        assert creates%s(factory);%n", layout.getName());
        for (PropertyModel property : layout.getAllInstanceProperties()) {
            stream.printf("        assert factory.getShape().hasProperty(%s_IDENTIFIER);%n", NameUtils.identifierToConstant(property.getName()));
        }
    }
    for (PropertyModel property : layout.getAllInstanceProperties()) {
        if (!property.getType().getKind().isPrimitive() && !property.isNullable()) {
            stream.printf("        assert %s != null;%n", property.getName());
        }
    }
    if (layout.hasInstanceProperties()) {
        if (builder) {
            stream.println("        return new Object[] { ");
        } else {
            stream.println("        return factory.newInstance(");
        }
        for (PropertyModel property : layout.getAllInstanceProperties()) {
            if (property.isVolatile()) {
                if (property.getType().getKind() == TypeKind.INT) {
                    stream.printf("            new AtomicInteger(%s)", property.getName());
                } else if (property.getType().getKind() == TypeKind.BOOLEAN) {
                    stream.printf("            new AtomicBoolean(%s)", property.getName());
                } else {
                    stream.printf("            new AtomicReference<>(%s)", property.getName());
                }
            } else {
                stream.printf("            %s", property.getName());
            }
            if (property == layout.getAllProperties().get(layout.getAllProperties().size() - 1)) {
                stream.println(builder ? " };" : ");");
            } else {
                stream.println(",");
            }
        }
    } else {
        if (builder) {
            stream.println("        return new Object[0];");
        } else {
            stream.println("        return factory.newInstance();");
        }
    }
    stream.println("    }");
    stream.println("    ");
}
Also used : PropertyModel(com.oracle.truffle.object.dsl.processor.model.PropertyModel)

Example 9 with PropertyModel

use of com.oracle.truffle.object.dsl.processor.model.PropertyModel in project graal by oracle.

the class LayoutGenerator method generateAllocator.

private void generateAllocator(final PrintStream stream) {
    if (layout.getSuperLayout() == null) {
        stream.print("    protected static final Layout LAYOUT = Layout.newLayout()");
        for (Layout.ImplicitCast implicitCast : layout.getImplicitCasts()) {
            stream.print(".addAllowedImplicitCast(Layout.ImplicitCast.");
            stream.print(implicitCast.name());
            stream.print(")");
        }
        stream.println(".build();");
        stream.printf("    protected static final Shape.Allocator %S_ALLOCATOR = LAYOUT.createAllocator();%n", NameUtils.identifierToConstant(layout.getName()));
    } else {
        stream.printf("    protected static final Shape.Allocator %S_ALLOCATOR = LAYOUT.createAllocator();%n", NameUtils.identifierToConstant(layout.getName()));
        stream.println("    ");
        if (layout.getSuperLayout().hasInstanceProperties()) {
            stream.println("    static {");
            for (PropertyModel property : layout.getSuperLayout().getAllInstanceProperties()) {
                final List<String> modifiers = new ArrayList<>();
                if (!property.isNullable()) {
                    modifiers.add("LocationModifier.NonNull");
                }
                if (property.isFinal()) {
                    modifiers.add("LocationModifier.Final");
                }
                final String modifiersExpression;
                if (modifiers.isEmpty()) {
                    modifiersExpression = "";
                } else {
                    final StringBuilder modifiersExpressionBuilder = new StringBuilder();
                    modifiersExpressionBuilder.append(", EnumSet.of(");
                    for (String modifier : modifiers) {
                        if (!modifier.equals(modifiers.get(0))) {
                            modifiersExpressionBuilder.append(", ");
                        }
                        modifiersExpressionBuilder.append(modifier);
                    }
                    modifiersExpressionBuilder.append(")");
                    modifiersExpression = modifiersExpressionBuilder.toString();
                }
                final String locationType;
                if (property.isVolatile()) {
                    if (property.getType().getKind() == TypeKind.INT) {
                        locationType = "AtomicInteger";
                    } else if (property.getType().getKind() == TypeKind.BOOLEAN) {
                        locationType = "AtomicBoolean";
                    } else {
                        locationType = "AtomicReference";
                    }
                } else {
                    locationType = NameUtils.typeWithoutParameters(property.getType().toString());
                }
                stream.printf("         %s_ALLOCATOR.locationForType(%s.class%s);%n", NameUtils.identifierToConstant(layout.getName()), locationType, modifiersExpression);
            }
            stream.println("    }");
            stream.println("    ");
        }
    }
}
Also used : Layout(com.oracle.truffle.api.object.Layout) PropertyModel(com.oracle.truffle.object.dsl.processor.model.PropertyModel) ArrayList(java.util.ArrayList)

Example 10 with PropertyModel

use of com.oracle.truffle.object.dsl.processor.model.PropertyModel in project graal by oracle.

the class LayoutGenerator method generateShapeFactory.

private void generateShapeFactory(final PrintStream stream) {
    if (layout.hasShapeProperties()) {
        stream.println("    @Override");
        stream.print("    public");
    } else {
        stream.print("    private static");
    }
    stream.printf(" DynamicObjectFactory create%sShape(", layout.getName());
    if (layout.hasShapeProperties()) {
        stream.println();
        for (PropertyModel property : layout.getAllShapeProperties()) {
            stream.printf("            %s %s", property.getType().toString(), property.getName());
            if (property == layout.getAllShapeProperties().get(layout.getAllShapeProperties().size() - 1)) {
                stream.println(") {");
            } else {
                stream.println(",");
            }
        }
    } else {
        stream.println(") {");
    }
    stream.printf("        return LAYOUT.createShape(new %sType(", layout.getName());
    if (layout.hasShapeProperties()) {
        stream.println();
        iterateProperties(layout.getAllShapeProperties(), new PropertyIteratorAction() {

            @Override
            public void run(PropertyModel property, boolean last) {
                stream.printf("                %s", property.getName());
                if (last) {
                    stream.println("))");
                } else {
                    stream.println(",");
                }
            }
        });
    } else {
        stream.println("))");
    }
    iterateProperties(layout.getAllInstanceProperties(), new PropertyIteratorAction() {

        @Override
        public void run(PropertyModel property, boolean last) {
            stream.printf("            .addProperty(%s_PROPERTY)%n", NameUtils.identifierToConstant(property.getName()));
        }
    });
    stream.println("            .createFactory();");
    stream.println("    }");
    stream.println("    ");
}
Also used : PropertyModel(com.oracle.truffle.object.dsl.processor.model.PropertyModel)

Aggregations

PropertyModel (com.oracle.truffle.object.dsl.processor.model.PropertyModel)10 VariableElement (javax.lang.model.element.VariableElement)3 PropertyBuilder (com.oracle.truffle.object.dsl.processor.model.PropertyBuilder)2 ArrayList (java.util.ArrayList)2 Layout (com.oracle.truffle.api.object.Layout)1