Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/icsharpcode/ILSpy
Browse files Browse the repository at this point in the history
Conflicts:
	ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
	ILSpy.SharpDevelop.LGPL/ILSpy.SharpDevelop.LGPL.csproj
  • Loading branch information
pentp committed Jul 19, 2011
2 parents 26d903e + 9bfb3ed commit d2488a4
Show file tree
Hide file tree
Showing 208 changed files with 10,314 additions and 955 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!-- Updated 2005 by Daniel Grunwald for VB.NET 2.0 -->
<!-- Converted to AvalonEdit format by Daniel Grunwald in 2010 -->
<!-- Updated 2010 by Siegfried Oleg Pammer for VB.NET 9 and 10 -->
<!-- Updated 2011 by Siegfried Oleg Pammer for VB 11 CTP -->
<SyntaxDefinition name="VBNET" extensions=".vb" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" exampleText="' comment" />
<Color name="String" exampleText="text = &quot;Hello, World!&quot;" />
Expand Down Expand Up @@ -64,6 +65,7 @@
<Word>AddressOf</Word>
<Word>And</Word>
<Word>AndAlso</Word>
<Word>Await</Word>
<Word>Is</Word>
<Word>IsNot</Word>
<Word>Like</Word>
Expand Down Expand Up @@ -208,6 +210,7 @@
<Word>Aggregate</Word>
<Word>Ansi</Word>
<Word>Ascending</Word>
<Word>Async</Word>
<Word>Auto</Word>
<Word>Binary</Word>
<Word>By</Word>
Expand All @@ -221,6 +224,7 @@
<Word>Group</Word>
<Word>Infer</Word>
<Word>Into</Word>
<Word>Iterator</Word>
<Word>Join</Word>
<Word>Key</Word>
<Word>Off</Word>
Expand All @@ -232,6 +236,7 @@
<Word>Unicode</Word>
<Word>Until</Word>
<Word>Where</Word>
<Word>Yield</Word>
</Keywords>
</RuleSet>
<RuleSet name="PreprocessorSet" ignoreCase="true">
Expand Down
11 changes: 9 additions & 2 deletions Debugger/ILSpy.Debugger/Commands/DebuggerCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Windows.Interop;
using System.Windows.Media;
using System.Xml.Linq;

using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.ILSpy.Bookmarks;
using ICSharpCode.ILSpy.Debugger;
Expand All @@ -20,6 +19,7 @@
using ICSharpCode.ILSpy.TreeNodes;
using ICSharpCode.TreeView;
using Microsoft.Win32;
using Mono.Cecil;

namespace ICSharpCode.ILSpy.Debugger.Commands
{
Expand Down Expand Up @@ -182,7 +182,14 @@ internal sealed class DebugExecutableNodeCommand : DebuggerCommand, IContextMenu
{
public bool IsVisible(SharpTreeNode[] selectedNodes)
{
return selectedNodes.All(n => n is AssemblyTreeNode && null != (n as AssemblyTreeNode).LoadedAssembly.AssemblyDefinition.EntryPoint);
return selectedNodes.All(
delegate (SharpTreeNode n) {
AssemblyTreeNode a = n as AssemblyTreeNode;
if (a == null)
return false;
AssemblyDefinition asm = a.LoadedAssembly.AssemblyDefinition;
return asm != null && asm.EntryPoint != null;
});
}

public bool IsEnabled(SharpTreeNode[] selectedNodes)
Expand Down
5 changes: 1 addition & 4 deletions Debugger/ILSpy.Debugger/ILSpy.Debugger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<ProjectGuid>{6D3D0F0D-348D-456A-A6ED-E9BD5EFABB6A}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<RootNamespace>ICSharpCode.ILSpy.Debugger</RootNamespace>
<AssemblyName>ILSpy.Debugger.Plugin</AssemblyName>
Expand Down Expand Up @@ -42,9 +42,6 @@
<PlatformTarget>AnyCPU</PlatformTarget>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x86' ">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
Expand Down
15 changes: 15 additions & 0 deletions ICSharpCode.Decompiler/Ast/Annotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Mono.Cecil;

namespace ICSharpCode.Decompiler.Ast
{
public class TypeInformation
{
public readonly TypeReference InferredType;

public TypeInformation(TypeReference inferredType)
{
this.InferredType = inferredType;
}
}
}
30 changes: 20 additions & 10 deletions ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public BlockStatement CreateMethodBody(IEnumerable<ParameterDeclaration> paramet
context.CancellationToken.ThrowIfCancellationRequested();
ILBlock ilMethod = new ILBlock();
ILAstBuilder astBuilder = new ILAstBuilder();
ilMethod.Body = astBuilder.Build(methodDef, true);
ilMethod.Body = astBuilder.Build(methodDef, true, context);

context.CancellationToken.ThrowIfCancellationRequested();
ILAstOptimizer bodyGraph = new ILAstOptimizer();
Expand Down Expand Up @@ -258,6 +258,9 @@ AstNode TransformExpression(ILExpression expr)
else
result = node;

if (result != null)
result = result.WithAnnotation(new TypeInformation(expr.InferredType));

if (result != null)
return result.WithAnnotation(ilRanges);

Expand Down Expand Up @@ -397,7 +400,7 @@ AstNode TransformByteCode(ILExpression byteCode)
ace.Initializer.Elements.AddRange(args);
return ace;
}
case ILCode.Ldlen: return arg1.Member("Length");
case ILCode.Ldlen: return arg1.Member("Length");
case ILCode.Ldelem_I:
case ILCode.Ldelem_I1:
case ILCode.Ldelem_I2:
Expand Down Expand Up @@ -918,14 +921,21 @@ AstNode TransformCall(bool isVirtual, ILExpression byteCode, List<Ast.Expression
target = ((DirectionExpression)target).Expression;
target.Remove(); // detach from DirectionExpression
}
if (cecilMethodDef != null && cecilMethodDef.DeclaringType.IsInterface) {
TypeReference tr = byteCode.Arguments[0].InferredType;
if (tr != null) {
TypeDefinition td = tr.Resolve();
if (td != null && !td.IsInterface) {
// Calling an interface method on a non-interface object:
// we need to introduce an explicit cast
target = target.CastTo(AstBuilder.ConvertType(cecilMethod.DeclaringType));

if (cecilMethodDef != null) {
// convert null.ToLower() to ((string)null).ToLower()
if (target is NullReferenceExpression)
target = target.CastTo(AstBuilder.ConvertType(cecilMethod.DeclaringType));

if (cecilMethodDef.DeclaringType.IsInterface) {
TypeReference tr = byteCode.Arguments[0].InferredType;
if (tr != null) {
TypeDefinition td = tr.Resolve();
if (td != null && !td.IsInterface) {
// Calling an interface method on a non-interface object:
// we need to introduce an explicit cast
target = target.CastTo(AstBuilder.ConvertType(cecilMethod.DeclaringType));
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions ICSharpCode.Decompiler/Ast/TextOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@ public void WriteComment(CommentType commentType, string content)
output.Write("*/");
break;
case CommentType.Documentation:
if (!inDocumentationComment)
bool isLastLine = !(nodeStack.Peek().NextSibling is Comment);
if (!inDocumentationComment && !isLastLine) {
inDocumentationComment = true;
output.MarkFoldStart("///" + content, true);
}
output.Write("///");
output.Write(content);
inDocumentationComment = true;
bool isLastLine = !(nodeStack.Peek().NextSibling is Comment);
if (isLastLine) {
if (inDocumentationComment && isLastLine) {
inDocumentationComment = false;
output.MarkFoldEnd();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public void Run(AstNode compilationUnit)
if (d != null) {
foreach (var ca in d.CustomAttributes) {
if (ca.AttributeType.Name == "ExtensionAttribute" && ca.AttributeType.Namespace == "System.Runtime.CompilerServices") {
mre.Target = invocation.Arguments.First().Detach();
var firstArgument = invocation.Arguments.First();
if (firstArgument is NullReferenceExpression)
firstArgument = firstArgument.ReplaceWith(expr => expr.CastTo(AstBuilder.ConvertType(d.Parameters.First().ParameterType)));
else
mre.Target = firstArgument.Detach();
if (invocation.Arguments.Any()) {
// HACK: removing type arguments should be done indepently from whether a method is an extension method,
// just by testing whether the arguments can be inferred
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public class ReplaceMethodCallsWithOperators : DepthFirstAstVisitor<object, obje
MemberName = "TypeHandle"
};

DecompilerContext context;

public ReplaceMethodCallsWithOperators(DecompilerContext context)
{
this.context = context;
}

public override object VisitInvocationExpression(InvocationExpression invocationExpression, object data)
{
base.VisitInvocationExpression(invocationExpression, data);
Expand Down Expand Up @@ -214,7 +221,7 @@ public override object VisitAssignmentExpression(AssignmentExpression assignment
}
}
}
if (assignment.Operator == AssignmentOperatorType.Add || assignment.Operator == AssignmentOperatorType.Subtract) {
if (context.Settings.IntroduceIncrementAndDecrement && (assignment.Operator == AssignmentOperatorType.Add || assignment.Operator == AssignmentOperatorType.Subtract)) {
// detect increment/decrement
if (assignment.Right.IsMatch(new PrimitiveExpression(1))) {
// only if it's not a custom operator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static IAstTransform[] CreatePipeline(DecompilerContext context)
new PushNegation(),
new DelegateConstruction(context),
new PatternStatementTransform(context),
new ReplaceMethodCallsWithOperators(),
new ReplaceMethodCallsWithOperators(context),
new IntroduceUnsafeModifier(),
new AddCheckedBlocks(),
new DeclareVariables(context), // should run after most transforms that modify statements
Expand Down
8 changes: 5 additions & 3 deletions ICSharpCode.Decompiler/CecilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static int GetPushDelta(this Instruction instruction)
throw new NotSupportedException ();
}

public static int? GetPopDelta(this Instruction instruction)
public static int? GetPopDelta(this Instruction instruction, MethodDefinition methodDef)
{
OpCode code = instruction.OpCode;
switch (code.StackBehaviourPop) {
Expand Down Expand Up @@ -93,15 +93,17 @@ public static int GetPushDelta(this Instruction instruction)

case StackBehaviour.Varpop:
if (code == OpCodes.Ret)
return null;
return methodDef.ReturnType.IsVoid() ? 0 : 1;

if (code.FlowControl != FlowControl.Call)
break;

IMethodSignature method = (IMethodSignature) instruction.Operand;
int count = method.HasParameters ? method.Parameters.Count : 0;
if (code == OpCodes.Calli || (method.HasThis && code != OpCodes.Newobj))
if (method.HasThis && code != OpCodes.Newobj)
++count;
if (code == OpCodes.Calli)
++count; // calli takes a function pointer in additional to the normal args

return count;
}
Expand Down
27 changes: 27 additions & 0 deletions ICSharpCode.Decompiler/CodeMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using ICSharpCode.Decompiler.Ast;
using ICSharpCode.Decompiler.Disassembler;
using ICSharpCode.Decompiler.ILAst;
using ICSharpCode.NRefactory.CSharp;
using Mono.Cecil;

namespace ICSharpCode.Decompiler
Expand Down Expand Up @@ -316,4 +317,30 @@ public static bool GetInstructionByTokenAndOffset(
return true;
}
}

/// <summary>
/// Decompilation data. Can be used by other applications to store the decompilation data.
/// </summary>
public class DecompileInformation
{
/// <summary>
/// Gets ot sets the code mappings
/// </summary>
public Dictionary<int, List<MemberMapping>> CodeMappings { get; set; }

/// <summary>
/// Gets or sets the local variables.
/// </summary>
public ConcurrentDictionary<int, IEnumerable<ILVariable>> LocalVariables { get; set; }

/// <summary>
/// Gets the list of MembeReferences that are decompiled (TypeDefinitions, MethodDefinitions, etc)
/// </summary>
public Dictionary<int, MemberReference> DecompiledMemberReferences { get; set; }

/// <summary>
/// Gets (or internal sets) the AST nodes.
/// </summary>
public IEnumerable<AstNode> AstNodes { get; set; }
}
}
32 changes: 32 additions & 0 deletions ICSharpCode.Decompiler/DecompilerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,38 @@ public bool ShowXmlDocumentation {
}
}

#region Options to aid VB decompilation
bool introduceIncrementAndDecrement = true;

/// <summary>
/// Gets/Sets whether to use increment and decrement operators
/// </summary>
public bool IntroduceIncrementAndDecrement {
get { return introduceIncrementAndDecrement; }
set {
if (introduceIncrementAndDecrement != value) {
introduceIncrementAndDecrement = value;
OnPropertyChanged("IntroduceIncrementAndDecrement");
}
}
}

bool alwaysGenerateExceptionVariableForCatchBlocks = false;

/// <summary>
/// Gets/Sets whether to always generate exception variables in catch blocks
/// </summary>
public bool AlwaysGenerateExceptionVariableForCatchBlocks {
get { return alwaysGenerateExceptionVariableForCatchBlocks; }
set {
if (alwaysGenerateExceptionVariableForCatchBlocks != value) {
alwaysGenerateExceptionVariableForCatchBlocks = value;
OnPropertyChanged("AlwaysGenerateExceptionVariableForCatchBlocks");
}
}
}
#endregion

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
Expand Down
2 changes: 1 addition & 1 deletion ICSharpCode.Decompiler/FlowAnalysis/SsaFormBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void CreateInstructions(int blockIndex)
continue;
}

int popCount = inst.GetPopDelta() ?? stackSize;
int popCount = inst.GetPopDelta(method) ?? stackSize;
stackSize -= popCount;
if (stackSize < 0)
throw new InvalidProgramException("IL stack underflow");
Expand Down
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Ast\Annotations.cs" />
<Compile Include="Ast\AstBuilder.cs" />
<Compile Include="Ast\AstMethodBodyBuilder.cs" />
<Compile Include="Ast\CecilTypeResolveContext.cs" />
Expand Down
Loading

0 comments on commit d2488a4

Please sign in to comment.