PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms 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
 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 * ***************************************************************************/
15using System; using Microsoft;
16
17
18using Microsoft.Scripting.Utils;
19using Microsoft.Contracts;
20
21namespace Microsoft.Linq.Expressions {
22    public enum ArgumentType {
23        Positional,
24        Named
25    }
26
27    public abstract class Argument {
28        private readonly ArgumentType _type;
29
30        internal Argument(ArgumentType type) {
31            _type = type;
32        }
33
34        public ArgumentType ArgumentType {
35            get { return _type; }
36        }
37    }
38
39    public sealed class PositionalArgument : Argument {
40        private readonly int _position;
41
42        internal PositionalArgument(int position)
43            : base(ArgumentType.Positional) {
44            _position = position;
45        }
46
47        public int Position {
48            get { return _position; }
49        }
50
51        [Confined]
52        public override bool Equals(object obj) {
53            PositionalArgument arg = obj as PositionalArgument;
54            return arg != null && arg._position == _position;
55        }
56
57        [Confined]
58        public override int GetHashCode() {
59            return _position;
60        }
61    }
62
63    public sealed class NamedArgument : Argument {
64        private readonly string _name;
65
66        internal NamedArgument(string name)
67            : base(ArgumentType.Named) {
68            _name = name;
69        }
70
71        public string Name {
72            get { return _name; }
73        }
74
75        [Confined]
76        public override bool Equals(object obj) {
77            NamedArgument arg = obj as NamedArgument;
78            return arg != null && arg._name == _name;
79        }
80
81        [Confined]
82        public override int GetHashCode() {
83            return _name.GetHashCode();
84        }
85    }
86
87    public partial class Expression {
88        public static PositionalArgument PositionalArg(int position) {
89            ContractUtils.Requires(position >= 0, "position", Strings.MustBePositive);
90            return new PositionalArgument(position);
91        }
92        public static NamedArgument NamedArg(string name) {
93            // TODO: should we allow the empty string?
94            ContractUtils.Requires(!string.IsNullOrEmpty(name), "name");
95            return new NamedArgument(name);
96        }
97    }
98}