use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol 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));
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol 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;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangFunction funcNode) {
// Check for native functions
BSymbol funcSymbol = funcNode.symbol;
if (Symbols.isNative(funcSymbol)) {
return;
}
String functionName = funcNode.getName().getValue();
SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcSymbol.scope, symbolEnv);
if (isWithinParameterContext(functionName, NODE_TYPE_FUNCTION)) {
this.populateSymbols(this.resolveAllVisibleSymbols(funcEnv), funcEnv);
setTerminateVisitor(true);
} else if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(funcNode.getPosition(), funcNode, this, this.documentServiceContext)) {
// Visit the endpoints
funcNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, funcEnv));
this.blockOwnerStack.push(funcNode);
// Cursor position is calculated against the Block statement scope resolver
cursorPositionResolver = BlockStatementScopeResolver.class;
this.acceptNode(funcNode.body, funcEnv);
this.blockOwnerStack.pop();
// Process workers
if (terminateVisitor && !funcNode.workers.isEmpty()) {
this.setTerminateVisitor(false);
}
funcNode.workers.forEach(e -> this.symbolEnter.defineNode(e, funcEnv));
funcNode.workers.forEach(e -> this.acceptNode(e, funcEnv));
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangResource resourceNode) {
String resourceName = resourceNode.getName().getValue();
BSymbol resourceSymbol = resourceNode.symbol;
SymbolEnv resourceEnv = SymbolEnv.createResourceActionSymbolEnv(resourceNode, resourceSymbol.scope, symbolEnv);
if (isWithinParameterContext(resourceName, NODE_TYPE_RESOURCE)) {
this.populateSymbols(this.resolveAllVisibleSymbols(resourceEnv), resourceEnv);
setTerminateVisitor(true);
} else if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(resourceNode.getPosition(), resourceNode, this, this.documentServiceContext)) {
// TODO:Handle Annotation attachments
// Visit the endpoints
resourceNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, resourceEnv));
// Cursor position is calculated against the resource parameter scope resolver
cursorPositionResolver = ResourceParamScopeResolver.class;
resourceNode.workers.forEach(w -> this.acceptNode(w, resourceEnv));
this.blockOwnerStack.push(resourceNode);
// Cursor position is calculated against the Block statement scope resolver
cursorPositionResolver = BlockStatementScopeResolver.class;
acceptNode(resourceNode.body, resourceEnv);
this.blockOwnerStack.pop();
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangStruct structNode) {
if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(structNode.getPosition(), structNode, this, this.documentServiceContext)) {
BSymbol structSymbol = structNode.symbol;
SymbolEnv structEnv = SymbolEnv.createPkgLevelSymbolEnv(structNode, structSymbol.scope, symbolEnv);
if (structNode.fields.isEmpty() && isCursorWithinBlock(structNode.getPosition(), structEnv)) {
symbolEnv = structEnv;
Map<Name, Scope.ScopeEntry> visibleSymbolEntries = this.resolveAllVisibleSymbols(symbolEnv);
this.populateSymbols(visibleSymbolEntries, null);
this.setTerminateVisitor(true);
} else if (!structNode.fields.isEmpty()) {
// Since the struct definition do not have a block statement within, we push null
this.blockStmtStack.push(null);
this.blockOwnerStack.push(structNode);
// Cursor position is calculated against the Block statement scope resolver
this.cursorPositionResolver = BlockStatementScopeResolver.class;
structNode.fields.forEach(field -> acceptNode(field, structEnv));
this.blockStmtStack.pop();
this.blockOwnerStack.pop();
}
}
}
Aggregations