Search in sources :

Example 21 with BLangRecordLiteral

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 22 with BLangRecordLiteral

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);
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 23 with BLangRecordLiteral

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);
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 24 with BLangRecordLiteral

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);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangStreamLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStreamLiteral) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangTableLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangTableLiteral)

Example 25 with BLangRecordLiteral

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;
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)25 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)10 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)8 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)5 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)5 HashSet (java.util.HashSet)4 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 SecretModel (org.ballerinax.kubernetes.models.SecretModel)2 ServiceModel (org.ballerinax.kubernetes.models.ServiceModel)2 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)2 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)2 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)2 Collections (java.util.Collections)1