2 // typemanager.cs: C# type manager
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2003-2008 Novell, Inc.
16 using System.Globalization;
17 using System.Collections.Generic;
18 using System.Reflection;
19 using System.Reflection.Emit;
21 using System.Runtime.CompilerServices;
22 using System.Diagnostics;
25 namespace Mono.CSharp {
27 partial class TypeManager {
29 // A list of core types that the compiler requires or uses
31 static public PredefinedTypeSpec object_type;
32 static public PredefinedTypeSpec value_type;
33 static public PredefinedTypeSpec string_type;
34 static public PredefinedTypeSpec int32_type;
35 static public PredefinedTypeSpec uint32_type;
36 static public PredefinedTypeSpec int64_type;
37 static public PredefinedTypeSpec uint64_type;
38 static public PredefinedTypeSpec float_type;
39 static public PredefinedTypeSpec double_type;
40 static public PredefinedTypeSpec char_type;
41 static public PredefinedTypeSpec short_type;
42 static public PredefinedTypeSpec decimal_type;
43 static public PredefinedTypeSpec bool_type;
44 static public PredefinedTypeSpec sbyte_type;
45 static public PredefinedTypeSpec byte_type;
46 static public PredefinedTypeSpec ushort_type;
47 static public PredefinedTypeSpec enum_type;
48 static public PredefinedTypeSpec delegate_type;
49 static public PredefinedTypeSpec multicast_delegate_type;
50 static public PredefinedTypeSpec void_type;
51 static public PredefinedTypeSpec array_type;
52 static public PredefinedTypeSpec runtime_handle_type;
53 static public PredefinedTypeSpec type_type;
54 static public PredefinedTypeSpec ienumerator_type;
55 static public PredefinedTypeSpec ienumerable_type;
56 static public PredefinedTypeSpec idisposable_type;
57 static public PredefinedTypeSpec intptr_type;
58 static public PredefinedTypeSpec uintptr_type;
59 static public PredefinedTypeSpec runtime_field_handle_type;
60 static public PredefinedTypeSpec attribute_type;
61 static public PredefinedTypeSpec exception_type;
64 static public TypeSpec typed_reference_type;
65 static public TypeSpec arg_iterator_type;
66 static public TypeSpec mbr_type;
67 public static TypeSpec runtime_helpers_type;
68 static public TypeSpec iasyncresult_type;
69 static public TypeSpec asynccallback_type;
70 static public TypeSpec runtime_argument_handle_type;
71 static public TypeSpec void_ptr_type;
76 static internal TypeSpec isvolatile_type;
77 static public TypeSpec generic_ilist_type;
78 static public TypeSpec generic_icollection_type;
79 static public TypeSpec generic_ienumerator_type;
80 static public TypeSpec generic_ienumerable_type;
81 static public TypeSpec generic_nullable_type;
86 static internal TypeSpec expression_type;
87 public static TypeSpec parameter_expression_type;
88 public static TypeSpec fieldinfo_type;
89 public static TypeSpec methodinfo_type;
90 public static TypeSpec ctorinfo_type;
95 public static TypeSpec call_site_type;
96 public static TypeSpec generic_call_site_type;
97 public static TypeExpr binder_type;
98 public static TypeSpec binder_flags;
100 public static TypeExpr expression_type_expr;
104 // These methods are called by code generated by the compiler
106 static public FieldSpec string_empty;
107 static public MethodSpec system_type_get_type_from_handle;
108 static public MethodSpec bool_movenext_void;
109 static public MethodSpec void_dispose_void;
110 static public MethodSpec void_monitor_enter_object;
111 static public MethodSpec void_monitor_exit_object;
112 static public MethodSpec void_initializearray_array_fieldhandle;
113 static public MethodSpec delegate_combine_delegate_delegate;
114 static public MethodSpec delegate_remove_delegate_delegate;
115 static public PropertySpec int_get_offset_to_string_data;
116 static public MethodSpec int_interlocked_compare_exchange;
117 static public PropertySpec ienumerator_getcurrent;
118 public static MethodSpec methodbase_get_type_from_handle;
119 public static MethodSpec methodbase_get_type_from_handle_generic;
120 public static MethodSpec fieldinfo_get_field_from_handle;
121 public static MethodSpec fieldinfo_get_field_from_handle_generic;
122 public static MethodSpec activator_create_instance;
127 static public MethodSpec void_decimal_ctor_five_args;
128 static public MethodSpec void_decimal_ctor_int_arg;
129 public static MethodSpec void_decimal_ctor_long_arg;
131 static Dictionary<Assembly, bool> assembly_internals_vis_attrs;
133 static TypeManager ()
138 static public void Reset ()
140 // object_type = null;
142 assembly_internals_vis_attrs = new Dictionary<Assembly, bool> ();
144 // TODO: I am really bored by all this static stuff
145 system_type_get_type_from_handle =
148 void_monitor_enter_object =
149 void_monitor_exit_object =
150 void_initializearray_array_fieldhandle =
151 int_interlocked_compare_exchange =
152 methodbase_get_type_from_handle =
153 methodbase_get_type_from_handle_generic =
154 fieldinfo_get_field_from_handle =
155 fieldinfo_get_field_from_handle_generic =
156 activator_create_instance =
157 delegate_combine_delegate_delegate =
158 delegate_remove_delegate_delegate = null;
160 int_get_offset_to_string_data =
161 ienumerator_getcurrent = null;
163 void_decimal_ctor_five_args =
164 void_decimal_ctor_int_arg =
165 void_decimal_ctor_long_arg = null;
170 generic_call_site_type =
175 typed_reference_type = arg_iterator_type = mbr_type =
176 runtime_helpers_type = iasyncresult_type = asynccallback_type =
177 runtime_argument_handle_type = void_ptr_type = isvolatile_type =
178 generic_ilist_type = generic_icollection_type = generic_ienumerator_type =
179 generic_ienumerable_type = generic_nullable_type = expression_type =
180 parameter_expression_type = fieldinfo_type = methodinfo_type = ctorinfo_type = null;
182 expression_type_expr = null;
186 /// Returns the C# name of a type if possible, or the full type name otherwise
188 static public string CSharpName (TypeSpec t)
190 return t.GetSignatureForError ();
193 static public string CSharpName (IList<TypeSpec> types)
195 if (types.Count == 0)
198 StringBuilder sb = new StringBuilder ();
199 for (int i = 0; i < types.Count; ++i) {
203 sb.Append (CSharpName (types [i]));
205 return sb.ToString ();
208 static public string GetFullNameSignature (MemberSpec mi)
210 return mi.GetSignatureForError ();
213 static public string CSharpSignature (MemberSpec mb)
215 return mb.GetSignatureForError ();
219 // Looks up a type, and aborts if it is not found. This is used
220 // by predefined types required by the compiler
222 public static TypeSpec CoreLookupType (CompilerContext ctx, string ns_name, string name, MemberKind kind, bool required)
224 return CoreLookupType (ctx, ns_name, name, 0, kind, required);
227 public static TypeSpec CoreLookupType (CompilerContext ctx, string ns_name, string name, int arity, MemberKind kind, bool required)
229 Namespace ns = GlobalRootNamespace.Instance.GetNamespace (ns_name, true);
230 var te = ns.LookupType (ctx, name, arity, !required, Location.Null);
231 var ts = te == null ? null : te.Type;
237 ctx.Report.Error (518, "The predefined type `{0}.{1}' is not defined or imported",
242 if (ts.Kind != kind) {
243 ctx.Report.Error (520, "The predefined type `{0}.{1}' is not declared correctly",
251 static MemberSpec GetPredefinedMember (TypeSpec t, MemberFilter filter, Location loc)
253 var member = MemberCache.FindMember (t, filter, BindingRestriction.DeclaredOnly);
255 if (member != null && member.IsAccessible (InternalType.FakeInternalType))
258 string method_args = null;
259 if (filter.Parameters != null)
260 method_args = filter.Parameters.GetSignatureForError ();
262 RootContext.ToplevelTypes.Compiler.Report.Error (656, loc, "The compiler required member `{0}.{1}{2}' could not be found or is inaccessible",
263 TypeManager.CSharpName (t), filter.Name, method_args);
269 // Returns the ConstructorInfo for "args"
271 public static MethodSpec GetPredefinedConstructor (TypeSpec t, Location loc, params TypeSpec [] args)
273 var pc = ParametersCompiled.CreateFullyResolved (args);
274 return GetPredefinedMember (t, MemberFilter.Constructor (pc), loc) as MethodSpec;
278 // Returns the method specification for a method named `name' defined
279 // in type `t' which takes arguments of types `args'
281 public static MethodSpec GetPredefinedMethod (TypeSpec t, string name, Location loc, params TypeSpec [] args)
283 var pc = ParametersCompiled.CreateFullyResolved (args);
284 return GetPredefinedMethod (t, MemberFilter.Method (name, 0, pc, null), loc);
287 public static MethodSpec GetPredefinedMethod (TypeSpec t, MemberFilter filter, Location loc)
289 return GetPredefinedMember (t, filter, loc) as MethodSpec;
292 public static FieldSpec GetPredefinedField (TypeSpec t, string name, Location loc, TypeSpec type)
294 return GetPredefinedMember (t, MemberFilter.Field (name, type), loc) as FieldSpec;
297 public static PropertySpec GetPredefinedProperty (TypeSpec t, string name, Location loc, TypeSpec type)
299 return GetPredefinedMember (t, MemberFilter.Property (name, type), loc) as PropertySpec;
302 public static IList<PredefinedTypeSpec> InitCoreTypes ()
304 var core_types = new PredefinedTypeSpec[] {
305 object_type = new PredefinedTypeSpec (MemberKind.Class, "System", "Object"),
306 value_type = new PredefinedTypeSpec (MemberKind.Class, "System", "ValueType"),
307 attribute_type = new PredefinedTypeSpec (MemberKind.Class, "System", "Attribute"),
309 int32_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Int32"),
310 int64_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Int64"),
311 uint32_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "UInt32"),
312 uint64_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "UInt64"),
313 byte_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Byte"),
314 sbyte_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "SByte"),
315 short_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Int16"),
316 ushort_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "UInt16"),
318 ienumerator_type = new PredefinedTypeSpec (MemberKind.Interface, "System.Collections", "IEnumerator"),
319 ienumerable_type = new PredefinedTypeSpec (MemberKind.Interface, "System.Collections", "IEnumerable"),
320 idisposable_type = new PredefinedTypeSpec (MemberKind.Interface, "System", "IDisposable"),
322 char_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Char"),
323 string_type = new PredefinedTypeSpec (MemberKind.Class, "System", "String"),
324 float_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Single"),
325 double_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Double"),
326 decimal_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Decimal"),
327 bool_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Boolean"),
328 intptr_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "IntPtr"),
329 uintptr_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "UIntPtr"),
331 multicast_delegate_type = new PredefinedTypeSpec (MemberKind.Class, "System", "MulticastDelegate"),
332 delegate_type = new PredefinedTypeSpec (MemberKind.Class, "System", "Delegate"),
333 enum_type = new PredefinedTypeSpec (MemberKind.Class, "System", "Enum"),
334 array_type = new PredefinedTypeSpec (MemberKind.Class, "System", "Array"),
335 void_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "Void"),
336 type_type = new PredefinedTypeSpec (MemberKind.Class, "System", "Type"),
337 exception_type = new PredefinedTypeSpec (MemberKind.Class, "System", "Exception"),
338 runtime_field_handle_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "RuntimeFieldHandle"),
339 runtime_handle_type = new PredefinedTypeSpec (MemberKind.Struct, "System", "RuntimeTypeHandle"),
346 /// The types have to be initialized after the initial
347 /// population of the type has happened (for example, to
348 /// bootstrap the corlib.dll
350 public static bool InitCoreTypes (CompilerContext ctx, IList<PredefinedTypeSpec> predefined)
352 foreach (var p in predefined) {
353 var found = CoreLookupType (ctx, p.Namespace, p.Name, p.Kind, true);
354 if (found == null || found == p)
357 if (!RootContext.StdLib) {
358 var ns = GlobalRootNamespace.Instance.GetNamespace (p.Namespace, false);
359 ns.ReplaceTypeWithPredefined (found, p);
361 var tc = found.MemberDefinition as TypeContainer;
362 tc.SetPredefinedSpec (p);
363 p.SetDefinition (found);
367 PredefinedAttributes.Get.ParamArray.Initialize (ctx, false);
368 PredefinedAttributes.Get.Out.Initialize (ctx, false);
370 return ctx.Report.Errors == 0;
374 // Initializes optional core types
376 public static void InitOptionalCoreTypes (CompilerContext ctx)
378 void_ptr_type = PointerContainer.MakeType (void_type);
381 // Initialize InternalsVisibleTo as the very first optional type. Otherwise we would populate
382 // types cache with incorrect accessiblity when any of optional types is internal.
384 PredefinedAttributes.Get.Initialize (ctx);
386 runtime_argument_handle_type = CoreLookupType (ctx, "System", "RuntimeArgumentHandle", MemberKind.Struct, false);
387 asynccallback_type = CoreLookupType (ctx, "System", "AsyncCallback", MemberKind.Delegate, false);
388 iasyncresult_type = CoreLookupType (ctx, "System", "IAsyncResult", MemberKind.Interface, false);
389 typed_reference_type = CoreLookupType (ctx, "System", "TypedReference", MemberKind.Struct, false);
390 arg_iterator_type = CoreLookupType (ctx, "System", "ArgIterator", MemberKind.Struct, false);
391 mbr_type = CoreLookupType (ctx, "System", "MarshalByRefObject", MemberKind.Class, false);
393 generic_ienumerator_type = CoreLookupType (ctx, "System.Collections.Generic", "IEnumerator", 1, MemberKind.Interface, false);
394 generic_ilist_type = CoreLookupType (ctx, "System.Collections.Generic", "IList", 1, MemberKind.Interface, false);
395 generic_icollection_type = CoreLookupType (ctx, "System.Collections.Generic", "ICollection", 1, MemberKind.Interface, false);
396 generic_ienumerable_type = CoreLookupType (ctx, "System.Collections.Generic", "IEnumerable", 1, MemberKind.Interface, false);
397 generic_nullable_type = CoreLookupType (ctx, "System", "Nullable", 1, MemberKind.Struct, false);
400 // Optional types which are used as types and for member lookup
402 runtime_helpers_type = CoreLookupType (ctx, "System.Runtime.CompilerServices", "RuntimeHelpers", MemberKind.Class, false);
405 // Note: extension_attribute_type is already loaded
406 expression_type = CoreLookupType (ctx, "System.Linq.Expressions", "Expression", 1, MemberKind.Class, false);
409 public static bool IsBuiltinType (TypeSpec t)
411 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
412 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
413 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
414 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
421 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
422 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
424 public static bool IsPrimitiveType (TypeSpec t)
426 return (t == int32_type || t == uint32_type ||
427 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
428 t == char_type || t == short_type || t == bool_type ||
429 t == sbyte_type || t == byte_type || t == ushort_type);
433 public static bool IsDelegateType (TypeSpec t)
439 // When any element of the type is a dynamic type
441 // This method builds a transformation array for dynamic types
442 // used in places where DynamicAttribute cannot be applied to.
443 // It uses bool flag when type is of dynamic type and each
444 // section always starts with "false" for some reason.
446 // LAMESPEC: This should be part of C# specification !
448 // Example: Func<dynamic, int, dynamic[]>
449 // Transformation: { false, true, false, false, true }
451 public static bool[] HasDynamicTypeUsed (TypeSpec t)
453 var ac = t as ArrayContainer;
455 if (HasDynamicTypeUsed (ac.Element) != null)
456 return new bool[] { false, true };
464 if (IsGenericType (t)) {
465 List<bool> transform = null;
466 var targs = GetTypeArguments (t);
467 for (int i = 0; i < targs.Length; ++i) {
468 var element = HasDynamicTypeUsed (targs [i]);
469 if (element != null) {
470 if (transform == null) {
471 transform = new List<bool> ();
472 for (int ii = 0; ii <= i; ++ii)
473 transform.Add (false);
476 transform.AddRange (element);
477 } else if (transform != null) {
478 transform.Add (false);
482 if (transform != null)
483 return transform.ToArray ();
486 if (object.ReferenceEquals (InternalType.Dynamic, t))
487 return new bool [] { true };
493 public static bool IsEnumType (TypeSpec t)
498 public static bool IsBuiltinOrEnum (TypeSpec t)
500 if (IsBuiltinType (t))
510 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
512 public static bool IsUnmanagedType (TypeSpec t)
514 var ds = t.MemberDefinition as DeclSpace;
516 return ds.IsUnmanagedType ();
518 // some builtins that are not unmanaged types
519 if (t == TypeManager.object_type || t == TypeManager.string_type)
522 if (IsBuiltinOrEnum (t))
525 // Someone did the work of checking if the ElementType of t is unmanaged. Let's not repeat it.
527 return IsUnmanagedType (GetElementType (t));
529 if (!IsValueType (t))
532 if (t.IsNested && t.DeclaringType.IsGenericOrParentIsGeneric)
539 // Null is considered to be a reference type
541 public static bool IsReferenceType (TypeSpec t)
543 if (t.IsGenericParameter)
544 return ((TypeParameterSpec) t).IsReferenceType;
546 return !t.IsStruct && !IsEnumType (t);
549 public static bool IsValueType (TypeSpec t)
551 if (t.IsGenericParameter)
552 return ((TypeParameterSpec) t).IsValueType;
554 return t.IsStruct || IsEnumType (t);
557 public static bool IsStruct (TypeSpec t)
562 public static bool IsFamilyAccessible (TypeSpec type, TypeSpec parent)
564 // TypeParameter tparam = LookupTypeParameter (type);
565 // TypeParameter pparam = LookupTypeParameter (parent);
567 if (type.Kind == MemberKind.TypeParameter && parent.Kind == MemberKind.TypeParameter) { // (tparam != null) && (pparam != null)) {
571 throw new NotImplementedException ("net");
572 // return tparam.IsSubclassOf (parent);
576 if (IsInstantiationOfSameGenericType (type, parent))
579 type = type.BaseType;
580 } while (type != null);
586 // Checks whether `type' is a subclass or nested child of `base_type'.
588 public static bool IsNestedFamilyAccessible (TypeSpec type, TypeSpec base_type)
591 if (IsFamilyAccessible (type, base_type))
594 // Handle nested types.
595 type = type.DeclaringType;
596 } while (type != null);
602 // Checks whether `type' is a nested child of `parent'.
604 public static bool IsNestedChildOf (TypeSpec type, TypeSpec parent)
609 type = type.GetDefinition (); // DropGenericTypeArguments (type);
610 parent = parent.GetDefinition (); // DropGenericTypeArguments (parent);
615 type = type.DeclaringType;
616 while (type != null) {
617 if (type.GetDefinition () == parent)
620 type = type.DeclaringType;
626 public static bool IsSpecialType (TypeSpec t)
628 return t == arg_iterator_type || t == typed_reference_type;
632 // Checks whether `invocationAssembly' is same or a friend of the assembly
634 public static bool IsThisOrFriendAssembly (Assembly invocationAssembly, Assembly assembly)
636 if (assembly == null)
637 throw new ArgumentNullException ("assembly");
639 // TODO: This can happen for constants used at assembly level and
640 // predefined members
641 // But there is no way to test for it for now, so it could be abused
643 if (invocationAssembly == null)
644 invocationAssembly = CodeGen.Assembly.Builder;
646 if (invocationAssembly == assembly)
650 if (assembly_internals_vis_attrs.TryGetValue (assembly, out value))
653 object[] attrs = assembly.GetCustomAttributes (typeof (InternalsVisibleToAttribute), false);
654 if (attrs.Length == 0) {
655 assembly_internals_vis_attrs.Add (assembly, false);
659 bool is_friend = false;
661 AssemblyName this_name = CodeGen.Assembly.Name;
662 if (this_name == null)
665 byte [] this_token = this_name.GetPublicKeyToken ();
666 foreach (InternalsVisibleToAttribute attr in attrs) {
667 if (attr.AssemblyName == null || attr.AssemblyName.Length == 0)
670 AssemblyName aname = null;
672 aname = new AssemblyName (attr.AssemblyName);
673 } catch (FileLoadException) {
674 } catch (ArgumentException) {
677 if (aname == null || aname.Name != this_name.Name)
680 byte [] key_token = aname.GetPublicKeyToken ();
681 if (key_token != null) {
682 if (this_token.Length == 0) {
683 // Same name, but assembly is not strongnamed
684 Error_FriendAccessNameNotMatching (aname.FullName, RootContext.ToplevelTypes.Compiler.Report);
688 if (!CompareKeyTokens (this_token, key_token))
696 assembly_internals_vis_attrs.Add (assembly, is_friend);
700 static bool CompareKeyTokens (byte [] token1, byte [] token2)
702 for (int i = 0; i < token1.Length; i++)
703 if (token1 [i] != token2 [i])
709 static void Error_FriendAccessNameNotMatching (string other_name, Report Report)
712 "Friend access was granted to `{0}', but the output assembly is named `{1}'. Try adding a reference to `{0}' or change the output assembly name to match it",
713 other_name, CodeGen.Assembly.Name.FullName);
716 public static TypeSpec GetElementType (TypeSpec t)
718 return ((ElementTypeSpec)t).Element;
722 /// This method is not implemented by MS runtime for dynamic types
724 public static bool HasElementType (TypeSpec t)
726 return t is ElementTypeSpec;
729 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
731 // This is a custom version of Convert.ChangeType() which works
732 // with the TypeBuilder defined types when compiling corlib.
733 public static object ChangeType (object value, TypeSpec targetType, out bool error)
735 IConvertible convert_value = value as IConvertible;
737 if (convert_value == null){
743 // We cannot rely on build-in type conversions as they are
744 // more limited than what C# supports.
745 // See char -> float/decimal/double conversion
749 if (targetType == TypeManager.bool_type)
750 return convert_value.ToBoolean (nf_provider);
751 if (targetType == TypeManager.byte_type)
752 return convert_value.ToByte (nf_provider);
753 if (targetType == TypeManager.char_type)
754 return convert_value.ToChar (nf_provider);
755 if (targetType == TypeManager.short_type)
756 return convert_value.ToInt16 (nf_provider);
757 if (targetType == TypeManager.int32_type)
758 return convert_value.ToInt32 (nf_provider);
759 if (targetType == TypeManager.int64_type)
760 return convert_value.ToInt64 (nf_provider);
761 if (targetType == TypeManager.sbyte_type)
762 return convert_value.ToSByte (nf_provider);
764 if (targetType == TypeManager.decimal_type) {
765 if (convert_value.GetType () == typeof (char))
766 return (decimal) convert_value.ToInt32 (nf_provider);
767 return convert_value.ToDecimal (nf_provider);
770 if (targetType == TypeManager.double_type) {
771 if (convert_value.GetType () == typeof (char))
772 return (double) convert_value.ToInt32 (nf_provider);
773 return convert_value.ToDouble (nf_provider);
776 if (targetType == TypeManager.float_type) {
777 if (convert_value.GetType () == typeof (char))
778 return (float)convert_value.ToInt32 (nf_provider);
779 return convert_value.ToSingle (nf_provider);
782 if (targetType == TypeManager.string_type)
783 return convert_value.ToString (nf_provider);
784 if (targetType == TypeManager.ushort_type)
785 return convert_value.ToUInt16 (nf_provider);
786 if (targetType == TypeManager.uint32_type)
787 return convert_value.ToUInt32 (nf_provider);
788 if (targetType == TypeManager.uint64_type)
789 return convert_value.ToUInt64 (nf_provider);
790 if (targetType == TypeManager.object_type)
801 /// Utility function that can be used to probe whether a type
802 /// is managed or not.
804 public static bool VerifyUnmanaged (CompilerContext ctx, TypeSpec t, Location loc)
807 t = GetElementType (t);
809 if (IsUnmanagedType (t))
812 ctx.Report.SymbolRelatedToPreviousError (t);
813 ctx.Report.Error (208, loc,
814 "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
820 // This method always return false for non-generic compiler,
821 // while Type.IsGenericParameter is returned if it is supported.
822 public static bool IsGenericParameter (TypeSpec type)
824 return type.IsGenericParameter;
827 public static bool IsGenericType (TypeSpec type)
829 return type.IsGeneric;
832 // TODO: Implement correctly
833 public static bool ContainsGenericParameters (TypeSpec type)
835 return type.GetMetaInfo ().ContainsGenericParameters;
838 public static TypeSpec[] GetTypeArguments (TypeSpec t)
840 // TODO: return empty array !!
841 return t.TypeArguments;
845 /// Check whether `type' and `parent' are both instantiations of the same
846 /// generic type. Note that we do not check the type parameters here.
848 public static bool IsInstantiationOfSameGenericType (TypeSpec type, TypeSpec parent)
850 return type == parent || type.MemberDefinition == parent.MemberDefinition;
853 public static bool IsNullableType (TypeSpec t)
855 return generic_nullable_type == t.GetDefinition ();