use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class Desugar method getSafeAssignErrorPattern.
private BLangMatchStmtPatternClause getSafeAssignErrorPattern(DiagnosticPos pos, BSymbol invokableSymbol) {
// From here onwards we assume that this function has only one return type
// Owner of the variable symbol must be an invokable symbol
boolean noRetParams = ((BInvokableType) invokableSymbol.type).retTypes.isEmpty();
boolean returnErrorType = false;
if (!noRetParams) {
BType retType = ((BInvokableType) invokableSymbol.type).retTypes.get(0);
Set<BType> returnTypeSet = retType.tag == TypeTags.UNION ? ((BUnionType) retType).memberTypes : new HashSet<BType>() {
{
add(retType);
}
};
returnErrorType = returnTypeSet.stream().anyMatch(type -> types.isAssignable(type, symTable.errStructType));
}
// Create the pattern to match the error type
// 1) Create the pattern variable
String patternFailureCaseVarName = GEN_VAR_PREFIX.value + "t_failure";
BLangVariable patternFailureCaseVar = ASTBuilderUtil.createVariable(pos, patternFailureCaseVarName, symTable.errStructType, null, new BVarSymbol(0, names.fromString(patternFailureCaseVarName), this.env.scope.owner.pkgID, symTable.errStructType, this.env.scope.owner));
// 2) Create the pattern block
BLangVariableReference patternFailureCaseVarRef = ASTBuilderUtil.createVariableRef(pos, patternFailureCaseVar.symbol);
BLangBlockStmt patternBlockFailureCase = (BLangBlockStmt) TreeBuilder.createBlockNode();
patternBlockFailureCase.pos = pos;
if (noRetParams || !returnErrorType) {
// throw e
BLangThrow throwStmt = (BLangThrow) TreeBuilder.createThrowNode();
throwStmt.pos = pos;
throwStmt.expr = patternFailureCaseVarRef;
patternBlockFailureCase.stmts.add(throwStmt);
} else {
// return e;
BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
returnStmt.pos = pos;
returnStmt.exprs = new ArrayList<BLangExpression>() {
{
add(patternFailureCaseVarRef);
}
};
patternBlockFailureCase.stmts.add(returnStmt);
}
return ASTBuilderUtil.createMatchStatementPattern(pos, patternFailureCaseVar, patternBlockFailureCase);
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangVariableDef varDefNode) {
if (varDefNode.var.expr instanceof BLangRecordLiteral && ((BLangRecordLiteral) varDefNode.var.expr).type.tag == TypeTags.STREAM) {
((BLangRecordLiteral) varDefNode.var.expr).name = varDefNode.var.name;
}
varDefNode.var = rewrite(varDefNode.var, env);
BLangVariable varNode = varDefNode.var;
// Generate default init expression, if rhs expr is null
if (varNode.expr == null) {
varNode.expr = getInitExpr(varNode.type);
}
if (!varNode.safeAssignment) {
result = varDefNode;
return;
}
// Desugar the =? operator with the match statement
//
// e.g.
// var f =? openFile("/tmp/foo.txt"); // openFile: () -> (File | error)
//
// {
// File f;
// match openFile("/tmp/foo.txt") {
// File _$_f1 => f = _$_f1;
// error e => throw e | return e
// }
// }
// Create the pattern to match the success case
BLangMatchStmtPatternClause patternSuccessCase = getSafeAssignSuccessPattern(varNode.pos, varNode.symbol.type, true, varNode.symbol, null);
BLangMatchStmtPatternClause patternErrorCase = getSafeAssignErrorPattern(varNode.pos, varNode.symbol.owner);
// Create the match statement
BLangMatch matchStmt = ASTBuilderUtil.createMatchStatement(varNode.expr.pos, varNode.expr, new ArrayList<BLangMatchStmtPatternClause>() {
{
add(patternSuccessCase);
add(patternErrorCase);
}
});
// var f =? foo() -> var f;
varNode.expr = null;
varNode.safeAssignment = false;
BLangBlockStmt safeAssignmentBlock = ASTBuilderUtil.createBlockStmt(varDefNode.pos, new ArrayList<BLangStatement>() {
{
add(varDefNode);
add(matchStmt);
}
});
result = rewrite(safeAssignmentBlock, this.env);
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class Desugar method createAssignmentStmt.
// TODO: Same function is used in symbol enter. Refactor this to reuse the same function.
private StatementNode createAssignmentStmt(BLangVariable variable) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
varRef.pos = variable.pos;
varRef.variableName = variable.name;
varRef.symbol = variable.symbol;
varRef.type = variable.type;
varRef.pkgAlias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
BLangAssignment assignmentStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
assignmentStmt.expr = variable.expr;
assignmentStmt.pos = variable.pos;
assignmentStmt.addVariable(varRef);
return assignmentStmt;
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangMatch matchStmt) {
// Here we generate an if-else statement for the match statement
// Here is an example match statement
//
// match expr {
// int k => io:println("int value: " + k);
// string s => io:println("string value: " + s);
// json j => io:println("json value: " + s);
//
// }
//
// Here is how we convert the match statement to an if-else statement. The last clause should always be the
// else clause
//
// string | int | json | any _$$_matchexpr = expr;
// if ( _$$_matchexpr isassignable int ){
// int k = (int) _$$_matchexpr; // unbox
// io:println("int value: " + k);
//
// } else if (_$$_matchexpr isassignable string ) {
// string s = (string) _$$_matchexpr; // unbox
// io:println("string value: " + s);
//
// } else if ( _$$_matchexpr isassignable float || // should we consider json[] as well
// _$$_matchexpr isassignable boolean ||
// _$$_matchexpr isassignable json) {
//
// } else {
// // handle the last pattern
// any case..
// }
//
// First create a block statement to hold generated statements
BLangBlockStmt matchBlockStmt = (BLangBlockStmt) TreeBuilder.createBlockNode();
matchBlockStmt.pos = matchStmt.pos;
// Create a variable definition to store the value of the match expression
String matchExprVarName = GEN_VAR_PREFIX.value;
BLangVariable matchExprVar = ASTBuilderUtil.createVariable(matchStmt.expr.pos, matchExprVarName, matchStmt.expr.type, matchStmt.expr, new BVarSymbol(0, names.fromString(matchExprVarName), this.env.scope.owner.pkgID, matchStmt.expr.type, this.env.scope.owner));
// Now create a variable definition node
BLangVariableDef matchExprVarDef = ASTBuilderUtil.createVariableDef(matchBlockStmt.pos, matchExprVar);
// Add the var def statement to the block statement
// string | int _$$_matchexpr = expr;
matchBlockStmt.stmts.add(matchExprVarDef);
// Create if/else blocks with typeof binary expressions for each pattern
matchBlockStmt.stmts.add(generateIfElseStmt(matchStmt, matchExprVar));
rewrite(matchBlockStmt, this.env);
result = matchBlockStmt;
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class Desugar method getSafeAssignSuccessPattern.
private BLangMatchStmtPatternClause getSafeAssignSuccessPattern(DiagnosticPos pos, BType lhsType, boolean isVarDef, BVarSymbol varSymbol, BLangExpression lhsExpr) {
// File _$_f1 => f = _$_f1;
// 1) Create the pattern variable
String patternSuccessCaseVarName = GEN_VAR_PREFIX.value + "t_match";
BLangVariable patternSuccessCaseVar = ASTBuilderUtil.createVariable(pos, patternSuccessCaseVarName, lhsType, null, new BVarSymbol(0, names.fromString(patternSuccessCaseVarName), this.env.scope.owner.pkgID, lhsType, this.env.scope.owner));
// 2) Create the pattern body
BLangExpression varRefExpr;
if (isVarDef) {
varRefExpr = ASTBuilderUtil.createVariableRef(pos, varSymbol);
} else {
varRefExpr = lhsExpr;
}
BLangVariableReference patternSuccessCaseVarRef = ASTBuilderUtil.createVariableRef(pos, patternSuccessCaseVar.symbol);
BLangAssignment assignmentStmtSuccessCase = ASTBuilderUtil.createAssignmentStmt(pos, new ArrayList<BLangExpression>() {
{
add(varRefExpr);
}
}, patternSuccessCaseVarRef, false);
BLangBlockStmt patternBlockSuccessCase = ASTBuilderUtil.createBlockStmt(pos, new ArrayList<BLangStatement>() {
{
add(assignmentStmtSuccessCase);
}
});
return ASTBuilderUtil.createMatchStatementPattern(pos, patternSuccessCaseVar, patternBlockSuccessCase);
}
Aggregations