PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting/Actions/Argument.cs

https://bitbucket.org/stefanrusek/xronos
C# | 97 lines | 63 code | 17 blank | 17 comment | 8 complexity | 257d4a2df51eaeb7c07f918f3613b575 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. using System.Linq.Expressions;
  18. #else
  19. using System; using Microsoft;
  20. using Microsoft.Linq.Expressions;
  21. #endif
  22. using Microsoft.Contracts;
  23. using Microsoft.Scripting.Utils;
  24. using AstUtils = Microsoft.Scripting.Ast.Utils;
  25. namespace Microsoft.Scripting.Actions {
  26. /// <summary>
  27. /// TODO: Alternatively, it should be sufficient to remember indices for this, list, dict and block.
  28. /// </summary>
  29. public struct Argument : IEquatable<Argument> {
  30. private readonly ArgumentType _kind;
  31. private readonly string _name;
  32. public static readonly Argument Simple = new Argument(ArgumentType.Simple, null);
  33. public ArgumentType Kind { get { return _kind; } }
  34. public string Name { get { return _name; } }
  35. public Argument(string name) {
  36. _kind = ArgumentType.Named;
  37. _name = name;
  38. }
  39. public Argument(ArgumentType kind) {
  40. _kind = kind;
  41. _name = null;
  42. }
  43. public Argument(ArgumentType kind, string name) {
  44. ContractUtils.Requires((kind == ArgumentType.Named) ^ (name == null), "kind");
  45. _kind = kind;
  46. _name = name;
  47. }
  48. public override bool Equals(object obj) {
  49. return obj is Argument && Equals((Argument)obj);
  50. }
  51. [StateIndependent]
  52. public bool Equals(Argument other) {
  53. return _kind == other._kind && _name == other._name;
  54. }
  55. public static bool operator ==(Argument left, Argument right) {
  56. return left.Equals(right);
  57. }
  58. public static bool operator !=(Argument left, Argument right) {
  59. return !left.Equals(right);
  60. }
  61. public override int GetHashCode() {
  62. return (_name != null) ? _name.GetHashCode() ^ (int)_kind : (int)_kind;
  63. }
  64. public bool IsSimple {
  65. get {
  66. return Equals(Simple);
  67. }
  68. }
  69. public override string ToString() {
  70. return _name == null ? _kind.ToString() : _kind.ToString() + ":" + _name;
  71. }
  72. internal Expression CreateExpression() {
  73. return Expression.New(
  74. typeof(Argument).GetConstructor(new Type[] { typeof(ArgumentType), typeof(string) }),
  75. AstUtils.Constant(_kind),
  76. AstUtils.Constant(_name, typeof(string))
  77. );
  78. }
  79. }
  80. }