1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using System.Collections.Generic;
18 using System.Collections.ObjectModel;
19 using System.Diagnostics;
20 using System.Dynamic.Utils;
21 using System.Runtime.CompilerServices;
24 namespace Microsoft.Scripting.Ast {
26 namespace System.Linq.Expressions {
29 /// An expression that provides runtime read/write access to variables.
30 /// Needed to implement "eval" in some dynamic languages.
31 /// Evaluates to an instance of <see cref="IList{IStrongBox}" /> when executed.
34 [DebuggerTypeProxy(typeof(Expression.RuntimeVariablesExpressionProxy))]
36 public sealed class RuntimeVariablesExpression : Expression {
37 private readonly ReadOnlyCollection<ParameterExpression> _variables;
39 internal RuntimeVariablesExpression(ReadOnlyCollection<ParameterExpression> variables) {
40 _variables = variables;
44 /// Gets the static type of the expression that this <see cref="Expression" /> represents.
46 /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
47 public sealed override Type Type {
48 get { return typeof(IRuntimeVariables); }
52 /// Returns the node type of this Expression. Extension nodes should return
53 /// ExpressionType.Extension when overriding this method.
55 /// <returns>The <see cref="ExpressionType"/> of the expression.</returns>
56 public sealed override ExpressionType NodeType {
57 get { return ExpressionType.RuntimeVariables; }
61 /// The variables or parameters to which to provide runtime access.
63 public ReadOnlyCollection<ParameterExpression> Variables {
64 get { return _variables; }
68 /// Dispatches to the specific visit method for this node type.
70 protected internal override Expression Accept(ExpressionVisitor visitor) {
71 return visitor.VisitRuntimeVariables(this);
75 /// Creates a new expression that is like this one, but using the
76 /// supplied children. If all of the children are the same, it will
77 /// return this expression.
79 /// <param name="variables">The <see cref="Variables" /> property of the result.</param>
80 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
81 public RuntimeVariablesExpression Update(IEnumerable<ParameterExpression> variables) {
82 if (variables == Variables) {
85 return Expression.RuntimeVariables(variables);
89 public partial class Expression {
92 /// Creates an instance of <see cref="T:System.Linq.Expressions.RuntimeVariablesExpression" />.
94 /// <param name="variables">An array of <see cref="T:System.Linq.Expressions.ParameterExpression" /> objects to use to populate the <see cref="P:System.Linq.Expressions.RuntimeVariablesExpression.Variables" /> collection.</param>
95 /// <returns>An instance of <see cref="T:System.Linq.Expressions.RuntimeVariablesExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.RuntimeVariables" /> and the <see cref="P:System.Linq.Expressions.RuntimeVariablesExpression.Variables" /> property set to the specified value.</returns>
96 public static RuntimeVariablesExpression RuntimeVariables(params ParameterExpression[] variables) {
97 return RuntimeVariables((IEnumerable<ParameterExpression>)variables);
101 /// Creates an instance of <see cref="T:System.Linq.Expressions.RuntimeVariablesExpression" />.
103 /// <param name="variables">A collection of <see cref="T:System.Linq.Expressions.ParameterExpression" /> objects to use to populate the <see cref="P:System.Linq.Expressions.RuntimeVariablesExpression.Variables" /> collection.</param>
104 /// <returns>An instance of <see cref="T:System.Linq.Expressions.RuntimeVariablesExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.RuntimeVariables" /> and the <see cref="P:System.Linq.Expressions.RuntimeVariablesExpression.Variables" /> property set to the specified value.</returns>
105 public static RuntimeVariablesExpression RuntimeVariables(IEnumerable<ParameterExpression> variables) {
106 ContractUtils.RequiresNotNull(variables, "variables");
108 var vars = variables.ToReadOnly();
109 for (int i = 0; i < vars.Count; i++) {
110 Expression v = vars[i];
112 throw new ArgumentNullException("variables[" + i + "]");
116 return new RuntimeVariablesExpression(vars);