Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Actions / CallInfo.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
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.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 #if !FEATURE_CORE_DLR
17 using Microsoft.Scripting.Ast;
18 #else
19 using System.Linq.Expressions;
20 #endif
21
22 using System.Collections.Generic;
23 using System.Collections.ObjectModel;
24 using System.Dynamic.Utils;
25
26 namespace System.Dynamic {
27
28     /// <summary>
29     /// Describes arguments in the dynamic binding process.
30     /// </summary>
31     /// <remarks>
32     /// ArgumentCount - all inclusive number of arguments.
33     /// ArgumentNames - names for those arguments that are named.
34     ///
35     /// Argument names match to the argument values in left to right order 
36     /// and last name corresponds to the last argument.
37     /// 
38     /// Example:
39     ///   Foo(arg1, arg2, arg3, name1 = arg4, name2 = arg5, name3 = arg6)
40     ///
41     ///   will correspond to:
42     ///    ArgumentCount: 6
43     ///    ArgumentNames: {"name1", "name2", "name3"}
44     /// </remarks>
45     public sealed class CallInfo {
46         private readonly int _argCount;
47         private readonly ReadOnlyCollection<string> _argNames;
48
49         /// <summary>
50         /// Creates a new PositionalArgumentInfo.
51         /// </summary>
52         /// <param name="argCount">The number of arguments.</param>
53         /// <param name="argNames">The argument names.</param>
54         /// <returns>The new CallInfo</returns>
55         public CallInfo(int argCount, params string[] argNames)
56             : this(argCount, (IEnumerable<string>)argNames) {
57         }
58
59         /// <summary>
60         /// Creates a new CallInfo that represents arguments in the dynamic binding process.
61         /// </summary>
62         /// <param name="argCount">The number of arguments.</param>
63         /// <param name="argNames">The argument names.</param>
64         /// <returns>The new CallInfo</returns>
65         public CallInfo(int argCount, IEnumerable<string> argNames) {
66             ContractUtils.RequiresNotNull(argNames, "argNames");
67
68             var argNameCol = argNames.ToReadOnly();
69
70             if (argCount < argNameCol.Count) throw Error.ArgCntMustBeGreaterThanNameCnt();
71             ContractUtils.RequiresNotNullItems(argNameCol, "argNames");
72
73             _argCount = argCount;
74             _argNames = argNameCol;
75         }
76
77         /// <summary>
78         /// The number of arguments.
79         /// </summary>
80         public int ArgumentCount {
81             get { return _argCount; }
82         }
83
84         /// <summary>
85         /// The argument names.
86         /// </summary>
87         public ReadOnlyCollection<string> ArgumentNames {
88             get { return _argNames; }
89         }
90
91         /// <summary>
92         /// Serves as a hash function for the current CallInfo.
93         /// </summary>
94         /// <returns>A hash code for the current CallInfo.</returns>
95         public override int GetHashCode() {
96             return _argCount ^ _argNames.ListHashCode();
97         }
98
99         /// <summary>
100         /// Determines whether the specified CallInfo instance is considered equal to the current.
101         /// </summary>
102         /// <param name="obj">The instance of CallInfo to compare with the current instance.</param>
103         /// <returns>true if the specified instance is equal to the current one otherwise, false.</returns>
104         public override bool Equals(object obj) {
105             var other = obj as CallInfo;
106             return _argCount == other._argCount && _argNames.ListEquals(other._argNames);
107         }
108     }
109 }