UnaryExpression.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // UnaryExpression.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System;
  29. using System.Reflection;
  30. #if !FULL_AOT_RUNTIME
  31. using System.Reflection.Emit;
  32. #endif
  33. namespace System.Linq.Expressions {
  34. public sealed class UnaryExpression : Expression {
  35. Expression operand;
  36. MethodInfo method;
  37. bool is_lifted;
  38. public Expression Operand {
  39. get { return operand; }
  40. }
  41. public MethodInfo Method {
  42. get { return method; }
  43. }
  44. public bool IsLifted {
  45. get { return is_lifted; }
  46. }
  47. public bool IsLiftedToNull {
  48. get { return is_lifted && this.Type.IsNullable (); }
  49. }
  50. internal UnaryExpression (ExpressionType node_type, Expression operand, Type type)
  51. : base (node_type, type)
  52. {
  53. this.operand = operand;
  54. }
  55. internal UnaryExpression (ExpressionType node_type, Expression operand, Type type, MethodInfo method, bool is_lifted)
  56. : base (node_type, type)
  57. {
  58. this.operand = operand;
  59. this.method = method;
  60. this.is_lifted = is_lifted;
  61. }
  62. #if !FULL_AOT_RUNTIME
  63. void EmitArrayLength (EmitContext ec)
  64. {
  65. operand.Emit (ec);
  66. ec.ig.Emit (OpCodes.Ldlen);
  67. }
  68. void EmitTypeAs (EmitContext ec)
  69. {
  70. var type = this.Type;
  71. ec.EmitIsInst (operand, type);
  72. if (type.IsNullable ())
  73. ec.ig.Emit (OpCodes.Unbox_Any, type);
  74. }
  75. void EmitLiftedUnary (EmitContext ec)
  76. {
  77. var ig = ec.ig;
  78. var from = ec.EmitStored (operand);
  79. var to = ig.DeclareLocal (Type);
  80. var has_value = ig.DefineLabel ();
  81. var done = ig.DefineLabel ();
  82. ec.EmitNullableHasValue (from);
  83. ig.Emit (OpCodes.Brtrue, has_value);
  84. // if not has value
  85. ec.EmitNullableInitialize (to);
  86. ig.Emit (OpCodes.Br, done);
  87. ig.MarkLabel (has_value);
  88. // if has value
  89. ec.EmitNullableGetValueOrDefault (from);
  90. EmitUnaryOperator (ec);
  91. ec.EmitNullableNew (Type);
  92. ig.MarkLabel (done);
  93. }
  94. void EmitUnaryOperator (EmitContext ec)
  95. {
  96. var ig = ec.ig;
  97. switch (NodeType) {
  98. case ExpressionType.Not:
  99. if (operand.Type.GetNotNullableType () == typeof (bool)) {
  100. ig.Emit (OpCodes.Ldc_I4_0);
  101. ig.Emit (OpCodes.Ceq);
  102. } else
  103. ig.Emit (OpCodes.Not);
  104. break;
  105. case ExpressionType.Negate:
  106. ig.Emit (OpCodes.Neg);
  107. break;
  108. case ExpressionType.NegateChecked:
  109. ig.Emit (OpCodes.Ldc_I4_M1);
  110. ig.Emit (IsUnsigned (operand.Type) ? OpCodes.Mul_Ovf_Un : OpCodes.Mul_Ovf);
  111. break;
  112. case ExpressionType.Convert:
  113. case ExpressionType.ConvertChecked:
  114. // Called when converting from nullable from nullable
  115. EmitPrimitiveConversion (ec,
  116. operand.Type.GetNotNullableType (),
  117. Type.GetNotNullableType ());
  118. break;
  119. }
  120. }
  121. void EmitConvert (EmitContext ec)
  122. {
  123. var from = operand.Type;
  124. var target = Type;
  125. if (from == target)
  126. operand.Emit (ec);
  127. else if (from.IsNullable () && !target.IsNullable ())
  128. EmitConvertFromNullable (ec);
  129. else if (!from.IsNullable () && target.IsNullable ())
  130. EmitConvertToNullable (ec);
  131. else if (from.IsNullable () && target.IsNullable ())
  132. EmitConvertFromNullableToNullable (ec);
  133. else if (IsReferenceConversion (from, target))
  134. EmitCast (ec);
  135. else if (IsPrimitiveConversion (from, target))
  136. EmitPrimitiveConversion (ec);
  137. else
  138. throw new NotImplementedException ();
  139. }
  140. void EmitConvertFromNullableToNullable (EmitContext ec)
  141. {
  142. EmitLiftedUnary (ec);
  143. }
  144. void EmitConvertToNullable (EmitContext ec)
  145. {
  146. ec.Emit (operand);
  147. if (IsUnBoxing ()) {
  148. EmitUnbox (ec);
  149. return;
  150. }
  151. if (operand.Type != Type.GetNotNullableType ()) {
  152. EmitPrimitiveConversion (ec,
  153. operand.Type,
  154. Type.GetNotNullableType ());
  155. }
  156. ec.EmitNullableNew (Type);
  157. }
  158. void EmitConvertFromNullable (EmitContext ec)
  159. {
  160. if (IsBoxing ()) {
  161. ec.Emit (operand);
  162. EmitBox (ec);
  163. return;
  164. }
  165. ec.EmitCall (operand, operand.Type.GetMethod ("get_Value"));
  166. if (operand.Type.GetNotNullableType () != Type) {
  167. EmitPrimitiveConversion (ec,
  168. operand.Type.GetNotNullableType (),
  169. Type);
  170. }
  171. }
  172. bool IsBoxing ()
  173. {
  174. return operand.Type.IsValueType && !Type.IsValueType;
  175. }
  176. void EmitBox (EmitContext ec)
  177. {
  178. ec.ig.Emit (OpCodes.Box, operand.Type);
  179. }
  180. bool IsUnBoxing ()
  181. {
  182. return !operand.Type.IsValueType && Type.IsValueType;
  183. }
  184. void EmitUnbox (EmitContext ec)
  185. {
  186. ec.ig.Emit (OpCodes.Unbox_Any, Type);
  187. }
  188. void EmitCast (EmitContext ec)
  189. {
  190. operand.Emit (ec);
  191. if (IsBoxing ()) {
  192. EmitBox (ec);
  193. } else if (IsUnBoxing ()) {
  194. EmitUnbox (ec);
  195. } else
  196. ec.ig.Emit (OpCodes.Castclass, Type);
  197. }
  198. void EmitPrimitiveConversion (EmitContext ec, bool is_unsigned,
  199. OpCode signed, OpCode unsigned, OpCode signed_checked, OpCode unsigned_checked)
  200. {
  201. if (this.NodeType != ExpressionType.ConvertChecked)
  202. ec.ig.Emit (is_unsigned ? unsigned : signed);
  203. else
  204. ec.ig.Emit (is_unsigned ? unsigned_checked : signed_checked);
  205. }
  206. void EmitPrimitiveConversion (EmitContext ec)
  207. {
  208. operand.Emit (ec);
  209. EmitPrimitiveConversion (ec, operand.Type, Type);
  210. }
  211. void EmitPrimitiveConversion (EmitContext ec, Type from, Type to)
  212. {
  213. var is_unsigned = IsUnsigned (from);
  214. switch (Type.GetTypeCode (to)) {
  215. case TypeCode.SByte:
  216. EmitPrimitiveConversion (ec,
  217. is_unsigned,
  218. OpCodes.Conv_I1,
  219. OpCodes.Conv_U1,
  220. OpCodes.Conv_Ovf_I1,
  221. OpCodes.Conv_Ovf_I1_Un);
  222. return;
  223. case TypeCode.Byte:
  224. EmitPrimitiveConversion (ec,
  225. is_unsigned,
  226. OpCodes.Conv_I1,
  227. OpCodes.Conv_U1,
  228. OpCodes.Conv_Ovf_U1,
  229. OpCodes.Conv_Ovf_U1_Un);
  230. return;
  231. case TypeCode.Int16:
  232. EmitPrimitiveConversion (ec,
  233. is_unsigned,
  234. OpCodes.Conv_I2,
  235. OpCodes.Conv_U2,
  236. OpCodes.Conv_Ovf_I2,
  237. OpCodes.Conv_Ovf_I2_Un);
  238. return;
  239. case TypeCode.UInt16:
  240. EmitPrimitiveConversion (ec,
  241. is_unsigned,
  242. OpCodes.Conv_I2,
  243. OpCodes.Conv_U2,
  244. OpCodes.Conv_Ovf_U2,
  245. OpCodes.Conv_Ovf_U2_Un);
  246. return;
  247. case TypeCode.Int32:
  248. EmitPrimitiveConversion (ec,
  249. is_unsigned,
  250. OpCodes.Conv_I4,
  251. OpCodes.Conv_U4,
  252. OpCodes.Conv_Ovf_I4,
  253. OpCodes.Conv_Ovf_I4_Un);
  254. return;
  255. case TypeCode.UInt32:
  256. EmitPrimitiveConversion (ec,
  257. is_unsigned,
  258. OpCodes.Conv_I4,
  259. OpCodes.Conv_U4,
  260. OpCodes.Conv_Ovf_U4,
  261. OpCodes.Conv_Ovf_U4_Un);
  262. return;
  263. case TypeCode.Int64:
  264. EmitPrimitiveConversion (ec,
  265. is_unsigned,
  266. OpCodes.Conv_I8,
  267. OpCodes.Conv_U8,
  268. OpCodes.Conv_Ovf_I8,
  269. OpCodes.Conv_Ovf_I8_Un);
  270. return;
  271. case TypeCode.UInt64:
  272. EmitPrimitiveConversion (ec,
  273. is_unsigned,
  274. OpCodes.Conv_I8,
  275. OpCodes.Conv_U8,
  276. OpCodes.Conv_Ovf_U8,
  277. OpCodes.Conv_Ovf_U8_Un);
  278. return;
  279. case TypeCode.Single:
  280. if (is_unsigned)
  281. ec.ig.Emit (OpCodes.Conv_R_Un);
  282. ec.ig.Emit (OpCodes.Conv_R4);
  283. return;
  284. case TypeCode.Double:
  285. if (is_unsigned)
  286. ec.ig.Emit (OpCodes.Conv_R_Un);
  287. ec.ig.Emit (OpCodes.Conv_R8);
  288. return;
  289. default:
  290. throw new NotImplementedException (this.Type.ToString ());
  291. }
  292. }
  293. void EmitArithmeticUnary (EmitContext ec)
  294. {
  295. if (!IsLifted) {
  296. operand.Emit (ec);
  297. EmitUnaryOperator (ec);
  298. } else
  299. EmitLiftedUnary (ec);
  300. }
  301. void EmitUserDefinedLiftedToNullOperator (EmitContext ec)
  302. {
  303. var ig = ec.ig;
  304. var local = ec.EmitStored (operand);
  305. var ret = ig.DefineLabel ();
  306. var done = ig.DefineLabel ();
  307. ec.EmitNullableHasValue (local);
  308. ig.Emit (OpCodes.Brfalse, ret);
  309. ec.EmitNullableGetValueOrDefault (local);
  310. ec.EmitCall (method);
  311. ec.EmitNullableNew (Type);
  312. ig.Emit (OpCodes.Br, done);
  313. ig.MarkLabel (ret);
  314. var temp = ig.DeclareLocal (Type);
  315. ec.EmitNullableInitialize (temp);
  316. ig.MarkLabel (done);
  317. }
  318. void EmitUserDefinedLiftedOperator (EmitContext ec)
  319. {
  320. var local = ec.EmitStored (operand);
  321. ec.EmitNullableGetValue (local);
  322. ec.EmitCall (method);
  323. }
  324. void EmitUserDefinedOperator (EmitContext ec)
  325. {
  326. if (!IsLifted) {
  327. ec.Emit (operand);
  328. ec.EmitCall (method);
  329. } else if (IsLiftedToNull) {
  330. EmitUserDefinedLiftedToNullOperator (ec);
  331. } else
  332. EmitUserDefinedLiftedOperator (ec);
  333. }
  334. void EmitQuote (EmitContext ec)
  335. {
  336. ec.EmitScope ();
  337. ec.EmitReadGlobal (operand, typeof (Expression));
  338. if (ec.HasHoistedLocals)
  339. ec.EmitLoadHoistedLocalsStore ();
  340. else
  341. ec.ig.Emit (OpCodes.Ldnull);
  342. ec.EmitIsolateExpression ();
  343. }
  344. internal override void Emit (EmitContext ec)
  345. {
  346. if (method != null) {
  347. EmitUserDefinedOperator (ec);
  348. return;
  349. }
  350. switch (this.NodeType) {
  351. case ExpressionType.ArrayLength:
  352. EmitArrayLength (ec);
  353. return;
  354. case ExpressionType.TypeAs:
  355. EmitTypeAs (ec);
  356. return;
  357. case ExpressionType.Convert:
  358. case ExpressionType.ConvertChecked:
  359. EmitConvert (ec);
  360. return;
  361. case ExpressionType.Not:
  362. case ExpressionType.Negate:
  363. case ExpressionType.NegateChecked:
  364. case ExpressionType.UnaryPlus:
  365. EmitArithmeticUnary (ec);
  366. return;
  367. case ExpressionType.Quote:
  368. EmitQuote (ec);
  369. return;
  370. default:
  371. throw new NotImplementedException (this.NodeType.ToString ());
  372. }
  373. }
  374. #endif
  375. }
  376. }