Search in sources :

Example 1 with BLangTryCatchFinally

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

the class CodeGenerator method visit.

public void visit(BLangTryCatchFinally tryNode) {
    Operand gotoTryCatchEndAddr = getOperand(-1);
    Instruction instructGotoTryCatchEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoTryCatchEndAddr);
    List<int[]> unhandledErrorRangeList = new ArrayList<>();
    ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
    // Handle try block.
    int fromIP = nextIP();
    genNode(tryNode.tryBody, env);
    int toIP = nextIP() - 1;
    // Append finally block instructions.
    if (tryNode.finallyBody != null) {
        genNode(tryNode.finallyBody, env);
    }
    emit(instructGotoTryCatchEnd);
    unhandledErrorRangeList.add(new int[] { fromIP, toIP });
    // Handle catch blocks.
    int order = 0;
    for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
        addLineNumberInfo(bLangCatch.pos);
        int targetIP = nextIP();
        genNode(bLangCatch, env);
        unhandledErrorRangeList.add(new int[] { targetIP, nextIP() - 1 });
        // Append finally block instructions.
        if (tryNode.finallyBody != null) {
            genNode(tryNode.finallyBody, env);
        }
        emit(instructGotoTryCatchEnd);
        // Create Error table entry for this catch block
        BTypeSymbol structSymbol = bLangCatch.param.symbol.type.tsymbol;
        BPackageSymbol packageSymbol = (BPackageSymbol) bLangCatch.param.symbol.type.tsymbol.owner;
        int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, packageSymbol.pkgID);
        int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
        StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
        int structCPEntryIndex = currentPkgInfo.addCPEntry(structureRefCPEntry);
        StructInfo errorStructInfo = this.programFile.packageInfoMap.get(packageSymbol.pkgID.bvmAlias()).getStructInfo(structSymbol.name.value);
        ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, targetIP, order++, structCPEntryIndex);
        errorTableEntry.setError(errorStructInfo);
        errorTable.addErrorTableEntry(errorTableEntry);
    }
    if (tryNode.finallyBody != null) {
        // Create Error table entry for unhandled errors in try and catch(s) blocks
        for (int[] range : unhandledErrorRangeList) {
            ErrorTableEntry errorTableEntry = new ErrorTableEntry(range[0], range[1], nextIP(), order++, -1);
            errorTable.addErrorTableEntry(errorTableEntry);
        }
        // Append finally block instruction.
        genNode(tryNode.finallyBody, env);
        emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
    }
    gotoTryCatchEndAddr.value = nextIP();
}
Also used : StructInfo(org.wso2.ballerinalang.programfile.StructInfo) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) ArrayList(java.util.ArrayList) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) Instruction(org.wso2.ballerinalang.programfile.Instruction) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) ErrorTableEntry(org.wso2.ballerinalang.programfile.ErrorTableEntry)

Example 2 with BLangTryCatchFinally

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

the class CodeGenerator method generateFinallyInstructions.

private void generateFinallyInstructions(BLangStatement statement, NodeKind... expectedParentKinds) {
    BLangStatement current = statement;
    while (current != null && current.statementLink.parent != null) {
        BLangStatement parent = current.statementLink.parent.statement;
        for (NodeKind expected : expectedParentKinds) {
            if (expected == parent.getKind()) {
                return;
            }
        }
        if (NodeKind.TRY == parent.getKind()) {
            BLangTryCatchFinally tryCatchFinally = (BLangTryCatchFinally) parent;
            if (tryCatchFinally.finallyBody != null && current != tryCatchFinally.finallyBody) {
                genNode(tryCatchFinally.finallyBody, env);
            }
        } else if (NodeKind.LOCK == parent.getKind()) {
            BLangLock lockNode = (BLangLock) parent;
            if (!lockNode.lockVariables.isEmpty()) {
                Operand[] operands = getOperands(lockNode);
                emit((InstructionCodes.UNLOCK), operands);
            }
        }
        current = parent;
    }
}
Also used : NodeKind(org.ballerinalang.model.tree.NodeKind) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally)

Example 3 with BLangTryCatchFinally

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

the class CodeAnalyzer method visit.

public void visit(BLangTryCatchFinally tryNode) {
    this.checkStatementExecutionValidity(tryNode);
    tryNode.tryBody.accept(this);
    this.resetStatementReturns();
    List<BType> caughtTypes = new ArrayList<>();
    for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
        if (caughtTypes.contains(bLangCatch.getParameter().type)) {
            dlog.error(bLangCatch.getParameter().pos, DiagnosticCode.DUPLICATED_ERROR_CATCH, bLangCatch.getParameter().type);
        }
        caughtTypes.add(bLangCatch.getParameter().type);
        bLangCatch.body.accept(this);
        this.resetStatementReturns();
    }
    if (tryNode.finallyBody != null) {
        tryNode.finallyBody.accept(this);
        this.resetStatementReturns();
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch)

Example 4 with BLangTryCatchFinally

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

the class BLangPackageBuilder method addTryCatchFinallyStmt.

public void addTryCatchFinallyStmt(DiagnosticPos poc, Set<Whitespace> ws) {
    BLangTryCatchFinally stmtNode = tryCatchFinallyNodesStack.pop();
    stmtNode.pos = poc;
    stmtNode.addWS(ws);
    addStmtToCurrentBlock(stmtNode);
}
Also used : BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally)

Example 5 with BLangTryCatchFinally

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

the class BLangPackageBuilder method addFinallyBlock.

public void addFinallyBlock(DiagnosticPos poc, Set<Whitespace> ws) {
    BLangBlockStmt blockNode = (BLangBlockStmt) this.blockNodeStack.pop();
    BLangTryCatchFinally rootTry = tryCatchFinallyNodesStack.peek();
    rootTry.finallyBody = blockNode;
    rootTry.addWS(ws);
    blockNode.pos = poc;
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally)

Aggregations

BLangTryCatchFinally (org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally)3 ArrayList (java.util.ArrayList)2 BLangCatch (org.wso2.ballerinalang.compiler.tree.statements.BLangCatch)2 NodeKind (org.ballerinalang.model.tree.NodeKind)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)1 BLangLock (org.wso2.ballerinalang.compiler.tree.statements.BLangLock)1 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)1 ErrorTableEntry (org.wso2.ballerinalang.programfile.ErrorTableEntry)1 Instruction (org.wso2.ballerinalang.programfile.Instruction)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1 StructInfo (org.wso2.ballerinalang.programfile.StructInfo)1 ErrorTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo)1 StructureRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry)1