|
| 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.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | +using Mono.Cecil; |
| 9 | +using Mono.Linker.Steps; |
| 10 | + |
| 11 | +namespace Mono.Linker.Dataflow |
| 12 | +{ |
| 13 | + class DynamicallyAccessedMembersTypeHierarchy |
| 14 | + { |
| 15 | + readonly LinkContext _context; |
| 16 | + readonly MarkStep _markStep; |
| 17 | + |
| 18 | + // Cache of DynamicallyAccessedMembers annotations applied to types and their hierarchies |
| 19 | + // Values |
| 20 | + // annotation - the aggregated annotation value from the entire base and interface hierarchy of the given type |
| 21 | + // If the type has a base class with annotation a1 and an interface with annotation a2, the stored |
| 22 | + // annotation is a1 | a2. |
| 23 | + // applied - set to true once the annotation was applied to the type |
| 24 | + // This only happens once the right reflection pattern is found. |
| 25 | + // If a new type is being marked and one of its base types/interface has the applied set to true |
| 26 | + // the new type will apply its annotation and will also set its applied to true. |
| 27 | + // Non-interface types |
| 28 | + // - Only marked types with non-empty annotation are put into the cache |
| 29 | + // - Non-marked types are not stored in the cache |
| 30 | + // - Marked types which are not in the cache don't have any annotation |
| 31 | + // Interface types |
| 32 | + // - All interface types accessible from marked types are stored in the cache |
| 33 | + // - If the interface type doesn't have annotation the value None is stored here |
| 34 | + // |
| 35 | + // It's not possible to use the marking as a filter for interfaces in the cache |
| 36 | + // because interfaces are marked late and in effectively random order. |
| 37 | + // For this cache to be effective we need to be able to fill it for all base types and interfaces |
| 38 | + // of a type which is currently being marked - at which point the interfaces are not yet marked. |
| 39 | + readonly Dictionary<TypeDefinition, (DynamicallyAccessedMemberTypes annotation, bool applied)> _typesInDynamicallyAccessedMembersHierarchy; |
| 40 | + |
| 41 | + public DynamicallyAccessedMembersTypeHierarchy (LinkContext context, MarkStep markStep) |
| 42 | + { |
| 43 | + _context = context; |
| 44 | + _markStep = markStep; |
| 45 | + _typesInDynamicallyAccessedMembersHierarchy = new Dictionary<TypeDefinition, (DynamicallyAccessedMemberTypes, bool)> (); |
| 46 | + } |
| 47 | + |
| 48 | + public (DynamicallyAccessedMemberTypes annotation, bool applied) ProcessMarkedTypeForDynamicallyAccessedMembersHierarchy (TypeDefinition type) |
| 49 | + { |
| 50 | + // Non-interfaces must be marked already |
| 51 | + Debug.Assert (type.IsInterface || _context.Annotations.IsMarked (type)); |
| 52 | + |
| 53 | + DynamicallyAccessedMemberTypes annotation = _context.Annotations.FlowAnnotations.GetTypeAnnotation (type); |
| 54 | + bool apply = false; |
| 55 | + |
| 56 | + // We'll use the cache also as a way to detect and avoid recursion |
| 57 | + // There's no possiblity to have recursion among base types, so only do this for interfaces |
| 58 | + if (type.IsInterface) { |
| 59 | + if (_typesInDynamicallyAccessedMembersHierarchy.TryGetValue (type, out var existingValue)) |
| 60 | + return existingValue; |
| 61 | + |
| 62 | + _typesInDynamicallyAccessedMembersHierarchy.Add (type, (annotation, false)); |
| 63 | + } |
| 64 | + |
| 65 | + // Base should already be marked (since we're marking its derived type now) |
| 66 | + // so we should already have its cached values filled. |
| 67 | + TypeDefinition baseType = type.BaseType?.Resolve (); |
| 68 | + Debug.Assert (baseType == null || _context.Annotations.IsMarked (baseType)); |
| 69 | + if (baseType != null && _typesInDynamicallyAccessedMembersHierarchy.TryGetValue (baseType, out var baseValue)) { |
| 70 | + annotation |= baseValue.annotation; |
| 71 | + apply |= baseValue.applied; |
| 72 | + } |
| 73 | + |
| 74 | + // For the purposes of the DynamicallyAccessedMembers type hierarchies |
| 75 | + // we consider interfaces of marked types to be also "marked" in that |
| 76 | + // their annotations will be applied to the type regardless if later on |
| 77 | + // we decide to remove the interface. This is to keep the complexity of the implementation |
| 78 | + // relatively low. In the future it could be possibly optimized. |
| 79 | + if (type.HasInterfaces) { |
| 80 | + foreach (InterfaceImplementation iface in type.Interfaces) { |
| 81 | + var interfaceType = iface.InterfaceType.Resolve (); |
| 82 | + if (interfaceType != null) { |
| 83 | + var interfaceValue = ProcessMarkedTypeForDynamicallyAccessedMembersHierarchy (interfaceType); |
| 84 | + annotation |= interfaceValue.annotation; |
| 85 | + apply |= interfaceValue.applied; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + Debug.Assert (!apply || annotation != DynamicallyAccessedMemberTypes.None); |
| 91 | + |
| 92 | + if (apply) { |
| 93 | + // One of the base/interface types is already marked as having the annotation applied |
| 94 | + // so we need to apply the annotation to this type as well |
| 95 | + var reflectionMethodBodyScanner = new ReflectionMethodBodyScanner (_context, _markStep); |
| 96 | + var reflectionPatternContext = new ReflectionPatternContext (_context, true, type, type); |
| 97 | + reflectionMethodBodyScanner.ApplyDynamicallyAccessedMembersToType (ref reflectionPatternContext, type, annotation); |
| 98 | + reflectionPatternContext.Dispose (); |
| 99 | + } |
| 100 | + |
| 101 | + // Store the results in the cache |
| 102 | + // Don't store empty annotations for non-interface types - we can use the presence of the row |
| 103 | + // in the cache as indication of it instead. |
| 104 | + // This doesn't work for interfaces, since we can't rely on them being marked (and thus have the cache |
| 105 | + // already filled), so we need to always store the row (even if empty) for interfaces. |
| 106 | + if (annotation != DynamicallyAccessedMemberTypes.None || type.IsInterface) { |
| 107 | + _typesInDynamicallyAccessedMembersHierarchy[type] = (annotation, apply); |
| 108 | + } |
| 109 | + |
| 110 | + return (annotation, apply); |
| 111 | + } |
| 112 | + |
| 113 | + public DynamicallyAccessedMemberTypes ApplyDynamicallyAccessedMembersToTypeHierarchy ( |
| 114 | + ReflectionMethodBodyScanner reflectionMethodBodyScanner, |
| 115 | + ref ReflectionPatternContext reflectionPatternContext, |
| 116 | + TypeDefinition type) |
| 117 | + { |
| 118 | + Debug.Assert (_context.Annotations.IsMarked (type)); |
| 119 | + |
| 120 | + // The type should be in our cache already |
| 121 | + (var annotation, var applied) = GetCachedInfoForTypeInHierarchy (type); |
| 122 | + |
| 123 | + // If the annotation was already applied to this type, there's no reason to repeat the operation, the result will |
| 124 | + // be no change. |
| 125 | + if (applied || annotation == DynamicallyAccessedMemberTypes.None) |
| 126 | + return annotation; |
| 127 | + |
| 128 | + // Apply the effective annotation for the type |
| 129 | + reflectionMethodBodyScanner.ApplyDynamicallyAccessedMembersToType (ref reflectionPatternContext, type, annotation); |
| 130 | + |
| 131 | + // Mark it as applied in the cache |
| 132 | + _typesInDynamicallyAccessedMembersHierarchy[type] = (annotation, true); |
| 133 | + |
| 134 | + // Propagate the newly applied annotation to all derived/implementation types |
| 135 | + // Since we don't have a data structure which would allow us to enumerate all derived/implementation types |
| 136 | + // walk all of the types in the cache. These are good candidates as types not in the cache don't apply. |
| 137 | + foreach (var candidate in _typesInDynamicallyAccessedMembersHierarchy) { |
| 138 | + if (candidate.Value.annotation == DynamicallyAccessedMemberTypes.None) |
| 139 | + continue; |
| 140 | + |
| 141 | + ApplyDynamicallyAccessedMembersToTypeHierarchyInner (reflectionMethodBodyScanner, ref reflectionPatternContext, candidate.Key); |
| 142 | + } |
| 143 | + |
| 144 | + return annotation; |
| 145 | + } |
| 146 | + |
| 147 | + bool ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( |
| 148 | + ReflectionMethodBodyScanner reflectionMethodBodyScanner, |
| 149 | + ref ReflectionPatternContext reflectionPatternContext, |
| 150 | + TypeDefinition type) |
| 151 | + { |
| 152 | + (var annotation, var applied) = GetCachedInfoForTypeInHierarchy (type); |
| 153 | + |
| 154 | + if (annotation == DynamicallyAccessedMemberTypes.None) |
| 155 | + return false; |
| 156 | + |
| 157 | + if (applied) |
| 158 | + return true; |
| 159 | + |
| 160 | + TypeDefinition baseType = type.BaseType?.Resolve (); |
| 161 | + if (baseType != null) |
| 162 | + applied = ApplyDynamicallyAccessedMembersToTypeHierarchyInner (reflectionMethodBodyScanner, ref reflectionPatternContext, baseType); |
| 163 | + |
| 164 | + if (!applied && type.HasInterfaces) { |
| 165 | + foreach (InterfaceImplementation iface in type.Interfaces) { |
| 166 | + var interfaceType = iface.InterfaceType.Resolve (); |
| 167 | + if (interfaceType != null) { |
| 168 | + if (ApplyDynamicallyAccessedMembersToTypeHierarchyInner (reflectionMethodBodyScanner, ref reflectionPatternContext, interfaceType)) { |
| 169 | + applied = true; |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (applied) { |
| 177 | + reflectionMethodBodyScanner.ApplyDynamicallyAccessedMembersToType (ref reflectionPatternContext, type, annotation); |
| 178 | + _typesInDynamicallyAccessedMembersHierarchy[type] = (annotation, true); |
| 179 | + } |
| 180 | + |
| 181 | + return applied; |
| 182 | + } |
| 183 | + |
| 184 | + (DynamicallyAccessedMemberTypes annotation, bool applied) GetCachedInfoForTypeInHierarchy (TypeDefinition type) |
| 185 | + { |
| 186 | + Debug.Assert (type.IsInterface || _context.Annotations.IsMarked (type)); |
| 187 | + |
| 188 | + // The type should be in our cache already |
| 189 | + if (!_typesInDynamicallyAccessedMembersHierarchy.TryGetValue (type, out var existingValue)) { |
| 190 | + // If it's not in the cache it should be a non-interface type in which case it means there were no annotations |
| 191 | + Debug.Assert (!type.IsInterface); |
| 192 | + return (DynamicallyAccessedMemberTypes.None, false); |
| 193 | + } |
| 194 | + |
| 195 | + return existingValue; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments