Search in sources :

Example 1 with InitializerCall

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

the class ConstructorParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case DECLARATOR:
            switch(type) {
                case DyvilSymbols.AT:
                    this.parseAnnotation(pm, token);
                    return;
                case DyvilKeywords.INIT:
                    this.member = this.consumer.createConstructor(token.raw(), this.attributes);
                    this.mode = PARAMETERS;
                    return;
            }
            if (this.parseModifier(pm, token)) {
                return;
            }
        // Fallthrough
        case PARAMETERS:
            this.mode = PARAMETERS_END;
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ParameterListParser(this.member));
                return;
            }
            pm.reparse();
            pm.report(token, "constructor.parameters.open_paren");
            return;
        case PARAMETERS_END:
            this.mode = INITIALIZER;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "constructor.parameters.close_paren");
            }
            return;
        case INITIALIZER:
            if (type == BaseSymbols.COLON) {
                this.mode = INIT_TYPE;
                return;
            }
        // Fallthrough
        case EXCEPTIONS:
            if (type == DyvilKeywords.THROWS) {
                pm.pushParser(new TypeListParser(this.member.getExceptions()));
                this.mode = BODY;
                return;
            }
        // Fallthrough
        case BODY:
            switch(type) {
                case BaseSymbols.OPEN_CURLY_BRACKET:
                    pm.pushParser(new StatementListParser(this.member), true);
                    this.mode = END;
                    return;
                case BaseSymbols.EQUALS:
                    pm.pushParser(new ExpressionParser(this.member));
                    this.mode = END;
                    return;
            }
        // Fallthrough
        case END:
            this.consumer.addConstructor(this.member);
            pm.popParser(type != Tokens.EOF);
            return;
        case INIT_TYPE:
            boolean isSuper = false;
            switch(type) {
                case DyvilKeywords.SUPER:
                    isSuper = true;
                // Fallthrough
                case DyvilKeywords.THIS:
                    final InitializerCall init = new InitializerCall(token.raw(), isSuper);
                    this.member.setInitializer(init);
                    this.mode = INIT_ARGUMENTS;
                    return;
            }
            pm.report(token, "initializer.call.type");
            return;
        case INIT_ARGUMENTS:
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ArgumentListParser(this.member.getInitializer()));
                this.mode = INIT_END;
                return;
            }
            pm.report(token, "initializer.call.open_paren");
            this.mode = EXCEPTIONS;
            return;
        case INIT_END:
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "initializer.call.close_paren");
                return;
            }
            this.mode = EXCEPTIONS;
            return;
    }
}
Also used : StatementListParser(dyvilx.tools.compiler.parser.statement.StatementListParser) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) TypeListParser(dyvilx.tools.compiler.parser.type.TypeListParser) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) ArgumentListParser(dyvilx.tools.compiler.parser.expression.ArgumentListParser) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall)

Example 2 with InitializerCall

use of dyvilx.tools.compiler.ast.expression.access.InitializerCall 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 3 with InitializerCall

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

the class CodeConstructor method resolveInitCall.

private void resolveInitCall(MarkerList markers, IContext context) {
    if (this.initializerCall != null) {
        this.initializerCall.resolve(markers, context);
        return;
    }
    // No Super Type -> don't try to resolve a Super Constructor
    final IType superType = this.enclosingClass.getSuperType();
    if (superType == null) {
        return;
    }
    // Implicit Super Constructor
    final IConstructor match = IContext.resolveConstructor(context, superType, ArgumentList.EMPTY);
    if (match == null) {
        markers.add(Markers.semanticError(this.position, "constructor.super"));
        return;
    }
    this.initializerCall = new InitializerCall(this.position, true, ArgumentList.EMPTY, superType, match);
}
Also used : InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall) IType(dyvilx.tools.compiler.ast.type.IType)

Example 4 with InitializerCall

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

the class AbstractConstructor method toString.

@Override
public void toString(@NonNull String indent, @NonNull StringBuilder buffer) {
    super.toString(indent, buffer);
    buffer.append("init");
    this.parameters.toString(indent, buffer);
    final InitializerCall init = this.getInitializer();
    if (init != null) {
        Formatting.appendSeparator(buffer, "initializer.call.colon", ':');
        init.toString(indent, buffer);
    }
    if (this.exceptions != null && this.exceptions.size() > 0) {
        String throwsPrefix = indent;
        if (Formatting.getBoolean("constructor.throws.newline")) {
            throwsPrefix = Formatting.getIndent("constructor.throws.indent", indent);
            buffer.append('\n').append(throwsPrefix).append("throws ");
        } else {
            buffer.append(" throws ");
        }
        Util.astToString(throwsPrefix, this.exceptions.getTypes(), this.exceptions.size(), Formatting.getSeparator("constructor.throws", ','), buffer);
    }
    final IValue value = this.getValue();
    if (value != null && !Util.formatStatementList(indent, buffer, value)) {
        buffer.append(" = ");
        value.toString(indent, buffer);
    }
    if (Formatting.getBoolean("constructor.semicolon")) {
        buffer.append(';');
    }
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall)

Aggregations

InitializerCall (dyvilx.tools.compiler.ast.expression.access.InitializerCall)4 IValue (dyvilx.tools.compiler.ast.expression.IValue)2 IType (dyvilx.tools.compiler.ast.type.IType)2 SourcePosition (dyvil.source.position.SourcePosition)1 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 CodeConstructor (dyvilx.tools.compiler.ast.constructor.CodeConstructor)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 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 ArgumentListParser (dyvilx.tools.compiler.parser.expression.ArgumentListParser)1 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)1 ParameterListParser (dyvilx.tools.compiler.parser.method.ParameterListParser)1 StatementListParser (dyvilx.tools.compiler.parser.statement.StatementListParser)1 TypeListParser (dyvilx.tools.compiler.parser.type.TypeListParser)1