Search in sources :

Example 6 with BConversionOperatorSymbol

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

the class SymbolResolver method resolveImplicitConversionOp.

public BSymbol resolveImplicitConversionOp(BType sourceType, BType targetType) {
    BSymbol symbol = resolveOperator(Names.CONVERSION_OP, Lists.of(sourceType, targetType));
    if (symbol == symTable.notFoundSymbol) {
        return symbol;
    }
    BConversionOperatorSymbol castSymbol = (BConversionOperatorSymbol) symbol;
    if (castSymbol.implicit) {
        return symbol;
    }
    return symTable.notFoundSymbol;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Example 7 with BConversionOperatorSymbol

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

the class TypeChecker method checkNamedTransformerInvocation.

private List<BType> checkNamedTransformerInvocation(BLangTypeConversionExpr conversionExpr, BType sourceType, BType targetType) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    BLangInvocation transformerInvocation = conversionExpr.transformerInvocation;
    BSymbol transformerSymbol = symResolver.lookupSymbolInPackage(transformerInvocation.pos, env, names.fromIdNode(transformerInvocation.pkgAlias), names.fromIdNode(transformerInvocation.name), SymTag.TRANSFORMER);
    if (transformerSymbol == symTable.notFoundSymbol) {
        dlog.error(conversionExpr.pos, DiagnosticCode.UNDEFINED_TRANSFORMER, transformerInvocation.name);
    } else {
        conversionExpr.conversionSymbol = (BConversionOperatorSymbol) (transformerInvocation.symbol = transformerSymbol);
        // Check the transformer invocation. Expected type for the transformer is the target type
        // of the cast conversion operator, but not the lhs type.
        List<BType> prevExpType = expTypes;
        expTypes = Lists.of(targetType);
        checkInvocationParamAndReturnType(transformerInvocation);
        expTypes = prevExpType;
        if (transformerInvocation.type != symTable.errType) {
            BInvokableType transformerSymType = (BInvokableType) transformerSymbol.type;
            transformerInvocation.types = transformerSymType.retTypes;
            actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, conversionExpr.conversionSymbol);
        }
    }
    return actualTypes;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)

Example 8 with BConversionOperatorSymbol

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

the class Types method setImplicitCastExpr.

public void setImplicitCastExpr(BLangExpression expr, BType actualType, BType expType) {
    BSymbol symbol = symResolver.resolveImplicitConversionOp(actualType, expType);
    if (expType.tag == TypeTags.UNION && isValueType(actualType)) {
        symbol = symResolver.resolveImplicitConversionOp(actualType, symTable.anyType);
    }
    if (symbol == symTable.notFoundSymbol) {
        return;
    }
    BConversionOperatorSymbol conversionSym = (BConversionOperatorSymbol) symbol;
    BLangTypeConversionExpr implicitConversionExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
    implicitConversionExpr.pos = expr.pos;
    implicitConversionExpr.expr = expr.impConversionExpr == null ? expr : expr.impConversionExpr;
    implicitConversionExpr.type = expType;
    implicitConversionExpr.targetType = expType;
    implicitConversionExpr.conversionSymbol = conversionSym;
    expr.impConversionExpr = implicitConversionExpr;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Example 9 with BConversionOperatorSymbol

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

the class SymbolTable method defineConversionOperator.

private void defineConversionOperator(BType sourceType, BType targetType, boolean implicit, boolean safe, int opcode) {
    List<BType> paramTypes = Lists.of(sourceType, targetType);
    List<BType> retTypes = new ArrayList<>(1);
    if (safe) {
        retTypes.add(targetType);
    } else {
        if (targetType.tag == TypeTags.UNION) {
            BUnionType unionType = (BUnionType) targetType;
            unionType.memberTypes.add(this.errStructType);
            retTypes.add(targetType);
        } else {
            BUnionType unionType = new BUnionType(null, new HashSet<BType>(2) {

                {
                    add(targetType);
                    add(errStructType);
                }
            }, false);
            retTypes.add(unionType);
        }
    }
    BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
    BConversionOperatorSymbol symbol = new BConversionOperatorSymbol(this.rootPkgSymbol.pkgID, opType, this.rootPkgSymbol, implicit, safe, opcode);
    symbol.kind = SymbolKind.CONVERSION_OPERATOR;
    rootScope.define(symbol.name, symbol);
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Example 10 with BConversionOperatorSymbol

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

the class Symbols method createUnboxValueTypeOpSymbol.

public static BConversionOperatorSymbol createUnboxValueTypeOpSymbol(BType sourceType, BType targetType) {
    int opcode;
    switch(targetType.tag) {
        case TypeTags.INT:
            opcode = InstructionCodes.ANY2I;
            break;
        case TypeTags.FLOAT:
            opcode = InstructionCodes.ANY2F;
            break;
        case TypeTags.STRING:
            opcode = InstructionCodes.ANY2S;
            break;
        case TypeTags.BOOLEAN:
            opcode = InstructionCodes.ANY2B;
            break;
        default:
            opcode = InstructionCodes.ANY2L;
            break;
    }
    List<BType> paramTypes = Lists.of(sourceType, targetType);
    List<BType> retTypes = Lists.of(targetType);
    BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
    BConversionOperatorSymbol symbol = new BConversionOperatorSymbol(null, opType, null, false, true, opcode);
    symbol.kind = SymbolKind.CONVERSION_OPERATOR;
    return symbol;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)

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