|
| 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.Concurrent; |
| 5 | +using System.Collections.Immutable; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 9 | +using Microsoft.CodeAnalysis.FindSymbols; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 12 | + |
| 13 | +namespace Silk.NET.SilkTouch.Mods.LocationTransformation; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Utilities for transforming <see cref="Location"/>s. |
| 17 | +/// </summary> |
| 18 | +public static class LocationTransformationUtils |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Finds all references to the specified symbols and applies the specified transformations to them. |
| 22 | + /// Transformations will be done in order. |
| 23 | + /// </summary> |
| 24 | + public static async Task ModifyAllReferencesAsync( |
| 25 | + IModContext ctx, |
| 26 | + ILogger logger, |
| 27 | + IEnumerable<ISymbol> symbols, |
| 28 | + IEnumerable<LocationTransformer> transformers, |
| 29 | + CancellationToken ct = default) |
| 30 | + { |
| 31 | + var symbolSet = symbols.ToList(); |
| 32 | + var transformersSet = transformers.ToList(); |
| 33 | + |
| 34 | + var project = ctx.SourceProject; |
| 35 | + if (project == null) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + var compilation = await project.GetCompilationAsync(ct); |
| 41 | + if (compilation == null) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // Find all locations where the symbols are referenced |
| 47 | + var locations = new List<Location>(); |
| 48 | + var documents = project.Documents.ToImmutableHashSet(); |
| 49 | + foreach (var symbol in symbolSet) |
| 50 | + { |
| 51 | + var references = await SymbolFinder.FindReferencesAsync(symbol, project.Solution, documents, ct); |
| 52 | + locations.AddRange(references.SelectMany(r => r.Locations).Select(rl => rl.Location)); |
| 53 | + } |
| 54 | + |
| 55 | + // var locations = new ConcurrentDictionary<Location, string>(); |
| 56 | + // // TODO this needs parallelisation config & be sensitive to the environment (future src generator form factor?) |
| 57 | + // await Parallel.ForEachAsync( |
| 58 | + // toRename, |
| 59 | + // ct, |
| 60 | + // async (tuple, _) => |
| 61 | + // { |
| 62 | + // // First, let's add all of the locations of the declaration identifiers. |
| 63 | + // var (symbol, newName) = tuple; |
| 64 | + // if (includeDeclarations) |
| 65 | + // { |
| 66 | + // foreach (var syntaxRef in symbol.DeclaringSyntaxReferences) |
| 67 | + // { |
| 68 | + // var identifierLocation = IdentifierLocation( |
| 69 | + // await syntaxRef.GetSyntaxAsync(ct) |
| 70 | + // ); |
| 71 | + // if (identifierLocation is not null) |
| 72 | + // { |
| 73 | + // locations.TryAdd(identifierLocation, newName); |
| 74 | + // } |
| 75 | + // } |
| 76 | + // } |
| 77 | + // |
| 78 | + // // Next, let's find all the references of the symbols. |
| 79 | + // var references = await SymbolFinder.FindReferencesAsync( |
| 80 | + // symbol, |
| 81 | + // ctx.SourceProject?.Solution |
| 82 | + // ?? throw new ArgumentException("SourceProject is null"), |
| 83 | + // ct |
| 84 | + // ); |
| 85 | + // |
| 86 | + // foreach (var referencedSymbol in references) |
| 87 | + // { |
| 88 | + // foreach (var referencedSymbolLocation in referencedSymbol.Locations) |
| 89 | + // { |
| 90 | + // if ( |
| 91 | + // !includeCandidateLocations |
| 92 | + // && ( |
| 93 | + // referencedSymbolLocation.IsCandidateLocation |
| 94 | + // || referencedSymbolLocation.IsImplicit |
| 95 | + // ) |
| 96 | + // ) |
| 97 | + // { |
| 98 | + // continue; |
| 99 | + // } |
| 100 | + // |
| 101 | + // locations.TryAdd(referencedSymbolLocation.Location, newName); |
| 102 | + // } |
| 103 | + // } |
| 104 | + // } |
| 105 | + // ); |
| 106 | + |
| 107 | + // Group the locations by source tree. This will be used to prevent accidentally overwriting changes. |
| 108 | + var locationsBySourcetree = locations.GroupBy(l => l.SourceTree); |
| 109 | + foreach (var group in locationsBySourcetree) |
| 110 | + { |
| 111 | + var syntaxTree = group.Key; |
| 112 | + if (syntaxTree == null) |
| 113 | + { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + var document = project.GetDocument(syntaxTree); |
| 118 | + if (document == null) |
| 119 | + { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + var syntaxRoot = await syntaxTree.GetRootAsync(ct); |
| 124 | + |
| 125 | + // Modify each location |
| 126 | + // We order the locations so that we modify starting from the end of the file |
| 127 | + // This way we prevent changes from being accidentally overwriting changes |
| 128 | + foreach (var location in group.OrderByDescending(l => l.SourceSpan.Start)) |
| 129 | + { |
| 130 | + foreach (var transformer in transformersSet) |
| 131 | + { |
| 132 | + var syntaxNode = syntaxRoot.FindNode(location.SourceSpan); |
| 133 | + var nodeToModify = transformer.GetNodeToModify(syntaxNode); |
| 134 | + if (nodeToModify == null) |
| 135 | + { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + var newNode = transformer.Visit(nodeToModify); |
| 140 | + syntaxRoot = syntaxRoot.ReplaceNode(nodeToModify, newNode); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + // Commit the changes to the project |
| 147 | + var newDocument = document.WithSyntaxRoot(syntaxRoot); |
| 148 | + project = newDocument.Project; |
| 149 | + } |
| 150 | + |
| 151 | + ctx.SourceProject = project; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +/// <summary> |
| 156 | +/// Base class for location transformers used by <see cref="LocationTransformationUtils.ModifyAllReferencesAsync"/>. |
| 157 | +/// </summary> |
| 158 | +public abstract class LocationTransformer : CSharpSyntaxRewriter |
| 159 | +{ |
| 160 | + /// <summary> |
| 161 | + /// Given a node, this method should return a parent node, the given node, or null. |
| 162 | + /// Returning null will lead to no node being modified. |
| 163 | + /// Returning the parent node will lead to the parent node being modified instead of the original node. |
| 164 | + /// </summary> |
| 165 | + /// <param name="current">TODO</param> |
| 166 | + /// <returns>TODO</returns> |
| 167 | + public abstract SyntaxNode? GetNodeToModify(SyntaxNode current); |
| 168 | +} |
| 169 | + |
| 170 | +// // TODO: Implement this |
| 171 | +// /// <summary> |
| 172 | +// /// Renames the identifiers for all locations transformed. |
| 173 | +// /// </summary> |
| 174 | +// public class IdentifierRenamingTransformer(bool includeDeclarations = true, bool includeCandidateLocations = false) : LocationTransformer |
| 175 | +// { |
| 176 | +// /// <inheritdoc /> |
| 177 | +// public override SyntaxNode? GetNodeToModify(SyntaxNode current, ISymbol symbol) => current; |
| 178 | +// |
| 179 | +// /// <inheritdoc /> |
| 180 | +// public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) |
| 181 | +// { |
| 182 | +// return IdentifierName("Equals"); |
| 183 | +// } |
| 184 | +// } |
| 185 | + |
| 186 | +/// <summary> |
| 187 | +/// Reduces the pointer dimension by one for all locations transformed. |
| 188 | +/// If the location is already a non-pointer, then nothing will be done. |
| 189 | +/// </summary> |
| 190 | +/// <example> |
| 191 | +/// <c>Handle**</c> will be replaced with <c>Handle*</c>. <br/> |
| 192 | +/// <c>Handle*</c> will be replaced with <c>Handle</c>. |
| 193 | +/// </example> |
| 194 | +public class PointerDimensionReductionTransformer : LocationTransformer |
| 195 | +{ |
| 196 | + /// <inheritdoc /> |
| 197 | + public override SyntaxNode? GetNodeToModify(SyntaxNode current) |
| 198 | + { |
| 199 | + if (current.Parent is PointerTypeSyntax parent) |
| 200 | + { |
| 201 | + return parent; |
| 202 | + } |
| 203 | + |
| 204 | + return null; |
| 205 | + } |
| 206 | + |
| 207 | + /// <inheritdoc /> |
| 208 | + public override SyntaxNode? VisitPointerType(PointerTypeSyntax node) => node.ElementType; |
| 209 | +} |
0 commit comments