| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Core.Link |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Data.Symbols; |
| 10 | using WixToolset.Extensibility.Services; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// Resolves all the simple references in a section. |
| 14 | /// </summary> |
| 15 | internal class ResolveReferencesCommand |
| 16 | { |
| 17 | private readonly IntermediateSection entrySection; |
| 18 | private readonly IDictionary<string, SymbolWithSection> symbolsWithSections; |
| 19 | private HashSet<SymbolWithSection> referencedSymbols; |
| 20 | private HashSet<IntermediateSection> resolvedSections; |
| 21 | |
| 22 | public ResolveReferencesCommand(IMessaging messaging, IntermediateSection entrySection, IDictionary<string, SymbolWithSection> symbolsWithSections) |
| 23 | { |
| 24 | this.Messaging = messaging; |
| 25 | this.entrySection = entrySection; |
| 26 | this.symbolsWithSections = symbolsWithSections; |
| 27 | this.BuildingMergeModule = (SectionType.Module == entrySection.Type); |
| 28 | } |
| 29 | |
| 30 | public IReadOnlyCollection<SymbolWithSection> ReferencedSymbolWithSections => this.referencedSymbols; |
| 31 | |
| 32 | public ISet<IntermediateSection> ResolvedSections => this.resolvedSections; |
| 33 | |
| 34 | private bool BuildingMergeModule { get; } |
| 35 | |
| 36 | private IMessaging Messaging { get; } |
| 37 | |
| 38 | /// <summary> |
| 39 | /// Resolves all the simple references in a section. |
| 40 | /// </summary> |
| 41 | public void Execute() |
| 42 | { |
| 43 | this.resolvedSections = new HashSet<IntermediateSection>(); |
| 44 | this.referencedSymbols = new HashSet<SymbolWithSection>(); |
| 45 | |
| 46 | this.RecursivelyResolveReferences(this.entrySection); |
| 47 | } |
| 48 | |
| 49 | /// <summary> |
| 50 | /// Recursive helper function to resolve all references of passed in section. |
| 51 | /// </summary> |
| 52 | /// <param name="section">Section with references to resolve.</param> |
| 53 | /// <remarks>Note: recursive function.</remarks> |
| 54 | private void RecursivelyResolveReferences(IntermediateSection section) |
| 55 | { |
| 56 | // If we already resolved this section, move on to the next. |
| 57 | if (!this.resolvedSections.Add(section)) |
| 58 | { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // Process all of the references contained in this section using the collection of |
| 63 | // symbols provided. Then recursively call this method to process the |
| 64 | // located symbol's section. All in all this is a very simple depth-first |
| 65 | // search of the references per-section. |
| 66 | foreach (var reference in section.Symbols.OfType<WixSimpleReferenceSymbol>()) |
| 67 | { |
| 68 | // If we're building a Merge Module, ignore all references to the Media table |
| 69 | // because Merge Modules don't have Media tables. |
| 70 | if (this.BuildingMergeModule && reference.Table == "Media") |
| 71 | { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | // See if the symbol (and any of its duplicates) are appropriately accessible. |
| 76 | if (this.symbolsWithSections.TryGetValue(reference.SymbolicName, out var symbolWithSection)) |
| 77 | { |
| 78 | var accessible = this.DetermineAccessibleSymbols(section, symbolWithSection); |
| 79 | |
| 80 | if (accessible.Count == 1) |
| 81 | { |
| 82 | var accessibleSymbol = accessible[0]; |
| 83 | |
| 84 | accessibleSymbol.AddDirectReference(reference); |
| 85 | |
| 86 | if (this.referencedSymbols.Add(accessibleSymbol) && null != accessibleSymbol.Section) |
| 87 | { |
| 88 | this.RecursivelyResolveReferences(accessibleSymbol.Section); |
| 89 | } |
| 90 | } |
| 91 | else if (accessible.Count == 0) |
| 92 | { |
| 93 | this.Messaging.Write(ErrorMessages.UnresolvedReference(reference.SourceLineNumbers, reference.SymbolicName, symbolWithSection.Access)); |
| 94 | } |
| 95 | else // multiple accessible symbols referenced creates conflicting symbols. |
| 96 | { |
| 97 | // Remember the direct reference to the symbol for the error reporting later, |
| 98 | // but do NOT continue resolving references found in these conflicting symbols. |
| 99 | foreach (var conflict in accessible) |
| 100 | { |
| 101 | // This should NEVER happen. |
| 102 | if (!conflict.PossiblyConflicts.Any()) |
| 103 | { |
| 104 | throw new InvalidOperationException("If a reference can reference multiple symbols, those symbols MUST have already been recognized as possible conflicts."); |
| 105 | } |
| 106 | |
| 107 | conflict.AddDirectReference(reference); |
| 108 | |
| 109 | this.referencedSymbols.Add(conflict); |
| 110 | |
| 111 | if (conflict.Section != null) |
| 112 | { |
| 113 | this.resolvedSections.Add(conflict.Section); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | this.Messaging.Write(ErrorMessages.UnresolvedReference(reference.SourceLineNumbers, reference.SymbolicName)); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// <summary> |
| 126 | /// Determine if the symbol and any of its duplicates are accessbile by the referencing section. |
| 127 | /// </summary> |
| 128 | /// <param name="referencingSection">Section referencing the symbol.</param> |
| 129 | /// <param name="symbolWithSection">Symbol being referenced.</param> |
| 130 | /// <returns>List of symbols accessible by referencing section.</returns> |
| 131 | private List<SymbolWithSection> DetermineAccessibleSymbols(IntermediateSection referencingSection, SymbolWithSection symbolWithSection) |
| 132 | { |
| 133 | var accessibleSymbols = new List<SymbolWithSection>(); |
| 134 | List<SymbolWithSection> virtualSymbols = null; |
| 135 | |
| 136 | if (this.AccessibleSymbol(referencingSection, symbolWithSection)) |
| 137 | { |
| 138 | AddSymbolToCorrectCollection(symbolWithSection, accessibleSymbols, ref virtualSymbols); |
| 139 | } |
| 140 | |
| 141 | foreach (var dupe in symbolWithSection.PossiblyConflicts) |
| 142 | { |
| 143 | if (this.AccessibleSymbol(referencingSection, dupe)) |
| 144 | { |
| 145 | AddSymbolToCorrectCollection(dupe, accessibleSymbols, ref virtualSymbols); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // If a virtual symbol is accessible we have some extra work to do. |
| 150 | if (virtualSymbols != null) |
| 151 | { |
| 152 | // If there are multiple virtual symbols accessible that's an error case so add them all and |
| 153 | // they'll be reported as conflicts later. |
| 154 | if (virtualSymbols.Count > 1) |
| 155 | { |
| 156 | accessibleSymbols.AddRange(virtualSymbols); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | var overrode = false; |
| 161 | |
| 162 | // If there are any override symbols, skip adding the virtual symbols because they are "overridden". |
| 163 | foreach (var overrideSymbol in accessibleSymbols.Where(symbol => symbol.Access == AccessModifier.Override)) |
| 164 | { |
| 165 | overrideSymbol.OverrideVirtualSymbol(virtualSymbols[0]); |
| 166 | |
| 167 | overrode = true; |
| 168 | } |
| 169 | |
| 170 | // If there are no overriding symbols, add the virtual symbol (there is only one but we'll add the collection just in case) |
| 171 | // so it will be reported as a conflict later. |
| 172 | if (!overrode) |
| 173 | { |
| 174 | accessibleSymbols.AddRange(virtualSymbols); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return accessibleSymbols; |
| 180 | } |
| 181 | |
| 182 | /// <summary> |
| 183 | /// Add a symbol to the correct collection based on its access. |
| 184 | /// </summary> |
| 185 | /// <param name="symbolWithSection"></param> |
| 186 | /// <param name="accessibleSymbols">Collection of non-virtual symbol that was accessible.</param> |
| 187 | /// <param name="virtualSymbols">Collection of virtual symbol that was accessible</param> |
| 188 | private static void AddSymbolToCorrectCollection(SymbolWithSection symbolWithSection, List<SymbolWithSection> accessibleSymbols, ref List<SymbolWithSection> virtualSymbols) |
| 189 | { |
| 190 | if (symbolWithSection.Access == AccessModifier.Virtual) |
| 191 | { |
| 192 | if (virtualSymbols == null) |
| 193 | { |
| 194 | virtualSymbols = new List<SymbolWithSection>(); |
| 195 | } |
| 196 | |
| 197 | virtualSymbols.Add(symbolWithSection); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | accessibleSymbols.Add(symbolWithSection); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// <summary> |
| 206 | /// Determine if a single symbol is accessible by the referencing section. |
| 207 | /// </summary> |
| 208 | /// <param name="referencingSection">Section referencing the symbol.</param> |
| 209 | /// <param name="symbolWithSection">Symbol being referenced.</param> |
| 210 | /// <returns>True if symbol is accessible.</returns> |
| 211 | private bool AccessibleSymbol(IntermediateSection referencingSection, SymbolWithSection symbolWithSection) |
| 212 | { |
| 213 | switch (symbolWithSection.Access) |
| 214 | { |
| 215 | case AccessModifier.Global: |
| 216 | case AccessModifier.Virtual: |
| 217 | case AccessModifier.Override: |
| 218 | return true; |
| 219 | case AccessModifier.Library: |
| 220 | return symbolWithSection.Section.CompilationId == referencingSection.CompilationId || (null != symbolWithSection.Section.LibraryId && symbolWithSection.Section.LibraryId == referencingSection.LibraryId); |
| 221 | case AccessModifier.File: |
| 222 | return symbolWithSection.Section.CompilationId == referencingSection.CompilationId; |
| 223 | case AccessModifier.Section: |
| 224 | return referencingSection == symbolWithSection.Section; |
| 225 | default: |
| 226 | throw new ArgumentOutOfRangeException(nameof(symbolWithSection.Access)); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |