Search in sources :

Example 1 with CastOperator

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

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

the class BindingIfStatement method writeCondition.

@Override
protected void writeCondition(MethodWriter writer, Label elseStart) {
    for (IVariable var : this.variables) {
        IValue value = getOptionalValue(var);
        final CastOperator castOp;
        final int localCount = writer.localCount();
        final int varIndex;
        if (value instanceof CastOperator && (castOp = (CastOperator) value).isOptional()) {
            // optimization for optional cast operators
            value = castOp.getValue();
            varIndex = value.writeStoreLoad(writer, null);
            // branch if necessary (also branches if null)
            writer.visitTypeInsn(Opcodes.INSTANCEOF, castOp.getType().getInternalName());
            writer.visitJumpInsn(Opcodes.IFEQ, elseStart);
        } else {
            varIndex = value.writeStoreLoad(writer, null);
            // branch if necessary
            writer.visitJumpInsn(Opcodes.IFNULL, elseStart);
        }
        // load and unwrap the variable
        writer.visitVarInsn(Opcodes.ALOAD, varIndex);
        writer.resetLocals(localCount);
        value.getType().writeCast(writer, var.getType(), value.lineNumber());
        // store, but this time with the right type
        var.writeInit(writer, null);
    }
    super.writeCondition(writer, elseStart);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) IVariable(dyvilx.tools.compiler.ast.field.IVariable) CastOperator(dyvilx.tools.compiler.ast.expression.CastOperator)

Aggregations

CastOperator (dyvilx.tools.compiler.ast.expression.CastOperator)2 IValue (dyvilx.tools.compiler.ast.expression.IValue)2 SourcePosition (dyvil.source.position.SourcePosition)1 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 InstanceOfOperator (dyvilx.tools.compiler.ast.expression.InstanceOfOperator)1 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)1 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)1 NullValue (dyvilx.tools.compiler.ast.expression.constant.NullValue)1 IVariable (dyvilx.tools.compiler.ast.field.IVariable)1 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)1 IfStatement (dyvilx.tools.compiler.ast.statement.IfStatement)1 IType (dyvilx.tools.compiler.ast.type.IType)1