Search in sources :

Example 11 with BConversionOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol in project ballerina by ballerina-lang.

the class Desugar method addConversionExprIfRequired.

private BLangExpression addConversionExprIfRequired(BLangSimpleVarRef matchExprVarRef, BLangMatchStmtPatternClause patternClause) {
    BType lhsType = patternClause.variable.type;
    BType rhsType = matchExprVarRef.type;
    if (types.isSameType(rhsType, lhsType)) {
        return matchExprVarRef;
    }
    types.setImplicitCastExpr(matchExprVarRef, rhsType, lhsType);
    if (matchExprVarRef.impConversionExpr != null) {
        return matchExprVarRef;
    }
    BConversionOperatorSymbol conversionSymbol;
    if (types.isValueType(lhsType) && !(rhsType.tag == TypeTags.JSON)) {
        conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, lhsType);
    } else if (lhsType.tag == TypeTags.UNION || rhsType.tag == TypeTags.UNION) {
        conversionSymbol = Symbols.createConversionOperatorSymbol(rhsType, lhsType, symTable.errStructType, false, true, InstructionCodes.NOP, null, null);
    } else {
        conversionSymbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(rhsType, lhsType);
    }
    // Create a type cast expression
    BLangTypeConversionExpr conversionExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
    conversionExpr.expr = matchExprVarRef;
    conversionExpr.targetType = lhsType;
    conversionExpr.conversionSymbol = conversionSymbol;
    conversionExpr.type = lhsType;
    return conversionExpr;
}
Also used : BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Aggregations

BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)7 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)6 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)4 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)4 BLangTypeConversionExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)4 ArrayList (java.util.ArrayList)2 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)2 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BLangTransformerInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.BLangTransformerInvocation)1