Search in sources :

Example 6 with BLangTypeofExpr

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

the class TypeChecker method visit.

public void visit(BLangUnaryExpr unaryExpr) {
    BType exprType = null;
    BType actualType = symTable.errType;
    if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
        // Handle typeof operator separately
        if (unaryExpr.expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
            BLangSimpleVarRef varRef = (BLangSimpleVarRef) unaryExpr.expr;
            Name varRefName = names.fromIdNode((varRef).variableName);
            Name pkgAlias = names.fromIdNode((varRef).pkgAlias);
            // Resolve symbol for BLangSimpleVarRef
            BSymbol varRefSybmol = symResolver.lookupSymbolInPackage(unaryExpr.pos, env, pkgAlias, varRefName, SymTag.VARIABLE);
            if (varRefSybmol == symTable.notFoundSymbol) {
                // Resolve symbol for User Defined Type ( converted from BLangSimpleVarRef )
                BLangTypeofExpr typeAccessExpr = getTypeAccessExpression(varRef);
                unaryExpr.expr = typeAccessExpr;
                actualType = typeAccessExpr.type;
                resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
                return;
            } else {
                // Check type if resolved as BLangSimpleVarRef
                exprType = checkExpr(unaryExpr.expr, env).get(0);
            }
        } else {
            // Check type if resolved as non BLangSimpleVarRef Expression
            exprType = checkExpr(unaryExpr.expr, env).get(0);
        }
        if (exprType != symTable.errType) {
            unaryExpr.opSymbol = Symbols.createTypeofOperatorSymbol(exprType, types, symTable, names);
            actualType = unaryExpr.opSymbol.type.getReturnTypes().get(0);
        }
    } else {
        exprType = checkExpr(unaryExpr.expr, env).get(0);
        if (exprType != symTable.errType) {
            BSymbol symbol = symResolver.resolveUnaryOperator(unaryExpr.pos, unaryExpr.operator, exprType);
            if (symbol == symTable.notFoundSymbol) {
                dlog.error(unaryExpr.pos, DiagnosticCode.UNARY_OP_INCOMPATIBLE_TYPES, unaryExpr.operator, exprType);
            } else {
                unaryExpr.opSymbol = (BOperatorSymbol) symbol;
                actualType = symbol.type.getReturnTypes().get(0);
            }
        }
    }
    resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 7 with BLangTypeofExpr

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

the class TypeChecker method getTypeAccessExpression.

private BLangTypeofExpr getTypeAccessExpression(BLangSimpleVarRef varRef) {
    BLangUserDefinedType userDefinedType = new BLangUserDefinedType();
    userDefinedType.pkgAlias = varRef.pkgAlias;
    userDefinedType.typeName = varRef.variableName;
    userDefinedType.pos = varRef.pos;
    BLangTypeofExpr typeAccessExpr = (BLangTypeofExpr) TreeBuilder.createTypeAccessNode();
    typeAccessExpr.typeNode = userDefinedType;
    typeAccessExpr.resolvedType = symResolver.resolveTypeNode(userDefinedType, env);
    typeAccessExpr.pos = varRef.pos;
    typeAccessExpr.type = symTable.typeDesc;
    return typeAccessExpr;
}
Also used : BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr)

Example 8 with BLangTypeofExpr

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

the class Desugar method createInvocationFromTableExpr.

private BLangInvocation createInvocationFromTableExpr(BLangTableQueryExpression tableQueryExpression) {
    List<BLangExpression> args = new ArrayList<>();
    List<BType> retTypes = new ArrayList<>();
    String functionName = QUERY_TABLE_WITHOUT_JOIN_CLAUSE;
    // Order matters, because these are the args for a function invocation.
    args.add(getSQLPreparedStatement(tableQueryExpression));
    args.add(getFromTableVarRef(tableQueryExpression));
    // BLangTypeofExpr
    retTypes.add(tableQueryExpression.type);
    BLangSimpleVarRef joinTable = getJoinTableVarRef(tableQueryExpression);
    if (joinTable != null) {
        args.add(joinTable);
        functionName = QUERY_TABLE_WITH_JOIN_CLAUSE;
    }
    args.add(getSQLStatementParameters(tableQueryExpression));
    args.add(getReturnType(tableQueryExpression));
    return createInvocationNode(functionName, args, retTypes);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Aggregations

BLangTypeofExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)3 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 ArrayList (java.util.ArrayList)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)1 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)1 BLangUserDefinedType (org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType)1 Name (org.wso2.ballerinalang.compiler.util.Name)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1