|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Lightup |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Immutable; |
| 8 | + using System.Linq; |
| 9 | + using System.Linq.Expressions; |
| 10 | + using System.Reflection; |
| 11 | + using System.Threading; |
| 12 | + using Microsoft.CodeAnalysis; |
| 13 | + |
| 14 | + internal static class SemanticModelExtensions |
| 15 | + { |
| 16 | + private static readonly Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>> GetImportScopesAccessor; |
| 17 | + |
| 18 | + static SemanticModelExtensions() |
| 19 | + { |
| 20 | + CreateGetImportScopesAccessor(out var accessor, out var isSupported); |
| 21 | + GetImportScopesAccessor = accessor; |
| 22 | + SupportsGetImportScopes = isSupported; |
| 23 | + } |
| 24 | + |
| 25 | + public static bool SupportsGetImportScopes { get; } |
| 26 | + |
| 27 | + public static ImmutableArray<IImportScopeWrapper> GetImportScopes(this SemanticModel semanticModel, int position, CancellationToken cancellationToken = default) |
| 28 | + { |
| 29 | + return GetImportScopesAccessor(semanticModel, position, cancellationToken); |
| 30 | + } |
| 31 | + |
| 32 | + private static void CreateGetImportScopesAccessor( |
| 33 | + out Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>> accessor, |
| 34 | + out bool isSupported) |
| 35 | + { |
| 36 | + // Very error-prone code and potentially brittle regarding future API changes. |
| 37 | + // Wrapping everything in a try statement just in case something breaks later on. |
| 38 | + try |
| 39 | + { |
| 40 | + var semanticModelType = typeof(SemanticModel); |
| 41 | + |
| 42 | + var codeAnalysisWorkspacesAssembly = semanticModelType.GetTypeInfo().Assembly; |
| 43 | + var nativeImportScopeType = codeAnalysisWorkspacesAssembly.GetType("Microsoft.CodeAnalysis.IImportScope"); |
| 44 | + if (nativeImportScopeType is null) |
| 45 | + { |
| 46 | + accessor = FallbackAccessor; |
| 47 | + isSupported = false; |
| 48 | + } |
| 49 | + |
| 50 | + var methods = semanticModelType.GetTypeInfo().GetDeclaredMethods("GetImportScopes").Where(IsCorrectGetImportScopesMethod); |
| 51 | + var method = methods.FirstOrDefault(); |
| 52 | + if (method == null) |
| 53 | + { |
| 54 | + accessor = FallbackAccessor; |
| 55 | + isSupported = false; |
| 56 | + } |
| 57 | + |
| 58 | + var importScopeWrapperFromObjectMethod = typeof(IImportScopeWrapper).GetTypeInfo().GetDeclaredMethod("FromObject"); |
| 59 | + var nativeImportScopeArrayType = typeof(ImmutableArray<>).MakeGenericType(nativeImportScopeType); |
| 60 | + var nativeImportScopeArrayGetItemMethod = nativeImportScopeArrayType.GetTypeInfo().GetDeclaredMethod("get_Item"); |
| 61 | + var wrapperImportScopeArrayType = typeof(ImmutableArray<IImportScopeWrapper>); |
| 62 | + var wrapperImportScopeArrayBuilderType = typeof(ImmutableArray<IImportScopeWrapper>.Builder); |
| 63 | + var wrapperImportScopeArrayBuilderAddMethod = wrapperImportScopeArrayBuilderType.GetTypeInfo().GetDeclaredMethod("Add"); |
| 64 | + var wrapperImportScopeArrayBuilderToImmutableMethod = wrapperImportScopeArrayBuilderType.GetTypeInfo().GetDeclaredMethod("ToImmutable"); |
| 65 | + var arrayCreateImportScopeBuilderMethod = typeof(ImmutableArray).GetTypeInfo().GetDeclaredMethods("CreateBuilder").Single(IsCorrectCreateBuilderMethod).MakeGenericMethod(typeof(IImportScopeWrapper)); |
| 66 | + |
| 67 | + var semanticModelParameter = Expression.Parameter(typeof(SemanticModel), "semanticModel"); |
| 68 | + var positionParameter = Expression.Parameter(typeof(int), "position"); |
| 69 | + var cancellationTokenParameter = Expression.Parameter(typeof(CancellationToken), "cancellationToken"); |
| 70 | + |
| 71 | + var nativeImportScopesVariable = Expression.Variable(nativeImportScopeArrayType, "nativeImportScopes"); |
| 72 | + var nativeImportScopeVariable = Expression.Variable(nativeImportScopeType, "nativeImportScope"); |
| 73 | + var countVariable = Expression.Variable(typeof(int), "count"); |
| 74 | + var indexVariable = Expression.Variable(typeof(int), "index"); |
| 75 | + var wrapperImportScopesBuilderVariable = Expression.Variable(wrapperImportScopeArrayBuilderType, "wrapperImportScopesBuilder"); |
| 76 | + var wrapperImportScopesVariable = Expression.Variable(wrapperImportScopeArrayType, "wrapperImoprtScopes"); |
| 77 | + var wrapperImportScopeVariable = Expression.Variable(typeof(IImportScopeWrapper), "wrapperImportScope"); |
| 78 | + |
| 79 | + var breakLabel = Expression.Label("break"); |
| 80 | + var block = Expression.Block( |
| 81 | + new[] { nativeImportScopesVariable, countVariable, indexVariable, wrapperImportScopesBuilderVariable, wrapperImportScopesVariable }, |
| 82 | + //// nativeImportScopes = semanticModel.GetImportScopes(position, cancellationToken); |
| 83 | + Expression.Assign( |
| 84 | + nativeImportScopesVariable, |
| 85 | + Expression.Call( |
| 86 | + semanticModelParameter, |
| 87 | + method, |
| 88 | + new[] { positionParameter, cancellationTokenParameter })), |
| 89 | + //// index = 0; |
| 90 | + Expression.Assign(indexVariable, Expression.Constant(0)), |
| 91 | + //// count = nativeImportScopes.Length; |
| 92 | + Expression.Assign( |
| 93 | + countVariable, |
| 94 | + Expression.Property(nativeImportScopesVariable, "Length")), |
| 95 | + //// wrapperImportScopesBuilder = ImmutableArray.CreateBuilder<IImportScopesWrapper>(); |
| 96 | + Expression.Assign( |
| 97 | + wrapperImportScopesBuilderVariable, |
| 98 | + Expression.Call(null, arrayCreateImportScopeBuilderMethod)), |
| 99 | + Expression.Loop( |
| 100 | + Expression.Block( |
| 101 | + new[] { nativeImportScopeVariable, wrapperImportScopeVariable }, |
| 102 | + //// if (index >= count) break; |
| 103 | + Expression.IfThen( |
| 104 | + Expression.GreaterThanOrEqual(indexVariable, countVariable), |
| 105 | + Expression.Break(breakLabel)), |
| 106 | + //// nativeImportScope = nativeImportScopes[index]; |
| 107 | + Expression.Assign( |
| 108 | + nativeImportScopeVariable, |
| 109 | + Expression.Call( |
| 110 | + nativeImportScopesVariable, |
| 111 | + nativeImportScopeArrayGetItemMethod, |
| 112 | + indexVariable)), |
| 113 | + //// wrapperImportScope = IImportScopeWrapper.FromObject(nativeImportScope); |
| 114 | + Expression.Assign( |
| 115 | + wrapperImportScopeVariable, |
| 116 | + Expression.Call( |
| 117 | + null, |
| 118 | + importScopeWrapperFromObjectMethod, |
| 119 | + nativeImportScopeVariable)), |
| 120 | + //// wrapperImportScopesBuilder.Add(wrapperImportScope); |
| 121 | + Expression.Call( |
| 122 | + wrapperImportScopesBuilderVariable, |
| 123 | + wrapperImportScopeArrayBuilderAddMethod, |
| 124 | + new[] { wrapperImportScopeVariable }), |
| 125 | + //// index++; |
| 126 | + Expression.PostIncrementAssign(indexVariable)), |
| 127 | + breakLabel), |
| 128 | + //// wrapperImportScopes = wrapperImportScopesBuilder.ToImmutable(); |
| 129 | + Expression.Assign( |
| 130 | + wrapperImportScopesVariable, |
| 131 | + Expression.Call( |
| 132 | + wrapperImportScopesBuilderVariable, |
| 133 | + wrapperImportScopeArrayBuilderToImmutableMethod))); |
| 134 | + |
| 135 | + var lambda = Expression.Lambda<Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>>>( |
| 136 | + block, |
| 137 | + new[] { semanticModelParameter, positionParameter, cancellationTokenParameter }); |
| 138 | + |
| 139 | + accessor = lambda.Compile(); |
| 140 | + isSupported = true; |
| 141 | + } |
| 142 | + catch (Exception) |
| 143 | + { |
| 144 | + accessor = FallbackAccessor; |
| 145 | + isSupported = false; |
| 146 | + } |
| 147 | + |
| 148 | + // NOTE: At time of implementation, there is no other overload of GetImportScopes, but check in case more are added later on |
| 149 | + static bool IsCorrectGetImportScopesMethod(MethodInfo method) |
| 150 | + { |
| 151 | + var parameters = method.GetParameters(); |
| 152 | + if (parameters.Length != 2) |
| 153 | + { |
| 154 | + return false; |
| 155 | + } |
| 156 | + |
| 157 | + return |
| 158 | + parameters[0].ParameterType == typeof(int) && |
| 159 | + parameters[1].ParameterType == typeof(CancellationToken); |
| 160 | + } |
| 161 | + |
| 162 | + static bool IsCorrectCreateBuilderMethod(MethodInfo method) |
| 163 | + { |
| 164 | + var parameters = method.GetParameters(); |
| 165 | + return parameters.Length == 0; |
| 166 | + } |
| 167 | + |
| 168 | + static ImmutableArray<IImportScopeWrapper> FallbackAccessor(SemanticModel semanticModel, int position, CancellationToken cancellationToken) |
| 169 | + { |
| 170 | + if (semanticModel == null) |
| 171 | + { |
| 172 | + // Unlike an extension method which would throw ArgumentNullException here, the light-up |
| 173 | + // behavior needs to match the behavior of the underlying method. |
| 174 | + throw new NullReferenceException(); |
| 175 | + } |
| 176 | + |
| 177 | + return ImmutableArray<IImportScopeWrapper>.Empty; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments