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