BinaryExpression.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. //
  2. // BinaryExpression.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // Contains code from the Mono C# compiler:
  9. // Marek Safar ([email protected])
  10. // Martin Baulig ([email protected])
  11. // Raja Harinath ([email protected])
  12. //
  13. // (C) 2001-2003 Ximian, Inc.
  14. // (C) 2004-2008 Novell, Inc. (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using System;
  36. using System.Reflection;
  37. #if !FULL_AOT_RUNTIME
  38. using System.Reflection.Emit;
  39. #endif
  40. namespace System.Linq.Expressions {
  41. public sealed class BinaryExpression : Expression {
  42. Expression left;
  43. Expression right;
  44. LambdaExpression conversion;
  45. MethodInfo method;
  46. bool lift_to_null, is_lifted;
  47. public Expression Left {
  48. get { return left; }
  49. }
  50. public Expression Right {
  51. get { return right; }
  52. }
  53. public MethodInfo Method {
  54. get { return method; }
  55. }
  56. public bool IsLifted {
  57. get { return is_lifted; }
  58. }
  59. public bool IsLiftedToNull {
  60. get { return lift_to_null; }
  61. }
  62. public LambdaExpression Conversion {
  63. get { return conversion; }
  64. }
  65. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right)
  66. : base (node_type, type)
  67. {
  68. this.left = left;
  69. this.right = right;
  70. }
  71. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, MethodInfo method)
  72. : base (node_type, type)
  73. {
  74. this.left = left;
  75. this.right = right;
  76. this.method = method;
  77. }
  78. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, bool lift_to_null,
  79. bool is_lifted, MethodInfo method, LambdaExpression conversion) : base (node_type, type)
  80. {
  81. this.left = left;
  82. this.right = right;
  83. this.method = method;
  84. this.conversion = conversion;
  85. this.lift_to_null = lift_to_null;
  86. this.is_lifted = is_lifted;
  87. }
  88. #if !FULL_AOT_RUNTIME
  89. void EmitArrayAccess (EmitContext ec)
  90. {
  91. left.Emit (ec);
  92. right.Emit (ec);
  93. ec.ig.Emit (OpCodes.Ldelem, this.Type);
  94. }
  95. void EmitLogicalBinary (EmitContext ec)
  96. {
  97. switch (NodeType) {
  98. case ExpressionType.And:
  99. case ExpressionType.Or:
  100. if (!IsLifted)
  101. EmitLogical (ec);
  102. else if (Type == typeof (bool?))
  103. EmitLiftedLogical (ec);
  104. else
  105. EmitLiftedArithmeticBinary (ec);
  106. break;
  107. case ExpressionType.AndAlso:
  108. case ExpressionType.OrElse:
  109. if (!IsLifted)
  110. EmitLogicalShortCircuit (ec);
  111. else
  112. EmitLiftedLogicalShortCircuit (ec);
  113. break;
  114. }
  115. }
  116. void EmitLogical (EmitContext ec)
  117. {
  118. EmitNonLiftedBinary (ec);
  119. }
  120. void EmitLiftedLogical (EmitContext ec)
  121. {
  122. var ig = ec.ig;
  123. var and = NodeType == ExpressionType.And;
  124. var left = ec.EmitStored (this.left);
  125. var right = ec.EmitStored (this.right);
  126. var ret_from_left = ig.DefineLabel ();
  127. var ret_from_right = ig.DefineLabel ();
  128. var done = ig.DefineLabel ();
  129. ec.EmitNullableGetValueOrDefault (left);
  130. ig.Emit (OpCodes.Brtrue, ret_from_left);
  131. ec.EmitNullableGetValueOrDefault (right);
  132. ig.Emit (OpCodes.Brtrue, ret_from_right);
  133. ec.EmitNullableHasValue (left);
  134. ig.Emit (OpCodes.Brfalse, ret_from_left);
  135. ig.MarkLabel (ret_from_right);
  136. ec.EmitLoad (and ? left : right);
  137. ig.Emit (OpCodes.Br, done);
  138. ig.MarkLabel (ret_from_left);
  139. ec.EmitLoad (and ? right : left);
  140. ig.MarkLabel (done);
  141. }
  142. void EmitLogicalShortCircuit (EmitContext ec)
  143. {
  144. var ig = ec.ig;
  145. var and = NodeType == ExpressionType.AndAlso;
  146. var ret = ig.DefineLabel ();
  147. var done = ig.DefineLabel ();
  148. ec.Emit (left);
  149. ig.Emit (and ? OpCodes.Brfalse : OpCodes.Brtrue, ret);
  150. ec.Emit (right);
  151. ig.Emit (OpCodes.Br, done);
  152. ig.MarkLabel (ret);
  153. ig.Emit (and ? OpCodes.Ldc_I4_0 : OpCodes.Ldc_I4_1);
  154. ig.MarkLabel (done);
  155. }
  156. MethodInfo GetFalseOperator ()
  157. {
  158. return GetFalseOperator (left.Type.GetNotNullableType ());
  159. }
  160. MethodInfo GetTrueOperator ()
  161. {
  162. return GetTrueOperator (left.Type.GetNotNullableType ());
  163. }
  164. void EmitUserDefinedLogicalShortCircuit (EmitContext ec)
  165. {
  166. var ig = ec.ig;
  167. var and = NodeType == ExpressionType.AndAlso;
  168. var done = ig.DefineLabel ();
  169. var left = ec.EmitStored (this.left);
  170. ec.EmitLoad (left);
  171. ig.Emit (OpCodes.Dup);
  172. ec.EmitCall (and ? GetFalseOperator () : GetTrueOperator ());
  173. ig.Emit (OpCodes.Brtrue, done);
  174. ec.Emit (this.right);
  175. ec.EmitCall (method);
  176. ig.MarkLabel (done);
  177. }
  178. void EmitLiftedLogicalShortCircuit (EmitContext ec)
  179. {
  180. var ig = ec.ig;
  181. var and = NodeType == ExpressionType.AndAlso;
  182. var left_is_null = ig.DefineLabel ();
  183. var ret_from_left = ig.DefineLabel ();
  184. var ret_null = ig.DefineLabel ();
  185. var ret_new = ig.DefineLabel();
  186. var done = ig.DefineLabel();
  187. var left = ec.EmitStored (this.left);
  188. ec.EmitNullableHasValue (left);
  189. ig.Emit (OpCodes.Brfalse, left_is_null);
  190. ec.EmitNullableGetValueOrDefault (left);
  191. ig.Emit (OpCodes.Ldc_I4_0);
  192. ig.Emit (OpCodes.Ceq);
  193. ig.Emit (and ? OpCodes.Brtrue : OpCodes.Brfalse, ret_from_left);
  194. ig.MarkLabel (left_is_null);
  195. var right = ec.EmitStored (this.right);
  196. ec.EmitNullableHasValue (right);
  197. ig.Emit (OpCodes.Brfalse_S, ret_null);
  198. ec.EmitNullableGetValueOrDefault (right);
  199. ig.Emit (OpCodes.Ldc_I4_0);
  200. ig.Emit (OpCodes.Ceq);
  201. ig.Emit (and ? OpCodes.Brtrue : OpCodes.Brfalse, ret_from_left);
  202. ec.EmitNullableHasValue (left);
  203. ig.Emit (OpCodes.Brfalse, ret_null);
  204. ig.Emit (and ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
  205. ig.Emit (OpCodes.Br_S, ret_new);
  206. ig.MarkLabel (ret_from_left);
  207. ig.Emit (and ? OpCodes.Ldc_I4_0 : OpCodes.Ldc_I4_1);
  208. ig.MarkLabel (ret_new);
  209. ec.EmitNullableNew (Type);
  210. ig.Emit (OpCodes.Br, done);
  211. ig.MarkLabel (ret_null);
  212. var ret = ig.DeclareLocal (Type);
  213. ec.EmitNullableInitialize (ret);
  214. ig.MarkLabel (done);
  215. }
  216. void EmitCoalesce (EmitContext ec)
  217. {
  218. var ig = ec.ig;
  219. var done = ig.DefineLabel ();
  220. var load_right = ig.DefineLabel ();
  221. var left = ec.EmitStored (this.left);
  222. var left_is_nullable = left.LocalType.IsNullable ();
  223. if (left_is_nullable)
  224. ec.EmitNullableHasValue (left);
  225. else
  226. ec.EmitLoad (left);
  227. ig.Emit (OpCodes.Brfalse, load_right);
  228. if (left_is_nullable && !Type.IsNullable ())
  229. ec.EmitNullableGetValue (left);
  230. else
  231. ec.EmitLoad (left);
  232. ig.Emit (OpCodes.Br, done);
  233. ig.MarkLabel (load_right);
  234. ec.Emit (this.right);
  235. ig.MarkLabel (done);
  236. }
  237. void EmitConvertedCoalesce (EmitContext ec)
  238. {
  239. var ig = ec.ig;
  240. var done = ig.DefineLabel ();
  241. var load_right = ig.DefineLabel ();
  242. var left = ec.EmitStored (this.left);
  243. if (left.LocalType.IsNullable ())
  244. ec.EmitNullableHasValue (left);
  245. else
  246. ec.EmitLoad (left);
  247. ig.Emit (OpCodes.Brfalse, load_right);
  248. ec.Emit (conversion);
  249. ec.EmitLoad (left);
  250. ig.Emit (OpCodes.Callvirt, conversion.Type.GetInvokeMethod ());
  251. ig.Emit (OpCodes.Br, done);
  252. ig.MarkLabel (load_right);
  253. ec.Emit (this.right);
  254. ig.MarkLabel (done);
  255. }
  256. static bool IsInt32OrInt64 (Type type)
  257. {
  258. return type == typeof (int) || type == typeof (long);
  259. }
  260. static bool IsSingleOrDouble (Type type)
  261. {
  262. return type == typeof (float) || type == typeof (double);
  263. }
  264. void EmitBinaryOperator (EmitContext ec)
  265. {
  266. var ig = ec.ig;
  267. bool is_unsigned = IsUnsigned (left.Type);
  268. switch (NodeType) {
  269. case ExpressionType.Add:
  270. ig.Emit (OpCodes.Add);
  271. break;
  272. case ExpressionType.AddChecked:
  273. if (IsInt32OrInt64 (left.Type))
  274. ig.Emit (OpCodes.Add_Ovf);
  275. else
  276. ig.Emit (is_unsigned ? OpCodes.Add_Ovf_Un : OpCodes.Add);
  277. break;
  278. case ExpressionType.Subtract:
  279. ig.Emit (OpCodes.Sub);
  280. break;
  281. case ExpressionType.SubtractChecked:
  282. if (IsInt32OrInt64 (left.Type))
  283. ig.Emit (OpCodes.Sub_Ovf);
  284. else
  285. ig.Emit (is_unsigned ? OpCodes.Sub_Ovf_Un : OpCodes.Sub);
  286. break;
  287. case ExpressionType.Multiply:
  288. ig.Emit (OpCodes.Mul);
  289. break;
  290. case ExpressionType.MultiplyChecked:
  291. if (IsInt32OrInt64 (left.Type))
  292. ig.Emit (OpCodes.Mul_Ovf);
  293. else
  294. ig.Emit (is_unsigned ? OpCodes.Mul_Ovf_Un : OpCodes.Mul);
  295. break;
  296. case ExpressionType.Divide:
  297. ig.Emit (is_unsigned ? OpCodes.Div_Un : OpCodes.Div);
  298. break;
  299. case ExpressionType.Modulo:
  300. ig.Emit (is_unsigned ? OpCodes.Rem_Un : OpCodes.Rem);
  301. break;
  302. case ExpressionType.RightShift:
  303. case ExpressionType.LeftShift:
  304. ig.Emit (OpCodes.Ldc_I4, left.Type == typeof (int) ? 0x1f : 0x3f);
  305. ig.Emit (OpCodes.And);
  306. if (NodeType == ExpressionType.RightShift)
  307. ig.Emit (is_unsigned ? OpCodes.Shr_Un : OpCodes.Shr);
  308. else
  309. ig.Emit (OpCodes.Shl);
  310. break;
  311. case ExpressionType.And:
  312. ig.Emit (OpCodes.And);
  313. break;
  314. case ExpressionType.Or:
  315. ig.Emit (OpCodes.Or);
  316. break;
  317. case ExpressionType.ExclusiveOr:
  318. ig.Emit (OpCodes.Xor);
  319. break;
  320. case ExpressionType.GreaterThan:
  321. ig.Emit (is_unsigned ? OpCodes.Cgt_Un : OpCodes.Cgt);
  322. break;
  323. case ExpressionType.GreaterThanOrEqual:
  324. if (is_unsigned || IsSingleOrDouble (left.Type))
  325. ig.Emit (OpCodes.Clt_Un);
  326. else
  327. ig.Emit (OpCodes.Clt);
  328. ig.Emit (OpCodes.Ldc_I4_0);
  329. ig.Emit (OpCodes.Ceq);
  330. break;
  331. case ExpressionType.LessThan:
  332. ig.Emit (is_unsigned ? OpCodes.Clt_Un : OpCodes.Clt);
  333. break;
  334. case ExpressionType.LessThanOrEqual:
  335. if (is_unsigned || IsSingleOrDouble (left.Type))
  336. ig.Emit (OpCodes.Cgt_Un);
  337. else
  338. ig.Emit (OpCodes.Cgt);
  339. ig.Emit (OpCodes.Ldc_I4_0);
  340. ig.Emit (OpCodes.Ceq);
  341. break;
  342. case ExpressionType.Equal:
  343. ig.Emit (OpCodes.Ceq);
  344. break;
  345. case ExpressionType.NotEqual:
  346. ig.Emit (OpCodes.Ceq);
  347. ig.Emit (OpCodes.Ldc_I4_0);
  348. ig.Emit (OpCodes.Ceq);
  349. break;
  350. case ExpressionType.Power:
  351. ig.Emit (OpCodes.Call, typeof (Math).GetMethod ("Pow"));
  352. break;
  353. default:
  354. throw new InvalidOperationException (
  355. string.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
  356. }
  357. }
  358. bool IsLeftLiftedBinary ()
  359. {
  360. return left.Type.IsNullable () && !right.Type.IsNullable ();
  361. }
  362. void EmitLeftLiftedToNullBinary (EmitContext ec)
  363. {
  364. var ig = ec.ig;
  365. var ret = ig.DefineLabel ();
  366. var done = ig.DefineLabel ();
  367. var left = ec.EmitStored (this.left);
  368. ec.EmitNullableHasValue (left);
  369. ig.Emit (OpCodes.Brfalse, ret);
  370. ec.EmitNullableGetValueOrDefault (left);
  371. ec.Emit (right);
  372. EmitBinaryOperator (ec);
  373. ec.EmitNullableNew (Type);
  374. ig.Emit (OpCodes.Br, done);
  375. ig.MarkLabel (ret);
  376. var temp = ig.DeclareLocal (Type);
  377. ec.EmitNullableInitialize (temp);
  378. ig.MarkLabel (done);
  379. }
  380. void EmitLiftedArithmeticBinary (EmitContext ec)
  381. {
  382. if (IsLeftLiftedBinary ())
  383. EmitLeftLiftedToNullBinary (ec);
  384. else
  385. EmitLiftedToNullBinary (ec);
  386. }
  387. void EmitLiftedToNullBinary (EmitContext ec)
  388. {
  389. var ig = ec.ig;
  390. var left = ec.EmitStored (this.left);
  391. var right = ec.EmitStored (this.right);
  392. var result = ig.DeclareLocal (Type);
  393. var has_value = ig.DefineLabel ();
  394. var done = ig.DefineLabel ();
  395. ec.EmitNullableHasValue (left);
  396. ec.EmitNullableHasValue (right);
  397. ig.Emit (OpCodes.And);
  398. ig.Emit (OpCodes.Brtrue, has_value);
  399. ec.EmitNullableInitialize (result);
  400. ig.Emit (OpCodes.Br, done);
  401. ig.MarkLabel (has_value);
  402. ec.EmitNullableGetValueOrDefault (left);
  403. ec.EmitNullableGetValueOrDefault (right);
  404. EmitBinaryOperator (ec);
  405. ec.EmitNullableNew (result.LocalType);
  406. ig.MarkLabel (done);
  407. }
  408. void EmitLiftedRelationalBinary (EmitContext ec)
  409. {
  410. var ig = ec.ig;
  411. var left = ec.EmitStored (this.left);
  412. var right = ec.EmitStored (this.right);
  413. var ret = ig.DefineLabel ();
  414. var done = ig.DefineLabel ();
  415. ec.EmitNullableGetValueOrDefault (left);
  416. ec.EmitNullableGetValueOrDefault (right);
  417. switch (NodeType) {
  418. case ExpressionType.Equal:
  419. case ExpressionType.NotEqual:
  420. ig.Emit (OpCodes.Bne_Un, ret);
  421. break;
  422. default:
  423. EmitBinaryOperator (ec);
  424. ig.Emit (OpCodes.Brfalse, ret);
  425. break;
  426. }
  427. ec.EmitNullableHasValue (left);
  428. ec.EmitNullableHasValue (right);
  429. switch (NodeType) {
  430. case ExpressionType.Equal:
  431. ig.Emit (OpCodes.Ceq);
  432. break;
  433. case ExpressionType.NotEqual:
  434. ig.Emit (OpCodes.Ceq);
  435. ig.Emit (OpCodes.Ldc_I4_0);
  436. ig.Emit (OpCodes.Ceq);
  437. break;
  438. default:
  439. ig.Emit (OpCodes.And);
  440. break;
  441. }
  442. ig.Emit (OpCodes.Br, done);
  443. ig.MarkLabel (ret);
  444. ig.Emit (NodeType == ExpressionType.NotEqual ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
  445. ig.MarkLabel (done);
  446. }
  447. void EmitArithmeticBinary (EmitContext ec)
  448. {
  449. if (!IsLifted)
  450. EmitNonLiftedBinary (ec);
  451. else
  452. EmitLiftedArithmeticBinary (ec);
  453. }
  454. void EmitNonLiftedBinary (EmitContext ec)
  455. {
  456. ec.Emit (left);
  457. ec.Emit (right);
  458. EmitBinaryOperator (ec);
  459. }
  460. void EmitRelationalBinary (EmitContext ec)
  461. {
  462. if (!IsLifted) {
  463. EmitNonLiftedBinary (ec);
  464. return;
  465. }
  466. if (IsLiftedToNull) {
  467. EmitLiftedToNullBinary (ec);
  468. return;
  469. }
  470. if (ConstantExpression.IsNull (right) && !ConstantExpression.IsNull (left) && left.Type.IsNullable ()) {
  471. EmitNullEquality (ec, left);
  472. return;
  473. }
  474. if (ConstantExpression.IsNull (left) && !ConstantExpression.IsNull (right) && right.Type.IsNullable ()) {
  475. EmitNullEquality (ec, right);
  476. return;
  477. }
  478. EmitLiftedRelationalBinary (ec);
  479. }
  480. void EmitNullEquality (EmitContext ec, Expression e)
  481. {
  482. var ig = ec.ig;
  483. if (IsLiftedToNull) {
  484. e.Emit (ec);
  485. if (e.Type != typeof (void))
  486. ig.Emit (OpCodes.Pop);
  487. ec.EmitNullableNew (typeof (bool?));
  488. return;
  489. }
  490. var se = ec.EmitStored (e);
  491. ec.EmitNullableHasValue (se);
  492. if (NodeType == ExpressionType.Equal) {
  493. ig.Emit (OpCodes.Ldc_I4_0);
  494. ig.Emit (OpCodes.Ceq);
  495. }
  496. }
  497. void EmitLiftedUserDefinedOperator (EmitContext ec)
  498. {
  499. var ig = ec.ig;
  500. var ret_true = ig.DefineLabel ();
  501. var ret_false = ig.DefineLabel ();
  502. var done = ig.DefineLabel ();
  503. var left = ec.EmitStored (this.left);
  504. var right = ec.EmitStored (this.right);
  505. ec.EmitNullableHasValue (left);
  506. ec.EmitNullableHasValue (right);
  507. switch (NodeType) {
  508. case ExpressionType.Equal:
  509. ig.Emit (OpCodes.Bne_Un, ret_false);
  510. ec.EmitNullableHasValue (left);
  511. ig.Emit (OpCodes.Brfalse, ret_true);
  512. break;
  513. case ExpressionType.NotEqual:
  514. ig.Emit (OpCodes.Bne_Un, ret_true);
  515. ec.EmitNullableHasValue (left);
  516. ig.Emit (OpCodes.Brfalse, ret_false);
  517. break;
  518. default:
  519. ig.Emit (OpCodes.And);
  520. ig.Emit (OpCodes.Brfalse, ret_false);
  521. break;
  522. }
  523. ec.EmitNullableGetValueOrDefault (left);
  524. ec.EmitNullableGetValueOrDefault (right);
  525. ec.EmitCall (method);
  526. ig.Emit (OpCodes.Br, done);
  527. ig.MarkLabel (ret_true);
  528. ig.Emit (OpCodes.Ldc_I4_1);
  529. ig.Emit (OpCodes.Br, done);
  530. ig.MarkLabel (ret_false);
  531. ig.Emit (OpCodes.Ldc_I4_0);
  532. ig.Emit (OpCodes.Br, done);
  533. ig.MarkLabel (done);
  534. }
  535. void EmitLiftedToNullUserDefinedOperator (EmitContext ec)
  536. {
  537. var ig = ec.ig;
  538. var ret = ig.DefineLabel ();
  539. var done = ig.DefineLabel ();
  540. var left = ec.EmitStored (this.left);
  541. var right = ec.EmitStored (this.right);
  542. ec.EmitNullableHasValue (left);
  543. ec.EmitNullableHasValue (right);
  544. ig.Emit (OpCodes.And);
  545. ig.Emit (OpCodes.Brfalse, ret);
  546. ec.EmitNullableGetValueOrDefault (left);
  547. ec.EmitNullableGetValueOrDefault (right);
  548. ec.EmitCall (method);
  549. ec.EmitNullableNew (Type);
  550. ig.Emit (OpCodes.Br, done);
  551. ig.MarkLabel (ret);
  552. var temp = ig.DeclareLocal (Type);
  553. ec.EmitNullableInitialize (temp);
  554. ig.MarkLabel (done);
  555. }
  556. void EmitUserDefinedLiftedLogicalShortCircuit (EmitContext ec)
  557. {
  558. var ig = ec.ig;
  559. var and = NodeType == ExpressionType.AndAlso;
  560. var left_is_null = ig.DefineLabel ();
  561. var ret_left = ig.DefineLabel ();
  562. var ret_null = ig.DefineLabel ();
  563. var done = ig.DefineLabel ();
  564. var left = ec.EmitStored (this.left);
  565. ec.EmitNullableHasValue (left);
  566. ig.Emit (OpCodes.Brfalse, and ? ret_null : left_is_null);
  567. ec.EmitNullableGetValueOrDefault (left);
  568. ec.EmitCall (and ? GetFalseOperator () : GetTrueOperator ());
  569. ig.Emit (OpCodes.Brtrue, ret_left);
  570. ig.MarkLabel (left_is_null);
  571. var right = ec.EmitStored (this.right);
  572. ec.EmitNullableHasValue (right);
  573. ig.Emit (OpCodes.Brfalse, ret_null);
  574. ec.EmitNullableGetValueOrDefault (left);
  575. ec.EmitNullableGetValueOrDefault (right);
  576. ec.EmitCall (method);
  577. ec.EmitNullableNew (Type);
  578. ig.Emit (OpCodes.Br, done);
  579. ig.MarkLabel (ret_left);
  580. ec.EmitLoad (left);
  581. ig.Emit (OpCodes.Br, done);
  582. ig.MarkLabel (ret_null);
  583. var ret = ig.DeclareLocal (Type);
  584. ec.EmitNullableInitialize (ret);
  585. ig.MarkLabel (done);
  586. }
  587. void EmitUserDefinedOperator (EmitContext ec)
  588. {
  589. if (!IsLifted) {
  590. switch (NodeType) {
  591. case ExpressionType.AndAlso:
  592. case ExpressionType.OrElse:
  593. EmitUserDefinedLogicalShortCircuit (ec);
  594. break;
  595. default:
  596. left.Emit (ec);
  597. right.Emit (ec);
  598. ec.EmitCall (method);
  599. break;
  600. }
  601. } else if (IsLiftedToNull) {
  602. switch (NodeType) {
  603. case ExpressionType.AndAlso:
  604. case ExpressionType.OrElse:
  605. EmitUserDefinedLiftedLogicalShortCircuit (ec);
  606. break;
  607. default:
  608. EmitLiftedToNullUserDefinedOperator (ec);
  609. break;
  610. }
  611. } else
  612. EmitLiftedUserDefinedOperator (ec);
  613. }
  614. internal override void Emit (EmitContext ec)
  615. {
  616. if (method != null) {
  617. EmitUserDefinedOperator (ec);
  618. return;
  619. }
  620. switch (NodeType){
  621. case ExpressionType.ArrayIndex:
  622. EmitArrayAccess (ec);
  623. return;
  624. case ExpressionType.Coalesce:
  625. if (conversion != null)
  626. EmitConvertedCoalesce (ec);
  627. else
  628. EmitCoalesce (ec);
  629. return;
  630. case ExpressionType.Power:
  631. case ExpressionType.Add:
  632. case ExpressionType.AddChecked:
  633. case ExpressionType.Divide:
  634. case ExpressionType.ExclusiveOr:
  635. case ExpressionType.LeftShift:
  636. case ExpressionType.Modulo:
  637. case ExpressionType.Multiply:
  638. case ExpressionType.MultiplyChecked:
  639. case ExpressionType.RightShift:
  640. case ExpressionType.Subtract:
  641. case ExpressionType.SubtractChecked:
  642. EmitArithmeticBinary (ec);
  643. return;
  644. case ExpressionType.Equal:
  645. case ExpressionType.GreaterThan:
  646. case ExpressionType.GreaterThanOrEqual:
  647. case ExpressionType.LessThan:
  648. case ExpressionType.LessThanOrEqual:
  649. case ExpressionType.NotEqual:
  650. EmitRelationalBinary (ec);
  651. return;
  652. case ExpressionType.And:
  653. case ExpressionType.Or:
  654. case ExpressionType.AndAlso:
  655. case ExpressionType.OrElse:
  656. EmitLogicalBinary (ec);
  657. return;
  658. default:
  659. throw new NotSupportedException (this.NodeType.ToString ());
  660. }
  661. }
  662. #endif
  663. }
  664. }