2009-08-04 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / LabelExpression.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 using System.Diagnostics;
19
20 #if CODEPLEX_40
21 namespace System.Linq.Expressions {
22 #else
23 namespace Microsoft.Linq.Expressions {
24 #endif
25     /// <summary>
26     /// Represents a label, which can be placed in any <see cref="Expression"/> context. If
27     /// it is jumped to, it will get the value provided by the corresponding
28     /// <see cref="GotoExpression"/>. Otherwise, it gets the value in <see cref="LabelExpression.DefaultValue"/>. If the
29     /// <see cref="Type"/> equals System.Void, no value should be provided.
30     /// </summary>
31 #if !SILVERLIGHT
32     [DebuggerTypeProxy(typeof(Expression.LabelExpressionProxy))]
33 #endif
34     public sealed class LabelExpression : Expression {
35         private readonly Expression _defaultValue;
36         private readonly LabelTarget _target;
37
38         internal LabelExpression(LabelTarget label, Expression defaultValue) {
39             _target = label;
40             _defaultValue = defaultValue;
41         }
42
43         /// <summary>
44         /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
45         /// </summary>
46         /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
47         public sealed override Type Type {
48             get { return _target.Type; }
49         }
50
51         /// <summary>
52         /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
53         /// </summary>
54         /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
55         public sealed override ExpressionType NodeType {
56             get { return ExpressionType.Label; }
57         }
58
59         /// <summary>
60         /// The <see cref="LabelTarget"/> which this label is associated with.
61         /// </summary>
62         public LabelTarget Target {
63             get { return _target; }
64         }
65
66         /// <summary>
67         /// The value of the <see cref="LabelExpression"/> when the label is reached through
68         /// normal control flow (e.g. is not jumped to).
69         /// </summary>
70         public Expression DefaultValue {
71             get { return _defaultValue; }
72         }
73
74         /// <summary>
75         /// Dispatches to the specific visit method for this node type.
76         /// </summary>
77         protected internal override Expression Accept(ExpressionVisitor visitor) {
78             return visitor.VisitLabel(this);
79         }
80
81         /// <summary>
82         /// Creates a new expression that is like this one, but using the
83         /// supplied children. If all of the children are the same, it will
84         /// return this expression.
85         /// </summary>
86         /// <param name="target">The <see cref="Target" /> property of the result.</param>
87         /// <param name="defaultValue">The <see cref="DefaultValue" /> property of the result.</param>
88         /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
89         public LabelExpression Update(LabelTarget target, Expression defaultValue) {
90             if (target == Target && defaultValue == DefaultValue) {
91                 return this;
92             }
93             return Expression.Label(target, defaultValue);
94         }
95     }
96
97     public partial class Expression {
98         /// <summary>
99         /// Creates a <see cref="LabelExpression"/> representing a label with no default value.
100         /// </summary>
101         /// <param name="target">The <see cref="LabelTarget"/> which this <see cref="LabelExpression"/> will be associated with.</param>
102         /// <returns>A <see cref="LabelExpression"/> with no default value.</returns>
103         public static LabelExpression Label(LabelTarget target) {
104             return Label(target, null);
105         }
106
107         /// <summary>
108         /// Creates a <see cref="LabelExpression"/> representing a label with the given default value.
109         /// </summary>
110         /// <param name="target">The <see cref="LabelTarget"/> which this <see cref="LabelExpression"/> will be associated with.</param>
111         /// <param name="defaultValue">The value of this <see cref="LabelExpression"/> when the label is reached through normal control flow.</param>
112         /// <returns>A <see cref="LabelExpression"/> with the given default value.</returns>
113         public static LabelExpression Label(LabelTarget target, Expression defaultValue) {
114             ValidateGoto(target, ref defaultValue, "label", "defaultValue");
115             return new LabelExpression(target, defaultValue);
116         }
117     }
118 }