mirror of https://github.com/icsharpcode/ILSpy.git
152 changed files with 32 additions and 979 deletions
@ -1,47 +0,0 @@ |
|||||||
//
|
|
||||||
// Annotations.cs
|
|
||||||
//
|
|
||||||
// Author:
|
|
||||||
// Luís Reis <luiscubal@gmail.com>
|
|
||||||
//
|
|
||||||
// Copyright (c) 2013 Luís Reis
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
// THE SOFTWARE.
|
|
||||||
|
|
||||||
|
|
||||||
namespace ICSharpCode.NRefactory.CSharp |
|
||||||
{ |
|
||||||
public static class AnnotationNames |
|
||||||
{ |
|
||||||
//Used const instead of readonly to allow values to be used in switch cases.
|
|
||||||
|
|
||||||
public const string AssertionMethodAttribute = "JetBrains.Annotations.AssertionMethodAttribute"; |
|
||||||
public const string AssertionConditionAttribute = "JetBrains.Annotations.AssertionConditionAttribute"; |
|
||||||
public const string AssertionConditionTypeAttribute = "JetBrains.Annotations.AssertionConditionType"; |
|
||||||
|
|
||||||
public const string AssertionConditionTypeIsTrue = "JetBrains.Annotations.AssertionConditionType.IS_TRUE"; |
|
||||||
public const string AssertionConditionTypeIsFalse = "JetBrains.Annotations.AssertionConditionType.IS_FALSE"; |
|
||||||
public const string AssertionConditionTypeIsNull = "JetBrains.Annotations.AssertionConditionType.IS_NULL"; |
|
||||||
public const string AssertionConditionTypeIsNotNull = "JetBrains.Annotations.AssertionConditionType.IS_NOT_NULL"; |
|
||||||
|
|
||||||
public const string NotNullAttribute = "JetBrains.Annotations.NotNullAttribute"; |
|
||||||
public const string CanBeNullAttribute = "JetBrains.Annotations.CanBeNullAttribute"; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
@ -1,159 +0,0 @@ |
|||||||
//
|
|
||||||
// LovalVariableDeclarationSpace.cs
|
|
||||||
//
|
|
||||||
// Author:
|
|
||||||
// Simon Lindgren <simon.n.lindgren@gmail.com>
|
|
||||||
//
|
|
||||||
// Copyright (c) 2013 Simon Lindgren
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
// THE SOFTWARE.
|
|
||||||
|
|
||||||
using System.Collections.Generic; |
|
||||||
using System.Linq; |
|
||||||
using System; |
|
||||||
using ICSharpCode.Decompiler.CSharp.Syntax; |
|
||||||
using ICSharpCode.Decompiler.Util; |
|
||||||
|
|
||||||
namespace ICSharpCode.NRefactory.CSharp.Analysis |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// Represents a declaration space. (§3.3)
|
|
||||||
/// </summary>
|
|
||||||
public class LocalDeclarationSpace |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// Maps from variable name to the declarations in this declaration space.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// This maps from variable name
|
|
||||||
/// </remarks>
|
|
||||||
MultiDictionary<string, AstNode> declarations = new MultiDictionary<string, AstNode> (); |
|
||||||
|
|
||||||
public LocalDeclarationSpace() |
|
||||||
{ |
|
||||||
Children = new List<LocalDeclarationSpace> (); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The child declaration spaces.
|
|
||||||
/// </summary>
|
|
||||||
public IList<LocalDeclarationSpace> Children { |
|
||||||
get; |
|
||||||
private set; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The parent declaration space.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The parent.</value>
|
|
||||||
public LocalDeclarationSpace Parent { |
|
||||||
get; |
|
||||||
private set; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The names declared in this declaration space, excluding child spaces.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The declared names.</value>
|
|
||||||
public ICollection<string> DeclaredNames { |
|
||||||
get { |
|
||||||
return declarations.Keys; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get all nodes declaring the name specified in <paramref name="name"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The declaring nodes.</returns>
|
|
||||||
/// <param name="name">The declaration name.</param>
|
|
||||||
public IEnumerable<AstNode> GetNameDeclarations(string name) |
|
||||||
{ |
|
||||||
return declarations [name].Concat(Children.SelectMany(child => child.GetNameDeclarations(name))); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a child declaration space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="child">The <see cref="LocalDeclarationSpace"/> to add.</param>
|
|
||||||
public void AddChildSpace(LocalDeclarationSpace child) |
|
||||||
{ |
|
||||||
if (child == null) |
|
||||||
throw new ArgumentNullException("child"); |
|
||||||
if (Children.Contains(child)) |
|
||||||
throw new InvalidOperationException("the child was already added"); |
|
||||||
|
|
||||||
Children.Add(child); |
|
||||||
child.Parent = this; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a new declaration to the declaration space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="name">The name of the declared variable.</param>
|
|
||||||
/// <param name="node">A node associated with the declaration.</param>
|
|
||||||
public void AddDeclaration(string name, AstNode node) |
|
||||||
{ |
|
||||||
if (name == null) |
|
||||||
throw new ArgumentNullException("name"); |
|
||||||
if (node == null) |
|
||||||
throw new ArgumentNullException("node"); |
|
||||||
declarations.Add(name, node); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the name exists in the this declaration space.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns><c>true</c>, if the name specified in <paramref name="name"/> is used in this variable declaration space, <c>false</c> otherwise.</returns>
|
|
||||||
/// <param name="name">The name to look for.</param>
|
|
||||||
/// <param name="includeChildren">When <c>true</c>, child declaration spaces are included in the search.</param>
|
|
||||||
public bool ContainsName(string name, bool includeChildren) |
|
||||||
{ |
|
||||||
if (name == null) |
|
||||||
throw new ArgumentNullException("name"); |
|
||||||
|
|
||||||
if (declarations.Keys.Contains(name)) |
|
||||||
return true; |
|
||||||
return includeChildren && Children.Any(child => child.ContainsName(name, true)); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines whether the name specified in <paramref name="name"/> is used in surrouding code.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns><c>true</c> if the name is used, <c>false</c> otherwise.</returns>
|
|
||||||
/// <param name="name">The name to check.</param>
|
|
||||||
/// <remarks>
|
|
||||||
/// Contrary to <see cref="ContainsName"/>, this method also checks parent declaration spaces
|
|
||||||
/// for name conflicts. Typically, this will be the right method to use when determining if a name can be used.
|
|
||||||
/// </remarks>
|
|
||||||
public bool IsNameUsed(string name) |
|
||||||
{ |
|
||||||
if (name == null) |
|
||||||
throw new ArgumentNullException("name"); |
|
||||||
|
|
||||||
return IsNameUsedBySelfOrParent(name) || Children.Any(child => child.ContainsName(name, true)); |
|
||||||
} |
|
||||||
|
|
||||||
bool IsNameUsedBySelfOrParent(string name) |
|
||||||
{ |
|
||||||
if (declarations.Keys.Contains(name)) |
|
||||||
return true; |
|
||||||
return Parent != null && Parent.IsNameUsedBySelfOrParent(name); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,139 +0,0 @@ |
|||||||
//
|
|
||||||
// LocalDeclarationSpaceVisitor.cs
|
|
||||||
//
|
|
||||||
// Author:
|
|
||||||
// Simon Lindgren <simon.n.lindgren@gmail.com>
|
|
||||||
//
|
|
||||||
// Copyright (c) 2013 Simon Lindgren
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 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.Syntax; |
|
||||||
|
|
||||||
namespace ICSharpCode.NRefactory.CSharp.Analysis |
|
||||||
{ |
|
||||||
public class LocalDeclarationSpaceVisitor : DepthFirstAstVisitor |
|
||||||
{ |
|
||||||
LocalDeclarationSpace currentDeclarationSpace; |
|
||||||
Dictionary<AstNode, LocalDeclarationSpace> nodeDeclarationSpaces = new Dictionary<AstNode, LocalDeclarationSpace>(); |
|
||||||
|
|
||||||
public LocalDeclarationSpace GetDeclarationSpace(AstNode node) |
|
||||||
{ |
|
||||||
if (node == null) |
|
||||||
throw new ArgumentNullException("node"); |
|
||||||
while (node != null) { |
|
||||||
LocalDeclarationSpace declarationSpace; |
|
||||||
if (nodeDeclarationSpaces.TryGetValue(node, out declarationSpace)) |
|
||||||
return declarationSpace; |
|
||||||
node = node.Parent; |
|
||||||
} |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
#region Visitor
|
|
||||||
|
|
||||||
void AddDeclaration(string name, AstNode node) |
|
||||||
{ |
|
||||||
if (currentDeclarationSpace != null) |
|
||||||
currentDeclarationSpace.AddDeclaration(name, node); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitVariableInitializer(VariableInitializer variableInitializer) |
|
||||||
{ |
|
||||||
AddDeclaration(variableInitializer.Name, variableInitializer); |
|
||||||
base.VisitVariableInitializer(variableInitializer); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitParameterDeclaration(ParameterDeclaration parameterDeclaration) |
|
||||||
{ |
|
||||||
AddDeclaration(parameterDeclaration.Name, parameterDeclaration); |
|
||||||
base.VisitParameterDeclaration(parameterDeclaration); |
|
||||||
} |
|
||||||
|
|
||||||
void VisitNewDeclarationSpace(AstNode node) |
|
||||||
{ |
|
||||||
var oldDeclarationSpace = currentDeclarationSpace; |
|
||||||
currentDeclarationSpace = new LocalDeclarationSpace(); |
|
||||||
if (oldDeclarationSpace != null) |
|
||||||
oldDeclarationSpace.AddChildSpace(currentDeclarationSpace); |
|
||||||
|
|
||||||
VisitChildren(node); |
|
||||||
|
|
||||||
nodeDeclarationSpaces.Add(node, currentDeclarationSpace); |
|
||||||
currentDeclarationSpace = oldDeclarationSpace; |
|
||||||
} |
|
||||||
|
|
||||||
#region Declaration space creating nodes
|
|
||||||
|
|
||||||
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(methodDeclaration); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitBlockStatement(BlockStatement blockStatement) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(blockStatement); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitSwitchStatement(SwitchStatement switchStatement) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(switchStatement); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitForeachStatement(ForeachStatement foreachStatement) |
|
||||||
{ |
|
||||||
AddDeclaration(foreachStatement.VariableName, foreachStatement); |
|
||||||
VisitNewDeclarationSpace(foreachStatement); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitForStatement(ForStatement forStatement) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(forStatement); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitUsingStatement(UsingStatement usingStatement) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(usingStatement); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitLambdaExpression(LambdaExpression lambdaExpression) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(lambdaExpression); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(anonymousMethodExpression); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitEventDeclaration(EventDeclaration eventDeclaration) |
|
||||||
{ |
|
||||||
AddDeclaration(eventDeclaration.Name, eventDeclaration); |
|
||||||
} |
|
||||||
|
|
||||||
public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) |
|
||||||
{ |
|
||||||
VisitNewDeclarationSpace(eventDeclaration); |
|
||||||
} |
|
||||||
|
|
||||||
#endregion
|
|
||||||
#endregion
|
|
||||||
} |
|
||||||
} |
|
@ -1,123 +0,0 @@ |
|||||||
// Copyright (c) 2011 Daniel Grunwald
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
||||||
// software and associated documentation files (the "Software"), to deal in the Software
|
|
||||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
|
||||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
|
||||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or
|
|
||||||
// substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
||||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
||||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
|
||||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
||||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.IO; |
|
||||||
|
|
||||||
namespace ICSharpCode.Decompiler.Util |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// A binary reader that can read the output of BinaryWriterWith7BitEncodedInts.
|
|
||||||
/// </summary>
|
|
||||||
public sealed class BinaryReaderWith7BitEncodedInts : BinaryReader |
|
||||||
{ |
|
||||||
public BinaryReaderWith7BitEncodedInts(Stream stream) : base(stream) |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
public override short ReadInt16() |
|
||||||
{ |
|
||||||
return unchecked((short)(ushort)base.Read7BitEncodedInt()); |
|
||||||
} |
|
||||||
|
|
||||||
[CLSCompliant(false)] |
|
||||||
public override ushort ReadUInt16() |
|
||||||
{ |
|
||||||
return unchecked((ushort)base.Read7BitEncodedInt()); |
|
||||||
} |
|
||||||
|
|
||||||
public override int ReadInt32() |
|
||||||
{ |
|
||||||
return base.Read7BitEncodedInt(); |
|
||||||
} |
|
||||||
|
|
||||||
[CLSCompliant(false)] |
|
||||||
public override uint ReadUInt32() |
|
||||||
{ |
|
||||||
return unchecked((uint)base.Read7BitEncodedInt()); |
|
||||||
} |
|
||||||
|
|
||||||
public override long ReadInt64() |
|
||||||
{ |
|
||||||
return unchecked((long)this.ReadUInt64()); |
|
||||||
} |
|
||||||
|
|
||||||
[CLSCompliant(false)] |
|
||||||
public override ulong ReadUInt64() |
|
||||||
{ |
|
||||||
ulong num = 0; |
|
||||||
int shift = 0; |
|
||||||
while (shift < 64) { |
|
||||||
byte b = this.ReadByte(); |
|
||||||
num |= (ulong)(b & 127) << shift; |
|
||||||
shift += 7; |
|
||||||
if ((b & 128) == 0) { |
|
||||||
return num; |
|
||||||
} |
|
||||||
} |
|
||||||
throw new FormatException("Invalid 7-bit int64"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A binary writer that encodes all integers as 7-bit-encoded-ints.
|
|
||||||
/// </summary>
|
|
||||||
public sealed class BinaryWriterWith7BitEncodedInts : BinaryWriter |
|
||||||
{ |
|
||||||
public BinaryWriterWith7BitEncodedInts(Stream stream) : base(stream) |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
public override void Write(short value) |
|
||||||
{ |
|
||||||
base.Write7BitEncodedInt(unchecked((ushort)value)); |
|
||||||
} |
|
||||||
|
|
||||||
[CLSCompliant(false)] |
|
||||||
public override void Write(ushort value) |
|
||||||
{ |
|
||||||
base.Write7BitEncodedInt(value); |
|
||||||
} |
|
||||||
|
|
||||||
public override void Write(int value) |
|
||||||
{ |
|
||||||
base.Write7BitEncodedInt(value); |
|
||||||
} |
|
||||||
|
|
||||||
[CLSCompliant(false)] |
|
||||||
public override void Write(uint value) |
|
||||||
{ |
|
||||||
base.Write7BitEncodedInt(unchecked((int)value)); |
|
||||||
} |
|
||||||
|
|
||||||
public override void Write(long value) |
|
||||||
{ |
|
||||||
this.Write(unchecked((ulong)value)); |
|
||||||
} |
|
||||||
|
|
||||||
[CLSCompliant(false)] |
|
||||||
public override void Write(ulong value) |
|
||||||
{ |
|
||||||
while (value >= 128) { |
|
||||||
this.Write(unchecked((byte)(value | 128u))); |
|
||||||
value >>= 7; |
|
||||||
} |
|
||||||
this.Write(unchecked((byte)value)); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,131 +0,0 @@ |
|||||||
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
||||||
// software and associated documentation files (the "Software"), to deal in the Software
|
|
||||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
|
||||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
|
||||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or
|
|
||||||
// substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
||||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
||||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
|
||||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
||||||
// 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 System.Text; |
|
||||||
|
|
||||||
namespace ICSharpCode.Decompiler.Util |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// An immutable stack.
|
|
||||||
///
|
|
||||||
/// Using 'foreach' on the stack will return the items from top to bottom (in the order they would be popped).
|
|
||||||
/// </summary>
|
|
||||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] |
|
||||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")] |
|
||||||
[Serializable] |
|
||||||
public sealed class ImmutableStack<T> : IEnumerable<T> |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// Gets the empty stack instance.
|
|
||||||
/// </summary>
|
|
||||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "ImmutableStack is immutable")] |
|
||||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] |
|
||||||
public static readonly ImmutableStack<T> Empty = new ImmutableStack<T>(); |
|
||||||
|
|
||||||
readonly T value; |
|
||||||
readonly ImmutableStack<T> next; |
|
||||||
|
|
||||||
private ImmutableStack() |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
private ImmutableStack(T value, ImmutableStack<T> next) |
|
||||||
{ |
|
||||||
this.value = value; |
|
||||||
this.next = next; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Pushes an item on the stack. This does not modify the stack itself, but returns a new
|
|
||||||
/// one with the value pushed.
|
|
||||||
/// </summary>
|
|
||||||
public ImmutableStack<T> Push(T item) |
|
||||||
{ |
|
||||||
return new ImmutableStack<T>(item, this); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the item on the top of the stack.
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="InvalidOperationException">The stack is empty.</exception>
|
|
||||||
public T Peek() |
|
||||||
{ |
|
||||||
if (IsEmpty) |
|
||||||
throw new InvalidOperationException("Operation not valid on empty stack."); |
|
||||||
return value; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the item on the top of the stack.
|
|
||||||
/// Returns <c>default(T)</c> if the stack is empty.
|
|
||||||
/// </summary>
|
|
||||||
public T PeekOrDefault() |
|
||||||
{ |
|
||||||
return value; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the stack with the top item removed.
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="InvalidOperationException">The stack is empty.</exception>
|
|
||||||
public ImmutableStack<T> Pop() |
|
||||||
{ |
|
||||||
if (IsEmpty) |
|
||||||
throw new InvalidOperationException("Operation not valid on empty stack."); |
|
||||||
return next; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets if this stack is empty.
|
|
||||||
/// </summary>
|
|
||||||
public bool IsEmpty { |
|
||||||
get { return next == null; } |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets an enumerator that iterates through the stack top-to-bottom.
|
|
||||||
/// </summary>
|
|
||||||
public IEnumerator<T> GetEnumerator() |
|
||||||
{ |
|
||||||
ImmutableStack<T> t = this; |
|
||||||
while (!t.IsEmpty) { |
|
||||||
yield return t.value; |
|
||||||
t = t.next; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|
||||||
{ |
|
||||||
return this.GetEnumerator(); |
|
||||||
} |
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override string ToString() |
|
||||||
{ |
|
||||||
StringBuilder b = new StringBuilder("[Stack"); |
|
||||||
foreach (T val in this) { |
|
||||||
b.Append(' '); |
|
||||||
b.Append(val); |
|
||||||
} |
|
||||||
b.Append(']'); |
|
||||||
return b.ToString(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue