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
|
/* ****************************************************************************
*
* 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.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Runtime.CompilerServices;
#if SILVERLIGHT
using System.Core;
#endif
#if CLR2
namespace Microsoft.Scripting.Ast {
#else
namespace System.Linq.Expressions {
#endif
/// <summary>
/// Represents creating a new array and possibly initializing the elements of the new array.
/// </summary>
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.NewArrayExpressionProxy))]
#endif
public class NewArrayExpression : Expression {
private readonly ReadOnlyCollection<Expression> _expressions;
private readonly Type _type;
internal NewArrayExpression(Type type, ReadOnlyCollection<Expression> expressions) {
_expressions = expressions;
_type = type;
}
internal static NewArrayExpression Make(ExpressionType nodeType, Type type, ReadOnlyCollection<Expression> expressions) {
if (nodeType == ExpressionType.NewArrayInit) {
return new NewArrayInitExpression(type, expressions);
} else {
return new NewArrayBoundsExpression(type, expressions);
}
}
/// <summary>
/// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
/// </summary>
/// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
public sealed override Type Type {
get { return _type; }
}
/// <summary>
/// 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.
/// </summary>
public ReadOnlyCollection<Expression> Expressions {
get { return _expressions; }
}
/// <summary>
/// Dispatches to the specific visit method for this node type.
/// </summary>
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitNewArray(this);
}
/// <summary>
/// Creates a new expression that is like this one, but using the
/// supplied children. If all of the children are the same, it will
/// return this expression.
/// </summary>
/// <param name="expressions">The <see cref="Expressions" /> property of the result.</param>
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public NewArrayExpression Update(IEnumerable<Expression> expressions) {
if (expressions == Expressions) {
return this;
}
if (NodeType == ExpressionType.NewArrayInit) {
return Expression.NewArrayInit(Type.GetElementType(), expressions);
}
return Expression.NewArrayBounds(Type.GetElementType(), expressions);
}
}
internal sealed class NewArrayInitExpression : NewArrayExpression {
internal NewArrayInitExpression(Type type, ReadOnlyCollection<Expression> expressions)
: base(type, expressions) {
}
/// <summary>
/// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
/// </summary>
/// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
public sealed override ExpressionType NodeType {
get { return ExpressionType.NewArrayInit; }
}
}
internal sealed class NewArrayBoundsExpression : NewArrayExpression {
internal NewArrayBoundsExpression(Type type, ReadOnlyCollection<Expression> expressions)
: base(type, expressions) {
}
/// <summary>
/// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
/// </summary>
/// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
public sealed override ExpressionType NodeType {
get { return ExpressionType.NewArrayBounds; }
}
}
public partial class Expression {
#region NewArrayInit
/// <summary>
/// Creates a new array expression of the specified type from the provided initializers.
/// </summary>
/// <param name="type">A Type that represents the element type of the array.</param>
/// <param name="initializers">The expressions used to create the array elements.</param>
/// <returns>An instance of the <see cref="NewArrayExpression"/>.</returns>
public static NewArrayExpression NewArrayInit(Type type, params Expression[] initializers) {
return NewArrayInit(type, (IEnumerable<Expression>)initializers);
}
/// <summary>
/// Creates a new array expression of the specified type from the provided initializers.
/// </summary>
/// <param name="type">A Type that represents the element type of the array.</param>
/// <param name="initializers">The expressions used to create the array elements.</param>
/// <returns>An instance of the <see cref="NewArrayExpression"/>.</returns>
public static NewArrayExpression NewArrayInit(Type type, IEnumerable<Expression> initializers) {
ContractUtils.RequiresNotNull(type, "type");
ContractUtils.RequiresNotNull(initializers, "initializers");
if (type.Equals(typeof(void))) {
throw Error.ArgumentCannotBeOfTypeVoid();
}
ReadOnlyCollection<Expression> initializerList = initializers.ToReadOnly();
Expression[] newList = null;
for (int i = 0, n = initializerList.Count; i < n; i++) {
Expression expr = initializerList[i];
RequiresCanRead(expr, "initializers");
if (!TypeUtils.AreReferenceAssignable(type, expr.Type)) {
if (!TryQuote(type, ref expr)){
throw Error.ExpressionTypeCannotInitializeArrayType(expr.Type, type);
}
if (newList == null) {
newList = new Expression[initializerList.Count];
for (int j = 0; j < i; j++) {
newList[j] = initializerList[j];
}
}
}
if (newList != null) {
newList[i] = expr;
}
}
if (newList != null) {
initializerList = new TrueReadOnlyCollection<Expression>(newList);
}
return NewArrayExpression.Make(ExpressionType.NewArrayInit, type.MakeArrayType(), initializerList);
}
#endregion
#region NewArrayBounds
/// <summary>
/// Creates a <see cref="NewArrayExpression"/> that represents creating an array that has a specified rank.
/// </summary>
/// <param name="type">A <see cref="Type"/> that represents the element type of the array.</param>
/// <param name="bounds">An array that contains Expression objects to use to populate the Expressions collection.</param>
/// <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>
public static NewArrayExpression NewArrayBounds(Type type, params Expression[] bounds) {
return NewArrayBounds(type, (IEnumerable<Expression>)bounds);
}
/// <summary>
/// Creates a <see cref="NewArrayExpression"/> that represents creating an array that has a specified rank.
/// </summary>
/// <param name="type">A <see cref="Type"/> that represents the element type of the array.</param>
/// <param name="bounds">An IEnumerable{T} that contains Expression objects to use to populate the Expressions collection.</param>
/// <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>
public static NewArrayExpression NewArrayBounds(Type type, IEnumerable<Expression> bounds) {
ContractUtils.RequiresNotNull(type, "type");
ContractUtils.RequiresNotNull(bounds, "bounds");
if (type.Equals(typeof(void))) {
throw Error.ArgumentCannotBeOfTypeVoid();
}
ReadOnlyCollection<Expression> boundsList = bounds.ToReadOnly();
int dimensions = boundsList.Count;
if (dimensions <= 0) throw Error.BoundsCannotBeLessThanOne();
for (int i = 0; i < dimensions; i++) {
Expression expr = boundsList[i];
RequiresCanRead(expr, "bounds");
if (!TypeUtils.IsInteger(expr.Type)) {
throw Error.ArgumentMustBeInteger();
}
}
Type arrayType;
if (dimensions == 1) {
//To get a vector, need call Type.MakeArrayType().
//Type.MakeArrayType(1) gives a non-vector array, which will cause type check error.
arrayType = type.MakeArrayType();
} else {
arrayType = type.MakeArrayType(dimensions);
}
return NewArrayExpression.Make(ExpressionType.NewArrayBounds, arrayType, bounds.ToReadOnly());
}
#endregion
}
}
|