1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
//
// PerformRewrite.cs
//
// Authors:
// Chris Bacon (chrisbacon76@gmail.com)
//
// Copyright (C) 2010 Chris Bacon
//
// 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;
using System.Text;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.CodeContracts.Rewrite.Ast;
using Mono.CodeContracts.Rewrite.AstVisitors;
namespace Mono.CodeContracts.Rewrite {
class PerformRewrite {
public PerformRewrite (RewriterOptions options)
{
this.options = options;
}
private RewriterOptions options;
private Dictionary<MethodDefinition, TransformContractsVisitor> rewrittenMethods = new Dictionary<MethodDefinition, TransformContractsVisitor> ();
public void Rewrite (AssemblyDefinition assembly)
{
foreach (ModuleDefinition module in assembly.Modules) {
ContractsRuntime contractsRuntime = new ContractsRuntime(module, this.options);
var allMethods =
from type in module.Types
from method in type.Methods
select method;
foreach (MethodDefinition method in allMethods.ToArray ()) {
this.RewriteMethod (module, method, contractsRuntime);
}
}
}
private void RewriteMethod (ModuleDefinition module, MethodDefinition method, ContractsRuntime contractsRuntime)
{
if (this.rewrittenMethods.ContainsKey (method)) {
return;
}
var overridden = this.GetOverriddenMethod (method);
if (overridden != null) {
this.RewriteMethod (module, overridden, contractsRuntime);
}
bool anyRewrites = false;
var baseMethod = this.GetBaseOverriddenMethod (method);
if (baseMethod != method) {
// Contract inheritance must be used
var vOverriddenTransform = this.rewrittenMethods [baseMethod];
// Can be null if overriding an abstract method
if (vOverriddenTransform != null) {
if (this.options.Level >= 2) {
// Only insert re-written contracts if level >= 2
foreach (var inheritedRequires in vOverriddenTransform.ContractRequiresInfo) {
this.RewriteIL (method.Body, null, null, inheritedRequires.RewrittenExpr);
anyRewrites = true;
}
}
}
}
TransformContractsVisitor vTransform = null;
if (method.HasBody) {
vTransform = this.TransformContracts (module, method, contractsRuntime);
if (vTransform.ContractRequiresInfo.Any ()) {
anyRewrites = true;
}
}
this.rewrittenMethods.Add (method, vTransform);
if (anyRewrites) {
Console.WriteLine (method);
}
}
private TransformContractsVisitor TransformContracts (ModuleDefinition module, MethodDefinition method, ContractsRuntime contractsRuntime)
{
var body = method.Body;
Decompile decompile = new Decompile (module, method);
var decomp = decompile.Go ();
TransformContractsVisitor vTransform = new TransformContractsVisitor (module, method, decompile.Instructions, contractsRuntime);
vTransform.Visit (decomp);
foreach (var replacement in vTransform.ContractRequiresInfo) {
// Only insert re-written contracts if level >= 2
Expr rewritten = this.options.Level >= 2 ? replacement.RewrittenExpr : null;
this.RewriteIL (body, decompile.Instructions, replacement.OriginalExpr, rewritten);
}
return vTransform;
}
private void RewriteIL (MethodBody body, Dictionary<Expr,Instruction> instructionLookup, Expr remove, Expr insert)
{
var il = body.GetILProcessor ();
Instruction instInsertBefore;
if (remove != null) {
var vInstExtent = new InstructionExtentVisitor (instructionLookup);
vInstExtent.Visit (remove);
instInsertBefore = vInstExtent.Instructions.Last ().Next;
foreach (var instRemove in vInstExtent.Instructions) {
il.Remove (instRemove);
}
} else {
instInsertBefore = body.Instructions [0];
}
if (insert != null) {
var compiler = new CompileVisitor (il, instructionLookup, inst => il.InsertBefore (instInsertBefore, inst));
compiler.Visit (insert);
}
}
private MethodDefinition GetOverriddenMethod (MethodDefinition method)
{
if (method.IsNewSlot || !method.IsVirtual) {
return null;
}
var baseType = method.DeclaringType.BaseType;
if (baseType == null) {
return null;
}
var overridden = baseType.Resolve ().Methods.FirstOrDefault (x => x.Name == method.Name);
return overridden;
}
private MethodDefinition GetBaseOverriddenMethod (MethodDefinition method)
{
var overridden = method;
while (true) {
var overriddenTemp = this.GetOverriddenMethod (overridden);
if (overriddenTemp == null) {
return overridden;
}
overridden = overriddenTemp;
}
}
}
}
|