| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | |
| | | 8 | | using NexusLabs.Needlr.Generators.CodeGen; |
| | | 9 | | using NexusLabs.Needlr.Generators.Models; |
| | | 10 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 11 | | |
| | | 12 | | namespace NexusLabs.Needlr.Generators; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Discovers top-level partial positional record classes with marked property |
| | | 16 | | /// parameters and builds equatable constructor-overload models. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static class RecordConstructorOverloadDiscoveryHelper |
| | | 19 | | { |
| | | 20 | | private const string GenerateConstructorAttributeName = |
| | | 21 | | "GenerateConstructorAttribute"; |
| | | 22 | | private const string RecordConstructorOverloadParameterAttributeName = |
| | | 23 | | "RecordConstructorOverloadParameterAttribute"; |
| | | 24 | | private const string GeneratedFileSuffix = |
| | | 25 | | ".RecordConstructorOverload.g.cs"; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Cheap syntax predicate for the per-record incremental pipeline. |
| | | 29 | | /// </summary> |
| | | 30 | | internal static bool IsCandidateRecordDeclaration(SyntaxNode node) |
| | | 31 | | { |
| | 701 | 32 | | return node is RecordDeclarationSyntax; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Builds one canonical model for a record, or <see langword="null"/> when the |
| | | 37 | | /// record does not participate or any declaration is invalid. |
| | | 38 | | /// </summary> |
| | | 39 | | internal static RecordConstructorOverloadModel? TryCreateCanonicalModel( |
| | | 40 | | GeneratorSyntaxContext context) |
| | | 41 | | { |
| | 23 | 42 | | var recordDeclaration = (RecordDeclarationSyntax)context.Node; |
| | 23 | 43 | | if (context.SemanticModel.GetDeclaredSymbol(recordDeclaration) is not |
| | 23 | 44 | | INamedTypeSymbol typeSymbol || |
| | 23 | 45 | | !GeneratedConstructorEligibility.IsCanonicalDeclaration( |
| | 23 | 46 | | typeSymbol, |
| | 23 | 47 | | recordDeclaration)) |
| | | 48 | | { |
| | 2 | 49 | | return null; |
| | | 50 | | } |
| | | 51 | | |
| | 21 | 52 | | return TryGetModel( |
| | 21 | 53 | | typeSymbol, |
| | 21 | 54 | | context.SemanticModel.Compilation); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Returns every directly declared property carrying the record-overload marker in |
| | | 59 | | /// deterministic source order. |
| | | 60 | | /// </summary> |
| | | 61 | | internal static IReadOnlyList<IPropertySymbol> GetMarkedProperties( |
| | | 62 | | INamedTypeSymbol typeSymbol) |
| | | 63 | | { |
| | 54 | 64 | | return typeSymbol.GetMembers() |
| | 54 | 65 | | .OfType<IPropertySymbol>() |
| | 54 | 66 | | .Where(HasMarker) |
| | 54 | 67 | | .OrderBy( |
| | 54 | 68 | | property => |
| | 16 | 69 | | property.Locations.FirstOrDefault()?.SourceTree?.FilePath ?? |
| | 16 | 70 | | string.Empty, |
| | 54 | 71 | | System.StringComparer.Ordinal) |
| | 54 | 72 | | .ThenBy( |
| | 54 | 73 | | property => |
| | 16 | 74 | | property.Locations.FirstOrDefault()?.SourceSpan.Start ?? 0) |
| | 54 | 75 | | .ToArray(); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Returns whether a property carries Needlr's record-overload marker. |
| | | 80 | | /// </summary> |
| | | 81 | | internal static bool HasMarker(IPropertySymbol property) |
| | | 82 | | { |
| | 2308 | 83 | | return GetMarkerAttribute(property) is not null; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the marker attribute applied to a property. |
| | | 88 | | /// </summary> |
| | | 89 | | internal static AttributeData? GetMarkerAttribute(IPropertySymbol property) |
| | | 90 | | { |
| | 4803 | 91 | | foreach (var attribute in property.GetAttributes()) |
| | | 92 | | { |
| | 158 | 93 | | if (attribute.AttributeClass is { } attributeClass && |
| | 158 | 94 | | GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 158 | 95 | | attributeClass, |
| | 158 | 96 | | RecordConstructorOverloadParameterAttributeName)) |
| | | 97 | | { |
| | 153 | 98 | | return attribute; |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | 2167 | 102 | | return null; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the positional record declaration containing the primary parameter list. |
| | | 107 | | /// </summary> |
| | | 108 | | internal static RecordDeclarationSyntax? GetPrimaryRecordDeclaration( |
| | | 109 | | INamedTypeSymbol typeSymbol) |
| | | 110 | | { |
| | 123 | 111 | | return typeSymbol.DeclaringSyntaxReferences |
| | 123 | 112 | | .Select(reference => reference.GetSyntax()) |
| | 123 | 113 | | .OfType<RecordDeclarationSyntax>() |
| | 246 | 114 | | .FirstOrDefault(declaration => declaration.ParameterList is not null); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Returns why a marked property's containing type is outside the supported |
| | | 119 | | /// record-only contract. |
| | | 120 | | /// </summary> |
| | | 121 | | internal static string? GetTypeIneligibilityReason( |
| | | 122 | | INamedTypeSymbol typeSymbol) |
| | | 123 | | { |
| | 57 | 124 | | if (!typeSymbol.IsRecord) |
| | | 125 | | { |
| | 2 | 126 | | return typeSymbol.TypeKind == TypeKind.Class |
| | 2 | 127 | | ? "an ordinary class rather than a positional record class" |
| | 2 | 128 | | : $"a {typeSymbol.TypeKind.ToString().ToLowerInvariant()} rather than a positional record class"; |
| | | 129 | | } |
| | | 130 | | |
| | 55 | 131 | | if (typeSymbol.TypeKind == TypeKind.Struct) |
| | 2 | 132 | | return "a record struct"; |
| | | 133 | | |
| | 53 | 134 | | if (typeSymbol.IsFileLocal) |
| | 2 | 135 | | return "a file-local record that cannot be extended from a generated file"; |
| | | 136 | | |
| | 51 | 137 | | if (typeSymbol.ContainingType is not null) |
| | 2 | 138 | | return "a nested record"; |
| | | 139 | | |
| | 49 | 140 | | if (GetPrimaryRecordDeclaration(typeSymbol) is null) |
| | 2 | 141 | | return "a non-positional record with no primary parameter list"; |
| | | 142 | | |
| | 47 | 143 | | if (typeSymbol.BaseType is not null && |
| | 47 | 144 | | typeSymbol.BaseType.SpecialType != SpecialType.System_Object) |
| | | 145 | | { |
| | 2 | 146 | | return "an inherited record"; |
| | | 147 | | } |
| | | 148 | | |
| | 45 | 149 | | return null; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Returns why a marked property cannot participate, or |
| | | 154 | | /// <see langword="null"/> when it is assignable by the generated constructor. |
| | | 155 | | /// </summary> |
| | | 156 | | internal static string? GetPropertyIneligibilityReason( |
| | | 157 | | INamedTypeSymbol containingType, |
| | | 158 | | IPropertySymbol property, |
| | | 159 | | RecordDeclarationSyntax primaryDeclaration) |
| | | 160 | | { |
| | 47 | 161 | | if (!SymbolEqualityComparer.Default.Equals( |
| | 47 | 162 | | property.ContainingType, |
| | 47 | 163 | | containingType)) |
| | | 164 | | { |
| | 0 | 165 | | return "inherited rather than declared directly by the record"; |
| | | 166 | | } |
| | | 167 | | |
| | 47 | 168 | | if (property.IsStatic) |
| | 2 | 169 | | return "static"; |
| | | 170 | | |
| | 45 | 171 | | if (property.IsIndexer) |
| | 2 | 172 | | return "an indexer"; |
| | | 173 | | |
| | 43 | 174 | | if (IsPositionalProperty(property, primaryDeclaration)) |
| | | 175 | | { |
| | 2 | 176 | | return "a positional property synthesized from a primary constructor parameter"; |
| | | 177 | | } |
| | | 178 | | |
| | 41 | 179 | | if (property.SetMethod is null) |
| | | 180 | | { |
| | 2 | 181 | | return "get-only and cannot be assigned by the generated constructor"; |
| | | 182 | | } |
| | | 183 | | |
| | 39 | 184 | | if (property.ExplicitInterfaceImplementations.Length > 0) |
| | | 185 | | { |
| | 0 | 186 | | return "an explicit interface implementation and cannot be assigned by name"; |
| | | 187 | | } |
| | | 188 | | |
| | 39 | 189 | | if (property.IsAbstract) |
| | | 190 | | { |
| | 0 | 191 | | return "abstract and has no assignable implementation on the record"; |
| | | 192 | | } |
| | | 193 | | |
| | 39 | 194 | | if (property.IsRequired) |
| | | 195 | | { |
| | 2 | 196 | | return "required; the generated overload does not claim to satisfy the record's complete required-member con |
| | | 197 | | } |
| | | 198 | | |
| | 37 | 199 | | if (!IsTypeAccessibleFromGeneratedConstructor( |
| | 37 | 200 | | property.Type, |
| | 37 | 201 | | containingType.DeclaredAccessibility)) |
| | | 202 | | { |
| | 2 | 203 | | return $"typed as '{property.Type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}', which is |
| | | 204 | | } |
| | | 205 | | |
| | 35 | 206 | | return null; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Returns whether the record also participates in field-based |
| | | 211 | | /// <c>GenerateConstructor</c> generation. |
| | | 212 | | /// </summary> |
| | | 213 | | internal static bool HasFieldBasedGeneratedConstructorTrigger( |
| | | 214 | | INamedTypeSymbol typeSymbol) |
| | | 215 | | { |
| | 126 | 216 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 217 | | { |
| | 4 | 218 | | if (attribute.AttributeClass is { } attributeClass && |
| | 4 | 219 | | GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 4 | 220 | | attributeClass, |
| | 4 | 221 | | GenerateConstructorAttributeName)) |
| | | 222 | | { |
| | 4 | 223 | | return true; |
| | | 224 | | } |
| | | 225 | | } |
| | | 226 | | |
| | 57 | 227 | | return GeneratedConstructorEligibility.HasPositiveFieldGuardTrigger( |
| | 57 | 228 | | typeSymbol); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Finds an existing constructor whose C# signature collides with the proposed |
| | | 233 | | /// overload, ignoring names, nullable annotations, optional values, and |
| | | 234 | | /// <c>params</c>. |
| | | 235 | | /// </summary> |
| | | 236 | | internal static bool TryGetSignatureCollision( |
| | | 237 | | INamedTypeSymbol typeSymbol, |
| | | 238 | | Compilation compilation, |
| | | 239 | | out string collisionDisplay) |
| | | 240 | | { |
| | 31 | 241 | | var primaryDeclaration = GetPrimaryRecordDeclaration(typeSymbol); |
| | 31 | 242 | | if (primaryDeclaration?.ParameterList is null) |
| | | 243 | | { |
| | 0 | 244 | | collisionDisplay = string.Empty; |
| | 0 | 245 | | return false; |
| | | 246 | | } |
| | | 247 | | |
| | 31 | 248 | | var semanticModel = compilation.GetSemanticModel( |
| | 31 | 249 | | primaryDeclaration.SyntaxTree); |
| | 31 | 250 | | var proposed = new List<(ITypeSymbol Type, RefKind RefKind)>(); |
| | 138 | 251 | | foreach (var parameter in primaryDeclaration.ParameterList.Parameters) |
| | | 252 | | { |
| | 38 | 253 | | if (semanticModel.GetDeclaredSymbol(parameter) is not |
| | 38 | 254 | | IParameterSymbol parameterSymbol) |
| | | 255 | | { |
| | 0 | 256 | | collisionDisplay = string.Empty; |
| | 0 | 257 | | return false; |
| | | 258 | | } |
| | | 259 | | |
| | 38 | 260 | | proposed.Add((parameterSymbol.Type, parameterSymbol.RefKind)); |
| | | 261 | | } |
| | | 262 | | |
| | 132 | 263 | | foreach (var property in GetMarkedProperties(typeSymbol)) |
| | | 264 | | { |
| | 35 | 265 | | proposed.Add((property.Type, RefKind.None)); |
| | | 266 | | } |
| | | 267 | | |
| | 188 | 268 | | foreach (var constructor in typeSymbol.InstanceConstructors) |
| | | 269 | | { |
| | 67 | 270 | | if (IsDeclaredInGeneratedFile(constructor) || |
| | 67 | 271 | | constructor.Parameters.Length != proposed.Count) |
| | | 272 | | { |
| | | 273 | | continue; |
| | | 274 | | } |
| | | 275 | | |
| | 8 | 276 | | var matches = true; |
| | 42 | 277 | | for (var i = 0; i < proposed.Count; i++) |
| | | 278 | | { |
| | 13 | 279 | | if (constructor.Parameters[i].RefKind != proposed[i].RefKind || |
| | 13 | 280 | | !AreSignatureTypesEquivalent( |
| | 13 | 281 | | constructor.Parameters[i].Type, |
| | 13 | 282 | | proposed[i].Type)) |
| | | 283 | | { |
| | 0 | 284 | | matches = false; |
| | 0 | 285 | | break; |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | |
| | 8 | 289 | | if (!matches) |
| | | 290 | | continue; |
| | | 291 | | |
| | 8 | 292 | | collisionDisplay = BuildConstructorDisplay(constructor); |
| | 8 | 293 | | return true; |
| | | 294 | | } |
| | | 295 | | |
| | 23 | 296 | | collisionDisplay = string.Empty; |
| | 23 | 297 | | return false; |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | private static RecordConstructorOverloadModel? TryGetModel( |
| | | 301 | | INamedTypeSymbol typeSymbol, |
| | | 302 | | Compilation compilation) |
| | | 303 | | { |
| | 21 | 304 | | var markedProperties = GetMarkedProperties(typeSymbol); |
| | 21 | 305 | | if (markedProperties.Count == 0 || |
| | 21 | 306 | | GetTypeIneligibilityReason(typeSymbol) is not null || |
| | 21 | 307 | | !GeneratedConstructorEligibility.IsDeclaredPartial(typeSymbol) || |
| | 21 | 308 | | HasFieldBasedGeneratedConstructorTrigger(typeSymbol)) |
| | | 309 | | { |
| | 1 | 310 | | return null; |
| | | 311 | | } |
| | | 312 | | |
| | 20 | 313 | | var primaryDeclaration = GetPrimaryRecordDeclaration(typeSymbol); |
| | 20 | 314 | | if (primaryDeclaration?.ParameterList is null) |
| | 0 | 315 | | return null; |
| | | 316 | | |
| | 20 | 317 | | var semanticModel = compilation.GetSemanticModel( |
| | 20 | 318 | | primaryDeclaration.SyntaxTree); |
| | 20 | 319 | | var primaryParameters = |
| | 20 | 320 | | new RecordConstructorPrimaryParameter[ |
| | 20 | 321 | | primaryDeclaration.ParameterList.Parameters.Count]; |
| | 20 | 322 | | var primaryParameterNames = new HashSet<string>( |
| | 20 | 323 | | System.StringComparer.Ordinal); |
| | | 324 | | |
| | 20 | 325 | | for (var i = 0; |
| | 49 | 326 | | i < primaryDeclaration.ParameterList.Parameters.Count; |
| | 29 | 327 | | i++) |
| | | 328 | | { |
| | 29 | 329 | | var parameterSyntax = |
| | 29 | 330 | | primaryDeclaration.ParameterList.Parameters[i]; |
| | 29 | 331 | | if (semanticModel.GetDeclaredSymbol(parameterSyntax) is not |
| | 29 | 332 | | IParameterSymbol parameterSymbol || |
| | 29 | 333 | | !primaryParameterNames.Add(parameterSymbol.Name)) |
| | | 334 | | { |
| | 0 | 335 | | return null; |
| | | 336 | | } |
| | | 337 | | |
| | 29 | 338 | | var documentation = |
| | 29 | 339 | | DocumentationCommentHelper.GetParameterDocumentation( |
| | 29 | 340 | | typeSymbol, |
| | 29 | 341 | | parameterSymbol.Name) ?? |
| | 29 | 342 | | $"The value forwarded to the positional primary constructor parameter <paramref name=\"{parameterSymbol. |
| | 29 | 343 | | primaryParameters[i] = new RecordConstructorPrimaryParameter( |
| | 29 | 344 | | parameterSymbol.Name, |
| | 29 | 345 | | GeneratorHelpers.EscapeIdentifier(parameterSymbol.Name), |
| | 29 | 346 | | parameterSymbol.Type.ToDisplayString( |
| | 29 | 347 | | ConstructorGenerationDiscoveryHelper.NullableAwareFormat), |
| | 29 | 348 | | documentation); |
| | | 349 | | } |
| | | 350 | | |
| | 20 | 351 | | var propertyParameters = |
| | 20 | 352 | | new RecordConstructorPropertyParameter[markedProperties.Count]; |
| | 88 | 353 | | for (var i = 0; i < markedProperties.Count; i++) |
| | | 354 | | { |
| | 24 | 355 | | var property = markedProperties[i]; |
| | 24 | 356 | | if (GetPropertyIneligibilityReason( |
| | 24 | 357 | | typeSymbol, |
| | 24 | 358 | | property, |
| | 24 | 359 | | primaryDeclaration) is not null || |
| | 24 | 360 | | !primaryParameterNames.Add(property.Name)) |
| | | 361 | | { |
| | 0 | 362 | | return null; |
| | | 363 | | } |
| | | 364 | | |
| | 24 | 365 | | var effectiveGuards = |
| | 24 | 366 | | ConstructorGuardCodeGenerator.ComposeEffectiveGuards( |
| | 24 | 367 | | false, |
| | 24 | 368 | | ConstructorGuardDiscoveryHelper.GetExplicitGuards(property)); |
| | 24 | 369 | | if (!ArePropertyGuardsValid( |
| | 24 | 370 | | compilation, |
| | 24 | 371 | | typeSymbol, |
| | 24 | 372 | | property)) |
| | | 373 | | { |
| | 0 | 374 | | return null; |
| | | 375 | | } |
| | | 376 | | |
| | 24 | 377 | | var parameterType = property.Type; |
| | 24 | 378 | | if (parameterType.IsReferenceType && |
| | 24 | 379 | | parameterType.NullableAnnotation == |
| | 24 | 380 | | NullableAnnotation.Annotated && |
| | 24 | 381 | | ConstructorGuardCodeGenerator.HasBuiltInNullRejectingGuard( |
| | 24 | 382 | | effectiveGuards)) |
| | | 383 | | { |
| | 3 | 384 | | parameterType = parameterType.WithNullableAnnotation( |
| | 3 | 385 | | NullableAnnotation.NotAnnotated); |
| | | 386 | | } |
| | | 387 | | |
| | 24 | 388 | | var escapedName = GeneratorHelpers.EscapeIdentifier(property.Name); |
| | 24 | 389 | | var documentation = |
| | 24 | 390 | | DocumentationCommentHelper.GetSummaryDocumentation(property) ?? |
| | 24 | 391 | | $"The value assigned to <see cref=\"{escapedName}\"/>."; |
| | 24 | 392 | | propertyParameters[i] = |
| | 24 | 393 | | new RecordConstructorPropertyParameter( |
| | 24 | 394 | | property.Name, |
| | 24 | 395 | | escapedName, |
| | 24 | 396 | | parameterType.ToDisplayString( |
| | 24 | 397 | | ConstructorGenerationDiscoveryHelper.NullableAwareFormat), |
| | 24 | 398 | | documentation, |
| | 24 | 399 | | effectiveGuards); |
| | | 400 | | } |
| | | 401 | | |
| | 20 | 402 | | if (TryGetSignatureCollision( |
| | 20 | 403 | | typeSymbol, |
| | 20 | 404 | | compilation, |
| | 20 | 405 | | out _)) |
| | | 406 | | { |
| | 2 | 407 | | return null; |
| | | 408 | | } |
| | | 409 | | |
| | 18 | 410 | | var typeParameterList = typeSymbol.TypeParameters.Length == 0 |
| | 18 | 411 | | ? string.Empty |
| | 18 | 412 | | : "<" + string.Join( |
| | 18 | 413 | | ", ", |
| | 18 | 414 | | typeSymbol.TypeParameters.Select( |
| | 18 | 415 | | parameter => |
| | 2 | 416 | | GeneratorHelpers.EscapeIdentifier(parameter.Name))) + |
| | 18 | 417 | | ">"; |
| | 18 | 418 | | var containingNamespace = |
| | 18 | 419 | | typeSymbol.ContainingNamespace is { IsGlobalNamespace: false } |
| | 18 | 420 | | ? typeSymbol.ContainingNamespace.ToDisplayString() |
| | 18 | 421 | | : string.Empty; |
| | | 422 | | |
| | 18 | 423 | | return new RecordConstructorOverloadModel( |
| | 18 | 424 | | containingNamespace, |
| | 18 | 425 | | typeSymbol.Name, |
| | 18 | 426 | | GeneratorHelpers.EscapeIdentifier(typeSymbol.Name), |
| | 18 | 427 | | typeParameterList, |
| | 18 | 428 | | typeSymbol.TypeParameters.Length, |
| | 18 | 429 | | primaryParameters, |
| | 18 | 430 | | propertyParameters, |
| | 18 | 431 | | primaryDeclaration.SyntaxTree.FilePath); |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | private static bool ArePropertyGuardsValid( |
| | | 435 | | Compilation compilation, |
| | | 436 | | INamedTypeSymbol containingType, |
| | | 437 | | IPropertySymbol property) |
| | | 438 | | { |
| | 110 | 439 | | foreach (var attribute in property.GetAttributes()) |
| | | 440 | | { |
| | 31 | 441 | | if (attribute.AttributeClass is not { } attributeClass) |
| | | 442 | | continue; |
| | | 443 | | |
| | | 444 | | ConstructorGuardOccurrence occurrence; |
| | 31 | 445 | | if (ConstructorGuardDiscoveryHelper.IsConstructorGuardAttributeClass( |
| | 31 | 446 | | attributeClass)) |
| | | 447 | | { |
| | 5 | 448 | | occurrence = |
| | 5 | 449 | | ConstructorGuardAnalysisHelper.BuildDirectGuardOccurrence( |
| | 5 | 450 | | property, |
| | 5 | 451 | | property.Type, |
| | 5 | 452 | | "property", |
| | 5 | 453 | | attribute, |
| | 5 | 454 | | null); |
| | | 455 | | } |
| | 26 | 456 | | else if (ConstructorGuardAnalysisHelper.TryGetGuardDefinition( |
| | 26 | 457 | | attributeClass, |
| | 26 | 458 | | out var guardType, |
| | 26 | 459 | | out var methodName, |
| | 26 | 460 | | out var methodNameExplicit)) |
| | | 461 | | { |
| | 2 | 462 | | occurrence = |
| | 2 | 463 | | ConstructorGuardAnalysisHelper.BuildAliasOccurrence( |
| | 2 | 464 | | property, |
| | 2 | 465 | | property.Type, |
| | 2 | 466 | | "property", |
| | 2 | 467 | | attribute, |
| | 2 | 468 | | null, |
| | 2 | 469 | | guardType, |
| | 2 | 470 | | methodName, |
| | 2 | 471 | | methodNameExplicit, |
| | 2 | 472 | | attributeClass.DeclaringSyntaxReferences.Length > 0); |
| | | 473 | | } |
| | | 474 | | else |
| | | 475 | | { |
| | | 476 | | continue; |
| | | 477 | | } |
| | | 478 | | |
| | 7 | 479 | | if (!ConstructorGuardAnalysisHelper |
| | 7 | 480 | | .IsPositiveGuardOccurrenceValidForGeneration( |
| | 7 | 481 | | compilation, |
| | 7 | 482 | | containingType, |
| | 7 | 483 | | occurrence)) |
| | | 484 | | { |
| | 0 | 485 | | return false; |
| | | 486 | | } |
| | | 487 | | } |
| | | 488 | | |
| | 24 | 489 | | return true; |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | private static bool IsPositionalProperty( |
| | | 493 | | IPropertySymbol property, |
| | | 494 | | RecordDeclarationSyntax primaryDeclaration) |
| | | 495 | | { |
| | 43 | 496 | | return primaryDeclaration.ParameterList?.Parameters.Any( |
| | 93 | 497 | | parameter => parameter.Identifier.ValueText == property.Name) == true; |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | private static bool IsDeclaredInGeneratedFile(ISymbol symbol) |
| | | 501 | | { |
| | 67 | 502 | | return symbol.Locations.Any(location => |
| | 136 | 503 | | location.SourceTree?.FilePath.EndsWith( |
| | 136 | 504 | | GeneratedFileSuffix, |
| | 136 | 505 | | System.StringComparison.Ordinal) == true); |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | private static string BuildConstructorDisplay(IMethodSymbol constructor) |
| | | 509 | | { |
| | 21 | 510 | | return $"{constructor.ContainingType.Name}({string.Join(", ", constructor.Parameters.Select(parameter => paramet |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | private static bool AreSignatureTypesEquivalent( |
| | | 514 | | ITypeSymbol left, |
| | | 515 | | ITypeSymbol right) |
| | | 516 | | { |
| | 13 | 517 | | if (SymbolEqualityComparer.Default.Equals(left, right)) |
| | 11 | 518 | | return true; |
| | | 519 | | |
| | 2 | 520 | | if ((left is IDynamicTypeSymbol && |
| | 2 | 521 | | right.SpecialType == SpecialType.System_Object) || |
| | 2 | 522 | | (right is IDynamicTypeSymbol && |
| | 2 | 523 | | left.SpecialType == SpecialType.System_Object)) |
| | | 524 | | { |
| | 2 | 525 | | return true; |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | if (left is IArrayTypeSymbol leftArray && |
| | 0 | 529 | | right is IArrayTypeSymbol rightArray) |
| | | 530 | | { |
| | 0 | 531 | | return leftArray.Rank == rightArray.Rank && |
| | 0 | 532 | | AreSignatureTypesEquivalent( |
| | 0 | 533 | | leftArray.ElementType, |
| | 0 | 534 | | rightArray.ElementType); |
| | | 535 | | } |
| | | 536 | | |
| | 0 | 537 | | if (left is IPointerTypeSymbol leftPointer && |
| | 0 | 538 | | right is IPointerTypeSymbol rightPointer) |
| | | 539 | | { |
| | 0 | 540 | | return AreSignatureTypesEquivalent( |
| | 0 | 541 | | leftPointer.PointedAtType, |
| | 0 | 542 | | rightPointer.PointedAtType); |
| | | 543 | | } |
| | | 544 | | |
| | 0 | 545 | | if (left is not INamedTypeSymbol leftNamed || |
| | 0 | 546 | | right is not INamedTypeSymbol rightNamed || |
| | 0 | 547 | | !SymbolEqualityComparer.Default.Equals( |
| | 0 | 548 | | leftNamed.OriginalDefinition, |
| | 0 | 549 | | rightNamed.OriginalDefinition) || |
| | 0 | 550 | | leftNamed.TypeArguments.Length != rightNamed.TypeArguments.Length) |
| | | 551 | | { |
| | 0 | 552 | | return false; |
| | | 553 | | } |
| | | 554 | | |
| | 0 | 555 | | for (var i = 0; i < leftNamed.TypeArguments.Length; i++) |
| | | 556 | | { |
| | 0 | 557 | | if (!AreSignatureTypesEquivalent( |
| | 0 | 558 | | leftNamed.TypeArguments[i], |
| | 0 | 559 | | rightNamed.TypeArguments[i])) |
| | | 560 | | { |
| | 0 | 561 | | return false; |
| | | 562 | | } |
| | | 563 | | } |
| | | 564 | | |
| | 0 | 565 | | return true; |
| | | 566 | | } |
| | | 567 | | |
| | | 568 | | private static bool IsTypeAccessibleFromGeneratedConstructor( |
| | | 569 | | ITypeSymbol type, |
| | | 570 | | Accessibility containingTypeAccessibility) |
| | | 571 | | { |
| | | 572 | | switch (type) |
| | | 573 | | { |
| | | 574 | | case IArrayTypeSymbol arrayType: |
| | 0 | 575 | | return IsTypeAccessibleFromGeneratedConstructor( |
| | 0 | 576 | | arrayType.ElementType, |
| | 0 | 577 | | containingTypeAccessibility); |
| | | 578 | | case IPointerTypeSymbol pointerType: |
| | 0 | 579 | | return IsTypeAccessibleFromGeneratedConstructor( |
| | 0 | 580 | | pointerType.PointedAtType, |
| | 0 | 581 | | containingTypeAccessibility); |
| | | 582 | | case ITypeParameterSymbol: |
| | | 583 | | case IDynamicTypeSymbol: |
| | 2 | 584 | | return true; |
| | | 585 | | case INamedTypeSymbol namedType: |
| | 36 | 586 | | if (!IsNamedTypeAccessibleFromGeneratedConstructor( |
| | 36 | 587 | | namedType, |
| | 36 | 588 | | containingTypeAccessibility)) |
| | | 589 | | { |
| | 2 | 590 | | return false; |
| | | 591 | | } |
| | | 592 | | |
| | 34 | 593 | | return namedType.TypeArguments.All(typeArgument => |
| | 35 | 594 | | IsTypeAccessibleFromGeneratedConstructor( |
| | 35 | 595 | | typeArgument, |
| | 35 | 596 | | containingTypeAccessibility)); |
| | | 597 | | default: |
| | 0 | 598 | | return true; |
| | | 599 | | } |
| | | 600 | | } |
| | | 601 | | |
| | | 602 | | private static bool IsNamedTypeAccessibleFromGeneratedConstructor( |
| | | 603 | | INamedTypeSymbol type, |
| | | 604 | | Accessibility containingTypeAccessibility) |
| | | 605 | | { |
| | 140 | 606 | | for (var current = type; current is not null; current = current.ContainingType) |
| | | 607 | | { |
| | 36 | 608 | | if (current.SpecialType != SpecialType.None) |
| | | 609 | | continue; |
| | | 610 | | |
| | 8 | 611 | | if (containingTypeAccessibility == Accessibility.Public) |
| | | 612 | | { |
| | 7 | 613 | | if (current.DeclaredAccessibility != Accessibility.Public) |
| | 2 | 614 | | return false; |
| | | 615 | | } |
| | 1 | 616 | | else if (current.DeclaredAccessibility is not ( |
| | 1 | 617 | | Accessibility.Public or |
| | 1 | 618 | | Accessibility.Internal or |
| | 1 | 619 | | Accessibility.ProtectedOrInternal)) |
| | | 620 | | { |
| | 0 | 621 | | return false; |
| | | 622 | | } |
| | | 623 | | } |
| | | 624 | | |
| | 34 | 625 | | return true; |
| | | 626 | | } |
| | | 627 | | } |