| | | 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 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | |
| | | 9 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 10 | | { |
| | | 11 | | internal sealed class NamespaceCache |
| | | 12 | | { |
| | | 13 | | private readonly MetadataReader _metadataReader; |
| | 0 | 14 | | private readonly object _namespaceTableAndListLock = new object(); |
| | | 15 | | private Dictionary<NamespaceDefinitionHandle, NamespaceData>? _namespaceTable; |
| | | 16 | | private NamespaceData? _rootNamespace; |
| | | 17 | | private uint _virtualNamespaceCounter; |
| | | 18 | | |
| | 0 | 19 | | internal NamespaceCache(MetadataReader reader) |
| | 0 | 20 | | { |
| | 0 | 21 | | Debug.Assert(reader != null); |
| | 0 | 22 | | _metadataReader = reader; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Returns whether the namespaceTable has been created. If it hasn't, calling a GetXXX method |
| | | 27 | | /// on this will probably have a very high amount of overhead. |
| | | 28 | | /// </summary> |
| | | 29 | | internal bool CacheIsRealized |
| | | 30 | | { |
| | 0 | 31 | | get { return _namespaceTable != null; } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal string GetFullName(NamespaceDefinitionHandle handle) |
| | 0 | 35 | | { |
| | 0 | 36 | | Debug.Assert(!handle.HasFullName); // we should not hit the cache in this case. |
| | 0 | 37 | | NamespaceData data = GetNamespaceData(handle); |
| | 0 | 38 | | return data.FullName; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | internal NamespaceData GetRootNamespace() |
| | 0 | 42 | | { |
| | 0 | 43 | | EnsureNamespaceTableIsPopulated(); |
| | 0 | 44 | | Debug.Assert(_rootNamespace != null); |
| | 0 | 45 | | return _rootNamespace; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | internal NamespaceData GetNamespaceData(NamespaceDefinitionHandle handle) |
| | 0 | 49 | | { |
| | 0 | 50 | | EnsureNamespaceTableIsPopulated(); |
| | | 51 | | NamespaceData? result; |
| | 0 | 52 | | if (!_namespaceTable!.TryGetValue(handle, out result)) |
| | 0 | 53 | | { |
| | 0 | 54 | | Throw.InvalidHandle(); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | return result; |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// This will return a StringHandle for the simple name of a namespace name at the given segment index. |
| | | 62 | | /// If no segment index is passed explicitly or the "segment" index is greater than or equal to the number |
| | | 63 | | /// of segments, then the last segment is used. "Segment" in this context refers to part of a namespace |
| | | 64 | | /// name between dots. |
| | | 65 | | /// |
| | | 66 | | /// Example: Given a NamespaceDefinitionHandle to "System.Collections.Generic.Test" called 'handle': |
| | | 67 | | /// |
| | | 68 | | /// reader.GetString(GetSimpleName(handle)) == "Test" |
| | | 69 | | /// reader.GetString(GetSimpleName(handle, 0)) == "System" |
| | | 70 | | /// reader.GetString(GetSimpleName(handle, 1)) == "Collections" |
| | | 71 | | /// reader.GetString(GetSimpleName(handle, 2)) == "Generic" |
| | | 72 | | /// reader.GetString(GetSimpleName(handle, 3)) == "Test" |
| | | 73 | | /// reader.GetString(GetSimpleName(handle, 1000)) == "Test" |
| | | 74 | | /// </summary> |
| | | 75 | | private StringHandle GetSimpleName(NamespaceDefinitionHandle fullNamespaceHandle, int segmentIndex = int.MaxValu |
| | 0 | 76 | | { |
| | 0 | 77 | | StringHandle handleContainingSegment = fullNamespaceHandle.GetFullName(); |
| | 0 | 78 | | Debug.Assert(!handleContainingSegment.IsVirtual); |
| | | 79 | | |
| | 0 | 80 | | int lastFoundIndex = fullNamespaceHandle.GetHeapOffset() - 1; |
| | 0 | 81 | | int currentSegment = 0; |
| | 0 | 82 | | while (currentSegment < segmentIndex) |
| | 0 | 83 | | { |
| | 0 | 84 | | int currentIndex = _metadataReader.StringHeap.IndexOfRaw(lastFoundIndex + 1, '.'); |
| | 0 | 85 | | if (currentIndex == -1) |
| | 0 | 86 | | { |
| | 0 | 87 | | break; |
| | | 88 | | } |
| | 0 | 89 | | lastFoundIndex = currentIndex; |
| | 0 | 90 | | ++currentSegment; |
| | 0 | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | Debug.Assert(lastFoundIndex >= 0 || currentSegment == 0); |
| | | 94 | | |
| | | 95 | | // + 1 because lastFoundIndex will either "point" to a '.', or will be -1. Either way, |
| | | 96 | | // we want the next char. |
| | 0 | 97 | | int resultIndex = lastFoundIndex + 1; |
| | 0 | 98 | | return StringHandle.FromOffset(resultIndex).WithDotTermination(); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Two distinct namespace handles represent the same namespace if their full names are the same. This |
| | | 103 | | /// method merges builders corresponding to such namespace handles. |
| | | 104 | | /// </summary> |
| | | 105 | | private void PopulateNamespaceTable() |
| | 0 | 106 | | { |
| | 0 | 107 | | lock (_namespaceTableAndListLock) |
| | 0 | 108 | | { |
| | 0 | 109 | | if (_namespaceTable != null) |
| | 0 | 110 | | { |
| | 0 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | var namespaceBuilderTable = new Dictionary<NamespaceDefinitionHandle, NamespaceDataBuilder>(); |
| | | 115 | | |
| | | 116 | | // Make sure to add entry for root namespace. The root namespace is special in that even |
| | | 117 | | // though it might not have types of its own it always has an equivalent representation |
| | | 118 | | // as a nil handle and we don't want to handle it below as dot-terminated virtual namespace. |
| | | 119 | | // We use NamespaceDefinitionHandle.FromIndexOfFullName(0) instead of default(NamespaceDefinitionHandle) |
| | | 120 | | // that we never hand back a handle to the user that doesn't have a typeid as that prevents |
| | | 121 | | // round-trip conversion to Handle and back. (We may discover other handle aliases for the |
| | | 122 | | // root namespace (any nil/empty string will do), but we need this one to always be there. |
| | 0 | 123 | | NamespaceDefinitionHandle rootNamespace = NamespaceDefinitionHandle.FromFullNameOffset(0); |
| | 0 | 124 | | namespaceBuilderTable.Add( |
| | 0 | 125 | | rootNamespace, |
| | 0 | 126 | | new NamespaceDataBuilder( |
| | 0 | 127 | | rootNamespace, |
| | 0 | 128 | | rootNamespace.GetFullName(), |
| | 0 | 129 | | string.Empty)); |
| | | 130 | | |
| | 0 | 131 | | PopulateTableWithTypeDefinitions(namespaceBuilderTable); |
| | 0 | 132 | | PopulateTableWithExportedTypes(namespaceBuilderTable); |
| | | 133 | | |
| | | 134 | | Dictionary<string, NamespaceDataBuilder> stringTable; |
| | 0 | 135 | | MergeDuplicateNamespaces(namespaceBuilderTable, out stringTable); |
| | | 136 | | |
| | | 137 | | List<NamespaceDataBuilder>? virtualNamespaces; |
| | 0 | 138 | | ResolveParentChildRelationships(stringTable, out virtualNamespaces); |
| | | 139 | | |
| | 0 | 140 | | var namespaceTable = new Dictionary<NamespaceDefinitionHandle, NamespaceData>(); |
| | | 141 | | |
| | 0 | 142 | | foreach (var group in namespaceBuilderTable) |
| | 0 | 143 | | { |
| | | 144 | | // Freeze() caches the result, so any many-to-one relationships |
| | | 145 | | // between keys and values will be preserved and efficiently handled. |
| | 0 | 146 | | namespaceTable.Add(group.Key, group.Value.Freeze()); |
| | 0 | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | if (virtualNamespaces != null) |
| | 0 | 150 | | { |
| | 0 | 151 | | foreach (var virtualNamespace in virtualNamespaces) |
| | 0 | 152 | | { |
| | 0 | 153 | | namespaceTable.Add(virtualNamespace.Handle, virtualNamespace.Freeze()); |
| | 0 | 154 | | } |
| | 0 | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | _rootNamespace = namespaceTable[rootNamespace]; |
| | 0 | 158 | | _namespaceTable = namespaceTable; |
| | 0 | 159 | | } |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// This will take 'table' and merge all of the NamespaceData instances that point to the same |
| | | 164 | | /// namespace. It has to create 'stringTable' as an intermediate dictionary, so it will hand it |
| | | 165 | | /// back to the caller should the caller want to use it. |
| | | 166 | | /// </summary> |
| | | 167 | | private static void MergeDuplicateNamespaces(Dictionary<NamespaceDefinitionHandle, NamespaceDataBuilder> table, |
| | 0 | 168 | | { |
| | 0 | 169 | | var namespaces = new Dictionary<string, NamespaceDataBuilder>(); |
| | 0 | 170 | | List<KeyValuePair<NamespaceDefinitionHandle, NamespaceDataBuilder>>? remaps = null; |
| | 0 | 171 | | foreach (var group in table) |
| | 0 | 172 | | { |
| | 0 | 173 | | NamespaceDataBuilder data = group.Value; |
| | | 174 | | NamespaceDataBuilder? existingRecord; |
| | 0 | 175 | | if (namespaces.TryGetValue(data.FullName, out existingRecord)) |
| | 0 | 176 | | { |
| | | 177 | | // Children should not exist until the next step. |
| | 0 | 178 | | Debug.Assert(data.Namespaces!.Count == 0); |
| | 0 | 179 | | data.MergeInto(existingRecord); |
| | | 180 | | |
| | 0 | 181 | | remaps ??= new List<KeyValuePair<NamespaceDefinitionHandle, NamespaceDataBuilder>>(); |
| | 0 | 182 | | remaps.Add(new KeyValuePair<NamespaceDefinitionHandle, NamespaceDataBuilder>(group.Key, existingReco |
| | 0 | 183 | | } |
| | | 184 | | else |
| | 0 | 185 | | { |
| | 0 | 186 | | namespaces.Add(data.FullName, data); |
| | 0 | 187 | | } |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | // Needs to be done outside of foreach (var group in table) to avoid modifying the dictionary while foreach' |
| | 0 | 191 | | if (remaps != null) |
| | 0 | 192 | | { |
| | 0 | 193 | | foreach (var tuple in remaps) |
| | 0 | 194 | | { |
| | 0 | 195 | | table[tuple.Key] = tuple.Value; |
| | 0 | 196 | | } |
| | 0 | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | stringTable = namespaces; |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Creates a NamespaceDataBuilder instance that contains a synthesized NamespaceDefinitionHandle, |
| | | 204 | | /// as well as the name provided. |
| | | 205 | | /// </summary> |
| | | 206 | | private NamespaceDataBuilder SynthesizeNamespaceData(string fullName, NamespaceDefinitionHandle realChild) |
| | 0 | 207 | | { |
| | 0 | 208 | | Debug.Assert(realChild.HasFullName); |
| | | 209 | | |
| | 0 | 210 | | int numberOfSegments = fullName.AsSpan().Count('.'); |
| | | 211 | | |
| | 0 | 212 | | StringHandle simpleName = GetSimpleName(realChild, numberOfSegments); |
| | 0 | 213 | | var namespaceHandle = NamespaceDefinitionHandle.FromVirtualIndex(++_virtualNamespaceCounter); |
| | 0 | 214 | | return new NamespaceDataBuilder(namespaceHandle, simpleName, fullName); |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Quick convenience method that handles linking together child + parent |
| | | 219 | | /// </summary> |
| | | 220 | | private static void LinkChildDataToParentData(NamespaceDataBuilder child, NamespaceDataBuilder parent) |
| | 0 | 221 | | { |
| | 0 | 222 | | Debug.Assert(child != null && parent != null); |
| | 0 | 223 | | Debug.Assert(!child.Handle.IsNil); |
| | 0 | 224 | | child.Parent = parent.Handle; |
| | 0 | 225 | | parent.Namespaces!.Add(child.Handle); |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Links a child to its parent namespace. If the parent namespace doesn't exist, this will create a |
| | | 230 | | /// virtual one. This will automatically link any virtual namespaces it creates up to its parents. |
| | | 231 | | /// </summary> |
| | | 232 | | private void LinkChildToParentNamespace(Dictionary<string, NamespaceDataBuilder> existingNamespaces, |
| | | 233 | | NamespaceDataBuilder realChild, |
| | | 234 | | ref List<NamespaceDataBuilder>? virtualNamespaces) |
| | 0 | 235 | | { |
| | 0 | 236 | | Debug.Assert(realChild.Handle.HasFullName); |
| | 0 | 237 | | string childName = realChild.FullName; |
| | 0 | 238 | | var child = realChild; |
| | | 239 | | |
| | | 240 | | // The condition for this loop is very complex -- essentially, we keep going |
| | | 241 | | // until we: |
| | | 242 | | // A. Encounter the root namespace as 'child' |
| | | 243 | | // B. Find a preexisting namespace as 'parent' |
| | 0 | 244 | | while (true) |
| | 0 | 245 | | { |
| | 0 | 246 | | int lastIndex = childName.LastIndexOf('.'); |
| | | 247 | | string parentName; |
| | 0 | 248 | | if (lastIndex == -1) |
| | 0 | 249 | | { |
| | 0 | 250 | | if (childName.Length == 0) |
| | 0 | 251 | | { |
| | 0 | 252 | | return; |
| | | 253 | | } |
| | | 254 | | else |
| | 0 | 255 | | { |
| | 0 | 256 | | parentName = string.Empty; |
| | 0 | 257 | | } |
| | 0 | 258 | | } |
| | | 259 | | else |
| | 0 | 260 | | { |
| | 0 | 261 | | parentName = childName.Substring(0, lastIndex); |
| | 0 | 262 | | } |
| | | 263 | | |
| | | 264 | | NamespaceDataBuilder? parentData; |
| | 0 | 265 | | if (existingNamespaces.TryGetValue(parentName, out parentData)) |
| | 0 | 266 | | { |
| | 0 | 267 | | LinkChildDataToParentData(child, parentData); |
| | 0 | 268 | | return; |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | if (virtualNamespaces != null) |
| | 0 | 272 | | { |
| | 0 | 273 | | foreach (var data in virtualNamespaces) |
| | 0 | 274 | | { |
| | 0 | 275 | | if (data.FullName == parentName) |
| | 0 | 276 | | { |
| | 0 | 277 | | LinkChildDataToParentData(child, data); |
| | 0 | 278 | | return; |
| | | 279 | | } |
| | 0 | 280 | | } |
| | 0 | 281 | | } |
| | | 282 | | else |
| | 0 | 283 | | { |
| | 0 | 284 | | virtualNamespaces = new List<NamespaceDataBuilder>(); |
| | 0 | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | var virtualParent = SynthesizeNamespaceData(parentName, realChild.Handle); |
| | 0 | 288 | | LinkChildDataToParentData(child, virtualParent); |
| | 0 | 289 | | virtualNamespaces.Add(virtualParent); |
| | 0 | 290 | | childName = virtualParent.FullName; |
| | 0 | 291 | | child = virtualParent; |
| | 0 | 292 | | } |
| | 0 | 293 | | } |
| | | 294 | | |
| | | 295 | | /// <summary> |
| | | 296 | | /// This will link all parents/children in the given namespaces dictionary up to each other. |
| | | 297 | | /// |
| | | 298 | | /// In some cases, we need to synthesize namespaces that do not have any type definitions or forwarders |
| | | 299 | | /// of their own, but do have child namespaces. These are returned via the virtualNamespaces out |
| | | 300 | | /// parameter. |
| | | 301 | | /// </summary> |
| | | 302 | | private void ResolveParentChildRelationships(Dictionary<string, NamespaceDataBuilder> namespaces, out List<Names |
| | 0 | 303 | | { |
| | 0 | 304 | | virtualNamespaces = null; |
| | 0 | 305 | | foreach (var namespaceData in namespaces) |
| | 0 | 306 | | { |
| | 0 | 307 | | LinkChildToParentNamespace(namespaces, namespaceData.Value, ref virtualNamespaces); |
| | 0 | 308 | | } |
| | 0 | 309 | | } |
| | | 310 | | |
| | | 311 | | /// <summary> |
| | | 312 | | /// Loops through all type definitions in metadata, adding them to the given table |
| | | 313 | | /// </summary> |
| | | 314 | | private void PopulateTableWithTypeDefinitions(Dictionary<NamespaceDefinitionHandle, NamespaceDataBuilder> table) |
| | 0 | 315 | | { |
| | 0 | 316 | | Debug.Assert(table != null); |
| | | 317 | | |
| | 0 | 318 | | foreach (var typeHandle in _metadataReader.TypeDefinitions) |
| | 0 | 319 | | { |
| | 0 | 320 | | TypeDefinition type = _metadataReader.GetTypeDefinition(typeHandle); |
| | 0 | 321 | | if (type.Attributes.IsNested()) |
| | 0 | 322 | | { |
| | 0 | 323 | | continue; |
| | | 324 | | } |
| | | 325 | | |
| | 0 | 326 | | NamespaceDefinitionHandle namespaceHandle = _metadataReader.TypeDefTable.GetNamespaceDefinition(typeHand |
| | | 327 | | NamespaceDataBuilder? builder; |
| | 0 | 328 | | if (table.TryGetValue(namespaceHandle, out builder)) |
| | 0 | 329 | | { |
| | 0 | 330 | | builder.TypeDefinitions!.Add(typeHandle); |
| | 0 | 331 | | } |
| | | 332 | | else |
| | 0 | 333 | | { |
| | 0 | 334 | | StringHandle name = GetSimpleName(namespaceHandle); |
| | 0 | 335 | | string fullName = _metadataReader.GetString(namespaceHandle); |
| | 0 | 336 | | var newData = new NamespaceDataBuilder(namespaceHandle, name, fullName); |
| | 0 | 337 | | newData.TypeDefinitions!.Add(typeHandle); |
| | 0 | 338 | | table.Add(namespaceHandle, newData); |
| | 0 | 339 | | } |
| | 0 | 340 | | } |
| | 0 | 341 | | } |
| | | 342 | | |
| | | 343 | | /// <summary> |
| | | 344 | | /// Loops through all type forwarders in metadata, adding them to the given table |
| | | 345 | | /// </summary> |
| | | 346 | | private void PopulateTableWithExportedTypes(Dictionary<NamespaceDefinitionHandle, NamespaceDataBuilder> table) |
| | 0 | 347 | | { |
| | 0 | 348 | | Debug.Assert(table != null); |
| | | 349 | | |
| | 0 | 350 | | foreach (var exportedTypeHandle in _metadataReader.ExportedTypes) |
| | 0 | 351 | | { |
| | 0 | 352 | | ExportedType exportedType = _metadataReader.GetExportedType(exportedTypeHandle); |
| | 0 | 353 | | if (exportedType.Implementation.Kind == HandleKind.ExportedType) |
| | 0 | 354 | | { |
| | 0 | 355 | | continue; // skip nested exported types. |
| | | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | NamespaceDefinitionHandle namespaceHandle = exportedType.NamespaceDefinition; |
| | | 359 | | NamespaceDataBuilder? builder; |
| | 0 | 360 | | if (table.TryGetValue(namespaceHandle, out builder)) |
| | 0 | 361 | | { |
| | 0 | 362 | | builder.ExportedTypes!.Add(exportedTypeHandle); |
| | 0 | 363 | | } |
| | | 364 | | else |
| | 0 | 365 | | { |
| | 0 | 366 | | Debug.Assert(namespaceHandle.HasFullName); |
| | 0 | 367 | | StringHandle simpleName = GetSimpleName(namespaceHandle); |
| | 0 | 368 | | string fullName = _metadataReader.GetString(namespaceHandle); |
| | 0 | 369 | | var newData = new NamespaceDataBuilder(namespaceHandle, simpleName, fullName); |
| | 0 | 370 | | newData.ExportedTypes!.Add(exportedTypeHandle); |
| | 0 | 371 | | table.Add(namespaceHandle, newData); |
| | 0 | 372 | | } |
| | 0 | 373 | | } |
| | 0 | 374 | | } |
| | | 375 | | |
| | | 376 | | /// <summary> |
| | | 377 | | /// If the namespace table doesn't exist, populates it! |
| | | 378 | | /// </summary> |
| | | 379 | | private void EnsureNamespaceTableIsPopulated() |
| | 0 | 380 | | { |
| | | 381 | | // PERF: Branch will rarely be taken; do work in PopulateNamespaceList() so this can be inlined easily. |
| | 0 | 382 | | if (_namespaceTable == null) |
| | 0 | 383 | | { |
| | 0 | 384 | | PopulateNamespaceTable(); |
| | 0 | 385 | | } |
| | 0 | 386 | | Debug.Assert(_namespaceTable != null); |
| | 0 | 387 | | } |
| | | 388 | | |
| | | 389 | | /// <summary> |
| | | 390 | | /// An intermediate class used to build NamespaceData instances. This was created because we wanted to |
| | | 391 | | /// use ImmutableArrays in NamespaceData, but having ArrayBuilders and ImmutableArrays that served the |
| | | 392 | | /// same purpose in NamespaceData got ugly. With the current design of how we create our Namespace |
| | | 393 | | /// dictionary, this needs to be a class because we have a many-to-one mapping between NamespaceHandles |
| | | 394 | | /// and NamespaceData. So, the pointer semantics must be preserved. |
| | | 395 | | /// |
| | | 396 | | /// This class assumes that the builders will not be modified in any way after the first call to |
| | | 397 | | /// Freeze(). |
| | | 398 | | /// </summary> |
| | | 399 | | private sealed class NamespaceDataBuilder |
| | | 400 | | { |
| | | 401 | | public readonly NamespaceDefinitionHandle Handle; |
| | | 402 | | public readonly StringHandle Name; |
| | | 403 | | public readonly string FullName; |
| | | 404 | | public NamespaceDefinitionHandle Parent; |
| | | 405 | | public ImmutableArray<NamespaceDefinitionHandle>.Builder? Namespaces; |
| | | 406 | | public ImmutableArray<TypeDefinitionHandle>.Builder? TypeDefinitions; |
| | | 407 | | public ImmutableArray<ExportedTypeHandle>.Builder? ExportedTypes; |
| | | 408 | | |
| | | 409 | | private NamespaceData? _frozen; |
| | | 410 | | |
| | 0 | 411 | | public NamespaceDataBuilder(NamespaceDefinitionHandle handle, StringHandle name, string fullName) |
| | 0 | 412 | | { |
| | 0 | 413 | | Handle = handle; |
| | 0 | 414 | | Name = name; |
| | 0 | 415 | | FullName = fullName; |
| | 0 | 416 | | Namespaces = ImmutableArray.CreateBuilder<NamespaceDefinitionHandle>(); |
| | 0 | 417 | | TypeDefinitions = ImmutableArray.CreateBuilder<TypeDefinitionHandle>(); |
| | 0 | 418 | | ExportedTypes = ImmutableArray.CreateBuilder<ExportedTypeHandle>(); |
| | 0 | 419 | | } |
| | | 420 | | |
| | | 421 | | /// <summary> |
| | | 422 | | /// Returns a NamespaceData that represents this NamespaceDataBuilder instance. After calling |
| | | 423 | | /// this method, it is an error to use any methods or fields except Freeze() on the target |
| | | 424 | | /// NamespaceDataBuilder. |
| | | 425 | | /// </summary> |
| | | 426 | | public NamespaceData Freeze() |
| | 0 | 427 | | { |
| | | 428 | | // It is not an error to call this function multiple times. We cache the result |
| | | 429 | | // because it's immutable. |
| | 0 | 430 | | if (_frozen == null) |
| | 0 | 431 | | { |
| | 0 | 432 | | var namespaces = Namespaces!.ToImmutable(); |
| | 0 | 433 | | Namespaces = null; |
| | | 434 | | |
| | 0 | 435 | | var typeDefinitions = TypeDefinitions!.ToImmutable(); |
| | 0 | 436 | | TypeDefinitions = null; |
| | | 437 | | |
| | 0 | 438 | | var exportedTypes = ExportedTypes!.ToImmutable(); |
| | 0 | 439 | | ExportedTypes = null; |
| | | 440 | | |
| | 0 | 441 | | _frozen = new NamespaceData(Name, FullName, Parent, namespaces, typeDefinitions, exportedTypes); |
| | 0 | 442 | | } |
| | | 443 | | |
| | 0 | 444 | | return _frozen; |
| | 0 | 445 | | } |
| | | 446 | | |
| | | 447 | | public void MergeInto(NamespaceDataBuilder other) |
| | 0 | 448 | | { |
| | 0 | 449 | | Parent = default(NamespaceDefinitionHandle); |
| | 0 | 450 | | other.Namespaces!.AddRange(this.Namespaces!); |
| | 0 | 451 | | other.TypeDefinitions!.AddRange(this.TypeDefinitions!); |
| | 0 | 452 | | other.ExportedTypes!.AddRange(this.ExportedTypes!); |
| | 0 | 453 | | } |
| | | 454 | | } |
| | | 455 | | } |
| | | 456 | | } |
| | | 457 | | |