Search in sources :

Example 1 with BOperatorSymbol

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

the class SymbolResolver method getBinaryOpForNullChecks.

private BSymbol getBinaryOpForNullChecks(OperatorKind opKind, BType lhsType, BType rhsType) {
    if (opKind != OperatorKind.EQUAL && opKind != OperatorKind.NOT_EQUAL) {
        return symTable.notFoundSymbol;
    }
    int opcode = (opKind == OperatorKind.EQUAL) ? InstructionCodes.REQ_NULL : InstructionCodes.RNE_NULL;
    if (lhsType.tag == TypeTags.NULL && (rhsType.tag == TypeTags.STRUCT || rhsType.tag == TypeTags.CONNECTOR || rhsType.tag == TypeTags.ENUM || rhsType.tag == TypeTags.INVOKABLE)) {
        List<BType> paramTypes = Lists.of(lhsType, rhsType);
        List<BType> retTypes = Lists.of(symTable.booleanType);
        BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
        return new BOperatorSymbol(names.fromString(opKind.value()), null, opType, null, opcode);
    }
    if ((lhsType.tag == TypeTags.STRUCT || lhsType.tag == TypeTags.CONNECTOR || lhsType.tag == TypeTags.ENUM || lhsType.tag == TypeTags.INVOKABLE) && rhsType.tag == TypeTags.NULL) {
        List<BType> paramTypes = Lists.of(lhsType, rhsType);
        List<BType> retTypes = Lists.of(symTable.booleanType);
        BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
        return new BOperatorSymbol(names.fromString(opKind.value()), null, opType, null, opcode);
    }
    if (lhsType.tag == TypeTags.ENUM && rhsType.tag == TypeTags.ENUM && lhsType == rhsType) {
        opcode = (opKind == OperatorKind.EQUAL) ? InstructionCodes.REQ : InstructionCodes.RNE;
        List<BType> paramTypes = Lists.of(lhsType, rhsType);
        List<BType> retTypes = Lists.of(symTable.booleanType);
        BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
        return new BOperatorSymbol(names.fromString(opKind.value()), null, opType, null, opcode);
    }
    return symTable.notFoundSymbol;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol)

Example 2 with BOperatorSymbol

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

the class SemanticAnalyzer method getBinaryExpr.

private BLangExpression getBinaryExpr(BLangExpression lExpr, BLangExpression rExpr, OperatorKind opKind, BSymbol opSymbol) {
    BLangBinaryExpr binaryExpressionNode = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    binaryExpressionNode.lhsExpr = lExpr;
    binaryExpressionNode.rhsExpr = rExpr;
    binaryExpressionNode.pos = rExpr.pos;
    binaryExpressionNode.opKind = opKind;
    if (opSymbol != symTable.notFoundSymbol) {
        binaryExpressionNode.type = opSymbol.type.getReturnTypes().get(0);
        binaryExpressionNode.opSymbol = (BOperatorSymbol) opSymbol;
    } else {
        binaryExpressionNode.type = symTable.errType;
    }
    return binaryExpressionNode;
}
Also used : BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 3 with BOperatorSymbol

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

the class TypeChecker method visit.

public void visit(BLangBinaryExpr binaryExpr) {
    BType lhsType = checkExpr(binaryExpr.lhsExpr, env).get(0);
    BType rhsType = checkExpr(binaryExpr.rhsExpr, env).get(0);
    // Set error type as the actual type.
    BType actualType = symTable.errType;
    // Look up operator symbol if both rhs and lhs types are error types
    if (lhsType != symTable.errType && rhsType != symTable.errType) {
        BSymbol opSymbol = symResolver.resolveBinaryOperator(binaryExpr.opKind, lhsType, rhsType);
        if (opSymbol == symTable.notFoundSymbol) {
            dlog.error(binaryExpr.pos, DiagnosticCode.BINARY_OP_INCOMPATIBLE_TYPES, binaryExpr.opKind, lhsType, rhsType);
        } else {
            binaryExpr.opSymbol = (BOperatorSymbol) opSymbol;
            actualType = opSymbol.type.getReturnTypes().get(0);
        }
    }
    resultTypes = types.checkTypes(binaryExpr, Lists.of(actualType), expTypes);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 4 with BOperatorSymbol

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

the class ASTBuilderUtil method createUnaryExpr.

static BLangUnaryExpr createUnaryExpr(DiagnosticPos pos, BLangExpression expr, BType type, OperatorKind kind, BOperatorSymbol symbol) {
    final BLangUnaryExpr unaryExpr = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
    unaryExpr.pos = pos;
    unaryExpr.expr = expr;
    unaryExpr.type = type;
    unaryExpr.operator = kind;
    unaryExpr.opSymbol = symbol;
    return unaryExpr;
}
Also used : BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)

Example 5 with BOperatorSymbol

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

the class Desugar method createPatternMatchBinaryExpr.

private BLangExpression createPatternMatchBinaryExpr(DiagnosticPos pos, BVarSymbol varSymbol, BType patternType) {
    if (patternType == symTable.nullType) {
        BLangSimpleVarRef varRef = ASTBuilderUtil.createVariableRef(pos, varSymbol);
        BLangLiteral bLangLiteral = ASTBuilderUtil.createLiteral(pos, symTable.nullType, null);
        return ASTBuilderUtil.createBinaryExpr(pos, varRef, bLangLiteral, symTable.booleanType, OperatorKind.EQUAL, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.EQUAL, symTable.anyType, symTable.nullType));
    } else {
        return createIsAssignableExpression(pos, varSymbol, patternType);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)

Aggregations

BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)6 BOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)5 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)5 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)3 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)3 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BLangUnaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)2 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)2 ArrayList (java.util.ArrayList)1 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)1 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangIsAssignableExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIsAssignableExpr)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1