Search in sources :

Example 6 with SourcePosition

use of dyvil.source.position.SourcePosition 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 SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class EnumClassMetadata method createFromNameMethod.

private CodeMethod createFromNameMethod() {
    // @BytecodeName("valueOf")
    // public static func from(name: String) -> EnumType
    final SourcePosition position = this.theClass.position();
    final IType type = this.theClass.getClassType();
    final CodeMethod method = new CodeMethod(this.theClass, Name.fromRaw("from"), type, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
    method.setPosition(position);
    method.setInternalName("valueOf");
    final CodeParameter parameter = new CodeParameter(Name.fromRaw("name"), Types.STRING);
    method.getParameters().add(parameter);
    // = Enum.valueOf(class<EnumType>, name)
    final MethodCall valueOfCall = new MethodCall(position, new ClassAccess(Types.ENUM), Name.fromRaw("valueOf"), new ArgumentList(new ClassOperator(this.theClass.getClassType()), new FieldAccess(parameter)));
    method.setValue(valueOfCall);
    return method;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) ClassOperator(dyvilx.tools.compiler.ast.expression.ClassOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Example 8 with SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class InterfaceMetadata method processProperty.

protected void processProperty(IProperty property, MarkerList markers) {
    this.processMember(property, markers);
    final IMethod getter = property.getGetter();
    if (getter != null) {
        this.processMethod(getter, markers);
    }
    final IMethod setter = property.getSetter();
    if (setter != null) {
        this.processMethod(setter, markers);
    }
    final SourcePosition initializerPosition = property.getInitializerPosition();
    if (initializerPosition != null) {
        this.processPropertyInitializer(initializerPosition, markers);
    }
}
Also used : SourcePosition(dyvil.source.position.SourcePosition) IMethod(dyvilx.tools.compiler.ast.method.IMethod)

Example 9 with SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class StatementList method toString.

@Override
public void toString(String prefix, StringBuilder buffer) {
    if (this.valueCount == 0) {
        if (Formatting.getBoolean("statement.empty.newline")) {
            buffer.append('{').append('\n').append(prefix).append('}');
        } else if (Formatting.getBoolean("statement.empty.space_between")) {
            buffer.append("{ }");
        } else {
            buffer.append("{}");
        }
        return;
    }
    buffer.append('{').append('\n');
    String indentedPrefix = Formatting.getIndent("statement.indent", prefix);
    int prevLine = 0;
    Label label;
    for (int i = 0; i < this.valueCount; i++) {
        IValue value = this.values[i];
        SourcePosition pos = value.getPosition();
        buffer.append(indentedPrefix);
        if (pos != null) {
            if (pos.startLine() - prevLine > 1 && i > 0) {
                buffer.append('\n').append(indentedPrefix);
            }
            prevLine = pos.endLine();
        }
        if (this.labels != null && i < this.labels.length && (label = this.labels[i]) != null) {
            buffer.append("label ");
            buffer.append(label.name);
            if (Formatting.getBoolean("label.separator.space_before")) {
                buffer.append(' ');
            }
            buffer.append(':');
            if (Formatting.getBoolean("label.separator.newline_after")) {
                buffer.append('\n').append(indentedPrefix);
            } else if (Formatting.getBoolean("label.separator.newline_after")) {
                buffer.append(' ');
            }
        }
        value.toString(indentedPrefix, buffer);
        if (Formatting.getBoolean("statement.semicolon")) {
            buffer.append(';');
        }
        buffer.append('\n');
    }
    buffer.append(prefix).append('}');
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) SourcePosition(dyvil.source.position.SourcePosition) Label(dyvilx.tools.compiler.ast.statement.control.Label)

Example 10 with SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class CaseClassMetadata method createApplyMethod.

private CodeMethod createApplyMethod() {
    // static final func apply<TypeParams...>(classParams...: ClassParamTypes...) -> This
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = this.theClass.getThisType();
    final CodeMethod applyMethod = new CodeMethod(this.theClass, Names.apply, type, attributes);
    applyMethod.setPosition(position);
    applyMethod.getTypeParameters().addAll(this.theClass.getTypeParameters());
    this.copyClassParameters(applyMethod);
    // = new This<TypeParams...>(classParams...)
    final ArgumentList arguments = new ArgumentList();
    for (IParameter param : applyMethod.getParameters()) {
        // no need to check for override class parameters here, since we are dealing with parameters of the
        // apply method
        final IValue access;
        if (param.isVarargs()) {
            access = new VarargsOperator(position, new FieldAccess(param));
        } else {
            access = new FieldAccess(param);
        }
        arguments.add(access);
    }
    // = new This(params...)
    applyMethod.setValue(new ConstructorCall(this.theClass.position(), this.theClass.getThisType(), arguments));
    return applyMethod;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) VarargsOperator(dyvilx.tools.compiler.ast.expression.intrinsic.VarargsOperator) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

SourcePosition (dyvil.source.position.SourcePosition)20 IValue (dyvilx.tools.compiler.ast.expression.IValue)10 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)8 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)8 IType (dyvilx.tools.compiler.ast.type.IType)8 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)7 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)6 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)6 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)5 IContext (dyvilx.tools.compiler.ast.context.IContext)2 ThisExpr (dyvilx.tools.compiler.ast.expression.ThisExpr)2 FieldAssignment (dyvilx.tools.compiler.ast.expression.access.FieldAssignment)2 Variable (dyvilx.tools.compiler.ast.field.Variable)2 IMethod (dyvilx.tools.compiler.ast.method.IMethod)2 BindingIfStatement (dyvilx.tools.compiler.ast.statement.BindingIfStatement)2 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)2 TypeChecker (dyvilx.tools.compiler.transform.TypeChecker)2 NonNull (dyvil.annotation.internal.NonNull)1 BitSet (dyvil.collection.mutable.BitSet)1 Modifiers (dyvil.reflect.Modifiers)1