Search in sources :

Example 1 with BTupleType

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

the class SymbolResolver method visit.

public void visit(BLangTupleTypeNode tupleTypeNode) {
    List<BType> memberTypes = tupleTypeNode.memberTypeNodes.stream().map(memTypeNode -> resolveTypeNode(memTypeNode, env)).collect(Collectors.toList());
    resultType = new BTupleType(memberTypes);
}
Also used : BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BLangTupleTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangTupleTypeNode) Lists(org.wso2.ballerinalang.util.Lists) BLangBuiltInRefTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangBuiltInRefTypeNode) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BLangValueType(org.wso2.ballerinalang.compiler.tree.types.BLangValueType) OperatorKind(org.ballerinalang.model.tree.OperatorKind) Flag(org.ballerinalang.model.elements.Flag) Map(java.util.Map) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) EnumSet(java.util.EnumSet) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BLangArrayType(org.wso2.ballerinalang.compiler.tree.types.BLangArrayType) BLangUnionTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangUnionTypeNode) TypeKind(org.ballerinalang.model.types.TypeKind) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) NOT_FOUND_ENTRY(org.wso2.ballerinalang.compiler.semantics.model.Scope.NOT_FOUND_ENTRY) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) Stream(java.util.stream.Stream) InstructionCodes(org.wso2.ballerinalang.programfile.InstructionCodes) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BStreamType(org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType) HashMap(java.util.HashMap) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) ArrayList(java.util.ArrayList) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) BLangConstrainedType(org.wso2.ballerinalang.compiler.tree.types.BLangConstrainedType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangFunctionTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangFunctionTypeNode) Name(org.wso2.ballerinalang.compiler.util.Name) ScopeEntry(org.wso2.ballerinalang.compiler.semantics.model.Scope.ScopeEntry) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BLangType(org.wso2.ballerinalang.compiler.tree.types.BLangType) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType)

Example 2 with BTupleType

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

the class IterableAnalyzer method calculatedGivenOutputArgs.

private List<BType> calculatedGivenOutputArgs(Operation operation) {
    final List<BType> givenRetTypes;
    if (operation.lambdaType.getReturnTypes().isEmpty()) {
        givenRetTypes = Collections.emptyList();
        operation.outputType = symTable.voidType;
    } else {
        final BType returnType = operation.outputType = operation.lambdaType.getReturnTypes().get(0);
        if (returnType.tag == TypeTags.TUPLE) {
            givenRetTypes = ((BTupleType) returnType).tupleTypes;
        } else {
            givenRetTypes = operation.lambdaType.getReturnTypes();
        }
    }
    return givenRetTypes;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 3 with BTupleType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType 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 4 with BTupleType

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

the class IterableAnalyzer method assignOutputAndResultType.

private void assignOutputAndResultType(Operation op, List<BType> argTypes, List<BType> supportedRetTypes) {
    if (supportedRetTypes.isEmpty()) {
        op.outputType = op.resultType = symTable.voidType;
        return;
    }
    if (op.kind.isTerminal()) {
        op.outputType = op.resultType = supportedRetTypes.get(0);
        return;
    }
    if (op.kind == IterableKind.FILTER) {
        op.outputType = new BTupleType(argTypes);
        op.resultType = new BIntermediateCollectionType((BTupleType) op.outputType);
        return;
    }
    if (supportedRetTypes.size() == 1) {
        op.outputType = supportedRetTypes.get(0);
    } else {
        op.outputType = new BTupleType(supportedRetTypes);
    }
    op.resultType = new BIntermediateCollectionType(new BTupleType(supportedRetTypes));
}
Also used : BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BIntermediateCollectionType(org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType)

Example 5 with BTupleType

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

the class IterableAnalyzer method calculatedGivenInputArgs.

private List<BType> calculatedGivenInputArgs(Operation operation) {
    final BType inputParam = operation.lambdaType.getParameterTypes().get(0);
    final List<BType> givenArgTypes;
    if (inputParam.tag == TypeTags.TUPLE) {
        final BTupleType bTupleType = (BTupleType) inputParam;
        givenArgTypes = bTupleType.tupleTypes;
    } else {
        givenArgTypes = operation.lambdaType.getParameterTypes();
    }
    operation.arity = givenArgTypes.size();
    return givenArgTypes;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType)

Aggregations

BTupleType (org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType)10 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)9 ArrayList (java.util.ArrayList)6 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)3 List (java.util.List)2 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)2 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)2 BIntermediateCollectionType (org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType)2 BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)2 BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)2 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)2 Name (org.wso2.ballerinalang.compiler.util.Name)2 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1