Search in sources :

Example 86 with IType

use of dyvilx.tools.compiler.ast.type.IType in project Dyvil by Dyvil.

the class CodeClass method check.

@Override
public void check(MarkerList markers, IContext context) {
    context = context.push(this);
    this.attributes.check(markers, context, this.getElementType());
    if (this.typeParameters != null) {
        this.typeParameters.check(markers, context);
    }
    this.parameters.check(markers, context);
    if (this.superType != null) {
        this.superType.check(markers, context);
        final IClass superClass = this.superType.getTheClass();
        if (superClass != null) {
            final int modifiers = superClass.getAttributes().flags();
            if ((modifiers & Modifiers.CLASS_TYPE_MODIFIERS) != 0) {
                markers.add(Markers.semanticError(this.position, "class.extend.type", ModifierUtil.classTypeToString(modifiers), superClass.getName()));
            } else if ((modifiers & Modifiers.FINAL) != 0) {
                markers.add(Markers.semanticError(this.position, "class.extend.final", superClass.getName()));
            }
        }
    }
    if (this.interfaces != null) {
        for (IType type : this.interfaces) {
            type.check(markers, context);
            if (!type.isResolved()) {
                continue;
            }
            final IClass theClass = type.getTheClass();
            if (theClass == null) {
                continue;
            }
            if (!theClass.isInterface()) {
                final String classType = ModifierUtil.classTypeToString(theClass.getAttributes().flags());
                markers.add(Markers.semanticError(this.position, "class.implement.type", classType, theClass.getName()));
            }
        }
    }
    this.metadata.check(markers, context);
    if (this.body != null) {
        this.body.check(markers, context);
    }
    context.pop();
}
Also used : IType(dyvilx.tools.compiler.ast.type.IType)

Example 87 with IType

use of dyvilx.tools.compiler.ast.type.IType 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 88 with IType

use of dyvilx.tools.compiler.ast.type.IType 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 89 with IType

use of dyvilx.tools.compiler.ast.type.IType in project Dyvil by Dyvil.

the class EnumClassMetadata method createFromOrdMethod.

private CodeMethod createFromOrdMethod() {
    // @BytecodeName("valueOf")
    // public static func from(ordinal: int) -> EnumType
    final SourcePosition position = this.theClass.position();
    final IType type = this.theClass.getClassType();
    final CodeMethod method = new CodeMethod(this.theClass, Name.fromRaw("from"), type, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
    method.setPosition(position);
    method.setInternalName("valueOf");
    final CodeParameter parameter = new CodeParameter(Name.fromRaw("ordinal"), Types.INT);
    method.getParameters().add(parameter);
    // = $VALUES[ordinal]
    final FieldAccess valuesField = new FieldAccess(position, null, Names.$VALUES);
    final SubscriptAccess getCall = new SubscriptAccess(position, valuesField, new ArgumentList(new FieldAccess(parameter)));
    method.setValue(getCall);
    return method;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Example 90 with IType

use of dyvilx.tools.compiler.ast.type.IType in project Dyvil by Dyvil.

the class EnumClassMetadata method resolveTypesPre.

@Override
public void resolveTypesPre(MarkerList markers, IContext context) {
    super.resolveTypesPre(markers, context);
    this.theClass.getAttributes().addFlag(Modifiers.FINAL);
    // Ensure the super type is Enum<TYPE>
    final IType classType = this.theClass.getClassType();
    final IType superType = this.theClass.getSuperType();
    if (// default
    superType != Types.OBJECT) {
        markers.add(Markers.semanticError(superType.getPosition(), "enum.super_class"));
    }
    this.theClass.setSuperType(new ClassGenericType(Types.ENUM_CLASS, classType));
}
Also used : ClassGenericType(dyvilx.tools.compiler.ast.type.generic.ClassGenericType) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

IType (dyvilx.tools.compiler.ast.type.IType)159 IClass (dyvilx.tools.compiler.ast.classes.IClass)26 Marker (dyvilx.tools.parsing.marker.Marker)23 IValue (dyvilx.tools.compiler.ast.expression.IValue)21 TypeParameterList (dyvilx.tools.compiler.ast.generic.TypeParameterList)13 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)11 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)10 SourcePosition (dyvil.source.position.SourcePosition)9 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)9 ITypeParameter (dyvilx.tools.compiler.ast.generic.ITypeParameter)9 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)9 TypeList (dyvilx.tools.compiler.ast.type.TypeList)7 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)6 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)6 IVariable (dyvilx.tools.compiler.ast.field.IVariable)6 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)6 MethodWriter (dyvilx.tools.compiler.backend.MethodWriter)6 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)5 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)5 Name (dyvil.lang.Name)4