Search in sources :

Example 31 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class ArgumentList method checkVarargsValue.

protected static boolean checkVarargsValue(IValue[] values, int startIndex, int endIndex, IParameter param, GenericData genericData, MarkerList markers, IContext context) {
    final IValue value = values[startIndex];
    if (value.checkVarargs(true)) {
        values[startIndex] = convertValue(value, param, genericData, markers, context);
        return false;
    }
    final int count = endIndex - startIndex;
    final ArrayExpr arrayExpr = newArrayExpr(values, startIndex, count);
    final IValue converted = convertValue(arrayExpr, param, genericData, markers, context);
    values[startIndex] = converted;
    return true;
}
Also used : ArrayExpr(dyvilx.tools.compiler.ast.expression.ArrayExpr) IValue(dyvilx.tools.compiler.ast.expression.IValue)

Example 32 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class CodeParameter method resolve.

@Override
public void resolve(MarkerList markers, IContext context) {
    super.resolve(markers, context);
    if (this.type == Types.UNKNOWN) {
        markers.add(Markers.semanticError(this.position, "parameter.type.infer", this.name));
    }
    if (this.value == null) {
        return;
    }
    this.attributes.addFlag(Modifiers.DEFAULT);
    IValue value = this.value.resolve(markers, context);
    final String kindName = this.getKind().getName();
    final TypeChecker.MarkerSupplier markerSupplier = TypeChecker.markerSupplier(kindName + ".type.incompatible", kindName + ".type", "value.type", this.name);
    value = TypeChecker.convertValue(value, this.type, null, markers, context, markerSupplier);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) TypeChecker(dyvilx.tools.compiler.transform.TypeChecker)

Example 33 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class IParameter method writeDefaultValue.

default void writeDefaultValue(ClassWriter writer) {
    final IValue value = this.getValue();
    assert value != null;
    final ICallableMember method = this.getMethod();
    final IType type = this.getType();
    final String name;
    final int access;
    if (method == null) {
        name = "init$paramDefault$" + this.getInternalName();
        access = Modifiers.STATIC;
    } else {
        name = method.getInternalName() + "$paramDefault$" + this.getInternalName();
        access = (method.getAttributes().flags() & Modifiers.MEMBER_MODIFIERS) | Modifiers.STATIC;
    }
    final String desc = "()" + this.getDescriptor();
    final String signature = "()" + this.getSignature();
    final MethodWriter mw = new MethodWriterImpl(writer, writer.visitMethod(access, name, desc, signature, null));
    mw.visitCode();
    value.writeExpression(mw, type);
    mw.visitEnd(type);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) MethodWriterImpl(dyvilx.tools.compiler.backend.MethodWriterImpl) MethodWriter(dyvilx.tools.compiler.backend.MethodWriter) ICallableMember(dyvilx.tools.compiler.ast.method.ICallableMember) IType(dyvilx.tools.compiler.ast.type.IType)

Example 34 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class NamedArgumentList method writeValues.

@Override
public void writeValues(MethodWriter writer, ParameterList parameters, int startIndex) throws BytecodeException {
    if (this.hasParameterOrder()) {
        super.writeValues(writer, parameters, startIndex);
        return;
    }
    final int locals = writer.localCount();
    final int paramCount = parameters.size() - startIndex;
    // Step 1: Associate parameters to arguments
    final IParameter[] params = new IParameter[this.size];
    for (int i = 0; i < paramCount; i++) {
        final IParameter param = parameters.get(i + startIndex);
        final int argIndex = this.findIndex(i, param.getLabel());
        if (argIndex >= 0) {
            params[argIndex] = param;
        }
    }
    // Save the local indices in the targets array for later use
    // Maps parameter index -> local index of stored argument
    final int[] targets = new int[paramCount];
    // Fill the array with -1s to mark missing values
    Arrays.fill(targets, -1);
    // Step 2: Write all arguments that already have parameter order
    int argStartIndex = 0;
    for (int i = 0; i < this.size; i++) {
        IParameter param = params[i];
        if (param.getIndex() == startIndex + i) {
            this.values[i].writeExpression(writer, param.getCovariantType());
            targets[i] = -2;
            argStartIndex = i + 1;
        } else {
            break;
        }
    }
    if (argStartIndex < this.size) {
        for (int i = argStartIndex; i < this.size; i++) {
            final IParameter parameter = params[i];
            final IType parameterType = parameter.getCovariantType();
            final IValue value = this.values[i];
            final int localIndex = value.writeStore(writer, parameterType);
            targets[parameter.getIndex() - startIndex] = localIndex;
        }
        // Step 4: Load the local variables in order
        for (int i = 0; i < paramCount; i++) {
            final IParameter parameter = parameters.get(i + startIndex);
            final int target = targets[parameter.getIndex() - startIndex];
            switch(target) {
                case -1:
                case -2:
                    // Value for parameter was already written in Step 2
                    continue;
                default:
                    // Value for parameter exists -> load the variable
                    writer.visitVarInsn(parameter.getCovariantType().getLoadOpcode(), target);
            }
        }
    }
    writer.resetLocals(locals);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) IType(dyvilx.tools.compiler.ast.type.IType)

Example 35 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class HeaderContext method resolveImplicit.

@Override
public IValue resolveImplicit(IType type) {
    final IValue headerValue = this.header.resolveImplicit(type);
    if (headerValue != null) {
        return headerValue;
    }
    IValue candidate = null;
    for (int i = 0; i < this.header.importCount; i++) {
        final IValue value = this.header.importDeclarations[i].getContext().resolveImplicit(type);
        if (value == null) {
            continue;
        }
        if (candidate != null) {
            // ambiguous
            return null;
        }
        candidate = value;
    }
    return candidate;
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue)

Aggregations

IValue (dyvilx.tools.compiler.ast.expression.IValue)79 IType (dyvilx.tools.compiler.ast.type.IType)20 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)17 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)11 SourcePosition (dyvil.source.position.SourcePosition)10 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)9 ArrayExpr (dyvilx.tools.compiler.ast.expression.ArrayExpr)7 Marker (dyvilx.tools.parsing.marker.Marker)7 IMethod (dyvilx.tools.compiler.ast.method.IMethod)6 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)5 IContext (dyvilx.tools.compiler.ast.context.IContext)5 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)5 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)5 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)4 IClass (dyvilx.tools.compiler.ast.classes.IClass)4 Variable (dyvilx.tools.compiler.ast.field.Variable)4 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)4 CombiningContext (dyvilx.tools.compiler.ast.context.CombiningContext)3 IVariable (dyvilx.tools.compiler.ast.field.IVariable)3 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)3