Search in sources :

Example 21 with BLangService

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

the class SemanticAnalyzer method visit.

public void visit(BLangService serviceNode) {
    BServiceSymbol serviceSymbol = (BServiceSymbol) serviceNode.symbol;
    SymbolEnv serviceEnv = SymbolEnv.createServiceEnv(serviceNode, serviceSymbol.scope, env);
    handleServiceTypeStruct(serviceNode);
    handleServiceEndpointBinds(serviceNode, serviceSymbol);
    serviceNode.annAttachments.forEach(a -> {
        a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.SERVICE);
        this.analyzeDef(a, serviceEnv);
    });
    serviceNode.docAttachments.forEach(doc -> analyzeDef(doc, serviceEnv));
    serviceNode.vars.forEach(v -> this.analyzeDef(v, serviceEnv));
    serviceNode.endpoints.forEach(e -> {
        symbolEnter.defineNode(e, serviceEnv);
        analyzeDef(e, serviceEnv);
    });
    this.analyzeDef(serviceNode.initFunction, serviceEnv);
    serviceNode.resources.forEach(r -> this.analyzeDef(r, serviceEnv));
}
Also used : BServiceSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BServiceSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint)

Example 22 with BLangService

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

the class CodeGenerator method visit.

public void visit(BLangService serviceNode) {
    BLangFunction initFunction = (BLangFunction) serviceNode.getInitFunction();
    visit(initFunction);
    currentServiceInfo = currentPkgInfo.getServiceInfo(serviceNode.getName().getValue());
    SymbolEnv serviceEnv = SymbolEnv.createServiceEnv(serviceNode, serviceNode.symbol.scope, this.env);
    serviceNode.resources.forEach(resource -> genNode(resource, serviceEnv));
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 23 with BLangService

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

the class AnnotationDesugar method rewritePackageAnnotations.

protected void rewritePackageAnnotations(BLangPackage pkgNode) {
    BLangFunction initFunction = pkgNode.initFunction;
    // Remove last return statement. we will add it later. TODO : Fix this properly.
    initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
    // This is the variable which store all package level annotations.
    BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
    // handle Service Annotations.
    for (BLangService service : pkgNode.services) {
        generateAnnotations(service, service.name.value, initFunction, annotationMap);
        for (BLangResource resource : service.resources) {
            String key = service.name.value + DOT + resource.name.value;
            generateAnnotations(resource, key, initFunction, annotationMap);
        }
    }
    // Handle Function Annotations.
    for (BLangFunction function : pkgNode.functions) {
        generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
    }
    // Handle Connector Annotations.
    for (BLangConnector connector : pkgNode.connectors) {
        generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
        for (BLangAction action : connector.actions) {
            String key = connector.name.value + DOT + action.name.value;
            generateAnnotations(connector, key, initFunction, annotationMap);
        }
    }
    // Handle Struct Annotations.
    for (BLangStruct struct : pkgNode.structs) {
        generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
        for (BLangVariable field : struct.fields) {
            String key = struct.name.value + DOT + field.name.value;
            generateAnnotations(field, key, initFunction, annotationMap);
        }
    }
    for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
        generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
    }
    ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction)

Example 24 with BLangService

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

the class EndpointDesugar method rewriteEndpoint.

void rewriteEndpoint(BLangEndpoint endpoint, SymbolEnv env) {
    final BSymbol encSymbol, varSymbol;
    final BLangBlockStmt initBlock, startBlock;
    BLangBlockStmt stopBlock = null;
    if (env.enclInvokable != null) {
        // Function, Action, Resource. Code generate to its body directly.
        encSymbol = varSymbol = env.enclInvokable.symbol;
        initBlock = startBlock = ((BLangInvokableNode) env.node).body;
    } else if (env.enclService != null) {
        encSymbol = env.enclService.symbol;
        varSymbol = ((BLangService) env.node).initFunction.symbol;
        initBlock = startBlock = ((BLangService) env.node).initFunction.body;
    } else {
        // Pkg level endpoint.
        encSymbol = env.enclPkg.symbol;
        varSymbol = ((BLangPackage) env.node).initFunction.symbol;
        initBlock = ((BLangPackage) env.node).initFunction.body;
        startBlock = ((BLangPackage) env.node).startFunction.body;
        stopBlock = ((BLangPackage) env.node).stopFunction.body;
    }
    BLangBlockStmt genInit, genInitCall, genStartCall, genStopCall;
    genInit = generateEndpointInit(endpoint, env, encSymbol);
    genInitCall = generateEndpointInitFunctionCall(endpoint, env, encSymbol, varSymbol);
    genStartCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.startFunction, env, encSymbol);
    genStopCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.stopFunction, env, encSymbol);
    if (env.enclInvokable != null) {
        ASTBuilderUtil.prependStatements(genStartCall, startBlock);
        ASTBuilderUtil.prependStatements(genInitCall, initBlock);
        // TODO : Implement stop.
        ASTBuilderUtil.prependStatements(genInit, initBlock);
    } else if (env.enclService != null) {
        ASTBuilderUtil.appendStatements(genInit, initBlock);
        ASTBuilderUtil.appendStatements(genInitCall, initBlock);
        ASTBuilderUtil.appendStatements(genStartCall, startBlock);
    // TODO : Implement stop.
    } else {
        ASTBuilderUtil.appendStatements(genInit, initBlock);
        ASTBuilderUtil.appendStatements(genInitCall, initBlock);
        ASTBuilderUtil.appendStatements(genStartCall, startBlock);
        ASTBuilderUtil.appendStatements(genStopCall, Objects.requireNonNull(stopBlock));
    }
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode)

Example 25 with BLangService

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

the class EndpointDesugar method generateServiceRegistered.

private BLangBlockStmt generateServiceRegistered(BEndpointVarSymbol endpoint, BLangService service, SymbolEnv env, BSymbol encSymbol, BSymbol varEncSymbol) {
    final DiagnosticPos pos = service.pos;
    final String epName = endpoint.name.value;
    BLangBlockStmt temp = new BLangBlockStmt();
    final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.type);
    final Name name = endpoint.name;
    epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, name, SymTag.VARIABLE);
    final BLangVariableDef serviceTypeDef = ASTBuilderUtil.createVariableDefStmt(pos, temp);
    serviceTypeDef.var = ASTBuilderUtil.createVariable(pos, service.name + "type", symTable.typeDesc);
    ASTBuilderUtil.defineVariable(serviceTypeDef.var, varEncSymbol, names);
    final BLangUnaryExpr typeOfExpr = ASTBuilderUtil.createUnaryExpr(pos);
    serviceTypeDef.var.expr = typeOfExpr;
    typeOfExpr.operator = OperatorKind.TYPEOF;
    typeOfExpr.expr = getTypeAccessExpression(pos, service.symbol.type);
    typeOfExpr.type = symTable.typeDesc;
    List<BLangVariable> args = Lists.of(serviceTypeDef.var);
    if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() == 2) {
        // Endpoint is already desugared. Fix this correctly.
        args.add(0, epVariable);
    }
    final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
    final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, endpoint.registerFunction, args, symResolver);
    if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() != 2) {
        iExpr.expr = ASTBuilderUtil.createVariableRef(epVariable.pos, epVariable.symbol);
    }
    expressionStmt.expr = iExpr;
    return temp;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)8 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)6 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)6 BServiceSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BServiceSymbol)5 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)5 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)5 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)5 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 CompileResult (org.ballerinalang.launcher.util.CompileResult)3 PackageNode (org.ballerinalang.model.tree.PackageNode)3 Test (org.testng.annotations.Test)3 BServiceType (org.wso2.ballerinalang.compiler.semantics.model.types.BServiceType)3 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)3 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)3 BLangInvokableNode (org.wso2.ballerinalang.compiler.tree.BLangInvokableNode)3 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)3