Search in sources :

Example 16 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.

the class SymbolResolver method resolveOperator.

private BSymbol resolveOperator(ScopeEntry entry, List<BType> types) {
    BSymbol foundSymbol = symTable.notFoundSymbol;
    while (entry != NOT_FOUND_ENTRY) {
        BInvokableType opType = (BInvokableType) entry.symbol.type;
        if (types.size() == opType.paramTypes.size()) {
            boolean match = true;
            for (int i = 0; i < types.size(); i++) {
                if (types.get(i).tag != opType.paramTypes.get(i).tag) {
                    match = false;
                }
            }
            if (match) {
                foundSymbol = entry.symbol;
                break;
            }
        }
        entry = entry.next;
    }
    return foundSymbol;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)

Example 17 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types 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 18 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types 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 19 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.

the class SemanticAnalyzer method handleSafeAssignmentWithVarDeclaration.

private BType handleSafeAssignmentWithVarDeclaration(DiagnosticPos pos, BType rhsType) {
    if (rhsType.tag != TypeTags.UNION && types.isAssignable(symTable.errStructType, rhsType)) {
        dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
        return symTable.errType;
    } else if (rhsType.tag != TypeTags.UNION) {
        return rhsType;
    }
    // Collect all the rhs types from the union type
    boolean isErrorFound = false;
    BUnionType unionType = (BUnionType) rhsType;
    Set<BType> rhsTypeSet = new HashSet<>(unionType.memberTypes);
    for (BType type : unionType.memberTypes) {
        if (types.isAssignable(type, symTable.errStructType)) {
            rhsTypeSet.remove(type);
            isErrorFound = true;
        }
    }
    if (rhsTypeSet.isEmpty() || !isErrorFound) {
        dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
        return symTable.noType;
    } else if (rhsTypeSet.size() == 1) {
        return rhsTypeSet.toArray(new BType[0])[0];
    }
    return new BUnionType(null, rhsTypeSet, rhsTypeSet.contains(symTable.nullType));
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashSet(java.util.HashSet)

Example 20 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.

the class SemanticAnalyzer method handleServiceEndpointBinds.

private void handleServiceEndpointBinds(BLangService serviceNode, BServiceSymbol serviceSymbol) {
    for (BLangSimpleVarRef ep : serviceNode.boundEndpoints) {
        typeChecker.checkExpr(ep, env);
        if (ep.symbol == null || (ep.symbol.tag & SymTag.ENDPOINT) != SymTag.ENDPOINT) {
            dlog.error(ep.pos, DiagnosticCode.ENDPOINT_INVALID_TYPE, ep.variableName);
            continue;
        }
        final BEndpointVarSymbol epSym = (BEndpointVarSymbol) ep.symbol;
        if ((epSym.tag & SymTag.ENDPOINT) == SymTag.ENDPOINT) {
            if (epSym.registrable) {
                serviceSymbol.boundEndpoints.add(epSym);
                if (serviceNode.endpointType == null) {
                    serviceNode.endpointType = (BStructType) epSym.type;
                    serviceNode.endpointClientType = endpointSPIAnalyzer.getClientType((BStructSymbol) serviceNode.endpointType.tsymbol);
                }
            // TODO : Validate serviceType endpoint type with bind endpoint types.
            } else {
                dlog.error(ep.pos, DiagnosticCode.ENDPOINT_NOT_SUPPORT_REGISTRATION, epSym);
            }
        } else {
            dlog.error(ep.pos, DiagnosticCode.ENDPOINT_INVALID_TYPE, epSym);
        }
    }
    if (serviceNode.endpointType == null) {
        dlog.error(serviceNode.pos, DiagnosticCode.SERVICE_INVALID_ENDPOINT_TYPE, serviceNode.name);
    }
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Aggregations

ArrayList (java.util.ArrayList)20 Test (org.testng.annotations.Test)15 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)13 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)12 HashMap (java.util.HashMap)11 Map (java.util.Map)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 List (java.util.List)8 JsonObject (com.google.gson.JsonObject)5 Test (org.junit.Test)5 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)5 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4 JSONObject (org.json.simple.JSONObject)4 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)4 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)4 JsonElement (com.google.gson.JsonElement)3 Paths (java.nio.file.Paths)3 TokenStream (org.antlr.v4.runtime.TokenStream)3 StringUtils (org.apache.commons.lang3.StringUtils)3