Ejemplo n.º 1
0
        private static bool TryGetCommonPrimitiveType(TypeUsage type1, TypeUsage type2, out TypeUsage commonType)
        {
            Debug.Assert(type1 != null, "type1 must not be null");
            Debug.Assert(Helper.IsPrimitiveType(type1.EdmType), "type1 must be primitive type");
            Debug.Assert(type2 != null, "type2 must not be null");
            Debug.Assert(Helper.IsPrimitiveType(type2.EdmType), "type2 must be primitive type");

            commonType = null;

            if (IsPromotableTo(type1, type2))
            {
                commonType = ForgetConstraints(type2);
                return(true);
            }

            if (IsPromotableTo(type2, type1))
            {
                commonType = ForgetConstraints(type1);
                return(true);
            }

            objectModel.ReadOnlyCollection <PrimitiveType> superTypes = GetPrimitiveCommonSuperTypes((PrimitiveType)type1.EdmType,
                                                                                                     (PrimitiveType)type2.EdmType);
            if (superTypes.Count == 0)
            {
                return(false);
            }

            commonType = TypeUsage.CreateDefaultTypeUsage(superTypes[0]);
            return(null != commonType);
        }
Ejemplo n.º 2
0
        private static bool TryGetCommonType(PrimitiveType primitiveType1, PrimitiveType primitiveType2, out EdmType commonType)
        {
            commonType = null;

            if (IsSubTypeOf(primitiveType1, primitiveType2))
            {
                commonType = primitiveType2;
                return(true);
            }

            if (IsSubTypeOf(primitiveType2, primitiveType1))
            {
                commonType = primitiveType1;
                return(true);
            }

            objectModel.ReadOnlyCollection <PrimitiveType> superTypes = GetPrimitiveCommonSuperTypes(primitiveType1, primitiveType2);
            if (superTypes.Count > 0)
            {
                commonType = superTypes[0];
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        private static void ComputeCommonTypeClosure()
        {
            if (null != _commonTypeClosure)
            {
                return;
            }

            var commonTypeClosure =
                new objectModel.ReadOnlyCollection <PrimitiveType> [EdmConstants.NumPrimitiveTypes, EdmConstants.NumPrimitiveTypes];

            for (var i = 0; i < EdmConstants.NumPrimitiveTypes; i++)
            {
                commonTypeClosure[i, i] = Helper.EmptyPrimitiveTypeReadOnlyCollection;
            }

            var primitiveTypes = EdmProviderManifest.Instance.GetStoreTypes();

            for (var i = 0; i < EdmConstants.NumPrimitiveTypes; i++)
            {
                for (var j = 0; j < i; j++)
                {
                    commonTypeClosure[i, j] = Intersect(
                        EdmProviderManifest.Instance.GetPromotionTypes(primitiveTypes[i]),
                        EdmProviderManifest.Instance.GetPromotionTypes(primitiveTypes[j]));

                    commonTypeClosure[j, i] = commonTypeClosure[i, j];
                }
            }

            AssertTypeInvariant(
                "Common Type closure is incorrect",
                delegate
            {
                for (var i = 0; i < EdmConstants.NumPrimitiveTypes; i++)
                {
                    for (var j = 0; j < EdmConstants.NumPrimitiveTypes; j++)
                    {
                        if (commonTypeClosure[i, j]
                            != commonTypeClosure[j, i])
                        {
                            return(false);
                        }
                        if (i == j &&
                            commonTypeClosure[i, j].Count != 0)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            });

            Interlocked.CompareExchange(ref _commonTypeClosure, commonTypeClosure, null);
        }
Ejemplo n.º 4
0
        private static bool IsSubTypeOf(PrimitiveType subPrimitiveType, PrimitiveType superPrimitiveType)
        {
            if (object.ReferenceEquals(subPrimitiveType, superPrimitiveType))
            {
                return(true);
            }

            if (Helper.AreSameSpatialUnionType(subPrimitiveType, superPrimitiveType))
            {
                return(true);
            }

            objectModel.ReadOnlyCollection <PrimitiveType> superTypes = EdmProviderManifest.Instance.GetPromotionTypes(subPrimitiveType);

            return(-1 != superTypes.IndexOf(superPrimitiveType));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Computes the closure of common super types of the set of predefined edm primitive types
        /// This is done only once and cached as opposed to previous implementation that was computing
        /// this for every new pair of types.
        /// </summary>
        private static void ComputeCommonTypeClosure()
        {
            if (null != _commonTypeClosure)
            {
                return;
            }

            objectModel.ReadOnlyCollection<PrimitiveType>[,] commonTypeClosure = new objectModel.ReadOnlyCollection<PrimitiveType>[EdmConstants.NumPrimitiveTypes, EdmConstants.NumPrimitiveTypes];
            for (int i = 0; i < EdmConstants.NumPrimitiveTypes; i++)
            {
                commonTypeClosure[i, i] = Helper.EmptyPrimitiveTypeReadOnlyCollection;
            }

            objectModel.ReadOnlyCollection<PrimitiveType> primitiveTypes = EdmProviderManifest.Instance.GetStoreTypes();

            for (int i = 0; i < EdmConstants.NumPrimitiveTypes; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    commonTypeClosure[i, j] = Intersect(EdmProviderManifest.Instance.GetPromotionTypes(primitiveTypes[i]),
                                                        EdmProviderManifest.Instance.GetPromotionTypes(primitiveTypes[j]));

                    commonTypeClosure[j, i] = commonTypeClosure[i, j];
                }
            }

            TypeSemantics.AssertTypeInvariant("Common Type closure is incorrect",
                                                delegate()
                                                {
                                                    for (int i = 0; i < EdmConstants.NumPrimitiveTypes; i++)
                                                    {
                                                        for (int j = 0; j < EdmConstants.NumPrimitiveTypes; j++)
                                                        {
                                                            if (commonTypeClosure[i, j] != commonTypeClosure[j, i])
                                                                return false;
                                                            if (i == j && commonTypeClosure[i, j].Count != 0)
                                                                return false;
                                                        }
                                                    }
                                                    return true;
                                                });

            System.Threading.Interlocked.CompareExchange<objectModel.ReadOnlyCollection<PrimitiveType>[,]>(ref _commonTypeClosure, commonTypeClosure, null);
        }