Search in sources :

Example 16 with BType

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

the class TypeChecker method visit.

public void visit(BLangInvocation iExpr) {
    // e.g. foo();, foo(), foo().k;
    if (iExpr.expr == null) {
        // This is a function invocation expression. e.g. foo()
        checkFunctionInvocationExpr(iExpr);
        return;
    }
    Name pkgAlias = names.fromIdNode(iExpr.pkgAlias);
    if (pkgAlias != Names.EMPTY) {
        dlog.error(iExpr.pos, DiagnosticCode.PKG_ALIAS_NOT_ALLOWED_HERE);
        return;
    }
    // Find the variable reference expression type
    final List<BType> exprTypes = checkExpr(iExpr.expr, this.env, Lists.of(symTable.noType));
    if (isIterableOperationInvocation(iExpr)) {
        iExpr.iterableOperationInvocation = true;
        iterableAnalyzer.handlerIterableOperation(iExpr, expTypes.isEmpty() ? symTable.voidType : expTypes.get(0), env);
        if (iExpr.iContext.operations.getLast().resultType == symTable.voidType) {
            resultTypes = Collections.emptyList();
        } else {
            resultTypes = Lists.of(iExpr.iContext.operations.getLast().resultType);
        }
        return;
    }
    if (iExpr.actionInvocation) {
        if (exprTypes.size() != 1) {
            dlog.error(iExpr.expr.pos, DiagnosticCode.SINGLE_VALUE_RETURN_EXPECTED);
        }
        checkActionInvocationExpr(iExpr, exprTypes.size() > 0 ? exprTypes.get(0) : symTable.errType);
        return;
    }
    switch(iExpr.expr.type.tag) {
        case TypeTags.STRUCT:
            // Invoking a function bound to a struct
            // First check whether there exist a function with this name
            // Then perform arg and param matching
            checkFunctionInvocationExpr(iExpr, (BStructType) iExpr.expr.type);
            break;
        case TypeTags.CONNECTOR:
            dlog.error(iExpr.pos, DiagnosticCode.INVALID_ACTION_INVOCATION_SYNTAX);
            resultTypes = getListWithErrorTypes(expTypes.size());
            return;
        case TypeTags.BOOLEAN:
        case TypeTags.STRING:
        case TypeTags.INT:
        case TypeTags.FLOAT:
        case TypeTags.BLOB:
        case TypeTags.XML:
            checkFunctionInvocationExpr(iExpr, iExpr.expr.type);
            break;
        case TypeTags.JSON:
            checkFunctionInvocationExpr(iExpr, symTable.jsonType);
            break;
        case TypeTags.TABLE:
            checkFunctionInvocationExpr(iExpr, symTable.tableType);
            break;
        case TypeTags.STREAM:
            checkFunctionInvocationExpr(iExpr, symTable.streamType);
            break;
        case TypeTags.FUTURE:
            checkFunctionInvocationExpr(iExpr, symTable.futureType);
            break;
        case TypeTags.NONE:
            dlog.error(iExpr.pos, DiagnosticCode.UNDEFINED_FUNCTION, iExpr.name);
            break;
        case TypeTags.MAP:
            // allow map function for both constrained / un constrained maps
            checkFunctionInvocationExpr(iExpr, this.symTable.mapType);
            break;
        case TypeTags.ERROR:
            break;
        case TypeTags.INTERMEDIATE_COLLECTION:
            dlog.error(iExpr.pos, DiagnosticCode.INVALID_FUNCTION_INVOCATION_WITH_NAME, iExpr.name, iExpr.expr.type);
            resultTypes = getListWithErrorTypes(expTypes.size());
            break;
        default:
            dlog.error(iExpr.pos, DiagnosticCode.INVALID_FUNCTION_INVOCATION, iExpr.expr.type);
            resultTypes = getListWithErrorTypes(expTypes.size());
            break;
    }
// TODO other types of invocation expressions
// TODO pkg alias should be null or empty here.
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 17 with BType

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

the class CodeAnalyzer method visit.

@Override
public void visit(BLangMatch matchStmt) {
    this.returnWithintransactionCheckStack.push(true);
    boolean unmatchedExprTypesAvailable = false;
    analyzeExpr(matchStmt.expr);
    // TODO Handle **any** as a expr type.. special case it..
    // TODO Complete the exhaustive tests with any, struct and connector types
    // TODO Handle the case where there are incompatible types. e.g. input string : pattern int and pattern string
    List<BType> unmatchedExprTypes = new ArrayList<>();
    for (BType exprType : matchStmt.exprTypes) {
        boolean assignable = false;
        for (BLangMatchStmtPatternClause pattern : matchStmt.patternClauses) {
            BType patternType = pattern.variable.type;
            if (exprType.tag == TypeTags.ERROR || patternType.tag == TypeTags.ERROR) {
                return;
            }
            assignable = this.types.isAssignable(exprType, patternType);
            if (assignable) {
                pattern.matchedTypesDirect.add(exprType);
                break;
            } else if (exprType.tag == TypeTags.ANY) {
                pattern.matchedTypesIndirect.add(exprType);
            } else if (exprType.tag == TypeTags.JSON && this.types.isAssignable(patternType, exprType)) {
                pattern.matchedTypesIndirect.add(exprType);
            } else if (exprType.tag == TypeTags.STRUCT && this.types.isAssignable(patternType, exprType)) {
                pattern.matchedTypesIndirect.add(exprType);
            } else {
            // TODO Support other assignable types
            }
        }
        if (!assignable) {
            unmatchedExprTypes.add(exprType);
        }
    }
    if (!unmatchedExprTypes.isEmpty()) {
        unmatchedExprTypesAvailable = true;
        dlog.error(matchStmt.pos, DiagnosticCode.MATCH_STMT_CANNOT_GUARANTEE_A_MATCHING_PATTERN, unmatchedExprTypes);
    }
    boolean matchedPatternsAvailable = false;
    for (int i = matchStmt.patternClauses.size() - 1; i >= 0; i--) {
        BLangMatchStmtPatternClause pattern = matchStmt.patternClauses.get(i);
        if (pattern.matchedTypesDirect.isEmpty() && pattern.matchedTypesIndirect.isEmpty()) {
            if (matchedPatternsAvailable) {
                dlog.error(pattern.pos, DiagnosticCode.MATCH_STMT_UNMATCHED_PATTERN);
            } else {
                dlog.error(pattern.pos, DiagnosticCode.MATCH_STMT_UNREACHABLE_PATTERN);
            }
        } else {
            matchedPatternsAvailable = true;
        }
    }
    // Execute the following block if there are no unmatched expression types
    if (!unmatchedExprTypesAvailable) {
        this.checkStatementExecutionValidity(matchStmt);
        boolean matchStmtReturns = true;
        for (BLangMatchStmtPatternClause patternClause : matchStmt.patternClauses) {
            patternClause.body.accept(this);
            matchStmtReturns = matchStmtReturns && this.statementReturns;
            this.resetStatementReturns();
        }
        this.statementReturns = matchStmtReturns;
    }
    this.returnWithintransactionCheckStack.pop();
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 18 with BType

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

the class EndpointSPIAnalyzer method getEndpointTypeFromServiceType.

public BStructType getEndpointTypeFromServiceType(DiagnosticPos pos, BType type) {
    if (type.tag != TypeTags.STRUCT) {
        dlog.error(pos, DiagnosticCode.ENDPOINT_STRUCT_TYPE_REQUIRED);
        return null;
    }
    final BStructSymbol serviceType = (BStructSymbol) type.tsymbol;
    for (BStructSymbol.BAttachedFunction attachedFunc : serviceType.attachedFuncs) {
        if (Names.EP_SERVICE_GET_ENDPOINT.equals(attachedFunc.funcName)) {
            if (attachedFunc.type.getParameterTypes().size() != 0 || attachedFunc.type.retTypes.size() != 1 || attachedFunc.type.retTypes.get(0).tag != TypeTags.STRUCT) {
                dlog.error(pos, DiagnosticCode.SERVICE_INVALID_STRUCT_TYPE, serviceType);
                return null;
            }
            final BStructSymbol endPointType = (BStructSymbol) attachedFunc.type.retTypes.get(0).tsymbol;
            if (isValidEndpointSPI(pos, endPointType)) {
                return (BStructType) attachedFunc.type.retTypes.get(0);
            }
            break;
        }
    }
    dlog.error(pos, DiagnosticCode.SERVICE_INVALID_STRUCT_TYPE, serviceType);
    return null;
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 19 with BType

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

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

the class IterableAnalyzer method handlerIterableOperation.

public void handlerIterableOperation(BLangInvocation iExpr, BType expectedType, SymbolEnv env) {
    final IterableContext context;
    if (iExpr.expr.type.tag != TypeTags.INTERMEDIATE_COLLECTION) {
        // This is a new iteration chain.
        context = new IterableContext(iExpr.expr, env);
    } else {
        // Get context from previous invocation.
        context = ((BLangInvocation) iExpr.expr).iContext;
    }
    iExpr.iContext = context;
    final IterableKind iterableKind = IterableKind.getFromString(iExpr.name.value);
    final Operation iOperation = new Operation(iterableKind, iExpr, expectedType);
    iExpr.iContext.addOperation(iOperation);
    if (iterableKind.isLambdaRequired()) {
        handleLambdaBasedIterableOperation(context, iOperation);
    } else {
        handleSimpleTerminalOperations(iOperation);
    }
    validateIterableContext(context);
    if (iOperation.resultType != symTable.errType && context.foreachTypes.isEmpty()) {
        calculateForeachTypes(context);
    }
}
Also used : IterableContext(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)

Aggregations

BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)97 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)34 ArrayList (java.util.ArrayList)30 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)30 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)25 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)23 Name (org.wso2.ballerinalang.compiler.util.Name)20 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)18 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)17 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)17 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)17 List (java.util.List)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)16 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)16 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)15 Collectors (java.util.stream.Collectors)14 BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)13 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)13 Set (java.util.Set)12 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)12