Search in sources :

Example 61 with Tag

use of org.wso2.carbon.registry.core.Tag in project ballerina by ballerina-lang.

the class IterableAnalyzer method validateLambdaReturnArgs.

private void validateLambdaReturnArgs(Operation operation, List<BType> supportedTypes, List<BType> givenTypes) {
    if (supportedTypes == givenTypes) {
        // Ignore this validation.
        return;
    }
    if (givenTypes.size() > supportedTypes.size()) {
        dlog.error(operation.pos, DiagnosticCode.ITERABLE_TOO_MANY_RETURN_VARIABLES, operation.kind);
        operation.outputType = operation.resultType = symTable.errType;
        return;
    } else if (givenTypes.size() < supportedTypes.size()) {
        dlog.error(operation.pos, DiagnosticCode.ITERABLE_NOT_ENOUGH_RETURN_VARIABLES, operation.kind);
        operation.outputType = operation.resultType = symTable.errType;
        return;
    }
    for (int i = 0; i < givenTypes.size(); i++) {
        if (givenTypes.get(i).tag == TypeTags.ERROR) {
            return;
        }
        BType result = types.checkType(operation.pos, givenTypes.get(i), supportedTypes.get(i), DiagnosticCode.ITERABLE_LAMBDA_INCOMPATIBLE_TYPES);
        if (result.tag == TypeTags.ERROR) {
            operation.outputType = operation.resultType = symTable.errType;
        }
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 62 with Tag

use of org.wso2.carbon.registry.core.Tag in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

public void visit(BLangMatch matchNode) {
    List<BType> exprTypes = typeChecker.checkExpr(matchNode.expr, env, Lists.of(symTable.noType));
    if (exprTypes.size() > 1) {
        dlog.error(matchNode.expr.pos, DiagnosticCode.MULTI_VAL_EXPR_IN_SINGLE_VAL_CONTEXT);
        return;
    } else if (exprTypes.size() == 0) {
        dlog.error(matchNode.expr.pos, DiagnosticCode.INVALID_EXPR_IN_MATCH_STMT);
        return;
    } else if (exprTypes.get(0).tag == TypeTags.UNION) {
        BUnionType unionType = (BUnionType) exprTypes.get(0);
        exprTypes = new ArrayList<>(unionType.memberTypes);
    }
    // visit patterns
    matchNode.patternClauses.forEach(patternClause -> patternClause.accept(this));
    matchNode.exprTypes = exprTypes;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 63 with Tag

use of org.wso2.carbon.registry.core.Tag in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangArrayLiteral arrayLiteral) {
    // Check whether the expected type is an array type
    // var a = []; and var a = [1,2,3,4]; are illegal statements, because we cannot infer the type here.
    BType actualType = symTable.errType;
    int expTypeTag = expTypes.get(0).tag;
    if (expTypeTag == TypeTags.JSON || expTypeTag == TypeTags.ANY) {
        checkExprs(arrayLiteral.exprs, this.env, expTypes.get(0));
        actualType = expTypes.get(0);
    } else if (expTypeTag == TypeTags.ARRAY) {
        BArrayType arrayType = (BArrayType) expTypes.get(0);
        checkExprs(arrayLiteral.exprs, this.env, arrayType.eType);
        actualType = new BArrayType(arrayType.eType);
    } else if (expTypeTag != TypeTags.ERROR) {
        List<BType> resTypes = checkExprs(arrayLiteral.exprs, this.env, symTable.noType);
        Set<BType> arrayLitExprTypeSet = new HashSet<>(resTypes);
        BType[] uniqueExprTypes = arrayLitExprTypeSet.toArray(new BType[0]);
        if (uniqueExprTypes.length == 0) {
            actualType = symTable.anyType;
        } else if (uniqueExprTypes.length == 1) {
            actualType = resTypes.get(0);
        } else {
            BType superType = uniqueExprTypes[0];
            for (int i = 1; i < uniqueExprTypes.length; i++) {
                if (types.isAssignable(superType, uniqueExprTypes[i])) {
                    superType = uniqueExprTypes[i];
                } else if (!types.isAssignable(uniqueExprTypes[i], superType)) {
                    superType = symTable.anyType;
                    break;
                }
            }
            actualType = superType;
        }
        actualType = new BArrayType(actualType);
    }
    resultTypes = types.checkTypes(arrayLiteral, Lists.of(actualType), expTypes);
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashSet(java.util.HashSet)

Example 64 with Tag

use of org.wso2.carbon.registry.core.Tag in project ballerina by ballerina-lang.

the class TypeChecker method visit.

@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
    // Handle Tuple Expression.
    if (!expTypes.isEmpty() && expTypes.get(0).tag == TypeTags.TUPLE) {
        BTupleType tupleType = (BTupleType) this.expTypes.get(0);
        // Fix this.
        List<BType> expTypes = getListWithErrorTypes(bracedOrTupleExpr.expressions.size());
        if (tupleType.tupleTypes.size() != bracedOrTupleExpr.expressions.size()) {
            dlog.error(bracedOrTupleExpr.pos, DiagnosticCode.SYNTAX_ERROR, "tuple and expression size does not match");
        } else {
            expTypes = tupleType.tupleTypes;
        }
        List<BType> results = new ArrayList<>();
        for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
            results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(expTypes.get(i))).get(0));
        }
        resultTypes = Lists.of(new BTupleType(results));
    } else if (bracedOrTupleExpr.expressions.size() > 1) {
        // This is a tuple.
        List<BType> results = new ArrayList<>();
        for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
            results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(symTable.noType)).get(0));
        }
        resultTypes = Lists.of(new BTupleType(results));
    } else {
        // This is a braced expression.
        bracedOrTupleExpr.isBracedExpr = true;
        final BLangExpression expr = bracedOrTupleExpr.expressions.get(0);
        final BType actualType = checkExpr(expr, env, Lists.of(symTable.noType)).get(0);
        types.setImplicitCastExpr(expr, actualType, expTypes.get(0));
        resultTypes = Lists.of(actualType);
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) ArrayList(java.util.ArrayList) List(java.util.List) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 65 with Tag

use of org.wso2.carbon.registry.core.Tag in project ballerina by ballerina-lang.

the class Types method isArrayTypesAssignable.

public boolean isArrayTypesAssignable(BType source, BType target) {
    if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
        // Both types are array types
        BArrayType lhsArrayType = (BArrayType) target;
        BArrayType rhsArrayType = (BArrayType) source;
        return isArrayTypesAssignable(rhsArrayType.eType, lhsArrayType.eType);
    } else if (source.tag == TypeTags.ARRAY) {
        // to JSON.
        if (target.tag == TypeTags.JSON) {
            return getElementType(source).tag == TypeTags.JSON;
        }
        // Then lhs type should 'any' type
        return target.tag == TypeTags.ANY;
    } else if (target.tag == TypeTags.ARRAY) {
        // Only the left-hand side is an array type
        return false;
    }
    // Now both types are not array types and they have to be equal
    if (target == source) {
        // TODO Figure out this.
        return true;
    }
    // In this case, lhs type should be of type 'any' and the rhs type cannot be a value type
    return target.tag == TypeTags.ANY && !isValueType(source);
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)

Aggregations

ArrayList (java.util.ArrayList)21 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)21 Registry (org.wso2.carbon.registry.core.Registry)20 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)19 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)18 Tag (org.wso2.carbon.registry.core.Tag)18 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)18 API (org.wso2.carbon.apimgt.api.model.API)17 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)16 Resource (org.wso2.carbon.registry.core.Resource)16 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)14 UserStoreException (org.wso2.carbon.user.api.UserStoreException)14 HashSet (java.util.HashSet)13 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)12 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 JSONObject (org.json.simple.JSONObject)10 Tag (org.wso2.carbon.apimgt.api.model.Tag)10 List (java.util.List)9 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)9