Search in sources :

Example 6 with BLangInvokableNode

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

the class CodeGenerator method addParameterDefaultValues.

private void addParameterDefaultValues(BLangInvokableNode invokableNode, CallableUnitInfo callableUnitInfo) {
    int paramDefaultsAttrNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.PARAMETER_DEFAULTS_ATTRIBUTE.value());
    ParamDefaultValueAttributeInfo paramDefaulValAttrInfo = new ParamDefaultValueAttributeInfo(paramDefaultsAttrNameIndex);
    // Only named parameters can have default values.
    for (BLangVariableDef param : invokableNode.defaultableParams) {
        DefaultValue defaultVal = getDefaultValue((BLangLiteral) param.var.expr);
        paramDefaulValAttrInfo.addParamDefaultValueInfo(defaultVal);
    }
    callableUnitInfo.addAttributeInfo(AttributeInfo.Kind.PARAMETER_DEFAULTS_ATTRIBUTE, paramDefaulValAttrInfo);
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 7 with BLangInvokableNode

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

the class Desugar method addReturnIfNotPresent.

private void addReturnIfNotPresent(BLangInvokableNode invokableNode) {
    if (Symbols.isNative(invokableNode.symbol)) {
        return;
    }
    // This will only check whether last statement is a return and just add a return statement.
    // This won't analyse if else blocks etc to see whether return statements are present
    BLangBlockStmt blockStmt = invokableNode.body;
    if (invokableNode.workers.size() == 0 && invokableNode.retParams.isEmpty() && (blockStmt.stmts.size() < 1 || blockStmt.stmts.get(blockStmt.stmts.size() - 1).getKind() != NodeKind.RETURN)) {
        BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
        DiagnosticPos invPos = invokableNode.pos;
        DiagnosticPos pos = new DiagnosticPos(invPos.src, invPos.eLine, invPos.eLine, invPos.sCol, invPos.sCol);
        returnStmt.pos = pos;
        blockStmt.addStatement(returnStmt);
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)

Example 8 with BLangInvokableNode

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

the class TaintAnalyzer method analyzeReturnTaintedStatus.

private void analyzeReturnTaintedStatus(BLangInvokableNode invokableNode, SymbolEnv symbolEnv) {
    invokableNode.endpoints.forEach(endpoint -> endpoint.accept(this));
    if (invokableNode.workers.isEmpty()) {
        analyzeNode(invokableNode.body, symbolEnv);
    } else {
        for (BLangWorker worker : invokableNode.workers) {
            worker.endpoints.forEach(endpoint -> endpoint.accept(this));
            analyzeNode(worker, symbolEnv);
            if (this.blockedNode != null || taintErrorSet.size() > 0) {
                break;
            }
        }
    }
}
Also used : BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker)

Example 9 with BLangInvokableNode

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

the class CodeGenerator method processWorker.

private void processWorker(BLangInvokableNode invokableNode, WorkerInfo workerInfo, BLangBlockStmt body, LocalVariableAttributeInfo localVarAttributeInfo, SymbolEnv invokableSymbolEnv, boolean defaultWorker, VariableIndex lvIndexCopy) {
    int codeAttrNameCPIndex = this.addUTF8CPEntry(this.currentPkgInfo, AttributeInfo.Kind.CODE_ATTRIBUTE.value());
    workerInfo.codeAttributeInfo.attributeNameIndex = codeAttrNameCPIndex;
    workerInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, localVarAttributeInfo);
    if (body != null) {
        localVarAttrInfo = new LocalVariableAttributeInfo(localVarAttributeInfo.attributeNameIndex);
        localVarAttrInfo.localVars = new ArrayList<>(localVarAttributeInfo.localVars);
        workerInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, localVarAttrInfo);
        workerInfo.codeAttributeInfo.codeAddrs = nextIP();
        this.lvIndexes = lvIndexCopy;
        this.currentWorkerInfo = workerInfo;
        this.genNode(body, invokableSymbolEnv);
    }
    this.endWorkerInfoUnit(workerInfo.codeAttributeInfo);
    this.emit(InstructionCodes.HALT);
}
Also used : LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 10 with BLangInvokableNode

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

the class CodeGenerator method visitInvokableNode.

private void visitInvokableNode(BLangInvokableNode invokableNode, CallableUnitInfo callableUnitInfo, SymbolEnv invokableSymbolEnv) {
    int localVarAttrNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE.value());
    LocalVariableAttributeInfo localVarAttributeInfo = new LocalVariableAttributeInfo(localVarAttrNameIndex);
    // TODO Read annotations attached to this callableUnit
    // Add local variable indexes to the parameters and return parameters
    visitInvokableNodeParams(invokableNode.symbol, callableUnitInfo, localVarAttributeInfo);
    if (Symbols.isNative(invokableNode.symbol)) {
        this.processWorker(invokableNode, callableUnitInfo.defaultWorkerInfo, null, localVarAttributeInfo, invokableSymbolEnv, true, null);
    } else {
        // Clone lvIndex structure here. This structure contain local variable indexes of the input and
        // out parameters and they are common for all the workers.
        VariableIndex lvIndexCopy = this.copyVarIndex(lvIndexes);
        this.processWorker(invokableNode, callableUnitInfo.defaultWorkerInfo, invokableNode.body, localVarAttributeInfo, invokableSymbolEnv, true, lvIndexCopy);
        for (BLangWorker worker : invokableNode.getWorkers()) {
            this.processWorker(invokableNode, callableUnitInfo.getWorkerInfo(worker.name.value), worker.body, localVarAttributeInfo, invokableSymbolEnv, false, this.copyVarIndex(lvIndexCopy));
        }
    }
}
Also used : BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)7 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 BLangWorker (org.wso2.ballerinalang.compiler.tree.BLangWorker)4 TaintRecord (org.wso2.ballerinalang.compiler.semantics.model.symbols.TaintRecord)3 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)2 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)2 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)2 LocalVariableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo)2 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 XMLConstants (javax.xml.XMLConstants)1 CompilerPhase (org.ballerinalang.compiler.CompilerPhase)1 TreeBuilder (org.ballerinalang.model.TreeBuilder)1