PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting/Actions/CallSignature.cs

https://bitbucket.org/stefanrusek/xronos
C# | 333 lines | 240 code | 58 blank | 35 comment | 77 complexity | 65287e634ed938897676b69ee0c60395 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. 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 Microsoft Public License, 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 Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if CODEPLEX_40
  16. using System;
  17. #else
  18. using System; using Microsoft;
  19. #endif
  20. using System.Collections.Generic;
  21. using System.Diagnostics;
  22. #if CODEPLEX_40
  23. using System.Linq.Expressions;
  24. #else
  25. using Microsoft.Linq.Expressions;
  26. #endif
  27. using Microsoft.Scripting.Utils;
  28. using System.Text;
  29. using Microsoft.Contracts;
  30. using Microsoft.Scripting.Generation;
  31. using AstUtils = Microsoft.Scripting.Ast.Utils;
  32. namespace Microsoft.Scripting.Actions {
  33. /// <summary>
  34. /// Richly represents the signature of a callsite.
  35. /// </summary>
  36. public struct CallSignature : IEquatable<CallSignature> {
  37. // TODO: invariant _infos != null ==> _argumentCount == _infos.Length
  38. /// <summary>
  39. /// Array of additional meta information about the arguments, such as named arguments.
  40. /// Null for a simple signature that's just an expression list. eg: foo(a*b,c,d)
  41. /// </summary>
  42. private readonly Argument[] _infos;
  43. /// <summary>
  44. /// Number of arguments in the signature.
  45. /// </summary>
  46. private readonly int _argumentCount;
  47. /// <summary>
  48. /// All arguments are unnamed and matched by position.
  49. /// </summary>
  50. public bool IsSimple {
  51. get { return _infos == null; }
  52. }
  53. public int ArgumentCount {
  54. get {
  55. Debug.Assert(_infos == null || _infos.Length == _argumentCount);
  56. return _argumentCount;
  57. }
  58. }
  59. #region Construction
  60. public CallSignature(CallSignature signature) {
  61. _infos = signature.GetArgumentInfos();
  62. _argumentCount = signature._argumentCount;
  63. }
  64. public CallSignature(int argumentCount) {
  65. ContractUtils.Requires(argumentCount >= 0, "argumentCount");
  66. _argumentCount = argumentCount;
  67. _infos = null;
  68. }
  69. public CallSignature(params Argument[] infos) {
  70. bool simple = true;
  71. if (infos != null) {
  72. _argumentCount = infos.Length;
  73. for (int i = 0; i < infos.Length; i++) {
  74. if (infos[i].Kind != ArgumentType.Simple) {
  75. simple = false;
  76. break;
  77. }
  78. }
  79. } else {
  80. _argumentCount = 0;
  81. }
  82. _infos = (!simple) ? infos : null;
  83. }
  84. public CallSignature(params ArgumentType[] kinds) {
  85. bool simple = true;
  86. if (kinds != null) {
  87. _argumentCount = kinds.Length;
  88. for (int i = 0; i < kinds.Length; i++) {
  89. if (kinds[i] != ArgumentType.Simple) {
  90. simple = false;
  91. break;
  92. }
  93. }
  94. } else {
  95. _argumentCount = 0;
  96. }
  97. if (!simple) {
  98. _infos = new Argument[kinds.Length];
  99. for (int i = 0; i < kinds.Length; i++) {
  100. _infos[i] = new Argument(kinds[i]);
  101. }
  102. } else {
  103. _infos = null;
  104. }
  105. }
  106. #endregion
  107. #region IEquatable<CallSignature> Members
  108. [StateIndependent]
  109. public bool Equals(CallSignature other) {
  110. if (_infos == null) {
  111. return other._infos == null && other._argumentCount == _argumentCount;
  112. } else if (other._infos == null) {
  113. return false;
  114. }
  115. if (_infos.Length != other._infos.Length) return false;
  116. for (int i = 0; i < _infos.Length; i++) {
  117. if (!_infos[i].Equals(other._infos[i])) return false;
  118. }
  119. return true;
  120. }
  121. #endregion
  122. #region Overrides
  123. public override bool Equals(object obj) {
  124. return obj is CallSignature && Equals((CallSignature)obj);
  125. }
  126. public static bool operator ==(CallSignature left, CallSignature right) {
  127. return left.Equals(right);
  128. }
  129. public static bool operator !=(CallSignature left, CallSignature right) {
  130. return !left.Equals(right);
  131. }
  132. public override string ToString() {
  133. if (_infos == null) {
  134. return "Simple";
  135. }
  136. StringBuilder sb = new StringBuilder("(");
  137. for (int i = 0; i < _infos.Length; i++) {
  138. if (i > 0) {
  139. sb.Append(", ");
  140. }
  141. sb.Append(_infos[i].ToString());
  142. }
  143. sb.Append(")");
  144. return sb.ToString();
  145. }
  146. public override int GetHashCode() {
  147. int h = 6551;
  148. if (_infos != null) {
  149. foreach (Argument info in _infos) {
  150. h ^= (h << 5) ^ info.GetHashCode();
  151. }
  152. }
  153. return h;
  154. }
  155. #endregion
  156. #region Helpers
  157. public Argument[] GetArgumentInfos() {
  158. return (_infos != null) ? ArrayUtils.Copy(_infos) : CompilerHelpers.MakeRepeatedArray(Argument.Simple, _argumentCount);
  159. }
  160. public CallSignature InsertArgument(Argument info) {
  161. return InsertArgumentAt(0, info);
  162. }
  163. public CallSignature InsertArgumentAt(int index, Argument info) {
  164. if (this.IsSimple) {
  165. if (info.IsSimple) {
  166. return new CallSignature(_argumentCount + 1);
  167. }
  168. return new CallSignature(ArrayUtils.InsertAt(GetArgumentInfos(), index, info));
  169. }
  170. return new CallSignature(ArrayUtils.InsertAt(_infos, index, info));
  171. }
  172. public CallSignature RemoveFirstArgument() {
  173. return RemoveArgumentAt(0);
  174. }
  175. public CallSignature RemoveArgumentAt(int index) {
  176. if (_argumentCount == 0) {
  177. throw new InvalidOperationException();
  178. }
  179. if (IsSimple) {
  180. return new CallSignature(_argumentCount - 1);
  181. }
  182. return new CallSignature(ArrayUtils.RemoveAt(_infos, index));
  183. }
  184. public int IndexOf(ArgumentType kind) {
  185. if (_infos == null) {
  186. return (kind == ArgumentType.Simple && _argumentCount > 0) ? 0 : -1;
  187. }
  188. for (int i = 0; i < _infos.Length; i++) {
  189. if (_infos[i].Kind == kind) {
  190. return i;
  191. }
  192. }
  193. return -1;
  194. }
  195. public bool HasDictionaryArgument() {
  196. return IndexOf(ArgumentType.Dictionary) > -1;
  197. }
  198. public bool HasInstanceArgument() {
  199. return IndexOf(ArgumentType.Instance) > -1;
  200. }
  201. public bool HasListArgument() {
  202. return IndexOf(ArgumentType.List) > -1;
  203. }
  204. internal bool HasNamedArgument() {
  205. return IndexOf(ArgumentType.Named) > -1;
  206. }
  207. /// <summary>
  208. /// True if the OldCallAction includes an ArgumentInfo of ArgumentKind.Dictionary or ArgumentKind.Named.
  209. /// </summary>
  210. public bool HasKeywordArgument() {
  211. if (_infos != null) {
  212. foreach (Argument info in _infos) {
  213. if (info.Kind == ArgumentType.Dictionary || info.Kind == ArgumentType.Named) {
  214. return true;
  215. }
  216. }
  217. }
  218. return false;
  219. }
  220. public ArgumentType GetArgumentKind(int index) {
  221. // TODO: Contract.Requires(index >= 0 && index < _argumentCount, "index");
  222. return _infos != null ? _infos[index].Kind : ArgumentType.Simple;
  223. }
  224. public string GetArgumentName(int index) {
  225. ContractUtils.Requires(index >= 0 && index < _argumentCount);
  226. return _infos != null ? _infos[index].Name : null;
  227. }
  228. /// <summary>
  229. /// Gets the number of positional arguments the user provided at the call site.
  230. /// </summary>
  231. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
  232. public int GetProvidedPositionalArgumentCount() {
  233. int result = _argumentCount;
  234. if (_infos != null) {
  235. for (int i = 0; i < _infos.Length; i++) {
  236. ArgumentType kind = _infos[i].Kind;
  237. if (kind == ArgumentType.Dictionary || kind == ArgumentType.List || kind == ArgumentType.Named) {
  238. result--;
  239. }
  240. }
  241. }
  242. return result;
  243. }
  244. public string[] GetArgumentNames() {
  245. if (_infos == null) {
  246. return ArrayUtils.EmptyStrings;
  247. }
  248. List<string> result = new List<string>();
  249. foreach (Argument info in _infos) {
  250. if (info.Name != null) {
  251. result.Add(info.Name);
  252. }
  253. }
  254. return result.ToArray();
  255. }
  256. public Expression CreateExpression() {
  257. if (_infos == null) {
  258. return Expression.New(
  259. typeof(CallSignature).GetConstructor(new Type[] { typeof(int) }),
  260. AstUtils.Constant(ArgumentCount)
  261. );
  262. } else {
  263. Expression[] args = new Expression[_infos.Length];
  264. for (int i = 0; i < args.Length; i++) {
  265. args[i] = _infos[i].CreateExpression();
  266. }
  267. return Expression.New(
  268. typeof(CallSignature).GetConstructor(new Type[] { typeof(Argument[]) }),
  269. Expression.NewArrayInit(typeof(Argument), args)
  270. );
  271. }
  272. }
  273. #endregion
  274. }
  275. }