xronos /Microsoft.Scripting/Actions/CallSignature.cs

Language C# Lines 334
MD5 Hash 65287e634ed938897676b69ee0c60395 Estimated Cost $6,038 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* ****************************************************************************
 *
 * 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.Generic;
using System.Diagnostics;
#if CODEPLEX_40
using System.Linq.Expressions;
#else
using Microsoft.Linq.Expressions;
#endif
using Microsoft.Scripting.Utils;
using System.Text;
using Microsoft.Contracts;
using Microsoft.Scripting.Generation;
using AstUtils = Microsoft.Scripting.Ast.Utils;

namespace Microsoft.Scripting.Actions {
    /// <summary>
    /// Richly represents the signature of a callsite.
    /// </summary>
    public struct CallSignature : IEquatable<CallSignature> {
        // TODO: invariant _infos != null ==> _argumentCount == _infos.Length
        
        /// <summary>
        /// Array of additional meta information about the arguments, such as named arguments.
        /// Null for a simple signature that's just an expression list. eg: foo(a*b,c,d)
        /// </summary>
        private readonly Argument[] _infos;

        /// <summary>
        /// Number of arguments in the signature.
        /// </summary>
        private readonly int _argumentCount;

        /// <summary>
        /// All arguments are unnamed and matched by position. 
        /// </summary>
        public bool IsSimple {
            get { return _infos == null; }
        }

        public int ArgumentCount {
            get {
                Debug.Assert(_infos == null || _infos.Length == _argumentCount);
                return _argumentCount; 
            }
        }

        #region Construction

        public CallSignature(CallSignature signature) {
            _infos = signature.GetArgumentInfos();
            _argumentCount = signature._argumentCount;
        }
        
        public CallSignature(int argumentCount) {
            ContractUtils.Requires(argumentCount >= 0, "argumentCount");
            _argumentCount = argumentCount;
            _infos = null;
        }

        public CallSignature(params Argument[] infos) {
            bool simple = true;

            if (infos != null) {
                _argumentCount = infos.Length;
                for (int i = 0; i < infos.Length; i++) {
                    if (infos[i].Kind != ArgumentType.Simple) {
                        simple = false;
                        break;
                    }
                }
            } else {
                _argumentCount = 0;
            }

            _infos = (!simple) ? infos : null;
        }

        public CallSignature(params ArgumentType[] kinds) {
            bool simple = true;

            if (kinds != null) {
                _argumentCount = kinds.Length;
                for (int i = 0; i < kinds.Length; i++) {
                    if (kinds[i] != ArgumentType.Simple) {
                        simple = false;
                        break;
                    }
                }
            } else {
                _argumentCount = 0;
            }

            if (!simple) {
                _infos = new Argument[kinds.Length];
                for (int i = 0; i < kinds.Length; i++) {
                    _infos[i] = new Argument(kinds[i]);
                }
            } else {
                _infos = null;
            }
        }

        #endregion

        #region IEquatable<CallSignature> Members

        [StateIndependent]
        public bool Equals(CallSignature other) {
            if (_infos == null) {
                return other._infos == null && other._argumentCount == _argumentCount;
            } else if (other._infos == null) {
                return false;
            }

            if (_infos.Length != other._infos.Length) return false;

            for (int i = 0; i < _infos.Length; i++) {
                if (!_infos[i].Equals(other._infos[i])) return false;
            }

            return true;
        }

        #endregion

        #region Overrides

        public override bool Equals(object obj) {
            return obj is CallSignature && Equals((CallSignature)obj);
        }

        public static bool operator ==(CallSignature left, CallSignature right) {
            return left.Equals(right);
        }

        public static bool operator !=(CallSignature left, CallSignature right) {
            return !left.Equals(right);
        }

        public override string ToString() {
            if (_infos == null) {
                return "Simple";
            }
            
            StringBuilder sb = new StringBuilder("(");
            for (int i = 0; i < _infos.Length; i++) {
                if (i > 0) {
                    sb.Append(", ");
                }
                sb.Append(_infos[i].ToString());
            }
            sb.Append(")");
            return sb.ToString();
        }

        public override int GetHashCode() {
            int h = 6551;
            if (_infos != null) {
                foreach (Argument info in _infos) {
                    h ^= (h << 5) ^ info.GetHashCode();
                }
            }
            return h;
        }

        #endregion

        #region Helpers

        public Argument[] GetArgumentInfos() {
            return (_infos != null) ? ArrayUtils.Copy(_infos) : CompilerHelpers.MakeRepeatedArray(Argument.Simple, _argumentCount);
        }

        public CallSignature InsertArgument(Argument info) {
            return InsertArgumentAt(0, info);
        }

        public CallSignature InsertArgumentAt(int index, Argument info) {
            if (this.IsSimple) {
                if (info.IsSimple) {
                    return new CallSignature(_argumentCount + 1);
                }
                
                return new CallSignature(ArrayUtils.InsertAt(GetArgumentInfos(), index, info));
            }

            return new CallSignature(ArrayUtils.InsertAt(_infos, index, info));
        }

        public CallSignature RemoveFirstArgument() {
            return RemoveArgumentAt(0);
        }

        public CallSignature RemoveArgumentAt(int index) {
            if (_argumentCount == 0) {
                throw new InvalidOperationException();
            }

            if (IsSimple) {
                return new CallSignature(_argumentCount - 1);
            }

            return new CallSignature(ArrayUtils.RemoveAt(_infos, index));
        }

        public int IndexOf(ArgumentType kind) {
            if (_infos == null) {
                return (kind == ArgumentType.Simple && _argumentCount > 0) ? 0 : -1;
            }

            for (int i = 0; i < _infos.Length; i++) {
                if (_infos[i].Kind == kind) {
                    return i;
                }
            }
            return -1;
        }

        public bool HasDictionaryArgument() {
            return IndexOf(ArgumentType.Dictionary) > -1;
        }

        public bool HasInstanceArgument() {
            return IndexOf(ArgumentType.Instance) > -1;
        }

        public bool HasListArgument() {
            return IndexOf(ArgumentType.List) > -1;
        }

        internal bool HasNamedArgument() {
            return IndexOf(ArgumentType.Named) > -1;
        }

        /// <summary>
        /// True if the OldCallAction includes an ArgumentInfo of ArgumentKind.Dictionary or ArgumentKind.Named.
        /// </summary>
        public bool HasKeywordArgument() {
            if (_infos != null) {
                foreach (Argument info in _infos) {
                    if (info.Kind == ArgumentType.Dictionary || info.Kind == ArgumentType.Named) {
                        return true;
                    }
                }
            }
            return false;
        }
        
        public ArgumentType GetArgumentKind(int index) {
            // TODO: Contract.Requires(index >= 0 && index < _argumentCount, "index");
            return _infos != null ? _infos[index].Kind : ArgumentType.Simple;
        }

        public string GetArgumentName(int index) {
            ContractUtils.Requires(index >= 0 && index < _argumentCount);
            return _infos != null ? _infos[index].Name : null;
        }

        /// <summary>
        /// Gets the number of positional arguments the user provided at the call site.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public int GetProvidedPositionalArgumentCount() {
            int result = _argumentCount;

            if (_infos != null) {
                for (int i = 0; i < _infos.Length; i++) {
                    ArgumentType kind = _infos[i].Kind;

                    if (kind == ArgumentType.Dictionary || kind == ArgumentType.List || kind == ArgumentType.Named) {
                        result--;
                    }
                }
            }

            return result;
        }

        public string[] GetArgumentNames() {
            if (_infos == null) {
                return ArrayUtils.EmptyStrings;
            }

            List<string> result = new List<string>();
            foreach (Argument info in _infos) {
                if (info.Name != null) {
                    result.Add(info.Name);
                }
            }

            return result.ToArray();
        }

        public Expression CreateExpression() {            
            if (_infos == null) {
                return Expression.New(
                    typeof(CallSignature).GetConstructor(new Type[] { typeof(int) }),
                    AstUtils.Constant(ArgumentCount)
                );
            } else {
                Expression[] args = new Expression[_infos.Length];
                for (int i = 0; i < args.Length; i++) {
                    args[i] = _infos[i].CreateExpression();
                }
                return Expression.New(
                    typeof(CallSignature).GetConstructor(new Type[] { typeof(Argument[]) }), 
                    Expression.NewArrayInit(typeof(Argument), args)
                );
            }
        }

        #endregion
    }
}
Back to Top