Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

7
external/cecil/rocks/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
bin
obj
*.suo
*.user
*.pidb
*.userprefs
*.xml

View File

@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">net_4_0_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>
<FileAlignment>512</FileAlignment>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\mono.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_3_5_Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\net_3_5_Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;INSIDE_ROCKS;NET_3_5</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_3_5_Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\net_3_5_Release\</OutputPath>
<DefineConstants>TRACE;INSIDE_ROCKS;NET_3_5</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_0_Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\net_4_0_Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;INSIDE_ROCKS;NET_3_5;NET_4_0</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_0_Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\net_4_0_Release\</OutputPath>
<DefineConstants>TRACE;INSIDE_ROCKS;NET_3_5;NET_4_0</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'silverlight_Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\silverlight_Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NET_3_5;NET_4_0;SILVERLIGHT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'silverlight_Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\silverlight_Release\</OutputPath>
<DefineConstants>TRACE;NET_3_5;NET_4_0;SILVERLIGHT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'winphone_Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\winphone_Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NET_3_5;NET_4_0;SILVERLIGHT;CF</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkProfile>WindowsPhone</TargetFrameworkProfile>
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'winphone_Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\winphone_Release\</OutputPath>
<DefineConstants>TRACE;NET_3_5;NET_4_0;SILVERLIGHT;CF</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkProfile>WindowsPhone</TargetFrameworkProfile>
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Mono.Cecil.Rocks\AssemblyInfo.cs" />
<Compile Include="Mono.Cecil.Rocks\MethodDefinitionRocks.cs" />
<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>
<ItemGroup>
<ProjectReference Include="..\Mono.Cecil.csproj">
<Project>{D68133BD-1E63-496E-9EDE-4FBDBF77B486}</Project>
<Name>Mono.Cecil</Name>
</ProjectReference>
</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>

View File

@@ -0,0 +1,41 @@
//
// AssemblyInfo.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle ("Mono.Cecil.Rocks")]
[assembly: AssemblyProduct ("Mono.Cecil")]
[assembly: AssemblyCopyright ("Copyright © 2008 - 2011 Jb Evain")]
[assembly: CLSCompliant (false)]
[assembly: ComVisible (false)]
[assembly: AssemblyVersion ("0.9.5.0")]
[assembly: AssemblyFileVersion ("0.9.5.0")]

View File

@@ -0,0 +1,59 @@
//
// Functional.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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;
}
}
}

View File

@@ -0,0 +1,236 @@
//
// ILParser.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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 {
#if INSIDE_ROCKS
public
#endif
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);
}
#if INSIDE_ROCKS
public
#endif
static class ILParser {
class ParseContext {
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) => new CodeReader (reader.image.MetadataSection, reader));
return new ParseContext {
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 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);
break;
}
var method_ref = member as MethodReference;
if (method_ref != null) {
visitor.OnInlineMethod (opcode, method_ref);
break;
}
throw new InvalidOperationException ();
}
break;
}
}
}
static VariableDefinition GetVariable (ParseContext context, int index)
{
return context.Variables [index];
}
}
}

View File

@@ -0,0 +1,407 @@
//
// MethodBodyRocks.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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 {
#if INSIDE_ROCKS
public
#endif
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, self.GetParameter (0));
break;
case Code.Ldarg_1:
ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (1));
break;
case Code.Ldarg_2:
ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (2));
break;
case Code.Ldarg_3:
ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (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 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 ();
}
}
}
}

View File

@@ -0,0 +1,88 @@
//
// MethodDefinitionRocks.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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;
namespace Mono.Cecil.Rocks {
#if INSIDE_ROCKS
public
#endif
static class MethodDefinitionRocks {
public static MethodDefinition GetBaseMethod (this MethodDefinition self)
{
if (self == null)
throw new ArgumentNullException ("self");
if (!self.IsVirtual)
return self;
var base_type = ResolveBaseType (self.DeclaringType);
while (base_type != null) {
var @base = GetMatchingMethod (base_type, self);
if (@base != null)
return @base;
base_type = ResolveBaseType (base_type);
}
return self;
}
public static MethodDefinition GetOriginalBaseMethod (this MethodDefinition self)
{
if (self == null)
throw new ArgumentNullException ("self");
while (true) {
var @base = self.GetBaseMethod ();
if (@base == self)
return self;
self = @base;
}
}
static TypeDefinition ResolveBaseType (TypeDefinition type)
{
if (type == null)
return null;
var base_type = type.BaseType;
if (base_type == null)
return null;
return base_type.Resolve ();
}
static MethodDefinition GetMatchingMethod (TypeDefinition type, MethodDefinition method)
{
return MetadataResolver.GetMethod (type.Methods, method);
}
}
}

View File

@@ -0,0 +1,50 @@
//
// ModuleDefinitionRocks.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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 {
#if INSIDE_ROCKS
public
#endif
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)));
}
}
}

View File

@@ -0,0 +1,11 @@
namespace Mono.Cecil.Rocks {
public static class ParameterReferenceRocks {
public static int GetSequence (this ParameterReference self)
{
return self.Index + 1;
}
}
}

View File

@@ -0,0 +1,174 @@
//
// SecurityDeclarationRocks.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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.
//
#if !SILVERLIGHT && !CF
using System;
using System.Security;
using SSP = System.Security.Permissions;
namespace Mono.Cecil.Rocks {
#if INSIDE_ROCKS
public
#endif
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 attribute = new SSP.PermissionSetAttribute ((SSP.SecurityAction) declaration.Action);
var named_argument = security_attribute.Properties [0];
string value = (string) named_argument.Argument.Value;
switch (named_argument.Name) {
case "XML":
attribute.XML = value;
break;
case "Name":
attribute.Name = value;
break;
default:
throw new NotImplementedException (named_argument.Name);
}
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 ("attribute");
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;
}
}
}
#endif

View File

@@ -0,0 +1,83 @@
//
// TypeDefinitionRocks.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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 {
#if INSIDE_ROCKS
public
#endif
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);
}
public static TypeReference GetEnumUnderlyingType (this TypeDefinition self)
{
if (self == null)
throw new ArgumentNullException ("self");
if (!self.IsEnum)
throw new ArgumentException ();
return Mixin.GetEnumUnderlyingType (self);
}
}
}

View File

@@ -0,0 +1,107 @@
//
// TypeReferenceRocks.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2011 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 {
#if INSIDE_ROCKS
public
#endif
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);
}
}
}

7
external/cecil/rocks/Test/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
bin
obj
*.suo
*.user
*.pidb
*.userprefs
*.xml

View File

@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">net_4_0_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>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_3_5_Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\net_3_5_Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NET_3_5</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_3_5_Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\net_3_5_Release\</OutputPath>
<DefineConstants>TRACE;NET_3_5</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_0_Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\net_4_0_Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NET_3_5;NET_4_0</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_0_Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\net_4_0_Release\</OutputPath>
<DefineConstants>TRACE;NET_3_5;NET_4_0</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.core">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Test\libs\nunit-2.6.2\nunit.core.dll</HintPath>
</Reference>
<Reference Include="nunit.core.interfaces">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Test\libs\nunit-2.6.2\nunit.core.interfaces.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Test\libs\nunit-2.6.2\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</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>
<Compile Include="Mono.Cecil.Tests\Addin.cs" />
<Compile Include="Mono.Cecil.Tests\MethodDefinitionRocksTests.cs" />
<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>

View File

@@ -0,0 +1,8 @@
using NUnit.Core.Extensibility;
namespace Mono.Cecil.Tests {
[NUnitAddin]
public class CecilRocksAddin : CecilTestAddin {
}
}

View File

@@ -0,0 +1,53 @@
using System.Linq;
using NUnit.Framework;
using Mono.Cecil.Rocks;
namespace Mono.Cecil.Tests {
[TestFixture]
public class MethodDefinitionRocksTests : BaseTestFixture {
abstract class Foo {
public abstract void DoFoo ();
}
class Bar : Foo {
public override void DoFoo ()
{
}
}
class Baz : Bar {
public override void DoFoo ()
{
}
}
[Test]
public void GetBaseMethod ()
{
var baz = typeof (Baz).ToDefinition ();
var baz_dofoo = baz.GetMethod ("DoFoo");
var @base = baz_dofoo.GetBaseMethod ();
Assert.AreEqual ("Bar", @base.DeclaringType.Name);
@base = @base.GetBaseMethod ();
Assert.AreEqual ("Foo", @base.DeclaringType.Name);
Assert.AreEqual (@base, @base.GetBaseMethod ());
}
[Test]
public void GetOriginalBaseMethod ()
{
var baz = typeof (Baz).ToDefinition ();
var baz_dofoo = baz.GetMethod ("DoFoo");
var @base = baz_dofoo.GetOriginalBaseMethod ();
Assert.AreEqual ("Foo", @base.DeclaringType.Name);
}
}
}

View File

@@ -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 ()));
}
}
}

View File

@@ -0,0 +1,63 @@
using System.Security.Permissions;
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);
string permission_set_value = "<PermissionSet class=\"System.Security.PermissionSe"
+ "t\"\r\nversion=\"1\">\r\n<IPermission class=\"{0}\"\r\nversion=\"1\"\r\nFla"
+ "gs=\"UnmanagedCode\"/>\r\n</PermissionSet>\r\n";
permission_set_value = string.Format (permission_set_value, typeof (SecurityPermission).AssemblyQualifiedName);
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);
string permission_set_value = "<PermissionSet class=\"System.Security.PermissionSe"
+ "t\"\r\nversion=\"1\">\r\n<IPermission class=\"{0}\"\r\nversion=\"1\"\r\nFla"
+ "gs=\"UnmanagedCode\"/>\r\n</PermissionSet>\r\n";
permission_set_value = string.Format (permission_set_value, typeof (SecurityPermission).AssemblyQualifiedName);
Assert.AreEqual (Normalize (permission_set_value), Normalize (permission_set.ToXml ().ToString ()));
}
static string Normalize (string s)
{
return s.Replace ("\n", "").Replace ("\r", "");
}
}
}

View File

@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
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 ().Select (ctor => ctor.FullName);
var expected = new [] {
"System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.cctor()",
"System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.ctor(System.Int32)",
"System.Void Mono.Cecil.Tests.TypeDefinitionRocksTests/Foo::.ctor(System.Int32,System.String)",
};
AssertSet (expected, ctors);
}
static void AssertSet<T> (IEnumerable<T> expected, IEnumerable<T> actual)
{
Assert.IsFalse (expected.Except (actual).Any ());
Assert.IsTrue (expected.Intersect (actual).SequenceEqual (expected));
}
[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);
}
enum Pan : byte {
Pin,
Pon,
}
[Test]
public void GetEnumUnderlyingType ()
{
var pan = typeof (Pan).ToDefinition ();
Assert.IsNotNull (pan);
Assert.IsTrue (pan.IsEnum);
var underlying_type = pan.GetEnumUnderlyingType ();
Assert.IsNotNull (underlying_type);
Assert.AreEqual ("System.Byte", underlying_type.FullName);
}
}
}

View File

@@ -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.IsInstanceOf (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.IsInstanceOf (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.IsInstanceOf (typeof (PointerType), string_ptr);
}
[Test]
public void MakeByReferenceType ()
{
var @string = GetTypeReference (typeof (string));
var string_byref = @string.MakeByReferenceType ();
Assert.IsInstanceOf (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.IsInstanceOf (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.IsInstanceOf (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.IsInstanceOf (typeof (PinnedType), pinned_byte_array);
}
[Test]
public void MakeSentinelType ()
{
var @string = GetTypeReference (typeof (string));
var string_sentinel = @string.MakeSentinelType ();
Assert.IsInstanceOf (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.IsInstanceOf (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);
}
}
}

Some files were not shown because too many files have changed in this diff Show More