2 // TransformContractsVisitor.cs
5 // Chris Bacon (chrisbacon76@gmail.com)
7 // Copyright (C) 2010 Chris Bacon
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
33 using Mono.CodeContracts.Rewrite;
34 using Mono.CodeContracts.Rewrite.Ast;
35 using System.Diagnostics.Contracts;
37 using Mono.CodeContracts.Rewrite.AstVisitors;
40 namespace Mono.CodeContracts.Rewrite {
41 class TransformContractsVisitor : ExprVisitor {
43 public TransformContractsVisitor (ModuleDefinition module, MethodDefinition method, Dictionary<Expr, Instruction> instructionLookup, ContractsRuntime contractsRuntime)
45 //this.module = method.Module;
46 this.instructionLookup = instructionLookup;
47 this.contractsRuntime = contractsRuntime;
48 this.methodInfo = new MethodInfo (module, method);
51 //private ModuleDefinition module;
52 private Dictionary<Expr, Instruction> instructionLookup;
53 private ContractsRuntime contractsRuntime;
54 private MethodInfo methodInfo;
56 private List<ContractRequiresInfo> contractRequiresInfo = new List<ContractRequiresInfo> ();
58 public IEnumerable<ContractRequiresInfo> ContractRequiresInfo {
59 get { return this.contractRequiresInfo; }
62 protected override Expr VisitCall (ExprCall e)
64 var call = (ExprCall)base.VisitCall (e);
66 var method = e.Method;
67 if (method.DeclaringType.FullName == "System.Diagnostics.Contracts.Contract") {
68 switch (method.Name) {
70 if (!method.HasGenericParameters) {
71 switch (method.Parameters.Count) {
73 return this.ProcessRequires1 (call);
75 return this.ProcessRequires2 (call);
77 throw new NotSupportedException ("Invalid number of parameters to Contract.Requires()");
83 throw new NotSupportedException ("Cannot handle Contract." + e.Method.Name + "()");
90 private string GetConditionString (Expr e)
92 var vSource = new SourcePositionVisitor (this.instructionLookup);
94 var extractor = new ConditionTextExtractor (vSource.SourceCodeFileName, vSource.StartPosition, vSource.EndPosition);
95 return extractor.GetConditionText ();
98 private Expr ProcessRequires1 (ExprCall e)
100 MethodDefinition mRequires = this.contractsRuntime.GetRequires ();
101 Expr conditionExpr = e.Parameters.First ();
102 Expr nullArgExpr = new ExprLoadConstant (this.methodInfo, null);
103 string conditionText = this.GetConditionString (e);
104 Expr conditionStringExpr = new ExprLoadConstant (this.methodInfo, conditionText);
105 var call = new ExprCall (this.methodInfo, mRequires, new Expr [] { conditionExpr, nullArgExpr, conditionStringExpr });
107 this.contractRequiresInfo.Add (new ContractRequiresInfo (e, call));
112 private Expr ProcessRequires2 (ExprCall e)
114 MethodDefinition mRequires = this.contractsRuntime.GetRequires ();
115 Expr conditionExpr = e.Parameters.First ();
116 Expr msgExpr = e.Parameters.ElementAt (1);
117 string conditionText = this.GetConditionString (e);
118 Expr conditionStringExpr = new ExprLoadConstant (this.methodInfo, conditionText);
119 var call = new ExprCall (this.methodInfo, mRequires, new Expr [] { conditionExpr, msgExpr, conditionStringExpr });
121 this.contractRequiresInfo.Add (new ContractRequiresInfo (e, call));