1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Microsoft Public License. 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 Microsoft Public License, 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 Microsoft Public License.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
15 using System; using Microsoft;
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20 using System.Diagnostics;
22 using System.Dynamic.Utils;
24 using Microsoft.Scripting.Utils;
26 using System.Runtime.CompilerServices;
28 using Microsoft.Runtime.CompilerServices;
33 namespace System.Linq.Expressions {
35 namespace Microsoft.Linq.Expressions {
39 /// Represents creating a new array and possibly initializing the elements of the new array.
42 [DebuggerTypeProxy(typeof(Expression.NewArrayExpressionProxy))]
44 public class NewArrayExpression : Expression {
45 private readonly ReadOnlyCollection<Expression> _expressions;
46 private readonly Type _type;
48 internal NewArrayExpression(Type type, ReadOnlyCollection<Expression> expressions) {
49 _expressions = expressions;
53 internal static NewArrayExpression Make(ExpressionType nodeType, Type type, ReadOnlyCollection<Expression> expressions) {
54 if (nodeType == ExpressionType.NewArrayInit) {
55 return new NewArrayInitExpression(type, expressions);
57 return new NewArrayBoundsExpression(type, expressions);
62 /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
64 /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
65 public sealed override Type Type {
70 /// Gets the bounds of the array if the value of the <see cref="P:NodeType"/> property is NewArrayBounds, or the values to initialize the elements of the new array if the value of the <see cref="P:NodeType"/> property is NewArrayInit.
72 public ReadOnlyCollection<Expression> Expressions {
73 get { return _expressions; }
76 internal override Expression Accept(ExpressionVisitor visitor) {
77 return visitor.VisitNewArray(this);
81 internal sealed class NewArrayInitExpression : NewArrayExpression {
82 internal NewArrayInitExpression(Type type, ReadOnlyCollection<Expression> expressions)
83 : base(type, expressions) {
88 /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
90 /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
91 public sealed override ExpressionType NodeType {
92 get { return ExpressionType.NewArrayInit; }
96 internal sealed class NewArrayBoundsExpression : NewArrayExpression {
97 internal NewArrayBoundsExpression(Type type, ReadOnlyCollection<Expression> expressions)
98 : base(type, expressions) {
102 /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
104 /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
105 public sealed override ExpressionType NodeType {
106 get { return ExpressionType.NewArrayBounds; }
110 public partial class Expression {
116 /// Creates a new array expression of the specified type from the provided initializers.
118 /// <param name="type">A Type that represents the element type of the array.</param>
119 /// <param name="initializers">The expressions used to create the array elements.</param>
120 /// <returns>An instance of the <see cref="NewArrayExpression"/>.</returns>
121 public static NewArrayExpression NewArrayInit(Type type, params Expression[] initializers) {
122 return NewArrayInit(type, (IEnumerable<Expression>)initializers);
126 /// Creates a new array expression of the specified type from the provided initializers.
128 /// <param name="type">A Type that represents the element type of the array.</param>
129 /// <param name="initializers">The expressions used to create the array elements.</param>
130 /// <returns>An instance of the <see cref="NewArrayExpression"/>.</returns>
131 public static NewArrayExpression NewArrayInit(Type type, IEnumerable<Expression> initializers) {
132 ContractUtils.RequiresNotNull(type, "type");
133 ContractUtils.RequiresNotNull(initializers, "initializers");
134 if (type.Equals(typeof(void))) {
135 throw Error.ArgumentCannotBeOfTypeVoid();
138 ReadOnlyCollection<Expression> initializerList = initializers.ToReadOnly();
140 Expression[] newList = null;
141 for (int i = 0, n = initializerList.Count; i < n; i++) {
142 Expression expr = initializerList[i];
143 RequiresCanRead(expr, "initializers");
145 if (!TypeUtils.AreReferenceAssignable(type, expr.Type)) {
146 if (TypeUtils.IsSameOrSubclass(typeof(LambdaExpression), type) && type.IsAssignableFrom(expr.GetType())) {
147 expr = Expression.Quote(expr);
149 throw Error.ExpressionTypeCannotInitializeArrayType(expr.Type, type);
151 if (newList == null) {
152 newList = new Expression[initializerList.Count];
153 for (int j = 0; j < i; j++) {
154 newList[j] = initializerList[j];
158 if (newList != null) {
162 if (newList != null) {
163 initializerList = new TrueReadOnlyCollection<Expression>(newList);
166 return NewArrayExpression.Make(ExpressionType.NewArrayInit, type.MakeArrayType(), initializerList);
171 #region NewArrayBounds
175 /// Creates a <see cref="NewArrayExpression"/> that represents creating an array that has a specified rank.
177 /// <param name="type">A <see cref="Type"/> that represents the element type of the array.</param>
178 /// <param name="bounds">An array that contains Expression objects to use to populate the Expressions collection.</param>
179 /// <returns>A <see cref="NewArrayExpression"/> that has the <see cref="P:NodeType"/> property equal to type and the <see cref="P:Expressions"/> property set to the specified value.</returns>
180 public static NewArrayExpression NewArrayBounds(Type type, params Expression[] bounds) {
181 return NewArrayBounds(type, (IEnumerable<Expression>)bounds);
186 /// Creates a <see cref="NewArrayExpression"/> that represents creating an array that has a specified rank.
188 /// <param name="type">A <see cref="Type"/> that represents the element type of the array.</param>
189 /// <param name="bounds">An IEnumerable{T} that contains Expression objects to use to populate the Expressions collection.</param>
190 /// <returns>A <see cref="NewArrayExpression"/> that has the <see cref="P:NodeType"/> property equal to type and the <see cref="P:Expressions"/> property set to the specified value.</returns>
191 public static NewArrayExpression NewArrayBounds(Type type, IEnumerable<Expression> bounds) {
192 ContractUtils.RequiresNotNull(type, "type");
193 ContractUtils.RequiresNotNull(bounds, "bounds");
195 if (type.Equals(typeof(void))) {
196 throw Error.ArgumentCannotBeOfTypeVoid();
199 ReadOnlyCollection<Expression> boundsList = bounds.ToReadOnly();
201 int dimensions = boundsList.Count;
202 ContractUtils.Requires(dimensions > 0, "bounds", Strings.BoundsCannotBeLessThanOne);
204 for (int i = 0; i < dimensions; i++) {
205 Expression expr = boundsList[i];
206 RequiresCanRead(expr, "bounds");
207 if (!TypeUtils.IsInteger(expr.Type)) {
208 throw Error.ArgumentMustBeInteger();
213 if (dimensions == 1) {
214 //To get a vector, need call Type.MakeArrayType().
215 //Type.MakeArrayType(1) gives a non-vector array, which will cause type check error.
216 arrayType = type.MakeArrayType();
218 arrayType = type.MakeArrayType(dimensions);
221 return NewArrayExpression.Make(ExpressionType.NewArrayBounds, arrayType, bounds.ToReadOnly());