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;
19 using System.Dynamic.Utils;
22 namespace Microsoft.Scripting.Ast {
24 namespace System.Linq.Expressions {
27 /// Provides a wrapper around an IArgumentProvider which exposes the argument providers
28 /// members out as an IList of Expression. This is used to avoid allocating an array
29 /// which needs to be stored inside of a ReadOnlyCollection. Instead this type has
30 /// the same amount of overhead as an array without duplicating the storage of the
31 /// elements. This ensures that internally we can avoid creating and copying arrays
32 /// while users of the Expression trees also don't pay a size penalty for this internal
33 /// optimization. See IArgumentProvider for more general information on the Expression
34 /// tree optimizations being used here.
36 class ListArgumentProvider : IList<Expression> {
37 private readonly IArgumentProvider _provider;
38 private readonly Expression _arg0;
40 internal ListArgumentProvider(IArgumentProvider provider, Expression arg0) {
45 #region IList<Expression> Members
47 public int IndexOf(Expression item) {
52 for (int i = 1; i < _provider.ArgumentCount; i++) {
53 if (_provider.GetArgument(i) == item) {
61 public void Insert(int index, Expression item) {
62 throw ContractUtils.Unreachable;
65 public void RemoveAt(int index) {
66 throw ContractUtils.Unreachable;
69 public Expression this[int index] {
75 return _provider.GetArgument(index);
78 throw ContractUtils.Unreachable;
84 #region ICollection<Expression> Members
86 public void Add(Expression item) {
87 throw ContractUtils.Unreachable;
91 throw ContractUtils.Unreachable;
94 public bool Contains(Expression item) {
95 return IndexOf(item) != -1;
98 public void CopyTo(Expression[] array, int arrayIndex) {
99 array[arrayIndex++] = _arg0;
100 for (int i = 1; i < _provider.ArgumentCount; i++) {
101 array[arrayIndex++] = _provider.GetArgument(i);
106 get { return _provider.ArgumentCount; }
109 public bool IsReadOnly {
113 public bool Remove(Expression item) {
114 throw ContractUtils.Unreachable;
119 #region IEnumerable<Expression> Members
121 public IEnumerator<Expression> GetEnumerator() {
124 for (int i = 1; i < _provider.ArgumentCount; i++) {
125 yield return _provider.GetArgument(i);
131 #region IEnumerable Members
133 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
136 for (int i = 1; i < _provider.ArgumentCount; i++) {
137 yield return _provider.GetArgument(i);