PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_2_0/Src/Microsoft.Scripting.Core/Ast/Argument.cs

#
C# | 98 lines | 66 code | 17 blank | 15 comment | 6 complexity | 13a6a24cc67432ae1252c2942511b64b MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. using System; using Microsoft;
  16. using Microsoft.Scripting.Utils;
  17. using Microsoft.Contracts;
  18. namespace Microsoft.Linq.Expressions {
  19. public enum ArgumentType {
  20. Positional,
  21. Named
  22. }
  23. public abstract class Argument {
  24. private readonly ArgumentType _type;
  25. internal Argument(ArgumentType type) {
  26. _type = type;
  27. }
  28. public ArgumentType ArgumentType {
  29. get { return _type; }
  30. }
  31. }
  32. public sealed class PositionalArgument : Argument {
  33. private readonly int _position;
  34. internal PositionalArgument(int position)
  35. : base(ArgumentType.Positional) {
  36. _position = position;
  37. }
  38. public int Position {
  39. get { return _position; }
  40. }
  41. [Confined]
  42. public override bool Equals(object obj) {
  43. PositionalArgument arg = obj as PositionalArgument;
  44. return arg != null && arg._position == _position;
  45. }
  46. [Confined]
  47. public override int GetHashCode() {
  48. return _position;
  49. }
  50. }
  51. public sealed class NamedArgument : Argument {
  52. private readonly string _name;
  53. internal NamedArgument(string name)
  54. : base(ArgumentType.Named) {
  55. _name = name;
  56. }
  57. public string Name {
  58. get { return _name; }
  59. }
  60. [Confined]
  61. public override bool Equals(object obj) {
  62. NamedArgument arg = obj as NamedArgument;
  63. return arg != null && arg._name == _name;
  64. }
  65. [Confined]
  66. public override int GetHashCode() {
  67. return _name.GetHashCode();
  68. }
  69. }
  70. public partial class Expression {
  71. public static PositionalArgument PositionalArg(int position) {
  72. ContractUtils.Requires(position >= 0, "position", Strings.MustBePositive);
  73. return new PositionalArgument(position);
  74. }
  75. public static NamedArgument NamedArg(string name) {
  76. // TODO: should we allow the empty string?
  77. ContractUtils.Requires(!string.IsNullOrEmpty(name), "name");
  78. return new NamedArgument(name);
  79. }
  80. }
  81. }