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.Linq.Expressions;
19 using Microsoft.Scripting.Ast;
23 using System.Reflection;
25 using Microsoft.Scripting.Utils;
26 using AstUtils = Microsoft.Scripting.Ast.Utils;
28 namespace Microsoft.Scripting.Ast {
30 public enum ExpressionAccess {
34 ReadWrite = Read | Write,
37 public static partial class Utils {
39 /// Determines whether specified expression type represents an assignment.
42 /// True if the expression type represents an assignment.
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.
48 public static bool IsAssignment(this ExpressionType type) {
49 return IsWriteOnlyAssignment(type) || IsReadWriteAssignment(type);
52 public static bool IsWriteOnlyAssignment(this ExpressionType type) {
53 return type == ExpressionType.Assign;
56 public static bool IsReadWriteAssignment(this ExpressionType type) {
59 case ExpressionType.PostDecrementAssign:
60 case ExpressionType.PostIncrementAssign:
61 case ExpressionType.PreDecrementAssign:
62 case ExpressionType.PreIncrementAssign:
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:
85 /// Determines if the left child of the given expression is read or written to or both.
87 public static ExpressionAccess GetLValueAccess(this ExpressionType type) {
88 if (type.IsReadWriteAssignment()) {
89 return ExpressionAccess.ReadWrite;
92 if (type.IsWriteOnlyAssignment()) {
93 return ExpressionAccess.Write;
96 return ExpressionAccess.Read;
99 public static bool IsLValue(this ExpressionType type) {
100 // see Expression.RequiresCanWrite
102 case ExpressionType.Index:
103 case ExpressionType.MemberAccess:
104 case ExpressionType.Parameter: