/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Diagnostics;
#if !FEATURE_CORE_DLR
namespace Microsoft.Scripting.Ast {
#else
namespace System.Linq.Expressions {
#endif
///
/// Represents a label, which can be placed in any context. If
/// it is jumped to, it will get the value provided by the corresponding
/// . Otherwise, it gets the value in . If the
/// equals System.Void, no value should be provided.
///
[DebuggerTypeProxy(typeof(Expression.LabelExpressionProxy))]
public sealed class LabelExpression : Expression {
private readonly Expression _defaultValue;
private readonly LabelTarget _target;
internal LabelExpression(LabelTarget label, Expression defaultValue) {
_target = label;
_defaultValue = defaultValue;
}
///
/// Gets the static type of the expression that this represents. (Inherited from .)
///
/// The that represents the static type of the expression.
public sealed override Type Type {
get { return _target.Type; }
}
///
/// Returns the node type of this . (Inherited from .)
///
/// The that represents this expression.
public sealed override ExpressionType NodeType {
get { return ExpressionType.Label; }
}
///
/// The which this label is associated with.
///
public LabelTarget Target {
get { return _target; }
}
///
/// The value of the when the label is reached through
/// normal control flow (e.g. is not jumped to).
///
public Expression DefaultValue {
get { return _defaultValue; }
}
///
/// Dispatches to the specific visit method for this node type.
///
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitLabel(this);
}
///
/// Creates a new expression that is like this one, but using the
/// supplied children. If all of the children are the same, it will
/// return this expression.
///
/// The property of the result.
/// The property of the result.
/// This expression if no children changed, or an expression with the updated children.
public LabelExpression Update(LabelTarget target, Expression defaultValue) {
if (target == Target && defaultValue == DefaultValue) {
return this;
}
return Expression.Label(target, defaultValue);
}
}
public partial class Expression {
///
/// Creates a representing a label with no default value.
///
/// The which this will be associated with.
/// A with no default value.
public static LabelExpression Label(LabelTarget target) {
return Label(target, null);
}
///
/// Creates a representing a label with the given default value.
///
/// The which this will be associated with.
/// The value of this when the label is reached through normal control flow.
/// A with the given default value.
public static LabelExpression Label(LabelTarget target, Expression defaultValue) {
ValidateGoto(target, ref defaultValue, "label", "defaultValue");
return new LabelExpression(target, defaultValue);
}
}
}