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;
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations