Search in sources :

Example 16 with BInvokableSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol 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 17 with BInvokableSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol in project ballerina by ballerina-lang.

the class CodeGenerator method createActionInfoEntry.

private void createActionInfoEntry(BLangAction actionNode, ConnectorInfo connectorInfo) {
    BInvokableSymbol actionSymbol = actionNode.symbol;
    BInvokableType actionType = (BInvokableType) actionSymbol.type;
    // Add action name as an UTFCPEntry to the constant pool
    int actionNameCPIndex = addUTF8CPEntry(currentPkgInfo, actionNode.name.value);
    ActionInfo actionInfo = new ActionInfo(currentPackageRefCPIndex, actionNameCPIndex);
    actionInfo.paramTypes = actionType.paramTypes.toArray(new BType[0]);
    actionInfo.retParamTypes = actionType.retTypes.toArray(new BType[0]);
    actionInfo.flags = actionSymbol.flags;
    // setParameterNames(actionNode, actionInfo);
    actionInfo.signatureCPIndex = addUTF8CPEntry(currentPkgInfo, generateFunctionSig(actionInfo.paramTypes, actionInfo.retParamTypes));
    // Add worker info
    this.addWorkerInfoEntries(actionInfo, actionNode.getWorkers());
    // Add parameter default value info
    addParameterDefaultValues(actionNode, actionInfo);
    // Add action info to the connector info
    connectorInfo.actionInfoMap.put(actionNode.name.getValue(), actionInfo);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) ActionInfo(org.wso2.ballerinalang.programfile.ActionInfo) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 18 with BInvokableSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol in project ballerina by ballerina-lang.

the class CodeGenerator method createFunctionInfoEntry.

/**
 * Creates a {@code FunctionInfo} from the given function node in AST.
 *
 * @param funcNode function node in AST
 */
private void createFunctionInfoEntry(BLangFunction funcNode) {
    BInvokableSymbol funcSymbol = funcNode.symbol;
    BInvokableType funcType = (BInvokableType) funcSymbol.type;
    // Add function name as an UTFCPEntry to the constant pool
    int funcNameCPIndex = this.addUTF8CPEntry(currentPkgInfo, funcNode.name.value);
    FunctionInfo funcInfo = new FunctionInfo(currentPackageRefCPIndex, funcNameCPIndex);
    funcInfo.paramTypes = funcType.paramTypes.toArray(new BType[0]);
    funcInfo.retParamTypes = funcType.retTypes.toArray(new BType[0]);
    funcInfo.signatureCPIndex = addUTF8CPEntry(this.currentPkgInfo, generateFunctionSig(funcInfo.paramTypes, funcInfo.retParamTypes));
    funcInfo.flags = funcSymbol.flags;
    if (funcNode.receiver != null) {
        funcInfo.attachedToTypeCPIndex = getTypeCPIndex(funcNode.receiver.type).value;
    }
    this.addWorkerInfoEntries(funcInfo, funcNode.getWorkers());
    // Add parameter default value info
    addParameterDefaultValues(funcNode, funcInfo);
    this.currentPkgInfo.functionInfoMap.put(funcSymbol.name.value, funcInfo);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) AttachedFunctionInfo(org.wso2.ballerinalang.programfile.AttachedFunctionInfo) FunctionInfo(org.wso2.ballerinalang.programfile.FunctionInfo) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 19 with BInvokableSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol in project ballerina by ballerina-lang.

the class CodeGenerator method getFuncRefCPIndex.

private int getFuncRefCPIndex(BInvokableSymbol invokableSymbol) {
    int pkgRefCPIndex = addPackageRefCPEntry(currentPkgInfo, invokableSymbol.pkgID);
    int funcNameCPIndex = addUTF8CPEntry(currentPkgInfo, invokableSymbol.name.value);
    FunctionRefCPEntry funcRefCPEntry = new FunctionRefCPEntry(pkgRefCPIndex, funcNameCPIndex);
    return currentPkgInfo.addCPEntry(funcRefCPEntry);
}
Also used : FunctionRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.FunctionRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 20 with BInvokableSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangFunction funcNode) {
    SymbolEnv fucEnv = SymbolEnv.createFunctionEnv(funcNode, funcNode.symbol.scope, env);
    if (!funcNode.interfaceFunction) {
        addReturnIfNotPresent(funcNode);
    }
    // To preserve endpoint code gen order.
    Collections.reverse(funcNode.endpoints);
    funcNode.endpoints = rewrite(funcNode.endpoints, fucEnv);
    funcNode.body = rewrite(funcNode.body, fucEnv);
    funcNode.workers = rewrite(funcNode.workers, fucEnv);
    // the struct variable as the first parameter
    if (funcNode.receiver != null) {
        BInvokableSymbol funcSymbol = funcNode.symbol;
        List<BVarSymbol> params = funcSymbol.params;
        params.add(0, funcNode.receiver.symbol);
        BInvokableType funcType = (BInvokableType) funcSymbol.type;
        funcType.paramTypes.add(0, funcNode.receiver.type);
    }
    result = funcNode;
}
Also used : BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Aggregations

BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)31 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)14 ArrayList (java.util.ArrayList)12 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)10 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)7 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)7 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)7 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)6 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 HashMap (java.util.HashMap)4 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)4 BAttachedFunction (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction)4 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)4 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)4 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)4