| | | 1 | | using System.Collections.Generic; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | |
| | | 5 | | using NexusLabs.Needlr.Generators.Models; |
| | | 6 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Generators; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Discovers and normalizes direct constructor guards and custom guard aliases on a |
| | | 12 | | /// field or property. |
| | | 13 | | /// </summary> |
| | | 14 | | internal static class ConstructorGuardDiscoveryHelper |
| | | 15 | | { |
| | | 16 | | private const string ConstructorGuardAttributeName = "ConstructorGuardAttribute"; |
| | | 17 | | private const string ConstructorGuardDefinitionAttributeName = "ConstructorGuardDefinitionAttribute"; |
| | | 18 | | private const string DefaultGuardMethodName = "Validate"; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Returns every direct or aliased constructor guard on |
| | | 22 | | /// <paramref name="member"/>, preserving attribute declaration order. |
| | | 23 | | /// </summary> |
| | | 24 | | internal static ConstructorGuardModel[] GetExplicitGuards(ISymbol member) |
| | | 25 | | { |
| | 344 | 26 | | var guards = new List<ConstructorGuardModel>(); |
| | | 27 | | |
| | 880 | 28 | | foreach (var attribute in member.GetAttributes()) |
| | | 29 | | { |
| | 96 | 30 | | var attrClass = attribute.AttributeClass; |
| | 96 | 31 | | if (attrClass is null) |
| | | 32 | | continue; |
| | | 33 | | |
| | 96 | 34 | | if (IsConstructorGuardAttributeClass(attrClass)) |
| | | 35 | | { |
| | 41 | 36 | | var parsed = ParseConstructorGuardAttribute(attribute); |
| | 41 | 37 | | if (parsed.HasValue) |
| | 41 | 38 | | guards.Add(parsed.Value); |
| | | 39 | | |
| | 41 | 40 | | continue; |
| | | 41 | | } |
| | | 42 | | |
| | 55 | 43 | | if (!TryGetGuardDefinition(attrClass, out var guardTypeName, out var guardMethodName)) |
| | | 44 | | continue; |
| | | 45 | | |
| | 28 | 46 | | var forwardedArgumentLiterals = TryRenderForwardedArguments(attribute); |
| | 28 | 47 | | if (forwardedArgumentLiterals is null) |
| | | 48 | | { |
| | | 49 | | // Unsupported arguments invalidate the complete guard call so the |
| | | 50 | | // generator never emits a partial or positionally incorrect call. |
| | | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 25 | 54 | | guards.Add(new ConstructorGuardModel( |
| | 25 | 55 | | GeneratedConstructorGuardKind.Custom, |
| | 25 | 56 | | guardTypeName, |
| | 25 | 57 | | guardMethodName, |
| | 25 | 58 | | forwardedArgumentLiterals)); |
| | | 59 | | } |
| | | 60 | | |
| | 344 | 61 | | return guards.ToArray(); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Returns whether <paramref name="attributeClass"/> is Needlr's direct |
| | | 66 | | /// <c>ConstructorGuardAttribute</c>. |
| | | 67 | | /// </summary> |
| | | 68 | | internal static bool IsConstructorGuardAttributeClass(INamedTypeSymbol attributeClass) |
| | | 69 | | { |
| | 127 | 70 | | return GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 127 | 71 | | attributeClass, |
| | 127 | 72 | | ConstructorGuardAttributeName); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Resolves a custom guard alias definition from an attribute type. |
| | | 77 | | /// </summary> |
| | | 78 | | internal static bool TryGetGuardDefinition( |
| | | 79 | | INamedTypeSymbol attributeClass, |
| | | 80 | | out string guardTypeName, |
| | | 81 | | out string guardMethodName) |
| | | 82 | | { |
| | 190 | 83 | | foreach (var metaAttribute in attributeClass.GetAttributes()) |
| | | 84 | | { |
| | 54 | 85 | | var metaClass = metaAttribute.AttributeClass; |
| | 54 | 86 | | if (metaClass is null || |
| | 54 | 87 | | !GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 54 | 88 | | metaClass, |
| | 54 | 89 | | ConstructorGuardDefinitionAttributeName)) |
| | | 90 | | { |
| | | 91 | | continue; |
| | | 92 | | } |
| | | 93 | | |
| | 28 | 94 | | if (metaAttribute.ConstructorArguments.Length == 0 || |
| | 28 | 95 | | metaAttribute.ConstructorArguments[0].Value is not ITypeSymbol guardType) |
| | | 96 | | { |
| | | 97 | | continue; |
| | | 98 | | } |
| | | 99 | | |
| | 28 | 100 | | guardTypeName = guardType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 28 | 101 | | guardMethodName = metaAttribute.ConstructorArguments.Length > 1 && |
| | 28 | 102 | | metaAttribute.ConstructorArguments[1].Value is string explicitName |
| | 28 | 103 | | ? explicitName |
| | 28 | 104 | | : DefaultGuardMethodName; |
| | 28 | 105 | | return true; |
| | | 106 | | } |
| | | 107 | | |
| | 27 | 108 | | guardTypeName = string.Empty; |
| | 27 | 109 | | guardMethodName = string.Empty; |
| | 27 | 110 | | return false; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | private static string[]? TryRenderForwardedArguments(AttributeData attribute) |
| | | 114 | | { |
| | 28 | 115 | | if (attribute.NamedArguments.Length > 0) |
| | 1 | 116 | | return null; |
| | | 117 | | |
| | 27 | 118 | | if (attribute.ConstructorArguments.Length == 0) |
| | 4 | 119 | | return System.Array.Empty<string>(); |
| | | 120 | | |
| | 23 | 121 | | var rendered = new string[attribute.ConstructorArguments.Length]; |
| | 104 | 122 | | for (var i = 0; i < attribute.ConstructorArguments.Length; i++) |
| | | 123 | | { |
| | 31 | 124 | | if (!TypedConstantRenderer.TryRender(attribute.ConstructorArguments[i], out var literal)) |
| | 2 | 125 | | return null; |
| | | 126 | | |
| | 29 | 127 | | rendered[i] = literal; |
| | | 128 | | } |
| | | 129 | | |
| | 21 | 130 | | return rendered; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static ConstructorGuardModel? ParseConstructorGuardAttribute(AttributeData attribute) |
| | | 134 | | { |
| | 41 | 135 | | if (attribute.ConstructorArguments.Length == 0) |
| | 0 | 136 | | return null; |
| | | 137 | | |
| | 41 | 138 | | var first = attribute.ConstructorArguments[0]; |
| | | 139 | | |
| | 41 | 140 | | if (first.Kind == TypedConstantKind.Enum && first.Value is int enumValue) |
| | 28 | 141 | | return new ConstructorGuardModel( |
| | 28 | 142 | | (GeneratedConstructorGuardKind)enumValue, |
| | 28 | 143 | | null, |
| | 28 | 144 | | null, |
| | 28 | 145 | | null); |
| | | 146 | | |
| | 13 | 147 | | if (first.Value is not ITypeSymbol guardType) |
| | 0 | 148 | | return null; |
| | | 149 | | |
| | 13 | 150 | | var methodName = attribute.ConstructorArguments.Length > 1 && |
| | 13 | 151 | | attribute.ConstructorArguments[1].Value is string explicitName |
| | 13 | 152 | | ? explicitName |
| | 13 | 153 | | : DefaultGuardMethodName; |
| | | 154 | | |
| | 13 | 155 | | return new ConstructorGuardModel( |
| | 13 | 156 | | GeneratedConstructorGuardKind.Custom, |
| | 13 | 157 | | guardType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), |
| | 13 | 158 | | methodName, |
| | 13 | 159 | | null); |
| | | 160 | | } |
| | | 161 | | } |