Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/rocks
diff options
context:
space:
mode:
authorjbevain <jbevain@gmail.com>2010-04-12 11:19:07 +0400
committerjbevain <jbevain@gmail.com>2010-04-12 11:19:07 +0400
commit714ac9e6de3606a1f71966ff1684d964d8ae1334 (patch)
tree1a4d285f4603c8733b1f439acdf66fbc4d40eea5 /rocks
initial commit
Diffstat (limited to 'rocks')
-rw-r--r--rocks/.gitignore7
-rw-r--r--rocks/Mono.Cecil.Rocks.csproj66
-rw-r--r--rocks/Mono.Cecil.Rocks/Functional.cs60
-rw-r--r--rocks/Mono.Cecil.Rocks/ILParser.cs229
-rw-r--r--rocks/Mono.Cecil.Rocks/MethodBodyRocks.cs413
-rw-r--r--rocks/Mono.Cecil.Rocks/ModuleDefinitionRocks.cs47
-rw-r--r--rocks/Mono.Cecil.Rocks/ParameterReferenceRocks.cs11
-rw-r--r--rocks/Mono.Cecil.Rocks/SecurityDeclarationRocks.cs159
-rw-r--r--rocks/Mono.Cecil.Rocks/TypeDefinitionRocks.cs70
-rw-r--r--rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs104
-rw-r--r--rocks/Test/.gitignore7
-rw-r--r--rocks/Test/Mono.Cecil.Rocks.Tests.csproj77
-rw-r--r--rocks/Test/Mono.Cecil.Tests/ModuleDefinitionRocksTests.cs27
-rw-r--r--rocks/Test/Mono.Cecil.Tests/SecurityDeclarationRocksTests.cs61
-rw-r--r--rocks/Test/Mono.Cecil.Tests/TypeDefinitionRocksTests.cs68
-rw-r--r--rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs124
-rw-r--r--rocks/Test/Resources/assemblies/decsec-att.dllbin0 -> 4096 bytes
-rw-r--r--rocks/Test/Resources/assemblies/decsec-xml.dllbin0 -> 5120 bytes
-rwxr-xr-xrocks/Test/Resources/cs/Types.cs14
19 files changed, 1544 insertions, 0 deletions
diff --git a/rocks/.gitignore b/rocks/.gitignore
new file mode 100644
index 0000000..3629e37
--- /dev/null
+++ b/rocks/.gitignore
@@ -0,0 +1,7 @@
+bin
+obj
+*.suo
+*.user
+*.pidb
+*.userprefs
+*.xml
diff --git a/rocks/Mono.Cecil.Rocks.csproj b/rocks/Mono.Cecil.Rocks.csproj
new file mode 100644
index 0000000..1cc3507
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks.csproj
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.30729</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{FBC6DD59-D09D-499C-B03C-99C1C78FF2AC}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Mono.Cecil.Rocks</RootNamespace>
+ <AssemblyName>Mono.Cecil.Rocks</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <SignAssembly>true</SignAssembly>
+ <AssemblyOriginatorKeyFile>..\mono.snk</AssemblyOriginatorKeyFile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System.Core">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ <Aliases>global</Aliases>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Mono.Cecil.csproj">
+ <Project>{D68133BD-1E63-496E-9EDE-4FBDBF77B486}</Project>
+ <Name>Mono.Cecil</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Mono.Cecil.Rocks\SecurityDeclarationRocks.cs" />
+ <Compile Include="Mono.Cecil.Rocks\MethodBodyRocks.cs" />
+ <Compile Include="Mono.Cecil.Rocks\ILParser.cs" />
+ <Compile Include="Mono.Cecil.Rocks\TypeReferenceRocks.cs" />
+ <Compile Include="Mono.Cecil.Rocks\Functional.cs" />
+ <Compile Include="Mono.Cecil.Rocks\ModuleDefinitionRocks.cs" />
+ <Compile Include="Mono.Cecil.Rocks\TypeDefinitionRocks.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ No newline at end of file
diff --git a/rocks/Mono.Cecil.Rocks/Functional.cs b/rocks/Mono.Cecil.Rocks/Functional.cs
new file mode 100644
index 0000000..773306e
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/Functional.cs
@@ -0,0 +1,60 @@
+//
+// Functional.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// 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.Generic;
+
+namespace Mono.Cecil.Rocks {
+
+ static class Functional {
+
+ public static System.Func<A, R> Y<A, R> (System.Func<System.Func<A, R>, System.Func<A, R>> f)
+ {
+ System.Func<A, R> g = null;
+ g = f (a => g (a));
+ return g;
+ }
+
+ public static IEnumerable<TSource> Prepend<TSource> (this IEnumerable<TSource> source, TSource element)
+ {
+ if (source == null)
+ throw new ArgumentNullException ("source");
+
+ return PrependIterator (source, element);
+ }
+
+ static IEnumerable<TSource> PrependIterator<TSource> (IEnumerable<TSource> source, TSource element)
+ {
+ yield return element;
+
+ foreach (var item in source)
+ yield return item;
+
+ }
+ }
+}
diff --git a/rocks/Mono.Cecil.Rocks/ILParser.cs b/rocks/Mono.Cecil.Rocks/ILParser.cs
new file mode 100644
index 0000000..ae2fb3b
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/ILParser.cs
@@ -0,0 +1,229 @@
+//
+// ILParser.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// 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 Mono.Cecil.Cil;
+using Mono.Collections.Generic;
+
+namespace Mono.Cecil.Rocks {
+
+ public interface IILVisitor {
+ void OnInlineNone (OpCode opcode);
+ void OnInlineSByte (OpCode opcode, sbyte value);
+ void OnInlineByte (OpCode opcode, byte value);
+ void OnInlineInt32 (OpCode opcode, int value);
+ void OnInlineInt64 (OpCode opcode, long value);
+ void OnInlineSingle (OpCode opcode, float value);
+ void OnInlineDouble (OpCode opcode, double value);
+ void OnInlineString (OpCode opcode, string value);
+ void OnInlineBranch (OpCode opcode, int offset);
+ void OnInlineSwitch (OpCode opcode, int [] offsets);
+ void OnInlineVariable (OpCode opcode, VariableDefinition variable);
+ void OnInlineArgument (OpCode opcode, ParameterDefinition parameter);
+ void OnInlineSignature (OpCode opcode, CallSite callSite);
+ void OnInlineType (OpCode opcode, TypeReference type);
+ void OnInlineField (OpCode opcode, FieldReference field);
+ void OnInlineMethod (OpCode opcode, MethodReference method);
+ }
+
+ public class ILParser {
+
+ class ParseContext {
+ public MethodDefinition Method { get; set; }
+ public CodeReader Code { get; set; }
+ public MetadataReader Metadata { get; set; }
+ public Collection<VariableDefinition> Variables { get; set; }
+ public IILVisitor Visitor { get; set; }
+ }
+
+ public static void Parse (MethodDefinition method, IILVisitor visitor)
+ {
+ if (method == null)
+ throw new ArgumentNullException ("method");
+ if (visitor == null)
+ throw new ArgumentNullException ("visitor");
+ if (!method.HasBody || !method.HasImage)
+ throw new ArgumentException ();
+
+ var context = CreateContext (method, visitor);
+ var code = context.Code;
+
+ code.MoveTo (method.RVA);
+
+ var flags = code.ReadByte ();
+
+ switch (flags & 0x3) {
+ case 0x2: // tiny
+ int code_size = flags >> 2;
+ ParseCode (code_size, context);
+ break;
+ case 0x3: // fat
+ code.position--;
+ ParseFatMethod (context);
+ break;
+ default:
+ throw new NotSupportedException ();
+ }
+ }
+
+ static ParseContext CreateContext (MethodDefinition method, IILVisitor visitor)
+ {
+ var code = method.Module.Read (method, (_, reader) => CodeReader.CreateCodeReader (reader));
+
+ return new ParseContext {
+ Method = method,
+ Code = code,
+ Metadata = code.reader,
+ Visitor = visitor,
+ };
+ }
+
+ static void ParseFatMethod (ParseContext context)
+ {
+ var code = context.Code;
+
+ code.Advance (4);
+ var code_size = code.ReadInt32 ();
+ var local_var_token = code.ReadToken ();
+
+ if (local_var_token != MetadataToken.Zero)
+ context.Variables = code.ReadVariables (local_var_token);
+
+ ParseCode (code_size, context);
+ }
+
+ static void ParseCode (int code_size, ParseContext context)
+ {
+ var code = context.Code;
+ var metadata = context.Metadata;
+ var method = context.Method;
+ var visitor = context.Visitor;
+
+ var start = code.position;
+ var end = start + code_size;
+
+ while (code.position < end) {
+ var il_opcode = code.ReadByte ();
+ var opcode = il_opcode != 0xfe
+ ? OpCodes.OneByteOpCode [il_opcode]
+ : OpCodes.TwoBytesOpCode [code.ReadByte ()];
+
+ switch (opcode.OperandType) {
+ case OperandType.InlineNone:
+ visitor.OnInlineNone (opcode);
+ break;
+ case OperandType.InlineSwitch:
+ var length = code.ReadInt32 ();
+ var branches = new int [length];
+ for (int i = 0; i < length; i++)
+ branches [i] = code.ReadInt32 ();
+ visitor.OnInlineSwitch (opcode, branches);
+ break;
+ case OperandType.ShortInlineBrTarget:
+ visitor.OnInlineBranch (opcode, code.ReadSByte ());
+ break;
+ case OperandType.InlineBrTarget:
+ visitor.OnInlineBranch (opcode, code.ReadInt32 ());
+ break;
+ case OperandType.ShortInlineI:
+ if (opcode == OpCodes.Ldc_I4_S)
+ visitor.OnInlineSByte (opcode, code.ReadSByte ());
+ else
+ visitor.OnInlineByte (opcode, code.ReadByte ());
+ break;
+ case OperandType.InlineI:
+ visitor.OnInlineInt32 (opcode, code.ReadInt32 ());
+ break;
+ case OperandType.InlineI8:
+ visitor.OnInlineInt64 (opcode, code.ReadInt64 ());
+ break;
+ case OperandType.ShortInlineR:
+ visitor.OnInlineSingle (opcode, code.ReadSingle ());
+ break;
+ case OperandType.InlineR:
+ visitor.OnInlineDouble (opcode, code.ReadDouble ());
+ break;
+ case OperandType.InlineSig:
+ visitor.OnInlineSignature (opcode, code.GetCallSite (code.ReadToken ()));
+ break;
+ case OperandType.InlineString:
+ visitor.OnInlineString (opcode, code.GetString (code.ReadToken ()));
+ break;
+ case OperandType.ShortInlineArg:
+ visitor.OnInlineArgument (opcode, code.GetParameter (code.ReadByte ()));
+ break;
+ case OperandType.InlineArg:
+ visitor.OnInlineArgument (opcode, code.GetParameter (code.ReadInt16 ()));
+ break;
+ case OperandType.ShortInlineVar:
+ visitor.OnInlineVariable (opcode, GetVariable (context, code.ReadByte ()));
+ break;
+ case OperandType.InlineVar:
+ visitor.OnInlineVariable (opcode, GetVariable (context, code.ReadInt16 ()));
+ break;
+ case OperandType.InlineTok:
+ case OperandType.InlineField:
+ case OperandType.InlineMethod:
+ case OperandType.InlineType:
+ var member = metadata.LookupToken (code.ReadToken ());
+ switch (member.MetadataToken.TokenType) {
+ case TokenType.TypeDef:
+ case TokenType.TypeRef:
+ case TokenType.TypeSpec:
+ visitor.OnInlineType (opcode, (TypeReference) member);
+ break;
+ case TokenType.Method:
+ case TokenType.MethodSpec:
+ visitor.OnInlineMethod (opcode, (MethodReference) member);
+ break;
+ case TokenType.Field:
+ visitor.OnInlineField (opcode, (FieldReference) member);
+ break;
+ case TokenType.MemberRef:
+ var field_ref = member as FieldReference;
+ if (field_ref != null)
+ visitor.OnInlineField (opcode, field_ref);
+
+ var method_ref = member as MethodReference;
+ if (method_ref != null)
+ visitor.OnInlineMethod (opcode, method_ref);
+
+ throw new InvalidOperationException ();
+ }
+ break;
+ }
+ }
+ }
+
+ static VariableDefinition GetVariable (ParseContext context, int index)
+ {
+ return context.Variables [index];
+ }
+ }
+}
diff --git a/rocks/Mono.Cecil.Rocks/MethodBodyRocks.cs b/rocks/Mono.Cecil.Rocks/MethodBodyRocks.cs
new file mode 100644
index 0000000..4e46ae8
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/MethodBodyRocks.cs
@@ -0,0 +1,413 @@
+//
+// MethodBodyRocks.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// 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 Mono.Cecil.Cil;
+
+namespace Mono.Cecil.Rocks {
+
+ public static class MethodBodyRocks {
+
+ public static void SimplifyMacros (this MethodBody self)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+
+ foreach (var instruction in self.Instructions) {
+ if (instruction.OpCode.OpCodeType != OpCodeType.Macro)
+ continue;
+
+ switch (instruction.OpCode.Code) {
+ case Code.Ldarg_0:
+ ExpandMacro (instruction, OpCodes.Ldarg, GetParameter (self, 0));
+ break;
+ case Code.Ldarg_1:
+ ExpandMacro (instruction, OpCodes.Ldarg, GetParameter (self, 1));
+ break;
+ case Code.Ldarg_2:
+ ExpandMacro (instruction, OpCodes.Ldarg, GetParameter (self, 2));
+ break;
+ case Code.Ldarg_3:
+ ExpandMacro (instruction, OpCodes.Ldarg, GetParameter (self, 3));
+ break;
+ case Code.Ldloc_0:
+ ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [0]);
+ break;
+ case Code.Ldloc_1:
+ ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [1]);
+ break;
+ case Code.Ldloc_2:
+ ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [2]);
+ break;
+ case Code.Ldloc_3:
+ ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [3]);
+ break;
+ case Code.Stloc_0:
+ ExpandMacro (instruction, OpCodes.Stloc, self.Variables [0]);
+ break;
+ case Code.Stloc_1:
+ ExpandMacro (instruction, OpCodes.Stloc, self.Variables [1]);
+ break;
+ case Code.Stloc_2:
+ ExpandMacro (instruction, OpCodes.Stloc, self.Variables [2]);
+ break;
+ case Code.Stloc_3:
+ ExpandMacro (instruction, OpCodes.Stloc, self.Variables [3]);
+ break;
+ case Code.Ldarg_S:
+ instruction.OpCode = OpCodes.Ldarg;
+ break;
+ case Code.Ldarga_S:
+ instruction.OpCode = OpCodes.Ldarga;
+ break;
+ case Code.Starg_S:
+ instruction.OpCode = OpCodes.Starg;
+ break;
+ case Code.Ldloc_S:
+ instruction.OpCode = OpCodes.Ldloc;
+ break;
+ case Code.Ldloca_S:
+ instruction.OpCode = OpCodes.Ldloca;
+ break;
+ case Code.Stloc_S:
+ instruction.OpCode = OpCodes.Stloc;
+ break;
+ case Code.Ldc_I4_M1:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, -1);
+ break;
+ case Code.Ldc_I4_0:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 0);
+ break;
+ case Code.Ldc_I4_1:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 1);
+ break;
+ case Code.Ldc_I4_2:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 2);
+ break;
+ case Code.Ldc_I4_3:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 3);
+ break;
+ case Code.Ldc_I4_4:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 4);
+ break;
+ case Code.Ldc_I4_5:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 5);
+ break;
+ case Code.Ldc_I4_6:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 6);
+ break;
+ case Code.Ldc_I4_7:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 7);
+ break;
+ case Code.Ldc_I4_8:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, 8);
+ break;
+ case Code.Ldc_I4_S:
+ ExpandMacro (instruction, OpCodes.Ldc_I4, (int) (sbyte) instruction.Operand);
+ break;
+ case Code.Br_S:
+ instruction.OpCode = OpCodes.Br;
+ break;
+ case Code.Brfalse_S:
+ instruction.OpCode = OpCodes.Brfalse;
+ break;
+ case Code.Brtrue_S:
+ instruction.OpCode = OpCodes.Brtrue;
+ break;
+ case Code.Beq_S:
+ instruction.OpCode = OpCodes.Beq;
+ break;
+ case Code.Bge_S:
+ instruction.OpCode = OpCodes.Bge;
+ break;
+ case Code.Bgt_S:
+ instruction.OpCode = OpCodes.Bgt;
+ break;
+ case Code.Ble_S:
+ instruction.OpCode = OpCodes.Ble;
+ break;
+ case Code.Blt_S:
+ instruction.OpCode = OpCodes.Blt;
+ break;
+ case Code.Bne_Un_S:
+ instruction.OpCode = OpCodes.Bne_Un;
+ break;
+ case Code.Bge_Un_S:
+ instruction.OpCode = OpCodes.Bge_Un;
+ break;
+ case Code.Bgt_Un_S:
+ instruction.OpCode = OpCodes.Bgt_Un;
+ break;
+ case Code.Ble_Un_S:
+ instruction.OpCode = OpCodes.Ble_Un;
+ break;
+ case Code.Blt_Un_S:
+ instruction.OpCode = OpCodes.Blt_Un;
+ break;
+ case Code.Leave_S:
+ instruction.OpCode = OpCodes.Leave;
+ break;
+ }
+ }
+ }
+
+ static ParameterDefinition GetParameter (MethodBody body, int index)
+ {
+ var method = body.Method;
+ if (method.HasThis)
+ index--;
+
+ return method.Parameters [index];
+ }
+
+ static void ExpandMacro (Instruction instruction, OpCode opcode, object operand)
+ {
+ instruction.OpCode = opcode;
+ instruction.Operand = operand;
+ }
+
+ static void MakeMacro (Instruction instruction, OpCode opcode)
+ {
+ instruction.OpCode = opcode;
+ instruction.Operand = null;
+ }
+
+ public static void OptimizeMacros (this MethodBody self)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+
+ var method = self.Method;
+
+ foreach (var instruction in self.Instructions) {
+ int index;
+ switch (instruction.OpCode.Code) {
+ case Code.Ldarg:
+ index = ((ParameterDefinition) instruction.Operand).Index;
+ if (index == -1 && instruction.Operand == self.ThisParameter)
+ index = 0;
+ else if (method.HasThis)
+ index++;
+
+ switch (index) {
+ case 0:
+ MakeMacro (instruction, OpCodes.Ldarg_0);
+ break;
+ case 1:
+ MakeMacro (instruction, OpCodes.Ldarg_1);
+ break;
+ case 2:
+ MakeMacro (instruction, OpCodes.Ldarg_2);
+ break;
+ case 3:
+ MakeMacro (instruction, OpCodes.Ldarg_3);
+ break;
+ default:
+ if (index < 256)
+ ExpandMacro (instruction, OpCodes.Ldarg_S, instruction.Operand);
+ break;
+ }
+ break;
+ case Code.Ldloc:
+ index = ((VariableDefinition) instruction.Operand).Index;
+ switch (index) {
+ case 0:
+ MakeMacro (instruction, OpCodes.Ldloc_0);
+ break;
+ case 1:
+ MakeMacro (instruction, OpCodes.Ldloc_1);
+ break;
+ case 2:
+ MakeMacro (instruction, OpCodes.Ldloc_2);
+ break;
+ case 3:
+ MakeMacro (instruction, OpCodes.Ldloc_3);
+ break;
+ default:
+ if (index < 256)
+ ExpandMacro (instruction, OpCodes.Ldloc_S, instruction.Operand);
+ break;
+ }
+ break;
+ case Code.Stloc:
+ index = ((VariableDefinition) instruction.Operand).Index;
+ switch (index) {
+ case 0:
+ MakeMacro (instruction, OpCodes.Stloc_0);
+ break;
+ case 1:
+ MakeMacro (instruction, OpCodes.Stloc_1);
+ break;
+ case 2:
+ MakeMacro (instruction, OpCodes.Stloc_2);
+ break;
+ case 3:
+ MakeMacro (instruction, OpCodes.Stloc_3);
+ break;
+ default:
+ if (index < 256)
+ ExpandMacro (instruction, OpCodes.Stloc_S, instruction.Operand);
+ break;
+ }
+ break;
+ case Code.Ldarga:
+ index = ((ParameterDefinition) instruction.Operand).Index;
+ if (index == -1 && instruction.Operand == self.ThisParameter)
+ index = 0;
+ else if (method.HasThis)
+ index++;
+ if (index < 256)
+ ExpandMacro (instruction, OpCodes.Ldarga_S, instruction.Operand);
+ break;
+ case Code.Ldloca:
+ if (((VariableDefinition) instruction.Operand).Index < 256)
+ ExpandMacro (instruction, OpCodes.Ldloca_S, instruction.Operand);
+ break;
+ case Code.Ldc_I4:
+ int i = (int) instruction.Operand;
+ switch (i) {
+ case -1:
+ MakeMacro (instruction, OpCodes.Ldc_I4_M1);
+ break;
+ case 0:
+ MakeMacro (instruction, OpCodes.Ldc_I4_0);
+ break;
+ case 1:
+ MakeMacro (instruction, OpCodes.Ldc_I4_1);
+ break;
+ case 2:
+ MakeMacro (instruction, OpCodes.Ldc_I4_2);
+ break;
+ case 3:
+ MakeMacro (instruction, OpCodes.Ldc_I4_3);
+ break;
+ case 4:
+ MakeMacro (instruction, OpCodes.Ldc_I4_4);
+ break;
+ case 5:
+ MakeMacro (instruction, OpCodes.Ldc_I4_5);
+ break;
+ case 6:
+ MakeMacro (instruction, OpCodes.Ldc_I4_6);
+ break;
+ case 7:
+ MakeMacro (instruction, OpCodes.Ldc_I4_7);
+ break;
+ case 8:
+ MakeMacro (instruction, OpCodes.Ldc_I4_8);
+ break;
+ default:
+ if (i >= -128 && i < 128)
+ ExpandMacro (instruction, OpCodes.Ldc_I4_S, (sbyte) i);
+ break;
+ }
+ break;
+ }
+ }
+
+ OptimizeBranches (self);
+ }
+
+ static void OptimizeBranches (MethodBody body)
+ {
+ ComputeOffsets (body);
+
+ foreach (var instruction in body.Instructions) {
+ if (instruction.OpCode.OperandType != OperandType.InlineBrTarget)
+ continue;
+
+ if (OptimizeBranch (instruction))
+ ComputeOffsets (body);
+ }
+ }
+
+ static bool OptimizeBranch (Instruction instruction)
+ {
+ var offset = ((Instruction) instruction.Operand).Offset - (instruction.Offset + instruction.OpCode.Size + 4);
+ if (!(offset >= -128 && offset <= 127))
+ return false;
+
+ switch (instruction.OpCode.Code) {
+ case Code.Br:
+ instruction.OpCode = OpCodes.Br_S;
+ break;
+ case Code.Brfalse:
+ instruction.OpCode = OpCodes.Brfalse_S;
+ break;
+ case Code.Brtrue:
+ instruction.OpCode = OpCodes.Brtrue_S;
+ break;
+ case Code.Beq:
+ instruction.OpCode = OpCodes.Beq_S;
+ break;
+ case Code.Bge:
+ instruction.OpCode = OpCodes.Bge_S;
+ break;
+ case Code.Bgt:
+ instruction.OpCode = OpCodes.Bgt_S;
+ break;
+ case Code.Ble:
+ instruction.OpCode = OpCodes.Ble_S;
+ break;
+ case Code.Blt:
+ instruction.OpCode = OpCodes.Blt_S;
+ break;
+ case Code.Bne_Un:
+ instruction.OpCode = OpCodes.Bne_Un_S;
+ break;
+ case Code.Bge_Un:
+ instruction.OpCode = OpCodes.Bge_Un_S;
+ break;
+ case Code.Bgt_Un:
+ instruction.OpCode = OpCodes.Bgt_Un_S;
+ break;
+ case Code.Ble_Un:
+ instruction.OpCode = OpCodes.Ble_Un_S;
+ break;
+ case Code.Blt_Un:
+ instruction.OpCode = OpCodes.Blt_Un_S;
+ break;
+ case Code.Leave:
+ instruction.OpCode = OpCodes.Leave_S;
+ break;
+ }
+
+ return true;
+ }
+
+ static void ComputeOffsets (MethodBody body)
+ {
+ var offset = 0;
+ foreach (var instruction in body.Instructions) {
+ instruction.Offset = offset;
+ offset += instruction.GetSize ();
+ }
+ }
+ }
+}
diff --git a/rocks/Mono.Cecil.Rocks/ModuleDefinitionRocks.cs b/rocks/Mono.Cecil.Rocks/ModuleDefinitionRocks.cs
new file mode 100644
index 0000000..e3f33f2
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/ModuleDefinitionRocks.cs
@@ -0,0 +1,47 @@
+//
+// ModuleDefinitionRocks.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// 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.Generic;
+using System.Linq;
+
+namespace Mono.Cecil.Rocks {
+
+ public static class ModuleDefinitionRocks {
+
+ public static IEnumerable<TypeDefinition> GetAllTypes (this ModuleDefinition self)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+
+ // it was fun to write, but we need a somewhat less convoluted implementation
+ return self.Types.SelectMany (
+ Functional.Y<TypeDefinition, IEnumerable<TypeDefinition>> (f => type => type.NestedTypes.SelectMany (f).Prepend (type)));
+ }
+ }
+}
diff --git a/rocks/Mono.Cecil.Rocks/ParameterReferenceRocks.cs b/rocks/Mono.Cecil.Rocks/ParameterReferenceRocks.cs
new file mode 100644
index 0000000..554bdb9
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/ParameterReferenceRocks.cs
@@ -0,0 +1,11 @@
+
+namespace Mono.Cecil.Rocks {
+
+ public static class ParameterReferenceRocks {
+
+ public static int GetSequence (this ParameterReference self)
+ {
+ return self.Index + 1;
+ }
+ }
+}
diff --git a/rocks/Mono.Cecil.Rocks/SecurityDeclarationRocks.cs b/rocks/Mono.Cecil.Rocks/SecurityDeclarationRocks.cs
new file mode 100644
index 0000000..f12595b
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/SecurityDeclarationRocks.cs
@@ -0,0 +1,159 @@
+//
+// SecurityDeclarationRocks.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// 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.Security;
+using SSP = System.Security.Permissions;
+
+namespace Mono.Cecil.Rocks {
+
+ public static class SecurityDeclarationRocks {
+
+ public static PermissionSet ToPermissionSet (this SecurityDeclaration self)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+
+ PermissionSet set;
+ if (TryProcessPermissionSetAttribute (self, out set))
+ return set;
+
+ return CreatePermissionSet (self);
+ }
+
+ static bool TryProcessPermissionSetAttribute (SecurityDeclaration declaration, out PermissionSet set)
+ {
+ set = null;
+
+ if (!declaration.HasSecurityAttributes && declaration.SecurityAttributes.Count != 1)
+ return false;
+
+ var security_attribute = declaration.SecurityAttributes [0];
+ if (!security_attribute.AttributeType.IsTypeOf ("System.Security.Permissions", "PermissionSetAttribute"))
+ return false;
+
+ var named_argument = security_attribute.Properties [0];
+ if (named_argument.Name != "XML")
+ throw new NotSupportedException ();
+
+ var attribute = new SSP.PermissionSetAttribute ((SSP.SecurityAction) declaration.Action);
+ attribute.XML = (string) named_argument.Argument.Value;
+
+ set = attribute.CreatePermissionSet ();
+ return true;
+ }
+
+ static PermissionSet CreatePermissionSet (SecurityDeclaration declaration)
+ {
+ var set = new PermissionSet (SSP.PermissionState.None);
+
+ foreach (var attribute in declaration.SecurityAttributes) {
+ var permission = CreatePermission (declaration, attribute);
+ set.AddPermission (permission);
+ }
+
+ return set;
+ }
+
+ static IPermission CreatePermission (SecurityDeclaration declaration, SecurityAttribute attribute)
+ {
+ var attribute_type = Type.GetType (attribute.AttributeType.FullName);
+ if (attribute_type == null)
+ throw new ArgumentException ();
+
+ var security_attribute = CreateSecurityAttribute (attribute_type, declaration);
+ if (security_attribute == null)
+ throw new InvalidOperationException ();
+
+ CompleteSecurityAttribute (security_attribute, attribute);
+
+ return security_attribute.CreatePermission ();
+ }
+
+ static void CompleteSecurityAttribute (SSP.SecurityAttribute security_attribute, SecurityAttribute attribute)
+ {
+ if (attribute.HasFields)
+ CompleteSecurityAttributeFields (security_attribute, attribute);
+
+ if (attribute.HasProperties)
+ CompleteSecurityAttributeProperties (security_attribute, attribute);
+ }
+
+ static void CompleteSecurityAttributeFields (SSP.SecurityAttribute security_attribute, SecurityAttribute attribute)
+ {
+ var type = security_attribute.GetType ();
+
+ foreach (var named_argument in attribute.Fields)
+ type.GetField (named_argument.Name).SetValue (security_attribute, named_argument.Argument.Value);
+ }
+
+ static void CompleteSecurityAttributeProperties (SSP.SecurityAttribute security_attribute, SecurityAttribute attribute)
+ {
+ var type = security_attribute.GetType ();
+
+ foreach (var named_argument in attribute.Properties)
+ type.GetProperty (named_argument.Name).SetValue (security_attribute, named_argument.Argument.Value, null);
+ }
+
+ static SSP.SecurityAttribute CreateSecurityAttribute (Type attribute_type, SecurityDeclaration declaration)
+ {
+ SSP.SecurityAttribute security_attribute;
+ try {
+ security_attribute = (SSP.SecurityAttribute) Activator.CreateInstance (
+ attribute_type, new object [] { (SSP.SecurityAction) declaration.Action });
+ } catch (MissingMethodException) {
+ security_attribute = (SSP.SecurityAttribute) Activator.CreateInstance (attribute_type, new object [0]);
+ }
+
+ return security_attribute;
+ }
+
+ public static SecurityDeclaration ToSecurityDeclaration (this PermissionSet self, SecurityAction action, ModuleDefinition module)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+ if (module == null)
+ throw new ArgumentNullException ("module");
+
+ var declaration = new SecurityDeclaration (action);
+
+ var attribute = new SecurityAttribute (
+ module.TypeSystem.LookupType ("System.Security.Permissions", "PermissionSetAttribute"));
+
+ attribute.Properties.Add (
+ new CustomAttributeNamedArgument (
+ "XML",
+ new CustomAttributeArgument (
+ module.TypeSystem.String, self.ToXml ().ToString ())));
+
+ declaration.SecurityAttributes.Add (attribute);
+
+ return declaration;
+ }
+ }
+}
diff --git a/rocks/Mono.Cecil.Rocks/TypeDefinitionRocks.cs b/rocks/Mono.Cecil.Rocks/TypeDefinitionRocks.cs
new file mode 100644
index 0000000..3eb3a7a
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/TypeDefinitionRocks.cs
@@ -0,0 +1,70 @@
+//
+// TypeDefinitionRocks.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// 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.Generic;
+using System.Linq;
+
+namespace Mono.Cecil.Rocks {
+
+ public static class TypeDefinitionRocks {
+
+ public static IEnumerable<MethodDefinition> GetConstructors (this TypeDefinition self)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+
+ if (!self.HasMethods)
+ return Empty<MethodDefinition>.Array;
+
+ return self.Methods.Where (method => method.IsConstructor);
+ }
+
+ public static MethodDefinition GetStaticConstructor (this TypeDefinition self)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+
+ if (!self.HasMethods)
+ return null;
+
+ return self.GetConstructors ().FirstOrDefault (ctor => ctor.IsStatic);
+ }
+
+ public static IEnumerable<MethodDefinition> GetMethods (this TypeDefinition self)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+
+ if (!self.HasMethods)
+ return Empty<MethodDefinition>.Array;
+
+ return self.Methods.Where (method => !method.IsConstructor);
+ }
+ }
+}
diff --git a/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs b/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs
new file mode 100644
index 0000000..4dd541a
--- /dev/null
+++ b/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs
@@ -0,0 +1,104 @@
+//
+// TypeReferenceRocks.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// 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.Generic;
+using System.Linq;
+
+namespace Mono.Cecil.Rocks {
+
+ public static class TypeReferenceRocks {
+
+ public static ArrayType MakeArrayType (this TypeReference self)
+ {
+ return new ArrayType (self);
+ }
+
+ public static ArrayType MakeArrayType (this TypeReference self, int rank)
+ {
+ if (rank == 0)
+ throw new ArgumentOutOfRangeException ("rank");
+
+ var array = new ArrayType (self);
+
+ for (int i = 1; i < rank; i++)
+ array.Dimensions.Add (new ArrayDimension ());
+
+ return array;
+ }
+
+ public static PointerType MakePointerType (this TypeReference self)
+ {
+ return new PointerType (self);
+ }
+
+ public static ByReferenceType MakeByReferenceType (this TypeReference self)
+ {
+ return new ByReferenceType (self);
+ }
+
+ public static OptionalModifierType MakeOptionalModifierType (this TypeReference self, TypeReference modifierType)
+ {
+ return new OptionalModifierType (modifierType, self);
+ }
+
+ public static RequiredModifierType MakeRequiredModifierType (this TypeReference self, TypeReference modifierType)
+ {
+ return new RequiredModifierType (modifierType, self);
+ }
+
+ public static GenericInstanceType MakeGenericInstanceType (this TypeReference self, params TypeReference [] arguments)
+ {
+ if (self == null)
+ throw new ArgumentNullException ("self");
+ if (arguments == null)
+ throw new ArgumentNullException ("arguments");
+ if (arguments.Length == 0)
+ throw new ArgumentException ();
+ if (self.GenericParameters.Count != arguments.Length)
+ throw new ArgumentException ();
+
+ var instance = new GenericInstanceType (self);
+
+ foreach (var argument in arguments)
+ instance.GenericArguments.Add (argument);
+
+ return instance;
+ }
+
+ public static PinnedType MakePinnedType (this TypeReference self)
+ {
+ return new PinnedType (self);
+ }
+
+ public static SentinelType MakeSentinelType (this TypeReference self)
+ {
+ return new SentinelType (self);
+ }
+ }
+}
diff --git a/rocks/Test/.gitignore b/rocks/Test/.gitignore
new file mode 100644
index 0000000..3629e37
--- /dev/null
+++ b/rocks/Test/.gitignore
@@ -0,0 +1,7 @@
+bin
+obj
+*.suo
+*.user
+*.pidb
+*.userprefs
+*.xml
diff --git a/rocks/Test/Mono.Cecil.Rocks.Tests.csproj b/rocks/Test/Mono.Cecil.Rocks.Tests.csproj
new file mode 100644
index 0000000..b35c480
--- /dev/null
+++ b/rocks/Test/Mono.Cecil.Rocks.Tests.csproj
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.30729</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{C6CFD7E1-B855-44DC-B4CE-9CD72984AF52}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Mono.Cecil.Rocks.Tests</RootNamespace>
+ <AssemblyName>Mono.Cecil.Rocks.Tests</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="nunit.core" />
+ <Reference Include="nunit.core.interfaces" />
+ <Reference Include="nunit.framework" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\..\Mono.Cecil.csproj">
+ <Project>{D68133BD-1E63-496E-9EDE-4FBDBF77B486}</Project>
+ <Name>Mono.Cecil</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\..\Test\Mono.Cecil.Tests.csproj">
+ <Project>{A47B1F49-A81A-43E8-BE6B-DD28AF2C4055}</Project>
+ <Name>Mono.Cecil.Tests</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\Mono.Cecil.Rocks.csproj">
+ <Project>{FBC6DD59-D09D-499C-B03C-99C1C78FF2AC}</Project>
+ <Name>Mono.Cecil.Rocks</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Mono.Cecil.Tests\ModuleDefinitionRocksTests.cs" />
+ <Compile Include="Mono.Cecil.Tests\SecurityDeclarationRocksTests.cs" />
+ <Compile Include="Mono.Cecil.Tests\TypeDefinitionRocksTests.cs" />
+ <Compile Include="Mono.Cecil.Tests\TypeReferenceRocksTests.cs" />
+ <Content Include="Resources\cs\Types.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="Resources\assemblies\decsec-att.dll" />
+ <Content Include="Resources\assemblies\decsec-xml.dll" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ No newline at end of file
diff --git a/rocks/Test/Mono.Cecil.Tests/ModuleDefinitionRocksTests.cs b/rocks/Test/Mono.Cecil.Tests/ModuleDefinitionRocksTests.cs
new file mode 100644
index 0000000..f172b40
--- /dev/null
+++ b/rocks/Test/Mono.Cecil.Tests/ModuleDefinitionRocksTests.cs
@@ -0,0 +1,27 @@
+using System.Linq;
+
+using NUnit.Framework;
+
+using Mono.Cecil.Rocks;
+
+namespace Mono.Cecil.Tests {
+
+ [TestFixture]
+ public class ModuleDefinitionRocksTests : BaseTestFixture {
+
+ [TestCSharp ("Types.cs")]
+ public void GetAllTypesTest (ModuleDefinition module)
+ {
+ var sequence = new [] {
+ module.GetType ("<Module>"),
+ module.GetType ("Foo"),
+ module.GetType ("Foo/Bar"),
+ module.GetType ("Foo/Gazonk"),
+ module.GetType ("Foo/Gazonk/Baz"),
+ module.GetType ("Pan"),
+ };
+
+ Assert.IsTrue (sequence.SequenceEqual (module.GetAllTypes ()));
+ }
+ }
+}
diff --git a/rocks/Test/Mono.Cecil.Tests/SecurityDeclarationRocksTests.cs b/rocks/Test/Mono.Cecil.Tests/SecurityDeclarationRocksTests.cs
new file mode 100644
index 0000000..1825880
--- /dev/null
+++ b/rocks/Test/Mono.Cecil.Tests/SecurityDeclarationRocksTests.cs
@@ -0,0 +1,61 @@
+using NUnit.Framework;
+
+using Mono.Cecil.Rocks;
+
+namespace Mono.Cecil.Tests {
+
+ [TestFixture]
+ public class SecurityDeclarationRocksTests : BaseTestFixture {
+
+ [TestModule ("decsec-xml.dll")]
+ public void ToPermissionSetFromPermissionSetAttribute (ModuleDefinition module)
+ {
+ var type = module.GetType ("SubLibrary");
+
+ Assert.IsTrue (type.HasSecurityDeclarations);
+ Assert.AreEqual (1, type.SecurityDeclarations.Count);
+
+ var declaration = type.SecurityDeclarations [0];
+
+ var permission_set = declaration.ToPermissionSet ();
+
+ Assert.IsNotNull (permission_set);
+
+ const string permission_set_value = "<PermissionSet class=\"System.Security.PermissionSe"
+ + "t\"\r\nversion=\"1\">\r\n<IPermission class=\"System.Security.Permis"
+ + "sions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture"
+ + "=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nFla"
+ + "gs=\"UnmanagedCode\"/>\r\n</PermissionSet>\r\n";
+
+ Assert.AreEqual (Normalize (permission_set_value), Normalize (permission_set.ToXml ().ToString ()));
+ }
+
+ [TestModule ("decsec-att.dll")]
+ public void ToPermissionSetFromSecurityAttribute (ModuleDefinition module)
+ {
+ var type = module.GetType ("SubLibrary");
+
+ Assert.IsTrue (type.HasSecurityDeclarations);
+ Assert.AreEqual (1, type.SecurityDeclarations.Count);
+
+ var declaration = type.SecurityDeclarations [0];
+
+ var permission_set = declaration.ToPermissionSet ();
+
+ Assert.IsNotNull (permission_set);
+
+ const string permission_set_value = "<PermissionSet class=\"System.Security.PermissionSe"
+ + "t\"\r\nversion=\"1\">\r\n<IPermission class=\"System.Security.Permis"
+ + "sions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture"
+ + "=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nFla"
+ + "gs=\"UnmanagedCode\"/>\r\n</PermissionSet>\r\n";
+
+ Assert.AreEqual (Normalize (permission_set_value), Normalize (permission_set.ToXml ().ToString ()));
+ }
+
+ static string Normalize (string s)
+ {
+ return s.Replace ("\n", "").Replace ("\r", "");
+ }
+ }
+}
diff --git a/rocks/Test/Mono.Cecil.Tests/TypeDefinitionRocksTests.cs b/rocks/Test/Mono.Cecil.Tests/TypeDefinitionRocksTests.cs
new file mode 100644
index 0000000..1387189
--- /dev/null
+++ b/rocks/Test/Mono.Cecil.Tests/TypeDefinitionRocksTests.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Linq;
+using Mono.Cecil.Rocks;
+
+using NUnit.Framework;
+
+namespace Mono.Cecil.Tests {
+
+ [TestFixture]
+ public class TypeDefinitionRocksTests {
+
+ class Foo {
+
+ static Foo ()
+ {
+ }
+
+ public Foo (int a)
+ {
+ }
+
+ public Foo (int a, string s)
+ {
+ }
+
+ public static void Bar ()
+ {
+ }
+
+ void Baz ()
+ {
+ }
+ }
+
+ [Test]
+ public void GetConstructors ()
+ {
+ var foo = typeof (Foo).ToDefinition ();
+ var ctors = foo.GetConstructors ().ToArray ();
+
+ Assert.AreEqual (3, ctors.Length);
+ Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.cctor()", ctors [0].FullName);
+ Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.ctor(System.Int32)", ctors [1].FullName);
+ Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.ctor(System.Int32,System.String)", ctors [2].FullName);
+ }
+
+ [Test]
+ public void GetStaticConstructor ()
+ {
+ var foo = typeof (Foo).ToDefinition ();
+ var cctor = foo.GetStaticConstructor ();
+
+ Assert.IsNotNull (cctor);
+ Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.cctor()", cctor.FullName);
+ }
+
+ [Test]
+ public void GetMethods ()
+ {
+ var foo = typeof (Foo).ToDefinition ();
+ var methods = foo.GetMethods ().ToArray ();
+
+ Assert.AreEqual (2, methods.Length);
+ Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::Bar()", methods [0].FullName);
+ Assert.AreEqual ("System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::Baz()", methods [1].FullName);
+ }
+ }
+} \ No newline at end of file
diff --git a/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs b/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs
new file mode 100644
index 0000000..3845eaa
--- /dev/null
+++ b/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs
@@ -0,0 +1,124 @@
+using System;
+
+using Mono.Cecil.Rocks;
+
+using NUnit.Framework;
+
+namespace Mono.Cecil.Tests {
+
+ [TestFixture]
+ public class TypeReferenceRocksTests {
+
+ [Test]
+ public void MakeArrayType ()
+ {
+ var @string = GetTypeReference (typeof (string));
+
+ var string_array = @string.MakeArrayType ();
+
+ Assert.IsInstanceOfType (typeof (ArrayType), string_array);
+ Assert.AreEqual (1, string_array.Rank);
+ }
+
+ [Test]
+ public void MakeArrayTypeRank ()
+ {
+ var @string = GetTypeReference (typeof (string));
+
+ var string_array = @string.MakeArrayType (3);
+
+ Assert.IsInstanceOfType (typeof (ArrayType), string_array);
+ Assert.AreEqual (3, string_array.Rank);
+ }
+
+ [Test]
+ public void MakePointerType ()
+ {
+ var @string = GetTypeReference (typeof (string));
+
+ var string_ptr = @string.MakePointerType ();
+
+ Assert.IsInstanceOfType (typeof (PointerType), string_ptr);
+ }
+
+ [Test]
+ public void MakeByReferenceType ()
+ {
+ var @string = GetTypeReference (typeof (string));
+
+ var string_byref = @string.MakeByReferenceType ();
+
+ Assert.IsInstanceOfType (typeof (ByReferenceType), string_byref);
+ }
+
+ class OptionalModifier {}
+
+ [Test]
+ public void MakeOptionalModifierType ()
+ {
+ var @string = GetTypeReference (typeof (string));
+ var modopt = GetTypeReference (typeof (OptionalModifier));
+
+ var string_modopt = @string.MakeOptionalModifierType (modopt);
+
+ Assert.IsInstanceOfType (typeof (OptionalModifierType), string_modopt);
+ Assert.AreEqual (modopt, string_modopt.ModifierType);
+ }
+
+ class RequiredModifier { }
+
+ [Test]
+ public void MakeRequiredModifierType ()
+ {
+ var @string = GetTypeReference (typeof (string));
+ var modreq = GetTypeReference (typeof (RequiredModifierType));
+
+ var string_modreq = @string.MakeRequiredModifierType (modreq);
+
+ Assert.IsInstanceOfType (typeof (RequiredModifierType), string_modreq);
+ Assert.AreEqual (modreq, string_modreq.ModifierType);
+ }
+
+ [Test]
+ public void MakePinnedType ()
+ {
+ var byte_array = GetTypeReference (typeof (byte []));
+
+ var pinned_byte_array = byte_array.MakePinnedType ();
+
+ Assert.IsInstanceOfType (typeof (PinnedType), pinned_byte_array);
+ }
+
+ [Test]
+ public void MakeSentinelType ()
+ {
+ var @string = GetTypeReference (typeof (string));
+
+ var string_sentinel = @string.MakeSentinelType ();
+
+ Assert.IsInstanceOfType (typeof (SentinelType), string_sentinel);
+ }
+
+ class Foo<T1, T2> {}
+
+ [Test]
+ public void MakeGenericInstanceType ()
+ {
+ var foo = GetTypeReference (typeof (Foo<,>));
+ var @string = GetTypeReference (typeof (string));
+ var @int = GetTypeReference (typeof (int));
+
+ var foo_string_int = foo.MakeGenericInstanceType (@string, @int);
+
+ Assert.IsInstanceOfType (typeof (GenericInstanceType), foo_string_int);
+ Assert.AreEqual (2, foo_string_int.GenericArguments.Count);
+ Assert.AreEqual (@string, foo_string_int.GenericArguments [0]);
+ Assert.AreEqual (@int, foo_string_int.GenericArguments [1]);
+ }
+
+ static TypeReference GetTypeReference (Type type)
+ {
+ return ModuleDefinition.ReadModule (typeof (TypeReferenceRocksTests).Module.FullyQualifiedName).Import (type);
+ }
+ }
+} \ No newline at end of file
diff --git a/rocks/Test/Resources/assemblies/decsec-att.dll b/rocks/Test/Resources/assemblies/decsec-att.dll
new file mode 100644
index 0000000..e9ac355
--- /dev/null
+++ b/rocks/Test/Resources/assemblies/decsec-att.dll
Binary files differ
diff --git a/rocks/Test/Resources/assemblies/decsec-xml.dll b/rocks/Test/Resources/assemblies/decsec-xml.dll
new file mode 100644
index 0000000..fbaa51c
--- /dev/null
+++ b/rocks/Test/Resources/assemblies/decsec-xml.dll
Binary files differ
diff --git a/rocks/Test/Resources/cs/Types.cs b/rocks/Test/Resources/cs/Types.cs
new file mode 100755
index 0000000..ba04655
--- /dev/null
+++ b/rocks/Test/Resources/cs/Types.cs
@@ -0,0 +1,14 @@
+public class Foo {
+
+ public class Bar {
+ }
+
+ public class Gazonk {
+
+ public class Baz {
+ }
+ }
+}
+
+public class Pan {
+}