xronos /Microsoft.Scripting/Actions/BinderHelper.cs

Language C# Lines 236
MD5 Hash d93802a46510a61f5a291852254980b6 Estimated Cost $4,333 (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
/* ****************************************************************************
 *
 * 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.
 *
 *
 * ***************************************************************************/

#if CODEPLEX_40
using System;
#else
using System; using Microsoft;
#endif
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
#if CODEPLEX_40
using System.Linq.Expressions;
#else
using Microsoft.Linq.Expressions;
#endif
using System.Reflection;
using Microsoft.Scripting.Utils;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using AstUtils = Microsoft.Scripting.Ast.Utils;

namespace Microsoft.Scripting.Actions {
#if CODEPLEX_40
    using Ast = System.Linq.Expressions.Expression;
#else
    using Ast = Microsoft.Linq.Expressions.Expression;
#endif

    public class BinderHelper {
        internal BinderHelper() { }

        // This can produce a IsCallable rule that returns the immutable constant isCallable.
        // Beware that objects can have a mutable callable property. Eg, in Python, assign or delete the __call__ attribute.
        public static bool MakeIsCallableRule(CodeContext context, object self, bool isCallable, RuleBuilder rule) {
            rule.MakeTest(CompilerHelpers.GetType(self));
            rule.Target =
                rule.MakeReturn(
                    context.LanguageContext.Binder,
                    AstUtils.Constant(isCallable)
                );

            return true;
        }
    }

    public class BinderHelper<TAction> : BinderHelper
        where TAction : OldDynamicAction {

        private readonly CodeContext _context;
        private readonly TAction _action;

        public BinderHelper(CodeContext context, TAction action) {
            ContractUtils.RequiresNotNull(context, "context");
            ContractUtils.RequiresNotNull(action, "action");

            _context = context;
            _action = action;
        }

        protected CodeContext Context {
            get {
                return _context;
            }
        }

        protected TAction Action {
            get {
                return _action;
            }
        }

        protected DefaultBinder Binder {
            get {
                return (DefaultBinder)_context.LanguageContext.Binder;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] // TODO: fix
        public static UnaryExpression GetParamsList(RuleBuilder rule) {
            return Ast.Convert(
                rule.Parameters[rule.ParameterCount - 1],
                typeof(IList<object>)
            );
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] // TODO: fix
        public static Expression MakeParamsTest(object paramArg, Expression listArg) {
            return Ast.AndAlso(
                Ast.TypeIs(listArg, CompilerHelpers.GetType(paramArg)),
                Ast.Equal(
                    Ast.Property(
                        Ast.Convert(listArg, typeof(ICollection<object>)),
                        typeof(ICollection<object>).GetProperty("Count")
                    ),
                    AstUtils.Constant(((IList<object>)paramArg).Count)
                )
            );
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] // TODO: fix
        public static Type[] GetArgumentTypes(OldCallAction action, object[] args) {
            List<Type> res = new List<Type>();
            for (int i = 1; i < args.Length; i++) {
                switch (action.Signature.GetArgumentKind(i - 1)) {
                    case ArgumentType.Simple:
                    case ArgumentType.Instance:
                    case ArgumentType.Named:
                        res.Add(CompilerHelpers.GetType(args[i]));
                        continue;

                    case ArgumentType.List:
                        IList<object> list = args[i] as IList<object>;
                        if (list == null) return null;

                        for (int j = 0; j < list.Count; j++) {
                            res.Add(CompilerHelpers.GetType(list[j]));
                        }
                        break;

                    case ArgumentType.Dictionary:
                        // caller needs to process these...
                        break;

                    default:
                        throw new NotImplementedException();
                }
            }
            return res.ToArray();
        }

        internal MethodInfo GetMethod(Type type, string name) {
            // declaring type takes precedence
            MethodInfo mi = type.GetMethod(name);
            if (mi != null) {
                return mi;
            }

            // then search extension types.
            Type curType = type;
            do {
                IList<Type> extTypes = Binder.GetExtensionTypes(curType);
                foreach (Type t in extTypes) {
                    MethodInfo next = t.GetMethod(name);
                    if (next != null) {
                        if (mi != null) {
                            throw new AmbiguousMatchException(String.Format("Found multiple members for {0} on type {1}", name, curType));
                        }

                        mi = next;
                    }
                }

                if (mi != null) {
                    return mi;
                }

                curType = curType.BaseType;
            } while (curType != null);

            return null;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] // TODO: fix
        public static Expression MakeNecessaryTests(RuleBuilder rule, Type[] testTypes, IList<Expression> arguments) {
            Expression typeTest = AstUtils.Constant(true);

            if (testTypes != null) {
                for (int i = 0; i < testTypes.Length; i++) {
                    if (testTypes[i] != null) {
                        Debug.Assert(i < arguments.Count);
                        typeTest = Ast.AndAlso(typeTest, rule.MakeTypeTest(testTypes[i], arguments[i]));
                    }
                }
            }

            return typeTest;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] // TODO: fix
        public static Expression MakeNecessaryTests(RuleBuilder rule, IList<Type[]> necessaryTests, Expression[] arguments) {
            if (necessaryTests.Count == 0) {
                return AstUtils.Constant(true);
            }

            Type[] mostSpecificTypes = null; // This is the final types that will be checked after inspecting all the sets

            for (int i = 0; i < necessaryTests.Count; i++) {
                Type[] currentSet = necessaryTests[i];
                if (currentSet == null) {
                    // The current set is missing. Ignore it
                    continue;
                }

                // All the sets should be of the same size
                Debug.Assert(currentSet.Length == arguments.Length);

                if (mostSpecificTypes == null) {
                    mostSpecificTypes = new Type[currentSet.Length];
                }

                // For each type in the current set, check the type with the corresponding type in the previous sets
                for (int j = 0; j < currentSet.Length; j++) {
                    if (mostSpecificTypes[j] == null || mostSpecificTypes[j].IsAssignableFrom(currentSet[j])) {
                        // no test yet or more specific test
                        mostSpecificTypes[j] = currentSet[j];
                    } else {
                        // All sets should have compatible types in each slot
                        Debug.Assert(currentSet[j] == null || currentSet[j].IsAssignableFrom(mostSpecificTypes[j]));
                    }
                }
            }

            return MakeNecessaryTests(rule, mostSpecificTypes, arguments);
        }

        protected bool PrivateBinding {
            get {
                return Context.LanguageContext.DomainManager.Configuration.PrivateBinding;
            }
        }
    }
}
Back to Top