Search in sources :

Example 6 with FieldAccess

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

the class ProcessedText method resolve.

@Override
public IValue resolve(MarkerList markers, IContext context) {
    final int startLine = this.position.startLine();
    final int startColumn = this.position.startColumn();
    final StringInterpolationExpr parts = new StringInterpolationExpr();
    final String text = this.text;
    final int length = text.length();
    int prev = 0;
    for (int startIndex = 0; startIndex < length; ) {
        final int c = text.codePointAt(startIndex);
        // advance to an identifier character
        if (!Character.isJavaIdentifierStart(c)) {
            startIndex += Character.charCount(c);
            continue;
        }
        final int endIndex = identifierEnd(text, startIndex + 1, length);
        final String key = text.substring(startIndex, endIndex);
        final IDataMember field = context.resolveField(Name.fromRaw(key));
        if (field != null && (field.isLocal() || field.hasModifier(Modifiers.PUBLIC))) {
            // append contents before this identifier
            parts.append(new StringValue(text.substring(prev, startIndex)));
            final SourcePosition position = SourcePosition.apply(startLine, startColumn + startIndex, startColumn + endIndex);
            parts.append(new FieldAccess(position, null, field));
            // advance to the end of the identifier
            prev = endIndex;
            startIndex = endIndex;
            continue;
        }
        startIndex += Character.charCount(c);
    }
    if (prev != length) {
        parts.append(new StringValue(text.substring(prev, length)));
    }
    return new WriteCall(parts.resolve(markers, context));
}
Also used : SourcePosition(dyvil.source.position.SourcePosition) StringInterpolationExpr(dyvilx.tools.compiler.ast.expression.StringInterpolationExpr) StringValue(dyvilx.tools.compiler.ast.expression.constant.StringValue) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) IDataMember(dyvilx.tools.compiler.ast.field.IDataMember)

Example 7 with FieldAccess

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

the class CaseClassMetadata method createUnapplyMethod.

private CodeMethod createUnapplyMethod() {
    // static final func unapply<TypeParams...>(value: This) -> (T...)
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = this.getUnapplyReturnType();
    final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
    unapply.setPosition(position);
    unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
    final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, this.theClass.getThisType());
    unapply.getParameters().add(parameter);
    // = (value.classParams...)
    final TupleExpr tupleExpr = new TupleExpr(position);
    final ArgumentList arguments = tupleExpr.getValues();
    for (IParameter param : this.theClass.getParameters()) {
        // value
        final FieldAccess thisAccess = new FieldAccess(position, null, parameter);
        // value.classParam
        final IValue fieldAccess;
        if (param.isOverride()) {
            // if the class parameter is marked as 'override', we have to resolve it from a super-class
            // the easiest way to do this is by name
            fieldAccess = new FieldAccess(position, thisAccess, param.getName());
        } else {
            fieldAccess = new FieldAccess(position, thisAccess, param);
        }
        arguments.add(fieldAccess);
    }
    unapply.setValue(tupleExpr);
    return unapply;
}
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) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) TupleExpr(dyvilx.tools.compiler.ast.expression.TupleExpr) IType(dyvilx.tools.compiler.ast.type.IType)

Example 8 with FieldAccess

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

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

the class ClassBody method resolveImplicit.

public IValue resolveImplicit(IType type) {
    if (type == null) {
        return null;
    }
    IValue candidate = null;
    for (int i = 0; i < this.classCount; i++) {
        final IClass iclass = this.classes[i];
        if (!iclass.isImplicit() || !iclass.isObject() || !Types.isSuperType(type, iclass.getClassType())) {
            continue;
        }
        if (candidate != null) {
            // ambiguous
            return null;
        }
        candidate = new FieldAccess(iclass.getMetadata().getInstanceField());
    }
    for (int i = 0; i < this.fieldCount; i++) {
        final IField field = this.fields[i];
        if (!field.isImplicit() || !Types.isSuperType(type, field.getType())) {
            continue;
        }
        if (candidate != null) {
            // ambiguous
            return null;
        }
        // this<Class> is added automatically later
        candidate = new FieldAccess(field);
    }
    return candidate;
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) IField(dyvilx.tools.compiler.ast.field.IField)

Example 10 with FieldAccess

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

FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)16 IValue (dyvilx.tools.compiler.ast.expression.IValue)11 SourcePosition (dyvil.source.position.SourcePosition)8 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)6 IType (dyvilx.tools.compiler.ast.type.IType)6 Variable (dyvilx.tools.compiler.ast.field.Variable)5 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)5 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)4 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)4 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)3 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)3 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)3 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)3 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)3 ThisExpr (dyvilx.tools.compiler.ast.expression.ThisExpr)2 FieldAssignment (dyvilx.tools.compiler.ast.expression.access.FieldAssignment)2 IMethod (dyvilx.tools.compiler.ast.method.IMethod)2 BindingIfStatement (dyvilx.tools.compiler.ast.statement.BindingIfStatement)2 VariableStatement (dyvilx.tools.compiler.ast.statement.VariableStatement)2 Reified (dyvil.annotation.Reified)1