Search in sources :

Example 1 with BLangFunctionVarRef

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

the class CodeGenerator method visit.

public void visit(BLangTransaction transactionNode) {
    ++transactionIndex;
    Operand transactionIndexOperand = getOperand(transactionIndex);
    Operand retryCountRegIndex = new RegIndex(-1, TypeTags.INT);
    if (transactionNode.retryCount != null) {
        this.genNode(transactionNode.retryCount, this.env);
        retryCountRegIndex = transactionNode.retryCount.regIndex;
    }
    Operand committedFuncRegIndex = new RegIndex(-1, TypeTags.INVOKABLE);
    if (transactionNode.onCommitFunction != null) {
        committedFuncRegIndex.value = getFuncRefCPIndex((BInvokableSymbol) ((BLangFunctionVarRef) transactionNode.onCommitFunction).symbol);
    }
    Operand abortedFuncRegIndex = new RegIndex(-1, TypeTags.INVOKABLE);
    if (transactionNode.onAbortFunction != null) {
        abortedFuncRegIndex.value = getFuncRefCPIndex((BInvokableSymbol) ((BLangFunctionVarRef) transactionNode.onAbortFunction).symbol);
    }
    ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
    Operand transStmtEndAddr = getOperand(-1);
    Operand transStmtAbortEndAddr = getOperand(-1);
    Operand transStmtFailEndAddr = getOperand(-1);
    Instruction gotoAbortTransBlockEnd = InstructionFactory.get(InstructionCodes.GOTO, transStmtAbortEndAddr);
    Instruction gotoFailTransBlockEnd = InstructionFactory.get(InstructionCodes.GOTO, transStmtFailEndAddr);
    abortInstructions.push(gotoAbortTransBlockEnd);
    failInstructions.push(gotoFailTransBlockEnd);
    // start transaction
    this.emit(InstructionCodes.TR_BEGIN, transactionIndexOperand, retryCountRegIndex, committedFuncRegIndex, abortedFuncRegIndex);
    Operand transBlockStartAddr = getOperand(nextIP());
    // retry transaction;
    Operand retryEndWithThrowAddr = getOperand(-1);
    Operand retryEndWithNoThrowAddr = getOperand(-1);
    this.emit(InstructionCodes.TR_RETRY, transactionIndexOperand, retryEndWithThrowAddr, retryEndWithNoThrowAddr);
    // process transaction statements
    this.genNode(transactionNode.transactionBody, this.env);
    // end the transaction
    int transBlockEndAddr = nextIP();
    this.emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.SUCCESS.value()));
    abortInstructions.pop();
    failInstructions.pop();
    emit(InstructionCodes.GOTO, transStmtEndAddr);
    // CodeGen for error handling.
    int errorTargetIP = nextIP();
    transStmtFailEndAddr.value = errorTargetIP;
    emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.FAILED.value()));
    if (transactionNode.onRetryBody != null) {
        this.genNode(transactionNode.onRetryBody, this.env);
    }
    emit(InstructionCodes.GOTO, transBlockStartAddr);
    retryEndWithThrowAddr.value = nextIP();
    emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.END.value()));
    emit(InstructionCodes.THROW, getOperand(-1));
    ErrorTableEntry errorTableEntry = new ErrorTableEntry(transBlockStartAddr.value, transBlockEndAddr, errorTargetIP, 0, -1);
    errorTable.addErrorTableEntry(errorTableEntry);
    transStmtAbortEndAddr.value = nextIP();
    emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.ABORTED.value()));
    int transactionEndIp = nextIP();
    transStmtEndAddr.value = transactionEndIp;
    retryEndWithNoThrowAddr.value = transactionEndIp;
    emit(InstructionCodes.TR_END, transactionIndexOperand, getOperand(TransactionStatus.END.value()));
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) Instruction(org.wso2.ballerinalang.programfile.Instruction) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex) ErrorTableEntry(org.wso2.ballerinalang.programfile.ErrorTableEntry)

Example 2 with BLangFunctionVarRef

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

the class Desugar method visit.

@Override
public void visit(BLangSimpleVarRef varRefExpr) {
    BLangSimpleVarRef genVarRefExpr = varRefExpr;
    // XML qualified name reference. e.g: ns0:foo
    if (varRefExpr.pkgSymbol != null && varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
        BLangXMLQName qnameExpr = new BLangXMLQName(varRefExpr.variableName);
        qnameExpr.nsSymbol = (BXMLNSSymbol) varRefExpr.pkgSymbol;
        qnameExpr.localname = varRefExpr.variableName;
        qnameExpr.prefix = varRefExpr.pkgAlias;
        qnameExpr.namespaceURI = qnameExpr.nsSymbol.namespaceURI;
        qnameExpr.isUsedInXML = false;
        qnameExpr.pos = varRefExpr.pos;
        qnameExpr.type = symTable.stringType;
        result = qnameExpr;
        return;
    }
    BSymbol ownerSymbol = varRefExpr.symbol.owner;
    if ((varRefExpr.symbol.tag & SymTag.FUNCTION) == SymTag.FUNCTION && varRefExpr.symbol.type.tag == TypeTags.INVOKABLE) {
        genVarRefExpr = new BLangFunctionVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // Local variable in a function/resource/action/worker
        genVarRefExpr = new BLangLocalVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        // Field variable in a receiver
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.STRUCT) == SymTag.STRUCT) {
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymbol.tag & SymTag.SERVICE) == SymTag.SERVICE) {
        // Package variable | service variable
        // We consider both of them as package level variables
        genVarRefExpr = new BLangPackageVarRef(varRefExpr.symbol);
        // Only locking service level and package level variables
        if (!enclLocks.isEmpty()) {
            enclLocks.peek().addLockVariable(varRefExpr.symbol);
        }
    }
    genVarRefExpr.type = varRefExpr.type;
    result = genVarRefExpr;
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangPackageVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef) BLangFunctionVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangFieldVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef)

Aggregations

BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1 BLangFieldVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef)1 BLangFunctionVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef)1 BLangLocalVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef)1 BLangPackageVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef)1 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)1 ErrorTableEntry (org.wso2.ballerinalang.programfile.ErrorTableEntry)1 Instruction (org.wso2.ballerinalang.programfile.Instruction)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)1 ErrorTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo)1