PageRenderTime 45ms CodeModel.GetById 36ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/ListInitExpression.cs

https://github.com/Sciumo/mono
C# | 237 lines | 110 code | 22 blank | 105 comment | 15 complexity | 53168dd9ec9c62026f220ab0244e9bc5 MD5 | raw file
  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
 16using System;
 17using System.Collections.Generic;
 18using System.Collections.ObjectModel;
 19using System.Diagnostics;
 20using System.Dynamic.Utils;
 21using System.Reflection;
 22using System.Runtime.CompilerServices;
 23
 24#if SILVERLIGHT
 25using System.Core;
 26#endif
 27
 28#if CLR2
 29namespace Microsoft.Scripting.Ast {
 30#else
 31namespace System.Linq.Expressions {
 32#endif
 33    /// <summary>
 34    /// Represents a constructor call that has a collection initializer.
 35    /// </summary>
 36    /// <remarks>
 37    /// Use the <see cref="M:ListInit"/> factory methods to create a ListInitExpression. 
 38    /// The value of the NodeType property of a ListInitExpression is ListInit. 
 39    /// </remarks>
 40#if !SILVERLIGHT
 41    [DebuggerTypeProxy(typeof(Expression.ListInitExpressionProxy))]
 42#endif
 43    public sealed class ListInitExpression : Expression {
 44        private readonly NewExpression _newExpression;
 45        private readonly ReadOnlyCollection<ElementInit> _initializers;
 46
 47        internal ListInitExpression(NewExpression newExpression, ReadOnlyCollection<ElementInit> initializers) {
 48            _newExpression = newExpression;
 49            _initializers = initializers;
 50        }
 51
 52        /// <summary>
 53        /// Returns the node type of this <see cref="Expression"/>. (Inherited from <see cref="Expression" />.)
 54        /// </summary>
 55        /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
 56        public sealed override ExpressionType NodeType {
 57            get { return ExpressionType.ListInit; }
 58        }
 59
 60        /// <summary>
 61        /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
 62        /// </summary>
 63        /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
 64        public sealed override Type Type {
 65            get { return _newExpression.Type; }
 66        }
 67
 68        /// <summary>
 69        /// Gets a value that indicates whether the expression tree node can be reduced. 
 70        /// </summary>
 71        public override bool CanReduce {
 72            get {
 73                return true;
 74            }
 75        }
 76
 77        /// <summary>
 78        /// Gets the expression that contains a call to the constructor of a collection type. 
 79        /// </summary>
 80        public NewExpression NewExpression {
 81            get { return _newExpression; }
 82        }
 83
 84        /// <summary>
 85        /// Gets the element initializers that are used to initialize a collection. 
 86        /// </summary>
 87        public ReadOnlyCollection<ElementInit> Initializers {
 88            get { return _initializers; }
 89        }
 90
 91        /// <summary>
 92        /// Dispatches to the specific visit method for this node type.
 93        /// </summary>
 94        protected internal override Expression Accept(ExpressionVisitor visitor) {
 95            return visitor.VisitListInit(this);
 96        }
 97
 98        /// <summary>
 99        /// Reduces the binary expression node to a simpler expression. 
100        /// If CanReduce returns true, this should return a valid expression.
101        /// This method is allowed to return another node which itself 
102        /// must be reduced.
103        /// </summary>
104        /// <returns>The reduced expression.</returns>
105        public override Expression Reduce() {
106            return MemberInitExpression.ReduceListInit(_newExpression, _initializers, true);
107        }
108
109        /// <summary>
110        /// Creates a new expression that is like this one, but using the
111        /// supplied children. If all of the children are the same, it will
112        /// return this expression.
113        /// </summary>
114        /// <param name="newExpression">The <see cref="NewExpression" /> property of the result.</param>
115        /// <param name="initializers">The <see cref="Initializers" /> property of the result.</param>
116        /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
117        public ListInitExpression Update(NewExpression newExpression, IEnumerable<ElementInit> initializers) {
118            if (newExpression == NewExpression && initializers == Initializers) {
119                return this;
120            }
121            return Expression.ListInit(newExpression, initializers);
122        }
123    }
124
125
126    public partial class Expression {
127        /// <summary>
128        /// Creates a <see cref="ListInitExpression"/> that uses a method named "Add" to add elements to a collection.
129        /// </summary>
130        /// <param name="newExpression">A <see cref="NewExpression"/> to set the <see cref="P:ListInitExpression.NewExpression"/> property equal to.</param>
131        /// <param name="initializers">An array of <see cref="Expression"/> objects to use to populate the <see cref="ListInitExpression.Initializers"/> collection.</param>
132        /// <returns>A <see cref="ListInitExpression"/> that has the <see cref="P:ListInitExpression.NodeType"/> property equal to ListInit and the <see cref="P:ListInitExpression.NewExpression"/> property set to the specified value.</returns>
133        public static ListInitExpression ListInit(NewExpression newExpression, params Expression[] initializers) {
134            ContractUtils.RequiresNotNull(newExpression, "newExpression");
135            ContractUtils.RequiresNotNull(initializers, "initializers");
136            return ListInit(newExpression, initializers as IEnumerable<Expression>);
137        }
138
139        /// <summary>
140        /// Creates a <see cref="ListInitExpression"/> that uses a method named "Add" to add elements to a collection.
141        /// </summary>
142        /// <param name="newExpression">A <see cref="NewExpression"/> to set the <see cref="P:ListInitExpression.NewExpression"/> property equal to.</param>
143        /// <param name="initializers">An <see cref="IEnumerable{T}"/> that contains <see cref="M:ElementInit"/> objects to use to populate the <see cref="ListInitExpression.Initializers"/> collection.</param>
144        /// <returns>A <see cref="ListInitExpression"/> that has the <see cref="P:ListInitExpression.NodeType"/> property equal to ListInit and the <see cref="P:ListInitExpression.NewExpression"/> property set to the specified value.</returns>
145        public static ListInitExpression ListInit(NewExpression newExpression, IEnumerable<Expression> initializers) {
146            ContractUtils.RequiresNotNull(newExpression, "newExpression");
147            ContractUtils.RequiresNotNull(initializers, "initializers");
148
149            var initializerlist = initializers.ToReadOnly();
150            if (initializerlist.Count == 0) {
151                throw Error.ListInitializerWithZeroMembers();
152            }
153
154            MethodInfo addMethod = FindMethod(newExpression.Type, "Add", null, new Expression[] { initializerlist[0] }, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
155            return ListInit(newExpression, addMethod, initializers);
156        }
157
158        /// <summary>
159        /// Creates a <see cref="ListInitExpression"/> that uses a specified method to add elements to a collection. 
160        /// </summary>
161        /// <param name="newExpression">A <see cref="NewExpression"/> to set the <see cref="P:ListInitExpression.NewExpression"/> property equal to.</param>
162        /// <param name="addMethod">A <see cref="MethodInfo"/> that represents an instance method named "Add" (case insensitive), that adds an element to a collection. </param>
163        /// <param name="initializers">An array of <see cref="Expression"/> objects to use to populate the <see cref="ListInitExpression.Initializers"/> collection.</param>
164        /// <returns>A <see cref="ListInitExpression"/> that has the <see cref="P:ListInitExpression.NodeType"/> property equal to ListInit and the <see cref="P:ListInitExpression.NewExpression"/> property set to the specified value.</returns>
165        public static ListInitExpression ListInit(NewExpression newExpression, MethodInfo addMethod, params Expression[] initializers) {
166            if (addMethod == null) {
167                return ListInit(newExpression, initializers as IEnumerable<Expression>);
168            }
169            ContractUtils.RequiresNotNull(newExpression, "newExpression");
170            ContractUtils.RequiresNotNull(initializers, "initializers");
171            return ListInit(newExpression, addMethod, initializers as IEnumerable<Expression>);
172        }
173
174        /// <summary>
175        /// Creates a <see cref="ListInitExpression"/> that uses a specified method to add elements to a collection. 
176        /// </summary>
177        /// <param name="newExpression">A <see cref="NewExpression"/> to set the <see cref="P:ListInitExpression.NewExpression"/> property equal to.</param>
178        /// <param name="addMethod">A <see cref="MethodInfo"/> that represents an instance method named "Add" (case insensitive), that adds an element to a collection. </param>
179        /// <param name="initializers">An <see cref="IEnumerable{T}"/> that contains <see cref="Expression"/> objects to use to populate the Initializers collection.</param>
180        /// <returns>A <see cref="ListInitExpression"/> that has the <see cref="P:ListInitExpression.NodeType"/> property equal to ListInit and the <see cref="P:ListInitExpression.NewExpression"/> property set to the specified value.</returns>
181        public static ListInitExpression ListInit(NewExpression newExpression, MethodInfo addMethod, IEnumerable<Expression> initializers) {
182            if (addMethod == null) {
183                return ListInit(newExpression, initializers);
184            }
185            ContractUtils.RequiresNotNull(newExpression, "newExpression");
186            ContractUtils.RequiresNotNull(initializers, "initializers");
187
188            var initializerlist = initializers.ToReadOnly();
189            if (initializerlist.Count == 0) {
190                throw Error.ListInitializerWithZeroMembers();
191            }
192            ElementInit[] initList = new ElementInit[initializerlist.Count];
193            for (int i = 0; i < initializerlist.Count; i++) {
194                initList[i] = ElementInit(addMethod, initializerlist[i]);
195            }
196            return ListInit(newExpression, new TrueReadOnlyCollection<ElementInit>(initList));
197        }
198
199        /// <summary>
200        /// Creates a <see cref="ListInitExpression"/> that uses specified <see cref="M:ElementInit"/> objects to initialize a collection. 
201        /// </summary>
202        /// <param name="newExpression">A <see cref="NewExpression"/> to set the <see cref="P:ListInitExpression.NewExpression"/> property equal to.</param>
203        /// <param name="initializers">An array that contains <see cref="M:ElementInit"/> objects to use to populate the <see cref="ListInitExpression.Initializers"/> collection.</param>
204        /// <returns>
205        /// A <see cref="ListInitExpression"/> that has the <see cref="P:Expressions.NodeType"/> property equal to ListInit 
206        /// and the <see cref="P:ListInitExpression.NewExpression"/> and <see cref="P:ListInitExpression.Initializers"/> properties set to the specified values.
207        /// </returns>
208        /// <remarks>
209        /// The <see cref="P:Expressions.Type"/> property of <paramref name="newExpression"/> must represent a type that implements <see cref="System.Collections.IEnumerable"/>. 
210        /// The <see cref="P:Expressions.Type"/> property of the resulting <see cref="ListInitExpression"/> is equal to newExpression.Type. 
211        /// </remarks>
212        public static ListInitExpression ListInit(NewExpression newExpression, params ElementInit[] initializers) {
213            return ListInit(newExpression, (IEnumerable<ElementInit>)initializers);
214        }
215
216        /// <summary>
217        /// Creates a <see cref="ListInitExpression"/> that uses specified <see cref="M:ElementInit"/> objects to initialize a collection. 
218        /// </summary>
219        /// <param name="newExpression">A <see cref="NewExpression"/> to set the <see cref="P:ListInitExpression.NewExpression"/> property equal to.</param>
220        /// <param name="initializers">An <see cref="IEnumerable{T}"/> that contains <see cref="M:ElementInit"/> objects to use to populate the <see cref="ListInitExpression.Initializers"/> collection.</param>
221        /// <returns>An <see cref="IEnumerable{T}"/> that contains <see cref="M:ElementInit"/> objects to use to populate the <see cref="ListInitExpression.Initializers"/> collection.</returns>
222        /// <remarks>
223        /// The <see cref="P:Expressions.Type"/> property of <paramref name="newExpression"/> must represent a type that implements <see cref="System.Collections.IEnumerable"/>. 
224        /// The <see cref="P:Expressions.Type"/> property of the resulting <see cref="ListInitExpression"/> is equal to newExpression.Type. 
225        /// </remarks>
226        public static ListInitExpression ListInit(NewExpression newExpression, IEnumerable<ElementInit> initializers) {
227            ContractUtils.RequiresNotNull(newExpression, "newExpression");
228            ContractUtils.RequiresNotNull(initializers, "initializers");
229            var initializerlist = initializers.ToReadOnly();
230            if (initializerlist.Count == 0) {
231                throw Error.ListInitializerWithZeroMembers();
232            }
233            ValidateListInitArgs(newExpression.Type, initializerlist);
234            return new ListInitExpression(newExpression, initializerlist);
235        }
236    }
237}