Search in sources :

Example 6 with ArrayType

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

the class ArrayExpr method setElementType.

public void setElementType(IType elementType) {
    this.elementType = elementType;
    this.arrayType = new ArrayType(elementType, Mutability.IMMUTABLE);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType)

Example 7 with ArrayType

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

the class ArrayExpr method isType.

@Override
public boolean isType(IType type) {
    final ArrayType arrayType = type.extract(ArrayType.class);
    if (arrayType == null) {
        return type.getTheClass() == Types.OBJECT_CLASS || type.getAnnotation(LazyFields.ARRAY_CONVERTIBLE) != null;
    }
    final IType elementType = arrayType.getElementType();
    return this.values.isType(elementType);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IType(dyvilx.tools.compiler.ast.type.IType)

Example 8 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType 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 9 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType 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 10 with ArrayType

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

the class ArrayExpr method withType.

@Override
public IValue withType(IType type, ITypeContext typeContext, MarkerList markers, IContext context) {
    final IType elementType;
    final Mutability mutability;
    final ArrayType arrayType = type.extract(ArrayType.class);
    if (arrayType == null) {
        final Annotation annotation;
        if ((annotation = type.getAnnotation(LazyFields.ARRAY_CONVERTIBLE)) != null) {
            return new LiteralConversion(this, annotation, this.values).withType(type, typeContext, markers, context);
        }
        if (type.getTheClass() != Types.OBJECT_CLASS) {
            return null;
        }
        // Compute element type from scratch
        elementType = this.getElementType();
        mutability = Mutability.IMMUTABLE;
    } else {
        // If the type is an array type, get it's element type
        elementType = arrayType.getElementType();
        mutability = type.getMutability();
    }
    final int size = this.values.size();
    if (size == 0) {
        this.elementType = elementType.getConcreteType(ITypeContext.DEFAULT);
        this.arrayType = new ArrayType(this.elementType, mutability);
        return this;
    }
    for (int i = 0; i < size; i++) {
        final IValue value = TypeChecker.convertValue(this.values.get(i), elementType, typeContext, markers, context, LazyFields.ELEMENT_MARKER_SUPPLIER);
        this.values.set(i, value);
    }
    this.elementType = null;
    final int typecode = elementType.getTypecode();
    if (typecode < 0) {
        // local element type is an object type, so we infer the reference version of the computed element type
        this.elementType = this.getElementType().getObjectType();
    } else if (typecode != this.getElementType().getTypecode()) {
        // local element type is a primitive type, but a different one from the computed element type,
        // so we infer the local one
        this.elementType = elementType;
    }
    // else: the above call to this.getElementType() has set this.elementType already
    this.arrayType = new ArrayType(this.elementType, mutability);
    return this;
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) Mutability(dyvilx.tools.compiler.ast.type.Mutability) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)15 IType (dyvilx.tools.compiler.ast.type.IType)6 SourcePosition (dyvil.source.position.SourcePosition)2 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)2 IValue (dyvilx.tools.compiler.ast.expression.IValue)2 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)2 Label (dyvilx.tools.asm.Label)1 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 IClass (dyvilx.tools.compiler.ast.classes.IClass)1 IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)1 Field (dyvilx.tools.compiler.ast.field.Field)1 IField (dyvilx.tools.compiler.ast.field.IField)1 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)1 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)1 Mutability (dyvilx.tools.compiler.ast.type.Mutability)1 Marker (dyvilx.tools.parsing.marker.Marker)1