BinaryExpression.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. using System.Reflection.Emit;
  38. namespace System.Linq.Expressions {
  39. public sealed class BinaryExpression : Expression {
  40. Expression left;
  41. Expression right;
  42. LambdaExpression conversion;
  43. MethodInfo method;
  44. bool lift_to_null, is_lifted;
  45. public Expression Left {
  46. get { return left; }
  47. }
  48. public Expression Right {
  49. get { return right; }
  50. }
  51. public MethodInfo Method {
  52. get { return method; }
  53. }
  54. public bool IsLifted {
  55. get { return is_lifted; }
  56. }
  57. public bool IsLiftedToNull {
  58. get { return lift_to_null; }
  59. }
  60. public LambdaExpression Conversion {
  61. get { return conversion; }
  62. }
  63. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right)
  64. : base (node_type, type)
  65. {
  66. this.left = left;
  67. this.right = right;
  68. }
  69. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, MethodInfo method)
  70. : base (node_type, type)
  71. {
  72. this.left = left;
  73. this.right = right;
  74. this.method = method;
  75. }
  76. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, bool lift_to_null,
  77. bool is_lifted, MethodInfo method, LambdaExpression conversion) : base (node_type, type)
  78. {
  79. this.left = left;
  80. this.right = right;
  81. this.method = method;
  82. this.conversion = conversion;
  83. this.lift_to_null = lift_to_null;
  84. this.is_lifted = is_lifted;
  85. }
  86. void EmitArrayAccess (EmitContext ec)
  87. {
  88. left.Emit (ec);
  89. right.Emit (ec);
  90. ec.ig.Emit (OpCodes.Ldelem, this.Type);
  91. }
  92. void EmitLogicalBinary (EmitContext ec)
  93. {
  94. switch (NodeType) {
  95. case ExpressionType.And:
  96. case ExpressionType.Or:
  97. if (!IsLifted)
  98. EmitLogical (ec);
  99. else if (Type == typeof (bool?))
  100. EmitLiftedLogical (ec);
  101. else
  102. EmitLiftedArithmeticBinary (ec);
  103. break;
  104. case ExpressionType.AndAlso:
  105. case ExpressionType.OrElse:
  106. if (!IsLifted)
  107. EmitLogicalShortCircuit (ec);
  108. else
  109. EmitLiftedLogicalShortCircuit (ec);
  110. break;
  111. }
  112. }
  113. void EmitLogical (EmitContext ec)
  114. {
  115. EmitNonLiftedBinary (ec);
  116. }
  117. void EmitLiftedLogical (EmitContext ec)
  118. {
  119. var ig = ec.ig;
  120. var and = NodeType == ExpressionType.And;
  121. var left = ec.EmitStored (this.left);
  122. var right = ec.EmitStored (this.right);
  123. var ret_from_left = ig.DefineLabel ();
  124. var ret_from_right = ig.DefineLabel ();
  125. var done = ig.DefineLabel ();
  126. ec.EmitNullableGetValueOrDefault (left);
  127. ig.Emit (OpCodes.Brtrue, ret_from_left);
  128. ec.EmitNullableGetValueOrDefault (right);
  129. ig.Emit (OpCodes.Brtrue, ret_from_right);
  130. ec.EmitNullableHasValue (left);
  131. ig.Emit (OpCodes.Brfalse, ret_from_left);
  132. ig.MarkLabel (ret_from_right);
  133. ec.EmitLoad (and ? left : right);
  134. ig.Emit (OpCodes.Br, done);
  135. ig.MarkLabel (ret_from_left);
  136. ec.EmitLoad (and ? right : left);
  137. ig.MarkLabel (done);
  138. }
  139. void EmitLogicalShortCircuit (EmitContext ec)
  140. {
  141. var ig = ec.ig;
  142. var and = NodeType == ExpressionType.AndAlso;
  143. var ret = ig.DefineLabel ();
  144. var done = ig.DefineLabel ();
  145. ec.Emit (left);
  146. ig.Emit (and ? OpCodes.Brfalse : OpCodes.Brtrue, ret);
  147. ec.Emit (right);
  148. ig.Emit (OpCodes.Br, done);
  149. ig.MarkLabel (ret);
  150. ig.Emit (and ? OpCodes.Ldc_I4_0 : OpCodes.Ldc_I4_1);
  151. ig.MarkLabel (done);
  152. }
  153. MethodInfo GetFalseOperator ()
  154. {
  155. return GetNotNullableOf (left.Type).GetMethod ("op_False", AllStatic);
  156. }
  157. MethodInfo GetTrueOperator ()
  158. {
  159. return GetNotNullableOf (left.Type).GetMethod ("op_True", AllStatic);
  160. }
  161. void EmitUserDefinedLogicalShortCircuit (EmitContext ec)
  162. {
  163. var ig = ec.ig;
  164. var and = NodeType == ExpressionType.AndAlso;
  165. var done = ig.DefineLabel ();
  166. var left = ec.EmitStored (this.left);
  167. ec.EmitLoad (left);
  168. ig.Emit (OpCodes.Dup);
  169. ec.EmitCall (and ? GetFalseOperator () : GetTrueOperator ());
  170. ig.Emit (OpCodes.Brtrue, done);
  171. ec.Emit (this.right);
  172. ec.EmitCall (method);
  173. ig.MarkLabel (done);
  174. }
  175. void EmitLiftedLogicalShortCircuit (EmitContext ec)
  176. {
  177. var ig = ec.ig;
  178. var and = NodeType == ExpressionType.AndAlso;
  179. var left_is_null = ig.DefineLabel ();
  180. var ret_from_left = ig.DefineLabel ();
  181. var ret_null = ig.DefineLabel ();
  182. var ret_new = ig.DefineLabel();
  183. var done = ig.DefineLabel();
  184. var left = ec.EmitStored (this.left);
  185. ec.EmitNullableHasValue (left);
  186. ig.Emit (OpCodes.Brfalse, left_is_null);
  187. ec.EmitNullableGetValueOrDefault (left);
  188. ig.Emit (OpCodes.Ldc_I4_0);
  189. ig.Emit (OpCodes.Ceq);
  190. ig.Emit (and ? OpCodes.Brtrue : OpCodes.Brfalse, ret_from_left);
  191. ig.MarkLabel (left_is_null);
  192. var right = ec.EmitStored (this.right);
  193. ec.EmitNullableHasValue (right);
  194. ig.Emit (OpCodes.Brfalse_S, ret_null);
  195. ec.EmitNullableGetValueOrDefault (right);
  196. ig.Emit (OpCodes.Ldc_I4_0);
  197. ig.Emit (OpCodes.Ceq);
  198. ig.Emit (and ? OpCodes.Brtrue : OpCodes.Brfalse, ret_from_left);
  199. ec.EmitNullableHasValue (left);
  200. ig.Emit (OpCodes.Brfalse, ret_null);
  201. ig.Emit (and ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
  202. ig.Emit (OpCodes.Br_S, ret_new);
  203. ig.MarkLabel (ret_from_left);
  204. ig.Emit (and ? OpCodes.Ldc_I4_0 : OpCodes.Ldc_I4_1);
  205. ig.MarkLabel (ret_new);
  206. ec.EmitNullableNew (Type);
  207. ig.Emit (OpCodes.Br, done);
  208. ig.MarkLabel (ret_null);
  209. var ret = ig.DeclareLocal (Type);
  210. ec.EmitNullableInitialize (ret);
  211. ig.MarkLabel (done);
  212. }
  213. void EmitCoalesce (EmitContext ec)
  214. {
  215. var ig = ec.ig;
  216. var done = ig.DefineLabel ();
  217. var load_right = ig.DefineLabel ();
  218. var left = ec.EmitStored (this.left);
  219. var left_is_nullable = left.LocalType.IsNullable ();
  220. if (left_is_nullable)
  221. ec.EmitNullableHasValue (left);
  222. else
  223. ec.EmitLoad (left);
  224. ig.Emit (OpCodes.Brfalse, load_right);
  225. if (left_is_nullable && !Type.IsNullable ())
  226. ec.EmitNullableGetValue (left);
  227. else
  228. ec.EmitLoad (left);
  229. ig.Emit (OpCodes.Br, done);
  230. ig.MarkLabel (load_right);
  231. ec.Emit (this.right);
  232. ig.MarkLabel (done);
  233. }
  234. void EmitConvertedCoalesce (EmitContext ec)
  235. {
  236. var ig = ec.ig;
  237. var done = ig.DefineLabel ();
  238. var load_right = ig.DefineLabel ();
  239. var left = ec.EmitStored (this.left);
  240. if (left.LocalType.IsNullable ())
  241. ec.EmitNullableHasValue (left);
  242. else
  243. ec.EmitLoad (left);
  244. ig.Emit (OpCodes.Brfalse, load_right);
  245. // is it the right way to do it?
  246. ec.EmitReadGlobal (conversion.Compile ());
  247. ec.EmitLoad (left);
  248. ig.Emit (OpCodes.Callvirt, conversion.Type.GetInvokeMethod ());
  249. ig.Emit (OpCodes.Br, done);
  250. ig.MarkLabel (load_right);
  251. ec.Emit (this.right);
  252. ig.MarkLabel (done);
  253. }
  254. static bool IsInt32OrInt64 (Type type)
  255. {
  256. return type == typeof (int) || type == typeof (long);
  257. }
  258. static bool IsSingleOrDouble (Type type)
  259. {
  260. return type == typeof (float) || type == typeof (double);
  261. }
  262. void EmitBinaryOperator (EmitContext ec)
  263. {
  264. var ig = ec.ig;
  265. bool is_unsigned = IsUnsigned (left.Type);
  266. switch (NodeType) {
  267. case ExpressionType.Add:
  268. ig.Emit (OpCodes.Add);
  269. break;
  270. case ExpressionType.AddChecked:
  271. if (IsInt32OrInt64 (left.Type))
  272. ig.Emit (OpCodes.Add_Ovf);
  273. else
  274. ig.Emit (is_unsigned ? OpCodes.Add_Ovf_Un : OpCodes.Add);
  275. break;
  276. case ExpressionType.Subtract:
  277. ig.Emit (OpCodes.Sub);
  278. break;
  279. case ExpressionType.SubtractChecked:
  280. if (IsInt32OrInt64 (left.Type))
  281. ig.Emit (OpCodes.Sub_Ovf);
  282. else
  283. ig.Emit (is_unsigned ? OpCodes.Sub_Ovf_Un : OpCodes.Sub);
  284. break;
  285. case ExpressionType.Multiply:
  286. ig.Emit (OpCodes.Mul);
  287. break;
  288. case ExpressionType.MultiplyChecked:
  289. if (IsInt32OrInt64 (left.Type))
  290. ig.Emit (OpCodes.Mul_Ovf);
  291. else
  292. ig.Emit (is_unsigned ? OpCodes.Mul_Ovf_Un : OpCodes.Mul);
  293. break;
  294. case ExpressionType.Divide:
  295. ig.Emit (is_unsigned ? OpCodes.Div_Un : OpCodes.Div);
  296. break;
  297. case ExpressionType.Modulo:
  298. ig.Emit (is_unsigned ? OpCodes.Rem_Un : OpCodes.Rem);
  299. break;
  300. case ExpressionType.RightShift:
  301. ig.Emit (is_unsigned ? OpCodes.Shr_Un : OpCodes.Shr);
  302. break;
  303. case ExpressionType.LeftShift:
  304. ig.Emit (OpCodes.Shl);
  305. break;
  306. case ExpressionType.And:
  307. ig.Emit (OpCodes.And);
  308. break;
  309. case ExpressionType.Or:
  310. ig.Emit (OpCodes.Or);
  311. break;
  312. case ExpressionType.ExclusiveOr:
  313. ig.Emit (OpCodes.Xor);
  314. break;
  315. case ExpressionType.GreaterThan:
  316. ig.Emit (is_unsigned ? OpCodes.Cgt_Un : OpCodes.Cgt);
  317. break;
  318. case ExpressionType.GreaterThanOrEqual:
  319. if (is_unsigned || IsSingleOrDouble (left.Type))
  320. ig.Emit (OpCodes.Clt_Un);
  321. else
  322. ig.Emit (OpCodes.Clt);
  323. ig.Emit (OpCodes.Ldc_I4_0);
  324. ig.Emit (OpCodes.Ceq);
  325. break;
  326. case ExpressionType.LessThan:
  327. ig.Emit (is_unsigned ? OpCodes.Clt_Un : OpCodes.Clt);
  328. break;
  329. case ExpressionType.LessThanOrEqual:
  330. if (is_unsigned || IsSingleOrDouble (left.Type))
  331. ig.Emit (OpCodes.Cgt_Un);
  332. else
  333. ig.Emit (OpCodes.Cgt);
  334. ig.Emit (OpCodes.Ldc_I4_0);
  335. ig.Emit (OpCodes.Ceq);
  336. break;
  337. case ExpressionType.Equal:
  338. ig.Emit (OpCodes.Ceq);
  339. break;
  340. case ExpressionType.NotEqual:
  341. ig.Emit (OpCodes.Ceq);
  342. ig.Emit (OpCodes.Ldc_I4_0);
  343. ig.Emit (OpCodes.Ceq);
  344. break;
  345. case ExpressionType.Power:
  346. ig.Emit (OpCodes.Call, typeof (Math).GetMethod ("Pow"));
  347. break;
  348. default:
  349. throw new InvalidOperationException (
  350. string.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
  351. }
  352. }
  353. bool IsLeftLiftedBinary ()
  354. {
  355. return left.Type.IsNullable () && !right.Type.IsNullable ();
  356. }
  357. void EmitLeftLiftedToNullBinary (EmitContext ec)
  358. {
  359. var ig = ec.ig;
  360. var ret = ig.DefineLabel ();
  361. var done = ig.DefineLabel ();
  362. var left = ec.EmitStored (this.left);
  363. ec.EmitNullableHasValue (left);
  364. ig.Emit (OpCodes.Brfalse, ret);
  365. ec.EmitNullableGetValueOrDefault (left);
  366. ec.Emit (right);
  367. EmitBinaryOperator (ec);
  368. ec.EmitNullableNew (Type);
  369. ig.Emit (OpCodes.Br, done);
  370. ig.MarkLabel (ret);
  371. var temp = ig.DeclareLocal (Type);
  372. ec.EmitNullableInitialize (temp);
  373. ig.MarkLabel (done);
  374. }
  375. void EmitLiftedArithmeticBinary (EmitContext ec)
  376. {
  377. if (IsLeftLiftedBinary ())
  378. EmitLeftLiftedToNullBinary (ec);
  379. else
  380. EmitLiftedToNullBinary (ec);
  381. }
  382. void EmitLiftedToNullBinary (EmitContext ec)
  383. {
  384. var ig = ec.ig;
  385. var left = ec.EmitStored (this.left);
  386. var right = ec.EmitStored (this.right);
  387. var result = ig.DeclareLocal (Type);
  388. var has_value = ig.DefineLabel ();
  389. var done = ig.DefineLabel ();
  390. ec.EmitNullableHasValue (left);
  391. ec.EmitNullableHasValue (right);
  392. ig.Emit (OpCodes.And);
  393. ig.Emit (OpCodes.Brtrue, has_value);
  394. ec.EmitNullableInitialize (result);
  395. ig.Emit (OpCodes.Br, done);
  396. ig.MarkLabel (has_value);
  397. ec.EmitNullableGetValueOrDefault (left);
  398. ec.EmitNullableGetValueOrDefault (right);
  399. EmitBinaryOperator (ec);
  400. ec.EmitNullableNew (result.LocalType);
  401. ig.MarkLabel (done);
  402. }
  403. void EmitLiftedRelationalBinary (EmitContext ec)
  404. {
  405. var ig = ec.ig;
  406. var left = ec.EmitStored (this.left);
  407. var right = ec.EmitStored (this.right);
  408. var ret = ig.DefineLabel ();
  409. var done = ig.DefineLabel ();
  410. ec.EmitNullableGetValueOrDefault (left);
  411. ec.EmitNullableGetValueOrDefault (right);
  412. switch (NodeType) {
  413. case ExpressionType.Equal:
  414. case ExpressionType.NotEqual:
  415. ig.Emit (OpCodes.Bne_Un, ret);
  416. break;
  417. default:
  418. EmitBinaryOperator (ec);
  419. ig.Emit (OpCodes.Brfalse, ret);
  420. break;
  421. }
  422. ec.EmitNullableHasValue (left);
  423. ec.EmitNullableHasValue (right);
  424. switch (NodeType) {
  425. case ExpressionType.Equal:
  426. ig.Emit (OpCodes.Ceq);
  427. break;
  428. case ExpressionType.NotEqual:
  429. ig.Emit (OpCodes.Ceq);
  430. ig.Emit (OpCodes.Ldc_I4_0);
  431. ig.Emit (OpCodes.Ceq);
  432. break;
  433. default:
  434. ig.Emit (OpCodes.And);
  435. break;
  436. }
  437. ig.Emit (OpCodes.Br, done);
  438. ig.MarkLabel (ret);
  439. ig.Emit (NodeType == ExpressionType.NotEqual ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
  440. ig.MarkLabel (done);
  441. }
  442. void EmitArithmeticBinary (EmitContext ec)
  443. {
  444. if (!IsLifted)
  445. EmitNonLiftedBinary (ec);
  446. else
  447. EmitLiftedArithmeticBinary (ec);
  448. }
  449. void EmitNonLiftedBinary (EmitContext ec)
  450. {
  451. ec.Emit (left);
  452. ec.Emit (right);
  453. EmitBinaryOperator (ec);
  454. }
  455. void EmitRelationalBinary (EmitContext ec)
  456. {
  457. if (!IsLifted)
  458. EmitNonLiftedBinary (ec);
  459. else if (IsLiftedToNull)
  460. EmitLiftedToNullBinary (ec);
  461. else
  462. EmitLiftedRelationalBinary (ec);
  463. }
  464. void EmitLiftedUserDefinedOperator (EmitContext ec)
  465. {
  466. var ig = ec.ig;
  467. var ret_true = ig.DefineLabel ();
  468. var ret_false = ig.DefineLabel ();
  469. var done = ig.DefineLabel ();
  470. var left = ec.EmitStored (this.left);
  471. var right = ec.EmitStored (this.right);
  472. ec.EmitNullableHasValue (left);
  473. ec.EmitNullableHasValue (right);
  474. switch (NodeType) {
  475. case ExpressionType.Equal:
  476. ig.Emit (OpCodes.Bne_Un, ret_false);
  477. ec.EmitNullableHasValue (left);
  478. ig.Emit (OpCodes.Brfalse, ret_true);
  479. break;
  480. case ExpressionType.NotEqual:
  481. ig.Emit (OpCodes.Bne_Un, ret_true);
  482. ec.EmitNullableHasValue (left);
  483. ig.Emit (OpCodes.Brfalse, ret_false);
  484. break;
  485. default:
  486. ig.Emit (OpCodes.And);
  487. ig.Emit (OpCodes.Brfalse, ret_false);
  488. break;
  489. }
  490. ec.EmitNullableGetValueOrDefault (left);
  491. ec.EmitNullableGetValueOrDefault (right);
  492. ec.EmitCall (method);
  493. ig.Emit (OpCodes.Br, done);
  494. ig.MarkLabel (ret_true);
  495. ig.Emit (OpCodes.Ldc_I4_1);
  496. ig.Emit (OpCodes.Br, done);
  497. ig.MarkLabel (ret_false);
  498. ig.Emit (OpCodes.Ldc_I4_0);
  499. ig.Emit (OpCodes.Br, done);
  500. ig.MarkLabel (done);
  501. }
  502. void EmitLiftedToNullUserDefinedOperator (EmitContext ec)
  503. {
  504. var ig = ec.ig;
  505. var ret = ig.DefineLabel ();
  506. var done = ig.DefineLabel ();
  507. var left = ec.EmitStored (this.left);
  508. var right = ec.EmitStored (this.right);
  509. ec.EmitNullableHasValue (left);
  510. ec.EmitNullableHasValue (right);
  511. ig.Emit (OpCodes.And);
  512. ig.Emit (OpCodes.Brfalse, ret);
  513. ec.EmitNullableGetValueOrDefault (left);
  514. ec.EmitNullableGetValueOrDefault (right);
  515. ec.EmitCall (method);
  516. ec.EmitNullableNew (Type);
  517. ig.Emit (OpCodes.Br, done);
  518. ig.MarkLabel (ret);
  519. var temp = ig.DeclareLocal (Type);
  520. ec.EmitNullableInitialize (temp);
  521. ig.MarkLabel (done);
  522. }
  523. void EmitUserDefinedLiftedLogicalShortCircuit (EmitContext ec)
  524. {
  525. var ig = ec.ig;
  526. var and = NodeType == ExpressionType.AndAlso;
  527. var left_is_null = ig.DefineLabel ();
  528. var ret_left = ig.DefineLabel ();
  529. var ret_null = ig.DefineLabel ();
  530. var done = ig.DefineLabel ();
  531. var left = ec.EmitStored (this.left);
  532. ec.EmitNullableHasValue (left);
  533. ig.Emit (OpCodes.Brfalse, and ? ret_null : left_is_null);
  534. ec.EmitNullableGetValueOrDefault (left);
  535. ec.EmitCall (and ? GetFalseOperator () : GetTrueOperator ());
  536. ig.Emit (OpCodes.Brtrue, ret_left);
  537. ig.MarkLabel (left_is_null);
  538. var right = ec.EmitStored (this.right);
  539. ec.EmitNullableHasValue (right);
  540. ig.Emit (OpCodes.Brfalse, ret_null);
  541. ec.EmitNullableGetValueOrDefault (left);
  542. ec.EmitNullableGetValueOrDefault (right);
  543. ec.EmitCall (method);
  544. ec.EmitNullableNew (Type);
  545. ig.Emit (OpCodes.Br, done);
  546. ig.MarkLabel (ret_left);
  547. ec.EmitLoad (left);
  548. ig.Emit (OpCodes.Br, done);
  549. ig.MarkLabel (ret_null);
  550. var ret = ig.DeclareLocal (Type);
  551. ec.EmitNullableInitialize (ret);
  552. ig.MarkLabel (done);
  553. }
  554. void EmitUserDefinedOperator (EmitContext ec)
  555. {
  556. if (!IsLifted) {
  557. switch (NodeType) {
  558. case ExpressionType.AndAlso:
  559. case ExpressionType.OrElse:
  560. EmitUserDefinedLogicalShortCircuit (ec);
  561. break;
  562. default:
  563. left.Emit (ec);
  564. right.Emit (ec);
  565. ec.EmitCall (method);
  566. break;
  567. }
  568. } else if (IsLiftedToNull) {
  569. switch (NodeType) {
  570. case ExpressionType.AndAlso:
  571. case ExpressionType.OrElse:
  572. EmitUserDefinedLiftedLogicalShortCircuit (ec);
  573. break;
  574. default:
  575. EmitLiftedToNullUserDefinedOperator (ec);
  576. break;
  577. }
  578. } else
  579. EmitLiftedUserDefinedOperator (ec);
  580. }
  581. internal override void Emit (EmitContext ec)
  582. {
  583. if (method != null) {
  584. EmitUserDefinedOperator (ec);
  585. return;
  586. }
  587. switch (NodeType){
  588. case ExpressionType.ArrayIndex:
  589. EmitArrayAccess (ec);
  590. return;
  591. case ExpressionType.Coalesce:
  592. if (conversion != null)
  593. EmitConvertedCoalesce (ec);
  594. else
  595. EmitCoalesce (ec);
  596. return;
  597. case ExpressionType.Power:
  598. case ExpressionType.Add:
  599. case ExpressionType.AddChecked:
  600. case ExpressionType.Divide:
  601. case ExpressionType.ExclusiveOr:
  602. case ExpressionType.LeftShift:
  603. case ExpressionType.Modulo:
  604. case ExpressionType.Multiply:
  605. case ExpressionType.MultiplyChecked:
  606. case ExpressionType.RightShift:
  607. case ExpressionType.Subtract:
  608. case ExpressionType.SubtractChecked:
  609. EmitArithmeticBinary (ec);
  610. return;
  611. case ExpressionType.Equal:
  612. case ExpressionType.GreaterThan:
  613. case ExpressionType.GreaterThanOrEqual:
  614. case ExpressionType.LessThan:
  615. case ExpressionType.LessThanOrEqual:
  616. case ExpressionType.NotEqual:
  617. EmitRelationalBinary (ec);
  618. return;
  619. case ExpressionType.And:
  620. case ExpressionType.Or:
  621. case ExpressionType.AndAlso:
  622. case ExpressionType.OrElse:
  623. EmitLogicalBinary (ec);
  624. return;
  625. default:
  626. throw new NotSupportedException (this.NodeType.ToString ());
  627. }
  628. }
  629. }
  630. }