Search in sources :

Example 1 with BConversionOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol 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 BConversionOperatorSymbol

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

the class Symbols method createConversionOperatorSymbol.

public static BConversionOperatorSymbol createConversionOperatorSymbol(final BType sourceType, final BType targetType, final BType errorType, boolean implicit, boolean safe, int opcode, PackageID pkgID, BSymbol owner) {
    List<BType> paramTypes = Lists.of(sourceType, targetType);
    List<BType> retTypes = new ArrayList<>(1);
    if (safe) {
        retTypes.add(targetType);
    } else if (targetType.tag == TypeTags.UNION && targetType instanceof BUnionType) {
        BUnionType unionType = (BUnionType) targetType;
        unionType.memberTypes.add(errorType);
        retTypes.add(unionType);
    } else {
        Set<BType> memberTypes = new HashSet<>(2);
        memberTypes.add(targetType);
        memberTypes.add(errorType);
        BUnionType unionType = new BUnionType(null, memberTypes, false);
        retTypes.add(unionType);
    }
    BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
    BConversionOperatorSymbol symbol = new BConversionOperatorSymbol(pkgID, opType, owner, implicit, safe, opcode);
    symbol.kind = SymbolKind.CONVERSION_OPERATOR;
    return symbol;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) Set(java.util.Set) HashSet(java.util.HashSet) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)

Example 3 with BConversionOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol 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 BConversionOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol 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 BConversionOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol 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

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