xronos /Microsoft.Scripting/Actions/ComboBinder.cs

Language C# Lines 320
MD5 Hash e10b349b6699bcf9bea6d79771a9e7ef Estimated Cost $5,826 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, 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 Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/
using System; using Microsoft;


using System.Collections.Generic;
#if CODEPLEX_40
using System.Linq.Expressions;
using System.Dynamic;
#else
using Microsoft.Linq.Expressions;
using Microsoft.Scripting;
#endif
using System.Runtime.CompilerServices;
#if !CODEPLEX_40
using Microsoft.Runtime.CompilerServices;
#endif

using System.Text;
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting.Actions {
    /// <summary>
    /// A binder which can combine multiple binders into a single dynamic site.  The creator
    /// of this needs to perform the mapping of parameters, constants, and sub-site expressions
    /// and provide a List of BinderMappingInfo representing this data.  From there the ComboBinder
    /// just processes the list to create the resulting code.
    /// </summary>
    public class ComboBinder : DynamicMetaObjectBinder {
        private readonly BinderMappingInfo[] _metaBinders;

        public ComboBinder(params BinderMappingInfo[] binders)
            : this((ICollection<BinderMappingInfo>)binders) {
        }

        public ComboBinder(ICollection<BinderMappingInfo> binders) {
            Assert.NotNullItems(binders);

            _metaBinders = ArrayUtils.ToArray(binders);
        }

        public override DynamicMetaObject Bind(DynamicMetaObject target, params DynamicMetaObject[] args) {
            args = ArrayUtils.Insert(target, args);

            List<DynamicMetaObject> results = new List<DynamicMetaObject>(_metaBinders.Length);
            List<Expression> steps = new List<Expression>();
            List<ParameterExpression> temps = new List<ParameterExpression>();
            BindingRestrictions restrictions = BindingRestrictions.Empty;

            for (int i = 0; i < _metaBinders.Length; i++) {
                BinderMappingInfo curBinder = _metaBinders[i];

                DynamicMetaObject[] tmpargs = GetArguments(args, results, i);
                DynamicMetaObject next = curBinder.Binder.Bind(tmpargs[0], ArrayUtils.RemoveFirst(tmpargs));
                if (i != 0) {
                    // If the rule contains an embedded "update", replace it with a defer
                    var visitor = new ReplaceUpdateVisitor { Binder = curBinder.Binder, Arguments = tmpargs };
                    next = new DynamicMetaObject(visitor.Visit(next.Expression), next.Restrictions);
                }

                if (next.Expression.NodeType == ExpressionType.Throw) {
                    // end of the line... the expression is throwing, none of the other 
                    // binders will have an opportunity to run.
                    steps.Add(next.Expression);
                    break;
                }

                ParameterExpression tmp = Expression.Variable(next.Expression.Type, "comboTemp" + i.ToString());
                temps.Add(tmp);

                steps.Add(Expression.Assign(tmp, next.Expression));
                results.Add(new DynamicMetaObject(tmp, next.Restrictions));
                restrictions = restrictions.Merge(next.Restrictions);
            }

            return new DynamicMetaObject(
                Expression.Block(
                    temps.ToArray(),
                    steps.ToArray()
                ),
                restrictions
            );
        }

        private sealed class ReplaceUpdateVisitor : ExpressionVisitor {
            internal DynamicMetaObjectBinder Binder;
            internal DynamicMetaObject[] Arguments;

            protected override Expression VisitGoto(GotoExpression node) {
                if (node.Target == CallSiteBinder.UpdateLabel) {
                    return Binder.Defer(Arguments).Expression;
                }
                return base.Visit(node);
            }
        }


        private DynamicMetaObject[] GetArguments(DynamicMetaObject[] args, IList<DynamicMetaObject> results, int metaBinderIndex) {
            BinderMappingInfo indices = _metaBinders[metaBinderIndex];

            DynamicMetaObject[] res = new DynamicMetaObject[indices.MappingInfo.Count];
            for (int i = 0; i < res.Length; i++) {
                ParameterMappingInfo mappingInfo = indices.MappingInfo[i];

                if (mappingInfo.IsAction) {
                    // input is the result of a previous bind
                    res[i] = results[mappingInfo.ActionIndex];
                } else if (mappingInfo.IsParameter) {
                    // input is one of the original arguments
                    res[i] = args[mappingInfo.ParameterIndex];
                } else {
                    // input is a constant
                    res[i] = new DynamicMetaObject(
                        mappingInfo.Constant,
                        BindingRestrictions.Empty,
                        mappingInfo.Constant.Value
                    );
                }
            }

            return res;
        }

        public override int GetHashCode() {
            int res = 6551;
            foreach (BinderMappingInfo metaBinder in _metaBinders) {
                res ^= metaBinder.Binder.GetHashCode();

                foreach (ParameterMappingInfo mapInfo in metaBinder.MappingInfo) {
                    res ^= mapInfo.GetHashCode();
                }
            }

            return res;
        }

        public override bool Equals(object obj) {
            ComboBinder other = obj as ComboBinder;
            if (other != null) {
                if (_metaBinders.Length != other._metaBinders.Length) {
                    return false;
                }

                for (int i = 0; i < _metaBinders.Length; i++) {
                    BinderMappingInfo self = _metaBinders[i];
                    BinderMappingInfo otherBinders = other._metaBinders[i];

                    if (!self.Binder.Equals(otherBinders.Binder) ||
                        self.MappingInfo.Count != otherBinders.MappingInfo.Count) {
                        return false;
                    }

                    for (int j = 0; j < self.MappingInfo.Count; j++) {
                        if (!self.MappingInfo[j].Equals(otherBinders.MappingInfo[j])) {
                            return false;
                        }
                    }
                }
                return true;
            }

            return false;
        }
    }

    /// <summary>
    /// Provides a mapping for inputs of combo action expressions.  The input can map
    /// to either an input of the new dynamic site, an input of a previous DynamicExpression,
    /// or a ConstantExpression which has been pulled out of the dynamic site arguments.
    /// </summary>
    public class ParameterMappingInfo {
        private readonly int _parameterIndex;
        private readonly int _actionIndex;
        private ConstantExpression _fixedInput;

        private ParameterMappingInfo(int param, int action, ConstantExpression fixedInput) {
            _parameterIndex = param;
            _actionIndex = action;
            _fixedInput = fixedInput;
        }

        public static ParameterMappingInfo Parameter(int index) {
            return new ParameterMappingInfo(index, -1, null);
        }

        public static ParameterMappingInfo Action(int index) {
            return new ParameterMappingInfo(-1, index, null);
        }

        public static ParameterMappingInfo Fixed(ConstantExpression e) {
            return new ParameterMappingInfo(-1, -1, e);
        }

        public int ParameterIndex {
            get {
                return _parameterIndex;
            }
        }

        public int ActionIndex {
            get {
                return _actionIndex;
            }
        }

        public ConstantExpression Constant {
            get {
                return _fixedInput;
            }
        }

        public bool IsParameter {
            get {
                return _parameterIndex != -1;
            }
        }

        public bool IsAction {
            get {
                return _actionIndex != -1;
            }
        }

        public bool IsConstant {
            get {
                return _fixedInput != null;
            }
        }

        public override bool Equals(object obj) {
            ParameterMappingInfo pmi = obj as ParameterMappingInfo;
            if (pmi == null) {
                return false;
            }

            if (pmi.ParameterIndex == ParameterIndex && pmi.ActionIndex == ActionIndex) {
                if (Constant != null) {
                    if (pmi.Constant == null) {
                        return false;
                    }

                    return Constant.Value == pmi.Constant.Value;
                } else {
                    return pmi.Constant == null;
                }
            }

            return false;
        }

        public override int GetHashCode() {
            int res = ParameterIndex.GetHashCode() ^ ActionIndex.GetHashCode();
            if (Constant != null) {
                if (Constant.Value != null) {
                    res ^= Constant.Value.GetHashCode();
                }
            }
            return res;
        }

        public override string ToString() {
            if (IsAction) {
                return "Action" + ActionIndex.ToString();
            } else if (IsParameter) {
                return "Parameter" + ParameterIndex.ToString();
            } else {
                object value = Constant.Value;
                if (value == null) {
                    return "(null)";
                }

                return value.ToString();
            }
        }
    }

    /// <summary>
    /// Contains the mapping information for a single Combo Binder.  This includes the original
    /// meta-binder and the mapping of parameters, sub-sites, and constants into the binding.
    /// </summary>
    public class BinderMappingInfo {
        public DynamicMetaObjectBinder Binder;
        public IList<ParameterMappingInfo> MappingInfo;

        public BinderMappingInfo(DynamicMetaObjectBinder binder, IList<ParameterMappingInfo> mappingInfo) {
            Binder = binder;
            MappingInfo = mappingInfo;
        }

        public BinderMappingInfo(DynamicMetaObjectBinder binder, params ParameterMappingInfo[] mappingInfos)
            : this(binder, (IList<ParameterMappingInfo>)mappingInfos) {
        }

        public override string ToString() {
            StringBuilder res = new StringBuilder();
            res.Append(Binder.ToString());
            res.Append(" ");
            string comma = "";
            foreach (ParameterMappingInfo info in MappingInfo) {
                res.Append(comma);
                res.Append(info.ToString());
                comma = ", ";
            }
            return res.ToString();
        }
    }
}
Back to Top