2 // FullExpressionDecoder.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.
30 using System.Collections.Generic;
31 using Mono.CodeContracts.Static.AST;
32 using Mono.CodeContracts.Static.Analysis.HeapAnalysis.Paths;
33 using Mono.CodeContracts.Static.Analysis.Numerical;
34 using Mono.CodeContracts.Static.DataStructures;
35 using Mono.CodeContracts.Static.Lattices;
36 using Mono.CodeContracts.Static.Providers;
38 namespace Mono.CodeContracts.Static.Analysis.ExpressionAnalysis.Decoding {
39 class FullExpressionDecoder<V, E> : IFullExpressionDecoder<V, E>
40 where V : IEquatable<V>
41 where E : IEquatable<E> {
42 public readonly VisitorForIsBinaryExpression<V, E> BinaryExpressionVisitor;
43 public readonly VisitorForIsInst<V, E> IsInstVisitor;
44 public readonly VisitorForIsNull<V, E> IsNullVisitor;
45 public readonly VisitorForSizeOf<V, E> SizeOfVisitor;
46 public readonly VisitorForIsUnaryExpression<V, E> UnaryExpressionVisitor;
47 public readonly VisitorForUnderlyingVariable<V, E> UnderlyingVariableVisitor;
48 public readonly VisitorForValueOf<V, E> ValueOfVisitor;
49 public readonly VisitorForVariable<V, E> VariableVisitor;
50 public readonly VisitorForVariablesIn<V, E> VariablesInVisitor;
51 protected readonly IMetaDataProvider MetaDataProvider;
53 #region Implementation of IFullExpressionDecoder<V,E>
54 public bool IsVariable (E expr, out object variable)
57 bool res = VisitorForVariable<V, E>.IsVariable (expr, out var, this);
63 public V UnderlyingVariable (E expr)
65 return VisitorForUnderlyingVariable<V, E>.IsUnderlyingVariable (expr, this);
68 public bool IsNull (E expr)
70 return VisitorForIsNull<V, E>.IsNull (expr, this);
73 public bool IsConstant (E expr, out object value, out TypeNode type)
75 return VisitorForValueOf<V, E>.IsConstant (expr, out value, out type, this);
78 public bool IsSizeof (E expr, out TypeNode type)
80 return VisitorForSizeOf<V, E>.IsSizeOf (expr, out type, this);
83 public bool IsIsinst (E expr, out E arg, out TypeNode type)
85 return VisitorForIsInst<V, E>.IsIsInst (expr, out type, out arg, this);
88 public bool IsUnaryExpression (E expr, out UnaryOperator op, out E arg)
90 return VisitorForIsUnaryExpression<V, E>.IsUnary (expr, out op, out arg, this);
93 public bool IsBinaryExpression (E expr, out BinaryOperator op, out E left, out E right)
95 return VisitorForIsBinaryExpression<V, E>.IsBinary (expr, out op, out left, out right, this);
98 public void AddFreeVariables (E expr, ISet<E> set)
100 VisitorForVariablesIn<V, E>.AddFreeVariables (expr, set, this);
103 public Sequence<PathElement> GetVariableAccessPath (E expr)
105 return ContextProvider.ValueContext.AccessPathList (ContextProvider.ExpressionContext.GetPC (expr), ContextProvider.ExpressionContext.Unrefine (expr), true, false);
108 public bool TryGetType (E expr, out TypeNode type)
110 FlatDomain<TypeNode> aType = ContextProvider.ExpressionContext.GetType (expr);
111 if (aType.IsNormal()) {
120 public bool TrySizeOfAsConstant (E expr, out int sizeAsConstant)
122 return TrySizeOf (expr, out sizeAsConstant);
125 private bool TrySizeOf (E expr, out int sizeAsConstant)
128 if (VisitorForSizeOf<V, E>.IsSizeOf (expr, out type, this)) {
129 int size = this.MetaDataProvider.TypeSize (type);
131 sizeAsConstant = size;
141 public FullExpressionDecoder (IMetaDataProvider metaDataProvider, IExpressionContextProvider<E, V> contextProvider)
143 ContextProvider = contextProvider;
144 this.MetaDataProvider = metaDataProvider;
145 this.VariableVisitor = new VisitorForVariable<V, E> ();
146 this.UnderlyingVariableVisitor = new VisitorForUnderlyingVariable<V, E> ();
147 this.UnaryExpressionVisitor = new VisitorForIsUnaryExpression<V, E> ();
148 this.BinaryExpressionVisitor = new VisitorForIsBinaryExpression<V, E> ();
149 this.VariablesInVisitor = new VisitorForVariablesIn<V, E> (contextProvider);
150 this.ValueOfVisitor = new VisitorForValueOf<V, E> ();
151 this.SizeOfVisitor = new VisitorForSizeOf<V, E> ();
152 this.IsInstVisitor = new VisitorForIsInst<V, E> ();
153 this.IsNullVisitor = new VisitorForIsNull<V, E> ();
156 public IExpressionContextProvider<E, V> ContextProvider { get; private set; }