codeowners update
[mono.git] / mcs / mcs / ikvm.cs
1 //
2 // ikvm.cs: IKVM.Reflection and IKVM.Reflection.Emit specific implementations
3 //
4 // Author: Marek Safar (marek.safar@gmail.com)
5 //
6 // Dual licensed under the terms of the MIT X11 or GNU GPL
7 //
8 // Copyright 2009-2010 Novell, Inc. 
9 // Copyright 2011 Xamarin Inc
10 //
11 //
12
13 using System;
14 using System.Collections.Generic;
15 using MetaType = IKVM.Reflection.Type;
16 using IKVM.Reflection;
17 using IKVM.Reflection.Emit;
18 using System.IO;
19 using System.Configuration.Assemblies;
20
21 namespace Mono.CSharp
22 {
23 #if !STATIC
24         public class StaticImporter
25         {
26                 public StaticImporter (BuiltinTypes builtin)
27                 {
28                         throw new NotSupportedException ();
29                 }
30
31                 public void ImportAssembly (Assembly assembly, RootNamespace targetNamespace)
32                 {
33                         throw new NotSupportedException ();
34                 }
35
36                 public void ImportModule (Module module, RootNamespace targetNamespace)
37                 {
38                         throw new NotSupportedException ();
39                 }
40
41                 public TypeSpec ImportType (System.Type type)
42                 {
43                         throw new NotSupportedException ();
44                 }
45         }
46
47 #else
48
49         sealed class StaticImporter : MetadataImporter
50         {
51                 public StaticImporter (ModuleContainer module)
52                         : base (module)
53                 {
54                 }
55
56                 public void AddCompiledAssembly (AssemblyDefinitionStatic assembly)
57                 {
58                         assembly_2_definition.Add (assembly.Builder, assembly);
59                 }
60
61                 public override void AddCompiledType (TypeBuilder type, TypeSpec spec)
62                 {
63                         compiled_types.Add (type, spec);
64                 }
65
66                 protected override MemberKind DetermineKindFromBaseType (MetaType baseType)
67                 {
68                         string name = baseType.Name;
69
70                         if (name == "ValueType" && baseType.Namespace == "System")
71                                 return MemberKind.Struct;
72
73                         if (name == "Enum" && baseType.Namespace == "System")
74                                 return MemberKind.Enum;
75
76                         if (name == "MulticastDelegate" && baseType.Namespace == "System")
77                                 return MemberKind.Delegate;
78
79                         return MemberKind.Class;
80                 }
81
82                 protected override bool HasVolatileModifier (MetaType[] modifiers)
83                 {
84                         foreach (var t in modifiers) {
85                                 if (t.Name == "IsVolatile" && t.Namespace == CompilerServicesNamespace)
86                                         return true;
87                         }
88
89                         return false;
90                 }
91
92                 public void ImportAssembly (Assembly assembly, RootNamespace targetNamespace)
93                 {
94                         try {
95                                 // It can be used more than once when importing same assembly
96                                 // into 2 or more global aliases
97                                 // TODO: Should be just Add
98                                 GetAssemblyDefinition (assembly);
99
100                                 var all_types = assembly.GetTypes ();
101                                 ImportTypes (all_types, targetNamespace, true);
102
103                                 all_types = assembly.ManifestModule.__GetExportedTypes ();
104                                 if (all_types.Length != 0)
105                                         ImportForwardedTypes (all_types, targetNamespace);
106                         } catch (Exception e) {
107                                 throw new InternalErrorException (e, "Failed to import assembly `{0}'", assembly.FullName);
108                         }
109                 }
110
111                 public ImportedModuleDefinition ImportModule (Module module, RootNamespace targetNamespace)
112                 {
113                         var module_definition = new ImportedModuleDefinition (module);
114                         module_definition.ReadAttributes ();
115
116                         var all_types = module.GetTypes ();
117                         ImportTypes (all_types, targetNamespace, false);
118
119                         return module_definition;
120                 }
121
122                 void ImportForwardedTypes (MetaType[] types, Namespace targetNamespace)
123                 {
124                         Namespace ns = targetNamespace;
125                         string prev_namespace = null;
126                         foreach (var t in types) {
127                                 if (!t.__IsTypeForwarder)
128                                         continue;
129
130                                 // IsMissing tells us the type has been forwarded and target assembly is missing 
131                                 if (!t.__IsMissing)
132                                         continue;
133
134                                 if (t.Name[0] == '<')
135                                         continue;
136
137                                 var it = CreateType (t, null, new AttributesTypeInfoReader (t), true);
138                                 if (it == null)
139                                         continue;
140
141                                 if (prev_namespace != t.Namespace) {
142                                         ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true);
143                                         prev_namespace = t.Namespace;
144                                 }
145
146                                 ns.AddType (module, it);
147                         }
148                 }
149
150                 public void InitializeBuiltinTypes (BuiltinTypes builtin, Assembly corlib)
151                 {
152                         //
153                         // Setup mapping for build-in types to avoid duplication of their definition
154                         //
155                         foreach (var type in builtin.AllTypes) {
156                                 compiled_types.Add (corlib.GetType (type.FullName), type);
157                         }
158                 }
159         }
160 #endif
161
162         class AssemblyDefinitionStatic : AssemblyDefinition
163         {
164                 readonly StaticLoader loader;
165
166                 //
167                 // Assembly container with file output
168                 //
169                 public AssemblyDefinitionStatic (ModuleContainer module, StaticLoader loader, string name, string fileName)
170                         : base (module, name, fileName)
171                 {
172                         this.loader = loader;
173                         Importer = loader.MetadataImporter;
174                 }
175
176                 //
177                 // Initializes the assembly SRE domain
178                 //
179                 public void Create (Universe domain)
180                 {
181                         ResolveAssemblySecurityAttributes ();
182                         var an = CreateAssemblyName ();
183
184                         Builder = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, Path.GetDirectoryName (file_name));
185                         module.Create (this, CreateModuleBuilder ());
186                 }
187
188                 public override void Emit ()
189                 {
190                         if (loader.Corlib != null && !(loader.Corlib is AssemblyBuilder)) {
191                                 Builder.__SetImageRuntimeVersion (loader.Corlib.ImageRuntimeVersion, 0x20000);
192                         } else if (module.Compiler.Settings.RuntimeMetadataVersion != null) {
193                                 Builder.__SetImageRuntimeVersion (module.Compiler.Settings.RuntimeMetadataVersion, 0x20000);
194                         } else {
195                                 // Sets output file metadata version when there is no mscorlib
196                                 switch (module.Compiler.Settings.StdLibRuntimeVersion) {
197                                 case RuntimeVersion.v4:
198                                         Builder.__SetImageRuntimeVersion ("v4.0.30319", 0x20000);
199                                         break;
200                                 case RuntimeVersion.v2:
201                                         Builder.__SetImageRuntimeVersion ("v2.0.50727", 0x20000);
202                                         break;
203                                 case RuntimeVersion.v1:
204                                         // Compiler does not do any checks whether the produced metadata
205                                         // are valid in the context of 1.0 stream version
206                                         Builder.__SetImageRuntimeVersion ("v1.1.4322", 0x10000);
207                                         break;
208                                 default:
209                                         throw new NotImplementedException ();
210                                 }
211                         }
212
213                         builder_extra = new AssemblyBuilderIKVM (Builder, Compiler);
214
215                         base.Emit ();
216                 }
217
218                 public Module IncludeModule (RawModule moduleFile)
219                 {
220                         return Builder.__AddModule (moduleFile);
221                 }
222
223                 protected override List<AssemblyReferenceMessageInfo> GetNotUnifiedReferences (AssemblyName assemblyName)
224                 {
225                         return loader.GetNotUnifiedReferences (assemblyName);
226                 }
227
228                 protected override void SaveModule (PortableExecutableKinds pekind, ImageFileMachine machine)
229                 {
230                         module.Builder.__Save (pekind, machine);
231                 }
232         }
233
234         class StaticLoader : AssemblyReferencesLoader<Assembly>, IDisposable
235         {
236                 readonly StaticImporter importer;
237                 readonly Universe domain;
238                 Assembly corlib;
239                 readonly List<Tuple<AssemblyName, string, Assembly>> loaded_names;
240                 static readonly Dictionary<string, string[]> sdk_directory;
241                 Dictionary<AssemblyName, List<AssemblyReferenceMessageInfo>> resolved_version_mismatches;
242                 static readonly TypeName objectTypeName = new TypeName ("System", "Object");
243
244                 static StaticLoader ()
245                 {
246                         sdk_directory = new Dictionary<string, string[]> ();
247                         sdk_directory.Add ("2", new string[] { "2.0-api", "v2.0.50727" });
248                         sdk_directory.Add ("2.0", new string[] { "2.0-api", "v2.0.50727" });
249                         sdk_directory.Add ("4", new string[] { "4.0-api", "v4.0.30319" });
250                         sdk_directory.Add ("4.0", new string[] { "4.0-api", "v4.0.30319" });
251                         sdk_directory.Add ("4.5", new string[] { "4.5-api", "v4.0.30319" });
252                         sdk_directory.Add ("4.5.1", new string[] { "4.5.1-api", "v4.0.30319" });
253                         sdk_directory.Add ("4.5.2", new string[] { "4.5.2-api", "v4.0.30319" });
254                         sdk_directory.Add ("4.6", new string[] { "4.6-api", "v4.0.30319" });
255                         sdk_directory.Add ("4.6.1", new string[] { "4.6.1-api", "v4.0.30319" });
256                         sdk_directory.Add ("4.6.2", new string [] { "4.6.2-api", "v4.0.30319" });
257                         sdk_directory.Add ("4.7", new string [] { "4.7-api", "v4.0.30319" });
258                         sdk_directory.Add ("4.x", new string [] { "4.5", "net_4_x", "v4.0.30319" });
259                 }
260
261                 public StaticLoader (StaticImporter importer, CompilerContext compiler)
262                         : base (compiler)
263                 {
264                         this.importer = importer;
265                         domain = new Universe (UniverseOptions.MetadataOnly | UniverseOptions.ResolveMissingMembers | 
266                                 UniverseOptions.DisableFusion | UniverseOptions.DecodeVersionInfoAttributeBlobs |
267                                 UniverseOptions.DeterministicOutput | UniverseOptions.DisableDefaultAssembliesLookup);
268                         
269                         domain.AssemblyResolve += AssemblyReferenceResolver;
270                         loaded_names = new List<Tuple<AssemblyName, string, Assembly>> ();
271
272                         if (compiler.Settings.StdLib) {
273                                 var corlib_path = Path.GetDirectoryName (typeof (object).Assembly.Location);
274                                 string fx_path = corlib_path.Substring (0, corlib_path.LastIndexOf (Path.DirectorySeparatorChar));
275
276                                 string sdk_path = null;
277
278                                 string sdk_version = compiler.Settings.SdkVersion ?? "4.x";
279                                 string[] sdk_sub_dirs;
280
281                                 if (!sdk_directory.TryGetValue (sdk_version, out sdk_sub_dirs))
282                                         sdk_sub_dirs = new string[] { sdk_version };
283
284                                 foreach (var dir in sdk_sub_dirs) {
285                                         sdk_path = Path.Combine (fx_path, dir);
286                                         if (File.Exists (Path.Combine (sdk_path, "mscorlib.dll")))
287                                                 break;
288
289                                         sdk_path = null;
290                                 }
291
292                                 if (sdk_path == null) {
293                                         compiler.Report.Warning (-1, 1, "SDK path could not be resolved");
294                                         sdk_path = corlib_path;
295                                 }
296
297                                 paths.Add (sdk_path);
298                         }
299                 }
300
301                 #region Properties
302
303                 public Assembly Corlib {
304                         get {
305                                 return corlib;
306                         }
307                 }
308
309                 public AssemblyDefinitionStatic CompiledAssembly {  get; set; }
310
311                 public Universe Domain {
312                         get {
313                                 return domain;
314                         }
315                 }
316
317                 public StaticImporter MetadataImporter {
318                         get {
319                                 return importer;
320                         }
321                 }
322
323                 #endregion
324
325                 Assembly AssemblyReferenceResolver (object sender, IKVM.Reflection.ResolveEventArgs args)
326                 {
327                         var refname = args.Name;
328                         if (refname == "mscorlib")
329                                 return corlib;
330
331                         Assembly version_mismatch = null;
332                         bool is_fx_assembly = false;
333
334                         foreach (var assembly in domain.GetAssemblies ()) {
335                                 AssemblyComparisonResult result;
336                                 if (!domain.CompareAssemblyIdentity (refname, false, assembly.FullName, false, out result)) {
337                                         if ((result == AssemblyComparisonResult.NonEquivalentVersion || result == AssemblyComparisonResult.NonEquivalentPartialVersion) &&
338                                                 (version_mismatch == null || version_mismatch.GetName ().Version < assembly.GetName ().Version) &&
339                                                 !is_fx_assembly) {
340                                                 version_mismatch = assembly;
341                                         }
342
343                                         continue;
344                                 }
345
346                                 if (result == AssemblyComparisonResult.EquivalentFullMatch ||
347                                         result == AssemblyComparisonResult.EquivalentWeakNamed ||
348                                         result == AssemblyComparisonResult.EquivalentPartialMatch) {
349                                         return assembly;
350                                 }
351
352                                 if (result == AssemblyComparisonResult.EquivalentFXUnified) {
353                                         is_fx_assembly = true;
354
355                                         if (version_mismatch == null || version_mismatch.GetName ().Version < assembly.GetName ().Version)
356                                                 version_mismatch = assembly;
357
358                                         continue;
359                                 }
360
361                                 throw new NotImplementedException ("Assembly equality = " + result.ToString ());
362                         }
363
364                         if (version_mismatch != null) {
365                                 if (is_fx_assembly || version_mismatch is AssemblyBuilder)
366                                         return version_mismatch;
367
368                                 var ref_an = new AssemblyName (refname);
369                                 var v1 = ref_an.Version;
370                                 var v2 = version_mismatch.GetName ().Version;
371                                 AssemblyReferenceMessageInfo messageInfo;
372
373                                 if (v1 > v2) {
374                                         messageInfo = new AssemblyReferenceMessageInfo (ref_an, report => {
375                                                 report.SymbolRelatedToPreviousError (args.RequestingAssembly.Location);
376                                                 report.Error (1705, string.Format ("Assembly `{0}' depends on `{1}' which has a higher version number than referenced assembly `{2}'",
377                                                                                                                    args.RequestingAssembly.FullName, refname, version_mismatch.GetName ().FullName));
378                                         });
379
380                                 } else {
381                                         messageInfo = new AssemblyReferenceMessageInfo (ref_an, report => {
382                                                 if (v1.Major != v2.Major || v1.Minor != v2.Minor) {
383                                                         report.Warning (1701, 2,
384                                                                 "Assuming assembly reference `{0}' matches assembly `{1}'. You may need to supply runtime policy",
385                                                                 refname, version_mismatch.GetName ().FullName);
386                                                 } else {
387                                                         report.Warning (1702, 3,
388                                                                 "Assuming assembly reference `{0}' matches assembly `{1}'. You may need to supply runtime policy",
389                                                                 refname, version_mismatch.GetName ().FullName);
390                                                 }
391                                         });
392                                 }
393
394                                 AddReferenceVersionMismatch (args.RequestingAssembly.GetName (), messageInfo);
395
396                                 return version_mismatch;
397                         }
398
399                         //
400                         // Recursive reference to compiled assembly checks name only. Any other
401                         // details (PublicKey, Version, etc) are not yet known hence cannot be checked
402                         //
403                         ParsedAssemblyName referenced_assembly;
404                         if (Fusion.ParseAssemblyName (args.Name, out referenced_assembly) == ParseAssemblyResult.OK && CompiledAssembly.Name == referenced_assembly.Name)
405                                 return CompiledAssembly.Builder;
406
407                         // AssemblyReference has not been found in the domain
408                         // create missing reference and continue
409                         return domain.CreateMissingAssembly (args.Name);
410                 }
411
412                 void AddReferenceVersionMismatch (AssemblyName an, AssemblyReferenceMessageInfo errorInfo)
413                 {
414                         if (resolved_version_mismatches == null)
415                                 resolved_version_mismatches = new Dictionary<AssemblyName, List<AssemblyReferenceMessageInfo>> ();
416
417                         List<AssemblyReferenceMessageInfo> names;
418                         if (!resolved_version_mismatches.TryGetValue (an, out names)) {
419                                 names = new List<AssemblyReferenceMessageInfo> ();
420                                 resolved_version_mismatches.Add (an, names);
421                         }
422
423                         names.Add (errorInfo);
424                 }
425
426                 public void Dispose ()
427                 {
428                         domain.Dispose ();
429                 }
430
431                 protected override string[] GetDefaultReferences ()
432                 {
433                         //
434                         // For now the "default config" is harcoded into the compiler
435                         // we can move this outside later
436                         //
437                         var default_references = new List<string> (4);
438
439                         default_references.Add ("System.dll");
440                         default_references.Add ("System.Xml.dll");
441                         default_references.Add ("System.Core.dll");
442
443                         if (corlib != null && corlib.GetName ().Version.Major >= 4) {
444                                 default_references.Add ("Microsoft.CSharp.dll");
445                         }
446
447                         return default_references.ToArray ();
448                 }
449
450                 public List<AssemblyReferenceMessageInfo> GetNotUnifiedReferences (AssemblyName assemblyName)
451                 {
452                         List<AssemblyReferenceMessageInfo> list = null;
453                         if (resolved_version_mismatches != null)
454                                 resolved_version_mismatches.TryGetValue (assemblyName, out list);
455
456                         return list;
457                 }
458
459                 public override Assembly HasObjectType (Assembly assembly)
460                 {
461                         try {
462                                 // System.Object can be forwarded and ikvm
463                                 // transparently finds it in target assembly therefore
464                                 // need to return actual obj assembly becauase in such
465                                 // case it's different to assembly parameter
466                                 var obj = assembly.FindType (objectTypeName);
467                                 return obj == null ? null : obj.Assembly;
468                         } catch (Exception e) {
469                                 throw new InternalErrorException (e, "Failed to load assembly `{0}'", assembly.FullName);
470                         }
471                 }
472
473                 public override Assembly LoadAssemblyFile (string fileName, bool isImplicitReference)
474                 {
475                         bool? has_extension = null;
476                         foreach (var path in paths) {
477                                 var file = Path.Combine (path, fileName);
478                                 if (compiler.Settings.DebugFlags > 0)
479                                         Console.WriteLine ("Probing assembly location `{0}'", file);
480
481                                 if (!File.Exists (file)) {
482                                         if (!has_extension.HasValue)
483                                                 has_extension = fileName.EndsWith (".dll", StringComparison.Ordinal) || fileName.EndsWith (".exe", StringComparison.Ordinal);
484
485                                         if (has_extension.Value)
486                                                 continue;
487
488                                         file += ".dll";
489                                         if (!File.Exists (file))
490                                                 continue;
491                                 }
492
493                                 try {
494                                         using (var stream = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.Read)) {
495                                                 using (RawModule module = domain.OpenRawModule (stream, file)) {
496                                                         if (!module.IsManifestModule) {
497                                                                 Error_AssemblyIsModule (fileName);
498                                                                 return null;
499                                                         }
500
501                                                         //
502                                                         // check whether the assembly can be actually imported without
503                                                         // collision
504                                                         //
505                                                         var an = module.GetAssemblyName ();
506                                                         foreach (var entry in loaded_names) {
507                                                                 var loaded_name = entry.Item1;
508                                                                 if (an.Name != loaded_name.Name)
509                                                                         continue;
510
511                                                                 if (module.ModuleVersionId == entry.Item3.ManifestModule.ModuleVersionId)
512                                                                         return entry.Item3;
513                                                         
514                                                                 if (((an.Flags | loaded_name.Flags) & AssemblyNameFlags.PublicKey) == 0) {
515                                                                         compiler.Report.SymbolRelatedToPreviousError (entry.Item2);
516                                                                         compiler.Report.SymbolRelatedToPreviousError (fileName);
517                                                                         compiler.Report.Error (1704,
518                                                                                 "An assembly with the same name `{0}' has already been imported. Consider removing one of the references or sign the assembly",
519                                                                                 an.Name);
520                                                                         return null;
521                                                                 }
522
523                                                                 AssemblyComparisonResult result;
524                                                                 if ((an.Flags & AssemblyNameFlags.PublicKey) == (loaded_name.Flags & AssemblyNameFlags.PublicKey) &&
525                                                                     (domain.CompareAssemblyIdentity (an.FullName, false, loaded_name.FullName, false, out result))) {
526
527                                                                         //
528                                                                         // Roslyn is much more lenient than native compiler here
529                                                                         //
530                                                                         switch (result) {
531                                                                         case AssemblyComparisonResult.EquivalentFXUnified:
532                                                                         case AssemblyComparisonResult.EquivalentUnified:
533                                                                                 compiler.Report.SymbolRelatedToPreviousError (entry.Item2);
534                                                                                 compiler.Report.SymbolRelatedToPreviousError (fileName);
535                                                                                 compiler.Report.Error (1703,
536                                                                                         "An assembly `{0}' with the same identity has already been imported. Consider removing one of the references",
537                                                                                         an.Name);
538                                                                                 return null;
539                                                                         }
540                                                                 }
541                                                         }
542
543                                                         if (compiler.Settings.DebugFlags > 0)
544                                                                 Console.WriteLine ("Loading assembly `{0}'", fileName);
545
546                                                         var assembly = domain.LoadAssembly (module);
547                                                         if (assembly != null)
548                                                                 loaded_names.Add (Tuple.Create (an, fileName, assembly));
549
550                                                         return assembly;
551                                                 }
552                                         }
553                                 } catch (Exception e) {
554                                         if (compiler.Settings.DebugFlags > 0)
555                                                 Console.WriteLine ("Exception during loading: {0}'", e.ToString ());
556
557                                         if (!isImplicitReference)
558                                                 Error_FileCorrupted (file);
559
560                                         return null;
561                                 }
562                         }
563
564                         if (!isImplicitReference)
565                                 Error_FileNotFound (fileName);
566
567                         return null;
568                 }
569
570                 public RawModule LoadModuleFile (string moduleName)
571                 {
572                         foreach (var path in paths) {
573                                 var file = Path.Combine (path, moduleName);
574                                 if (!File.Exists (file)) {
575                                         if (moduleName.EndsWith (".netmodule", StringComparison.Ordinal))
576                                                 continue;
577
578                                         file += ".netmodule";
579                                         if (!File.Exists (file))
580                                                 continue;
581                                 }
582
583                                 try {
584                                         return domain.OpenRawModule (file);
585                                 } catch {
586                                         Error_FileCorrupted (file);
587                                         return null;
588                                 }
589                         }
590
591                         Error_FileNotFound (moduleName);
592                         return null;                            
593                 }
594
595                 public override void LoadReferences (ModuleContainer module)
596                 {
597                         List<Tuple<RootNamespace, Assembly>> loaded;
598                         base.LoadReferencesCore (module, out corlib, out loaded);
599
600                         compiler.TimeReporter.Start (TimeReporter.TimerType.ReferencesImporting);
601
602                         if (corlib == null || corlib.__IsMissing) {
603                                 // System.Object was not found in any referenced assembly, use compiled assembly as corlib
604                                 corlib = module.DeclaringAssembly.Builder;
605                         } else {
606                                 importer.InitializeBuiltinTypes (compiler.BuiltinTypes, corlib);
607                                 importer.ImportAssembly (corlib, module.GlobalRootNamespace);
608                         }
609
610                         foreach (var entry in loaded) {
611                                 importer.ImportAssembly (entry.Item2, entry.Item1);
612                         }
613
614                         compiler.TimeReporter.Stop (TimeReporter.TimerType.ReferencesImporting);
615                 }
616
617                 public void LoadModules (AssemblyDefinitionStatic assembly, RootNamespace targetNamespace)
618                 {
619                         foreach (var moduleName in compiler.Settings.Modules) {
620                                 var m = LoadModuleFile (moduleName);
621                                 if (m == null)
622                                         continue;
623
624                                 if (m.IsManifestModule) {
625                                         Error_ModuleIsAssembly (moduleName);
626                                         continue;
627                                 }
628
629                                 var md = importer.ImportModule (assembly.IncludeModule (m), targetNamespace);
630                                 assembly.AddModule (md);
631                         }
632                 }
633         }
634
635         class AssemblyBuilderIKVM : AssemblyBuilderExtension
636         {
637                 readonly AssemblyBuilder builder;
638
639                 public AssemblyBuilderIKVM (AssemblyBuilder builder, CompilerContext ctx)
640                         : base (ctx)
641                 {
642                         this.builder = builder;
643                 }
644
645                 public override void AddTypeForwarder (TypeSpec type, Location loc)
646                 {
647                         builder.__AddTypeForwarder (type.GetMetaInfo (), false);
648                 }
649
650                 public override void DefineWin32IconResource (string fileName)
651                 {
652                         byte[] bytes;
653                         try {
654                                 bytes = File.ReadAllBytes (fileName);
655                         } catch (Exception e) {
656                                 ctx.Report.Error (7064, Location.Null, "Error opening icon file `{0}'. {1}", fileName, e.Message);
657                                 return;
658                         }
659
660                         builder.__DefineIconResource (bytes);
661                 }
662
663                 public override AssemblyName[] GetReferencedAssemblies ()
664                 {
665                         foreach (var m in builder.Modules) {
666                                 if (m is ModuleBuilder)
667                                         return m.__GetReferencedAssemblies ();
668                         }
669
670                         return new AssemblyName [0];
671                 }
672
673                 public override void SetAlgorithmId (uint value, Location loc)
674                 {
675                         builder.__SetAssemblyAlgorithmId ((AssemblyHashAlgorithm) value);
676                 }
677
678                 public override void SetCulture (string culture, Location loc)
679                 {
680                         builder.__SetAssemblyCulture (culture);
681                 }
682
683                 public override void SetFlags (uint flags, Location loc)
684                 {
685                         builder.__AssemblyFlags = (AssemblyNameFlags) flags;
686                 }
687
688                 public override void SetVersion (Version version, Location loc)
689                 {
690                         builder.__SetAssemblyVersion (version);
691                 }
692         }
693 }