Search in sources :

Example 6 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(BLangTupleDestructure stmt) {
    // var (a, b) = (tuple)
    // 
    // desugar once
    // any[] x = (tuple);
    // a = x[0];
    final BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(stmt.pos);
    BType runTimeType = new BArrayType(symTable.anyType);
    final BLangVariable tuple = ASTBuilderUtil.createVariable(stmt.pos, "", runTimeType, null, new BVarSymbol(0, names.fromString("tuple"), this.env.scope.owner.pkgID, runTimeType, this.env.scope.owner));
    tuple.expr = stmt.expr;
    final BLangVariableDef variableDef = ASTBuilderUtil.createVariableDefStmt(stmt.pos, blockStmt);
    variableDef.var = tuple;
    for (int index = 0; index < stmt.varRefs.size(); index++) {
        BLangExpression varRef = stmt.varRefs.get(index);
        BLangLiteral indexExpr = ASTBuilderUtil.createLiteral(stmt.pos, symTable.intType, (long) index);
        BLangIndexBasedAccess arrayAccess = ASTBuilderUtil.createIndexBasesAccessExpr(stmt.pos, symTable.anyType, tuple.symbol, indexExpr);
        final BLangExpression assignmentExpr;
        if (types.isValueType(varRef.type)) {
            BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
            castExpr.expr = arrayAccess;
            castExpr.conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, varRef.type);
            castExpr.type = varRef.type;
            assignmentExpr = castExpr;
        } else {
            assignmentExpr = arrayAccess;
        }
        final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(stmt.pos, blockStmt);
        assignmentStmt.declaredWithVar = stmt.declaredWithVar;
        assignmentStmt.varRefs = Lists.of(varRef);
        assignmentStmt.expr = assignmentExpr;
    }
    result = rewrite(blockStmt, env);
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 7 with BLangTypeConversionExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr 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 BLangTypeConversionExpr

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

the class TypeChecker method checkUnNamedTransformerInvocation.

private List<BType> checkUnNamedTransformerInvocation(BLangTypeConversionExpr conversionExpr, BType sourceType, BType targetType) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    // Check whether a transformer is available for the two types
    BSymbol symbol = symResolver.resolveTransformer(env, sourceType, targetType);
    if (symbol == symTable.notFoundSymbol) {
        // check whether a casting is possible, to provide user a hint.
        BSymbol castSymbol = symResolver.resolveConversionOperator(sourceType, targetType);
        if (castSymbol == symTable.notFoundSymbol) {
            dlog.error(conversionExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION, sourceType, targetType);
        } else {
            dlog.error(conversionExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION_WITH_SUGGESTION, sourceType, targetType);
        }
    } else {
        BTransformerSymbol transformerSymbol = (BTransformerSymbol) symbol;
        conversionExpr.conversionSymbol = transformerSymbol;
        if (conversionExpr.conversionSymbol.safe) {
            ((BInvokableType) transformerSymbol.type).retTypes.add(symTable.errStructType);
        }
        actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, transformerSymbol);
    }
    return actualTypes;
}
Also used : BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 9 with BLangTypeConversionExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr 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 10 with BLangTypeConversionExpr

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

the class ASTBuilderUtil method wrapToConversionExpr.

static BLangExpression wrapToConversionExpr(BType sourceType, BLangExpression exprToWrap, SymbolTable symTable, Types types) {
    if (types.isSameType(sourceType, exprToWrap.type) || !isValueType(exprToWrap.type)) {
        // No conversion needed.
        return exprToWrap;
    }
    BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
    castExpr.expr = exprToWrap;
    castExpr.conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, exprToWrap.type);
    castExpr.type = exprToWrap.type;
    return castExpr;
}
Also used : BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)

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