Search in sources :

Example 1 with CodeConstructor

use of dyvilx.tools.compiler.ast.constructor.CodeConstructor 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 2 with CodeConstructor

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

the class ClassMetadata method resolveTypesGenerate.

@Override
public void resolveTypesGenerate(MarkerList markers, IContext context) {
    if ((this.members & CONSTRUCTOR) == 0) {
        // Generate the constructor signature
        final CodeConstructor constructor = this.createDefaultConstructor();
        // resolves attributes too
        constructor.resolveTypes(markers, context);
        this.constructor = constructor;
        this.theClass.createBody().addConstructor(constructor);
    }
}
Also used : CodeConstructor(dyvilx.tools.compiler.ast.constructor.CodeConstructor)

Aggregations

CodeConstructor (dyvilx.tools.compiler.ast.constructor.CodeConstructor)2 SourcePosition (dyvil.source.position.SourcePosition)1 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 IValue (dyvilx.tools.compiler.ast.expression.IValue)1 ThisExpr (dyvilx.tools.compiler.ast.expression.ThisExpr)1 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)1 FieldAssignment (dyvilx.tools.compiler.ast.expression.access.FieldAssignment)1 InitializerCall (dyvilx.tools.compiler.ast.expression.access.InitializerCall)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)1 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)1 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)1 IType (dyvilx.tools.compiler.ast.type.IType)1