Search in sources :

Example 1 with Mutability

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

Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 IType (dyvilx.tools.compiler.ast.type.IType)1 Mutability (dyvilx.tools.compiler.ast.type.Mutability)1 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)1