| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Collections.Immutable; |
| | | 4 | | using System.Linq; |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 8 | | using Microsoft.CodeAnalysis.Operations; |
| | | 9 | | |
| | | 10 | | namespace Lattice.Analyzers |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// 为确定性运行时提供最小静态守卫: |
| | | 14 | | /// - 禁止系统 / runtime 类型引入可变静态字段 |
| | | 15 | | /// - 禁止在正式模拟入口中调用典型非确定性 API |
| | | 16 | | /// - 禁止系统在未显式声明或错误声明 Contract 时使用受守卫的 Frame API |
| | | 17 | | /// </summary> |
| | | 18 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 19 | | public sealed class DeterminismGuardAnalyzer : DiagnosticAnalyzer |
| | | 20 | | { |
| | | 21 | | public const string MutableStaticFieldId = "LATTICE100"; |
| | | 22 | | public const string NondeterministicApiId = "LATTICE101"; |
| | | 23 | | public const string MissingContractId = "LATTICE102"; |
| | | 24 | | |
| | 1 | 25 | | private static readonly DiagnosticDescriptor MutableStaticFieldRule = new( |
| | 1 | 26 | | MutableStaticFieldId, |
| | 1 | 27 | | "确定性模拟类型禁止可变静态字段", |
| | 1 | 28 | | "类型 '{0}' 包含可变静态字段 '{1}',会引入跨 Tick / 跨 Session 的隐式共享状态", |
| | 1 | 29 | | "Determinism", |
| | 1 | 30 | | DiagnosticSeverity.Error, |
| | 1 | 31 | | isEnabledByDefault: true, |
| | 1 | 32 | | description: "ISystem 与 SessionRuntime 相关类型不应通过可变静态字段缓存运行态数据。"); |
| | | 33 | | |
| | 1 | 34 | | private static readonly DiagnosticDescriptor NondeterministicApiRule = new( |
| | 1 | 35 | | NondeterministicApiId, |
| | 1 | 36 | | "正式模拟入口禁止非确定性 API", |
| | 1 | 37 | | "方法 '{0}' 调用了非确定性 API '{1}'", |
| | 1 | 38 | | "Determinism", |
| | 1 | 39 | | DiagnosticSeverity.Error, |
| | 1 | 40 | | isEnabledByDefault: true, |
| | 1 | 41 | | description: "系统 OnUpdate 与输入应用等正式模拟入口不应依赖时间、随机数、异步调度等非确定性 API。"); |
| | | 42 | | |
| | 1 | 43 | | private static readonly DiagnosticDescriptor MissingContractRule = new( |
| | 1 | 44 | | MissingContractId, |
| | 1 | 45 | | "系统使用受守卫 Frame API 时必须显式声明兼容 Contract", |
| | 1 | 46 | | "系统 '{0}' 使用了受守卫的 Frame API '{1}',但 Contract 未显式声明或未授予所需权限 '{2}'", |
| | 1 | 47 | | "Determinism", |
| | 1 | 48 | | DiagnosticSeverity.Error, |
| | 1 | 49 | | isEnabledByDefault: true, |
| | 1 | 50 | | description: "使用 global state 或结构性修改相关 API 的系统必须显式声明兼容的 SystemAuthoringContract。"); |
| | | 51 | | |
| | | 52 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 10 | 53 | | ImmutableArray.Create(MutableStaticFieldRule, NondeterministicApiRule, MissingContractRule); |
| | | 54 | | |
| | | 55 | | public override void Initialize(AnalysisContext context) |
| | | 56 | | { |
| | 10 | 57 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 10 | 58 | | context.EnableConcurrentExecution(); |
| | | 59 | | |
| | 10 | 60 | | context.RegisterCompilationStartAction(static startContext => |
| | 10 | 61 | | { |
| | 10 | 62 | | Compilation compilation = startContext.Compilation; |
| | 10 | 63 | | INamedTypeSymbol? systemType = compilation.GetTypeByMetadataName("Lattice.ECS.Framework.ISystem"); |
| | 10 | 64 | | INamedTypeSymbol? runtimeType = compilation.GetTypeByMetadataName("Lattice.ECS.Session.SessionRuntime"); |
| | 10 | 65 | | INamedTypeSymbol? frameType = compilation.GetTypeByMetadataName("Lattice.ECS.Core.Frame"); |
| | 10 | 66 | | INamedTypeSymbol? contractType = compilation.GetTypeByMetadataName("Lattice.ECS.Framework.SystemAuthorin |
| | 10 | 67 | | |
| | 10 | 68 | | if (systemType == null || runtimeType == null || frameType == null || contractType == null) |
| | 10 | 69 | | { |
| | 0 | 70 | | return; |
| | 10 | 71 | | } |
| | 10 | 72 | | |
| | 10 | 73 | | startContext.RegisterSymbolAction( |
| | 5 | 74 | | symbolContext => AnalyzeField(symbolContext, systemType, runtimeType), |
| | 10 | 75 | | SymbolKind.Field); |
| | 10 | 76 | | |
| | 10 | 77 | | startContext.RegisterOperationBlockStartAction(operationContext => |
| | 10 | 78 | | { |
| | 34 | 79 | | if (operationContext.OwningSymbol is not IMethodSymbol method) |
| | 10 | 80 | | { |
| | 3 | 81 | | return; |
| | 10 | 82 | | } |
| | 10 | 83 | | |
| | 31 | 84 | | bool isSystemUpdate = IsSystemUpdateMethod(method, systemType); |
| | 31 | 85 | | bool isSimulationHook = IsSystemSimulationMethod(method, systemType) || IsRuntimeSimulationHook(meth |
| | 31 | 86 | | if (!isSimulationHook) |
| | 10 | 87 | | { |
| | 6 | 88 | | return; |
| | 10 | 89 | | } |
| | 10 | 90 | | |
| | 25 | 91 | | bool sawGuardedApi = false; |
| | 25 | 92 | | string? firstGuardedApiName = null; |
| | 25 | 93 | | string? firstRequiredCapability = null; |
| | 25 | 94 | | Location? firstGuardedLocation = null; |
| | 25 | 95 | | bool needsStructuralContract = false; |
| | 25 | 96 | | bool needsGlobalReadContract = false; |
| | 25 | 97 | | bool needsGlobalWriteContract = false; |
| | 10 | 98 | | |
| | 25 | 99 | | operationContext.RegisterOperationAction(static operationAction => |
| | 25 | 100 | | { |
| | 6 | 101 | | var invocation = (IInvocationOperation)operationAction.Operation; |
| | 6 | 102 | | if (TryGetNondeterministicApiName(invocation.TargetMethod, out string? apiName)) |
| | 25 | 103 | | { |
| | 2 | 104 | | operationAction.ReportDiagnostic( |
| | 2 | 105 | | Diagnostic.Create( |
| | 2 | 106 | | NondeterministicApiRule, |
| | 2 | 107 | | invocation.Syntax.GetLocation(), |
| | 2 | 108 | | operationAction.ContainingSymbol.Name, |
| | 2 | 109 | | apiName)); |
| | 25 | 110 | | } |
| | 31 | 111 | | }, OperationKind.Invocation); |
| | 10 | 112 | | |
| | 25 | 113 | | operationContext.RegisterOperationAction(static operationAction => |
| | 25 | 114 | | { |
| | 1 | 115 | | var creation = (IObjectCreationOperation)operationAction.Operation; |
| | 1 | 116 | | if (TryGetNondeterministicTypeName(creation.Type, out string? apiName)) |
| | 25 | 117 | | { |
| | 1 | 118 | | operationAction.ReportDiagnostic( |
| | 1 | 119 | | Diagnostic.Create( |
| | 1 | 120 | | NondeterministicApiRule, |
| | 1 | 121 | | creation.Syntax.GetLocation(), |
| | 1 | 122 | | operationAction.ContainingSymbol.Name, |
| | 1 | 123 | | apiName)); |
| | 25 | 124 | | } |
| | 26 | 125 | | }, OperationKind.ObjectCreation); |
| | 10 | 126 | | |
| | 25 | 127 | | operationContext.RegisterOperationAction(static operationAction => |
| | 25 | 128 | | { |
| | 1 | 129 | | var propertyReference = (IPropertyReferenceOperation)operationAction.Operation; |
| | 1 | 130 | | if (TryGetNondeterministicPropertyName(propertyReference.Property, out string? apiName)) |
| | 25 | 131 | | { |
| | 1 | 132 | | operationAction.ReportDiagnostic( |
| | 1 | 133 | | Diagnostic.Create( |
| | 1 | 134 | | NondeterministicApiRule, |
| | 1 | 135 | | propertyReference.Syntax.GetLocation(), |
| | 1 | 136 | | operationAction.ContainingSymbol.Name, |
| | 1 | 137 | | apiName)); |
| | 25 | 138 | | } |
| | 26 | 139 | | }, OperationKind.PropertyReference); |
| | 10 | 140 | | |
| | 25 | 141 | | if (!isSystemUpdate) |
| | 10 | 142 | | { |
| | 17 | 143 | | return; |
| | 10 | 144 | | } |
| | 10 | 145 | | |
| | 8 | 146 | | operationContext.RegisterOperationAction(operationAction => |
| | 8 | 147 | | { |
| | 4 | 148 | | var invocation = (IInvocationOperation)operationAction.Operation; |
| | 4 | 149 | | if (!SymbolEqualityComparer.Default.Equals(invocation.TargetMethod.ContainingType, frameType)) |
| | 8 | 150 | | { |
| | 0 | 151 | | return; |
| | 8 | 152 | | } |
| | 8 | 153 | | |
| | 4 | 154 | | if (!TryGetRequiredContractCapability(invocation.TargetMethod.Name, out string? requiredCapabili |
| | 8 | 155 | | { |
| | 0 | 156 | | return; |
| | 8 | 157 | | } |
| | 8 | 158 | | |
| | 4 | 159 | | sawGuardedApi = true; |
| | 4 | 160 | | firstGuardedApiName ??= invocation.TargetMethod.Name; |
| | 4 | 161 | | firstRequiredCapability ??= requiredCapability; |
| | 4 | 162 | | firstGuardedLocation ??= invocation.Syntax.GetLocation(); |
| | 8 | 163 | | |
| | 8 | 164 | | switch (requiredCapability) |
| | 8 | 165 | | { |
| | 8 | 166 | | case nameof(ContractRequirement.StructuralChange): |
| | 2 | 167 | | needsStructuralContract = true; |
| | 2 | 168 | | break; |
| | 8 | 169 | | case nameof(ContractRequirement.GlobalRead): |
| | 2 | 170 | | needsGlobalReadContract = true; |
| | 2 | 171 | | break; |
| | 8 | 172 | | case nameof(ContractRequirement.GlobalWrite): |
| | 0 | 173 | | needsGlobalWriteContract = true; |
| | 8 | 174 | | break; |
| | 8 | 175 | | } |
| | 8 | 176 | | }, OperationKind.Invocation); |
| | 10 | 177 | | |
| | 8 | 178 | | operationContext.RegisterOperationBlockEndAction(endContext => |
| | 8 | 179 | | { |
| | 8 | 180 | | if (!sawGuardedApi) |
| | 8 | 181 | | { |
| | 4 | 182 | | return; |
| | 8 | 183 | | } |
| | 8 | 184 | | |
| | 4 | 185 | | if (!TryGetContractCapabilities(compilation, method.ContainingType, contractType, out ContractCa |
| | 8 | 186 | | { |
| | 2 | 187 | | endContext.ReportDiagnostic( |
| | 2 | 188 | | Diagnostic.Create( |
| | 2 | 189 | | MissingContractRule, |
| | 2 | 190 | | firstGuardedLocation ?? method.Locations.FirstOrDefault(), |
| | 2 | 191 | | method.ContainingType.Name, |
| | 2 | 192 | | firstGuardedApiName ?? "Frame API", |
| | 2 | 193 | | firstRequiredCapability ?? "ExplicitContract")); |
| | 2 | 194 | | return; |
| | 8 | 195 | | } |
| | 8 | 196 | | |
| | 2 | 197 | | bool lacksStructural = needsStructuralContract && !capabilities.AllowsStructuralChanges; |
| | 2 | 198 | | bool lacksGlobalRead = needsGlobalReadContract && !capabilities.AllowsGlobalReads; |
| | 2 | 199 | | bool lacksGlobalWrite = needsGlobalWriteContract && !capabilities.AllowsGlobalWrites; |
| | 8 | 200 | | |
| | 2 | 201 | | if (!lacksStructural && !lacksGlobalRead && !lacksGlobalWrite) |
| | 8 | 202 | | { |
| | 2 | 203 | | return; |
| | 8 | 204 | | } |
| | 8 | 205 | | |
| | 0 | 206 | | string missingCapability = lacksStructural |
| | 0 | 207 | | ? nameof(ContractRequirement.StructuralChange) |
| | 0 | 208 | | : lacksGlobalWrite |
| | 0 | 209 | | ? nameof(ContractRequirement.GlobalWrite) |
| | 0 | 210 | | : nameof(ContractRequirement.GlobalRead); |
| | 8 | 211 | | |
| | 0 | 212 | | endContext.ReportDiagnostic( |
| | 0 | 213 | | Diagnostic.Create( |
| | 0 | 214 | | MissingContractRule, |
| | 0 | 215 | | firstGuardedLocation ?? method.Locations.FirstOrDefault(), |
| | 0 | 216 | | method.ContainingType.Name, |
| | 0 | 217 | | firstGuardedApiName ?? "Frame API", |
| | 0 | 218 | | missingCapability)); |
| | 8 | 219 | | }); |
| | 18 | 220 | | }); |
| | 20 | 221 | | }); |
| | 10 | 222 | | } |
| | | 223 | | |
| | | 224 | | private static void AnalyzeField(SymbolAnalysisContext context, INamedTypeSymbol systemType, INamedTypeSymbol ru |
| | | 225 | | { |
| | 5 | 226 | | var field = (IFieldSymbol)context.Symbol; |
| | 5 | 227 | | if (!field.IsStatic || field.IsConst || field.IsReadOnly) |
| | | 228 | | { |
| | 3 | 229 | | return; |
| | | 230 | | } |
| | | 231 | | |
| | 2 | 232 | | INamedTypeSymbol containingType = field.ContainingType; |
| | 2 | 233 | | if (!Implements(containingType, systemType) && !DerivesFrom(containingType, runtimeType)) |
| | | 234 | | { |
| | 0 | 235 | | return; |
| | | 236 | | } |
| | | 237 | | |
| | 2 | 238 | | context.ReportDiagnostic( |
| | 2 | 239 | | Diagnostic.Create( |
| | 2 | 240 | | MutableStaticFieldRule, |
| | 2 | 241 | | field.Locations.FirstOrDefault(), |
| | 2 | 242 | | containingType.Name, |
| | 2 | 243 | | field.Name)); |
| | 2 | 244 | | } |
| | | 245 | | |
| | | 246 | | private static bool IsSystemSimulationMethod(IMethodSymbol method, INamedTypeSymbol systemType) |
| | | 247 | | { |
| | 31 | 248 | | if (method.ContainingType == null || !Implements(method.ContainingType, systemType)) |
| | | 249 | | { |
| | 5 | 250 | | return false; |
| | | 251 | | } |
| | | 252 | | |
| | 26 | 253 | | return ((method.Name == "OnInit" || method.Name == "OnDestroy") && |
| | 26 | 254 | | method.Parameters.Length == 1) || |
| | 26 | 255 | | IsSystemUpdateMethod(method, systemType); |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | private static bool IsSystemUpdateMethod(IMethodSymbol method, INamedTypeSymbol systemType) |
| | | 259 | | { |
| | 41 | 260 | | return method.Name == "OnUpdate" && |
| | 41 | 261 | | method.Parameters.Length == 2 && |
| | 41 | 262 | | method.ContainingType != null && |
| | 41 | 263 | | Implements(method.ContainingType, systemType); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | private static bool IsRuntimeSimulationHook(IMethodSymbol method, INamedTypeSymbol runtimeType) |
| | | 267 | | { |
| | 7 | 268 | | if (method.ContainingType == null || !DerivesFrom(method.ContainingType, runtimeType)) |
| | | 269 | | { |
| | 2 | 270 | | return false; |
| | | 271 | | } |
| | | 272 | | |
| | 5 | 273 | | return method.Name == "ApplyInputSet" || |
| | 5 | 274 | | method.Name == "ApplyInputs"; |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | private static bool Implements(INamedTypeSymbol type, INamedTypeSymbol interfaceType) |
| | | 278 | | { |
| | 98 | 279 | | return type.AllInterfaces.Any(candidate => SymbolEqualityComparer.Default.Equals(candidate, interfaceType)); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | private static bool DerivesFrom(INamedTypeSymbol type, INamedTypeSymbol baseType) |
| | | 283 | | { |
| | 8 | 284 | | INamedTypeSymbol? current = type; |
| | 18 | 285 | | while (current != null) |
| | | 286 | | { |
| | 16 | 287 | | if (SymbolEqualityComparer.Default.Equals(current, baseType)) |
| | | 288 | | { |
| | 6 | 289 | | return true; |
| | | 290 | | } |
| | | 291 | | |
| | 10 | 292 | | current = current.BaseType; |
| | | 293 | | } |
| | | 294 | | |
| | 2 | 295 | | return false; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | private static bool TryGetNondeterministicApiName(IMethodSymbol method, out string? apiName) |
| | | 299 | | { |
| | 6 | 300 | | string containingType = method.ContainingType.ToDisplayString(); |
| | 6 | 301 | | string methodName = method.Name; |
| | | 302 | | |
| | 6 | 303 | | if (containingType == "System.Guid" && methodName == "NewGuid") |
| | | 304 | | { |
| | 1 | 305 | | apiName = "System.Guid.NewGuid()"; |
| | 1 | 306 | | return true; |
| | | 307 | | } |
| | | 308 | | |
| | 5 | 309 | | if (containingType == "System.Threading.Tasks.Task" && (methodName == "Run" || methodName == "Delay")) |
| | | 310 | | { |
| | 0 | 311 | | apiName = $"System.Threading.Tasks.Task.{methodName}"; |
| | 0 | 312 | | return true; |
| | | 313 | | } |
| | | 314 | | |
| | 5 | 315 | | if (containingType == "System.Threading.Thread" && (methodName == "Sleep" || methodName == "Yield")) |
| | | 316 | | { |
| | 0 | 317 | | apiName = $"System.Threading.Thread.{methodName}"; |
| | 0 | 318 | | return true; |
| | | 319 | | } |
| | | 320 | | |
| | 5 | 321 | | if (containingType == "System.Threading.SpinWait" && methodName == "SpinUntil") |
| | | 322 | | { |
| | 0 | 323 | | apiName = "System.Threading.SpinWait.SpinUntil"; |
| | 0 | 324 | | return true; |
| | | 325 | | } |
| | | 326 | | |
| | 5 | 327 | | if (containingType == "System.Diagnostics.Stopwatch" && (methodName == "StartNew" || methodName == "GetTimes |
| | | 328 | | { |
| | 0 | 329 | | apiName = $"System.Diagnostics.Stopwatch.{methodName}"; |
| | 0 | 330 | | return true; |
| | | 331 | | } |
| | | 332 | | |
| | 5 | 333 | | if (containingType == "System.Random") |
| | | 334 | | { |
| | 1 | 335 | | apiName = $"System.Random.{methodName}"; |
| | 1 | 336 | | return true; |
| | | 337 | | } |
| | | 338 | | |
| | 4 | 339 | | apiName = null; |
| | 4 | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | private static bool TryGetNondeterministicTypeName(ITypeSymbol? type, out string? apiName) |
| | | 344 | | { |
| | 1 | 345 | | string? typeName = type?.ToDisplayString(); |
| | 1 | 346 | | if (typeName == "System.Random" || |
| | 1 | 347 | | typeName == "System.Diagnostics.Stopwatch" || |
| | 1 | 348 | | typeName == "System.Threading.Thread") |
| | | 349 | | { |
| | 1 | 350 | | apiName = typeName; |
| | 1 | 351 | | return true; |
| | | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | apiName = null; |
| | 0 | 355 | | return false; |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | private static bool TryGetNondeterministicPropertyName(IPropertySymbol property, out string? apiName) |
| | | 359 | | { |
| | 1 | 360 | | string containingType = property.ContainingType.ToDisplayString(); |
| | 1 | 361 | | string propertyName = property.Name; |
| | | 362 | | |
| | 1 | 363 | | if ((containingType == "System.DateTime" || containingType == "System.DateTimeOffset") && |
| | 1 | 364 | | (propertyName == "Now" || propertyName == "UtcNow")) |
| | | 365 | | { |
| | 1 | 366 | | apiName = $"{containingType}.{propertyName}"; |
| | 1 | 367 | | return true; |
| | | 368 | | } |
| | | 369 | | |
| | 0 | 370 | | if (containingType == "System.Environment" && |
| | 0 | 371 | | (propertyName == "TickCount" || propertyName == "TickCount64")) |
| | | 372 | | { |
| | 0 | 373 | | apiName = $"{containingType}.{propertyName}"; |
| | 0 | 374 | | return true; |
| | | 375 | | } |
| | | 376 | | |
| | 0 | 377 | | if (containingType == "System.Random" && propertyName == "Shared") |
| | | 378 | | { |
| | 0 | 379 | | apiName = "System.Random.Shared"; |
| | 0 | 380 | | return true; |
| | | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | apiName = null; |
| | 0 | 384 | | return false; |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | private static bool TryGetRequiredContractCapability(string frameMethodName, out string? requiredCapability) |
| | | 388 | | { |
| | | 389 | | switch (frameMethodName) |
| | | 390 | | { |
| | | 391 | | case "CreateEntity": |
| | | 392 | | case "DestroyEntity": |
| | | 393 | | case "Add": |
| | | 394 | | case "Remove": |
| | 2 | 395 | | requiredCapability = nameof(ContractRequirement.StructuralChange); |
| | 2 | 396 | | return true; |
| | | 397 | | |
| | | 398 | | case "HasGlobal": |
| | | 399 | | case "TryGetGlobal": |
| | 2 | 400 | | requiredCapability = nameof(ContractRequirement.GlobalRead); |
| | 2 | 401 | | return true; |
| | | 402 | | |
| | | 403 | | case "GetGlobal": |
| | | 404 | | case "GetGlobalEntity": |
| | | 405 | | case "TryGetGlobalEntity": |
| | | 406 | | case "SetGlobal": |
| | | 407 | | case "RemoveGlobal": |
| | 0 | 408 | | requiredCapability = nameof(ContractRequirement.GlobalWrite); |
| | 0 | 409 | | return true; |
| | | 410 | | |
| | | 411 | | default: |
| | 0 | 412 | | requiredCapability = null; |
| | 0 | 413 | | return false; |
| | | 414 | | } |
| | | 415 | | } |
| | | 416 | | |
| | | 417 | | private static bool TryGetContractCapabilities( |
| | | 418 | | Compilation compilation, |
| | | 419 | | INamedTypeSymbol type, |
| | | 420 | | INamedTypeSymbol contractType, |
| | | 421 | | out ContractCapabilities capabilities) |
| | | 422 | | { |
| | 4 | 423 | | IPropertySymbol? contractProperty = type.GetMembers("Contract") |
| | 4 | 424 | | .OfType<IPropertySymbol>() |
| | 6 | 425 | | .FirstOrDefault(static property => !property.IsImplicitlyDeclared); |
| | | 426 | | |
| | 4 | 427 | | if (contractProperty == null) |
| | | 428 | | { |
| | 2 | 429 | | capabilities = default; |
| | 2 | 430 | | return false; |
| | | 431 | | } |
| | | 432 | | |
| | 6 | 433 | | foreach (SyntaxReference syntaxReference in contractProperty.DeclaringSyntaxReferences) |
| | | 434 | | { |
| | 2 | 435 | | if (syntaxReference.GetSyntax() is not PropertyDeclarationSyntax propertySyntax) |
| | | 436 | | { |
| | | 437 | | continue; |
| | | 438 | | } |
| | | 439 | | |
| | 2 | 440 | | ExpressionSyntax? expression = propertySyntax.ExpressionBody?.Expression; |
| | 2 | 441 | | if (expression == null && propertySyntax.AccessorList != null) |
| | | 442 | | { |
| | 0 | 443 | | AccessorDeclarationSyntax? getter = propertySyntax.AccessorList.Accessors |
| | 0 | 444 | | .FirstOrDefault(static accessor => accessor.Keyword.Text == "get"); |
| | | 445 | | |
| | 0 | 446 | | if (getter?.ExpressionBody != null) |
| | | 447 | | { |
| | 0 | 448 | | expression = getter.ExpressionBody.Expression; |
| | | 449 | | } |
| | 0 | 450 | | else if (getter?.Body?.Statements.Count == 1 && |
| | 0 | 451 | | getter.Body.Statements[0] is ReturnStatementSyntax returnStatement) |
| | | 452 | | { |
| | 0 | 453 | | expression = returnStatement.Expression; |
| | | 454 | | } |
| | | 455 | | } |
| | | 456 | | |
| | 2 | 457 | | if (expression == null) |
| | | 458 | | { |
| | | 459 | | continue; |
| | | 460 | | } |
| | | 461 | | |
| | 2 | 462 | | SemanticModel semanticModel = compilation.GetSemanticModel(propertySyntax.SyntaxTree); |
| | 2 | 463 | | IOperation? operation = semanticModel.GetOperation(expression); |
| | 2 | 464 | | if (operation == null) |
| | | 465 | | { |
| | | 466 | | continue; |
| | | 467 | | } |
| | | 468 | | |
| | 2 | 469 | | if (TryEvaluateContractOperation(operation, contractType, out capabilities)) |
| | | 470 | | { |
| | 2 | 471 | | return true; |
| | | 472 | | } |
| | | 473 | | } |
| | | 474 | | |
| | 0 | 475 | | capabilities = default; |
| | 0 | 476 | | return false; |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | private static bool TryEvaluateContractOperation( |
| | | 480 | | IOperation operation, |
| | | 481 | | INamedTypeSymbol contractType, |
| | | 482 | | out ContractCapabilities capabilities) |
| | | 483 | | { |
| | 2 | 484 | | switch (operation) |
| | | 485 | | { |
| | | 486 | | case IPropertyReferenceOperation propertyReference |
| | 0 | 487 | | when SymbolEqualityComparer.Default.Equals(propertyReference.Property.ContainingType, contractType): |
| | 0 | 488 | | if (propertyReference.Property.Name == "Default") |
| | | 489 | | { |
| | 0 | 490 | | capabilities = default; |
| | 0 | 491 | | return true; |
| | | 492 | | } |
| | | 493 | | |
| | 0 | 494 | | if (propertyReference.Property.Name == "ReadOnly") |
| | | 495 | | { |
| | 0 | 496 | | capabilities = default; |
| | 0 | 497 | | return true; |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | break; |
| | | 501 | | |
| | | 502 | | case IObjectCreationOperation creation |
| | 2 | 503 | | when SymbolEqualityComparer.Default.Equals(creation.Constructor?.ContainingType, contractType): |
| | 2 | 504 | | int globalAccess = 0; |
| | 2 | 505 | | int structuralChangeAccess = 0; |
| | | 506 | | |
| | 16 | 507 | | foreach (IArgumentOperation argument in creation.Arguments) |
| | | 508 | | { |
| | 6 | 509 | | string? parameterName = argument.Parameter?.Name; |
| | 6 | 510 | | if (string.Equals(parameterName, "globalAccess", StringComparison.Ordinal)) |
| | | 511 | | { |
| | 2 | 512 | | globalAccess = GetEnumValue(argument.Value); |
| | 2 | 513 | | continue; |
| | | 514 | | } |
| | | 515 | | |
| | 4 | 516 | | if (string.Equals(parameterName, "structuralChangeAccess", StringComparison.Ordinal)) |
| | | 517 | | { |
| | 2 | 518 | | structuralChangeAccess = GetEnumValue(argument.Value); |
| | | 519 | | continue; |
| | | 520 | | } |
| | | 521 | | } |
| | | 522 | | |
| | 2 | 523 | | capabilities = new ContractCapabilities( |
| | 2 | 524 | | allowsGlobalReads: globalAccess >= 1, |
| | 2 | 525 | | allowsGlobalWrites: globalAccess >= 2, |
| | 2 | 526 | | allowsStructuralChanges: structuralChangeAccess >= 1); |
| | 2 | 527 | | return true; |
| | | 528 | | } |
| | | 529 | | |
| | 0 | 530 | | capabilities = default; |
| | 0 | 531 | | return false; |
| | | 532 | | } |
| | | 533 | | |
| | | 534 | | private static int GetEnumValue(IOperation operation) |
| | | 535 | | { |
| | 4 | 536 | | Optional<object?> constantValue = operation.ConstantValue; |
| | 4 | 537 | | if (constantValue.HasValue && constantValue.Value != null) |
| | | 538 | | { |
| | 4 | 539 | | return Convert.ToInt32(constantValue.Value, System.Globalization.CultureInfo.InvariantCulture); |
| | | 540 | | } |
| | | 541 | | |
| | 0 | 542 | | if (operation is IConversionOperation conversion) |
| | | 543 | | { |
| | 0 | 544 | | return GetEnumValue(conversion.Operand); |
| | | 545 | | } |
| | | 546 | | |
| | 0 | 547 | | return 0; |
| | | 548 | | } |
| | | 549 | | |
| | | 550 | | private enum ContractRequirement |
| | | 551 | | { |
| | | 552 | | StructuralChange, |
| | | 553 | | GlobalRead, |
| | | 554 | | GlobalWrite |
| | | 555 | | } |
| | | 556 | | |
| | | 557 | | private readonly struct ContractCapabilities |
| | | 558 | | { |
| | | 559 | | public ContractCapabilities(bool allowsGlobalReads, bool allowsGlobalWrites, bool allowsStructuralChanges) |
| | | 560 | | { |
| | 2 | 561 | | AllowsGlobalReads = allowsGlobalReads; |
| | 2 | 562 | | AllowsGlobalWrites = allowsGlobalWrites; |
| | 2 | 563 | | AllowsStructuralChanges = allowsStructuralChanges; |
| | 2 | 564 | | } |
| | | 565 | | |
| | 1 | 566 | | public bool AllowsGlobalReads { get; } |
| | | 567 | | |
| | 0 | 568 | | public bool AllowsGlobalWrites { get; } |
| | | 569 | | |
| | 1 | 570 | | public bool AllowsStructuralChanges { get; } |
| | | 571 | | } |
| | | 572 | | } |
| | | 573 | | } |