Search in sources :

Example 71 with Types

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

the class SemanticAnalyzer method handleSafeAssignment.

private void handleSafeAssignment(DiagnosticPos lhsPos, BType lhsType, BLangExpression rhsExpr, SymbolEnv env) {
    // Collect all the lhs types
    Set<BType> lhsTypes = lhsType.tag == TypeTags.UNION ? ((BUnionType) lhsType).memberTypes : new HashSet<BType>() {

        {
            add(lhsType);
        }
    };
    // If there is at least one lhs type which assignable to the error type, then report an error.
    for (BType type : lhsTypes) {
        if (types.isAssignable(symTable.errStructType, type)) {
            dlog.error(lhsPos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
            typeChecker.checkExpr(rhsExpr, env, Lists.of(symTable.errType));
            return;
        }
    }
    // Create a new union type with the error type and continue the type checking process.
    lhsTypes.add(symTable.errStructType);
    BUnionType lhsUnionType = new BUnionType(null, lhsTypes, lhsTypes.contains(symTable.nullType));
    typeChecker.checkExpr(rhsExpr, env, Lists.of(lhsUnionType));
    if (rhsExpr.type.tag == TypeTags.UNION) {
        BUnionType rhsUnionType = (BUnionType) rhsExpr.type;
        for (BType type : rhsUnionType.memberTypes) {
            if (types.isAssignable(symTable.errStructType, type)) {
                return;
            }
        }
    } else if (rhsExpr.type.tag != TypeTags.ERROR) {
        dlog.error(rhsExpr.pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
    }
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 72 with Types

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

the class TypeChecker method checkUnNamedTransformerInvocation.

private List<BType> checkUnNamedTransformerInvocation(BLangTypeConversionExpr conversionExpr, BType sourceType, BType targetType) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    // Check whether a transformer is available for the two types
    BSymbol symbol = symResolver.resolveTransformer(env, sourceType, targetType);
    if (symbol == symTable.notFoundSymbol) {
        // check whether a casting is possible, to provide user a hint.
        BSymbol castSymbol = symResolver.resolveConversionOperator(sourceType, targetType);
        if (castSymbol == symTable.notFoundSymbol) {
            dlog.error(conversionExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION, sourceType, targetType);
        } else {
            dlog.error(conversionExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION_WITH_SUGGESTION, sourceType, targetType);
        }
    } else {
        BTransformerSymbol transformerSymbol = (BTransformerSymbol) symbol;
        conversionExpr.conversionSymbol = transformerSymbol;
        if (conversionExpr.conversionSymbol.safe) {
            ((BInvokableType) transformerSymbol.type).retTypes.add(symTable.errStructType);
        }
        actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, transformerSymbol);
    }
    return actualTypes;
}
Also used : BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 73 with Types

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

the class Types method checkArrayEquality.

public boolean checkArrayEquality(BType source, BType target) {
    if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
        // Both types are array types
        BArrayType lhrArrayType = (BArrayType) target;
        BArrayType rhsArrayType = (BArrayType) source;
        return checkArrayEquality(lhrArrayType.eType, rhsArrayType.eType);
    }
    // Now one or both types are not array types and they have to be equal
    return isSameType(source, target);
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)

Example 74 with Types

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

Example 75 with Types

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

the class Symbols method createTypeofOperatorSymbol.

public static BOperatorSymbol createTypeofOperatorSymbol(BType exprType, Types types, SymbolTable symTable, Names names) {
    List<BType> paramTypes = Lists.of(exprType);
    List<BType> retTypes = Lists.of(symTable.typeDesc);
    BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
    if (types.isValueType(exprType)) {
        return new BOperatorSymbol(names.fromString(OperatorKind.TYPEOF.value()), symTable.rootPkgSymbol.pkgID, opType, symTable.rootPkgSymbol, InstructionCodes.TYPELOAD);
    } else {
        return new BOperatorSymbol(names.fromString(OperatorKind.TYPEOF.value()), symTable.rootPkgSymbol.pkgID, opType, symTable.rootPkgSymbol, InstructionCodes.TYPEOF);
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)

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