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
// THE SOFTWARE.
#nullable enable
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler.CSharp.Syntax
{
public enum AccessorKind
{
Any,
Getter,
Setter,
Init,
Adder,
Remover
}
/// <summary>
/// get/set/init/add/remove
/// </summary>
@ -42,21 +54,15 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
get { return SymbolKind.Method; }
}
public AccessorKind Kind { get; set; }
/// <summary>
/// Gets the 'get'/'set'/'init'/'add'/'remove' keyword
/// </summary>
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,
};
}
}
}

22
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);
}

Loading…
Cancel
Save