Search in sources :

Example 6 with ParameterList

use of dyvilx.tools.compiler.ast.parameter.ParameterList in project Dyvil by Dyvil.

the class ClassMetadata method createDefaultConstructor.

private CodeConstructor createDefaultConstructor() {
    // init(classParams...)
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = this.theClass.getConstructorAttributes();
    attributes.addFlag(Modifiers.GENERATED);
    final CodeConstructor constructor = new CodeConstructor(this.theClass, attributes);
    constructor.setPosition(position);
    this.copyClassParameters(constructor);
    // : super(superParams...)
    final IType superType = this.theClass.getSuperType();
    if (superType != null) {
        // Generate the constructor body
        ArgumentList arguments = this.theClass.getSuperConstructorArguments();
        if (arguments == null) {
            arguments = ArgumentList.empty();
        }
        final InitializerCall init = new InitializerCall(this.theClass.getPosition(), true, arguments, superType);
        constructor.setInitializer(init);
    }
    // { this.classParams... = classParams... }
    final ParameterList classParams = this.theClass.getParameters();
    final StatementList ctorBody = new StatementList();
    final ParameterList ctorParams = constructor.getParameters();
    // j is the counter for class parameters, as there may be leading synthetic constructor parameters
    for (int i = 0, j = 0, count = ctorParams.size(); i < count; i++) {
        final IParameter ctorParam = ctorParams.get(i);
        if (ctorParam.hasModifier(Modifiers.SYNTHETIC)) {
            continue;
        }
        final IParameter classParam = classParams.get(j++);
        if (classParam.hasModifier(Modifiers.OVERRIDE)) {
            continue;
        }
        final IValue receiver = new ThisExpr(this.theClass);
        final FieldAccess access = new FieldAccess(ctorParam);
        final FieldAssignment assignment = new FieldAssignment(position, receiver, classParam, access);
        ctorBody.add(assignment);
    }
    constructor.setValue(ctorBody);
    return constructor;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) FieldAssignment(dyvilx.tools.compiler.ast.expression.access.FieldAssignment) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall) IType(dyvilx.tools.compiler.ast.type.IType) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeConstructor(dyvilx.tools.compiler.ast.constructor.CodeConstructor) SourcePosition(dyvil.source.position.SourcePosition) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ThisExpr(dyvilx.tools.compiler.ast.expression.ThisExpr)

Example 7 with ParameterList

use of dyvilx.tools.compiler.ast.parameter.ParameterList in project Dyvil by Dyvil.

the class ClassMetadata method copyClassParameters.

protected void copyClassParameters(ICallableMember constructor) {
    ParameterList from = this.theClass.getParameters();
    final int parameterCount = from.size();
    final ParameterList ctrParams = constructor.getParameters();
    for (int i = 0; i < parameterCount; i++) {
        final IParameter classParameter = from.get(i);
        AttributeList attributes = classParameter.getAttributes().filtered(Modifiers.PARAMETER_MODIFIERS);
        ctrParams.add(constructor.createParameter(classParameter.getPosition(), classParameter.getName(), classParameter.getType(), attributes));
    }
    if (ctrParams.isLastVariadic()) {
        constructor.getAttributes().addFlag(Modifiers.ACC_VARARGS);
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList)

Example 8 with ParameterList

use of dyvilx.tools.compiler.ast.parameter.ParameterList in project Dyvil by Dyvil.

the class AnonymousClassMetadata method write.

@Override
public void write(ClassWriter writer) throws BytecodeException {
    final CaptureHelper<CaptureField> captureHelper = this.theClass.captureHelper;
    final FieldThis thisField = this.theClass.thisField;
    final IConstructor constructor = this.theClass.constructor;
    captureHelper.writeCaptureFields(writer);
    final MethodWriter initWriter = new MethodWriterImpl(writer, writer.visitMethod(Modifiers.MANDATED, "<init>", this.theClass.getConstructorDesc(), null, null));
    final ParameterList parameterList = constructor.getParameters();
    final int parameterCount = parameterList.size();
    // Signature & Parameter Data
    initWriter.setThisType(this.theClass.getInternalName());
    parameterList.write(initWriter);
    int index = initWriter.localCount();
    int thisIndex = index;
    if (thisField != null) {
        thisField.writeField(writer);
        index = initWriter.visitParameter(index, thisField.getName(), thisField.getTargetClass().getThisType(), Modifiers.MANDATED);
    }
    captureHelper.writeCaptureParameters(initWriter, index);
    // Constructor Body
    initWriter.visitCode();
    initWriter.visitVarInsn(Opcodes.ALOAD, 0);
    for (int i = 0; i < parameterCount; i++) {
        parameterList.get(i).writeGet(initWriter);
    }
    constructor.writeInvoke(initWriter, 0);
    if (thisField != null) {
        initWriter.visitVarInsn(Opcodes.ALOAD, 0);
        initWriter.visitVarInsn(Opcodes.ALOAD, thisIndex);
        initWriter.visitFieldInsn(Opcodes.PUTFIELD, this.theClass.getInternalName(), thisField.getName(), thisField.getDescriptor());
    }
    captureHelper.writeFieldAssignments(initWriter);
    this.theClass.writeClassInit(initWriter);
    initWriter.visitEnd(Types.VOID);
}
Also used : MethodWriterImpl(dyvilx.tools.compiler.backend.MethodWriterImpl) IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) MethodWriter(dyvilx.tools.compiler.backend.MethodWriter) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) CaptureField(dyvilx.tools.compiler.ast.field.capture.CaptureField)

Example 9 with ParameterList

use of dyvilx.tools.compiler.ast.parameter.ParameterList 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 10 with ParameterList

use of dyvilx.tools.compiler.ast.parameter.ParameterList in project Dyvil by Dyvil.

the class AnnotationExpr method writeExpression.

@Override
public void writeExpression(MethodWriter writer, IType type) throws BytecodeException {
    final StringBuilder descBuilder = new StringBuilder().append('(');
    final ArgumentList arguments = this.annotation.getArguments();
    final IClass iclass = this.annotation.getType().getTheClass();
    final ParameterList parameterList = iclass.getParameters();
    final int count = parameterList.size();
    String[] parameterNames = new String[count];
    for (int i = 0; i < count; i++) {
        final IParameter parameter = parameterList.get(i);
        final IType parameterType = parameter.getType();
        parameterNames[i] = parameter.getInternalName();
        parameterType.appendExtendedName(descBuilder);
        arguments.writeValue(i, parameter, writer);
    }
    descBuilder.append(')');
    descBuilder.append('L').append(iclass.getInternalName()).append(';');
    writer.visitInvokeDynamicInsn("_", descBuilder.toString(), ANNOTATION_METAFACTORY, (Object[]) parameterNames);
    if (type != null) {
        this.annotation.getType().writeCast(writer, type, this.lineNumber());
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) IClass(dyvilx.tools.compiler.ast.classes.IClass) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)27 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)12 IType (dyvilx.tools.compiler.ast.type.IType)11 TypeParameterList (dyvilx.tools.compiler.ast.generic.TypeParameterList)7 IClass (dyvilx.tools.compiler.ast.classes.IClass)5 IValue (dyvilx.tools.compiler.ast.expression.IValue)5 IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)3 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)3 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)3 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)3 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)2 ThisExpr (dyvilx.tools.compiler.ast.expression.ThisExpr)2 IDataMember (dyvilx.tools.compiler.ast.field.IDataMember)2 Name (dyvil.lang.Name)1 SourcePosition (dyvil.source.position.SourcePosition)1 Label (dyvilx.tools.asm.Label)1 IInstruction (dyvilx.tools.compiler.ast.bytecode.IInstruction)1 VarInstruction (dyvilx.tools.compiler.ast.bytecode.VarInstruction)1 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)1 CodeConstructor (dyvilx.tools.compiler.ast.constructor.CodeConstructor)1