Search in sources :

Example 6 with BUnionType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.

the class Desugar method createPatternIfCondition.

private BLangExpression createPatternIfCondition(BLangMatchStmtPatternClause patternClause, BVarSymbol varSymbol) {
    BLangExpression binaryExpr;
    BType patternType = patternClause.variable.type;
    BType[] memberTypes;
    if (patternType.tag == TypeTags.UNION) {
        BUnionType unionType = (BUnionType) patternType;
        memberTypes = unionType.memberTypes.toArray(new BType[0]);
    } else {
        memberTypes = new BType[1];
        memberTypes[0] = patternType;
    }
    if (memberTypes.length == 1) {
        binaryExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
    } else {
        BLangExpression lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
        BLangExpression rhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[1]);
        binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
        for (int i = 2; i < memberTypes.length; i++) {
            lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[i]);
            rhsExpr = binaryExpr;
            binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
        }
    }
    return binaryExpr;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 7 with BUnionType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.

the class SymbolResolver method resolveTypeNode.

public BType resolveTypeNode(BLangType typeNode, SymbolEnv env, DiagnosticCode diagCode) {
    SymbolEnv prevEnv = this.env;
    DiagnosticCode preDiagCode = this.diagCode;
    this.env = env;
    this.diagCode = diagCode;
    typeNode.accept(this);
    this.env = prevEnv;
    this.diagCode = preDiagCode;
    // if it is not already a union type, JSON type, or any type
    if (typeNode.nullable && this.resultType.tag == TypeTags.UNION) {
        BUnionType unionType = (BUnionType) this.resultType;
        unionType.memberTypes.add(symTable.nullType);
        unionType.setNullable(true);
    } else if (typeNode.nullable && resultType.tag != TypeTags.JSON && resultType.tag != TypeTags.ANY) {
        Set<BType> memberTypes = new HashSet<BType>(2) {

            {
                add(resultType);
                add(symTable.nullType);
            }
        };
        this.resultType = new BUnionType(null, memberTypes, true);
    }
    typeNode.type = resultType;
    return resultType;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode)

Example 8 with BUnionType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.

the class SemanticAnalyzer method handleSafeAssignment.

private void handleSafeAssignment(DiagnosticPos lhsPos, BType lhsType, BLangExpression rhsExpr, SymbolEnv env) {
    // Collect all the lhs types
    Set<BType> lhsTypes = lhsType.tag == TypeTags.UNION ? ((BUnionType) lhsType).memberTypes : new HashSet<BType>() {

        {
            add(lhsType);
        }
    };
    // If there is at least one lhs type which assignable to the error type, then report an error.
    for (BType type : lhsTypes) {
        if (types.isAssignable(symTable.errStructType, type)) {
            dlog.error(lhsPos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
            typeChecker.checkExpr(rhsExpr, env, Lists.of(symTable.errType));
            return;
        }
    }
    // Create a new union type with the error type and continue the type checking process.
    lhsTypes.add(symTable.errStructType);
    BUnionType lhsUnionType = new BUnionType(null, lhsTypes, lhsTypes.contains(symTable.nullType));
    typeChecker.checkExpr(rhsExpr, env, Lists.of(lhsUnionType));
    if (rhsExpr.type.tag == TypeTags.UNION) {
        BUnionType rhsUnionType = (BUnionType) rhsExpr.type;
        for (BType type : rhsUnionType.memberTypes) {
            if (types.isAssignable(symTable.errStructType, type)) {
                return;
            }
        }
    } else if (rhsExpr.type.tag != TypeTags.ERROR) {
        dlog.error(rhsExpr.pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
    }
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 9 with BUnionType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

public void visit(BLangMatch matchNode) {
    List<BType> exprTypes = typeChecker.checkExpr(matchNode.expr, env, Lists.of(symTable.noType));
    if (exprTypes.size() > 1) {
        dlog.error(matchNode.expr.pos, DiagnosticCode.MULTI_VAL_EXPR_IN_SINGLE_VAL_CONTEXT);
        return;
    } else if (exprTypes.size() == 0) {
        dlog.error(matchNode.expr.pos, DiagnosticCode.INVALID_EXPR_IN_MATCH_STMT);
        return;
    } else if (exprTypes.get(0).tag == TypeTags.UNION) {
        BUnionType unionType = (BUnionType) exprTypes.get(0);
        exprTypes = new ArrayList<>(unionType.memberTypes);
    }
    // visit patterns
    matchNode.patternClauses.forEach(patternClause -> patternClause.accept(this));
    matchNode.exprTypes = exprTypes;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 10 with BUnionType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType 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)

Aggregations

BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)13 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)13 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 List (java.util.List)5 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)5 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)5 Collectors (java.util.stream.Collectors)4 BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)4 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)4 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)4 TypeTags (org.wso2.ballerinalang.compiler.util.TypeTags)4 DiagnosticCode (org.ballerinalang.util.diagnostic.DiagnosticCode)3 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2