Browse Source

Add SpecializingDecompilerTypeSystem

pull/728/head
Daniel Grunwald 10 years ago
parent
commit
5ead8e9230
  1. 23
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 12
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  3. 4
      ICSharpCode.Decompiler/IL/BlockBuilder.cs
  4. 4
      ICSharpCode.Decompiler/IL/ILReader.cs
  5. 2
      ICSharpCode.Decompiler/IL/Instructions/TransformStackIntoVariablesState.cs
  6. 2
      ICSharpCode.Decompiler/IL/TransformStackIntoVariables.cs
  7. 2
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  8. 100
      ICSharpCode.Decompiler/Tests/Util/IntervalTests.cs
  9. 5
      ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs
  10. 35
      ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs
  11. 0
      ICSharpCode.Decompiler/TypeSystem/ReferenceResolvingException.cs
  12. 67
      ICSharpCode.Decompiler/TypeSystem/SpecializingDecompilerTypeSystem.cs
  13. 0
      ICSharpCode.Decompiler/TypeSystem/TypesHierarchyHelpers.cs

23
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -23,6 +23,7 @@ using System.Linq; @@ -23,6 +23,7 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp.Refactoring;
using ICSharpCode.NRefactory.TypeSystem;
@ -193,12 +194,30 @@ namespace ICSharpCode.Decompiler.CSharp @@ -193,12 +194,30 @@ namespace ICSharpCode.Decompiler.CSharp
return entityDecl;
}
IDecompilerTypeSystem GetSpecializingTypeSystem(ITypeResolveContext decompilationContext)
{
IList<IType> classTypeParameters = null;
IList<IType> methodTypeParameters = null;
if (decompilationContext.CurrentTypeDefinition != null)
classTypeParameters = decompilationContext.CurrentTypeDefinition.TypeArguments;
IMethod method = decompilationContext.CurrentMember as IMethod;
if (method != null)
methodTypeParameters = method.TypeArguments;
if ((classTypeParameters != null && classTypeParameters.Count > 0) || (methodTypeParameters != null && methodTypeParameters.Count > 0))
return new SpecializingDecompilerTypeSystem(typeSystem, new TypeParameterSubstitution(classTypeParameters, methodTypeParameters));
else
return typeSystem;
}
void DecompileBody(MethodDefinition methodDefinition, IMethod method, EntityDeclaration entityDecl, ITypeResolveContext decompilationContext, TypeSystemAstBuilder typeSystemAstBuilder)
{
var ilReader = new ILReader(typeSystem);
var specializingTypeSystem = GetSpecializingTypeSystem(decompilationContext);
var ilReader = new ILReader(specializingTypeSystem);
var function = ilReader.ReadIL(methodDefinition.Body, CancellationToken);
function.CheckInvariant();
var context = new ILTransformContext { TypeSystem = typeSystem };
var context = new ILTransformContext { TypeSystem = specializingTypeSystem };
foreach (var transform in ilTransforms) {
transform.Run(function, context);
function.CheckInvariant();

12
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -76,7 +76,6 @@ @@ -76,7 +76,6 @@
<Compile Include="CSharp\Transforms\IAstTransform.cs" />
<Compile Include="CSharp\Transforms\IntroduceUnsafeModifier.cs" />
<Compile Include="CSharp\Transforms\ReplaceMethodCallsWithOperators.cs" />
<Compile Include="DecompilerTypeSystem.cs" />
<Compile Include="IL\Instructions.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
@ -106,7 +105,6 @@ @@ -106,7 +105,6 @@
<Compile Include="IL\TransformingVisitor.cs" />
<Compile Include="IL\TransformStackIntoVariables.cs" />
<Compile Include="IL\UnionFind.cs" />
<Compile Include="TypesHierarchyHelpers.cs" />
<Compile Include="CecilExtensions.cs" />
<Compile Include="Disassembler\DisassemblerHelpers.cs" />
<Compile Include="Disassembler\ILStructure.cs" />
@ -130,7 +128,11 @@ @@ -130,7 +128,11 @@
<Compile Include="Output\PlainTextOutput.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Output\TextOutputWriter.cs" />
<Compile Include="ReferenceResolvingException.cs" />
<Compile Include="TypeSystem\DecompilerTypeSystem.cs" />
<Compile Include="TypeSystem\SpecializingDecompilerTypeSystem.cs" />
<Compile Include="TypeSystem\IDecompilerTypeSystem.cs" />
<Compile Include="TypeSystem\ReferenceResolvingException.cs" />
<Compile Include="TypeSystem\TypesHierarchyHelpers.cs" />
<Compile Include="Util\Argument.cs" />
<Compile Include="Util\CollectionExtensions.cs" />
<Compile Include="Util\Interval.cs" />
@ -166,7 +168,9 @@ @@ -166,7 +168,9 @@
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="TypeSystem" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<Target Name="BeforeBuild">
<MSBuild Projects="$(MSBuildProjectDirectory)\..\BuildTools\UpdateAssemblyInfo\UpdateAssemblyInfo.csproj" Targets="Build" Properties="Configuration=Debug" />

4
ICSharpCode.Decompiler/IL/BlockBuilder.cs

@ -30,9 +30,9 @@ namespace ICSharpCode.Decompiler.IL @@ -30,9 +30,9 @@ namespace ICSharpCode.Decompiler.IL
class BlockBuilder
{
readonly Mono.Cecil.Cil.MethodBody body;
readonly DecompilerTypeSystem typeSystem;
readonly IDecompilerTypeSystem typeSystem;
public BlockBuilder(Mono.Cecil.Cil.MethodBody body, DecompilerTypeSystem typeSystem)
public BlockBuilder(Mono.Cecil.Cil.MethodBody body, IDecompilerTypeSystem typeSystem)
{
Debug.Assert(body != null);
Debug.Assert(typeSystem != null);

4
ICSharpCode.Decompiler/IL/ILReader.cs

@ -48,9 +48,9 @@ namespace ICSharpCode.Decompiler.IL @@ -48,9 +48,9 @@ namespace ICSharpCode.Decompiler.IL
}
readonly ICompilation compilation;
readonly DecompilerTypeSystem typeSystem;
readonly IDecompilerTypeSystem typeSystem;
public ILReader(DecompilerTypeSystem typeSystem)
public ILReader(IDecompilerTypeSystem typeSystem)
{
if (typeSystem == null)
throw new ArgumentNullException("typeSystem");

2
ICSharpCode.Decompiler/IL/Instructions/TransformStackIntoVariablesState.cs

@ -29,7 +29,7 @@ namespace ICSharpCode.Decompiler.IL @@ -29,7 +29,7 @@ namespace ICSharpCode.Decompiler.IL
public Stack<ILVariable> Variables { get; set; }
public UnionFind<ILVariable> UnionFind { get; set; }
public Dictionary<Block, ImmutableArray<ILVariable>> InitialVariables { get; set; }
public DecompilerTypeSystem TypeSystem { get; set; }
public IDecompilerTypeSystem TypeSystem { get; set; }
public TransformStackIntoVariablesState()
{

2
ICSharpCode.Decompiler/IL/TransformStackIntoVariables.cs

@ -24,7 +24,7 @@ namespace ICSharpCode.Decompiler.IL @@ -24,7 +24,7 @@ namespace ICSharpCode.Decompiler.IL
{
public class ILTransformContext
{
public DecompilerTypeSystem TypeSystem { get; set; }
public IDecompilerTypeSystem TypeSystem { get; set; }
}
public interface IILTransform

2
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -107,12 +107,14 @@ @@ -107,12 +107,14 @@
<Compile Include="TestCases\HelloWorld.cs" />
<Compile Include="TestCases\PropertiesAndEvents.cs" />
<Compile Include="TestRunner.cs" />
<Compile Include="Util\IntervalTests.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<Folder Include="ILTransforms" />
<Folder Include="Util" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project>

100
ICSharpCode.Decompiler/Tests/Util/IntervalTests.cs

@ -0,0 +1,100 @@ @@ -0,0 +1,100 @@
// 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 System.Diagnostics;
using ICSharpCode.Decompiler.IL;
using NUnit.Framework;
using ICSharpCode.Decompiler.Tests.Helpers;
namespace ICSharpCode.Decompiler.Tests.ILTransforms
{
public class IntervalTests
{
[Test]
public void DefaultIsEmpty()
{
Assert.IsTrue(default(Interval).IsEmpty);
Assert.IsFalse(default(Interval).Contains(-1));
Assert.IsFalse(default(Interval).Contains(0));
Assert.IsFalse(default(Interval).Contains(1));
}
[Test]
public void EmptyAt1()
{
Interval i = new Interval(1, 1);
Assert.IsTrue(default(Interval).IsEmpty);
Assert.IsFalse(default(Interval).Contains(-1));
Assert.IsFalse(default(Interval).Contains(0));
Assert.IsFalse(default(Interval).Contains(1));
Assert.IsFalse(default(Interval).Contains(2));
}
[Test]
public void OneToThree()
{
Interval i = new Interval(1, 3);
Assert.IsFalse(i.IsEmpty);
Assert.IsFalse(i.Contains(0));
Assert.IsTrue(i.Contains(1));
Assert.IsTrue(i.Contains(2));
Assert.IsFalse(i.Contains(3));
}
[Test]
public void FullInterval()
{
Interval full = new Interval(int.MinValue, int.MinValue);
Assert.IsFalse(full.IsEmpty);
Assert.IsTrue(full.Contains(int.MinValue));
Assert.IsTrue(full.Contains(0));
Assert.IsTrue(full.Contains(int.MaxValue));
}
[Test]
public void NonNegativeIntegers()
{
Interval i = new Interval(0, int.MinValue);
Assert.IsFalse(i.IsEmpty);
Assert.IsTrue(i.Contains(0));
Assert.IsTrue(i.Contains(1000));
Assert.IsTrue(i.Contains(int.MaxValue));
Assert.IsFalse(i.Contains(-1));
Assert.IsFalse(i.Contains(-1000));
Assert.IsFalse(i.Contains(int.MinValue));
}
[Test]
public void Intersection()
{
Interval empty = new Interval(0, 0);
Interval emptyAtOne = new Interval(0, 0);
Interval zero = new Interval(0, 1);
Interval full = new Interval(int.MinValue, int.MinValue);
Interval nonneg = new Interval(0, int.MinValue);
Interval nonpos = new Interval(int.MinValue, 1);
Interval maxval = new Interval(int.MaxValue, int.MinValue);
Assert.AreEqual(nonneg, full.Intersect(nonneg));
Assert.AreEqual(nonneg, nonneg.Intersect(full));
Assert.AreEqual(zero, nonneg.Intersect(zero));
Assert.AreEqual(zero, nonneg.Intersect(nonpos));
Assert.AreEqual(maxval, nonneg.Intersect(maxval));
Assert.AreEqual(empty, nonpos.Intersect(maxval));
}
}
}

5
ICSharpCode.Decompiler/DecompilerTypeSystem.cs → ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs

@ -16,7 +16,7 @@ namespace ICSharpCode.Decompiler @@ -16,7 +16,7 @@ namespace ICSharpCode.Decompiler
/// Manages the NRefactory type system for the decompiler.
/// This class is thread-safe.
/// </summary>
public class DecompilerTypeSystem
public class DecompilerTypeSystem : IDecompilerTypeSystem
{
readonly ModuleDefinition moduleDefinition;
readonly ICompilation compilation;
@ -112,9 +112,6 @@ namespace ICSharpCode.Decompiler @@ -112,9 +112,6 @@ namespace ICSharpCode.Decompiler
}
#region Resolve Type
/// <summary>
/// Retrieves a type definition for a type defined in the compilation's main assembly.
/// </summary>
public IType Resolve(TypeReference typeReference)
{
if (typeReference == null)

35
ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
// 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.NRefactory.TypeSystem;
using Mono.Cecil;
namespace ICSharpCode.Decompiler
{
/// <summary>
/// Allows resolving cecil types into the NRefactory type system.
/// </summary>
public interface IDecompilerTypeSystem
{
ICompilation Compilation { get; }
IType Resolve(TypeReference typeReference);
IField Resolve(FieldReference fieldReference);
IMethod Resolve(MethodReference methodReference);
}
}

0
ICSharpCode.Decompiler/ReferenceResolvingException.cs → ICSharpCode.Decompiler/TypeSystem/ReferenceResolvingException.cs

67
ICSharpCode.Decompiler/TypeSystem/SpecializingDecompilerTypeSystem.cs

@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
// 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.NRefactory.TypeSystem;
namespace ICSharpCode.Decompiler
{
/// <summary>
/// Decompiler type system for generic types or methods:
/// used to replace the dummy type parameters by the actual type parameters of the method being decompiled.
/// </summary>
public class SpecializingDecompilerTypeSystem : IDecompilerTypeSystem
{
readonly IDecompilerTypeSystem context;
readonly TypeParameterSubstitution substitution;
public SpecializingDecompilerTypeSystem(IDecompilerTypeSystem context, TypeParameterSubstitution substitution)
{
if (context == null)
throw new ArgumentNullException("context");
if (substitution == null)
throw new ArgumentNullException("substitution");
this.context = context;
this.substitution = substitution;
}
public ICompilation Compilation {
get { return context.Compilation; }
}
public IType Resolve(Mono.Cecil.TypeReference typeReference)
{
return context.Resolve(typeReference).AcceptVisitor(substitution);
}
public IField Resolve(Mono.Cecil.FieldReference fieldReference)
{
IField field = context.Resolve(fieldReference);
if (field != null)
field = (IField)field.Specialize(substitution);
return field;
}
public IMethod Resolve(Mono.Cecil.MethodReference methodReference)
{
IMethod method = context.Resolve(methodReference);
if (method != null)
method = (IMethod)method.Specialize(substitution);
return method;
}
}
}

0
ICSharpCode.Decompiler/TypesHierarchyHelpers.cs → ICSharpCode.Decompiler/TypeSystem/TypesHierarchyHelpers.cs

Loading…
Cancel
Save