Search in sources :

Example 1 with BLangUnaryExpr

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

the class CodeGenerator method visit.

public void visit(BLangUnaryExpr unaryExpr) {
    RegIndex exprIndex = calcAndGetExprRegIndex(unaryExpr);
    if (OperatorKind.ADD.equals(unaryExpr.operator) || OperatorKind.UNTAINT.equals(unaryExpr.operator)) {
        unaryExpr.expr.regIndex = createLHSRegIndex(unaryExpr.regIndex);
        genNode(unaryExpr.expr, this.env);
        return;
    }
    int opcode;
    genNode(unaryExpr.expr, this.env);
    if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
        opcode = unaryExpr.opSymbol.opcode;
        if (opcode == InstructionCodes.TYPEOF) {
            emit(opcode, unaryExpr.expr.regIndex, exprIndex);
        } else {
            Operand typeCPIndex = getTypeCPIndex(unaryExpr.expr.type);
            emit(opcode, typeCPIndex, exprIndex);
        }
    } else if (OperatorKind.LENGTHOF.equals(unaryExpr.operator)) {
        Operand typeCPIndex = getTypeCPIndex(unaryExpr.expr.type);
        opcode = unaryExpr.opSymbol.opcode;
        emit(opcode, unaryExpr.expr.regIndex, typeCPIndex, exprIndex);
    } else {
        opcode = unaryExpr.opSymbol.opcode;
        emit(opcode, unaryExpr.expr.regIndex, exprIndex);
    }
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 2 with BLangUnaryExpr

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

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

the class BLangPackageBuilder method createUnaryExpr.

public void createUnaryExpr(DiagnosticPos pos, Set<Whitespace> ws, String operator) {
    BLangUnaryExpr unaryExpressionNode = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
    unaryExpressionNode.pos = pos;
    unaryExpressionNode.addWS(ws);
    unaryExpressionNode.expr = (BLangExpression) exprNodeStack.pop();
    unaryExpressionNode.operator = OperatorKind.valueFrom(operator);
    addExpressionNode(unaryExpressionNode);
}
Also used : BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)

Example 4 with BLangUnaryExpr

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

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

the class EndpointDesugar method generateServiceRegistered.

private BLangBlockStmt generateServiceRegistered(BEndpointVarSymbol endpoint, BLangService service, SymbolEnv env, BSymbol encSymbol, BSymbol varEncSymbol) {
    final DiagnosticPos pos = service.pos;
    final String epName = endpoint.name.value;
    BLangBlockStmt temp = new BLangBlockStmt();
    final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.type);
    final Name name = endpoint.name;
    epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, name, SymTag.VARIABLE);
    final BLangVariableDef serviceTypeDef = ASTBuilderUtil.createVariableDefStmt(pos, temp);
    serviceTypeDef.var = ASTBuilderUtil.createVariable(pos, service.name + "type", symTable.typeDesc);
    ASTBuilderUtil.defineVariable(serviceTypeDef.var, varEncSymbol, names);
    final BLangUnaryExpr typeOfExpr = ASTBuilderUtil.createUnaryExpr(pos);
    serviceTypeDef.var.expr = typeOfExpr;
    typeOfExpr.operator = OperatorKind.TYPEOF;
    typeOfExpr.expr = getTypeAccessExpression(pos, service.symbol.type);
    typeOfExpr.type = symTable.typeDesc;
    List<BLangVariable> args = Lists.of(serviceTypeDef.var);
    if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() == 2) {
        // Endpoint is already desugared. Fix this correctly.
        args.add(0, epVariable);
    }
    final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
    final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, endpoint.registerFunction, args, symResolver);
    if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() != 2) {
        iExpr.expr = ASTBuilderUtil.createVariableRef(epVariable.pos, epVariable.symbol);
    }
    expressionStmt.expr = iExpr;
    return temp;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

BLangUnaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)4 Name (org.wso2.ballerinalang.compiler.util.Name)2 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)2 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1 BLangTypeofExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr)1 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)1 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)1 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)1 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)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