Browse Source

Simplify the transform code by using extension methods.

pull/1/head^2
David Srbecký 18 years ago
parent
commit
9aff0b724d
  1. 12
      Decompiler.csproj
  2. 5
      lib/NRefactory/Project/NRefactory.csproj
  3. 38
      lib/NRefactory/Project/Src/Ast/INode.cs
  4. 38
      src/Transforms/Ast/RemoveGotos.cs
  5. 21
      src/Transforms/Ast/RestoreLoop.cs

12
Decompiler.csproj

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectGuid>{EE3A3C1A-F9C3-4C75-853D-A9476E518C3A}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>Decompiler</RootNamespace>
<AssemblyName>Decompiler</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug\</OutputPath>
@ -26,10 +27,19 @@ @@ -26,10 +27,19 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="src" />

5
lib/NRefactory/Project/NRefactory.csproj

@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
<FileAlignment>4096</FileAlignment>
<RunCodeAnalysis>False</RunCodeAnalysis>
<CodeAnalysisRules>-Microsoft.Design#CA1002;-Microsoft.Design#CA1020;-Microsoft.Design#CA1051;-Microsoft.Design#CA1062;-Microsoft.Globalization#CA1303;-Microsoft.Globalization#CA1305;-Microsoft.Naming#CA1704;-Microsoft.Performance#CA1800;-Microsoft.Performance#CA1805;-Microsoft.Usage#CA2211;-Microsoft.Usage#CA2227</CodeAnalysisRules>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Optimize>False</Optimize>
@ -49,6 +49,9 @@ @@ -49,6 +49,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="Resources\ICSharpCode.NRefactory.snk" />

38
lib/NRefactory/Project/Src/Ast/INode.cs

@ -52,4 +52,42 @@ namespace ICSharpCode.NRefactory.Ast @@ -52,4 +52,42 @@ namespace ICSharpCode.NRefactory.Ast
/// <returns>The value the visitor returns after the visit</returns>
object AcceptVisitor(IAstVisitor visitor, object data);
}
public static class INodeExtensionMethods
{
public static void Remove(this INode node)
{
node.Parent.Children.Remove(node);
}
public static INode Previous(this INode node)
{
if (node.Parent == null) return null;
int myIndex = node.Parent.Children.IndexOf(node);
int index = myIndex - 1;
if (0 <= index && index < node.Parent.Children.Count) {
return node.Parent.Children[index];
} else {
return null;
}
}
public static INode Next(this INode node)
{
if (node.Parent == null) return null;
int myIndex = node.Parent.Children.IndexOf(node);
int index = myIndex + 1;
if (0 <= index && index < node.Parent.Children.Count) {
return node.Parent.Children[index];
} else {
return null;
}
}
public static void ReplaceWith(this INode node, INode newNode)
{
int myIndex = node.Parent.Children.IndexOf(node);
node.Parent.Children[myIndex] = newNode;
}
}
}

38
src/Transforms/Ast/RemoveGotos.cs

@ -12,19 +12,19 @@ namespace Decompiler.Transforms.Ast @@ -12,19 +12,19 @@ namespace Decompiler.Transforms.Ast
base.VisitBlockStatement(blockStatement, data);
// Remove redundant jump at the end of block
INode lastStmt = blockStatement.Children[blockStatement.Children.Count - 1];
INode lastStmt = blockStatement.Children.Last;
// End of while loop
if (lastStmt is ContinueStatement &&
blockStatement.Parent is DoLoopStatement)
{
blockStatement.Children.Remove(lastStmt);
lastStmt.Remove();
return null;
}
// End of for loop
if (lastStmt is ContinueStatement &&
blockStatement.Parent is ForStatement)
{
blockStatement.Children.Remove(lastStmt);
lastStmt.Remove();
return null;
}
// End of method
@ -32,26 +32,21 @@ namespace Decompiler.Transforms.Ast @@ -32,26 +32,21 @@ namespace Decompiler.Transforms.Ast
blockStatement.Parent is MethodDeclaration &&
((ReturnStatement)lastStmt).Expression.IsNull)
{
blockStatement.Children.Remove(lastStmt);
lastStmt.Remove();
return null;
}
// End of if body
if (lastStmt is GotoStatement &&
blockStatement.Parent is IfElseStatement)
{
INode ifParent = blockStatement.Parent.Parent;
int ifIndex = ifParent.Children.IndexOf(blockStatement.Parent);
if (ifIndex + 1 < ifParent.Children.Count) {
MyLabelStatement nextNodeAsLabel = ifParent.Children[ifIndex + 1] as MyLabelStatement;
if (nextNodeAsLabel != null) {
if (nextNodeAsLabel.NodeLabel == ((MyGotoStatement)lastStmt).NodeLabel) {
((MyGotoStatement)lastStmt).NodeLabel.ReferenceCount--;
blockStatement.Children.Remove(lastStmt);
}
MyLabelStatement nextNodeAsLabel = blockStatement.Parent.Next() as MyLabelStatement;
if (nextNodeAsLabel != null) {
if (nextNodeAsLabel.NodeLabel == ((MyGotoStatement)lastStmt).NodeLabel) {
((MyGotoStatement)lastStmt).NodeLabel.ReferenceCount--;
lastStmt.Remove();
return null;
}
}
return null;
}
return null;
@ -60,15 +55,10 @@ namespace Decompiler.Transforms.Ast @@ -60,15 +55,10 @@ namespace Decompiler.Transforms.Ast
public override object VisitGotoStatement(GotoStatement gotoStatement, object data)
{
MyGotoStatement myGoto = (MyGotoStatement)gotoStatement;
if (gotoStatement.Parent == null) return null;
int index = gotoStatement.Parent.Children.IndexOf(gotoStatement);
if (index + 1 < gotoStatement.Parent.Children.Count) {
INode nextStmt = gotoStatement.Parent.Children[index + 1];
MyLabelStatement myLabel = nextStmt as MyLabelStatement;
if (myLabel != null && myLabel.NodeLabel == myGoto.NodeLabel) {
myGoto.NodeLabel.ReferenceCount--;
RemoveCurrentNode();
}
MyLabelStatement followingLabel = myGoto.Next() as MyLabelStatement;
if (followingLabel != null && followingLabel.NodeLabel == myGoto.NodeLabel) {
myGoto.NodeLabel.ReferenceCount--;
RemoveCurrentNode();
}
return null;
}

21
src/Transforms/Ast/RestoreLoop.cs

@ -14,13 +14,10 @@ namespace Decompiler.Transforms.Ast @@ -14,13 +14,10 @@ namespace Decompiler.Transforms.Ast
// Restore loop initializer
if (forStatement.Initializers.Count == 0) {
int myIndex = forStatement.Parent.Children.IndexOf(forStatement);
if (myIndex - 1 >= 0) {
LocalVariableDeclaration varDeclr = forStatement.Parent.Children[myIndex - 1] as LocalVariableDeclaration;
if (varDeclr != null) {
forStatement.Parent.Children[myIndex - 1] = Statement.Null;
forStatement.Initializers.Add(varDeclr);
}
LocalVariableDeclaration varDeclr = forStatement.Previous() as LocalVariableDeclaration;
if (varDeclr != null) {
varDeclr.ReplaceWith(Statement.Null);
forStatement.Initializers.Add(varDeclr);
}
}
@ -31,15 +28,13 @@ namespace Decompiler.Transforms.Ast @@ -31,15 +28,13 @@ namespace Decompiler.Transforms.Ast
IfElseStatement condition = forStatement.EmbeddedStatement.Children[0] as IfElseStatement;
BreakStatement breakStmt = forStatement.EmbeddedStatement.Children[1] as BreakStatement;
MyLabelStatement label = forStatement.EmbeddedStatement.Children[2] as MyLabelStatement;
if (condition != null &&
breakStmt != null &&
label != null &&
if (condition != null && breakStmt != null && label != null &&
condition.TrueStatement.Count == 1)
{
MyGotoStatement gotoStmt = condition.TrueStatement[0] as MyGotoStatement;
if (gotoStmt != null && gotoStmt.NodeLabel == label.NodeLabel) {
forStatement.EmbeddedStatement.Children.Remove(condition);
forStatement.EmbeddedStatement.Children.Remove(breakStmt);
condition.Remove();
breakStmt.Remove();
gotoStmt.NodeLabel.ReferenceCount--;
forStatement.Condition = condition.Condition;
}
@ -54,7 +49,7 @@ namespace Decompiler.Transforms.Ast @@ -54,7 +49,7 @@ namespace Decompiler.Transforms.Ast
if (lastStmt != null) {
AssignmentExpression assign = lastStmt.Expression as AssignmentExpression;
if (assign != null) {
forStatement.EmbeddedStatement.Children.Remove(lastStmt);
lastStmt.Remove();
forStatement.Iterator.Add(lastStmt);
}
}

Loading…
Cancel
Save