From df1f1051d4fd69595b00fbe829b52826505e4ea6 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 5 May 2025 01:12:09 +0200 Subject: [PATCH] Generate PatternPlaceholder nodes --- .../DecompilerAstNodeAttribute.cs | 2 +- .../DecompilerSyntaxTreeGenerator.cs | 69 ++++++++++++++++++- .../CSharp/Syntax/AstNode.cs | 48 +------------ .../CSharp/Syntax/AstType.cs | 55 +-------------- .../Expressions/ArrayInitializerExpression.cs | 46 ------------- .../CSharp/Syntax/Expressions/Expression.cs | 48 +------------ .../Syntax/GeneralScope/AttributeSection.cs | 48 +------------ .../Syntax/Statements/BlockStatement.cs | 48 +------------ .../CSharp/Syntax/Statements/Statement.cs | 48 +------------ .../Syntax/Statements/SwitchStatement.cs | 48 +------------ .../Syntax/Statements/TryCatchStatement.cs | 48 +------------ .../TypeMembers/ParameterDeclaration.cs | 48 +------------ .../Syntax/TypeMembers/VariableInitializer.cs | 48 +------------ 13 files changed, 78 insertions(+), 526 deletions(-) diff --git a/ICSharpCode.Decompiler.Generators.Attributes/DecompilerAstNodeAttribute.cs b/ICSharpCode.Decompiler.Generators.Attributes/DecompilerAstNodeAttribute.cs index b0d1924dc..c81232de2 100644 --- a/ICSharpCode.Decompiler.Generators.Attributes/DecompilerAstNodeAttribute.cs +++ b/ICSharpCode.Decompiler.Generators.Attributes/DecompilerAstNodeAttribute.cs @@ -4,7 +4,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax { public sealed class DecompilerAstNodeAttribute : Attribute { - public DecompilerAstNodeAttribute(bool hasNullNode) { } + public DecompilerAstNodeAttribute(bool hasNullNode = false, bool hasPatternPlaceholder = false) { } } public sealed class ExcludeFromMatchAttribute : Attribute diff --git a/ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs b/ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs index 5bcd43a0e..9a8788058 100644 --- a/ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs +++ b/ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs @@ -14,7 +14,7 @@ namespace ICSharpCode.Decompiler.Generators; [Generator] internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator { - record AstNodeAdditions(string NodeName, bool NeedsAcceptImpls, bool NeedsVisitor, bool NeedsNullNode, int NullNodeBaseCtorParamCount, bool IsTypeNode, string VisitMethodName, string VisitMethodParamType, EquatableArray<(string Member, string TypeName, bool RecursiveMatch, bool MatchAny)>? MembersToMatch); + record AstNodeAdditions(string NodeName, bool NeedsAcceptImpls, bool NeedsVisitor, bool NeedsNullNode, bool NeedsPatternPlaceholder, int NullNodeBaseCtorParamCount, bool IsTypeNode, string VisitMethodName, string VisitMethodParamType, EquatableArray<(string Member, string TypeName, bool RecursiveMatch, bool MatchAny)>? MembersToMatch); AstNodeAdditions GetAstNodeAdditions(GeneratorAttributeSyntaxContext context, CancellationToken ct) { @@ -63,6 +63,7 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator return new(targetSymbol.Name, !targetSymbol.MemberNames.Contains("AcceptVisitor"), NeedsVisitor: !targetSymbol.IsAbstract && targetSymbol.BaseType!.IsAbstract, NeedsNullNode: (bool)attribute.ConstructorArguments[0].Value!, + NeedsPatternPlaceholder: (bool)attribute.ConstructorArguments[1].Value!, NullNodeBaseCtorParamCount: targetSymbol.InstanceConstructors.Min(m => m.Parameters.Length), IsTypeNode: targetSymbol.Name == "AstType" || targetSymbol.BaseType?.Name == "AstType", visitMethodName, paramTypeName, membersToMatch?.ToEquatableArray()); @@ -75,6 +76,13 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator builder.AppendLine("namespace ICSharpCode.Decompiler.CSharp.Syntax;"); builder.AppendLine(); + if (source.NeedsPatternPlaceholder) + { + builder.AppendLine("using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;"); + } + + builder.AppendLine(); + builder.AppendLine("#nullable enable"); builder.AppendLine(); @@ -140,6 +148,63 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator } + if (source.NeedsPatternPlaceholder) + { + const string toTypeReferenceCode = @" + + public override Decompiler.TypeSystem.ITypeReference ToTypeReference(Resolver.NameLookupMode lookupMode, Decompiler.TypeSystem.InterningProvider? interningProvider = null) + { + throw new System.NotSupportedException(); + }"; + + builder.Append( + $@" public static implicit operator {source.NodeName}?(PatternMatching.Pattern? pattern) + {{ + return pattern != null ? new PatternPlaceholder(pattern) : null; + }} + + sealed class PatternPlaceholder : {source.NodeName}, INode + {{ + readonly PatternMatching.Pattern child; + + public PatternPlaceholder(PatternMatching.Pattern child) + {{ + this.child = child; + }} + + public override NodeType NodeType {{ + get {{ return NodeType.Pattern; }} + }} + + public override void AcceptVisitor(IAstVisitor visitor) + {{ + visitor.VisitPatternPlaceholder(this, child); + }} + + public override T AcceptVisitor(IAstVisitor visitor) + {{ + return visitor.VisitPatternPlaceholder(this, child); + }} + + public override S AcceptVisitor(IAstVisitor visitor, T data) + {{ + return visitor.VisitPatternPlaceholder(this, child, data); + }} + + protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match) + {{ + return child.DoMatch(other, match); + }} + + bool PatternMatching.INode.DoMatchCollection(Role? role, PatternMatching.INode? pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) + {{ + return child.DoMatchCollection(role, pos, match, backtrackingInfo); + }}{(source.NodeName == "AstType" ? toTypeReferenceCode : "")} + }} +" + ); + } + if (source.NeedsAcceptImpls && source.NeedsVisitor) { builder.Append($@" public override void AcceptVisitor(IAstVisitor visitor) @@ -202,7 +267,7 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator builder.AppendLine("namespace ICSharpCode.Decompiler.CSharp.Syntax;"); source = source - .Concat([new("NullNode", false, true, false, 0, false, "NullNode", "AstNode", null), new("PatternPlaceholder", false, true, false, 0, false, "PatternPlaceholder", "AstNode", null)]) + .Concat([new("NullNode", false, true, false, false, 0, false, "NullNode", "AstNode", null), new("PatternPlaceholder", false, true, false, false, 0, false, "PatternPlaceholder", "AstNode", null)]) .ToImmutableArray(); WriteInterface("IAstVisitor", "void", ""); diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs b/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs index 0f5666c8e..605f6b8e7 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs @@ -37,58 +37,12 @@ using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.Decompiler.CSharp.Syntax { - [DecompilerAstNode(hasNullNode: true)] + [DecompilerAstNode(hasNullNode: true, hasPatternPlaceholder: true)] public abstract partial class AstNode : AbstractAnnotatable, IFreezable, INode, ICloneable { // the Root role must be available when creating the null nodes, so we can't put it in the Roles class internal static readonly Role RootRole = new Role("Root", null); - #region PatternPlaceholder - public static implicit operator AstNode?(PatternMatching.Pattern? pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : AstNode, INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role? role, PatternMatching.INode? pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - AstNode? parent; AstNode? prevSibling; AstNode? nextSibling; diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/AstType.cs b/ICSharpCode.Decompiler/CSharp/Syntax/AstType.cs index 9766e0d77..a8f5cdc97 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/AstType.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/AstType.cs @@ -16,11 +16,9 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -using System; using System.Collections.Generic; using ICSharpCode.Decompiler.CSharp.Resolver; -using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching; using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -28,60 +26,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// A type reference in the C# AST. /// - [DecompilerAstNode(hasNullNode: true)] + [DecompilerAstNode(hasNullNode: true, hasPatternPlaceholder: true)] public abstract partial class AstType : AstNode { - #region PatternPlaceholder - public static implicit operator AstType(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : AstType, INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider) - { - throw new NotSupportedException(); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public override NodeType NodeType { get { return NodeType.TypeReference; } } diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayInitializerExpression.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayInitializerExpression.cs index 9c5458af8..3f29a2c1b 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayInitializerExpression.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayInitializerExpression.cs @@ -94,52 +94,6 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } - - #region PatternPlaceholder - public static implicit operator ArrayInitializerExpression(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : ArrayInitializerExpression, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion } } diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/Expression.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/Expression.cs index 54c5bfb5e..f73e50549 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/Expression.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/Expression.cs @@ -27,55 +27,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// This class is useful even though it doesn't provide any additional functionality: /// It can be used to communicate more information in APIs, e.g. "this subnode will always be an expression" /// - [DecompilerAstNode(hasNullNode: true)] + [DecompilerAstNode(hasNullNode: true, hasPatternPlaceholder: true)] public abstract partial class Expression : AstNode { - #region PatternPlaceholder - public static implicit operator Expression(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : Expression, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public override NodeType NodeType { get { return NodeType.Expression; diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/AttributeSection.cs b/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/AttributeSection.cs index a07706c89..2b74052b1 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/AttributeSection.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/AttributeSection.cs @@ -30,55 +30,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// [AttributeTarget: Attributes] /// - [DecompilerAstNode(hasNullNode: false)] + [DecompilerAstNode(hasNullNode: false, hasPatternPlaceholder: true)] public partial class AttributeSection : AstNode { - #region PatternPlaceholder - public static implicit operator AttributeSection(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : AttributeSection, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public override NodeType NodeType { get { return NodeType.Unknown; diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/BlockStatement.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/BlockStatement.cs index 37a6f3244..e57fa6ad9 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/BlockStatement.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/BlockStatement.cs @@ -31,57 +31,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// { Statements } /// - [DecompilerAstNode(hasNullNode: true)] + [DecompilerAstNode(hasNullNode: true, hasPatternPlaceholder: true)] public partial class BlockStatement : Statement, IEnumerable { public static readonly Role StatementRole = new Role("Statement", Statement.Null); - #region PatternPlaceholder - public static implicit operator BlockStatement(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : BlockStatement, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public CSharpTokenNode LBraceToken { get { return GetChildByRole(Roles.LBrace); } } diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/Statement.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/Statement.cs index 7767a5590..49f87dfaa 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/Statement.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/Statement.cs @@ -27,55 +27,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// This class is useful even though it doesn't provide any additional functionality: /// It can be used to communicate more information in APIs, e.g. "this subnode will always be a statement" /// - [DecompilerAstNode(hasNullNode: true)] + [DecompilerAstNode(hasNullNode: true, hasPatternPlaceholder: true)] public abstract partial class Statement : AstNode { - #region PatternPlaceholder - public static implicit operator Statement(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : Statement, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public new Statement Clone() { return (Statement)base.Clone(); diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs index 4da46969f..53af944eb 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs @@ -72,55 +72,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } - [DecompilerAstNode(hasNullNode: false)] + [DecompilerAstNode(hasNullNode: false, hasPatternPlaceholder: true)] public partial class SwitchSection : AstNode { - #region PatternPlaceholder - public static implicit operator SwitchSection(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : SwitchSection, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public static readonly Role CaseLabelRole = new Role("CaseLabel", null); public override NodeType NodeType { diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/TryCatchStatement.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/TryCatchStatement.cs index 91a8733e8..1a21e7c13 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/TryCatchStatement.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/TryCatchStatement.cs @@ -71,7 +71,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// catch (Type VariableName) { Body } /// - [DecompilerAstNode(hasNullNode: true)] + [DecompilerAstNode(hasNullNode: true, hasPatternPlaceholder: true)] public partial class CatchClause : AstNode { public static readonly TokenRole CatchKeywordRole = new TokenRole("catch"); @@ -80,52 +80,6 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public static readonly TokenRole CondLPar = new TokenRole("("); public static readonly TokenRole CondRPar = new TokenRole(")"); - #region PatternPlaceholder - public static implicit operator CatchClause(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : CatchClause, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public override NodeType NodeType { get { return NodeType.Unknown; diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs index 4f02ef800..d9d0cf1fa 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ParameterDeclaration.cs @@ -30,7 +30,7 @@ using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.Decompiler.CSharp.Syntax { - [DecompilerAstNode(hasNullNode: false)] + [DecompilerAstNode(hasNullNode: false, hasPatternPlaceholder: true)] public partial class ParameterDeclaration : AstNode { public static readonly Role AttributeRole = EntityDeclaration.AttributeRole; @@ -42,52 +42,6 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public static readonly TokenRole InModifierRole = new TokenRole("in"); public static readonly TokenRole ParamsModifierRole = new TokenRole("params"); - #region PatternPlaceholder - public static implicit operator ParameterDeclaration?(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : ParameterDeclaration, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public override NodeType NodeType => NodeType.Unknown; public AstNodeCollection Attributes { diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/VariableInitializer.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/VariableInitializer.cs index 0e51e9299..ee2d6166c 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/VariableInitializer.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/VariableInitializer.cs @@ -27,55 +27,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax { - [DecompilerAstNode(hasNullNode: true)] + [DecompilerAstNode(hasNullNode: true, hasPatternPlaceholder: true)] public partial class VariableInitializer : AstNode { - #region PatternPlaceholder - public static implicit operator VariableInitializer(PatternMatching.Pattern pattern) - { - return pattern != null ? new PatternPlaceholder(pattern) : null; - } - - sealed class PatternPlaceholder : VariableInitializer, PatternMatching.INode - { - readonly PatternMatching.Pattern child; - - public PatternPlaceholder(PatternMatching.Pattern child) - { - this.child = child; - } - - public override NodeType NodeType { - get { return NodeType.Pattern; } - } - - public override void AcceptVisitor(IAstVisitor visitor) - { - visitor.VisitPatternPlaceholder(this, child); - } - - public override T AcceptVisitor(IAstVisitor visitor) - { - return visitor.VisitPatternPlaceholder(this, child); - } - - public override S AcceptVisitor(IAstVisitor visitor, T data) - { - return visitor.VisitPatternPlaceholder(this, child, data); - } - - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) - { - return child.DoMatch(other, match); - } - - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) - { - return child.DoMatchCollection(role, pos, match, backtrackingInfo); - } - } - #endregion - public override NodeType NodeType { get { return NodeType.Unknown;