Browse Source

move DecompilerSettings to ICSharpCode.Decompiler

pull/728/head
Siegfried Pammer 10 years ago
parent
commit
ebc0526ac1
  1. 87
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 2
      ICSharpCode.Decompiler/CSharp/DecompilerSettings.cs
  3. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  4. 2
      ICSharpCode.Decompiler/Tests/DecompilerTestBase.cs
  5. 2
      ICSharpCode.Decompiler/Tests/Helpers/Tester.cs
  6. 1
      ILSpy/DecompilationOptions.cs
  7. 1
      ILSpy/ILSpy.csproj
  8. 20
      ILSpy/Languages/CSharpLanguage.cs
  9. 2
      ILSpy/Languages/ILAstLanguage.cs
  10. 1
      ILSpy/Options/DecompilerSettingsPanel.xaml.cs

87
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -45,6 +45,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -45,6 +45,7 @@ namespace ICSharpCode.Decompiler.CSharp
public class CSharpDecompiler
{
readonly DecompilerTypeSystem typeSystem;
readonly DecompilerSettings settings;
List<IILTransform> ilTransforms = new List<IILTransform> {
new OptimizingTransform(),
@ -89,18 +90,86 @@ namespace ICSharpCode.Decompiler.CSharp @@ -89,18 +90,86 @@ namespace ICSharpCode.Decompiler.CSharp
get { return astTransforms; }
}
public CSharpDecompiler(ModuleDefinition module)
: this(new DecompilerTypeSystem(module))
public CSharpDecompiler(ModuleDefinition module, DecompilerSettings settings)
: this(new DecompilerTypeSystem(module), settings)
{
}
public CSharpDecompiler(DecompilerTypeSystem typeSystem)
public CSharpDecompiler(DecompilerTypeSystem typeSystem, DecompilerSettings settings)
{
if (typeSystem == null)
throw new ArgumentNullException("typeSystem");
this.typeSystem = typeSystem;
this.settings = settings;
}
#region MemberIsHidden
public static bool MemberIsHidden(MemberReference member, DecompilerSettings settings)
{
MethodDefinition method = member as MethodDefinition;
if (method != null) {
if (method.IsGetter || method.IsSetter || method.IsAddOn || method.IsRemoveOn)
return true;
if (settings.AnonymousMethods && method.HasGeneratedName() && method.IsCompilerGenerated())
return true;
}
TypeDefinition type = member as TypeDefinition;
if (type != null) {
if (type.DeclaringType != null) {
if (settings.AnonymousMethods && IsClosureType(type))
return true;
// if (settings.YieldReturn && YieldReturnDecompiler.IsCompilerGeneratorEnumerator(type))
// return true;
// if (settings.AsyncAwait && AsyncDecompiler.IsCompilerGeneratedStateMachine(type))
// return true;
} else if (type.IsCompilerGenerated()) {
if (type.Name.StartsWith("<PrivateImplementationDetails>", StringComparison.Ordinal))
return true;
if (type.IsAnonymousType())
return true;
}
}
FieldDefinition field = member as FieldDefinition;
if (field != null) {
if (field.IsCompilerGenerated()) {
if (settings.AnonymousMethods && IsAnonymousMethodCacheField(field))
return true;
if (settings.AutomaticProperties && IsAutomaticPropertyBackingField(field))
return true;
if (settings.SwitchStatementOnString && IsSwitchOnStringCache(field))
return true;
}
// event-fields are not [CompilerGenerated]
if (settings.AutomaticEvents && field.DeclaringType.Events.Any(ev => ev.Name == field.Name))
return true;
}
return false;
}
static bool IsSwitchOnStringCache(FieldDefinition field)
{
return field.Name.StartsWith("<>f__switch", StringComparison.Ordinal);
}
static bool IsAutomaticPropertyBackingField(FieldDefinition field)
{
return field.HasGeneratedName() && field.Name.EndsWith("BackingField", StringComparison.Ordinal);
}
static bool IsAnonymousMethodCacheField(FieldDefinition field)
{
return field.Name.StartsWith("CS$<>", StringComparison.Ordinal) || field.Name.StartsWith("<>f__am", StringComparison.Ordinal);
}
static bool IsClosureType(TypeDefinition type)
{
return type.HasGeneratedName() && type.IsCompilerGenerated() && (type.Name.Contains("DisplayClass") || type.Name.Contains("AnonStorey"));
}
#endregion
TypeSystemAstBuilder CreateAstBuilder(ITypeResolveContext decompilationContext)
{
var typeSystemAstBuilder = new TypeSystemAstBuilder();
@ -151,7 +220,9 @@ namespace ICSharpCode.Decompiler.CSharp @@ -151,7 +220,9 @@ namespace ICSharpCode.Decompiler.CSharp
var typeDef = typeSystem.Resolve(cecilType).GetDefinition();
if (typeDef.Name == "<Module>" && typeDef.Members.Count == 0)
continue;
if (string.IsNullOrEmpty(cecilType.Namespace)) {
if (MemberIsHidden(cecilType, settings))
continue;
if(string.IsNullOrEmpty(cecilType.Namespace)) {
groupNode = syntaxTree;
} else {
if (currentNamespace != cecilType.Namespace) {
@ -247,28 +318,28 @@ namespace ICSharpCode.Decompiler.CSharp @@ -247,28 +318,28 @@ namespace ICSharpCode.Decompiler.CSharp
}
foreach (var field in typeDef.Fields) {
var fieldDef = typeSystem.GetCecil(field) as FieldDefinition;
if (fieldDef != null) {
if (fieldDef != null && !MemberIsHidden(fieldDef, settings)) {
var memberDecl = DoDecompile(fieldDef, field, decompilationContext.WithCurrentMember(field));
typeDecl.Members.Add(memberDecl);
}
}
foreach (var property in typeDef.Properties) {
var propDef = typeSystem.GetCecil(property) as PropertyDefinition;
if (propDef != null) {
if (propDef != null && !MemberIsHidden(propDef, settings)) {
var propDecl = DoDecompile(propDef, property, decompilationContext.WithCurrentMember(property));
typeDecl.Members.Add(propDecl);
}
}
foreach (var @event in typeDef.Events) {
var eventDef = typeSystem.GetCecil(@event) as EventDefinition;
if (eventDef != null) {
if (eventDef != null && !MemberIsHidden(eventDef, settings)) {
var eventDecl = DoDecompile(eventDef, @event, decompilationContext.WithCurrentMember(@event));
typeDecl.Members.Add(eventDecl);
}
}
foreach (var method in typeDef.Methods) {
var methodDef = typeSystem.GetCecil(method) as MethodDefinition;
if (methodDef != null) {
if (methodDef != null && !MemberIsHidden(methodDef, settings)) {
var memberDecl = DoDecompile(methodDef, method, decompilationContext.WithCurrentMember(method));
typeDecl.Members.Add(memberDecl);
}

2
ILSpy/Options/DecompilerSettings.cs → ICSharpCode.Decompiler/CSharp/DecompilerSettings.cs

@ -20,7 +20,7 @@ using System; @@ -20,7 +20,7 @@ using System;
using System.ComponentModel;
using ICSharpCode.NRefactory.CSharp;
namespace ICSharpCode.ILSpy.Options
namespace ICSharpCode.Decompiler.CSharp
{
/// <summary>
/// Settings for the decompiler.

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -63,6 +63,7 @@ @@ -63,6 +63,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CSharp\Annotations.cs" />
<Compile Include="CSharp\DecompilerSettings.cs" />
<Compile Include="CSharp\Transforms\EscapeInvalidIdentifiers.cs" />
<Compile Include="CSharp\Transforms\IntroduceUsingDeclarations.cs" />
<Compile Include="CSharp\Transforms\TransformContext.cs" />

2
ICSharpCode.Decompiler/Tests/DecompilerTestBase.cs

@ -51,7 +51,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -51,7 +51,7 @@ namespace ICSharpCode.Decompiler.Tests
var code = RemoveIgnorableLines(File.ReadLines(fileName));
AssemblyDefinition assembly = CompileLegacy(code, optimize, useDebug, compilerVersion);
CSharpDecompiler decompiler = new CSharpDecompiler(assembly.MainModule);
CSharpDecompiler decompiler = new CSharpDecompiler(assembly.MainModule, new DecompilerSettings());
decompiler.AstTransforms.Insert(0, new RemoveCompilerAttribute());

2
ICSharpCode.Decompiler/Tests/Helpers/Tester.cs

@ -66,7 +66,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -66,7 +66,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
public static string DecompileCSharp(string assemblyFileName)
{
var typeSystem = new DecompilerTypeSystem(ModuleDefinition.ReadModule(assemblyFileName));
CSharpDecompiler decompiler = new CSharpDecompiler(typeSystem);
CSharpDecompiler decompiler = new CSharpDecompiler(typeSystem, new DecompilerSettings());
decompiler.AstTransforms.Insert(0, new RemoveCompilerAttribute());
decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers());
var syntaxTree = decompiler.DecompileWholeModuleAsSingleFile();

1
ILSpy/DecompilationOptions.cs

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
using System;
using System.Threading;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.ILSpy.Options;
namespace ICSharpCode.ILSpy

1
ILSpy/ILSpy.csproj

@ -178,7 +178,6 @@ @@ -178,7 +178,6 @@
<Compile Include="OpenListDialog.xaml.cs">
<DependentUpon>OpenListDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Options\DecompilerSettings.cs" />
<Compile Include="Options\DecompilerSettingsPanel.xaml.cs">
<DependentUpon>DecompilerSettingsPanel.xaml</DependentUpon>
<SubType>Code</SubType>

20
ILSpy/Languages/CSharpLanguage.cs

@ -55,7 +55,7 @@ namespace ICSharpCode.ILSpy @@ -55,7 +55,7 @@ namespace ICSharpCode.ILSpy
internal static IEnumerable<CSharpLanguage> GetDebugLanguages()
{
//DecompilerContext context = new DecompilerContext(ModuleDefinition.CreateModule("dummy", ModuleKind.Dll));
var decompiler = new CSharpDecompiler(ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll));
var decompiler = new CSharpDecompiler(ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll), new DecompilerSettings());
string lastTransformName = "no transforms";
int transformCount = 0;
foreach (var transform in decompiler.AstTransforms) {
@ -93,7 +93,7 @@ namespace ICSharpCode.ILSpy @@ -93,7 +93,7 @@ namespace ICSharpCode.ILSpy
public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options)
{
WriteCommentLine(output, TypeToString(method.DeclaringType, includeNamespace: true));
CSharpDecompiler decompiler = new CSharpDecompiler(method.Module);
CSharpDecompiler decompiler = new CSharpDecompiler(method.Module, options.DecompilerSettings);
while (decompiler.AstTransforms.Count > transformCount)
decompiler.AstTransforms.RemoveAt(decompiler.AstTransforms.Count - 1);
output.WriteLine(decompiler.Decompile(method).ToString());
@ -214,7 +214,7 @@ namespace ICSharpCode.ILSpy @@ -214,7 +214,7 @@ namespace ICSharpCode.ILSpy
*/
public override void DecompileType(TypeDefinition type, ITextOutput output, DecompilationOptions options)
{
CSharpDecompiler decompiler = new CSharpDecompiler(type.Module);
CSharpDecompiler decompiler = new CSharpDecompiler(type.Module, options.DecompilerSettings);
output.WriteLine(decompiler.Decompile(type).ToString());
// AstBuilder codeDomBuilder = CreateAstBuilder(options, currentType: type);
// codeDomBuilder.AddType(type);
@ -327,7 +327,7 @@ namespace ICSharpCode.ILSpy @@ -327,7 +327,7 @@ namespace ICSharpCode.ILSpy
// don't automatically load additional assemblies when an assembly node is selected in the tree view
using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) {
CSharpDecompiler decompiler = new CSharpDecompiler(assembly.ModuleDefinition);
CSharpDecompiler decompiler = new CSharpDecompiler(assembly.ModuleDefinition, options.DecompilerSettings);
if (options.FullDecompilation) {
SyntaxTree st = decompiler.DecompileWholeModuleAsSingleFile();
output.WriteLine(st.ToString());
@ -467,10 +467,10 @@ namespace ICSharpCode.ILSpy @@ -467,10 +467,10 @@ namespace ICSharpCode.ILSpy
#region WriteCodeFilesInProject
bool IncludeTypeWhenDecompilingProject(TypeDefinition type, DecompilationOptions options)
{
// if (type.Name == "<Module>" || CSharpDecompiler.MemberIsHidden(type, options.DecompilerSettings))
// return false;
// if (type.Namespace == "XamlGeneratedNamespace" && type.Name == "GeneratedInternalTypeHelper")
// return false;
if (type.Name == "<Module>" || CSharpDecompiler.MemberIsHidden(type, options.DecompilerSettings))
return false;
if (type.Namespace == "XamlGeneratedNamespace" && type.Name == "GeneratedInternalTypeHelper")
return false;
return true;
}
@ -479,7 +479,7 @@ namespace ICSharpCode.ILSpy @@ -479,7 +479,7 @@ namespace ICSharpCode.ILSpy
// don't automatically load additional assemblies when an assembly node is selected in the tree view
using (LoadedAssembly.DisableAssemblyLoad())
{
CSharpDecompiler decompiler = new CSharpDecompiler(ts);
CSharpDecompiler decompiler = new CSharpDecompiler(ts, options.DecompilerSettings);
decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers());
SyntaxTree syntaxTree = decompiler.DecompileModuleAndAssemblyAttributes();
@ -514,7 +514,7 @@ namespace ICSharpCode.ILSpy @@ -514,7 +514,7 @@ namespace ICSharpCode.ILSpy
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
delegate(IGrouping<string, TypeDefinition> file) {
using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) {
CSharpDecompiler decompiler = new CSharpDecompiler(ts);
CSharpDecompiler decompiler = new CSharpDecompiler(ts, options.DecompilerSettings);
decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers());
var syntaxTree = decompiler.DecompileTypes(file.ToArray());
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(w, options.DecompilerSettings.CSharpFormattingOptions));

2
ILSpy/Languages/ILAstLanguage.cs

@ -89,7 +89,7 @@ namespace ICSharpCode.ILSpy @@ -89,7 +89,7 @@ namespace ICSharpCode.ILSpy
internal static IEnumerable<ILAstLanguage> GetDebugLanguages()
{
yield return new TypedIL();
CSharpDecompiler decompiler = new CSharpDecompiler(ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll));
CSharpDecompiler decompiler = new CSharpDecompiler(ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll), new DecompilerSettings());
for (int i = 0; i <= decompiler.ILTransforms.Count; i++) {
yield return new BlockIL(decompiler.ILTransforms.Take(i).ToList());
}

1
ILSpy/Options/DecompilerSettingsPanel.xaml.cs

@ -20,6 +20,7 @@ using System; @@ -20,6 +20,7 @@ using System;
using System.Windows.Controls;
using System.Xml.Linq;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
namespace ICSharpCode.ILSpy.Options
{

Loading…
Cancel
Save