Move Enum and CorElementType to shared (#23177)
authorEgor Bogatov <egorbo@gmail.com>
Sun, 17 Mar 2019 06:10:36 +0000 (07:10 +0100)
committerJan Kotas <jkotas@microsoft.com>
Sun, 17 Mar 2019 06:10:36 +0000 (23:10 -0700)
src/System.Private.CoreLib/System.Private.CoreLib.csproj
src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems
src/System.Private.CoreLib/shared/System/Enum.cs [moved from src/System.Private.CoreLib/src/System/Enum.cs with 92% similarity]
src/System.Private.CoreLib/shared/System/Reflection/CorElementType.cs [new file with mode: 0644]
src/System.Private.CoreLib/src/System/Enum.CoreCLR.cs [new file with mode: 0644]
src/System.Private.CoreLib/src/System/Reflection/MdImport.cs

index 5369771..6e73aef 100644 (file)
     <Compile Include="$(BclSourcesRoot)\System\Diagnostics\SymbolStore\ISymWriter.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Diagnostics\SymbolStore\SymAddressKind.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Diagnostics\SymbolStore\Token.cs" />
-    <Compile Include="$(BclSourcesRoot)\System\Enum.cs" />
+    <Compile Include="$(BclSourcesRoot)\System\Enum.CoreCLR.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Environment.CoreCLR.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Exception.cs" />
     <Compile Include="$(BclSourcesRoot)\System\GC.cs" />
index e8baac9..85ba8b8 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)System\Environment.SpecialFolder.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Environment.SpecialFolderOption.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\EnvironmentVariableTarget.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Enum.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\EventArgs.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\EventHandler.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\ExecutionEngineException.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\BindingFlags.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CallingConventions.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ConstructorInfo.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CorElementType.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CustomAttributeFormatException.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\DefaultMemberAttribute.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\AssemblyBuilderAccess.cs" />
@@ -6,10 +6,13 @@ using System.Diagnostics;
 using System.Globalization;
 using System.Reflection;
 using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-using System.Text;
 using Internal.Runtime.CompilerServices;
 
+#if CORERT
+using CorElementType = System.Runtime.RuntimeImports.RhCorElementType;
+using RuntimeType = System.Type;
+#endif
+
 // The code below includes partial support for float/double and
 // pointer sized enums.
 //
@@ -26,34 +29,13 @@ namespace System
 {
     [Serializable]
     [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
-    public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible
+    public abstract partial class Enum : ValueType, IComparable, IFormattable, IConvertible
     {
         #region Private Constants
         private const char EnumSeparatorChar = ',';
         #endregion
 
         #region Private Static Methods
-        private static TypeValuesAndNames GetCachedValuesAndNames(RuntimeType enumType, bool getNames)
-        {
-            TypeValuesAndNames entry = enumType.GenericCache as TypeValuesAndNames;
-
-            if (entry == null || (getNames && entry.Names == null))
-            {
-                ulong[] values = null;
-                string[] names = null;
-                GetEnumValuesAndNames(
-                    enumType.GetTypeHandleInternal(),
-                    JitHelpers.GetObjectHandleOnStack(ref values),
-                    JitHelpers.GetObjectHandleOnStack(ref names),
-                    getNames);
-                bool isFlags = enumType.IsDefined(typeof(FlagsAttribute), inherit: false);
-
-                entry = new TypeValuesAndNames(isFlags, values, names);
-                enumType.GenericCache = entry;
-            }
-
-            return entry;
-        }
 
         private string ValueToString()
         {
@@ -153,12 +135,12 @@ namespace System
         internal static string GetEnumName(RuntimeType eT, ulong ulValue)
         {
             Debug.Assert(eT != null);
-            ulong[] ulValues = Enum.InternalGetValues(eT);
+            ulong[] ulValues = InternalGetValues(eT);
             int index = Array.BinarySearch(ulValues, ulValue);
 
             if (index >= 0)
             {
-                string[] names = Enum.InternalGetNames(eT);
+                string[] names = InternalGetNames(eT);
                 return names[index];
             }
 
@@ -174,7 +156,7 @@ namespace System
 
             if (!entry.IsFlag) // Not marked with Flags attribute
             {
-                return Enum.GetEnumName(eT, value);
+                return GetEnumName(eT, value);
             }
             else // These are flags OR'ed together (We treat everything as unsigned types)
             {
@@ -336,17 +318,6 @@ namespace System
             return result;
         }
 
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        private static extern int InternalCompareTo(object o1, object o2);
-
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
-
-        [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
-        private static extern void GetEnumValuesAndNames(RuntimeTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, bool getNames);
-
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        private static extern object InternalBoxEnum(RuntimeType enumType, long value);
         #endregion
 
         #region Public Static Methods
@@ -811,44 +782,12 @@ namespace System
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         private static bool StartsNumber(char c) => char.IsInRange(c, '0', '9') || c == '-' || c == '+';
 
-        public static Type GetUnderlyingType(Type enumType)
-        {
-            if (enumType == null)
-                throw new ArgumentNullException(nameof(enumType));
-
-            return enumType.GetEnumUnderlyingType();
-        }
-
-        public static Array GetValues(Type enumType)
-        {
-            if (enumType == null)
-                throw new ArgumentNullException(nameof(enumType));
-
-            return enumType.GetEnumValues();
-        }
-
         internal static ulong[] InternalGetValues(RuntimeType enumType)
         {
             // Get all of the values
             return GetCachedValuesAndNames(enumType, false).Values;
         }
 
-        public static string GetName(Type enumType, object value)
-        {
-            if (enumType == null)
-                throw new ArgumentNullException(nameof(enumType));
-
-            return enumType.GetEnumName(value);
-        }
-
-        public static string[] GetNames(Type enumType)
-        {
-            if (enumType == null)
-                throw new ArgumentNullException(nameof(enumType));
-
-            return enumType.GetEnumNames();
-        }
-
         internal static string[] InternalGetNames(RuntimeType enumType)
         {
             // Get all of the names
@@ -918,7 +857,7 @@ namespace System
 
             if (format == null)
                 throw new ArgumentNullException(nameof(format));
-            
+
             // If the value is an Enum then we need to extract the underlying value from it
             Type valueType = value.GetType();
             if (valueType.IsEnum)
@@ -933,7 +872,7 @@ namespace System
                 }
                 return ((Enum)value).ToString(format);
             }
-            
+
             // The value must be of the same type as the Underlying type of the Enum
             Type underlyingType = GetUnderlyingType(enumType);
             if (valueType != underlyingType)
@@ -1060,17 +999,9 @@ namespace System
             }
         }
 
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        private extern bool InternalHasFlag(Enum flags);
-
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        private extern CorElementType InternalGetCorElementType();
-
         #endregion
 
         #region Object Overrides
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        public extern override bool Equals(object obj);
 
         public override int GetHashCode()
         {
@@ -1207,23 +1138,9 @@ namespace System
             return ToString();
         }
 
-        [Intrinsic]
-        public bool HasFlag(Enum flag)
-        {
-            if (flag == null)
-                throw new ArgumentNullException(nameof(flag));
-
-            if (!this.GetType().IsEquivalentTo(flag.GetType()))
-            {
-                throw new ArgumentException(SR.Format(SR.Argument_EnumTypeDoesNotMatch, flag.GetType(), this.GetType()));
-            }
-
-            return InternalHasFlag(flag);
-        }
-
         #endregion
 
-        #region IConvertable
+        #region IConvertible
         public TypeCode GetTypeCode()
         {
             switch (InternalGetCorElementType())
@@ -1364,16 +1281,6 @@ namespace System
         private static object ToObject(Type enumType, bool value) =>
             InternalBoxEnum(ValidateRuntimeType(enumType), value ? 1 : 0);
 
-        private static RuntimeType ValidateRuntimeType(Type enumType)
-        {
-            if (enumType == null)
-                throw new ArgumentNullException(nameof(enumType));
-            if (!enumType.IsEnum)
-                throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType));
-            if (!(enumType is RuntimeType rtType))
-                throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType));
-            return rtType;
-        }
         #endregion
     }
 }
diff --git a/src/System.Private.CoreLib/shared/System/Reflection/CorElementType.cs b/src/System.Private.CoreLib/shared/System/Reflection/CorElementType.cs
new file mode 100644 (file)
index 0000000..37ffcfa
--- /dev/null
@@ -0,0 +1,46 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace System.Reflection
+{
+    internal enum CorElementType : byte
+    {
+        ELEMENT_TYPE_END = 0x00,
+        ELEMENT_TYPE_VOID = 0x01,
+        ELEMENT_TYPE_BOOLEAN = 0x02,
+        ELEMENT_TYPE_CHAR = 0x03,
+        ELEMENT_TYPE_I1 = 0x04,
+        ELEMENT_TYPE_U1 = 0x05,
+        ELEMENT_TYPE_I2 = 0x06,
+        ELEMENT_TYPE_U2 = 0x07,
+        ELEMENT_TYPE_I4 = 0x08,
+        ELEMENT_TYPE_U4 = 0x09,
+        ELEMENT_TYPE_I8 = 0x0A,
+        ELEMENT_TYPE_U8 = 0x0B,
+        ELEMENT_TYPE_R4 = 0x0C,
+        ELEMENT_TYPE_R8 = 0x0D,
+        ELEMENT_TYPE_STRING = 0x0E,
+        ELEMENT_TYPE_PTR = 0x0F,
+        ELEMENT_TYPE_BYREF = 0x10,
+        ELEMENT_TYPE_VALUETYPE = 0x11,
+        ELEMENT_TYPE_CLASS = 0x12,
+        ELEMENT_TYPE_VAR = 0x13,
+        ELEMENT_TYPE_ARRAY = 0x14,
+        ELEMENT_TYPE_GENERICINST = 0x15,
+        ELEMENT_TYPE_TYPEDBYREF = 0x16,
+        ELEMENT_TYPE_I = 0x18,
+        ELEMENT_TYPE_U = 0x19,
+        ELEMENT_TYPE_FNPTR = 0x1B,
+        ELEMENT_TYPE_OBJECT = 0x1C,
+        ELEMENT_TYPE_SZARRAY = 0x1D,
+        ELEMENT_TYPE_MVAR = 0x1E,
+        ELEMENT_TYPE_CMOD_REQD = 0x1F,
+        ELEMENT_TYPE_CMOD_OPT = 0x20,
+        ELEMENT_TYPE_INTERNAL = 0x21,
+        ELEMENT_TYPE_MAX = 0x22,
+        ELEMENT_TYPE_MODIFIER = 0x40,
+        ELEMENT_TYPE_SENTINEL = 0x41,
+        ELEMENT_TYPE_PINNED = 0x45,
+    }
+}
diff --git a/src/System.Private.CoreLib/src/System/Enum.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Enum.CoreCLR.cs
new file mode 100644 (file)
index 0000000..1c4e4d1
--- /dev/null
@@ -0,0 +1,113 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+namespace System
+{
+    public abstract partial class Enum
+    {
+        [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
+        private static extern void GetEnumValuesAndNames(RuntimeTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, bool getNames);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        public override extern bool Equals(object obj);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern object InternalBoxEnum(RuntimeType enumType, long value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern int InternalCompareTo(object o1, object o2);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private extern CorElementType InternalGetCorElementType();
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private extern bool InternalHasFlag(Enum flags);
+
+        private static TypeValuesAndNames GetCachedValuesAndNames(RuntimeType enumType, bool getNames)
+        {
+            TypeValuesAndNames entry = enumType.GenericCache as TypeValuesAndNames;
+
+            if (entry == null || (getNames && entry.Names == null))
+            {
+                ulong[] values = null;
+                string[] names = null;
+                GetEnumValuesAndNames(
+                    enumType.GetTypeHandleInternal(),
+                    JitHelpers.GetObjectHandleOnStack(ref values),
+                    JitHelpers.GetObjectHandleOnStack(ref names),
+                    getNames);
+                bool isFlags = enumType.IsDefined(typeof(FlagsAttribute), inherit: false);
+
+                entry = new TypeValuesAndNames(isFlags, values, names);
+                enumType.GenericCache = entry;
+            }
+
+            return entry;
+        }
+
+        [Intrinsic]
+        public bool HasFlag(Enum flag)
+        {
+            if (flag == null)
+                throw new ArgumentNullException(nameof(flag));
+
+            if (!this.GetType().IsEquivalentTo(flag.GetType()))
+            {
+                throw new ArgumentException(SR.Format(SR.Argument_EnumTypeDoesNotMatch, flag.GetType(), this.GetType()));
+            }
+
+            return InternalHasFlag(flag);
+        }
+
+        public static string GetName(Type enumType, object value)
+        {
+            if (enumType == null)
+                throw new ArgumentNullException(nameof(enumType));
+
+            return enumType.GetEnumName(value);
+        }
+
+        public static string[] GetNames(Type enumType)
+        {
+            if (enumType == null)
+                throw new ArgumentNullException(nameof(enumType));
+
+            return enumType.GetEnumNames();
+        }
+
+        public static Type GetUnderlyingType(Type enumType)
+        {
+            if (enumType == null)
+                throw new ArgumentNullException(nameof(enumType));
+
+            return enumType.GetEnumUnderlyingType();
+        }
+
+        public static Array GetValues(Type enumType)
+        {
+            if (enumType == null)
+                throw new ArgumentNullException(nameof(enumType));
+
+            return enumType.GetEnumValues();
+        }
+
+        private static RuntimeType ValidateRuntimeType(Type enumType)
+        {
+            if (enumType == null)
+                throw new ArgumentNullException(nameof(enumType));
+            if (!enumType.IsEnum)
+                throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType));
+            if (!(enumType is RuntimeType rtType))
+                throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType));
+            return rtType;
+        }
+    }
+}
index 3277bb0..e729d3c 100644 (file)
@@ -10,46 +10,6 @@ using System.Runtime.InteropServices;
 
 namespace System.Reflection
 {
-    internal enum CorElementType : byte
-    {
-        ELEMENT_TYPE_END = 0x00,
-        ELEMENT_TYPE_VOID = 0x01,
-        ELEMENT_TYPE_BOOLEAN = 0x02,
-        ELEMENT_TYPE_CHAR = 0x03,
-        ELEMENT_TYPE_I1 = 0x04,
-        ELEMENT_TYPE_U1 = 0x05,
-        ELEMENT_TYPE_I2 = 0x06,
-        ELEMENT_TYPE_U2 = 0x07,
-        ELEMENT_TYPE_I4 = 0x08,
-        ELEMENT_TYPE_U4 = 0x09,
-        ELEMENT_TYPE_I8 = 0x0A,
-        ELEMENT_TYPE_U8 = 0x0B,
-        ELEMENT_TYPE_R4 = 0x0C,
-        ELEMENT_TYPE_R8 = 0x0D,
-        ELEMENT_TYPE_STRING = 0x0E,
-        ELEMENT_TYPE_PTR = 0x0F,
-        ELEMENT_TYPE_BYREF = 0x10,
-        ELEMENT_TYPE_VALUETYPE = 0x11,
-        ELEMENT_TYPE_CLASS = 0x12,
-        ELEMENT_TYPE_VAR = 0x13,
-        ELEMENT_TYPE_ARRAY = 0x14,
-        ELEMENT_TYPE_GENERICINST = 0x15,
-        ELEMENT_TYPE_TYPEDBYREF = 0x16,
-        ELEMENT_TYPE_I = 0x18,
-        ELEMENT_TYPE_U = 0x19,
-        ELEMENT_TYPE_FNPTR = 0x1B,
-        ELEMENT_TYPE_OBJECT = 0x1C,
-        ELEMENT_TYPE_SZARRAY = 0x1D,
-        ELEMENT_TYPE_MVAR = 0x1E,
-        ELEMENT_TYPE_CMOD_REQD = 0x1F,
-        ELEMENT_TYPE_CMOD_OPT = 0x20,
-        ELEMENT_TYPE_INTERNAL = 0x21,
-        ELEMENT_TYPE_MAX = 0x22,
-        ELEMENT_TYPE_MODIFIER = 0x40,
-        ELEMENT_TYPE_SENTINEL = 0x41,
-        ELEMENT_TYPE_PINNED = 0x45,
-    }
-
     [Flags()]
     internal enum MdSigCallingConvention : byte
     {