1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System;
6 using System.Collections.Generic;
7 using ILCompiler.Metadata;
8 using Internal.TypeSystem;
9 
10 namespace MetadataTransformTests
11 {
12     /// <summary>
13     /// Represents a multifile compilation policy. The list of modules that are to become
14     /// part of the metadata blob is passed as an argument to the constructor.
15     /// Supports one to one mapping (if modules.Length == 1), or a many to one mapping
16     /// (similar to the SharedAssembly concept in .NET Native for UWP).
17     /// </summary>
18     struct MultifileMetadataPolicy : IMetadataPolicy
19     {
20         ExplicitScopeAssemblyPolicyMixin _explicitScopePolicyMixin;
21         HashSet<ModuleDesc> _modules;
22 
MultifileMetadataPolicyMetadataTransformTests.MultifileMetadataPolicy23         public MultifileMetadataPolicy(params ModuleDesc[] modules)
24         {
25             _modules = new HashSet<ModuleDesc>(modules);
26             _explicitScopePolicyMixin = new ExplicitScopeAssemblyPolicyMixin();
27         }
28 
GeneratesMetadataMetadataTransformTests.MultifileMetadataPolicy29         public bool GeneratesMetadata(MethodDesc methodDef)
30         {
31             return GeneratesMetadata((MetadataType)methodDef.OwningType);
32         }
33 
GeneratesMetadataMetadataTransformTests.MultifileMetadataPolicy34         public bool GeneratesMetadata(FieldDesc fieldDef)
35         {
36             return GeneratesMetadata((MetadataType)fieldDef.OwningType);
37         }
38 
GeneratesMetadataMetadataTransformTests.MultifileMetadataPolicy39         public bool GeneratesMetadata(MetadataType typeDef)
40         {
41             return _modules.Contains(typeDef.Module);
42         }
43 
IsBlockedMetadataTransformTests.MultifileMetadataPolicy44         public bool IsBlocked(MetadataType typeDef)
45         {
46             if (typeDef.Name == "ICastable")
47                 return true;
48 
49             if (typeDef.HasCustomAttribute("System.Runtime.CompilerServices", "__BlockReflectionAttribute"))
50                 return true;
51 
52             return false;
53         }
54 
IsBlockedMetadataTransformTests.MultifileMetadataPolicy55         public bool IsBlocked(MethodDesc method)
56         {
57             return IsBlocked((MetadataType)method.OwningType);
58         }
59 
GetModuleOfTypeMetadataTransformTests.MultifileMetadataPolicy60         public ModuleDesc GetModuleOfType(MetadataType typeDef)
61         {
62             return _explicitScopePolicyMixin.GetModuleOfType(typeDef);
63         }
64     }
65 }
66