| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.Generators.Models; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// The complete shared model describing a generated constructor for a single partial |
| | | 8 | | /// class. Consumed by both source emission and Needlr's type-registry constructor |
| | | 9 | | /// discovery so both agree on the effective constructor shape. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Implements value equality (rather than the default struct member-by-member |
| | | 13 | | /// reflection-based comparison) — in particular <see cref="Fields"/> is compared |
| | | 14 | | /// element-by-element rather than by array reference — so that the incremental |
| | | 15 | | /// generator pipeline can cache this model across unrelated edits: an edit to one |
| | | 16 | | /// type's fields must not force every other eligible type's model to be considered |
| | | 17 | | /// "changed" just because a new (but equal) model instance was rebuilt for it. |
| | | 18 | | /// </remarks> |
| | | 19 | | internal readonly struct GeneratedConstructorModel : IEquatable<GeneratedConstructorModel> |
| | | 20 | | { |
| | | 21 | | public GeneratedConstructorModel( |
| | | 22 | | string containingNamespace, |
| | | 23 | | string containingTypeName, |
| | | 24 | | string typeParameterList, |
| | | 25 | | int arity, |
| | | 26 | | GeneratedConstructorNullGuardMode nullGuardMode, |
| | | 27 | | EligibleConstructorField[] fields, |
| | | 28 | | string? sourceFilePath) |
| | | 29 | | { |
| | 113 | 30 | | ContainingNamespace = containingNamespace; |
| | 113 | 31 | | ContainingTypeName = containingTypeName; |
| | 113 | 32 | | TypeParameterList = typeParameterList; |
| | 113 | 33 | | Arity = arity; |
| | 113 | 34 | | NullGuardMode = nullGuardMode; |
| | 113 | 35 | | Fields = fields; |
| | 113 | 36 | | SourceFilePath = sourceFilePath; |
| | 113 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The containing namespace, or an empty string for the global namespace. |
| | | 41 | | /// </summary> |
| | 298 | 42 | | public string ContainingNamespace { get; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The simple containing type name, without namespace or type parameters. |
| | | 46 | | /// </summary> |
| | 364 | 47 | | public string ContainingTypeName { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The type parameter list syntax (e.g. <c><T></c>), or an empty string for |
| | | 51 | | /// non-generic types. |
| | | 52 | | /// </summary> |
| | 232 | 53 | | public string TypeParameterList { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The type's generic arity (number of type parameters). Used, alongside |
| | | 57 | | /// <see cref="ContainingNamespace"/> and <see cref="ContainingTypeName"/>, as a |
| | | 58 | | /// deterministic hint-name discriminator so that two distinct types sharing the |
| | | 59 | | /// same namespace and name but different arity (e.g. <c>Foo</c> and |
| | | 60 | | /// <c>Foo<T></c>) never collide on the same generated file name — the same |
| | | 61 | | /// discriminator the CLR itself uses to distinguish same-named generic arities |
| | | 62 | | /// within a namespace. |
| | | 63 | | /// </summary> |
| | 100 | 64 | | public int Arity { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// The class-level null-guard mode in effect for this constructor. |
| | | 68 | | /// </summary> |
| | 109 | 69 | | public GeneratedConstructorNullGuardMode NullGuardMode { get; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Eligible fields, in deterministic declaration order, that become constructor |
| | | 73 | | /// parameters. |
| | | 74 | | /// </summary> |
| | 697 | 75 | | public EligibleConstructorField[] Fields { get; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// The source file path of the containing type declaration, used for breadcrumbs. |
| | | 79 | | /// </summary> |
| | 34 | 80 | | public string? SourceFilePath { get; } |
| | | 81 | | |
| | | 82 | | public bool Equals(GeneratedConstructorModel other) |
| | | 83 | | { |
| | 17 | 84 | | return string.Equals(ContainingNamespace, other.ContainingNamespace, StringComparison.Ordinal) && |
| | 17 | 85 | | string.Equals(ContainingTypeName, other.ContainingTypeName, StringComparison.Ordinal) && |
| | 17 | 86 | | string.Equals(TypeParameterList, other.TypeParameterList, StringComparison.Ordinal) && |
| | 17 | 87 | | Arity == other.Arity && |
| | 17 | 88 | | NullGuardMode == other.NullGuardMode && |
| | 17 | 89 | | string.Equals(SourceFilePath, other.SourceFilePath, StringComparison.Ordinal) && |
| | 17 | 90 | | FieldsEqual(Fields, other.Fields); |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | public override bool Equals(object? obj) => obj is GeneratedConstructorModel other && Equals(other); |
| | | 94 | | |
| | | 95 | | public override int GetHashCode() |
| | | 96 | | { |
| | | 97 | | unchecked |
| | | 98 | | { |
| | 0 | 99 | | var hash = ContainingNamespace.GetHashCode(); |
| | 0 | 100 | | hash = (hash * 397) ^ ContainingTypeName.GetHashCode(); |
| | 0 | 101 | | hash = (hash * 397) ^ TypeParameterList.GetHashCode(); |
| | 0 | 102 | | hash = (hash * 397) ^ Arity; |
| | 0 | 103 | | hash = (hash * 397) ^ (int)NullGuardMode; |
| | 0 | 104 | | hash = (hash * 397) ^ (SourceFilePath?.GetHashCode() ?? 0); |
| | | 105 | | |
| | 0 | 106 | | foreach (var field in Fields) |
| | | 107 | | { |
| | 0 | 108 | | hash = (hash * 397) ^ field.GetHashCode(); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | return hash; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | public static bool operator ==(GeneratedConstructorModel left, GeneratedConstructorModel right) => left.Equals(right |
| | | 116 | | |
| | 0 | 117 | | public static bool operator !=(GeneratedConstructorModel left, GeneratedConstructorModel right) => !left.Equals(righ |
| | | 118 | | |
| | | 119 | | private static bool FieldsEqual(EligibleConstructorField[] left, EligibleConstructorField[] right) |
| | | 120 | | { |
| | 17 | 121 | | if (ReferenceEquals(left, right)) |
| | 8 | 122 | | return true; |
| | | 123 | | |
| | 9 | 124 | | if (left.Length != right.Length) |
| | 0 | 125 | | return false; |
| | | 126 | | |
| | 9 | 127 | | return left.SequenceEqual(right); |
| | | 128 | | } |
| | | 129 | | } |