Search in sources :

Example 1 with BLangTypeConversionExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangTypeConversionExpr conversionExpr) {
    // Set error type as the actual type.
    // List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    List<BType> actualTypes;
    BType targetType = symResolver.resolveTypeNode(conversionExpr.typeNode, env);
    conversionExpr.targetType = targetType;
    BType sourceType = checkExpr(conversionExpr.expr, env, Lists.of(symTable.noType)).get(0);
    if (conversionExpr.transformerInvocation == null) {
        // Lookup for built-in type conversion operator symbol
        BSymbol symbol = symResolver.resolveConversionOperator(sourceType, targetType);
        if (symbol == symTable.notFoundSymbol) {
            // If not found, look for unnamed transformers for the given types
            actualTypes = checkUnNamedTransformerInvocation(conversionExpr, sourceType, targetType);
        } else {
            BConversionOperatorSymbol conversionSym = (BConversionOperatorSymbol) symbol;
            conversionExpr.conversionSymbol = conversionSym;
            actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, conversionSym);
        }
    } else {
        actualTypes = checkNamedTransformerInvocation(conversionExpr, sourceType, targetType);
    }
    resultTypes = types.checkTypes(conversionExpr, actualTypes, expTypes);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Example 2 with BLangTypeConversionExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangTypeConversionExpr convExpr) {
    int opcode = convExpr.conversionSymbol.opcode;
    // Figure out the reg index of the result value
    BType castExprType = convExpr.type;
    RegIndex convExprRegIndex = calcAndGetExprRegIndex(convExpr.regIndex, castExprType.tag);
    convExpr.regIndex = convExprRegIndex;
    if (opcode == InstructionCodes.NOP) {
        convExpr.expr.regIndex = createLHSRegIndex(convExprRegIndex);
        genNode(convExpr.expr, this.env);
        return;
    }
    genNode(convExpr.expr, this.env);
    if (opcode == InstructionCodes.MAP2T || opcode == InstructionCodes.JSON2T || opcode == InstructionCodes.ANY2T || opcode == InstructionCodes.ANY2C || opcode == InstructionCodes.ANY2E || opcode == InstructionCodes.ANY2M || opcode == InstructionCodes.CHECKCAST) {
        Operand typeCPIndex = getTypeCPIndex(convExpr.targetType);
        emit(opcode, convExpr.expr.regIndex, typeCPIndex, convExprRegIndex);
    } else {
        emit(opcode, convExpr.expr.regIndex, convExprRegIndex);
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 3 with BLangTypeConversionExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.

the class ASTBuilderUtil method generateConversionExpr.

static BLangExpression generateConversionExpr(BLangExpression varRef, BType target, SymbolResolver symResolver) {
    if (varRef.type.tag == target.tag || varRef.type.tag > TypeTags.TYPEDESC) {
        return varRef;
    }
    // Box value using cast expression.
    final BLangTypeConversionExpr conversion = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
    conversion.pos = varRef.pos;
    conversion.expr = varRef;
    conversion.type = target;
    conversion.targetType = target;
    conversion.conversionSymbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(varRef.type, target);
    return conversion;
}
Also used : BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)

Example 4 with BLangTypeConversionExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangTypeConversionExpr conversionExpr) {
    conversionExpr.expr = rewriteExpr(conversionExpr.expr);
    // Built-in conversion
    if (conversionExpr.conversionSymbol.tag != SymTag.TRANSFORMER) {
        result = conversionExpr;
        return;
    }
    // Named transformer invocation
    BLangInvocation transformerInvoc = conversionExpr.transformerInvocation;
    if (transformerInvoc != null) {
        transformerInvoc = rewriteExpr(transformerInvoc);
        // Add the rExpr as the first argument
        conversionExpr.transformerInvocation.requiredArgs.add(0, conversionExpr.expr);
        result = new BLangTransformerInvocation(conversionExpr.pos, transformerInvoc.requiredArgs, transformerInvoc.namedArgs, transformerInvoc.restArgs, transformerInvoc.symbol, conversionExpr.types);
        conversionExpr.transformerInvocation = transformerInvoc;
        return;
    }
    // Unnamed transformer invocation
    BConversionOperatorSymbol transformerSym = conversionExpr.conversionSymbol;
    transformerInvoc = new BLangTransformerInvocation(conversionExpr.pos, Lists.of(conversionExpr.expr), transformerSym, conversionExpr.types);
    transformerInvoc.types = transformerSym.type.getReturnTypes();
    result = transformerInvoc;
}
Also used : BLangTransformerInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.BLangTransformerInvocation) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Example 5 with BLangTypeConversionExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr in project ballerina by ballerina-lang.

the class Desugar method createTypeConversionExpr.

private BLangExpression createTypeConversionExpr(BLangExpression expr, BType sourceType, BType targetType) {
    BConversionOperatorSymbol symbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(sourceType, targetType);
    BLangTypeConversionExpr conversionExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
    conversionExpr.pos = expr.pos;
    conversionExpr.expr = expr;
    conversionExpr.type = targetType;
    conversionExpr.conversionSymbol = symbol;
    return conversionExpr;
}
Also used : BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Aggregations

BLangTypeConversionExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)7 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)6 BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)5 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)4 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)2 BTransformerSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)1 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)1 BLangTransformerInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.BLangTransformerInvocation)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)1 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)1 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)1