Search in sources :

Example 1 with BLangTransaction

use of org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction 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 BLangTransaction

use of org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addRetryCountExpression.

public void addRetryCountExpression() {
    BLangTransaction transaction = (BLangTransaction) transactionNodeStack.peek();
    transaction.retryCount = (BLangExpression) exprNodeStack.pop();
}
Also used : BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction)

Example 3 with BLangTransaction

use of org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addAbortedBlock.

public void addAbortedBlock() {
    BLangTransaction transaction = (BLangTransaction) transactionNodeStack.peek();
    transaction.onAbortFunction = (BLangExpression) exprNodeStack.pop();
}
Also used : BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction)

Example 4 with BLangTransaction

use of org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction in project ballerina by ballerina-lang.

the class BLangPackageBuilder method endTransactionStmt.

public void endTransactionStmt(DiagnosticPos pos, Set<Whitespace> ws, boolean distributedTransactionEnabled) {
    BLangTransaction transaction = (BLangTransaction) transactionNodeStack.pop();
    transaction.pos = pos;
    transaction.addWS(ws);
    addStmtToCurrentBlock(transaction);
    if (distributedTransactionEnabled) {
        // TODO This is a temporary workaround to flag coordinator service start
        String value = compilerOptions.get(CompilerOptionName.TRANSACTION_EXISTS);
        if (value != null) {
            return;
        }
        compilerOptions.put(CompilerOptionName.TRANSACTION_EXISTS, "true");
        List<String> nameComps = getPackageNameComps(Names.TRANSACTION_PACKAGE.value);
        addImportPackageDeclaration(pos, null, Names.TRANSACTION_ORG.value, nameComps, Names.DEFAULT_VERSION.value, Names.DOT.value + nameComps.get(nameComps.size() - 1));
    }
}
Also used : BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)

Example 5 with BLangTransaction

use of org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addCommittedBlock.

public void addCommittedBlock() {
    BLangTransaction transaction = (BLangTransaction) transactionNodeStack.peek();
    transaction.onCommitFunction = (BLangExpression) exprNodeStack.pop();
}
Also used : BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction)

Aggregations

BLangTransaction (org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction)5 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Map (java.util.Map)1 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)1 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)1 CommonUtil (org.ballerinalang.langserver.common.utils.CommonUtil)1 TreeVisitor (org.ballerinalang.langserver.completions.TreeVisitor)1 Node (org.ballerinalang.model.tree.Node)1 Scope (org.wso2.ballerinalang.compiler.semantics.model.Scope)1 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)1 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)1 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)1 BLangCatch (org.wso2.ballerinalang.compiler.tree.statements.BLangCatch)1 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)1