1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using System.Diagnostics;
18 using System.Dynamic.Utils;
25 namespace Microsoft.Scripting.Ast {
27 namespace System.Linq.Expressions {
30 /// Specifies what kind of jump this <see cref="GotoExpression"/> represents.
32 public enum GotoExpressionKind {
34 /// A <see cref="GotoExpression"/> that represents a jump to some location.
38 /// A <see cref="GotoExpression"/> that represents a return statement.
42 /// A <see cref="GotoExpression"/> that represents a break statement.
46 /// A <see cref="GotoExpression"/> that represents a continue statement.
52 /// Represents an unconditional jump. This includes return statements, break and continue statements, and other jumps.
55 [DebuggerTypeProxy(typeof(Expression.GotoExpressionProxy))]
57 public sealed class GotoExpression : Expression {
58 private readonly GotoExpressionKind _kind;
59 private readonly Expression _value;
60 private readonly LabelTarget _target;
61 private readonly Type _type;
63 internal GotoExpression(GotoExpressionKind kind, LabelTarget target, Expression value, Type type) {
71 /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
73 /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
74 public sealed override Type Type {
79 /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
81 /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
82 public sealed override ExpressionType NodeType {
83 get { return ExpressionType.Goto; }
87 /// The value passed to the target, or null if the target is of type
90 public Expression Value {
91 get { return _value; }
95 /// The target label where this node jumps to.
97 public LabelTarget Target {
98 get { return _target; }
102 /// The kind of the goto. For information purposes only.
104 public GotoExpressionKind Kind {
105 get { return _kind; }
109 /// Dispatches to the specific visit method for this node type.
111 protected internal override Expression Accept(ExpressionVisitor visitor) {
112 return visitor.VisitGoto(this);
116 /// Creates a new expression that is like this one, but using the
117 /// supplied children. If all of the children are the same, it will
118 /// return this expression.
120 /// <param name="target">The <see cref="Target" /> property of the result.</param>
121 /// <param name="value">The <see cref="Value" /> property of the result.</param>
122 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
123 public GotoExpression Update(LabelTarget target, Expression value) {
124 if (target == Target && value == Value) {
127 return Expression.MakeGoto(Kind, target, value, Type);
131 public partial class Expression {
133 /// Creates a <see cref="GotoExpression"/> representing a break statement.
135 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
137 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Break,
138 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>, and a null value to be passed to the target label upon jumping.
140 public static GotoExpression Break(LabelTarget target) {
141 return MakeGoto(GotoExpressionKind.Break, target, null, typeof(void));
145 /// Creates a <see cref="GotoExpression"/> representing a break statement. The value passed to the label upon jumping can be specified.
147 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
148 /// <param name="value">The value that will be passed to the associated label upon jumping.</param>
150 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Break,
151 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
152 /// and <paramref name="value"/> to be passed to the target label upon jumping.
154 public static GotoExpression Break(LabelTarget target, Expression value) {
155 return MakeGoto(GotoExpressionKind.Break, target, value, typeof(void));
159 /// Creates a <see cref="GotoExpression"/> representing a break statement with the specified type.
161 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
162 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
164 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Break,
165 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
166 /// and the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>.
168 public static GotoExpression Break(LabelTarget target, Type type) {
169 return MakeGoto(GotoExpressionKind.Break, target, null, type);
173 /// Creates a <see cref="GotoExpression"/> representing a break statement with the specified type.
174 /// The value passed to the label upon jumping can be specified.
176 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
177 /// <param name="value">The value that will be passed to the associated label upon jumping.</param>
178 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
180 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Break,
181 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
182 /// the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>,
183 /// and <paramref name="value"/> to be passed to the target label upon jumping.
185 public static GotoExpression Break(LabelTarget target, Expression value, Type type) {
186 return MakeGoto(GotoExpressionKind.Break, target, value, type);
190 /// Creates a <see cref="GotoExpression"/> representing a continue statement.
192 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
194 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Continue,
195 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
196 /// and a null value to be passed to the target label upon jumping.
198 public static GotoExpression Continue(LabelTarget target) {
199 return MakeGoto(GotoExpressionKind.Continue, target, null, typeof(void));
203 /// Creates a <see cref="GotoExpression"/> representing a continue statement with the specified type.
205 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
206 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
208 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Continue,
209 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
210 /// the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>,
211 /// and a null value to be passed to the target label upon jumping.
213 public static GotoExpression Continue(LabelTarget target, Type type) {
214 return MakeGoto(GotoExpressionKind.Continue, target, null, type);
218 /// Creates a <see cref="GotoExpression"/> representing a return statement.
220 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
222 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Return,
223 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
224 /// and a null value to be passed to the target label upon jumping.
226 public static GotoExpression Return(LabelTarget target) {
227 return MakeGoto(GotoExpressionKind.Return, target, null, typeof(void));
231 /// Creates a <see cref="GotoExpression"/> representing a return statement with the specified type.
233 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
234 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
236 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Return,
237 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
238 /// the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>,
239 /// and a null value to be passed to the target label upon jumping.
241 public static GotoExpression Return(LabelTarget target, Type type) {
242 return MakeGoto(GotoExpressionKind.Return, target, null, type);
246 /// Creates a <see cref="GotoExpression"/> representing a return statement. The value passed to the label upon jumping can be specified.
248 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
249 /// <param name="value">The value that will be passed to the associated label upon jumping.</param>
251 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Continue,
252 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
253 /// and <paramref name="value"/> to be passed to the target label upon jumping.
255 public static GotoExpression Return(LabelTarget target, Expression value) {
256 return MakeGoto(GotoExpressionKind.Return, target, value, typeof(void));
260 /// Creates a <see cref="GotoExpression"/> representing a return statement with the specified type.
261 /// The value passed to the label upon jumping can be specified.
263 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
264 /// <param name="value">The value that will be passed to the associated label upon jumping.</param>
265 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
267 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Continue,
268 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
269 /// the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>,
270 /// and <paramref name="value"/> to be passed to the target label upon jumping.
272 public static GotoExpression Return(LabelTarget target, Expression value, Type type) {
273 return MakeGoto(GotoExpressionKind.Return, target, value, type);
277 /// Creates a <see cref="GotoExpression"/> representing a goto.
279 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
281 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Goto,
282 /// the <see cref="P:GotoExpression.Target"/> property set to the specified value,
283 /// and a null value to be passed to the target label upon jumping.
285 public static GotoExpression Goto(LabelTarget target) {
286 return MakeGoto(GotoExpressionKind.Goto, target, null, typeof(void));
290 /// Creates a <see cref="GotoExpression"/> representing a goto with the specified type.
292 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
293 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
295 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Goto,
296 /// the <see cref="P:GotoExpression.Target"/> property set to the specified value,
297 /// the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>,
298 /// and a null value to be passed to the target label upon jumping.
300 public static GotoExpression Goto(LabelTarget target, Type type) {
301 return MakeGoto(GotoExpressionKind.Goto, target, null, type);
305 /// Creates a <see cref="GotoExpression"/> representing a goto. The value passed to the label upon jumping can be specified.
307 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
308 /// <param name="value">The value that will be passed to the associated label upon jumping.</param>
310 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Goto,
311 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
312 /// and <paramref name="value"/> to be passed to the target label upon jumping.
314 public static GotoExpression Goto(LabelTarget target, Expression value) {
315 return MakeGoto(GotoExpressionKind.Goto, target, value, typeof(void));
319 /// Creates a <see cref="GotoExpression"/> representing a goto with the specified type.
320 /// The value passed to the label upon jumping can be specified.
322 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
323 /// <param name="value">The value that will be passed to the associated label upon jumping.</param>
324 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
326 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to Goto,
327 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
328 /// the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>,
329 /// and <paramref name="value"/> to be passed to the target label upon jumping.
331 public static GotoExpression Goto(LabelTarget target, Expression value, Type type) {
332 return MakeGoto(GotoExpressionKind.Goto, target, value, type);
336 /// Creates a <see cref="GotoExpression"/> representing a jump of the specified <see cref="GotoExpressionKind"/>.
337 /// The value passed to the label upon jumping can also be specified.
339 /// <param name="kind">The <see cref="GotoExpressionKind"/> of the <see cref="GotoExpression"/>.</param>
340 /// <param name="target">The <see cref="LabelTarget"/> that the <see cref="GotoExpression"/> will jump to.</param>
341 /// <param name="value">The value that will be passed to the associated label upon jumping.</param>
342 /// <param name="type">An <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
344 /// A <see cref="GotoExpression"/> with <see cref="P:GotoExpression.Kind"/> equal to <paramref name="kind"/>,
345 /// the <see cref="P:GotoExpression.Target"/> property set to <paramref name="target"/>,
346 /// the <see cref="P:Expression.Type"/> property set to <paramref name="type"/>,
347 /// and <paramref name="value"/> to be passed to the target label upon jumping.
349 public static GotoExpression MakeGoto(GotoExpressionKind kind, LabelTarget target, Expression value, Type type) {
350 ValidateGoto(target, ref value, "target", "value");
351 return new GotoExpression(kind, target, value, type);
354 private static void ValidateGoto(LabelTarget target, ref Expression value, string targetParameter, string valueParameter) {
355 ContractUtils.RequiresNotNull(target, targetParameter);
357 if (target.Type != typeof(void)) throw Error.LabelMustBeVoidOrHaveExpression();
359 ValidateGotoType(target.Type, ref value, valueParameter);
363 // Standard argument validation, taken from ValidateArgumentTypes
364 private static void ValidateGotoType(Type expectedType, ref Expression value, string paramName) {
365 RequiresCanRead(value, paramName);
366 if (expectedType != typeof(void)) {
367 if (!TypeUtils.AreReferenceAssignable(expectedType, value.Type)) {
368 // C# autoquotes return values, so we'll do that here
369 if (!TryQuote(expectedType, ref value)) {
370 throw Error.ExpressionTypeDoesNotMatchLabel(value.Type, expectedType);