2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / LabelTarget.cs
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
17
18 #if CODEPLEX_40
19 using System.Dynamic.Utils;
20 #else
21 using Microsoft.Scripting.Utils;
22 #endif
23
24 #if CODEPLEX_40
25 namespace System.Linq.Expressions {
26 #else
27 namespace Microsoft.Linq.Expressions {
28 #endif
29
30     /// <summary>
31     /// Used to denote the target of a <see cref="GotoExpression"/>.
32     /// </summary>
33     public sealed class LabelTarget {
34         private readonly Type _type;
35         private readonly string _name;
36
37         internal LabelTarget(Type type, string name) {
38             _type = type;
39             _name = name;
40         }
41
42         /// <summary>
43         /// Gets the name of the label.
44         /// </summary>
45         /// <remarks>The label's name is provided for information purposes only.</remarks>
46         public string Name {
47             get { return _name; }
48         }
49
50         /// <summary>
51         /// The type of value that is passed when jumping to the label
52         /// (or System.Void if no value should be passed).
53         /// </summary>
54         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
55         public Type Type {
56             get { return _type; }
57         }
58
59         /// <summary>
60         /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>. 
61         /// </summary>
62         /// <returns>A <see cref="String"/> that represents the current <see cref="Object"/>. </returns>
63         public override string ToString() {
64             return String.IsNullOrEmpty(this.Name) ? "UnamedLabel" : this.Name;
65         }
66     }
67
68     public partial class Expression {
69         /// <summary>
70         /// Creates a <see cref="LabelTarget"/> representing a label with void type and no name.
71         /// </summary>
72         /// <returns>The new <see cref="LabelTarget"/>.</returns>
73         public static LabelTarget Label() {
74             return Label(typeof(void), null);
75         }
76
77         /// <summary>
78         /// Creates a <see cref="LabelTarget"/> representing a label with void type and the given name.
79         /// </summary>
80         /// <param name="name">The name of the label.</param>
81         /// <returns>The new <see cref="LabelTarget"/>.</returns>
82         public static LabelTarget Label(string name) {
83             return Label(typeof(void), name);
84         }
85
86         /// <summary>
87         /// Creates a <see cref="LabelTarget"/> representing a label with the given type.
88         /// </summary>
89         /// <param name="type">The type of value that is passed when jumping to the label.</param>
90         /// <returns>The new <see cref="LabelTarget"/>.</returns>
91         public static LabelTarget Label(Type type) {
92             return Label(type, null);
93         }
94
95         /// <summary>
96         /// Creates a <see cref="LabelTarget"/> representing a label with the given type and name.
97         /// </summary>
98         /// <param name="type">The type of value that is passed when jumping to the label.</param>
99         /// <param name="name">The name of the label.</param>
100         /// <returns>The new <see cref="LabelTarget"/>.</returns>
101         public static LabelTarget Label(Type type, string name) {
102             ContractUtils.RequiresNotNull(type, "type");
103             TypeUtils.ValidateType(type);
104             return new LabelTarget(type, name);
105         }
106     }
107 }