2 // ExpressionPrinterFactory.cs
5 // Alexander Chebaturkin (chebaturkin@gmail.com)
7 // Copyright (C) 2011 Alexander Chebaturkin
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.
31 using Mono.CodeContracts.Static.AST;
32 using Mono.CodeContracts.Static.AST.Visitors;
33 using Mono.CodeContracts.Static.Analysis.Drivers;
34 using Mono.CodeContracts.Static.Analysis.HeapAnalysis;
35 using Mono.CodeContracts.Static.ControlFlow;
36 using Mono.CodeContracts.Static.DataStructures;
38 namespace Mono.CodeContracts.Static.Analysis.ExpressionAnalysis {
39 static class ExpressionPrinterFactory {
40 public static Func<LabeledSymbol<APC, SymbolicValue>, string> Printer<SymbolicValue>
41 (IExpressionContextProvider<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> contextProvider,
42 IMethodDriver<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> methodDriver)
43 where SymbolicValue : IEquatable<SymbolicValue>
45 return new PrinterImpl<SymbolicValue> (contextProvider, methodDriver).PrintAt;
48 #region Nested type: PrinterImpl
49 private class PrinterImpl<SymbolicValue> :
50 ISymbolicExpressionVisitor<LabeledSymbol<APC, SymbolicValue>, LabeledSymbol<APC, SymbolicValue>, SymbolicValue, StringBuilder, Dummy>
51 where SymbolicValue : IEquatable<SymbolicValue> {
52 private readonly IExpressionContextProvider<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> context_provider;
53 private readonly IMethodDriver<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> method_driver;
55 public PrinterImpl (IExpressionContextProvider<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> contextProvider,
56 IMethodDriver<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> methodDriver)
58 this.context_provider = contextProvider;
59 this.method_driver = methodDriver;
62 public string PrintAt (LabeledSymbol<APC, SymbolicValue> expr)
64 var sb = new StringBuilder ();
66 return sb.ToString ();
69 private void Recurse (StringBuilder sb, LabeledSymbol<APC, SymbolicValue> expr)
71 if (expr.Symbol.Equals (default(SymbolicValue)))
72 sb.Append ("<!null!>");
74 this.context_provider.ExpressionContext.Decode<StringBuilder, Dummy, PrinterImpl<SymbolicValue>> (expr, this, sb);
77 #region Implementation of IExpressionILVisitor<ExternalExpression<APC,SymbolicValue>,ExternalExpression<APC,SymbolicValue>,SymbolicValue,StringBuilder,Dummy>
78 public Dummy Binary (LabeledSymbol<APC, SymbolicValue> pc, BinaryOperator op, SymbolicValue dest, LabeledSymbol<APC, SymbolicValue> operand1, LabeledSymbol<APC, SymbolicValue> operand2,
82 Recurse (data, operand1);
83 data.AppendFormat (" {0} ", op);
84 Recurse (data, operand2);
89 public Dummy Isinst (LabeledSymbol<APC, SymbolicValue> pc, TypeNode type, SymbolicValue dest, LabeledSymbol<APC, SymbolicValue> obj, StringBuilder data)
91 data.AppendFormat ("IsInst({0}) ", this.method_driver.MetaDataProvider.FullName (type));
96 public Dummy LoadNull (LabeledSymbol<APC, SymbolicValue> pc, SymbolicValue dest, StringBuilder polarity)
98 polarity.Append ("NULL");
102 public Dummy LoadConst (LabeledSymbol<APC, SymbolicValue> pc, TypeNode type, object constant, SymbolicValue dest, StringBuilder data)
104 data.Append (constant.ToString ());
108 public Dummy Sizeof (LabeledSymbol<APC, SymbolicValue> pc, TypeNode type, SymbolicValue dest, StringBuilder data)
110 data.AppendFormat ("sizeof({0})", this.method_driver.MetaDataProvider.FullName (type));
114 public Dummy Unary (LabeledSymbol<APC, SymbolicValue> pc, UnaryOperator op, bool unsigned, SymbolicValue dest, LabeledSymbol<APC, SymbolicValue> source, StringBuilder data)
116 data.AppendFormat ("{0} ", op.ToString ());
117 Recurse (data, source);
122 #region Implementation of ISymbolicExpressionVisitor<ExternalExpression<APC,SymbolicValue>,ExternalExpression<APC,SymbolicValue>,SymbolicValue,StringBuilder,Dummy>
123 public Dummy SymbolicConstant (LabeledSymbol<APC, SymbolicValue> pc, SymbolicValue variable, StringBuilder data)
125 data.Append (variable.ToString ());