Search in sources :

Example 11 with FieldAccess

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

the class CaseClassMetadata method createUnapplyAnyMethod.

private CodeMethod createUnapplyAnyMethod() {
    // static final func unapply<TypeParams...>(value: any) -> (T...)?
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = NullableType.apply(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, Types.NULLABLE_ANY);
    unapply.getParameters().add(parameter);
    // = (param is This) ? unapply(param as This) : null
    final InstanceOfOperator isOperator = new InstanceOfOperator(new FieldAccess(parameter), this.theClass.getClassType());
    final CastOperator castOperator = new CastOperator(new FieldAccess(parameter), this.theClass.getThisType());
    final IValue call = new MethodCall(position, null, Names.unapply, new ArgumentList(castOperator));
    final IfStatement ifStatement = new IfStatement(isOperator, call, new NullValue());
    unapply.setValue(ifStatement);
    return unapply;
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) CastOperator(dyvilx.tools.compiler.ast.expression.CastOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall) IType(dyvilx.tools.compiler.ast.type.IType) InstanceOfOperator(dyvilx.tools.compiler.ast.expression.InstanceOfOperator) IfStatement(dyvilx.tools.compiler.ast.statement.IfStatement) IValue(dyvilx.tools.compiler.ast.expression.IValue) NullValue(dyvilx.tools.compiler.ast.expression.constant.NullValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess)

Example 12 with FieldAccess

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

the class BraceAccessExpr method resolve.

@Override
public IValue resolve(MarkerList markers, IContext context) {
    if (this.value != null) {
        this.value = this.value.resolve(markers, context);
    } else {
        this.value = context.resolveImplicit(null);
    }
    if (this.value == null) {
        markers.add(Markers.semanticError(this.position, "braceaccess.invalid"));
    } else {
        final IType valueType = this.value.getType();
        final IValue typedValue = this.value.withType(valueType, valueType, markers, context);
        if (typedValue != null) {
            this.value = typedValue;
        }
        this.variable = new Variable(Names.$0, this.value.getType());
        this.implicitAccess = new FieldAccess(this.variable);
    }
    context = context.push(this);
    this.statement = this.statement.resolve(markers, context);
    context.pop();
    return this;
}
Also used : Variable(dyvilx.tools.compiler.ast.field.Variable) IVariable(dyvilx.tools.compiler.ast.field.IVariable) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) IType(dyvilx.tools.compiler.ast.type.IType)

Example 13 with FieldAccess

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

the class SideEffectHelper method processValue.

public IValue processValue(IValue value) {
    if (value == null || !value.hasSideEffects()) {
        return value;
    }
    if (this.statementList == null) {
        this.statementList = new StatementList();
    }
    final Variable variable = new Variable(value.getPosition(), Name.fromRaw("sideEffect$" + this.registered), value.getType());
    variable.setValue(value);
    this.statementList.add(new VariableStatement(variable));
    this.statementList.addVariable(variable);
    this.registered++;
    return new FieldAccess(value.getPosition(), null, variable);
}
Also used : Variable(dyvilx.tools.compiler.ast.field.Variable) VariableStatement(dyvilx.tools.compiler.ast.statement.VariableStatement) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess)

Example 14 with FieldAccess

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

the class FuncDirective method convertBlock.

protected static StatementList convertBlock(StatementList block) {
    final StatementList value = new StatementList();
    // new StringWriter()
    final ConstructorCall newStringWriter = new ConstructorCall(null, Template.LazyTypes.StringWriter, ArgumentList.EMPTY);
    // let writer = new StringWriter()
    final Variable writer = new Variable(Name.fromRaw("writer"), Template.LazyTypes.Writer, newStringWriter);
    writer.getAttributes().addFlag(Modifiers.FINAL | Modifiers.GENERATED);
    // { let writer = new StringWriter; { ... }; writer.toString }
    value.add(new VariableStatement(writer));
    value.add(block);
    value.add(new MethodCall(null, new FieldAccess(writer), Names.toString));
    return value;
}
Also used : Variable(dyvilx.tools.compiler.ast.field.Variable) VariableStatement(dyvilx.tools.compiler.ast.statement.VariableStatement) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall)

Example 15 with FieldAccess

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

the class OptionalChainAware method transform.

static IValue transform(OptionalChainAware oca) {
    // oca = receiver?.access
    // <- oco ->
    final IValue oco = oca.getReceiver();
    if (oco == null || oco.valueTag() != IValue.OPTIONAL_CHAIN) {
        // no transformation needed as it is actually not an optional chain
        return oca;
    }
    final SourcePosition position = oca.getPosition();
    final IValue receiver = ((OptionalChainOperator) oco).getReceiver();
    // receiver is now the actual receiver of the optional chain operator
    BindingIfStatement bindingIf;
    if (receiver instanceof BindingIfStatement && (bindingIf = (BindingIfStatement) receiver).getElse() == NullValue.NULL) {
        // safe bet that the receiver used to be an optional chain
        // Perform the following transformation (the entire statement is the receiver):
        // if let $0 = oldReceiver { $0.oldAccess } else null
        // becomes
        // if (let $0 = oldReceiver, let $1 = $0.oldAccess) { $1.access } else null
        final Variable var = newVar(position, bindingIf.getThen());
        bindingIf.addVariable(var);
        oca.setReceiver(new FieldAccess(var));
        bindingIf.setThen(oca);
        return bindingIf;
    }
    // oca = receiver?.access, and receiver is not an optional chain
    // receiver?.access
    // becomes
    // if let $0 = receiver { $0.access } else null
    final Variable var = newVar(position, receiver);
    bindingIf = new BindingIfStatement(position);
    bindingIf.addVariable(var);
    oca.setReceiver(new FieldAccess(var));
    bindingIf.setThen(oca);
    bindingIf.setElse(NullValue.NULL);
    return bindingIf;
}
Also used : BindingIfStatement(dyvilx.tools.compiler.ast.statement.BindingIfStatement) IValue(dyvilx.tools.compiler.ast.expression.IValue) Variable(dyvilx.tools.compiler.ast.field.Variable) SourcePosition(dyvil.source.position.SourcePosition) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess)

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