Use correct location for unexpected type parameter
[mono.git] / mcs / mcs / typemanager.cs
1 //
2 // typemanager.cs: C# type manager
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //         Ravi Pratap     (ravi@ximian.com)
6 //         Marek Safar     (marek.safar@seznam.cz)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 //
10 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2003-2008 Novell, Inc.
12 //
13
14 using System;
15 using System.IO;
16 using System.Globalization;
17 using System.Collections.Generic;
18 using System.Reflection;
19 using System.Reflection.Emit;
20 using System.Text;
21 using System.Runtime.CompilerServices;
22 using System.Diagnostics;
23 using System.Linq;
24
25 namespace Mono.CSharp {
26
27         partial class TypeManager {
28         //
29         // A list of core types that the compiler requires or uses
30         //
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;
62
63
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;
72
73         // 
74         // C# 2.0
75         //
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;
82
83         //
84         // C# 3.0
85         //
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;
91
92         //
93         // C# 4.0
94         //
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;
99
100         public static TypeExpr expression_type_expr;
101
102
103         //
104         // These methods are called by code generated by the compiler
105         //
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;
123
124         //
125         // The constructors.
126         //
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;
130
131         static Dictionary<Assembly, bool> assembly_internals_vis_attrs;
132
133         static TypeManager ()
134         {
135                 Reset ();
136         }
137
138         static public void Reset ()
139         {
140 //              object_type = null;
141         
142                 assembly_internals_vis_attrs = new Dictionary<Assembly, bool> ();
143                 
144                 // TODO: I am really bored by all this static stuff
145                 system_type_get_type_from_handle =
146                 bool_movenext_void =
147                 void_dispose_void =
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;
159
160                 int_get_offset_to_string_data =
161                 ienumerator_getcurrent = null;
162
163                 void_decimal_ctor_five_args =
164                 void_decimal_ctor_int_arg =
165                 void_decimal_ctor_long_arg = null;
166
167                 string_empty = null;
168
169                 call_site_type =
170                 generic_call_site_type =
171                 binder_flags = null;
172
173                 binder_type = null;
174
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;
181
182                 expression_type_expr = null;
183         }
184
185         /// <summary>
186         ///   Returns the C# name of a type if possible, or the full type name otherwise
187         /// </summary>
188         static public string CSharpName (TypeSpec t)
189         {
190                 return t.GetSignatureForError ();
191         }
192
193         static public string CSharpName (IList<TypeSpec> types)
194         {
195                 if (types.Count == 0)
196                         return string.Empty;
197
198                 StringBuilder sb = new StringBuilder ();
199                 for (int i = 0; i < types.Count; ++i) {
200                         if (i > 0)
201                                 sb.Append (",");
202
203                         sb.Append (CSharpName (types [i]));
204                 }
205                 return sb.ToString ();
206         }
207
208         static public string GetFullNameSignature (MemberSpec mi)
209         {
210                 return mi.GetSignatureForError ();
211         }
212
213         static public string CSharpSignature (MemberSpec mb)
214         {
215                 return mb.GetSignatureForError ();
216         }
217
218         //
219         // Looks up a type, and aborts if it is not found.  This is used
220         // by predefined types required by the compiler
221         //
222         public static TypeSpec CoreLookupType (CompilerContext ctx, string ns_name, string name, MemberKind kind, bool required)
223         {
224                 return CoreLookupType (ctx, ns_name, name, 0, kind, required);
225         }
226
227         public static TypeSpec CoreLookupType (CompilerContext ctx, string ns_name, string name, int arity, MemberKind kind, bool required)
228         {
229                 Namespace ns = ctx.GlobalRootNamespace.GetNamespace (ns_name, true);
230                 var te = ns.LookupType (ctx, name, arity, !required, Location.Null);
231                 var ts = te == null ? null : te.Type;
232
233                 if (!required)
234                         return ts;
235
236                 if (ts == null) {
237                         ctx.Report.Error (518, "The predefined type `{0}.{1}' is not defined or imported",
238                                 ns_name, name);
239                         return null;
240                 }
241
242                 if (ts.Kind != kind) {
243                         ctx.Report.Error (520, "The predefined type `{0}.{1}' is not declared correctly",
244                                 ns_name, name);
245                         return null;
246                 }
247
248                 return ts;
249         }
250
251         static MemberSpec GetPredefinedMember (TypeSpec t, MemberFilter filter, Location loc)
252         {
253                 var member = MemberCache.FindMember (t, filter, BindingRestriction.DeclaredOnly);
254
255                 if (member != null && member.IsAccessible (InternalType.FakeInternalType))
256                         return member;
257
258                 string method_args = null;
259                 if (filter.Parameters != null)
260                         method_args = filter.Parameters.GetSignatureForError ();
261
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);
264
265                 return null;
266         }
267
268         //
269         // Returns the ConstructorInfo for "args"
270         //
271         public static MethodSpec GetPredefinedConstructor (TypeSpec t, Location loc, params TypeSpec [] args)
272         {
273                 var pc = ParametersCompiled.CreateFullyResolved (args);
274                 return GetPredefinedMember (t, MemberFilter.Constructor (pc), loc) as MethodSpec;
275         }
276
277         //
278         // Returns the method specification for a method named `name' defined
279         // in type `t' which takes arguments of types `args'
280         //
281         public static MethodSpec GetPredefinedMethod (TypeSpec t, string name, Location loc, params TypeSpec [] args)
282         {
283                 var pc = ParametersCompiled.CreateFullyResolved (args);
284                 return GetPredefinedMethod (t, MemberFilter.Method (name, 0, pc, null), loc);
285         }
286
287         public static MethodSpec GetPredefinedMethod (TypeSpec t, MemberFilter filter, Location loc)
288         {
289                 return GetPredefinedMember (t, filter, loc) as MethodSpec;
290         }
291
292         public static FieldSpec GetPredefinedField (TypeSpec t, string name, Location loc, TypeSpec type)
293         {
294                 return GetPredefinedMember (t, MemberFilter.Field (name, type), loc) as FieldSpec;
295         }
296
297         public static PropertySpec GetPredefinedProperty (TypeSpec t, string name, Location loc, TypeSpec type)
298         {
299                 return GetPredefinedMember (t, MemberFilter.Property (name, type), loc) as PropertySpec;
300         }
301
302         public static IList<PredefinedTypeSpec> InitCoreTypes ()
303         {
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"),
308
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"),
317
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"),
321
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"),
330
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"),
340                 };
341
342                 return core_types;
343         }
344
345         /// <remarks>
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
349         /// </remarks>
350         public static bool InitCoreTypes (CompilerContext ctx, IList<PredefinedTypeSpec> predefined)
351         {
352                 foreach (var p in predefined) {
353                         var found = CoreLookupType (ctx, p.Namespace, p.Name, p.Kind, true);
354                         if (found == null || found == p)
355                                 continue;
356
357                         if (!RootContext.StdLib) {
358                                 var ns = ctx.GlobalRootNamespace.GetNamespace (p.Namespace, false);
359                                 ns.ReplaceTypeWithPredefined (found, p);
360
361                                 var tc = found.MemberDefinition as TypeContainer;
362                                 tc.SetPredefinedSpec (p);
363                                 p.SetDefinition (found);
364                         }
365                 }
366
367                 ctx.PredefinedAttributes.ParamArray.Initialize (ctx, false);
368                 ctx.PredefinedAttributes.Out.Initialize (ctx, false);
369
370                 if (InternalType.Dynamic.GetMetaInfo () == null) {
371                         InternalType.Dynamic.SetMetaInfo (object_type.GetMetaInfo ());
372
373                         if (object_type.MemberDefinition.IsImported)
374                                 InternalType.Dynamic.MemberCache = object_type.MemberCache;
375
376                         InternalType.Null.SetMetaInfo (object_type.GetMetaInfo ());
377                 }
378
379                 return ctx.Report.Errors == 0;
380         }
381
382         //
383         // Initializes optional core types
384         //
385         public static void InitOptionalCoreTypes (CompilerContext ctx)
386         {
387                 void_ptr_type = PointerContainer.MakeType (void_type);
388
389                 //
390                 // Initialize InternalsVisibleTo as the very first optional type. Otherwise we would populate
391                 // types cache with incorrect accessiblity when any of optional types is internal.
392                 //
393                 ctx.PredefinedAttributes.Initialize (ctx);
394
395                 runtime_argument_handle_type = CoreLookupType (ctx, "System", "RuntimeArgumentHandle", MemberKind.Struct, false);
396                 asynccallback_type = CoreLookupType (ctx, "System", "AsyncCallback", MemberKind.Delegate, false);
397                 iasyncresult_type = CoreLookupType (ctx, "System", "IAsyncResult", MemberKind.Interface, false);
398                 typed_reference_type = CoreLookupType (ctx, "System", "TypedReference", MemberKind.Struct, false);
399                 arg_iterator_type = CoreLookupType (ctx, "System", "ArgIterator", MemberKind.Struct, false);
400                 mbr_type = CoreLookupType (ctx, "System", "MarshalByRefObject", MemberKind.Class, false);
401
402                 generic_ienumerator_type = CoreLookupType (ctx, "System.Collections.Generic", "IEnumerator", 1, MemberKind.Interface, false);
403                 generic_ilist_type = CoreLookupType (ctx, "System.Collections.Generic", "IList", 1, MemberKind.Interface, false);
404                 generic_icollection_type = CoreLookupType (ctx, "System.Collections.Generic", "ICollection", 1, MemberKind.Interface, false);
405                 generic_ienumerable_type = CoreLookupType (ctx, "System.Collections.Generic", "IEnumerable", 1, MemberKind.Interface, false);
406                 generic_nullable_type = CoreLookupType (ctx, "System", "Nullable", 1, MemberKind.Struct, false);
407
408                 //
409                 // Optional types which are used as types and for member lookup
410                 //
411                 runtime_helpers_type = CoreLookupType (ctx, "System.Runtime.CompilerServices", "RuntimeHelpers", MemberKind.Class, false);
412
413                 // New in .NET 3.5
414                 // Note: extension_attribute_type is already loaded
415                 expression_type = CoreLookupType (ctx, "System.Linq.Expressions", "Expression", 1, MemberKind.Class, false);
416         }
417
418         public static bool IsBuiltinType (TypeSpec t)
419         {
420                 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
421                     t == int64_type || t == uint64_type || t == float_type || t == double_type ||
422                     t == char_type || t == short_type || t == decimal_type || t == bool_type ||
423                     t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
424                         return true;
425                 else
426                         return false;
427         }
428
429         //
430         // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
431         // the pieces in the code where we use IsBuiltinType and special case decimal_type.
432         // 
433         public static bool IsPrimitiveType (TypeSpec t)
434         {
435                 return (t == int32_type || t == uint32_type ||
436                     t == int64_type || t == uint64_type || t == float_type || t == double_type ||
437                     t == char_type || t == short_type || t == bool_type ||
438                     t == sbyte_type || t == byte_type || t == ushort_type);
439         }
440
441         // Obsolete
442         public static bool IsDelegateType (TypeSpec t)
443         {
444                 return t.IsDelegate;
445         }
446         
447         // Obsolete
448         public static bool IsEnumType (TypeSpec t)
449         {
450                 return t.IsEnum;
451         }
452
453         public static bool IsBuiltinOrEnum (TypeSpec t)
454         {
455                 if (IsBuiltinType (t))
456                         return true;
457                 
458                 if (IsEnumType (t))
459                         return true;
460
461                 return false;
462         }
463
464         //
465         // Whether a type is unmanaged.  This is used by the unsafe code (25.2)
466         //
467         public static bool IsUnmanagedType (TypeSpec t)
468         {
469                 var ds = t.MemberDefinition as DeclSpace;
470                 if (ds != null)
471                         return ds.IsUnmanagedType ();
472
473                 // some builtins that are not unmanaged types
474                 if (t == TypeManager.object_type || t == TypeManager.string_type)
475                         return false;
476
477                 if (IsBuiltinOrEnum (t))
478                         return true;
479
480                 // Someone did the work of checking if the ElementType of t is unmanaged.  Let's not repeat it.
481                 if (t.IsPointer)
482                         return IsUnmanagedType (GetElementType (t));
483
484                 if (!IsValueType (t))
485                         return false;
486
487                 if (t.IsNested && t.DeclaringType.IsGenericOrParentIsGeneric)
488                         return false;
489
490                 return true;
491         }
492
493         //
494         // Null is considered to be a reference type
495         //                      
496         public static bool IsReferenceType (TypeSpec t)
497         {
498                 if (t.IsGenericParameter)
499                         return ((TypeParameterSpec) t).IsReferenceType;
500
501                 return !t.IsStruct && !IsEnumType (t);
502         }                       
503                 
504         public static bool IsValueType (TypeSpec t)
505         {
506                 if (t.IsGenericParameter)
507                         return ((TypeParameterSpec) t).IsValueType;
508
509                 return t.IsStruct || IsEnumType (t);
510         }
511
512         public static bool IsStruct (TypeSpec t)
513         {
514                 return t.IsStruct;
515         }
516
517         public static bool IsFamilyAccessible (TypeSpec type, TypeSpec parent)
518         {
519 //              TypeParameter tparam = LookupTypeParameter (type);
520 //              TypeParameter pparam = LookupTypeParameter (parent);
521
522                 if (type.Kind == MemberKind.TypeParameter && parent.Kind == MemberKind.TypeParameter) { // (tparam != null) && (pparam != null)) {
523                         if (type == parent)
524                                 return true;
525
526                         throw new NotImplementedException ("net");
527 //                      return tparam.IsSubclassOf (parent);
528                 }
529
530                 do {
531                         if (IsInstantiationOfSameGenericType (type, parent))
532                                 return true;
533
534                         type = type.BaseType;
535                 } while (type != null);
536
537                 return false;
538         }
539
540         //
541         // Checks whether `type' is a subclass or nested child of `base_type'.
542         //
543         public static bool IsNestedFamilyAccessible (TypeSpec type, TypeSpec base_type)
544         {
545                 do {
546                         if (IsFamilyAccessible (type, base_type))
547                                 return true;
548
549                         // Handle nested types.
550                         type = type.DeclaringType;
551                 } while (type != null);
552
553                 return false;
554         }
555
556         //
557         // Checks whether `type' is a nested child of `parent'.
558         //
559         public static bool IsNestedChildOf (TypeSpec type, TypeSpec parent)
560         {
561                 if (type == null)
562                         return false;
563
564                 type = type.GetDefinition (); // DropGenericTypeArguments (type);
565                 parent = parent.GetDefinition (); // DropGenericTypeArguments (parent);
566
567                 if (type == parent)
568                         return false;
569
570                 type = type.DeclaringType;
571                 while (type != null) {
572                         if (type.GetDefinition () == parent)
573                                 return true;
574
575                         type = type.DeclaringType;
576                 }
577
578                 return false;
579         }
580
581         public static bool IsSpecialType (TypeSpec t)
582         {
583                 return t == arg_iterator_type || t == typed_reference_type;
584         }
585
586         //
587         // Checks whether `invocationAssembly' is same or a friend of the assembly
588         //
589         public static bool IsThisOrFriendAssembly (Assembly invocationAssembly, Assembly assembly)
590         {
591                 if (assembly == null)
592                         throw new ArgumentNullException ("assembly");
593
594                 // TODO: This can happen for constants used at assembly level and
595                 // predefined members
596                 // But there is no way to test for it for now, so it could be abused
597                 // elsewhere too.
598                 if (invocationAssembly == null)
599                         invocationAssembly = CodeGen.Assembly.Builder;
600
601                 if (invocationAssembly == assembly)
602                         return true;
603
604                 bool value;
605                 if (assembly_internals_vis_attrs.TryGetValue (assembly, out value))
606                         return value;
607
608                 object[] attrs = assembly.GetCustomAttributes (typeof (InternalsVisibleToAttribute), false);
609                 if (attrs.Length == 0) {
610                         assembly_internals_vis_attrs.Add (assembly, false);
611                         return false;
612                 }
613
614                 bool is_friend = false;
615
616                 AssemblyName this_name = CodeGen.Assembly.Name;
617                 if (this_name == null)
618                         return false;
619
620                 byte [] this_token = this_name.GetPublicKeyToken ();
621                 foreach (InternalsVisibleToAttribute attr in attrs) {
622                         if (attr.AssemblyName == null || attr.AssemblyName.Length == 0)
623                                 continue;
624                         
625                         AssemblyName aname = null;
626                         try {
627                                 aname = new AssemblyName (attr.AssemblyName);
628                         } catch (FileLoadException) {
629                         } catch (ArgumentException) {
630                         }
631
632                         if (aname == null || aname.Name != this_name.Name)
633                                 continue;
634                         
635                         byte [] key_token = aname.GetPublicKeyToken ();
636                         if (key_token != null) {
637                                 if (this_token.Length == 0) {
638                                         // Same name, but assembly is not strongnamed
639                                         Error_FriendAccessNameNotMatching (aname.FullName, RootContext.ToplevelTypes.Compiler.Report);
640                                         break;
641                                 }
642                                 
643                                 if (!CompareKeyTokens (this_token, key_token))
644                                         continue;
645                         }
646
647                         is_friend = true;
648                         break;
649                 }
650
651                 assembly_internals_vis_attrs.Add (assembly, is_friend);
652                 return is_friend;
653         }
654
655         static bool CompareKeyTokens (byte [] token1, byte [] token2)
656         {
657                 for (int i = 0; i < token1.Length; i++)
658                         if (token1 [i] != token2 [i])
659                                 return false;
660
661                 return true;
662         }
663
664         static void Error_FriendAccessNameNotMatching (string other_name, Report Report)
665         {
666                 Report.Error (281,
667                         "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",
668                         other_name, CodeGen.Assembly.Name.FullName);
669         }
670
671         public static TypeSpec GetElementType (TypeSpec t)
672         {
673                 return ((ElementTypeSpec)t).Element;
674         }
675
676         /// <summary>
677         /// This method is not implemented by MS runtime for dynamic types
678         /// </summary>
679         public static bool HasElementType (TypeSpec t)
680         {
681                 return t is ElementTypeSpec;
682         }
683
684         static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
685
686         // This is a custom version of Convert.ChangeType() which works
687         // with the TypeBuilder defined types when compiling corlib.
688         public static object ChangeType (object value, TypeSpec targetType, out bool error)
689         {
690                 IConvertible convert_value = value as IConvertible;
691                 
692                 if (convert_value == null){
693                         error = true;
694                         return null;
695                 }
696                 
697                 //
698                 // We cannot rely on build-in type conversions as they are
699                 // more limited than what C# supports.
700                 // See char -> float/decimal/double conversion
701                 //
702                 error = false;
703                 try {
704                         if (targetType == TypeManager.bool_type)
705                                 return convert_value.ToBoolean (nf_provider);
706                         if (targetType == TypeManager.byte_type)
707                                 return convert_value.ToByte (nf_provider);
708                         if (targetType == TypeManager.char_type)
709                                 return convert_value.ToChar (nf_provider);
710                         if (targetType == TypeManager.short_type)
711                                 return convert_value.ToInt16 (nf_provider);
712                         if (targetType == TypeManager.int32_type)
713                                 return convert_value.ToInt32 (nf_provider);
714                         if (targetType == TypeManager.int64_type)
715                                 return convert_value.ToInt64 (nf_provider);
716                         if (targetType == TypeManager.sbyte_type)
717                                 return convert_value.ToSByte (nf_provider);
718
719                         if (targetType == TypeManager.decimal_type) {
720                                 if (convert_value.GetType () == typeof (char))
721                                         return (decimal) convert_value.ToInt32 (nf_provider);
722                                 return convert_value.ToDecimal (nf_provider);
723                         }
724
725                         if (targetType == TypeManager.double_type) {
726                                 if (convert_value.GetType () == typeof (char))
727                                         return (double) convert_value.ToInt32 (nf_provider);
728                                 return convert_value.ToDouble (nf_provider);
729                         }
730
731                         if (targetType == TypeManager.float_type) {
732                                 if (convert_value.GetType () == typeof (char))
733                                         return (float)convert_value.ToInt32 (nf_provider);
734                                 return convert_value.ToSingle (nf_provider);
735                         }
736
737                         if (targetType == TypeManager.string_type)
738                                 return convert_value.ToString (nf_provider);
739                         if (targetType == TypeManager.ushort_type)
740                                 return convert_value.ToUInt16 (nf_provider);
741                         if (targetType == TypeManager.uint32_type)
742                                 return convert_value.ToUInt32 (nf_provider);
743                         if (targetType == TypeManager.uint64_type)
744                                 return convert_value.ToUInt64 (nf_provider);
745                         if (targetType == TypeManager.object_type)
746                                 return value;
747
748                         error = true;
749                 } catch {
750                         error = true;
751                 }
752                 return null;
753         }
754
755         /// <summary>
756         ///   Utility function that can be used to probe whether a type
757         ///   is managed or not.  
758         /// </summary>
759         public static bool VerifyUnmanaged (CompilerContext ctx, TypeSpec t, Location loc)
760         {
761                 while (t.IsPointer)
762                         t = GetElementType (t);
763
764                 if (IsUnmanagedType (t))
765                         return true;
766
767                 ctx.Report.SymbolRelatedToPreviousError (t);
768                 ctx.Report.Error (208, loc,
769                         "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
770                         CSharpName (t));
771
772                 return false;   
773         }
774 #region Generics
775         // This method always return false for non-generic compiler,
776         // while Type.IsGenericParameter is returned if it is supported.
777         public static bool IsGenericParameter (TypeSpec type)
778         {
779                 return type.IsGenericParameter;
780         }
781
782         public static bool IsGenericType (TypeSpec type)
783         {
784                 return type.IsGeneric;
785         }
786
787         // TODO: Implement correctly
788         public static bool ContainsGenericParameters (TypeSpec type)
789         {
790                 return type.GetMetaInfo ().ContainsGenericParameters;
791         }
792
793         public static TypeSpec[] GetTypeArguments (TypeSpec t)
794         {
795                 // TODO: return empty array !!
796                 return t.TypeArguments;
797         }
798
799         /// <summary>
800         ///   Check whether `type' and `parent' are both instantiations of the same
801         ///   generic type.  Note that we do not check the type parameters here.
802         /// </summary>
803         public static bool IsInstantiationOfSameGenericType (TypeSpec type, TypeSpec parent)
804         {
805                 return type == parent || type.MemberDefinition == parent.MemberDefinition;
806         }
807
808         public static bool IsNullableType (TypeSpec t)
809         {
810                 return generic_nullable_type == t.GetDefinition ();
811         }
812 #endregion
813 }
814
815 }