From bcac87c8f00362cdb6069efe4fd47e4418a595bb Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 5 May 2025 01:33:43 +0200 Subject: [PATCH] Add AccessorKind to make Accessor keywords independent of their role. --- .../CSharp/Syntax/TypeMembers/Accessor.cs | 38 ++++++++++++------- .../CSharp/Syntax/TypeSystemAstBuilder.cs | 22 ++++++----- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/Accessor.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/Accessor.cs index 7019beddd..bbdb172fd 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/Accessor.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/Accessor.cs @@ -24,10 +24,22 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +#nullable enable + using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.Decompiler.CSharp.Syntax { + public enum AccessorKind + { + Any, + Getter, + Setter, + Init, + Adder, + Remover + } + /// /// get/set/init/add/remove /// @@ -42,21 +54,15 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax get { return SymbolKind.Method; } } + public AccessorKind Kind { get; set; } + /// /// Gets the 'get'/'set'/'init'/'add'/'remove' keyword /// public CSharpTokenNode Keyword { get { - for (AstNode child = this.FirstChild; child != null; child = child.NextSibling) - { - if (child.Role == PropertyDeclaration.GetKeywordRole || child.Role == PropertyDeclaration.SetKeywordRole - || child.Role == PropertyDeclaration.InitKeywordRole - || child.Role == CustomEventDeclaration.AddKeywordRole || child.Role == CustomEventDeclaration.RemoveKeywordRole) - { - return (CSharpTokenNode)child; - } - } - return CSharpTokenNode.Null; + var role = GetAccessorKeywordRole(Kind); + return role == null ? CSharpTokenNode.Null : GetChildByRole(role); } } @@ -65,10 +71,16 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax set { SetChildByRole(Roles.Body, value); } } - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) + public static TokenRole? GetAccessorKeywordRole(AccessorKind kind) { - Accessor o = other as Accessor; - return o != null && !o.IsNull && this.MatchAttributesAndModifiers(o, match) && this.Body.DoMatch(o.Body, match); + return kind switch { + AccessorKind.Getter => PropertyDeclaration.GetKeywordRole, + AccessorKind.Setter => PropertyDeclaration.SetKeywordRole, + AccessorKind.Init => PropertyDeclaration.InitKeywordRole, + AccessorKind.Adder => CustomEventDeclaration.AddKeywordRole, + AccessorKind.Remover => CustomEventDeclaration.RemoveKeywordRole, + _ => null, + }; } } } diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs index 119185bc5..07ef4484c 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs @@ -2082,22 +2082,24 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax decl.Modifiers = ModifierFromAccessibility(accessor.Accessibility, UsePrivateProtectedAccessibility); if (this.ShowModifiers && accessor.HasReadonlyModifier()) decl.Modifiers |= Modifiers.Readonly; - TokenRole keywordRole = kind switch { - MethodSemanticsAttributes.Getter => PropertyDeclaration.GetKeywordRole, - MethodSemanticsAttributes.Setter => PropertyDeclaration.SetKeywordRole, - MethodSemanticsAttributes.Adder => CustomEventDeclaration.AddKeywordRole, - MethodSemanticsAttributes.Remover => CustomEventDeclaration.RemoveKeywordRole, - _ => null + AccessorKind accessorKind = kind switch { + MethodSemanticsAttributes.Getter => AccessorKind.Getter, + MethodSemanticsAttributes.Setter => AccessorKind.Setter, + MethodSemanticsAttributes.Adder => AccessorKind.Adder, + MethodSemanticsAttributes.Remover => AccessorKind.Remover, + _ => AccessorKind.Any }; if (kind == MethodSemanticsAttributes.Setter && SupportInitAccessors && accessor.IsInitOnly) { - keywordRole = PropertyDeclaration.InitKeywordRole; + accessorKind = AccessorKind.Init; } - if (keywordRole != null) + decl.Kind = accessorKind; + if (accessorKind != AccessorKind.Any) { - decl.AddChild(new CSharpTokenNode(TextLocation.Empty, keywordRole), keywordRole); + var tokenKind = Accessor.GetAccessorKeywordRole(accessorKind); + decl.AddChild(new CSharpTokenNode(TextLocation.Empty, tokenKind), tokenKind); } - if (accessor.IsInitOnly && keywordRole != PropertyDeclaration.InitKeywordRole) + if (accessor.IsInitOnly && accessorKind != AccessorKind.Init) { decl.AddChild(new Comment("init", CommentType.MultiLine), Roles.Comment); }