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