Skip to content

Commit bcac87c

Browse files
Add AccessorKind to make Accessor keywords independent of their role.
1 parent 1a1980a commit bcac87c

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/Accessor.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,22 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
2626

27+
#nullable enable
28+
2729
using ICSharpCode.Decompiler.TypeSystem;
2830

2931
namespace ICSharpCode.Decompiler.CSharp.Syntax
3032
{
33+
public enum AccessorKind
34+
{
35+
Any,
36+
Getter,
37+
Setter,
38+
Init,
39+
Adder,
40+
Remover
41+
}
42+
3143
/// <summary>
3244
/// get/set/init/add/remove
3345
/// </summary>
@@ -42,21 +54,15 @@ public override SymbolKind SymbolKind {
4254
get { return SymbolKind.Method; }
4355
}
4456

57+
public AccessorKind Kind { get; set; }
58+
4559
/// <summary>
4660
/// Gets the 'get'/'set'/'init'/'add'/'remove' keyword
4761
/// </summary>
4862
public CSharpTokenNode Keyword {
4963
get {
50-
for (AstNode child = this.FirstChild; child != null; child = child.NextSibling)
51-
{
52-
if (child.Role == PropertyDeclaration.GetKeywordRole || child.Role == PropertyDeclaration.SetKeywordRole
53-
|| child.Role == PropertyDeclaration.InitKeywordRole
54-
|| child.Role == CustomEventDeclaration.AddKeywordRole || child.Role == CustomEventDeclaration.RemoveKeywordRole)
55-
{
56-
return (CSharpTokenNode)child;
57-
}
58-
}
59-
return CSharpTokenNode.Null;
64+
var role = GetAccessorKeywordRole(Kind);
65+
return role == null ? CSharpTokenNode.Null : GetChildByRole(role);
6066
}
6167
}
6268

@@ -65,10 +71,16 @@ public BlockStatement Body {
6571
set { SetChildByRole(Roles.Body, value); }
6672
}
6773

68-
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
74+
public static TokenRole? GetAccessorKeywordRole(AccessorKind kind)
6975
{
70-
Accessor o = other as Accessor;
71-
return o != null && !o.IsNull && this.MatchAttributesAndModifiers(o, match) && this.Body.DoMatch(o.Body, match);
76+
return kind switch {
77+
AccessorKind.Getter => PropertyDeclaration.GetKeywordRole,
78+
AccessorKind.Setter => PropertyDeclaration.SetKeywordRole,
79+
AccessorKind.Init => PropertyDeclaration.InitKeywordRole,
80+
AccessorKind.Adder => CustomEventDeclaration.AddKeywordRole,
81+
AccessorKind.Remover => CustomEventDeclaration.RemoveKeywordRole,
82+
_ => null,
83+
};
7284
}
7385
}
7486
}

ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,22 +2082,24 @@ Accessor ConvertAccessor(IMethod accessor, MethodSemanticsAttributes kind, Acces
20822082
decl.Modifiers = ModifierFromAccessibility(accessor.Accessibility, UsePrivateProtectedAccessibility);
20832083
if (this.ShowModifiers && accessor.HasReadonlyModifier())
20842084
decl.Modifiers |= Modifiers.Readonly;
2085-
TokenRole keywordRole = kind switch {
2086-
MethodSemanticsAttributes.Getter => PropertyDeclaration.GetKeywordRole,
2087-
MethodSemanticsAttributes.Setter => PropertyDeclaration.SetKeywordRole,
2088-
MethodSemanticsAttributes.Adder => CustomEventDeclaration.AddKeywordRole,
2089-
MethodSemanticsAttributes.Remover => CustomEventDeclaration.RemoveKeywordRole,
2090-
_ => null
2085+
AccessorKind accessorKind = kind switch {
2086+
MethodSemanticsAttributes.Getter => AccessorKind.Getter,
2087+
MethodSemanticsAttributes.Setter => AccessorKind.Setter,
2088+
MethodSemanticsAttributes.Adder => AccessorKind.Adder,
2089+
MethodSemanticsAttributes.Remover => AccessorKind.Remover,
2090+
_ => AccessorKind.Any
20912091
};
20922092
if (kind == MethodSemanticsAttributes.Setter && SupportInitAccessors && accessor.IsInitOnly)
20932093
{
2094-
keywordRole = PropertyDeclaration.InitKeywordRole;
2094+
accessorKind = AccessorKind.Init;
20952095
}
2096-
if (keywordRole != null)
2096+
decl.Kind = accessorKind;
2097+
if (accessorKind != AccessorKind.Any)
20972098
{
2098-
decl.AddChild(new CSharpTokenNode(TextLocation.Empty, keywordRole), keywordRole);
2099+
var tokenKind = Accessor.GetAccessorKeywordRole(accessorKind);
2100+
decl.AddChild(new CSharpTokenNode(TextLocation.Empty, tokenKind), tokenKind);
20992101
}
2100-
if (accessor.IsInitOnly && keywordRole != PropertyDeclaration.InitKeywordRole)
2102+
if (accessor.IsInitOnly && accessorKind != AccessorKind.Init)
21012103
{
21022104
decl.AddChild(new Comment("init", CommentType.MultiLine), Roles.Comment);
21032105
}

0 commit comments

Comments
 (0)