use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.
the class Desugar method visitFunctionPointerInvocation.
// private functions
private void visitFunctionPointerInvocation(BLangInvocation iExpr) {
BLangVariableReference expr;
if (iExpr.expr == null) {
expr = new BLangSimpleVarRef();
} else {
BLangFieldBasedAccess fieldBasedAccess = new BLangFieldBasedAccess();
fieldBasedAccess.expr = iExpr.expr;
fieldBasedAccess.field = iExpr.name;
expr = fieldBasedAccess;
}
expr.symbol = (BVarSymbol) iExpr.symbol;
expr.type = iExpr.symbol.type;
expr = rewriteExpr(expr);
result = new BFunctionPointerInvocation(iExpr, expr);
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol 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.semantics.model.symbols.BVarSymbol 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.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangAction actionNode) {
addReturnIfNotPresent(actionNode);
SymbolEnv actionEnv = SymbolEnv.createResourceActionSymbolEnv(actionNode, actionNode.symbol.scope, env);
// To preserve endpoint code gen order at action.
Collections.reverse(actionNode.endpoints);
actionNode.endpoints = rewrite(actionNode.endpoints, actionEnv);
actionNode.body = rewrite(actionNode.body, actionEnv);
actionNode.workers = rewrite(actionNode.workers, actionEnv);
// we rewrite it's parameter list to have the receiver variable as the first parameter
BInvokableSymbol actionSymbol = actionNode.symbol;
List<BVarSymbol> params = actionSymbol.params;
BVarSymbol receiverSymbol = actionNode.symbol.receiverSymbol;
params.add(0, receiverSymbol);
BInvokableType actionType = (BInvokableType) actionSymbol.type;
if (receiverSymbol != null) {
actionType.paramTypes.add(0, receiverSymbol.type);
}
result = actionNode;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol 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