Search in sources :

Example 11 with BLangFunction

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

the class CodeGenerator method createServiceInfoEntry.

private void createServiceInfoEntry(BLangService serviceNode) {
    // Add service name as an UTFCPEntry to the constant pool
    int serviceNameCPIndex = addUTF8CPEntry(currentPkgInfo, serviceNode.name.value);
    // Create service info
    if (serviceNode.endpointType != null) {
        String endPointQName = serviceNode.endpointType.tsymbol.toString();
        // TODO: bvmAlias needed?
        int epNameCPIndex = addUTF8CPEntry(currentPkgInfo, endPointQName);
        ServiceInfo serviceInfo = new ServiceInfo(currentPackageRefCPIndex, serviceNameCPIndex, serviceNode.symbol.flags, epNameCPIndex);
        // Add service level variables
        int localVarAttNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE.value());
        LocalVariableAttributeInfo localVarAttributeInfo = new LocalVariableAttributeInfo(localVarAttNameIndex);
        serviceNode.vars.forEach(var -> visitVarSymbol(var.var.symbol, pvIndexes, localVarAttributeInfo));
        serviceInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, localVarAttributeInfo);
        // Create the init function info
        BLangFunction serviceInitFunction = (BLangFunction) serviceNode.getInitFunction();
        createFunctionInfoEntry(serviceInitFunction);
        serviceInfo.initFuncInfo = currentPkgInfo.functionInfoMap.get(serviceInitFunction.name.toString());
        currentPkgInfo.addServiceInfo(serviceNode.name.value, serviceInfo);
        // Create resource info entries for all resources
        serviceNode.resources.forEach(res -> createResourceInfoEntry(res, serviceInfo));
    }
}
Also used : ServiceInfo(org.wso2.ballerinalang.programfile.ServiceInfo) LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 12 with BLangFunction

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

the class CodeGenerator method visit.

public void visit(BLangFunction funcNode) {
    SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcNode.symbol.scope, this.env);
    currentCallableUnitInfo = currentPkgInfo.functionInfoMap.get(funcNode.symbol.name.value);
    visitInvokableNode(funcNode, currentCallableUnitInfo, funcEnv);
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 13 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction 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 14 with BLangFunction

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

Example 15 with BLangFunction

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

the class IterableCodeDesugar method createIteratorResultVariables.

private List<BLangVariable> createIteratorResultVariables(IterableContext ctx, BLangVariable lastOperationArg, BLangFunction funcNode) {
    List<BLangVariable> resultVariables = new ArrayList<>();
    if (lastOperationArg.type.tag != TypeTags.TUPLE) {
        resultVariables.add(lastOperationArg);
        defineVariable(lastOperationArg, ctx.env.enclPkg.symbol.pkgID, funcNode);
        return resultVariables;
    }
    final List<BType> tupleTypes = ((BTupleType) lastOperationArg.type).tupleTypes;
    int index = 0;
    for (BType type : tupleTypes) {
        String varName = VAR_RESULT_VAL + index++;
        final BLangVariable variable = ASTBuilderUtil.createVariable(funcNode.pos, varName, type);
        resultVariables.add(variable);
        defineVariable(variable, ctx.env.enclPkg.symbol.pkgID, funcNode);
    }
    return resultVariables;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Aggregations

BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)37 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)24 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)16 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)15 ArrayList (java.util.ArrayList)14 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)13 List (java.util.List)11 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)10 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)9 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)9 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)9 Collectors (java.util.stream.Collectors)8 NodeKind (org.ballerinalang.model.tree.NodeKind)8