use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.
the class ASTBuilderUtil method createReturnStmt.
static BLangReturn createReturnStmt(DiagnosticPos pos, BLangBlockStmt target) {
final BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
returnStmt.pos = pos;
target.addStatement(returnStmt);
return returnStmt;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.
the class AnnotationDesugar method createAnnotationMapEntryVar.
private BLangVariable createAnnotationMapEntryVar(String key, BLangVariable annotationMapVar, BLangBlockStmt target, BSymbol parentSymbol) {
// create: map key = {};
final BLangRecordLiteral recordLiteralNode = ASTBuilderUtil.createEmptyRecordLiteral(target.pos, symTable.mapType);
BLangVariable entryVariable = ASTBuilderUtil.createVariable(target.pos, key, recordLiteralNode.type);
entryVariable.expr = recordLiteralNode;
ASTBuilderUtil.defineVariable(entryVariable, parentSymbol, names);
ASTBuilderUtil.createVariableDefStmt(target.pos, target).var = entryVariable;
// create: annotationMapVar["key"] = key;
BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, entryVariable.symbol);
BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
indexAccessNode.pos = target.pos;
indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, key);
indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapVar.symbol);
indexAccessNode.type = recordLiteralNode.type;
assignmentStmt.varRefs.add(indexAccessNode);
return entryVariable;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.
the class AnnotationDesugar method initAnnotation.
private void initAnnotation(BLangAnnotationAttachment attachment, BLangVariable annotationMapEntryVar, BLangBlockStmt target, BSymbol parentSymbol, int index) {
BLangVariable annotationVar = null;
if (attachment.annotationSymbol.attachedType != null) {
// create: AttachedType annotationVar = { annotation-expression }
annotationVar = ASTBuilderUtil.createVariable(attachment.pos, attachment.annotationName.value, attachment.annotationSymbol.attachedType.type);
annotationVar.expr = attachment.expr;
ASTBuilderUtil.defineVariable(annotationVar, parentSymbol, names);
ASTBuilderUtil.createVariableDefStmt(attachment.pos, target).var = annotationVar;
}
// create: annotationMapEntryVar["name$index"] = annotationVar;
BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
if (annotationVar != null) {
assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationVar.symbol);
} else {
assignmentStmt.expr = ASTBuilderUtil.createLiteral(target.pos, symTable.nullType, null);
}
BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
indexAccessNode.pos = target.pos;
indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, attachment.annotationSymbol.bvmAlias() + "$" + index);
indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapEntryVar.symbol);
indexAccessNode.type = annotationMapEntryVar.symbol.type;
assignmentStmt.varRefs.add(indexAccessNode);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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.statements.BLangBlockStmt 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);
}
Aggregations