Merge pull request #1851 from esdrubal/mmf
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / SwitchCase.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.Collections.Generic;
18 using System.Collections.ObjectModel;
19 using System.Diagnostics;
20 using System.Dynamic.Utils;
21
22 #if !FEATURE_CORE_DLR
23 namespace Microsoft.Scripting.Ast {
24 #else
25 namespace System.Linq.Expressions {
26 #endif
27     /// <summary>
28     /// Represents one case of a <see cref="SwitchExpression"/>.
29     /// </summary>
30     [DebuggerTypeProxy(typeof(Expression.SwitchCaseProxy))]
31     public sealed class SwitchCase {
32         private readonly ReadOnlyCollection<Expression> _testValues;
33         private readonly Expression _body;
34
35         internal SwitchCase(Expression body, ReadOnlyCollection<Expression> testValues) {
36             _body = body;
37             _testValues = testValues;
38         }
39
40         /// <summary>
41         /// Gets the values of this case. This case is selected for execution when the <see cref="SwitchExpression.SwitchValue"/> matches any of these values.
42         /// </summary>
43         public ReadOnlyCollection<Expression> TestValues {
44             get { return _testValues; }
45         }
46
47         /// <summary>
48         /// Gets the body of this case.
49         /// </summary>
50         public Expression Body {
51             get { return _body; }
52         }
53
54         /// <summary>
55         /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>. 
56         /// </summary>
57         /// <returns>A <see cref="String"/> that represents the current <see cref="Object"/>. </returns>
58         public override string ToString() {
59             return ExpressionStringBuilder.SwitchCaseToString(this);
60         }
61
62         /// <summary>
63         /// Creates a new expression that is like this one, but using the
64         /// supplied children. If all of the children are the same, it will
65         /// return this expression.
66         /// </summary>
67         /// <param name="testValues">The <see cref="TestValues" /> property of the result.</param>
68         /// <param name="body">The <see cref="Body" /> property of the result.</param>
69         /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
70         public SwitchCase Update(IEnumerable<Expression> testValues, Expression body) {
71             if (testValues == TestValues && body == Body) {
72                 return this;
73             }
74             return Expression.SwitchCase(body, testValues);
75         }
76     }
77
78     public partial class Expression {
79         /// <summary>
80         /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
81         /// </summary>
82         /// <param name="body">The body of the case.</param>
83         /// <param name="testValues">The test values of the case.</param>
84         /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
85         public static SwitchCase SwitchCase(Expression body, params Expression[] testValues) {
86             return SwitchCase(body, (IEnumerable<Expression>)testValues);
87         }
88
89         /// <summary>
90         /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
91         /// </summary>
92         /// <param name="body">The body of the case.</param>
93         /// <param name="testValues">The test values of the case.</param>
94         /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
95         public static SwitchCase SwitchCase(Expression body, IEnumerable<Expression> testValues) {
96             RequiresCanRead(body, "body");
97             
98             var values = testValues.ToReadOnly();
99             RequiresCanRead(values, "testValues");
100             ContractUtils.RequiresNotEmpty(values, "testValues");
101
102             return new SwitchCase(body, values);
103         }
104     }
105 }