diff --git a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs index ff5122676..7cf420ba7 100644 --- a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs @@ -543,7 +543,7 @@ namespace ICSharpCode.Decompiler.CSharp argumentList.Arguments.ToList(), argumentList.ArgumentNames); if (((AssignmentExpression)assignment).Left is IndexerExpression indexer && !indexer.Target.IsNull) - indexer.Target.ReplaceWith(n => null); + indexer.Target.Remove(); if (value != null) return assignment; diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs b/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs index 91a05b3e2..b23183c6d 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs @@ -1,4 +1,5 @@ -// +#nullable enable +// // AstNode.cs // // Author: @@ -39,7 +40,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public abstract 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"); + internal static readonly Role RootRole = new Role("Root", null); #region Null public static readonly AstNode Null = new NullAstNode(); @@ -73,7 +74,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return visitor.VisitNullNode(this, data); } - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) + protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match) { return other == null || other.IsNull; } @@ -81,7 +82,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax #endregion #region PatternPlaceholder - public static implicit operator AstNode(PatternMatching.Pattern pattern) + public static implicit operator AstNode?(PatternMatching.Pattern? pattern) { return pattern != null ? new PatternPlaceholder(pattern) : null; } @@ -114,23 +115,23 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return visitor.VisitPatternPlaceholder(this, child, data); } - protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) + 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) + 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; - AstNode firstChild; - AstNode lastChild; + AstNode? parent; + AstNode? prevSibling; + AstNode? nextSibling; + AstNode? firstChild; + AstNode? lastChild; // Flags, from least significant to most significant bits: // - Role.RoleIndexBits: role index @@ -157,7 +158,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax { if (!IsFrozen) { - for (AstNode child = firstChild; child != null; child = child.nextSibling) + for (AstNode? child = firstChild; child != null; child = child.nextSibling) child.Freeze(); flags |= frozenBit; } @@ -197,7 +198,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } - public AstNode Parent { + public AstNode? Parent { get { return parent; } } @@ -224,19 +225,19 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax flags = (flags & ~roleIndexMask) | role.Index; } - public AstNode NextSibling { + public AstNode? NextSibling { get { return nextSibling; } } - public AstNode PrevSibling { + public AstNode? PrevSibling { get { return prevSibling; } } - public AstNode FirstChild { + public AstNode? FirstChild { get { return firstChild; } } - public AstNode LastChild { + public AstNode? LastChild { get { return lastChild; } } @@ -248,8 +249,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public IEnumerable Children { get { - AstNode next; - for (AstNode cur = firstChild; cur != null; cur = next) + AstNode? next; + for (AstNode? cur = firstChild; cur != null; cur = next) { Debug.Assert(cur.parent == this); // Remember next before yielding cur. @@ -265,7 +266,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// public IEnumerable Ancestors { get { - for (AstNode cur = parent; cur != null; cur = cur.parent) + for (AstNode? cur = parent; cur != null; cur = cur.parent) { yield return cur; } @@ -277,7 +278,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// public IEnumerable AncestorsAndSelf { get { - for (AstNode cur = this; cur != null; cur = cur.parent) + for (AstNode? cur = this; cur != null; cur = cur.parent) { yield return cur; } @@ -298,18 +299,18 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax get { return GetDescendantsImpl(true); } } - public IEnumerable DescendantNodes(Func descendIntoChildren = null) + public IEnumerable DescendantNodes(Func? descendIntoChildren = null) { return GetDescendantsImpl(false, descendIntoChildren); } - public IEnumerable DescendantNodesAndSelf(Func descendIntoChildren = null) + public IEnumerable DescendantNodesAndSelf(Func? descendIntoChildren = null) { return GetDescendantsImpl(true, descendIntoChildren); } - IEnumerable GetDescendantsImpl(bool includeSelf, Func descendIntoChildren = null) + IEnumerable GetDescendantsImpl(bool includeSelf, Func? descendIntoChildren = null) { if (includeSelf) { @@ -318,9 +319,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax yield break; } - Stack nextStack = new Stack(); + Stack nextStack = new Stack(); nextStack.Push(null); - AstNode pos = firstChild; + AstNode? pos = firstChild; while (pos != null) { // Remember next before yielding pos. @@ -339,7 +340,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// Gets the first child with the specified role. /// Returns the role's null object if the child is not found. /// - public T GetChildByRole(Role role) where T : AstNode + public T GetChildByRole(Role role) where T : AstNode? { if (role == null) throw new ArgumentNullException(nameof(role)); @@ -352,12 +353,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return role.NullObject; } - public T GetParent() where T : AstNode + public T? GetParent() where T : AstNode { return Ancestors.OfType().FirstOrDefault(); } - public AstNode GetParent(Func pred) + public AstNode GetParent(Func? pred) { return Ancestors.FirstOrDefault(pred); } @@ -392,7 +393,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax AddChildUnsafe(child, role); } - public void AddChildWithExistingRole(AstNode child) + public void AddChildWithExistingRole(AstNode? child) { if (child == null || child.IsNull) return; @@ -419,13 +420,13 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } else { - lastChild.nextSibling = child; + lastChild!.nextSibling = child; child.prevSibling = lastChild; lastChild = child; } } - public void InsertChildBefore(AstNode nextSibling, T child, Role role) where T : AstNode + public void InsertChildBefore(AstNode? nextSibling, T child, Role role) where T : AstNode { if (role == null) throw new ArgumentNullException(nameof(role)); @@ -469,7 +470,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax nextSibling.prevSibling = child; } - public void InsertChildAfter(AstNode prevSibling, T child, Role role) where T : AstNode + public void InsertChildAfter(AstNode? prevSibling, T child, Role role) where T : AstNode { InsertChildBefore((prevSibling == null || prevSibling.IsNull) ? firstChild : prevSibling.nextSibling, child, role); } @@ -511,7 +512,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// Replaces this node with the new node. /// - public void ReplaceWith(AstNode newNode) + public void ReplaceWith(AstNode? newNode) { if (newNode == null || newNode.IsNull) { @@ -578,7 +579,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax nextSibling = null; } - public AstNode ReplaceWith(Func replaceFunction) + public AstNode? ReplaceWith(Func replaceFunction) { if (replaceFunction == null) throw new ArgumentNullException(nameof(replaceFunction)); @@ -587,10 +588,10 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax throw new InvalidOperationException(this.IsNull ? "Cannot replace the null nodes" : "Cannot replace the root node"); } AstNode oldParent = parent; - AstNode oldSuccessor = nextSibling; + AstNode? oldSuccessor = nextSibling; Role oldRole = this.Role; Remove(); - AstNode replacement = replaceFunction(this); + AstNode? replacement = replaceFunction(this); if (oldSuccessor != null && oldSuccessor.parent != oldParent) throw new InvalidOperationException("replace function changed nextSibling of node being replaced?"); if (!(replacement == null || replacement.IsNull)) @@ -626,7 +627,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax copy.flags &= ~frozenBit; // unfreeze the copy // Then perform a deep copy: - for (AstNode cur = firstChild; cur != null; cur = cur.nextSibling) + for (AstNode? cur = firstChild; cur != null; cur = cur.nextSibling) { copy.AddChildUnsafe(cur.Clone(), cur.Role); } @@ -649,37 +650,37 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public abstract S AcceptVisitor(IAstVisitor visitor, T data); #region Pattern Matching - protected static bool MatchString(string pattern, string text) + protected static bool MatchString(string? pattern, string? text) { return PatternMatching.Pattern.MatchString(pattern, text); } - protected internal abstract bool DoMatch(AstNode other, PatternMatching.Match match); + protected internal abstract bool DoMatch(AstNode? other, PatternMatching.Match match); - bool PatternMatching.INode.DoMatch(PatternMatching.INode other, PatternMatching.Match match) + bool PatternMatching.INode.DoMatch(PatternMatching.INode? other, PatternMatching.Match match) { - AstNode o = other as AstNode; + AstNode? o = other as AstNode; // try matching if other is null, or if other is an AstNode return (other == null || o != null) && DoMatch(o, match); } - bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) + bool PatternMatching.INode.DoMatchCollection(Role? role, PatternMatching.INode? pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo? backtrackingInfo) { - AstNode o = pos as AstNode; + AstNode? o = pos as AstNode; return (pos == null || o != null) && DoMatch(o, match); } - PatternMatching.INode PatternMatching.INode.NextSibling { + PatternMatching.INode? PatternMatching.INode.NextSibling { get { return nextSibling; } } - PatternMatching.INode PatternMatching.INode.FirstChild { + PatternMatching.INode? PatternMatching.INode.FirstChild { get { return firstChild; } } #endregion - public AstNode GetNextNode() + public AstNode? GetNextNode() { if (NextSibling != null) return NextSibling; @@ -693,7 +694,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// The next node. /// The predicate. - public AstNode GetNextNode(Func pred) + public AstNode? GetNextNode(Func pred) { var next = GetNextNode(); while (next != null && !pred(next)) @@ -701,7 +702,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return next; } - public AstNode GetPrevNode() + public AstNode? GetPrevNode() { if (PrevSibling != null) return PrevSibling; @@ -715,7 +716,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// The next node. /// The predicate. - public AstNode GetPrevNode(Func pred) + public AstNode? GetPrevNode(Func pred) { var prev = GetPrevNode(); while (prev != null && !pred(prev)) @@ -723,7 +724,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return prev; } // filters all non c# nodes (comments, white spaces or pre processor directives) - public AstNode GetCSharpNodeBefore(AstNode node) + public AstNode? GetCSharpNodeBefore(AstNode node) { var n = node.PrevSibling; while (n != null) @@ -740,7 +741,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// The next node. /// The predicate. - public AstNode GetNextSibling(Func pred) + public AstNode? GetNextSibling(Func pred) { var next = NextSibling; while (next != null && !pred(next)) @@ -753,7 +754,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// The next node. /// The predicate. - public AstNode GetPrevSibling(Func pred) + public AstNode? GetPrevSibling(Func pred) { var prev = PrevSibling; while (prev != null && !pred(prev)) @@ -767,7 +768,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End exclusive) /// - public AstNode GetNodeAt(int line, int column, Predicate pred = null) + public AstNode? GetNodeAt(int line, int column, Predicate? pred = null) { return GetNodeAt(new TextLocation(line, column), pred); } @@ -777,9 +778,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End exclusive) /// - public AstNode GetNodeAt(TextLocation location, Predicate pred = null) + public AstNode? GetNodeAt(TextLocation location, Predicate? pred = null) { - AstNode result = null; + AstNode? result = null; AstNode node = this; while (node.LastChild != null) { @@ -806,7 +807,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End exclusive) /// - public T GetNodeAt(int line, int column) where T : AstNode + public T? GetNodeAt(int line, int column) where T : AstNode { return GetNodeAt(new TextLocation(line, column)); } @@ -816,9 +817,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End exclusive) /// - public T GetNodeAt(TextLocation location) where T : AstNode + public T? GetNodeAt(TextLocation location) where T : AstNode { - T result = null; + T? result = null; AstNode node = this; while (node.LastChild != null) { @@ -848,7 +849,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End inclusive) /// - public AstNode GetAdjacentNodeAt(int line, int column, Predicate pred = null) + public AstNode? GetAdjacentNodeAt(int line, int column, Predicate? pred = null) { return GetAdjacentNodeAt(new TextLocation(line, column), pred); } @@ -858,9 +859,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End inclusive) /// - public AstNode GetAdjacentNodeAt(TextLocation location, Predicate pred = null) + public AstNode? GetAdjacentNodeAt(TextLocation location, Predicate? pred = null) { - AstNode result = null; + AstNode? result = null; AstNode node = this; while (node.LastChild != null) { @@ -887,7 +888,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End inclusive) /// - public T GetAdjacentNodeAt(int line, int column) where T : AstNode + public T? GetAdjacentNodeAt(int line, int column) where T : AstNode { return GetAdjacentNodeAt(new TextLocation(line, column)); } @@ -897,9 +898,9 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// the current method declaration. /// (End inclusive) /// - public T GetAdjacentNodeAt(TextLocation location) where T : AstNode + public T? GetAdjacentNodeAt(TextLocation location) where T : AstNode { - T result = null; + T? result = null; AstNode node = this; while (node.LastChild != null) { @@ -908,8 +909,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax child = child.prevSibling; if (child != null && location <= child.EndLocation) { - if (child is T) - result = (T)child; + if (child is T t) + result = t; node = child; } else @@ -928,7 +929,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// public AstNode GetNodeContaining(TextLocation startLocation, TextLocation endLocation) { - for (AstNode child = firstChild; child != null; child = child.nextSibling) + for (AstNode? child = firstChild; child != null; child = child.nextSibling) { if (child.StartLocation <= startLocation && endLocation <= child.EndLocation) return child.GetNodeContaining(startLocation, endLocation); @@ -949,10 +950,10 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// public IEnumerable GetNodesBetween(TextLocation start, TextLocation end) { - AstNode node = this; + AstNode? node = this; while (node != null) { - AstNode next; + AstNode? next; if (start <= node.StartLocation && node.EndLocation <= end) { // Remember next before yielding node. @@ -984,7 +985,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// /// Formatting options. /// - public virtual string ToString(CSharpFormattingOptions formattingOptions) + public virtual string ToString(CSharpFormattingOptions? formattingOptions) { if (IsNull) return ""; diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/ComposedType.cs b/ICSharpCode.Decompiler/CSharp/Syntax/ComposedType.cs index 05532d89e..c042a2ca9 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/ComposedType.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/ComposedType.cs @@ -41,7 +41,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public static readonly TokenRole ReadonlyRole = new TokenRole("readonly"); public static readonly TokenRole NullableRole = new TokenRole("?"); public static readonly TokenRole PointerRole = new TokenRole("*"); - public static readonly Role ArraySpecifierRole = new Role("ArraySpecifier"); + public static readonly Role ArraySpecifierRole = new Role("ArraySpecifier", null); public AstNodeCollection Attributes { get { return base.GetChildrenByRole(AttributeRole); } } @@ -299,4 +299,3 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } } - diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayCreateExpression.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayCreateExpression.cs index eaad6ff6b..e4314f001 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayCreateExpression.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/ArrayCreateExpression.cs @@ -25,7 +25,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public class ArrayCreateExpression : Expression { public readonly static TokenRole NewKeywordRole = new TokenRole("new"); - public readonly static Role AdditionalArraySpecifierRole = new Role("AdditionalArraySpecifier"); + public readonly static Role AdditionalArraySpecifierRole = new Role("AdditionalArraySpecifier", null); public readonly static Role InitializerRole = new Role("Initializer", ArrayInitializerExpression.Null); public CSharpTokenNode NewToken { diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/QueryExpression.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/QueryExpression.cs index 6b91ec02f..87315b01f 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/QueryExpression.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/QueryExpression.cs @@ -16,13 +16,11 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -using System.Collections.Generic; - namespace ICSharpCode.Decompiler.CSharp.Syntax { public class QueryExpression : Expression { - public static readonly Role ClauseRole = new Role("Clause"); + public static readonly Role ClauseRole = new Role("Clause", null); #region Null public new static readonly QueryExpression Null = new NullQueryExpression(); @@ -420,7 +418,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public class QueryOrderClause : QueryClause { public static readonly TokenRole OrderbyKeywordRole = new TokenRole("orderby"); - public static readonly Role OrderingRole = new Role("Ordering"); + public static readonly Role OrderingRole = new Role("Ordering", null); public CSharpTokenNode OrderbyToken { get { return GetChildByRole(OrderbyKeywordRole); } @@ -585,4 +583,4 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return o != null && this.Projection.DoMatch(o.Projection, match) && this.Key.DoMatch(o.Key, match); } } -} \ No newline at end of file +} diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/SwitchExpression.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/SwitchExpression.cs index 9c9e166d6..ca66a0796 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/SwitchExpression.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Expressions/SwitchExpression.cs @@ -25,7 +25,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public class SwitchExpression : Expression { public static readonly TokenRole SwitchKeywordRole = new TokenRole("switch"); - public static readonly Role SwitchSectionRole = new Role("SwitchSection"); + public static readonly Role SwitchSectionRole = new Role("SwitchSection", null); public Expression Expression { get { return GetChildByRole(Roles.Expression); } diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/PreProcessorDirective.cs b/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/PreProcessorDirective.cs index 87005c003..a1b175d1c 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/PreProcessorDirective.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/GeneralScope/PreProcessorDirective.cs @@ -69,7 +69,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public class PragmaWarningPreprocessorDirective : PreProcessorDirective { - public static readonly Role WarningRole = new Role("Warning"); + public static readonly Role WarningRole = new Role("Warning", null); public static readonly TokenRole PragmaKeywordRole = new TokenRole("#pragma"); public static readonly TokenRole WarningKeywordRole = new TokenRole("warning"); @@ -200,4 +200,3 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } } - diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Role.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Role.cs index 6d9afb8df..2411a2652 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Role.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Role.cs @@ -16,6 +16,8 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable + using System; using System.Threading; @@ -64,7 +66,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax /// Represents the role a node plays within its parent. /// All nodes with this role have type T. /// - public class Role : Role where T : class + public class Role : Role where T : class? { readonly string name; // helps with debugging the AST readonly T nullObject; @@ -86,21 +88,19 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax return node is T; } + [Obsolete("Use the other overload explicitly specifying the nullObject.")] public Role(string name) { if (name == null) throw new ArgumentNullException(nameof(name)); this.name = name; + this.nullObject = null!; } public Role(string name, T nullObject) { - if (name == null) - throw new ArgumentNullException(nameof(name)); - if (nullObject == null) - throw new ArgumentNullException(nameof(nullObject)); + this.name = name ?? throw new ArgumentNullException(nameof(name)); this.nullObject = nullObject; - this.name = name; } public override string ToString() diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs index 10c9ab72c..f55c63542 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Roles.cs @@ -34,18 +34,18 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax // some pre defined constants for common roles public static readonly Role Identifier = new Role("Identifier", Syntax.Identifier.Null); public static readonly Role Body = new Role("Body", BlockStatement.Null); - public static readonly Role Parameter = new Role("Parameter"); + public static readonly Role Parameter = new Role("Parameter", null); public static readonly Role Argument = new Role("Argument", Syntax.Expression.Null); public static readonly Role Type = new Role("Type", AstType.Null); public static readonly Role Expression = new Role("Expression", Syntax.Expression.Null); public static readonly Role TargetExpression = new Role("Target", Syntax.Expression.Null); public readonly static Role Condition = new Role("Condition", Syntax.Expression.Null); - public static readonly Role TypeParameter = new Role("TypeParameter"); + public static readonly Role TypeParameter = new Role("TypeParameter", null); public static readonly Role TypeArgument = new Role("TypeArgument", AstType.Null); - public readonly static Role Constraint = new Role("Constraint"); + public readonly static Role Constraint = new Role("Constraint", null); public static readonly Role Variable = new Role("Variable", VariableInitializer.Null); public static readonly Role EmbeddedStatement = new Role("EmbeddedStatement", Statement.Null); - public readonly static Role TypeMemberRole = new Role("TypeMember"); + public readonly static Role TypeMemberRole = new Role("TypeMember", null); public static readonly Role VariableDesignationRole = new Role("VariableDesignation", VariableDesignation.Null); @@ -68,12 +68,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public static readonly TokenRole Colon = new TokenRole(":"); public static readonly TokenRole DoubleColon = new TokenRole("::"); public static readonly TokenRole Arrow = new TokenRole("=>"); - public static readonly Role Comment = new Role("Comment"); - public static readonly Role PreProcessorDirective = new Role("PreProcessorDirective"); + public static readonly Role Comment = new Role("Comment", null); + public static readonly Role PreProcessorDirective = new Role("PreProcessorDirective", null); public readonly static Role BaseType = new Role("BaseType", AstType.Null); - public static readonly Role Attribute = new Role("Attribute"); + public static readonly Role Attribute = new Role("Attribute", null); public static readonly Role AttributeTargetRole = new Role("AttributeTarget", CSharpTokenNode.Null); public readonly static TokenRole WhereKeyword = new TokenRole("where"); diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/LocalFunctionDeclarationStatement.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/LocalFunctionDeclarationStatement.cs index 105789bb3..da2d1550f 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/LocalFunctionDeclarationStatement.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/LocalFunctionDeclarationStatement.cs @@ -22,7 +22,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax { public class LocalFunctionDeclarationStatement : Statement { - public static readonly Role MethodDeclarationRole = new Role("Method"); + public static readonly Role MethodDeclarationRole = new Role("Method", null); public MethodDeclaration Declaration { get { return GetChildByRole(MethodDeclarationRole); } diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs index c6f3c5745..a3412d1c8 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/Statements/SwitchStatement.cs @@ -33,7 +33,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public class SwitchStatement : Statement { public static readonly TokenRole SwitchKeywordRole = new TokenRole("switch"); - public static readonly Role SwitchSectionRole = new Role("SwitchSection"); + public static readonly Role SwitchSectionRole = new Role("SwitchSection", null); public CSharpTokenNode SwitchToken { get { return GetChildByRole(SwitchKeywordRole); } @@ -134,7 +134,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } #endregion - public static readonly Role CaseLabelRole = new Role("CaseLabel"); + public static readonly Role CaseLabelRole = new Role("CaseLabel", null); public override NodeType NodeType { get { diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/EntityDeclaration.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/EntityDeclaration.cs index d040f0483..28cfb0798 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/EntityDeclaration.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/EntityDeclaration.cs @@ -25,8 +25,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax { public abstract class EntityDeclaration : AstNode { - public static readonly Role AttributeRole = new Role("Attribute"); - public static readonly Role ModifierRole = new Role("Modifier"); + public static readonly Role AttributeRole = new Role("Attribute", null); + public static readonly Role ModifierRole = new Role("Modifier", null); public static readonly Role PrivateImplementationTypeRole = new Role("PrivateImplementationType", AstType.Null); public override NodeType NodeType { diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/FixedFieldDeclaration.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/FixedFieldDeclaration.cs index 32e8a7512..2d31d86ab 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/FixedFieldDeclaration.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/FixedFieldDeclaration.cs @@ -30,7 +30,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax public class FixedFieldDeclaration : EntityDeclaration { public static readonly TokenRole FixedKeywordRole = new TokenRole("fixed"); - public static readonly Role VariableRole = new Role("FixedVariable"); + public static readonly Role VariableRole = new Role("FixedVariable", null); public override SymbolKind SymbolKind { get { return SymbolKind.Field; } @@ -67,4 +67,3 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax } } } -