Search in sources :

Example 1 with BTableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BTableType in project ballerina by ballerina-lang.

the class SymbolResolver method visit.

public void visit(BLangConstrainedType constrainedTypeNode) {
    BType type = resolveTypeNode(constrainedTypeNode.type, env);
    BType constraintType = resolveTypeNode(constrainedTypeNode.constraint, env);
    if (type.tag == TypeTags.TABLE) {
        resultType = new BTableType(TypeTags.TABLE, constraintType, type.tsymbol);
    } else if (type.tag == TypeTags.STREAM) {
        resultType = new BStreamType(TypeTags.STREAM, constraintType, type.tsymbol);
    } else if (type.tag == TypeTags.FUTURE) {
        resultType = new BFutureType(TypeTags.FUTURE, constraintType, type.tsymbol);
    } else if (type.tag == TypeTags.MAP) {
        resultType = new BMapType(TypeTags.MAP, constraintType, type.tsymbol);
    } else {
        if (!types.checkStructToJSONCompatibility(constraintType) && constraintType != symTable.errType) {
            dlog.error(constrainedTypeNode.pos, DiagnosticCode.INCOMPATIBLE_TYPE_CONSTRAINT, type, constraintType);
            resultType = symTable.errType;
            return;
        }
        resultType = new BJSONType(TypeTags.JSON, constraintType, type.tsymbol);
    }
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BStreamType(org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType)

Example 2 with BTableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BTableType in project ballerina by ballerina-lang.

the class IterableAnalyzer method validateIterableContext.

public void validateIterableContext(IterableContext context) {
    final Operation lastOperation = context.operations.getLast();
    final BType expectedType = lastOperation.expectedType;
    final BType outputType = lastOperation.resultType;
    if (expectedType.tag == TypeTags.VOID && outputType.tag == TypeTags.VOID) {
        context.resultType = symTable.noType;
        return;
    }
    if (expectedType.tag == TypeTags.VOID) {
        // This error already logged.
        return;
    }
    if (expectedType == symTable.errType) {
        context.resultType = symTable.errType;
        return;
    }
    if (outputType.tag == TypeTags.VOID) {
        dlog.error(lastOperation.pos, DiagnosticCode.DOES_NOT_RETURN_VALUE, lastOperation.kind);
        context.resultType = symTable.errType;
        return;
    }
    // Calculate expected type, if this is an chained iterable operation.
    if (outputType.tag == TypeTags.INTERMEDIATE_COLLECTION) {
        BIntermediateCollectionType collectionType = (BIntermediateCollectionType) outputType;
        final BTupleType tupleType = collectionType.tupleType;
        if (expectedType.tag == TypeTags.ARRAY && tupleType.tupleTypes.size() == 1) {
            // Convert result into an array.
            context.resultType = new BArrayType(tupleType.tupleTypes.get(0));
            return;
        } else if (expectedType.tag == TypeTags.MAP && tupleType.tupleTypes.size() == 2 && tupleType.tupleTypes.get(0).tag == TypeTags.STRING) {
            // Convert result into a map.
            context.resultType = new BMapType(TypeTags.MAP, tupleType.tupleTypes.get(1), null);
            return;
        } else if (expectedType.tag == TypeTags.TABLE) {
            // 3. Whether the returned struct is compatible with the constraint struct of the expected type(table)
            if (tupleType.getTupleTypes().size() == 1 && tupleType.getTupleTypes().get(0).tag == TypeTags.STRUCT && types.isAssignable(tupleType.getTupleTypes().get(0), ((BTableType) expectedType).constraint)) {
                context.resultType = symTable.tableType;
            } else {
                context.resultType = types.checkType(lastOperation.pos, outputType, ((BTableType) expectedType).constraint, DiagnosticCode.INCOMPATIBLE_TYPES);
            }
            return;
        } else if (expectedType.tag == TypeTags.ANY) {
            context.resultType = symTable.errType;
            dlog.error(lastOperation.pos, DiagnosticCode.ITERABLE_RETURN_TYPE_MISMATCH, lastOperation.kind);
            return;
        } else if (expectedType.tag == TypeTags.NONE) {
            context.resultType = symTable.noType;
            return;
        }
    }
    // Validate compatibility with calculated and expected type.
    context.resultType = types.checkType(lastOperation.pos, outputType, expectedType, DiagnosticCode.INCOMPATIBLE_TYPES);
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) BIntermediateCollectionType(org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Example 3 with BTableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BTableType in project ballerina by ballerina-lang.

the class Desugar method getReturnType.

private BLangStructLiteral getReturnType(BLangTableQueryExpression tableQueryExpression) {
    // create a literal to represent the sql query.
    BTableType tableType = (BTableType) tableQueryExpression.type;
    BStructType structType = (BStructType) tableType.constraint;
    return new BLangStructLiteral(new ArrayList<>(), structType);
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Example 4 with BTableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BTableType in project ballerina by ballerina-lang.

the class IterableCodeDesugar method createResultVarDefStmt.

/**
 * Generates following.
 *
 * array:   result =[];
 * map:     result = {};
 *
 * @param funcNode functionNode
 * @param ctx      current context
 */
private void createResultVarDefStmt(BLangFunction funcNode, IterableContext ctx) {
    BLangBlockStmt blockStmt = funcNode.body;
    final IterableKind kind = ctx.getLastOperation().kind;
    if (ctx.resultType.tag != TypeTags.ARRAY && ctx.resultType.tag != TypeTags.MAP && ctx.resultType.tag != TypeTags.TABLE && kind != IterableKind.MAX && kind != IterableKind.MIN) {
        return;
    }
    final DiagnosticPos pos = blockStmt.pos;
    final BLangVariableDef defStmt = ASTBuilderUtil.createVariableDefStmt(pos, blockStmt);
    defStmt.var = ctx.resultVar;
    switch(ctx.resultType.tag) {
        case TypeTags.ARRAY:
            final BLangArrayLiteral arrayInit = (BLangArrayLiteral) TreeBuilder.createArrayLiteralNode();
            arrayInit.pos = pos;
            arrayInit.exprs = new ArrayList<>();
            arrayInit.type = ctx.resultType;
            defStmt.var.expr = arrayInit;
            break;
        case TypeTags.MAP:
            defStmt.var.expr = ASTBuilderUtil.createEmptyRecordLiteral(pos, ctx.resultType);
            break;
        case TypeTags.TABLE:
            BLangVariable retVars = ctx.getFirstOperation().retVar;
            BType tableType = new BTableType(TypeTags.TABLE, retVars.type, symTable.tableType.tsymbol);
            defStmt.var.expr = ASTBuilderUtil.createEmptyRecordLiteral(pos, tableType);
            break;
        case TypeTags.INT:
            if (kind == IterableKind.MAX) {
                defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, Long.MIN_VALUE);
            } else if (kind == IterableKind.MIN) {
                defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, Long.MAX_VALUE);
            }
            break;
        case TypeTags.FLOAT:
            if (kind == IterableKind.MAX) {
                defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, Double.MIN_NORMAL);
            } else if (kind == IterableKind.MIN) {
                defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, Double.MAX_VALUE);
            }
            break;
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 5 with BTableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BTableType in project ballerina by ballerina-lang.

the class PackageActionFunctionAndTypesFilter method populateIterableOperations.

private void populateIterableOperations(SymbolInfo variable, List<SymbolInfo> symbolInfoList) {
    BType bType = variable.getScopeEntry().symbol.getType();
    if (bType instanceof BArrayType || bType instanceof BMapType || bType instanceof BJSONType || bType instanceof BXMLType || bType instanceof BTableType || bType instanceof BIntermediateCollectionType) {
        fillForeachIterableOperation(bType, symbolInfoList);
        fillMapIterableOperation(bType, symbolInfoList);
        fillFilterIterableOperation(bType, symbolInfoList);
        fillCountIterableOperation(symbolInfoList);
        if (bType instanceof BArrayType && (((BArrayType) bType).eType.toString().equals("int") || ((BArrayType) bType).eType.toString().equals("float"))) {
            fillMinIterableOperation(symbolInfoList);
            fillMaxIterableOperation(symbolInfoList);
            fillAverageIterableOperation(symbolInfoList);
            fillSumIterableOperation(symbolInfoList);
        }
    // TODO: Add support for Table and Tuple collection
    }
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BXMLType(org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BIntermediateCollectionType(org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Aggregations

BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)6 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)5 BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)4 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)3 BIntermediateCollectionType (org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType)2 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)2 IterableKind (org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind)1 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)1 BFutureType (org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType)1 BStreamType (org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType)1 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)1 BTupleType (org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType)1 BXMLType (org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)1 BLangStructLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral)1 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)1 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)1 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)1