5 // Jb Evain (jbevain@novell.com)
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Reflection;
31 using System.Reflection.Emit;
33 namespace System.Linq.Expressions {
35 public sealed class UnaryExpression : Expression {
41 public Expression Operand {
42 get { return operand; }
45 public MethodInfo Method {
46 get { return method; }
49 public bool IsLifted {
50 get { return is_lifted; }
53 public bool IsLiftedToNull {
54 get { return is_lifted && IsNullable (this.Type); }
57 internal UnaryExpression (ExpressionType node_type, Expression operand, Type type)
58 : base (node_type, type)
60 this.operand = operand;
63 internal UnaryExpression (ExpressionType node_type, Expression operand, Type type, MethodInfo method, bool is_lifted)
64 : base (node_type, type)
66 this.operand = operand;
68 this.is_lifted = is_lifted;
71 void EmitArrayLength (EmitContext ec)
74 ec.ig.Emit (OpCodes.Ldlen);
77 void EmitTypeAs (EmitContext ec)
81 ec.EmitIsInst (operand, type);
83 if (IsNullable (type))
84 ec.ig.Emit (OpCodes.Unbox_Any, type);
87 void EmitUnaryOperator (EmitContext ec)
92 case ExpressionType.Not:
93 if (GetNotNullableOf (operand.Type) == typeof (bool)) {
94 ig.Emit (OpCodes.Ldc_I4_0);
95 ig.Emit (OpCodes.Ceq);
97 ig.Emit (OpCodes.Not);
99 case ExpressionType.Negate:
100 case ExpressionType.NegateChecked:
101 ig.Emit (OpCodes.Neg);
106 void EmitConvert (EmitContext ec)
108 if (Type == operand.Type)
116 throw new NotImplementedException ();
121 return CheckReferenceConversion (operand.Type, Type);
126 return operand.Type.IsValueType && !Type.IsValueType;
131 return !operand.Type.IsValueType && Type.IsValueType;
134 void EmitCast (EmitContext ec)
139 ig.Emit (OpCodes.Box, Type);
140 } else if (IsUnBoxing ()) {
141 ig.Emit (OpCodes.Unbox_Any, Type);
143 ec.ig.Emit (OpCodes.Castclass, Type);
146 internal override void Emit (EmitContext ec)
148 switch (this.NodeType) {
149 case ExpressionType.ArrayLength:
150 EmitArrayLength (ec);
152 case ExpressionType.TypeAs:
155 case ExpressionType.Not:
156 case ExpressionType.Negate:
157 case ExpressionType.NegateChecked:
158 case ExpressionType.UnaryPlus:
161 EmitUnaryOperator (ec);
163 throw new NotImplementedException ();
165 case ExpressionType.Convert:
166 case ExpressionType.ConvertChecked:
170 throw new NotImplementedException (this.NodeType.ToString ());