Browse Source

Add AccessorKind to make Accessor keywords independent of their role.

ast-source-generator
Siegfried Pammer 3 months ago
parent
commit
bcac87c8f0
  1. 38
      ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/Accessor.cs
  2. 22
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

38
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 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#nullable enable
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler.CSharp.Syntax namespace ICSharpCode.Decompiler.CSharp.Syntax
{ {
public enum AccessorKind
{
Any,
Getter,
Setter,
Init,
Adder,
Remover
}
/// <summary> /// <summary>
/// get/set/init/add/remove /// get/set/init/add/remove
/// </summary> /// </summary>
@ -42,21 +54,15 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
get { return SymbolKind.Method; } get { return SymbolKind.Method; }
} }
public AccessorKind Kind { get; set; }
/// <summary> /// <summary>
/// Gets the 'get'/'set'/'init'/'add'/'remove' keyword /// Gets the 'get'/'set'/'init'/'add'/'remove' keyword
/// </summary> /// </summary>
public CSharpTokenNode Keyword { public CSharpTokenNode Keyword {
get { get {
for (AstNode child = this.FirstChild; child != null; child = child.NextSibling) var role = GetAccessorKeywordRole(Kind);
{ return role == null ? CSharpTokenNode.Null : GetChildByRole(role);
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;
} }
} }
@ -65,10 +71,16 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
set { SetChildByRole(Roles.Body, value); } 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 kind switch {
return o != null && !o.IsNull && this.MatchAttributesAndModifiers(o, match) && this.Body.DoMatch(o.Body, match); AccessorKind.Getter => PropertyDeclaration.GetKeywordRole,
AccessorKind.Setter => PropertyDeclaration.SetKeywordRole,
AccessorKind.Init => PropertyDeclaration.InitKeywordRole,
AccessorKind.Adder => CustomEventDeclaration.AddKeywordRole,
AccessorKind.Remover => CustomEventDeclaration.RemoveKeywordRole,
_ => null,
};
} }
} }
} }

22
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -2082,22 +2082,24 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
decl.Modifiers = ModifierFromAccessibility(accessor.Accessibility, UsePrivateProtectedAccessibility); decl.Modifiers = ModifierFromAccessibility(accessor.Accessibility, UsePrivateProtectedAccessibility);
if (this.ShowModifiers && accessor.HasReadonlyModifier()) if (this.ShowModifiers && accessor.HasReadonlyModifier())
decl.Modifiers |= Modifiers.Readonly; decl.Modifiers |= Modifiers.Readonly;
TokenRole keywordRole = kind switch { AccessorKind accessorKind = kind switch {
MethodSemanticsAttributes.Getter => PropertyDeclaration.GetKeywordRole, MethodSemanticsAttributes.Getter => AccessorKind.Getter,
MethodSemanticsAttributes.Setter => PropertyDeclaration.SetKeywordRole, MethodSemanticsAttributes.Setter => AccessorKind.Setter,
MethodSemanticsAttributes.Adder => CustomEventDeclaration.AddKeywordRole, MethodSemanticsAttributes.Adder => AccessorKind.Adder,
MethodSemanticsAttributes.Remover => CustomEventDeclaration.RemoveKeywordRole, MethodSemanticsAttributes.Remover => AccessorKind.Remover,
_ => null _ => AccessorKind.Any
}; };
if (kind == MethodSemanticsAttributes.Setter && SupportInitAccessors && accessor.IsInitOnly) 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); decl.AddChild(new Comment("init", CommentType.MultiLine), Roles.Comment);
} }

Loading…
Cancel
Save