use of org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef in project ballerina by ballerina-lang.
the class Desugar method getMatchPatternBody.
private BLangBlockStmt getMatchPatternBody(BLangMatchStmtPatternClause patternClause, BLangVariable matchExprVar) {
// Add the variable definition to the body of the pattern clause
if (patternClause.variable.name.value.equals(Names.IGNORE.value)) {
return patternClause.body;
}
// create TypeName i = <TypeName> _$$_
// Create a variable reference for _$$_
BLangSimpleVarRef matchExprVarRef = ASTBuilderUtil.createVariableRef(patternClause.pos, matchExprVar.symbol);
BLangExpression patternVarExpr = addConversionExprIfRequired(matchExprVarRef, patternClause);
// Add the variable def statement
BLangVariable patternVar = ASTBuilderUtil.createVariable(patternClause.pos, "", patternClause.variable.type, patternVarExpr, patternClause.variable.symbol);
BLangVariableDef patternVarDef = ASTBuilderUtil.createVariableDef(patternVar.pos, patternVar);
patternClause.body.stmts.add(0, patternVarDef);
return patternClause.body;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangAssignment assignNode) {
if (assignNode.expr.type.tag == TypeTags.STREAM && assignNode.varRefs.get(0) instanceof BLangSimpleVarRef) {
((BLangRecordLiteral) assignNode.expr).name = ((BLangSimpleVarRef) assignNode.varRefs.get(0)).variableName;
}
assignNode.varRefs = rewriteExprs(assignNode.varRefs);
assignNode.expr = rewriteExpr(assignNode.expr);
result = assignNode;
if (!assignNode.safeAssignment) {
return;
}
// Desugar the =? operator with the match statement
//
// e.g.
// File f;
// .....
// f =? openFile("/tmp/foo.txt"); // openFile: () -> (File | error)
//
// {
// match openFile("/tmp/foo.txt") {
// File _$_f1 => f = _$_f1;
// error e => throw e | return e
// }
// }
BLangBlockStmt safeAssignmentBlock = ASTBuilderUtil.createBlockStmt(assignNode.pos, new ArrayList<>());
BLangExpression lhsExpr = assignNode.varRefs.get(0);
BLangMatchStmtPatternClause patternSuccessCase;
if (assignNode.declaredWithVar) {
BVarSymbol varSymbol = ((BLangSimpleVarRef) lhsExpr).symbol;
BLangVariable variable = ASTBuilderUtil.createVariable(assignNode.pos, "", lhsExpr.type, null, varSymbol);
BLangVariableDef variableDef = ASTBuilderUtil.createVariableDef(assignNode.pos, variable);
safeAssignmentBlock.stmts.add(variableDef);
patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, true, varSymbol, null);
} else {
patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, false, null, lhsExpr);
}
// Create the pattern to match the success case
BLangMatchStmtPatternClause patternErrorCase = getSafeAssignErrorPattern(assignNode.pos, this.env.enclInvokable.symbol);
// Create the match statement
BLangMatch matchStmt = ASTBuilderUtil.createMatchStatement(assignNode.pos, assignNode.expr, new ArrayList<BLangMatchStmtPatternClause>() {
{
add(patternSuccessCase);
add(patternErrorCase);
}
});
// var f =? foo() -> var f;
assignNode.expr = null;
assignNode.safeAssignment = false;
safeAssignmentBlock.stmts.add(matchStmt);
result = rewrite(safeAssignmentBlock, this.env);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef in project ballerina by ballerina-lang.
the class EndpointDesugar method generateEndpointInit.
private BLangBlockStmt generateEndpointInit(BLangEndpoint endpoint, SymbolEnv env, BSymbol encSymbol) {
final String epName = endpoint.name.value;
final DiagnosticPos pos = endpoint.pos;
BLangBlockStmt temp = new BLangBlockStmt();
final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.symbol.type);
epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, names.fromString(epName), SymTag.VARIABLE);
final BLangExpression newExpr;
if (endpoint.configurationExpr != null && endpoint.configurationExpr.getKind() != NodeKind.RECORD_LITERAL_EXPR) {
// Handle Endpoint Assignment.
newExpr = endpoint.configurationExpr;
} else if (endpoint.configurationExpr != null && endpoint.configurationExpr.getKind() == NodeKind.RECORD_LITERAL_EXPR) {
// Handle Endpoint initialization.
newExpr = ASTBuilderUtil.createEmptyRecordLiteral(pos, endpoint.symbol.type);
} else {
newExpr = null;
}
// EPType ep_name = {};
if (env.enclInvokable != null) {
// In callable unit, endpoint is same scope variable.
final BLangVariableDef epNewStmt = ASTBuilderUtil.createVariableDefStmt(pos, temp);
epNewStmt.var = epVariable;
epNewStmt.var.expr = newExpr;
} else {
// This is an init function. ep variable is defined in outside.
if (env.enclService != null) {
// Add to endpoint variable to relevant location
final BLangVariableDef epVarDef = ASTBuilderUtil.createVariableDef(pos);
epVarDef.var = epVariable;
env.enclService.vars.add(epVarDef);
}
final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(pos, temp);
assignmentStmt.varRefs.add(ASTBuilderUtil.createVariableRef(pos, epVariable.symbol));
assignmentStmt.expr = newExpr;
}
return temp;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef in project ballerina by ballerina-lang.
the class EndpointDesugar method generateEndpointInitFunctionCall.
private BLangBlockStmt generateEndpointInitFunctionCall(BLangEndpoint endpoint, SymbolEnv env, BSymbol encSymbol, BSymbol varEncSymbol) {
BLangBlockStmt temp = new BLangBlockStmt();
if (endpoint.configurationExpr == null || endpoint.configurationExpr.getKind() != NodeKind.RECORD_LITERAL_EXPR) {
return temp;
}
final String epName = endpoint.name.value;
final DiagnosticPos pos = endpoint.pos;
final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.symbol.type);
epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, names.fromString(epName), SymTag.VARIABLE);
// EPConfigType ep_nameConf = { ep-config-expr };
final BLangVariableDef epConfigNewStmt = ASTBuilderUtil.createVariableDefStmt(pos, temp);
epConfigNewStmt.var = ASTBuilderUtil.createVariable(pos, epName + "Conf", endpoint.configurationExpr.type);
epConfigNewStmt.var.expr = endpoint.configurationExpr;
ASTBuilderUtil.defineVariable(epConfigNewStmt.var, varEncSymbol, names);
List<BLangVariable> args = Lists.of(epConfigNewStmt.var);
if (endpoint.symbol.initFunction != null && endpoint.symbol.initFunction.params.size() == 2) {
// Endpoint is already desugared. Fix this correctly.
args.add(0, epVariable);
}
// epName.init(ep_nameConf);
final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, endpoint.symbol.initFunction, args, symResolver);
if (endpoint.symbol.initFunction != null && endpoint.symbol.initFunction.params.size() != 2) {
iExpr.expr = ASTBuilderUtil.createVariableRef(epVariable.pos, epVariable.symbol);
}
expressionStmt.expr = iExpr;
return temp;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef in project ballerina by ballerina-lang.
the class IterableCodeDesugar method createCounterVarDefStmt.
/**
* Generates following.
*
* int count = 0;
*
* @param funcNode functionNode
* @param ctx current context
*/
private void createCounterVarDefStmt(BLangFunction funcNode, IterableContext ctx) {
BLangBlockStmt blockStmt = funcNode.body;
final DiagnosticPos pos = blockStmt.pos;
ctx.countVar = ASTBuilderUtil.createVariable(pos, VAR_COUNT, symTable.intType);
ctx.countVar.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
defineVariable(ctx.countVar, funcNode.symbol.pkgID, funcNode);
final BLangVariableDef variableDefStmt = ASTBuilderUtil.createVariableDefStmt(pos, blockStmt);
variableDefStmt.var = ctx.countVar;
}
Aggregations