Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / DefaultExpression.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 the default value of a type or an empty expression.
26     /// </summary>
27     [DebuggerTypeProxy(typeof(Expression.DefaultExpressionProxy))]
28     public sealed class DefaultExpression : Expression {
29         private readonly Type _type;
30
31         internal DefaultExpression(Type type) {
32             _type = type;
33         }
34
35         /// <summary>
36         /// Gets the static type of the expression that this <see cref="Expression" /> represents.
37         /// </summary>
38         /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
39         public sealed override Type Type {
40             get { return _type; }
41         }
42
43         /// <summary>
44         /// Returns the node type of this Expression. Extension nodes should return
45         /// ExpressionType.Extension when overriding this method.
46         /// </summary>
47         /// <returns>The <see cref="ExpressionType"/> of the expression.</returns>
48         public sealed override ExpressionType NodeType {
49             get { return ExpressionType.Default; }
50         }
51
52         /// <summary>
53         /// Dispatches to the specific visit method for this node type.
54         /// </summary>
55         protected internal override Expression Accept(ExpressionVisitor visitor) {
56             return visitor.VisitDefault(this);
57         }
58     }
59
60     public partial class Expression {
61         /// <summary>
62         /// Creates an empty expression that has <see cref="System.Void"/> type.
63         /// </summary>
64         /// <returns>
65         /// A <see cref="DefaultExpression"/> that has the <see cref="P:Expression.NodeType"/> property equal to 
66         /// <see cref="F:ExpressionType.Default"/> and the <see cref="P:Expression.Type"/> property set to <see cref="System.Void"/>.
67         /// </returns>
68         public static DefaultExpression Empty() {
69             return new DefaultExpression(typeof(void));
70         }
71
72         /// <summary>
73         /// Creates a <see cref="DefaultExpression"/> that has the <see cref="P:Expression.Type"/> property set to the specified type.
74         /// </summary>
75         /// <param name="type">A <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
76         /// <returns>
77         /// A <see cref="DefaultExpression"/> that has the <see cref="P:Expression.NodeType"/> property equal to 
78         /// <see cref="F:ExpressionType.Default"/> and the <see cref="P:Expression.Type"/> property set to the specified type.
79         /// </returns>
80         public static DefaultExpression Default(Type type) {
81             if (type == typeof(void)) {
82                 return Empty();
83             }
84             return new DefaultExpression(type);
85         }
86     }
87 }