use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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.tree.statements.BLangBlockStmt 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.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateMap.
/**
* Generates statements for Map/Select operation. Select operation is similar to Map except that Select returns
* a Table where as Map returns an Array.
*
* v3,v4 = lambda(v1,v2);
*
* @param blockStmt target
* @param operation operation instance
*/
private void generateMap(BLangBlockStmt blockStmt, Operation operation) {
final DiagnosticPos pos = operation.pos;
final BLangAssignment assignment = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
assignment.varRefs.add(ASTBuilderUtil.createVariableRef(operation.pos, operation.retVar.symbol));
assignment.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateDefaultIfEmpty.
/**
* Generates following.
*
* if(count == 0){
* return;
* }
*
* @param blockStmt target
* @param ctx current context
*/
private void generateDefaultIfEmpty(BLangBlockStmt blockStmt, IterableContext ctx) {
if (ctx.resultVar.symbol.type.tag > TypeTags.TYPEDESC) {
return;
}
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr equality = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
equality.pos = pos;
equality.type = symTable.booleanType;
equality.opKind = OperatorKind.EQUAL;
equality.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
equality.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
equality.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.EQUAL, symTable.intType, symTable.intType);
final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
ifNode.expr = equality;
ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
if (ctx.resultVar.symbol.type.tag <= TypeTags.FLOAT) {
final BLangAssignment assign = ASTBuilderUtil.createAssignmentStmt(pos, ifNode.body);
assign.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
switch(ctx.resultVar.symbol.type.tag) {
case TypeTags.INT:
assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
break;
case TypeTags.FLOAT:
assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, 0D);
break;
}
}
final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(pos, ifNode.body);
returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateFilter.
/**
* Generates statements for filter operation.
*
* if(!lambda(...)){
* next;
* }
*
* @param blockStmt target
* @param operation operation instance
*/
private void generateFilter(BLangBlockStmt blockStmt, Operation operation) {
final DiagnosticPos pos = operation.pos;
final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
final BLangUnaryExpr notExpr = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
notExpr.pos = pos;
notExpr.operator = OperatorKind.NOT;
notExpr.opSymbol = (BOperatorSymbol) symResolver.resolveUnaryOperator(pos, notExpr.operator, symTable.booleanType);
notExpr.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
notExpr.type = symTable.booleanType;
ifNode.expr = notExpr;
ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
ASTBuilderUtil.createNextStmt(pos, ifNode.body);
}
Aggregations