Fix XMM scanning on Mac x86.
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Ast / Utils.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 #if FEATURE_CORE_DLR
17 using System.Linq.Expressions;
18 #else
19 using Microsoft.Scripting.Ast;
20 #endif
21
22 using System;
23 using System.Reflection;
24 using System.Dynamic;
25 using Microsoft.Scripting.Utils;
26 using AstUtils = Microsoft.Scripting.Ast.Utils;
27
28 namespace Microsoft.Scripting.Ast {
29     [Flags]
30     public enum ExpressionAccess {
31         None = 0,
32         Read = 1,
33         Write = 2,
34         ReadWrite = Read | Write,
35     }
36
37     public static partial class Utils {
38         /// <summary>
39         /// Determines whether specified expression type represents an assignment.
40         /// </summary>
41         /// <returns>
42         /// True if the expression type represents an assignment.
43         /// </returns>
44         /// <remarks>
45         /// Note that some other nodes can also assign to variables, members or array items:
46         /// MemberInit, NewArrayInit, Call with ref params, New with ref params, Dynamic with ref params.
47         /// </remarks>
48         public static bool IsAssignment(this ExpressionType type) {
49             return IsWriteOnlyAssignment(type) || IsReadWriteAssignment(type);
50         }
51
52         public static bool IsWriteOnlyAssignment(this ExpressionType type) {
53             return type == ExpressionType.Assign;
54         }
55
56         public static bool IsReadWriteAssignment(this ExpressionType type) {
57             switch (type) {
58                 // unary:
59                 case ExpressionType.PostDecrementAssign:
60                 case ExpressionType.PostIncrementAssign:
61                 case ExpressionType.PreDecrementAssign:
62                 case ExpressionType.PreIncrementAssign:
63
64                 // binary - compound:
65                 case ExpressionType.AddAssign:
66                 case ExpressionType.AddAssignChecked:
67                 case ExpressionType.AndAssign:
68                 case ExpressionType.DivideAssign:
69                 case ExpressionType.ExclusiveOrAssign:
70                 case ExpressionType.LeftShiftAssign:
71                 case ExpressionType.ModuloAssign:
72                 case ExpressionType.MultiplyAssign:
73                 case ExpressionType.MultiplyAssignChecked:
74                 case ExpressionType.OrAssign:
75                 case ExpressionType.PowerAssign:
76                 case ExpressionType.RightShiftAssign:
77                 case ExpressionType.SubtractAssign:
78                 case ExpressionType.SubtractAssignChecked:
79                     return true;
80             }
81             return false;
82         }
83
84         /// <summary>
85         /// Determines if the left child of the given expression is read or written to or both.
86         /// </summary>
87         public static ExpressionAccess GetLValueAccess(this ExpressionType type) {
88             if (type.IsReadWriteAssignment()) {
89                 return ExpressionAccess.ReadWrite;
90             }
91
92             if (type.IsWriteOnlyAssignment()) {
93                 return ExpressionAccess.Write;
94             }
95
96             return ExpressionAccess.Read;
97         }
98
99         public static bool IsLValue(this ExpressionType type) {
100             // see Expression.RequiresCanWrite
101             switch (type) {
102                 case ExpressionType.Index:
103                 case ExpressionType.MemberAccess:
104                 case ExpressionType.Parameter:
105                     return true;
106             }
107
108             return false;
109         }
110     }
111 }