use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.
the class AnnotationDesugar method createGlobalAnnotationMapVar.
private BLangVariable createGlobalAnnotationMapVar(BLangPackage pkgNode) {
DiagnosticPos pos = pkgNode.pos;
BLangVariable annotationMap = ASTBuilderUtil.createVariable(pkgNode.pos, ANNOTATION_DATA, symTable.mapType);
ASTBuilderUtil.defineVariable(annotationMap, pkgNode.symbol, names);
pkgNode.addGlobalVariable(annotationMap);
final BLangRecordLiteral recordLiteralNode = ASTBuilderUtil.createEmptyRecordLiteral(pos, symTable.mapType);
final BLangAssignment annMapAssignment = ASTBuilderUtil.createAssignmentStmt(pos, pkgNode.initFunction.body);
annMapAssignment.expr = recordLiteralNode;
annMapAssignment.addVariable(ASTBuilderUtil.createVariableRef(pos, annotationMap.symbol));
return annotationMap;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral 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.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addMapStructLiteral.
public void addMapStructLiteral(DiagnosticPos pos, Set<Whitespace> ws) {
BLangRecordLiteral recordTypeLiteralNode = recordLiteralNodes.pop();
recordTypeLiteralNode.pos = pos;
recordTypeLiteralNode.addWS(ws);
addExpressionNode(recordTypeLiteralNode);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangRecordLiteral recordLiteral) {
recordLiteral.keyValuePairs.forEach(keyValue -> {
BLangExpression keyExpr = keyValue.key.expr;
if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) keyExpr;
keyValue.key.expr = createStringLiteral(varRef.pos, varRef.variableName.value);
} else {
keyValue.key.expr = rewriteExpr(keyValue.key.expr);
}
keyValue.valueExpr = rewriteExpr(keyValue.valueExpr);
});
BLangExpression expr;
if (recordLiteral.type.tag == TypeTags.STRUCT) {
expr = new BLangStructLiteral(recordLiteral.keyValuePairs, recordLiteral.type);
} else if (recordLiteral.type.tag == TypeTags.MAP) {
expr = new BLangMapLiteral(recordLiteral.keyValuePairs, recordLiteral.type);
} else if (recordLiteral.type.tag == TypeTags.TABLE) {
expr = new BLangTableLiteral(recordLiteral.type);
} else if (recordLiteral.type.tag == TypeTags.STREAM) {
expr = new BLangStreamLiteral(recordLiteral.type, recordLiteral.name);
} else {
expr = new BLangJSONLiteral(recordLiteral.keyValuePairs, recordLiteral.type);
}
result = rewriteExpr(expr);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral in project ballerina by ballerina-lang.
the class ResourceContextHolder method buildContext.
public static ResourceContextHolder buildContext(ResourceNode resource) throws CodeGeneratorException {
ResourceContextHolder context = new ResourceContextHolder();
context.name = resource.getName().getValue();
context.parameters = new ArrayList<>();
for (VariableNode node : resource.getParameters()) {
ParameterContextHolder parameter = ParameterContextHolder.buildContext(node);
if (parameter != null) {
context.parameters.add(parameter);
}
}
// Iterate through all resource level annotations and find out resource configuration information
AnnotationAttachmentNode ann = GeneratorUtils.getAnnotationFromList(GeneratorConstants.RES_CONFIG_ANNOTATION, GeneratorConstants.HTTP_PKG_ALIAS, resource.getAnnotationAttachments());
if (ann == null) {
throw new CodeGeneratorException("Incomplete resource configuration found");
}
BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) ann).getExpression());
List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs();
Map<String, String[]> attrs = GeneratorUtils.getKeyValuePairAsMap(list);
// We don't expect multiple http methods to be supported by single action
// We only consider first content type for a single resource
context.method = attrs.get(GeneratorConstants.ATTR_METHODS) != null ? attrs.get(GeneratorConstants.ATTR_METHODS)[0] : null;
context.contentType = attrs.get(GeneratorConstants.ATTR_CONSUMES) != null ? attrs.get(GeneratorConstants.ATTR_CONSUMES)[0] : null;
String path = attrs.get(GeneratorConstants.ATTR_PATH) != null ? attrs.get(GeneratorConstants.ATTR_PATH)[0] : null;
context.path = context.getTemplatePath(path);
return context;
}
Aggregations