[runtime] Replace pedump boehm dependency with sgen dependency
[mono.git] / mcs / class / corlib / System / RuntimeTypeHandle.cs
1 //
2 // System.RuntimeTypeHandle.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Runtime.Serialization;
35 using System.Runtime.InteropServices;
36 using System.Runtime.ConstrainedExecution;
37 using System.Threading;
38 using System.Runtime.CompilerServices;
39 using System.Reflection;
40 using System.Diagnostics.Contracts;
41
42 namespace System
43 {
44         [ComVisible (true)]
45         [Serializable]
46         public struct RuntimeTypeHandle : ISerializable
47         {
48                 IntPtr value;
49
50                 internal RuntimeTypeHandle (IntPtr val)
51                 {
52                         value = val;
53                 }
54
55                 internal RuntimeTypeHandle (RuntimeType type)
56                         : this (type._impl.value)
57                 {
58                 }
59
60                 RuntimeTypeHandle (SerializationInfo info, StreamingContext context)
61                 {
62                         if (info == null)
63                                 throw new ArgumentNullException ("info");
64
65                         MonoType mt = ((MonoType) info.GetValue ("TypeObj", typeof (MonoType)));
66                         value = mt.TypeHandle.Value;
67                         if (value == IntPtr.Zero)
68                                 throw new SerializationException (Locale.GetText ("Insufficient state."));
69                 }
70
71                 public IntPtr Value {
72                         get {
73                                 return value;
74                         }
75                 }
76
77                 public void GetObjectData (SerializationInfo info, StreamingContext context)
78                 {
79                         if (info == null)
80                                 throw new ArgumentNullException ("info");
81
82                         if (value == IntPtr.Zero)
83                                 throw new SerializationException ("Object fields may not be properly initialized");
84
85                         info.AddValue ("TypeObj", Type.GetTypeHandle (this), typeof (MonoType));
86                 }
87
88                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
89                 public override bool Equals (object obj)
90                 {
91                         if (obj == null || GetType () != obj.GetType ())
92                                 return false;
93
94                         return value == ((RuntimeTypeHandle)obj).Value;
95                 }
96
97                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
98                 public bool Equals (RuntimeTypeHandle handle)
99                 {
100                         return value == handle.Value;
101                 }
102
103                 public override int GetHashCode ()
104                 {
105                         return value.GetHashCode ();
106                 }
107
108                 public static bool operator == (RuntimeTypeHandle left, Object right)
109                 {
110                         return (right != null) && (right is RuntimeTypeHandle) && left.Equals ((RuntimeTypeHandle)right);
111                 }
112
113                 public static bool operator != (RuntimeTypeHandle left, Object right)
114                 {
115                         return (right == null) || !(right is RuntimeTypeHandle) || !left.Equals ((RuntimeTypeHandle)right);
116                 }
117
118                 public static bool operator == (Object left, RuntimeTypeHandle right)
119                 {
120                         return (left != null) && (left is RuntimeTypeHandle) && ((RuntimeTypeHandle)left).Equals (right);
121                 }
122
123                 public static bool operator != (Object left, RuntimeTypeHandle right)
124                 {
125                         return (left == null) || !(left is RuntimeTypeHandle) || !((RuntimeTypeHandle)left).Equals (right);
126                 }
127
128                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
129                 internal static extern TypeAttributes GetAttributes (RuntimeType type);
130
131                 [CLSCompliant (false)]
132                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
133                 public ModuleHandle GetModuleHandle ()
134                 {
135                         // Although MS' runtime is crashing here, we prefer throwing an exception.
136                         // The check is needed because Type.GetTypeFromHandle returns null
137                         // for zero handles.
138                         if (value == IntPtr.Zero)
139                                 throw new InvalidOperationException ("Object fields may not be properly initialized");
140
141                         return Type.GetTypeFromHandle (this).Module.ModuleHandle;
142                 }
143
144                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
145                 static extern int GetMetadataToken (RuntimeType type);
146
147                 internal static int GetToken (RuntimeType type)
148                 {
149                         return GetMetadataToken (type);
150                 }
151
152                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
153                 extern static Type GetGenericTypeDefinition_impl (RuntimeType type);
154
155                 internal static Type GetGenericTypeDefinition (RuntimeType type)
156                 {
157                         return GetGenericTypeDefinition_impl (type);
158                 }
159
160                 internal static bool HasElementType (RuntimeType type)
161                 {
162                         return IsArray (type) || IsByRef (type) || IsPointer (type);
163                 }
164
165                 internal static bool HasProxyAttribute (RuntimeType type)
166                 {
167                         throw new NotImplementedException ("HasProxyAttribute");
168                 }
169
170                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
171                 internal extern static bool HasInstantiation (RuntimeType type);
172
173                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
174                 internal extern static bool IsArray(RuntimeType type);
175
176                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
177                 internal extern static bool IsByRef (RuntimeType type);
178
179                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180                 internal extern static bool IsComObject (RuntimeType type);
181
182                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
183                 internal extern static bool IsInstanceOfType (RuntimeType type, Object o);              
184
185                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
186                 internal extern static bool IsPointer (RuntimeType type);
187
188                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
189                 internal extern static bool IsPrimitive (RuntimeType type);
190
191                 internal static bool IsComObject (RuntimeType type, bool isGenericCOM)
192                 {
193                         return isGenericCOM ? false : IsComObject (type);
194                 }
195
196                 internal static bool IsContextful (RuntimeType type)
197                 {
198                         return typeof (ContextBoundObject).IsAssignableFrom (type);
199                 }
200
201                 internal static bool IsEquivalentTo (RuntimeType rtType1, RuntimeType rtType2)
202                 {
203                         // refence check is done earlier and we don't recognize anything else
204                         return false;
205                 }               
206
207                 internal static bool IsSzArray(RuntimeType type)
208                 {
209                         // TODO: Better check
210                         return IsArray (type) && type.GetArrayRank () == 1;
211                 }
212
213                 internal static bool IsInterface (RuntimeType type)
214                 {
215                         return (type.Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
216                 }
217
218                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
219                 internal extern static int GetArrayRank(RuntimeType type);
220
221                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
222                 internal extern static RuntimeAssembly GetAssembly (RuntimeType type);
223
224                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
225                 internal extern static RuntimeType GetElementType (RuntimeType type);
226
227                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
228                 internal extern static RuntimeModule GetModule (RuntimeType type);
229
230                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
231                 internal extern static bool IsGenericVariable (RuntimeType type);
232
233                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
234                 internal extern static RuntimeType GetBaseType (RuntimeType type);
235
236                 internal static bool CanCastTo (RuntimeType type, RuntimeType target)
237                 {
238                         return type_is_assignable_from (target, type);
239                 }
240
241                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
242                 static extern bool type_is_assignable_from (Type a, Type b);
243
244                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
245                 internal extern static bool IsGenericTypeDefinition (RuntimeType type);
246         }
247 }