BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / RuntimeVariablesExpression.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
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.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 using System;
17 using System.Collections.Generic;
18 using System.Collections.ObjectModel;
19 using System.Diagnostics;
20 using System.Dynamic.Utils;
21 using System.Runtime.CompilerServices;
22
23 #if CLR2
24 namespace Microsoft.Scripting.Ast {
25 #else
26 namespace System.Linq.Expressions {
27 #endif
28     /// <summary>
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.
32     /// </summary>
33 #if !SILVERLIGHT
34     [DebuggerTypeProxy(typeof(Expression.RuntimeVariablesExpressionProxy))]
35 #endif
36     public sealed class RuntimeVariablesExpression : Expression {
37         private readonly ReadOnlyCollection<ParameterExpression> _variables;
38
39         internal RuntimeVariablesExpression(ReadOnlyCollection<ParameterExpression> variables) {
40             _variables = variables;
41         }
42
43         /// <summary>
44         /// Gets the static type of the expression that this <see cref="Expression" /> represents.
45         /// </summary>
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); }
49         }
50
51         /// <summary>
52         /// Returns the node type of this Expression. Extension nodes should return
53         /// ExpressionType.Extension when overriding this method.
54         /// </summary>
55         /// <returns>The <see cref="ExpressionType"/> of the expression.</returns>
56         public sealed override ExpressionType NodeType {
57             get { return ExpressionType.RuntimeVariables; }
58         }
59
60         /// <summary>
61         /// The variables or parameters to which to provide runtime access.
62         /// </summary>
63         public ReadOnlyCollection<ParameterExpression> Variables {
64             get { return _variables; }
65         }
66
67         /// <summary>
68         /// Dispatches to the specific visit method for this node type.
69         /// </summary>
70         protected internal override Expression Accept(ExpressionVisitor visitor) {
71             return visitor.VisitRuntimeVariables(this);
72         }
73
74         /// <summary>
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.
78         /// </summary>
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) {
83                 return this;
84             }
85             return Expression.RuntimeVariables(variables);
86         }
87     }
88
89     public partial class Expression {
90
91         /// <summary>
92         /// Creates an instance of <see cref="T:System.Linq.Expressions.RuntimeVariablesExpression" />.
93         /// </summary>
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);
98         }
99
100         /// <summary>
101         /// Creates an instance of <see cref="T:System.Linq.Expressions.RuntimeVariablesExpression" />.
102         /// </summary>
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");
107
108             var vars = variables.ToReadOnly();
109             for (int i = 0; i < vars.Count; i++) {
110                 Expression v = vars[i];
111                 if (v == null) {
112                     throw new ArgumentNullException("variables[" + i + "]");
113                 }
114             }
115
116             return new RuntimeVariablesExpression(vars);
117         }
118     }
119 }