Search in sources :

Example 6 with IConstructor

use of dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.

the class ClassBody method getConstructor.

public IConstructor getConstructor(ParameterList parameters) {
    for (int i = 0; i < this.constructorCount; i++) {
        final IConstructor constructor = this.constructors[i];
        final ParameterList constructorParameterList = constructor.getParameters();
        if (parameters.matches(constructorParameterList)) {
            return constructor;
        }
    }
    return null;
}
Also used : IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList)

Example 7 with IConstructor

use of dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.

the class EnumClassMetadata method updateConstructor.

private void updateConstructor(IConstructor constructor) {
    constructor.getAttributes().addFlag(Modifiers.PRIVATE_PROTECTED);
    // Prepend parameters and set super initializer
    final CodeParameter nameParam = new CodeParameter(constructor, null, Names.name, new ImplicitNullableType(Types.STRING), AttributeList.of(Modifiers.SYNTHETIC));
    final CodeParameter ordParam = new CodeParameter(constructor, null, Names.ordinal, Types.INT, AttributeList.of(Modifiers.SYNTHETIC));
    constructor.getParameters().insert(0, nameParam);
    constructor.getParameters().insert(1, ordParam);
    final IConstructor enumConstructor = Types.ENUM_CLASS.getBody().getConstructor(0);
    final InitializerCall initializer = constructor.getInitializer();
    if (initializer == null) {
        final ArgumentList arguments = new ArgumentList(new FieldAccess(nameParam), new FieldAccess(ordParam));
        final SourcePosition position = constructor.position();
        final InitializerCall init = new InitializerCall(position, true, arguments, this.theClass.getSuperType(), enumConstructor);
        constructor.setInitializer(init);
        return;
    }
    // Prepend access to new parameters
    final ArgumentList arguments = initializer.getArguments();
    arguments.insert(0, new FieldAccess(nameParam));
    arguments.insert(1, new FieldAccess(ordParam));
}
Also used : IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) SourcePosition(dyvil.source.position.SourcePosition) ImplicitNullableType(dyvilx.tools.compiler.ast.type.compound.ImplicitNullableType) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter)

Example 8 with IConstructor

use of dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.

the class ConstructorCall method resolveCall.

@Override
public IValue resolveCall(MarkerList markers, IContext context, boolean report) {
    if (!this.type.isResolved()) {
        return this;
    }
    final ArrayType arrayType = this.type.extract(ArrayType.class);
    if (arrayType != null) {
        this.resolveArrayConstructor(markers, context, arrayType);
        return this;
    }
    final IClass theClass = this.type.getTheClass();
    if (theClass != null && theClass.isInterface()) {
        if (!report) {
            return null;
        }
        markers.add(Markers.semanticError(this.position, "constructor.access.interface", this.type));
        return this;
    }
    final MatchList<IConstructor> candidates = this.resolveCandidates(context, this.type);
    if (candidates.hasCandidate()) {
        this.checkArguments(markers, context, candidates.getBestMember());
        return this;
    }
    if (report) {
        reportResolve(markers, candidates, this.position, this.type, this.arguments);
        return this;
    }
    return null;
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) IClass(dyvilx.tools.compiler.ast.classes.IClass)

Example 9 with IConstructor

use of dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.

the class InitializerCall method resolveCall.

@Override
public IValue resolveCall(MarkerList markers, IContext context, boolean report) {
    final IType targetType = this.getTargetType(context);
    if (targetType == null || !targetType.isResolved()) {
        return this;
    }
    final MatchList<IConstructor> matches = IContext.resolveConstructors(context, targetType, this.arguments);
    if (matches.hasCandidate()) {
        this.constructor = matches.getBestMember();
        this.constructor.checkArguments(markers, this.position, context, this.targetType, this.arguments);
        return this;
    }
    if (report) {
        ConstructorCall.reportResolve(markers, matches, this.position, targetType, this.arguments);
        return this;
    }
    return null;
}
Also used : IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) IType(dyvilx.tools.compiler.ast.type.IType)

Example 10 with IConstructor

use of dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.

the class Variable method writeRefInit.

public static void writeRefInit(IVariable variable, MethodWriter writer, IValue value) {
    final IType type = variable.getType();
    final IType refType = variable.getReferenceType();
    final IConstructor constructor = refType.getTheClass().getBody().getConstructor(0);
    writer.visitTypeInsn(Opcodes.NEW, refType.getInternalName());
    if (value != null) {
        // new
        writer.visitInsn(Opcodes.DUP);
        // new, new
        value.writeExpression(writer, type);
    // new, new, value
    } else {
        // value, new
        writer.visitInsn(Opcodes.AUTO_DUP_X1);
        // new, value, new
        writer.visitInsn(Opcodes.AUTO_SWAP);
    // new, new, value
    }
    constructor.writeInvoke(writer, variable.lineNumber());
    final int localIndex = writer.localCount();
    variable.setLocalIndex(localIndex);
    writer.setLocalType(localIndex, refType.getInternalName());
    writer.visitVarInsn(Opcodes.ASTORE, localIndex);
}
Also used : IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)10 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)3 IType (dyvilx.tools.compiler.ast.type.IType)3 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)2 IClass (dyvilx.tools.compiler.ast.classes.IClass)2 SourcePosition (dyvil.source.position.SourcePosition)1 AbstractCall (dyvilx.tools.compiler.ast.expression.access.AbstractCall)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 CaptureField (dyvilx.tools.compiler.ast.field.capture.CaptureField)1 IMethod (dyvilx.tools.compiler.ast.method.IMethod)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)1 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)1 ImplicitNullableType (dyvilx.tools.compiler.ast.type.compound.ImplicitNullableType)1 MethodWriter (dyvilx.tools.compiler.backend.MethodWriter)1 MethodWriterImpl (dyvilx.tools.compiler.backend.MethodWriterImpl)1