~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Parser/mcs/assembly.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// assembly.cs: Assembly declaration and specifications
 
3
//
 
4
// Authors:
 
5
//   Miguel de Icaza (miguel@ximian.com)
 
6
//   Marek Safar (marek.safar@gmail.com)
 
7
//
 
8
// Copyright 2001, 2002, 2003 Ximian, Inc.
 
9
// Copyright 2004-2011 Novell, Inc.
 
10
// Copyright 2011 Xamarin Inc
 
11
//
 
12
 
 
13
 
 
14
using System;
 
15
using System.IO;
 
16
using System.Collections.Generic;
 
17
using System.Globalization;
 
18
using System.Security;
 
19
using System.Security.Cryptography;
 
20
using System.Security.Permissions;
 
21
using Mono.Security.Cryptography;
 
22
using Mono.CompilerServices.SymbolWriter;
 
23
 
 
24
#if STATIC
 
25
using IKVM.Reflection;
 
26
using IKVM.Reflection.Emit;
 
27
using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
 
28
#else
 
29
using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
 
30
using System.Reflection;
 
31
using System.Reflection.Emit;
 
32
#endif
 
33
 
 
34
namespace Mono.CSharp
 
35
{
 
36
        public interface IAssemblyDefinition
 
37
        {
 
38
                string FullName { get; }
 
39
                bool IsCLSCompliant { get; }
 
40
                bool IsMissing { get; }
 
41
                string Name { get; }
 
42
 
 
43
                byte[] GetPublicKeyToken ();
 
44
                bool IsFriendAssemblyTo (IAssemblyDefinition assembly);
 
45
        }
 
46
                
 
47
        public abstract class AssemblyDefinition : IAssemblyDefinition
 
48
        {
 
49
                // TODO: make it private and move all builder based methods here
 
50
                public AssemblyBuilder Builder;
 
51
                protected AssemblyBuilderExtension builder_extra;
 
52
                MonoSymbolFile symbol_writer;
 
53
 
 
54
                bool is_cls_compliant;
 
55
                bool wrap_non_exception_throws;
 
56
                bool wrap_non_exception_throws_custom;
 
57
 
 
58
                protected ModuleContainer module;
 
59
                readonly string name;
 
60
                protected readonly string file_name;
 
61
 
 
62
                byte[] public_key, public_key_token;
 
63
                bool delay_sign;
 
64
 
 
65
                // Holds private/public key pair when private key
 
66
                // was available
 
67
                StrongNameKeyPair private_key;  
 
68
 
 
69
                Attribute cls_attribute;
 
70
                Method entry_point;
 
71
 
 
72
                protected List<ImportedModuleDefinition> added_modules;
 
73
                SecurityType declarative_security;
 
74
                Dictionary<ITypeDefinition, Attribute> emitted_forwarders;
 
75
                AssemblyAttributesPlaceholder module_target_attrs;
 
76
 
 
77
                protected AssemblyDefinition (ModuleContainer module, string name)
 
78
                {
 
79
                        this.module = module;
 
80
                        this.name = Path.GetFileNameWithoutExtension (name);
 
81
 
 
82
                        wrap_non_exception_throws = true;
 
83
 
 
84
                        delay_sign = Compiler.Settings.StrongNameDelaySign;
 
85
 
 
86
                        //
 
87
                        // Load strong name key early enough for assembly importer to be able to
 
88
                        // use the keys for InternalsVisibleTo
 
89
                        // This should go somewhere close to ReferencesLoading but don't have the place yet
 
90
                        //
 
91
                        if (Compiler.Settings.HasKeyFileOrContainer) {
 
92
                                LoadPublicKey (Compiler.Settings.StrongNameKeyFile, Compiler.Settings.StrongNameKeyContainer);
 
93
                        }
 
94
                }
 
95
 
 
96
                protected AssemblyDefinition (ModuleContainer module, string name, string fileName)
 
97
                        : this (module, name)
 
98
                {
 
99
                        this.file_name = fileName;
 
100
                }
 
101
 
 
102
                #region Properties
 
103
 
 
104
                public Attribute CLSCompliantAttribute {
 
105
                        get {
 
106
                                return cls_attribute;
 
107
                        }
 
108
                }
 
109
 
 
110
                public CompilerContext Compiler {
 
111
                        get {
 
112
                                return module.Compiler;
 
113
                        }
 
114
                }
 
115
 
 
116
                //
 
117
                // Assembly entry point, aka Main method
 
118
                //
 
119
                public Method EntryPoint {
 
120
                        get {
 
121
                                return entry_point;
 
122
                        }
 
123
                        set {
 
124
                                entry_point = value;
 
125
                        }
 
126
                }
 
127
 
 
128
                public string FullName {
 
129
                        get {
 
130
                                return Builder.FullName;
 
131
                        }
 
132
                }
 
133
 
 
134
                public bool HasCLSCompliantAttribute {
 
135
                        get {
 
136
                                return cls_attribute != null;
 
137
                        }
 
138
                }
 
139
 
 
140
                // TODO: This should not exist here but will require more changes
 
141
                public MetadataImporter Importer {
 
142
                    get; set;
 
143
                }
 
144
 
 
145
                public bool IsCLSCompliant {
 
146
                        get {
 
147
                                return is_cls_compliant;
 
148
                        }
 
149
                }
 
150
 
 
151
                bool IAssemblyDefinition.IsMissing {
 
152
                        get {
 
153
                                return false;
 
154
                        }
 
155
                }
 
156
 
 
157
                public bool IsSatelliteAssembly { get; private set; }
 
158
 
 
159
                public string Name {
 
160
                        get {
 
161
                                return name;
 
162
                        }
 
163
                }
 
164
 
 
165
                public bool WrapNonExceptionThrows {
 
166
                        get {
 
167
                                return wrap_non_exception_throws;
 
168
                        }
 
169
                }
 
170
 
 
171
                protected Report Report {
 
172
                        get {
 
173
                                return Compiler.Report;
 
174
                        }
 
175
                }
 
176
 
 
177
                public MonoSymbolFile SymbolWriter {
 
178
                        get {
 
179
                                return symbol_writer;
 
180
                        }
 
181
                }
 
182
 
 
183
                #endregion
 
184
 
 
185
                public void AddModule (ImportedModuleDefinition module)
 
186
                {
 
187
                        if (added_modules == null) {
 
188
                                added_modules = new List<ImportedModuleDefinition> ();
 
189
                                added_modules.Add (module);
 
190
                        }
 
191
                }
 
192
 
 
193
                public void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
 
194
                {
 
195
                        if (a.IsValidSecurityAttribute ()) {
 
196
                                a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
 
197
                                return;
 
198
                        }
 
199
 
 
200
                        if (a.Type == pa.AssemblyCulture) {
 
201
                                string value = a.GetString ();
 
202
                                if (value == null || value.Length == 0)
 
203
                                        return;
 
204
 
 
205
                                if (Compiler.Settings.Target == Target.Exe) {
 
206
                                        a.Error_AttributeEmitError ("The executables cannot be satelite assemblies, remove the attribute or keep it empty");
 
207
                                        return;
 
208
                                }
 
209
 
 
210
                                if (value == "neutral")
 
211
                                        value = "";
 
212
 
 
213
                                if (Compiler.Settings.Target == Target.Module) {
 
214
                                        SetCustomAttribute (ctor, cdata);
 
215
                                } else {
 
216
                                        builder_extra.SetCulture (value, a.Location);
 
217
                                }
 
218
 
 
219
                                IsSatelliteAssembly = true;
 
220
                                return;
 
221
                        }
 
222
 
 
223
                        if (a.Type == pa.AssemblyVersion) {
 
224
                                string value = a.GetString ();
 
225
                                if (value == null || value.Length == 0)
 
226
                                        return;
 
227
 
 
228
                                var vinfo = IsValidAssemblyVersion (value, true);
 
229
                                if (vinfo == null) {
 
230
                                        a.Error_AttributeEmitError (string.Format ("Specified version `{0}' is not valid", value));
 
231
                                        return;
 
232
                                }
 
233
 
 
234
                                if (Compiler.Settings.Target == Target.Module) {
 
235
                                        SetCustomAttribute (ctor, cdata);
 
236
                                } else {
 
237
                                        builder_extra.SetVersion (vinfo, a.Location);
 
238
                                }
 
239
 
 
240
                                return;
 
241
                        }
 
242
 
 
243
                        if (a.Type == pa.AssemblyAlgorithmId) {
 
244
                                const int pos = 2; // skip CA header
 
245
                                uint alg = (uint) cdata [pos];
 
246
                                alg |= ((uint) cdata [pos + 1]) << 8;
 
247
                                alg |= ((uint) cdata [pos + 2]) << 16;
 
248
                                alg |= ((uint) cdata [pos + 3]) << 24;
 
249
 
 
250
                                if (Compiler.Settings.Target == Target.Module) {
 
251
                                        SetCustomAttribute (ctor, cdata);
 
252
                                } else {
 
253
                                        builder_extra.SetAlgorithmId (alg, a.Location);
 
254
                                }
 
255
 
 
256
                                return;
 
257
                        }
 
258
 
 
259
                        if (a.Type == pa.AssemblyFlags) {
 
260
                                const int pos = 2; // skip CA header
 
261
                                uint flags = (uint) cdata[pos];
 
262
                                flags |= ((uint) cdata [pos + 1]) << 8;
 
263
                                flags |= ((uint) cdata [pos + 2]) << 16;
 
264
                                flags |= ((uint) cdata [pos + 3]) << 24;
 
265
 
 
266
                                // Ignore set PublicKey flag if assembly is not strongnamed
 
267
                                if ((flags & (uint) AssemblyNameFlags.PublicKey) != 0 && public_key == null)
 
268
                                        flags &= ~(uint) AssemblyNameFlags.PublicKey;
 
269
 
 
270
                                if (Compiler.Settings.Target == Target.Module) {
 
271
                                        SetCustomAttribute (ctor, cdata);
 
272
                                } else {
 
273
                                        builder_extra.SetFlags (flags, a.Location);
 
274
                                }
 
275
 
 
276
                                return;
 
277
                        }
 
278
 
 
279
                        if (a.Type == pa.TypeForwarder) {
 
280
                                TypeSpec t = a.GetArgumentType ();
 
281
                                if (t == null || TypeManager.HasElementType (t)) {
 
282
                                        Report.Error (735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute");
 
283
                                        return;
 
284
                                }
 
285
 
 
286
                                if (emitted_forwarders == null) {
 
287
                                        emitted_forwarders = new Dictionary<ITypeDefinition, Attribute> ();
 
288
                                } else if (emitted_forwarders.ContainsKey (t.MemberDefinition)) {
 
289
                                        Report.SymbolRelatedToPreviousError (emitted_forwarders[t.MemberDefinition].Location, null);
 
290
                                        Report.Error (739, a.Location, "A duplicate type forward of type `{0}'",
 
291
                                                TypeManager.CSharpName (t));
 
292
                                        return;
 
293
                                }
 
294
 
 
295
                                emitted_forwarders.Add (t.MemberDefinition, a);
 
296
 
 
297
                                if (t.MemberDefinition.DeclaringAssembly == this) {
 
298
                                        Report.SymbolRelatedToPreviousError (t);
 
299
                                        Report.Error (729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly",
 
300
                                                TypeManager.CSharpName (t));
 
301
                                        return;
 
302
                                }
 
303
 
 
304
                                if (t.IsNested) {
 
305
                                        Report.Error (730, a.Location, "Cannot forward type `{0}' because it is a nested type",
 
306
                                                TypeManager.CSharpName (t));
 
307
                                        return;
 
308
                                }
 
309
 
 
310
                                builder_extra.AddTypeForwarder (t.GetDefinition (), a.Location);
 
311
                                return;
 
312
                        }
 
313
 
 
314
                        if (a.Type == pa.Extension) {
 
315
                                a.Error_MisusedExtensionAttribute ();
 
316
                                return;
 
317
                        }
 
318
 
 
319
                        if (a.Type == pa.InternalsVisibleTo) {
 
320
                                string assembly_name = a.GetString ();
 
321
                                if (assembly_name.Length == 0)
 
322
                                        return;
 
323
#if STATIC
 
324
                                ParsedAssemblyName aname;
 
325
                                ParseAssemblyResult r = Fusion.ParseAssemblyName (assembly_name, out aname);
 
326
                                if (r != ParseAssemblyResult.OK) {
 
327
                                        Report.Warning (1700, 3, a.Location, "Assembly reference `{0}' is invalid and cannot be resolved",
 
328
                                                assembly_name);
 
329
                                        return;
 
330
                                }
 
331
 
 
332
                                if (aname.Version != null || aname.Culture != null || aname.ProcessorArchitecture != ProcessorArchitecture.None) {
 
333
                                        Report.Error (1725, a.Location,
 
334
                                                "Friend assembly reference `{0}' is invalid. InternalsVisibleTo declarations cannot have a version, culture or processor architecture specified",
 
335
                                                assembly_name);
 
336
 
 
337
                                        return;
 
338
                                }
 
339
 
 
340
                                if (public_key != null && !aname.HasPublicKey) {
 
341
                                        Report.Error (1726, a.Location,
 
342
                                                "Friend assembly reference `{0}' is invalid. Strong named assemblies must specify a public key in their InternalsVisibleTo declarations",
 
343
                                                assembly_name);
 
344
                                        return;
 
345
                                }
 
346
#endif
 
347
                        } else if (a.Type == pa.RuntimeCompatibility) {
 
348
                                wrap_non_exception_throws_custom = true;
 
349
                        } else if (a.Type == pa.AssemblyFileVersion) {
 
350
                                string value = a.GetString ();
 
351
                                if (string.IsNullOrEmpty (value) || IsValidAssemblyVersion (value, false) == null) {
 
352
                                        Report.Warning (1607, 1, a.Location, "The version number `{0}' specified for `{1}' is invalid",
 
353
                                                value, a.Name);
 
354
                                        return;
 
355
                                }
 
356
                        }
 
357
 
 
358
 
 
359
                        SetCustomAttribute (ctor, cdata);
 
360
                }
 
361
 
 
362
                //
 
363
                // When using assembly public key attributes InternalsVisibleTo key
 
364
                // was not checked, we have to do it later when we actually know what
 
365
                // our public key token is
 
366
                //
 
367
                void CheckReferencesPublicToken ()
 
368
                {
 
369
                        // TODO: It should check only references assemblies but there is
 
370
                        // no working SRE API
 
371
                        foreach (var entry in Importer.Assemblies) {
 
372
                                var a = entry as ImportedAssemblyDefinition;
 
373
                                if (a == null)
 
374
                                        continue;
 
375
 
 
376
                                if (public_key != null && !a.HasStrongName) {
 
377
                                        Report.Error (1577, "Referenced assembly `{0}' does not have a strong name",
 
378
                                                a.FullName);
 
379
                                }
 
380
 
 
381
                                var ci = a.Assembly.GetName ().CultureInfo;
 
382
                                if (!ci.Equals (System.Globalization.CultureInfo.InvariantCulture)) {
 
383
                                        Report.Warning (1607, 1, "Referenced assembly `{0}' has different culture setting of `{1}'",
 
384
                                                a.Name, ci.Name);
 
385
                                }
 
386
 
 
387
                                if (!a.IsFriendAssemblyTo (this))
 
388
                                        continue;
 
389
 
 
390
                                var attr = a.GetAssemblyVisibleToName (this);
 
391
                                var atoken = attr.GetPublicKeyToken ();
 
392
 
 
393
                                if (ArrayComparer.IsEqual (GetPublicKeyToken (), atoken))
 
394
                                        continue;
 
395
 
 
396
                                Report.SymbolRelatedToPreviousError (a.Location);
 
397
                                Report.Error (281,
 
398
                                        "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",
 
399
                                        attr.FullName, FullName);
 
400
                        }
 
401
                }
 
402
 
 
403
                protected AssemblyName CreateAssemblyName ()
 
404
                {
 
405
                        var an = new AssemblyName (name);
 
406
 
 
407
                        if (public_key != null && Compiler.Settings.Target != Target.Module) {
 
408
                                if (delay_sign) {
 
409
                                        an.SetPublicKey (public_key);
 
410
                                } else {
 
411
                                        if (public_key.Length == 16) {
 
412
                                                Report.Error (1606, "Could not sign the assembly. ECMA key can only be used to delay-sign assemblies");
 
413
                                        } else if (private_key == null) {
 
414
                                                Error_AssemblySigning ("The specified key file does not have a private key");
 
415
                                        } else {
 
416
                                                an.KeyPair = private_key;
 
417
                                        }
 
418
                                }
 
419
                        }
 
420
 
 
421
                        return an;
 
422
                }
 
423
 
 
424
                public virtual ModuleBuilder CreateModuleBuilder ()
 
425
                {
 
426
                        if (file_name == null)
 
427
                                throw new NotSupportedException ("transient module in static assembly");
 
428
 
 
429
                        var module_name = Path.GetFileName (file_name);
 
430
 
 
431
                        // Always initialize module without symbolInfo. We could be framework dependent
 
432
                        // but returned ISymbolWriter does not have all what we need therefore some
 
433
                        // adaptor will be needed for now we alwayas emit MDB format when generating
 
434
                        // debug info
 
435
                        return Builder.DefineDynamicModule (module_name, module_name, false);
 
436
                }
 
437
 
 
438
                public virtual void Emit ()
 
439
                {
 
440
                        if (Compiler.Settings.Target == Target.Module) {
 
441
                                module_target_attrs = new AssemblyAttributesPlaceholder (module, name);
 
442
                                module_target_attrs.CreateContainer ();
 
443
                                module_target_attrs.DefineContainer ();
 
444
                                module_target_attrs.Define ();
 
445
                                module.AddCompilerGeneratedClass (module_target_attrs);
 
446
                        } else if (added_modules != null) {
 
447
                                ReadModulesAssemblyAttributes ();
 
448
                        }
 
449
 
 
450
                        if (Compiler.Settings.GenerateDebugInfo) {
 
451
                                symbol_writer = new MonoSymbolFile ();
 
452
                        }
 
453
 
 
454
                        module.EmitContainer ();
 
455
 
 
456
                        if (module.HasExtensionMethod) {
 
457
                                var pa = module.PredefinedAttributes.Extension;
 
458
                                if (pa.IsDefined) {
 
459
                                        SetCustomAttribute (pa.Constructor, AttributeEncoder.Empty);
 
460
                                }
 
461
                        }
 
462
 
 
463
                        if (!IsSatelliteAssembly) {
 
464
                                if (!wrap_non_exception_throws_custom) {
 
465
                                        PredefinedAttribute pa = module.PredefinedAttributes.RuntimeCompatibility;
 
466
                                        if (pa.IsDefined && pa.ResolveBuilder ()) {
 
467
                                                var prop = module.PredefinedMembers.RuntimeCompatibilityWrapNonExceptionThrows.Get ();
 
468
                                                if (prop != null) {
 
469
                                                        AttributeEncoder encoder = new AttributeEncoder ();
 
470
                                                        encoder.EncodeNamedPropertyArgument (prop, new BoolLiteral (Compiler.BuiltinTypes, true, Location.Null));
 
471
                                                        SetCustomAttribute (pa.Constructor, encoder.ToArray ());
 
472
                                                }
 
473
                                        }
 
474
                                }
 
475
 
 
476
                                if (declarative_security != null) {
 
477
#if STATIC
 
478
                                        foreach (var entry in declarative_security) {
 
479
                                                Builder.__AddDeclarativeSecurity (entry);
 
480
                                        }
 
481
#else
 
482
                                        throw new NotSupportedException ("Assembly-level security");
 
483
#endif
 
484
                                }
 
485
                        }
 
486
 
 
487
                        CheckReferencesPublicToken ();
 
488
 
 
489
                        SetEntryPoint ();
 
490
                }
 
491
 
 
492
                public byte[] GetPublicKeyToken ()
 
493
                {
 
494
                        if (public_key == null || public_key_token != null)
 
495
                                return public_key_token;
 
496
 
 
497
                        HashAlgorithm ha = SHA1.Create ();
 
498
                        byte[] hash = ha.ComputeHash (public_key);
 
499
                        // we need the last 8 bytes in reverse order
 
500
                        public_key_token = new byte[8];
 
501
                        Buffer.BlockCopy (hash, hash.Length - 8, public_key_token, 0, 8);
 
502
                        Array.Reverse (public_key_token, 0, 8);
 
503
                        return public_key_token;
 
504
                }
 
505
 
 
506
                //
 
507
                // Either keyFile or keyContainer has to be non-null
 
508
                //
 
509
                void LoadPublicKey (string keyFile, string keyContainer)
 
510
                {
 
511
                        if (keyContainer != null) {
 
512
                                try {
 
513
                                        private_key = new StrongNameKeyPair (keyContainer);
 
514
                                        public_key = private_key.PublicKey;
 
515
                                } catch {
 
516
                                        Error_AssemblySigning ("The specified key container `" + keyContainer + "' does not exist");
 
517
                                }
 
518
 
 
519
                                return;
 
520
                        }
 
521
 
 
522
                        bool key_file_exists = File.Exists (keyFile);
 
523
 
 
524
                        //
 
525
                        // For attribute based KeyFile do additional lookup
 
526
                        // in output assembly path
 
527
                        //
 
528
                        if (!key_file_exists && Compiler.Settings.StrongNameKeyFile == null) {
 
529
                                //
 
530
                                // The key file can be relative to output assembly
 
531
                                //
 
532
                                string test_path = Path.Combine (Path.GetDirectoryName (file_name), keyFile);
 
533
                                key_file_exists = File.Exists (test_path);
 
534
                                if (key_file_exists)
 
535
                                        keyFile = test_path;
 
536
                        }
 
537
 
 
538
                        if (!key_file_exists) {
 
539
                                Error_AssemblySigning ("The specified key file `" + keyFile + "' does not exist");
 
540
                                return;
 
541
                        }
 
542
 
 
543
                        using (FileStream fs = new FileStream (keyFile, FileMode.Open, FileAccess.Read)) {
 
544
                                byte[] snkeypair = new byte[fs.Length];
 
545
                                fs.Read (snkeypair, 0, snkeypair.Length);
 
546
 
 
547
                                // check for ECMA key
 
548
                                if (snkeypair.Length == 16) {
 
549
                                        public_key = snkeypair;
 
550
                                        return;
 
551
                                }
 
552
 
 
553
                                try {
 
554
                                        // take it, with or without, a private key
 
555
                                        RSA rsa = CryptoConvert.FromCapiKeyBlob (snkeypair);
 
556
                                        // and make sure we only feed the public part to Sys.Ref
 
557
                                        byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob (rsa);
 
558
 
 
559
                                        // AssemblyName.SetPublicKey requires an additional header
 
560
                                        byte[] publicKeyHeader = new byte[8] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00 };
 
561
 
 
562
                                        // Encode public key
 
563
                                        public_key = new byte[12 + publickey.Length];
 
564
                                        Buffer.BlockCopy (publicKeyHeader, 0, public_key, 0, publicKeyHeader.Length);
 
565
 
 
566
                                        // Length of Public Key (in bytes)
 
567
                                        int lastPart = public_key.Length - 12;
 
568
                                        public_key[8] = (byte) (lastPart & 0xFF);
 
569
                                        public_key[9] = (byte) ((lastPart >> 8) & 0xFF);
 
570
                                        public_key[10] = (byte) ((lastPart >> 16) & 0xFF);
 
571
                                        public_key[11] = (byte) ((lastPart >> 24) & 0xFF);
 
572
 
 
573
                                        Buffer.BlockCopy (publickey, 0, public_key, 12, publickey.Length);
 
574
                                } catch {
 
575
                                        Error_AssemblySigning ("The specified key file `" + keyFile + "' has incorrect format");
 
576
                                        return;
 
577
                                }
 
578
 
 
579
                                if (delay_sign)
 
580
                                        return;
 
581
 
 
582
                                try {
 
583
                                        // TODO: Is there better way to test for a private key presence ?
 
584
                                        CryptoConvert.FromCapiPrivateKeyBlob (snkeypair);
 
585
                                        private_key = new StrongNameKeyPair (snkeypair);
 
586
                                } catch { }
 
587
                        }
 
588
                }
 
589
 
 
590
                void ReadModulesAssemblyAttributes ()
 
591
                {
 
592
                        foreach (var m in added_modules) {
 
593
                                var cattrs = m.ReadAssemblyAttributes ();
 
594
                                if (cattrs == null)
 
595
                                        continue;
 
596
 
 
597
                                module.OptAttributes.AddAttributes (cattrs);
 
598
                        }
 
599
                }
 
600
 
 
601
                public void Resolve ()
 
602
                {
 
603
                        if (Compiler.Settings.Unsafe && module.PredefinedTypes.SecurityAction.Define ()) {
 
604
                                //
 
605
                                // Emits [assembly: SecurityPermissionAttribute (SecurityAction.RequestMinimum, SkipVerification = true)]
 
606
                                // when -unsafe option was specified
 
607
                                //
 
608
                                Location loc = Location.Null;
 
609
 
 
610
                                MemberAccess system_security_permissions = new MemberAccess (new MemberAccess (
 
611
                                        new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Security", loc), "Permissions", loc);
 
612
 
 
613
                                var req_min = module.PredefinedMembers.SecurityActionRequestMinimum.Resolve (loc);
 
614
 
 
615
                                Arguments pos = new Arguments (1);
 
616
                                pos.Add (new Argument (req_min.GetConstant (null)));
 
617
 
 
618
                                Arguments named = new Arguments (1);
 
619
                                named.Add (new NamedArgument ("SkipVerification", loc, new BoolLiteral (Compiler.BuiltinTypes, true, loc)));
 
620
 
 
621
                                Attribute g = new Attribute ("assembly",
 
622
                                        new MemberAccess (system_security_permissions, "SecurityPermissionAttribute"),
 
623
                                        new Arguments[] { pos, named }, loc, false);
 
624
                                g.AttachTo (module, module);
 
625
 
 
626
                                // Disable no-location warnings (e.g. obsolete) for compiler generated attribute
 
627
                                Compiler.Report.DisableReporting ();
 
628
                                try {
 
629
                                        var ctor = g.Resolve ();
 
630
                                        if (ctor != null) {
 
631
                                                g.ExtractSecurityPermissionSet (ctor, ref declarative_security);
 
632
                                        }
 
633
                                } finally {
 
634
                                        Compiler.Report.EnableReporting ();
 
635
                                }
 
636
                        }
 
637
 
 
638
                        if (module.OptAttributes == null)
 
639
                                return;
 
640
 
 
641
                        // Ensure that we only have GlobalAttributes, since the Search isn't safe with other types.
 
642
                        if (!module.OptAttributes.CheckTargets())
 
643
                                return;
 
644
 
 
645
                        cls_attribute = module.ResolveAssemblyAttribute (module.PredefinedAttributes.CLSCompliant);
 
646
 
 
647
                        if (cls_attribute != null) {
 
648
                                is_cls_compliant = cls_attribute.GetClsCompliantAttributeValue ();
 
649
                        }
 
650
 
 
651
                        if (added_modules != null && Compiler.Settings.VerifyClsCompliance && is_cls_compliant) {
 
652
                                foreach (var m in added_modules) {
 
653
                                        if (!m.IsCLSCompliant) {
 
654
                                                Report.Error (3013,
 
655
                                                        "Added modules must be marked with the CLSCompliant attribute to match the assembly",
 
656
                                                        m.Name);
 
657
                                        }
 
658
                                }
 
659
                        }
 
660
 
 
661
                        Attribute a = module.ResolveAssemblyAttribute (module.PredefinedAttributes.RuntimeCompatibility);
 
662
                        if (a != null) {
 
663
                                var val = a.GetNamedValue ("WrapNonExceptionThrows") as BoolConstant;
 
664
                                if (val != null)
 
665
                                        wrap_non_exception_throws = val.Value;
 
666
                        }
 
667
                }
 
668
 
 
669
                protected void ResolveAssemblySecurityAttributes ()
 
670
                {
 
671
                        string key_file = null;
 
672
                        string key_container = null;
 
673
 
 
674
                        if (module.OptAttributes != null) {
 
675
                                foreach (Attribute a in module.OptAttributes.Attrs) {
 
676
                                        // cannot rely on any resolve-based members before you call Resolve
 
677
                                        if (a.ExplicitTarget != "assembly")
 
678
                                                continue;
 
679
 
 
680
                                        // TODO: This code is buggy: comparing Attribute name without resolving is wrong.
 
681
                                        //       However, this is invoked by CodeGen.Init, when none of the namespaces
 
682
                                        //       are loaded yet.
 
683
                                        // TODO: Does not handle quoted attributes properly
 
684
                                        switch (a.Name) {
 
685
                                        case "AssemblyKeyFile":
 
686
                                        case "AssemblyKeyFileAttribute":
 
687
                                        case "System.Reflection.AssemblyKeyFileAttribute":
 
688
                                                if (Compiler.Settings.StrongNameKeyFile != null) {
 
689
                                                        Report.SymbolRelatedToPreviousError (a.Location, a.GetSignatureForError ());
 
690
                                                        Report.Warning (1616, 1, "Option `{0}' overrides attribute `{1}' given in a source file or added module",
 
691
                                                                        "keyfile", "System.Reflection.AssemblyKeyFileAttribute");
 
692
                                                } else {
 
693
                                                        string value = a.GetString ();
 
694
                                                        if (!string.IsNullOrEmpty (value)) {
 
695
                                                                Error_ObsoleteSecurityAttribute (a, "keyfile");
 
696
                                                                key_file = value;
 
697
                                                        }
 
698
                                                }
 
699
                                                break;
 
700
                                        case "AssemblyKeyName":
 
701
                                        case "AssemblyKeyNameAttribute":
 
702
                                        case "System.Reflection.AssemblyKeyNameAttribute":
 
703
                                                if (Compiler.Settings.StrongNameKeyContainer != null) {
 
704
                                                        Report.SymbolRelatedToPreviousError (a.Location, a.GetSignatureForError ());
 
705
                                                        Report.Warning (1616, 1, "Option `{0}' overrides attribute `{1}' given in a source file or added module",
 
706
                                                                        "keycontainer", "System.Reflection.AssemblyKeyNameAttribute");
 
707
                                                } else {
 
708
                                                        string value = a.GetString ();
 
709
                                                        if (!string.IsNullOrEmpty (value)) {
 
710
                                                                Error_ObsoleteSecurityAttribute (a, "keycontainer");
 
711
                                                                key_container = value;
 
712
                                                        }
 
713
                                                }
 
714
                                                break;
 
715
                                        case "AssemblyDelaySign":
 
716
                                        case "AssemblyDelaySignAttribute":
 
717
                                        case "System.Reflection.AssemblyDelaySignAttribute":
 
718
                                                bool b = a.GetBoolean ();
 
719
                                                if (b) {
 
720
                                                        Error_ObsoleteSecurityAttribute (a, "delaysign");
 
721
                                                }
 
722
 
 
723
                                                delay_sign = b;
 
724
                                                break;
 
725
                                        }
 
726
                                }
 
727
                        }
 
728
 
 
729
                        // We came here only to report assembly attributes warnings
 
730
                        if (public_key != null)
 
731
                                return;
 
732
 
 
733
                        //
 
734
                        // Load the strong key file found in attributes when no
 
735
                        // command line key was given
 
736
                        //
 
737
                        if (key_file != null || key_container != null) {
 
738
                                LoadPublicKey (key_file, key_container);
 
739
                        } else if (delay_sign) {
 
740
                                Report.Warning (1607, 1, "Delay signing was requested but no key file was given");
 
741
                        }
 
742
                }
 
743
 
 
744
                public void EmbedResources ()
 
745
                {
 
746
                        //
 
747
                        // Add Win32 resources
 
748
                        //
 
749
                        if (Compiler.Settings.Win32ResourceFile != null) {
 
750
                                Builder.DefineUnmanagedResource (Compiler.Settings.Win32ResourceFile);
 
751
                        } else {
 
752
                                Builder.DefineVersionInfoResource ();
 
753
                        }
 
754
 
 
755
                        if (Compiler.Settings.Win32IconFile != null) {
 
756
                                builder_extra.DefineWin32IconResource (Compiler.Settings.Win32IconFile);
 
757
                        }
 
758
 
 
759
                        if (Compiler.Settings.Resources != null) {
 
760
                                if (Compiler.Settings.Target == Target.Module) {
 
761
                                        Report.Error (1507, "Cannot link resource file when building a module");
 
762
                                } else {
 
763
                                        int counter = 0;
 
764
                                        foreach (var res in Compiler.Settings.Resources) {
 
765
                                                if (!File.Exists (res.FileName)) {
 
766
                                                        Report.Error (1566, "Error reading resource file `{0}'", res.FileName);
 
767
                                                        continue;
 
768
                                                }
 
769
 
 
770
                                                if (res.IsEmbeded) {
 
771
                                                        Stream stream;
 
772
                                                        if (counter++ < 10) {
 
773
                                                                stream = File.OpenRead (res.FileName);
 
774
                                                        } else {
 
775
                                                                // TODO: SRE API requires resource stream to be available during AssemblyBuilder::Save
 
776
                                                                // we workaround it by reading everything into memory to compile projects with
 
777
                                                                // many embedded resource (over 3500) references
 
778
                                                                stream = new MemoryStream (File.ReadAllBytes (res.FileName));
 
779
                                                        }
 
780
 
 
781
                                                        module.Builder.DefineManifestResource (res.Name, stream, res.Attributes);
 
782
                                                } else {
 
783
                                                        Builder.AddResourceFile (res.Name, Path.GetFileName (res.FileName), res.Attributes);
 
784
                                                }
 
785
                                        }
 
786
                                }
 
787
                        }
 
788
                }
 
789
 
 
790
                public void Save ()
 
791
                {
 
792
                        PortableExecutableKinds pekind = PortableExecutableKinds.ILOnly;
 
793
                        ImageFileMachine machine;
 
794
 
 
795
                        switch (Compiler.Settings.Platform) {
 
796
                        case Platform.X86:
 
797
                                pekind |= PortableExecutableKinds.Required32Bit;
 
798
                                machine = ImageFileMachine.I386;
 
799
                                break;
 
800
                        case Platform.X64:
 
801
                                pekind |= PortableExecutableKinds.PE32Plus;
 
802
                                machine = ImageFileMachine.AMD64;
 
803
                                break;
 
804
                        case Platform.IA64:
 
805
                                machine = ImageFileMachine.IA64;
 
806
                                break;
 
807
                        case Platform.AnyCPU32Preferred:
 
808
#if STATIC
 
809
                                pekind |= PortableExecutableKinds.Preferred32Bit;
 
810
                                machine = ImageFileMachine.I386;
 
811
                                break;
 
812
#else
 
813
                                throw new NotSupportedException ();
 
814
#endif
 
815
                        case Platform.Arm:
 
816
#if STATIC
 
817
                                machine = ImageFileMachine.ARM;
 
818
                                break;
 
819
#else
 
820
                                throw new NotSupportedException ();
 
821
#endif
 
822
                        case Platform.AnyCPU:
 
823
                        default:
 
824
                                machine = ImageFileMachine.I386;
 
825
                                break;
 
826
                        }
 
827
 
 
828
                        Compiler.TimeReporter.Start (TimeReporter.TimerType.OutputSave);
 
829
                        try {
 
830
                                if (Compiler.Settings.Target == Target.Module) {
 
831
                                        SaveModule (pekind, machine);
 
832
                                } else {
 
833
                                        Builder.Save (module.Builder.ScopeName, pekind, machine);
 
834
                                }
 
835
                        } catch (Exception e) {
 
836
                                Report.Error (16, "Could not write to file `" + name + "', cause: " + e.Message);
 
837
                        }
 
838
                        Compiler.TimeReporter.Stop (TimeReporter.TimerType.OutputSave);
 
839
 
 
840
                        // Save debug symbols file
 
841
                        if (symbol_writer != null && Compiler.Report.Errors == 0) {
 
842
                                // TODO: it should run in parallel
 
843
                                Compiler.TimeReporter.Start (TimeReporter.TimerType.DebugSave);
 
844
 
 
845
                                var filename = file_name + ".mdb";
 
846
                                try {
 
847
                                        // We mmap the file, so unlink the previous version since it may be in use
 
848
                                        File.Delete (filename);
 
849
                                } catch {
 
850
                                        // We can safely ignore
 
851
                                }
 
852
 
 
853
                                module.WriteDebugSymbol (symbol_writer);
 
854
 
 
855
                                using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write)) {
 
856
                                        symbol_writer.CreateSymbolFile (module.Builder.ModuleVersionId, fs);
 
857
                                }
 
858
 
 
859
                                Compiler.TimeReporter.Stop (TimeReporter.TimerType.DebugSave);
 
860
                        }
 
861
                }
 
862
 
 
863
                protected virtual void SaveModule (PortableExecutableKinds pekind, ImageFileMachine machine)
 
864
                {
 
865
                        Report.RuntimeMissingSupport (Location.Null, "-target:module");
 
866
                }
 
867
 
 
868
                void SetCustomAttribute (MethodSpec ctor, byte[] data)
 
869
                {
 
870
                        if (module_target_attrs != null)
 
871
                                module_target_attrs.AddAssemblyAttribute (ctor, data);
 
872
                        else
 
873
                                Builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), data);
 
874
                }
 
875
 
 
876
                void SetEntryPoint ()
 
877
                {
 
878
                        if (!Compiler.Settings.NeedsEntryPoint) {
 
879
                                if (Compiler.Settings.MainClass != null)
 
880
                                        Report.Error (2017, "Cannot specify -main if building a module or library");
 
881
 
 
882
                                return;
 
883
                        }
 
884
 
 
885
                        PEFileKinds file_kind;
 
886
 
 
887
                        switch (Compiler.Settings.Target) {
 
888
                        case Target.Library:
 
889
                        case Target.Module:
 
890
                                file_kind = PEFileKinds.Dll;
 
891
                                break;
 
892
                        case Target.WinExe:
 
893
                                file_kind = PEFileKinds.WindowApplication;
 
894
                                break;
 
895
                        default:
 
896
                                file_kind = PEFileKinds.ConsoleApplication;
 
897
                                break;
 
898
                        }
 
899
 
 
900
                        if (entry_point == null) {
 
901
                                string main_class = Compiler.Settings.MainClass;
 
902
                                if (main_class != null) {
 
903
                                        // TODO: Handle dotted names
 
904
                                        var texpr = module.GlobalRootNamespace.LookupType (module, main_class, 0, LookupMode.Probing, Location.Null);
 
905
                                        if (texpr == null) {
 
906
                                                Report.Error (1555, "Could not find `{0}' specified for Main method", main_class);
 
907
                                                return;
 
908
                                        }
 
909
 
 
910
                                        var mtype = texpr.Type.MemberDefinition as ClassOrStruct;
 
911
                                        if (mtype == null) {
 
912
                                                Report.Error (1556, "`{0}' specified for Main method must be a valid class or struct", main_class);
 
913
                                                return;
 
914
                                        }
 
915
 
 
916
                                        Report.Error (1558, mtype.Location, "`{0}' does not have a suitable static Main method", mtype.GetSignatureForError ());
 
917
                                } else {
 
918
                                        string pname = file_name == null ? name : Path.GetFileName (file_name);
 
919
                                        Report.Error (5001, "Program `{0}' does not contain a static `Main' method suitable for an entry point",
 
920
                                                pname);
 
921
                                }
 
922
 
 
923
                                return;
 
924
                        }
 
925
 
 
926
                        Builder.SetEntryPoint (entry_point.MethodBuilder, file_kind);
 
927
                }
 
928
 
 
929
                void Error_ObsoleteSecurityAttribute (Attribute a, string option)
 
930
                {
 
931
                        Report.Warning (1699, 1, a.Location,
 
932
                                "Use compiler option `{0}' or appropriate project settings instead of `{1}' attribute",
 
933
                                option, a.Name);
 
934
                }
 
935
 
 
936
                void Error_AssemblySigning (string text)
 
937
                {
 
938
                        Report.Error (1548, "Error during assembly signing. " + text);
 
939
                }
 
940
 
 
941
                public bool IsFriendAssemblyTo (IAssemblyDefinition assembly)
 
942
                {
 
943
                        return false;
 
944
                }
 
945
 
 
946
                static Version IsValidAssemblyVersion (string version, bool allowGenerated)
 
947
                {
 
948
                        string[] parts = version.Split ('.');
 
949
                        if (parts.Length < 1 || parts.Length > 4)
 
950
                                return null;
 
951
 
 
952
                        var values = new int[4];
 
953
                        for (int i = 0; i < parts.Length; ++i) {
 
954
                                if (!int.TryParse (parts[i], out values[i])) {
 
955
                                        if (parts[i].Length == 1 && parts[i][0] == '*' && allowGenerated) {
 
956
                                                if (i == 2) {
 
957
                                                        // Nothing can follow *
 
958
                                                        if (parts.Length > 3)
 
959
                                                                return null;
 
960
 
 
961
                                                        // Generate Build value based on days since 1/1/2000
 
962
                                                        TimeSpan days = DateTime.Today - new DateTime (2000, 1, 1);
 
963
                                                        values[i] = System.Math.Max (days.Days, 0);
 
964
                                                        i = 3;
 
965
                                                }
 
966
 
 
967
                                                if (i == 3) {
 
968
                                                        // Generate Revision value based on every other second today
 
969
                                                        var seconds = DateTime.Now - DateTime.Today;
 
970
                                                        values[i] = (int) seconds.TotalSeconds / 2;
 
971
                                                        continue;
 
972
                                                }
 
973
                                        }
 
974
 
 
975
                                        return null;
 
976
                                }
 
977
 
 
978
                                if (values[i] > ushort.MaxValue)
 
979
                                        return null;
 
980
                        }
 
981
 
 
982
                        return new Version (values[0], values[1], values[2], values[3]);
 
983
                }
 
984
        }
 
985
 
 
986
        public class AssemblyResource : IEquatable<AssemblyResource>
 
987
        {
 
988
                public AssemblyResource (string fileName, string name)
 
989
                        : this (fileName, name, false)
 
990
                {
 
991
                }
 
992
 
 
993
                public AssemblyResource (string fileName, string name, bool isPrivate)
 
994
                {
 
995
                        FileName = fileName;
 
996
                        Name = name;
 
997
                        Attributes = isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public;
 
998
                }
 
999
 
 
1000
                public ResourceAttributes Attributes { get; private set; }
 
1001
                public string Name { get; private set; }
 
1002
                public string FileName { get; private set; }
 
1003
                public bool IsEmbeded { get; set; }
 
1004
 
 
1005
                #region IEquatable<AssemblyResource> Members
 
1006
 
 
1007
                public bool Equals (AssemblyResource other)
 
1008
                {
 
1009
                        return Name == other.Name;
 
1010
                }
 
1011
 
 
1012
                #endregion
 
1013
        }
 
1014
 
 
1015
        //
 
1016
        // A placeholder class for assembly attributes when emitting module
 
1017
        //
 
1018
        class AssemblyAttributesPlaceholder : CompilerGeneratedContainer
 
1019
        {
 
1020
                static readonly string TypeNamePrefix = "<$AssemblyAttributes${0}>";
 
1021
                public static readonly string AssemblyFieldName = "attributes";
 
1022
 
 
1023
                Field assembly;
 
1024
 
 
1025
                public AssemblyAttributesPlaceholder (ModuleContainer parent, string outputName)
 
1026
                        : base (parent, new MemberName (GetGeneratedName (outputName)), Modifiers.STATIC | Modifiers.INTERNAL)
 
1027
                {
 
1028
                        assembly = new Field (this, new TypeExpression (parent.Compiler.BuiltinTypes.Object, Location), Modifiers.PUBLIC | Modifiers.STATIC,
 
1029
                                new MemberName (AssemblyFieldName), null);
 
1030
 
 
1031
                        AddField (assembly);
 
1032
                }
 
1033
 
 
1034
                public void AddAssemblyAttribute (MethodSpec ctor, byte[] data)
 
1035
                {
 
1036
                        assembly.SetCustomAttribute (ctor, data);
 
1037
                }
 
1038
 
 
1039
                public static string GetGeneratedName (string outputName)
 
1040
                {
 
1041
                        return string.Format (TypeNamePrefix, outputName);
 
1042
                }
 
1043
        }
 
1044
 
 
1045
        //
 
1046
        // Extension to System.Reflection.Emit.AssemblyBuilder to have fully compatible
 
1047
        // compiler. This is a default implementation for framework System.Reflection.Emit
 
1048
        // which does not implement any of the methods
 
1049
        //
 
1050
        public class AssemblyBuilderExtension
 
1051
        {
 
1052
                readonly CompilerContext ctx;
 
1053
 
 
1054
                public AssemblyBuilderExtension (CompilerContext ctx)
 
1055
                {
 
1056
                        this.ctx = ctx;
 
1057
                }
 
1058
 
 
1059
                public virtual System.Reflection.Module AddModule (string module)
 
1060
                {
 
1061
                        ctx.Report.RuntimeMissingSupport (Location.Null, "-addmodule");
 
1062
                        return null;
 
1063
                }
 
1064
 
 
1065
                public virtual void AddPermissionRequests (PermissionSet[] permissions)
 
1066
                {
 
1067
                        ctx.Report.RuntimeMissingSupport (Location.Null, "assembly declarative security");
 
1068
                }
 
1069
 
 
1070
                public virtual void AddTypeForwarder (TypeSpec type, Location loc)
 
1071
                {
 
1072
                        ctx.Report.RuntimeMissingSupport (loc, "TypeForwardedToAttribute");
 
1073
                }
 
1074
 
 
1075
                public virtual void DefineWin32IconResource (string fileName)
 
1076
                {
 
1077
                        ctx.Report.RuntimeMissingSupport (Location.Null, "-win32icon");
 
1078
                }
 
1079
 
 
1080
                public virtual void SetAlgorithmId (uint value, Location loc)
 
1081
                {
 
1082
                        ctx.Report.RuntimeMissingSupport (loc, "AssemblyAlgorithmIdAttribute");
 
1083
                }
 
1084
 
 
1085
                public virtual void SetCulture (string culture, Location loc)
 
1086
                {
 
1087
                        ctx.Report.RuntimeMissingSupport (loc, "AssemblyCultureAttribute");
 
1088
                }
 
1089
 
 
1090
                public virtual void SetFlags (uint flags, Location loc)
 
1091
                {
 
1092
                        ctx.Report.RuntimeMissingSupport (loc, "AssemblyFlagsAttribute");
 
1093
                }
 
1094
 
 
1095
                public virtual void SetVersion (Version version, Location loc)
 
1096
                {
 
1097
                        ctx.Report.RuntimeMissingSupport (loc, "AssemblyVersionAttribute");
 
1098
                }
 
1099
        }
 
1100
 
 
1101
        abstract class AssemblyReferencesLoader<T>
 
1102
        {
 
1103
                protected readonly CompilerContext compiler;
 
1104
 
 
1105
                protected readonly List<string> paths;
 
1106
 
 
1107
                public AssemblyReferencesLoader (CompilerContext compiler)
 
1108
                {
 
1109
                        this.compiler = compiler;
 
1110
 
 
1111
                        paths = new List<string> ();
 
1112
                        paths.Add (Directory.GetCurrentDirectory ());
 
1113
                        paths.AddRange (compiler.Settings.ReferencesLookupPaths);
 
1114
                }
 
1115
 
 
1116
                public abstract bool HasObjectType (T assembly);
 
1117
                protected abstract string[] GetDefaultReferences ();
 
1118
                public abstract T LoadAssemblyFile (string fileName, bool isImplicitReference);
 
1119
                public abstract void LoadReferences (ModuleContainer module);
 
1120
 
 
1121
                protected void Error_FileNotFound (string fileName)
 
1122
                {
 
1123
                        compiler.Report.Error (6, "Metadata file `{0}' could not be found", fileName);
 
1124
                }
 
1125
 
 
1126
                protected void Error_FileCorrupted (string fileName)
 
1127
                {
 
1128
                        compiler.Report.Error (9, "Metadata file `{0}' does not contain valid metadata", fileName);
 
1129
                }
 
1130
 
 
1131
                protected void Error_AssemblyIsModule (string fileName)
 
1132
                {
 
1133
                        compiler.Report.Error (1509,
 
1134
                                "Referenced assembly file `{0}' is a module. Consider using `-addmodule' option to add the module",
 
1135
                                fileName);
 
1136
                }
 
1137
 
 
1138
                protected void Error_ModuleIsAssembly (string fileName)
 
1139
                {
 
1140
                        compiler.Report.Error (1542,
 
1141
                                "Added module file `{0}' is an assembly. Consider using `-r' option to reference the file",
 
1142
                                fileName);
 
1143
                }
 
1144
 
 
1145
                protected void LoadReferencesCore (ModuleContainer module, out T corlib_assembly, out List<Tuple<RootNamespace, T>> loaded)
 
1146
                {
 
1147
                        compiler.TimeReporter.Start (TimeReporter.TimerType.ReferencesLoading);
 
1148
 
 
1149
                        loaded = new List<Tuple<RootNamespace, T>> ();
 
1150
 
 
1151
                        //
 
1152
                        // Load mscorlib.dll as the first
 
1153
                        //
 
1154
                        if (module.Compiler.Settings.StdLib) {
 
1155
                                corlib_assembly = LoadAssemblyFile ("mscorlib.dll", true);
 
1156
                        } else {
 
1157
                                corlib_assembly = default (T);
 
1158
                        }
 
1159
 
 
1160
                        T a;
 
1161
                        foreach (string r in module.Compiler.Settings.AssemblyReferences) {
 
1162
                                a = LoadAssemblyFile (r, false);
 
1163
                                if (a == null || EqualityComparer<T>.Default.Equals (a, corlib_assembly))
 
1164
                                        continue;
 
1165
 
 
1166
                                var key = Tuple.Create (module.GlobalRootNamespace, a);
 
1167
                                if (loaded.Contains (key))
 
1168
                                        continue;
 
1169
 
 
1170
                                // A corlib assembly is the first assembly which contains System.Object
 
1171
                                if (corlib_assembly == null && HasObjectType (a)) {
 
1172
                                        corlib_assembly = a;
 
1173
                                        continue;
 
1174
                                }
 
1175
 
 
1176
                                loaded.Add (key);
 
1177
                        }
 
1178
 
 
1179
                        foreach (var entry in module.Compiler.Settings.AssemblyReferencesAliases) {
 
1180
                                a = LoadAssemblyFile (entry.Item2, false);
 
1181
                                if (a == null)
 
1182
                                        continue;
 
1183
 
 
1184
                                var key = Tuple.Create (module.CreateRootNamespace (entry.Item1), a);
 
1185
                                if (loaded.Contains (key))
 
1186
                                        continue;
 
1187
 
 
1188
                                loaded.Add (key);
 
1189
                        }
 
1190
 
 
1191
                        if (compiler.Settings.LoadDefaultReferences) {
 
1192
                                foreach (string r in GetDefaultReferences ()) {
 
1193
                                        a = LoadAssemblyFile (r, true);
 
1194
                                        if (a == null)
 
1195
                                                continue;
 
1196
 
 
1197
                                        var key = Tuple.Create (module.GlobalRootNamespace, a);
 
1198
                                        if (loaded.Contains (key))
 
1199
                                                continue;
 
1200
 
 
1201
                                        loaded.Add (key);
 
1202
                                }
 
1203
                        }
 
1204
 
 
1205
                        compiler.TimeReporter.Stop (TimeReporter.TimerType.ReferencesLoading);
 
1206
                }
 
1207
        }
 
1208
}