From 7c318157d5db2cee9c4b0a66c8637efe4934d482 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Thu, 12 Mar 2015 23:53:06 +0100 Subject: [PATCH] TypeSystem helper for unit tests --- .../IL/Instructions/Block.cs | 18 ++++++- ICSharpCode.Decompiler/IL/SemanticHelper.cs | 2 + .../Properties/AssemblyInfo.template.cs | 3 ++ .../Tests/Helpers/TypeSystemHelper.cs | 51 ++++++++++++++++++ .../Tests/ICSharpCode.Decompiler.Tests.csproj | 15 +++++- .../Tests/ILTransforms/Inlining.cs | 52 +++++++++++++++++++ 6 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 ICSharpCode.Decompiler/Tests/Helpers/TypeSystemHelper.cs create mode 100644 ICSharpCode.Decompiler/Tests/ILTransforms/Inlining.cs diff --git a/ICSharpCode.Decompiler/IL/Instructions/Block.cs b/ICSharpCode.Decompiler/IL/Instructions/Block.cs index 97a638e71..f574f96a8 100644 --- a/ICSharpCode.Decompiler/IL/Instructions/Block.cs +++ b/ICSharpCode.Decompiler/IL/Instructions/Block.cs @@ -57,7 +57,7 @@ namespace ICSharpCode.Decompiler.IL /// However, this is just of theoretical interest; we currently don't plan to use inline blocks that /// pop elements that they didn't push themselves. /// - partial class Block : ILInstruction + partial class Block : ILInstruction, IEnumerable { public readonly InstructionCollection Instructions; @@ -150,5 +150,21 @@ namespace ICSharpCode.Decompiler.IL // an inline block has no phase-1 effects, so we're immediately done with inlining return this; } + + // Add() and GetEnumerator() for collection initializer support: + public void Add(ILInstruction instruction) + { + this.Instructions.Add(instruction); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this.Instructions.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return this.Instructions.GetEnumerator(); + } } } diff --git a/ICSharpCode.Decompiler/IL/SemanticHelper.cs b/ICSharpCode.Decompiler/IL/SemanticHelper.cs index 39b1c9804..b39f4379f 100644 --- a/ICSharpCode.Decompiler/IL/SemanticHelper.cs +++ b/ICSharpCode.Decompiler/IL/SemanticHelper.cs @@ -22,6 +22,8 @@ namespace ICSharpCode.Decompiler.IL { static class SemanticHelper { + // TODO: consider moving IfInstruction.CombineFlags and Block.Phase1Boundary here + /// /// Gets whether the instruction sequence 'inst1; inst2;' may be ordered to 'inst2; inst1;' /// diff --git a/ICSharpCode.Decompiler/Properties/AssemblyInfo.template.cs b/ICSharpCode.Decompiler/Properties/AssemblyInfo.template.cs index bec57df5f..059625750 100644 --- a/ICSharpCode.Decompiler/Properties/AssemblyInfo.template.cs +++ b/ICSharpCode.Decompiler/Properties/AssemblyInfo.template.cs @@ -3,6 +3,7 @@ using System; using System.Resources; using System.Reflection; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; #endregion @@ -25,3 +26,5 @@ using System.Runtime.InteropServices; [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2243:AttributeStringLiteralsShouldParseCorrectly", Justification = "AssemblyInformationalVersion does not need to be a parsable version")] + +[assembly: InternalsVisibleTo("ICSharpCode.Decompiler.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001004dcf3979c4e902efa4dd2163a039701ed5822e6f1134d77737296abbb97bf0803083cfb2117b4f5446a217782f5c7c634f9fe1fc60b4c11d62c5b3d33545036706296d31903ddcf750875db38a8ac379512f51620bb948c94d0831125fbc5fe63707cbb93f48c1459c4d1749eb7ac5e681a2f0d6d7c60fa527a3c0b8f92b02bf")] diff --git a/ICSharpCode.Decompiler/Tests/Helpers/TypeSystemHelper.cs b/ICSharpCode.Decompiler/Tests/Helpers/TypeSystemHelper.cs new file mode 100644 index 000000000..54f0414b9 --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/Helpers/TypeSystemHelper.cs @@ -0,0 +1,51 @@ +// Copyright (c) 2015 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.Collections.Concurrent; +using System.Linq; +using System.Reflection; +using ICSharpCode.Decompiler.IL; +using ICSharpCode.NRefactory.TypeSystem; +using ICSharpCode.NRefactory.TypeSystem.Implementation; +using Mono.Cecil; + +namespace ICSharpCode.Decompiler.Tests.Helpers +{ + /// + /// Helper class for building NRefactory type systems in test cases. + /// + public static class TypeSystem + { + static readonly Lazy decompilerTypeSystem = new Lazy( + delegate { + var module = ModuleDefinition.ReadModule(typeof(TypeSystem).Module.FullyQualifiedName); + return new DecompilerTypeSystem(module); + }); + + public static IAssembly FromReflection(Assembly assembly) + { + return decompilerTypeSystem.Value.Compilation.Assemblies.Single(asm => asm.AssemblyName == assembly.GetName().Name); + } + + public static IType FromReflection(Type type) + { + return decompilerTypeSystem.Value.Compilation.FindType(type); + } + } +} diff --git a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj index b84e7c4ef..73fb97389 100644 --- a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj @@ -16,6 +16,11 @@ False 67,169,1058,728,1720,649 + False + True + ..\..\NRefactory\ICSharpCode.NRefactory.snk + False + File x86 @@ -73,6 +78,10 @@ {D68133BD-1E63-496E-9EDE-4FBDBF77B486} Mono.Cecil + + {2B8F4F83-C2B3-4E84-A27B-8DEE1BE0E006} + ICSharpCode.NRefactory.Cecil + {53DCA265-3C3C-42F9-B647-F72BA678122B} ICSharpCode.NRefactory.CSharp @@ -87,10 +96,11 @@ - + + @@ -100,5 +110,8 @@ + + + \ No newline at end of file diff --git a/ICSharpCode.Decompiler/Tests/ILTransforms/Inlining.cs b/ICSharpCode.Decompiler/Tests/ILTransforms/Inlining.cs new file mode 100644 index 000000000..2fb3ee21d --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/ILTransforms/Inlining.cs @@ -0,0 +1,52 @@ +// Copyright (c) 2014 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 ICSharpCode.Decompiler.IL; +using NUnit.Framework; + +namespace ICSharpCode.Decompiler.Tests.ILTransforms +{ + /// + /// IL Inlining unit tests. + /// + public class Inlining + { + static ILFunction MakeFunction(ILInstruction body) + { + var f = new ILFunction(null, body); + f.AddRef(); + return f; + } + + [Test] + public void Simple() + { + var f = MakeFunction( + new Block { + new LdcI4(1), + new LdcI4(2), + new Add(new Pop(StackType.I4), new Pop(StackType.I4), false, Sign.Signed) + }); + f.AcceptVisitor(new TransformingVisitor()); + /*Assert.AreEqual( + , + f.Body + );*/ + } + } +}