Search in sources :

Example 11 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class CaseClassMetadata method createApplyMethod.

private CodeMethod createApplyMethod() {
    // static final func apply<TypeParams...>(classParams...: ClassParamTypes...) -> This
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = this.theClass.getThisType();
    final CodeMethod applyMethod = new CodeMethod(this.theClass, Names.apply, type, attributes);
    applyMethod.setPosition(position);
    applyMethod.getTypeParameters().addAll(this.theClass.getTypeParameters());
    this.copyClassParameters(applyMethod);
    // = new This<TypeParams...>(classParams...)
    final ArgumentList arguments = new ArgumentList();
    for (IParameter param : applyMethod.getParameters()) {
        // no need to check for override class parameters here, since we are dealing with parameters of the
        // apply method
        final IValue access;
        if (param.isVarargs()) {
            access = new VarargsOperator(position, new FieldAccess(param));
        } else {
            access = new FieldAccess(param);
        }
        arguments.add(access);
    }
    // = new This(params...)
    applyMethod.setValue(new ConstructorCall(this.theClass.position(), this.theClass.getThisType(), arguments));
    return applyMethod;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) VarargsOperator(dyvilx.tools.compiler.ast.expression.intrinsic.VarargsOperator) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall) IType(dyvilx.tools.compiler.ast.type.IType)

Example 12 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class CaseClassMetadata method createUnapplyAnyMethod.

private CodeMethod createUnapplyAnyMethod() {
    // static final func unapply<TypeParams...>(value: any) -> (T...)?
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = NullableType.apply(this.getUnapplyReturnType());
    final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
    unapply.setPosition(position);
    unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
    final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, Types.NULLABLE_ANY);
    unapply.getParameters().add(parameter);
    // = (param is This) ? unapply(param as This) : null
    final InstanceOfOperator isOperator = new InstanceOfOperator(new FieldAccess(parameter), this.theClass.getClassType());
    final CastOperator castOperator = new CastOperator(new FieldAccess(parameter), this.theClass.getThisType());
    final IValue call = new MethodCall(position, null, Names.unapply, new ArgumentList(castOperator));
    final IfStatement ifStatement = new IfStatement(isOperator, call, new NullValue());
    unapply.setValue(ifStatement);
    return unapply;
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) CastOperator(dyvilx.tools.compiler.ast.expression.CastOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall) IType(dyvilx.tools.compiler.ast.type.IType) InstanceOfOperator(dyvilx.tools.compiler.ast.expression.InstanceOfOperator) IfStatement(dyvilx.tools.compiler.ast.statement.IfStatement) IValue(dyvilx.tools.compiler.ast.expression.IValue) NullValue(dyvilx.tools.compiler.ast.expression.constant.NullValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess)

Example 13 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class EnumClassMetadata method createValuesField.

private Field createValuesField() {
    final SourcePosition position = this.theClass.position();
    final ArrayType type = new ArrayType(this.theClass.getClassType(), Mutability.IMMUTABLE);
    final AttributeList attributes = AttributeList.of(Modifiers.PRIVATE | Modifiers.CONST | Modifiers.SYNTHETIC);
    final Field field = new Field(this.theClass, position, Names.$VALUES, type, attributes);
    field.setValue(this.createValuesArray());
    return field;
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IField(dyvilx.tools.compiler.ast.field.IField) Field(dyvilx.tools.compiler.ast.field.Field) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) SourcePosition(dyvil.source.position.SourcePosition)

Example 14 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class EnumClassMetadata method createValuesMethod.

private CodeMethod createValuesMethod() {
    // public static func values() -> [final EnumType]
    final SourcePosition position = this.theClass.position();
    final ArrayType type = new ArrayType(this.theClass.getClassType());
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC);
    final CodeMethod method = new CodeMethod(this.theClass, Names.values, type, attributes);
    method.setPosition(position);
    // = $VALUES.copy()
    final FieldAccess valuesField = new FieldAccess(position, null, Names.$VALUES);
    final MethodCall cloneCall = new MethodCall(position, valuesField, Name.fromRaw("copy"), ArgumentList.EMPTY);
    method.setValue(cloneCall);
    return method;
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) SourcePosition(dyvil.source.position.SourcePosition)

Example 15 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class ModifierUtil method checkModifiers.

public static void checkModifiers(IMember member, MarkerList markers) {
    final AttributeList attributes = member.getAttributes();
    final MemberKind memberKind = member.getKind();
    final int defaultAccess = memberKind.getDefaultAccess(member);
    StringBuilder errorBuilder = null;
    for (Attribute modifier : attributes) {
        if (!memberKind.isAttributeAllowed(modifier)) {
            if (errorBuilder == null) {
                errorBuilder = new StringBuilder();
            } else {
                errorBuilder.append(", ");
            }
            modifier.toString(errorBuilder);
        }
        final int visibility = modifier.flags() & Modifiers.VISIBILITY_MODIFIERS;
        if (visibility != 0 && visibility == defaultAccess) {
            markers.add(Markers.semantic(member.getPosition(), "modifiers.visibility.default", Util.memberNamed(member), ModifierUtil.accessModifiersToString(visibility)));
        }
    }
    if (errorBuilder != null) {
        markers.add(Markers.semanticError(member.getPosition(), "modifiers.illegal", Util.memberNamed(member), errorBuilder.toString()));
    }
    if (!attributes.hasAnyFlag(Modifiers.VISIBILITY_MODIFIERS)) {
        // If there is no explicit or implicit visibility modifier already, add the default one
        attributes.addFlag(defaultAccess);
    }
}
Also used : MemberKind(dyvilx.tools.compiler.ast.member.MemberKind) Attribute(dyvilx.tools.compiler.ast.attribute.Attribute) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList)

Aggregations

AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)20 SourcePosition (dyvil.source.position.SourcePosition)7 IValue (dyvilx.tools.compiler.ast.expression.IValue)6 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)6 IType (dyvilx.tools.compiler.ast.type.IType)6 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)5 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)4 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)4 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)4 Name (dyvil.lang.Name)2 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)2 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)2 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)2 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)2 IToken (dyvilx.tools.parsing.token.IToken)2 Reified (dyvil.annotation.Reified)1 Modifiers (dyvil.reflect.Modifiers)1 Opcodes (dyvil.reflect.Opcodes)1 AnnotatableVisitor (dyvilx.tools.asm.AnnotatableVisitor)1 AnnotationVisitor (dyvilx.tools.asm.AnnotationVisitor)1