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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
//
// PathElement`1.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2011 Alexander Chebaturkin
//
// 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.CodeContracts.Static.AST;
using Mono.CodeContracts.Static.DataStructures;
using Mono.CodeContracts.Static.Providers;
namespace Mono.CodeContracts.Static.Analysis.HeapAnalysis.Paths {
class PathElement<T> : PathElementBase {
public readonly string Description;
public readonly T Element;
protected string castTo;
protected bool isManagedPointer;
protected bool isStatic;
protected bool isUnmanagedPointer;
#region Overrides of PathElement
public override bool IsStatic
{
get { return this.isStatic; }
}
public override bool IsUnmanagedPointer
{
get { return this.isUnmanagedPointer; }
}
public override bool IsManagedPointer
{
get { return this.isManagedPointer; }
}
public override bool IsParameterRef
{
get { return typeof (T) == typeof (Parameter); }
}
public override string CastTo
{
get { return this.castTo; }
}
public override bool IsAddressOf
{
get { return true; }
}
public override bool TryField (out Field f)
{
if (typeof (T) == typeof (Field)) {
f = (Field) (object) this.Element;
return true;
}
f = default(Field);
return false;
}
public override bool TryGetResultType (out TypeNode type)
{
type = ResultType;
return true;
}
public override TResult Decode<TData, TResult, TVisitor, TLabel> (TLabel label, TVisitor visitor, TData data)
{
if (typeof (T) == typeof (Field)) {
var field = (Field) (object) this.Element;
if (this.isStatic)
return visitor.LoadStaticFieldAddress (label, field, Dummy.Value, data);
return visitor.LoadFieldAddress (label, field, Dummy.Value, Dummy.Value, data);
}
if (typeof (T) == typeof (Local)) {
var local = (Local) (object) this.Element;
return visitor.LoadLocalAddress (label, local, Dummy.Value, data);
}
if (typeof (T) == typeof (Method)) {
var method = (Method) (object) this.Element;
bool isVirtualMethod = this.Func.IsVirtualMethod;
return visitor.Call (label, method, isVirtualMethod, Indexable<TypeNode>.Empty, Dummy.Value, Indexable<Dummy>.Empty, data);
}
if (typeof (T) == typeof (Parameter)) {
var parameter = (Parameter) (object) this.Element;
return visitor.LoadArgAddress (label, parameter, false, Dummy.Value, data);
}
throw new InvalidOperationException ("Field, Local, Method or Parameter expected");
}
public override string ToString ()
{
return this.Description;
}
#endregion
public PathElement (T element, string description, SymFunction c) : base (c)
{
this.Element = element;
this.Description = description;
this.isStatic = false;
this.isUnmanagedPointer = false;
this.isManagedPointer = false;
}
public TypeNode ResultType { get; protected set; }
public virtual bool IsCallerVisible ()
{
return (typeof (T) == typeof (Parameter));
}
#region Overrides of PathElementBase
public override bool TrySetType (TypeNode expectedType, IMetaDataProvider metaDataProvider, out TypeNode resultType)
{
if (typeof (T) == typeof (Parameter)) {
var p = (Parameter) (object) this.Element;
TypeNode type = metaDataProvider.ParameterType (p);
this.isManagedPointer = metaDataProvider.IsManagedPointer (type);
ResultType = resultType = metaDataProvider.ManagedPointer (type);
return true;
}
if (typeof (T) == typeof (Field)) {
var f = (Field) (object) this.Element;
TypeNode type = metaDataProvider.FieldType (f);
this.isStatic = metaDataProvider.IsStatic (f);
this.isManagedPointer = metaDataProvider.IsManagedPointer (type);
ResultType = resultType = metaDataProvider.ManagedPointer (type);
TypeNode declaringType = metaDataProvider.DeclaringType (f);
if (metaDataProvider.IsManagedPointer (expectedType))
expectedType = metaDataProvider.ElementType (expectedType);
expectedType = metaDataProvider.Unspecialized (expectedType);
if (!metaDataProvider.IsStatic (f) && declaringType.Equals (expectedType) &&
(!metaDataProvider.DerivesFrom (expectedType, declaringType) ||
!metaDataProvider.IsProtected (f) && !metaDataProvider.IsPublic (f)))
this.castTo = metaDataProvider.FullName (declaringType);
return true;
}
if (typeof (T) == typeof (Local)) {
var local = (Local) (object) this.Element;
TypeNode type = metaDataProvider.LocalType (local);
this.isManagedPointer = metaDataProvider.IsManagedPointer (type);
ResultType = resultType = metaDataProvider.ManagedPointer (type);
return true;
}
if (typeof (T) == typeof (Method)) {
var method = (Method) (object) this.Element;
ResultType = resultType = !IsAddressOf
? metaDataProvider.ReturnType (method)
: metaDataProvider.ManagedPointer (metaDataProvider.ReturnType (method));
if (metaDataProvider.IsManagedPointer (expectedType))
expectedType = metaDataProvider.ElementType (expectedType);
expectedType = metaDataProvider.Unspecialized (expectedType);
TypeNode declaringType = metaDataProvider.DeclaringType (method);
if (!metaDataProvider.IsStatic (method) && declaringType.Equals (expectedType) &&
(!metaDataProvider.DerivesFrom (expectedType, declaringType)
|| !metaDataProvider.IsProtected (method) && !metaDataProvider.IsPublic (method)))
this.castTo = metaDataProvider.FullName (declaringType);
return true;
}
ResultType = resultType = default(TypeNode);
return false;
}
#endregion
}
}
|