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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if CLR2
using Microsoft.Scripting.Ast;
#else
using System.Linq.Expressions;
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Runtime.CompilerServices;
namespace System.Dynamic {
/// <summary>
/// Represents a set of binding restrictions on the <see cref="DynamicMetaObject"/>under which the dynamic binding is valid.
/// </summary>
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(BindingRestrictionsProxy)), DebuggerDisplay("{DebugView}")]
#endif
public abstract class BindingRestrictions {
/// <summary>
/// Represents an empty set of binding restrictions. This field is read only.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly BindingRestrictions Empty = new CustomRestriction(Expression.Constant(true));
private const int TypeRestrictionHash = 0x10000000;
private const int InstanceRestrictionHash = 0x20000000;
private const int CustomRestrictionHash = 0x40000000;
private BindingRestrictions() {
}
// Overridden by specialized subclasses
internal abstract Expression GetExpression();
/// <summary>
/// Merges the set of binding restrictions with the current binding restrictions.
/// </summary>
/// <param name="restrictions">The set of restrictions with which to merge the current binding restrictions.</param>
/// <returns>The new set of binding restrictions.</returns>
public BindingRestrictions Merge(BindingRestrictions restrictions) {
ContractUtils.RequiresNotNull(restrictions, "restrictions");
if (this == Empty) {
return restrictions;
}
if (restrictions == Empty) {
return this;
}
return new MergedRestriction(this, restrictions);
}
/// <summary>
/// Creates the binding restriction that check the expression for runtime type identity.
/// </summary>
/// <param name="expression">The expression to test.</param>
/// <param name="type">The exact type to test.</param>
/// <returns>The new binding restrictions.</returns>
public static BindingRestrictions GetTypeRestriction(Expression expression, Type type) {
ContractUtils.RequiresNotNull(expression, "expression");
ContractUtils.RequiresNotNull(type, "type");
return new TypeRestriction(expression, type);
}
/// <summary>
/// The method takes a DynamicMetaObject, and returns an instance restriction for testing null if the object
/// holds a null value, otherwise returns a type restriction.
/// </summary>
internal static BindingRestrictions GetTypeRestriction(DynamicMetaObject obj) {
if (obj.Value == null && obj.HasValue) {
return BindingRestrictions.GetInstanceRestriction(obj.Expression, null);
} else {
return BindingRestrictions.GetTypeRestriction(obj.Expression, obj.LimitType);
}
}
/// <summary>
/// Creates the binding restriction that checks the expression for object instance identity.
/// </summary>
/// <param name="expression">The expression to test.</param>
/// <param name="instance">The exact object instance to test.</param>
/// <returns>The new binding restrictions.</returns>
public static BindingRestrictions GetInstanceRestriction(Expression expression, object instance) {
ContractUtils.RequiresNotNull(expression, "expression");
return new InstanceRestriction(expression, instance);
}
/// <summary>
/// Creates the binding restriction that checks the expression for arbitrary immutable properties.
/// </summary>
/// <param name="expression">The expression expression the restrictions.</param>
/// <returns>The new binding restrictions.</returns>
/// <remarks>
/// By convention, the general restrictions created by this method must only test
/// immutable object properties.
/// </remarks>
public static BindingRestrictions GetExpressionRestriction(Expression expression) {
ContractUtils.RequiresNotNull(expression, "expression");
ContractUtils.Requires(expression.Type == typeof(bool), "expression");
return new CustomRestriction(expression);
}
/// <summary>
/// Combines binding restrictions from the list of <see cref="DynamicMetaObject"/> instances into one set of restrictions.
/// </summary>
/// <param name="contributingObjects">The list of <see cref="DynamicMetaObject"/> instances from which to combine restrictions.</param>
/// <returns>The new set of binding restrictions.</returns>
public static BindingRestrictions Combine(IList<DynamicMetaObject> contributingObjects) {
BindingRestrictions res = BindingRestrictions.Empty;
if (contributingObjects != null) {
foreach (DynamicMetaObject mo in contributingObjects) {
if (mo != null) {
res = res.Merge(mo.Restrictions);
}
}
}
return res;
}
/// <summary>
/// Builds a balanced tree of AndAlso nodes.
/// We do this so the compiler won't stack overflow if we have many
/// restrictions.
/// </summary>
private sealed class TestBuilder {
private readonly Set<BindingRestrictions> _unique = new Set<BindingRestrictions>();
private readonly Stack<AndNode> _tests = new Stack<AndNode>();
private struct AndNode {
internal int Depth;
internal Expression Node;
}
internal void Append(BindingRestrictions restrictions) {
if (_unique.Contains(restrictions)) {
return;
}
_unique.Add(restrictions);
Push(restrictions.GetExpression(), 0);
}
internal Expression ToExpression() {
Expression result = _tests.Pop().Node;
while (_tests.Count > 0) {
result = Expression.AndAlso(_tests.Pop().Node, result);
}
return result;
}
private void Push(Expression node, int depth) {
while (_tests.Count > 0 && _tests.Peek().Depth == depth) {
node = Expression.AndAlso(_tests.Pop().Node, node);
depth++;
}
_tests.Push(new AndNode { Node = node, Depth = depth });
}
}
/// <summary>
/// Creates the <see cref="Expression"/> representing the binding restrictions.
/// </summary>
/// <returns>The expression tree representing the restrictions.</returns>
public Expression ToExpression() {
// We could optimize this better, e.g. common subexpression elimination
// But for now, it's good enough.
if (this == Empty) {
return Expression.Constant(true);
}
var testBuilder = new TestBuilder();
// Visit the tree, left to right.
// Use an explicit stack so we don't stack overflow.
//
// Left-most node is on top of the stack, so we always expand the
// left most node each iteration.
var stack = new Stack<BindingRestrictions>();
stack.Push(this);
do {
var top = stack.Pop();
var m = top as MergedRestriction;
if (m != null) {
stack.Push(m.Right);
stack.Push(m.Left);
} else {
testBuilder.Append(top);
}
} while (stack.Count > 0);
return testBuilder.ToExpression();
}
private sealed class MergedRestriction : BindingRestrictions {
internal readonly BindingRestrictions Left;
internal readonly BindingRestrictions Right;
internal MergedRestriction(BindingRestrictions left, BindingRestrictions right) {
Left = left;
Right = right;
}
internal override Expression GetExpression() {
throw ContractUtils.Unreachable;
}
}
private sealed class CustomRestriction : BindingRestrictions {
private readonly Expression _expression;
internal CustomRestriction(Expression expression) {
_expression = expression;
}
public override bool Equals(object obj) {
var other = obj as CustomRestriction;
return other != null && other._expression == _expression;
}
public override int GetHashCode() {
return CustomRestrictionHash ^ _expression.GetHashCode();
}
internal override Expression GetExpression() {
return _expression;
}
}
private sealed class TypeRestriction : BindingRestrictions {
private readonly Expression _expression;
private readonly Type _type;
internal TypeRestriction(Expression parameter, Type type) {
_expression = parameter;
_type = type;
}
public override bool Equals(object obj) {
var other = obj as TypeRestriction;
return other != null && TypeUtils.AreEquivalent(other._type, _type) && other._expression == _expression;
}
public override int GetHashCode() {
return TypeRestrictionHash ^ _expression.GetHashCode() ^ _type.GetHashCode();
}
internal override Expression GetExpression() {
return Expression.TypeEqual(_expression, _type);
}
}
private sealed class InstanceRestriction : BindingRestrictions {
private readonly Expression _expression;
private readonly object _instance;
internal InstanceRestriction(Expression parameter, object instance) {
_expression = parameter;
_instance = instance;
}
public override bool Equals(object obj) {
var other = obj as InstanceRestriction;
return other != null && other._instance == _instance && other._expression == _expression;
}
public override int GetHashCode() {
return InstanceRestrictionHash ^ RuntimeHelpers.GetHashCode(_instance) ^ _expression.GetHashCode();
}
internal override Expression GetExpression() {
if (_instance == null) {
return Expression.Equal(
Expression.Convert(_expression, typeof(object)),
Expression.Constant(null)
);
}
ParameterExpression temp = Expression.Parameter(typeof(object), null);
return Expression.Block(
new[] { temp },
Expression.Assign(
temp,
Expression.Property(
Expression.Constant(new WeakReference(_instance)),
typeof(WeakReference).GetProperty("Target")
)
),
Expression.AndAlso(
//check that WeekReference was not collected.
Expression.NotEqual(temp, Expression.Constant(null)),
Expression.Equal(
Expression.Convert(_expression, typeof(object)),
temp
)
)
);
}
}
private string DebugView {
get { return ToExpression().ToString(); }
}
private sealed class BindingRestrictionsProxy {
private readonly BindingRestrictions _node;
public BindingRestrictionsProxy(BindingRestrictions node) {
_node = node;
}
public bool IsEmpty {
get { return _node == Empty; }
}
public Expression Test {
get { return _node.ToExpression(); }
}
public BindingRestrictions[] Restrictions {
get {
var restrictions = new List<BindingRestrictions>();
// Visit the tree, left to right
//
// Left-most node is on top of the stack, so we always expand the
// left most node each iteration.
var stack = new Stack<BindingRestrictions>();
stack.Push(_node);
do {
var top = stack.Pop();
var m = top as MergedRestriction;
if (m != null) {
stack.Push(m.Right);
stack.Push(m.Left);
} else {
restrictions.Add(top);
}
} while (stack.Count > 0);
return restrictions.ToArray();
}
}
public override string ToString() {
// To prevent fxcop warning about this field
return _node.DebugView;
}
}
}
}
|