2
0

Expression.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. //
  2. // Expression.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // (C) 2008 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.Linq;
  33. using System.Reflection;
  34. namespace System.Linq.Expressions {
  35. public abstract class Expression {
  36. ExpressionType node_type;
  37. Type type;
  38. static BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance;
  39. static BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
  40. public ExpressionType NodeType {
  41. get { return node_type; }
  42. }
  43. public Type Type {
  44. get { return type; }
  45. }
  46. // TODO: remove when all Expression subtypes
  47. // have their constructor implemented
  48. protected Expression ()
  49. {
  50. }
  51. protected Expression (ExpressionType node_type, Type type)
  52. {
  53. this.node_type = node_type;
  54. this.type = type;
  55. }
  56. public override string ToString ()
  57. {
  58. return ExpressionPrinter.ToString (this);
  59. }
  60. static void CheckMethod (MethodInfo m)
  61. {
  62. }
  63. #region Binary Expressions
  64. static bool IsInt (Type t)
  65. {
  66. return t == typeof (byte) || t == typeof (sbyte) ||
  67. t == typeof (short) || t == typeof (ushort) ||
  68. t == typeof (int) || t == typeof (uint) ||
  69. t == typeof (long) || t == typeof (ulong);
  70. }
  71. static bool IsNumber (Type t)
  72. {
  73. if (IsInt (t))
  74. return true;
  75. return t == typeof (float) || t == typeof (double) || t == typeof (decimal);
  76. }
  77. static MethodInfo GetUnaryOperator (string oper_name, Type on_type, Expression expression)
  78. {
  79. var methods = on_type.GetMethods (PublicStatic);
  80. foreach (var method in methods) {
  81. if (method.Name != oper_name)
  82. continue;
  83. var parameters = method.GetParameters ();
  84. if (parameters.Length != 1)
  85. continue;
  86. if (!parameters [0].ParameterType.IsAssignableFrom (expression.Type))
  87. continue;
  88. return method;
  89. }
  90. return null;
  91. }
  92. static MethodInfo UnaryCoreCheck (string oper_name, Expression expression, MethodInfo method)
  93. {
  94. if (expression == null)
  95. throw new ArgumentNullException ("expression");
  96. if (method != null) {
  97. if (method.ReturnType == typeof (void))
  98. throw new ArgumentException ("Specified method must return a value", "method");
  99. if (!method.IsStatic)
  100. throw new ArgumentException ("Method must be static", "method");
  101. var parameters = method.GetParameters ();
  102. if (parameters.Length != 1)
  103. throw new ArgumentException ("Must have only one parameters", "method");
  104. if (!parameters [0].ParameterType.IsAssignableFrom (expression.Type))
  105. throw new InvalidOperationException ("left-side argument type does not match left expression type");
  106. return method;
  107. } else {
  108. if (IsNumber (expression.Type))
  109. return null;
  110. if (oper_name != null) {
  111. method = GetUnaryOperator (oper_name, expression.Type, expression);
  112. if (method != null)
  113. return method;
  114. }
  115. throw new InvalidOperationException (
  116. String.Format ("Operation {0} not defined for {1}", oper_name != null ? oper_name.Substring (3) : "is", expression.Type));
  117. }
  118. }
  119. static MethodInfo GetBinaryOperator (string oper_name, Type on_type, Expression left, Expression right)
  120. {
  121. MethodInfo [] methods = on_type.GetMethods (PublicStatic);
  122. foreach (MethodInfo m in methods){
  123. if (m.Name != oper_name)
  124. continue;
  125. ParameterInfo [] pi = m.GetParameters ();
  126. if (pi.Length != 2)
  127. continue;
  128. if (!pi [0].ParameterType.IsAssignableFrom (left.Type))
  129. continue;
  130. if (!pi [1].ParameterType.IsAssignableFrom (right.Type))
  131. continue;
  132. // Method has papers in order.
  133. return m;
  134. }
  135. return null;
  136. }
  137. //
  138. // Performs basic checks on the incoming expressions for binary expressions
  139. // and any provided MethodInfo.
  140. //
  141. static MethodInfo BinaryCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
  142. {
  143. if (left == null)
  144. throw new ArgumentNullException ("left");
  145. if (right == null)
  146. throw new ArgumentNullException ("right");
  147. if (method != null){
  148. if (method.ReturnType == typeof (void))
  149. throw new ArgumentException ("Specified method must return a value", "method");
  150. if (!method.IsStatic)
  151. throw new ArgumentException ("Method must be static", "method");
  152. ParameterInfo [] pi = method.GetParameters ();
  153. if (pi.Length != 2)
  154. throw new ArgumentException ("Must have only two parameters", "method");
  155. Type ltype = left.Type.IsValueType && IsNullable (left.Type) ? GetNullableOf(left.Type) : left.Type;
  156. Type rtype = left.Type.IsValueType && IsNullable (right.Type) ? GetNullableOf(right.Type) :right.Type;
  157. if (ltype != pi [0].ParameterType)
  158. throw new InvalidOperationException ("left-side argument type does not match left expression type");
  159. if (rtype != pi [1].ParameterType)
  160. throw new InvalidOperationException ("right-side argument type does not match right expression type");
  161. return method;
  162. } else {
  163. Type ltype = left.Type;
  164. Type rtype = right.Type;
  165. Type ultype = left.Type;
  166. Type urtype = right.Type;
  167. if (IsNullable (ltype))
  168. ultype = GetNullableOf (ltype);
  169. if (IsNullable (rtype))
  170. urtype = GetNullableOf (rtype);
  171. // Use IsNumber to avoid expensive reflection.
  172. if (IsNumber (ultype)){
  173. if (ultype == urtype && ltype == rtype)
  174. return method;
  175. if (oper_name != null){
  176. method = GetBinaryOperator (oper_name, rtype, left, right);
  177. if (method != null)
  178. return method;
  179. }
  180. }
  181. if (oper_name != null){
  182. method = GetBinaryOperator (oper_name, ltype, left, right);
  183. if (method != null)
  184. return method;
  185. }
  186. //
  187. // == and != allow reference types without operators defined.
  188. //
  189. if (!ltype.IsValueType && !rtype.IsValueType &&
  190. (oper_name == "op_Equality" || oper_name == "op_Inequality"))
  191. return null;
  192. throw new InvalidOperationException (
  193. String.Format ("Operation {0} not defined for {1} and {2}", oper_name != null ? oper_name.Substring (3) : "is", ltype, rtype));
  194. }
  195. }
  196. //
  197. // This is like BinaryCoreCheck, but if no method is used adds the restriction that
  198. // only ints and bools are allowed
  199. //
  200. static MethodInfo BinaryBitwiseCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
  201. {
  202. if (left == null)
  203. throw new ArgumentNullException ("left");
  204. if (right == null)
  205. throw new ArgumentNullException ("right");
  206. if (method == null){
  207. // avoid reflection shortcut and catches Ints/bools before we check Numbers in general
  208. if (left.Type == right.Type && (left.Type == typeof (bool) || IsInt (left.Type)))
  209. return method;
  210. }
  211. method = BinaryCoreCheck (oper_name, left, right, method);
  212. if (method == null){
  213. //
  214. // The check in BinaryCoreCheck allows a bit more than we do
  215. // (floats and doubles). Catch this here
  216. //
  217. throw new InvalidOperationException ("Types not supported");
  218. }
  219. return method;
  220. }
  221. static BinaryExpression MakeSimpleBinary (ExpressionType et, Expression left, Expression right, MethodInfo method)
  222. {
  223. Type result = method == null ? left.Type : method.ReturnType;
  224. bool is_lifted;
  225. if (method == null){
  226. if (IsNullable (left.Type)){
  227. if (!IsNullable (right.Type))
  228. throw new Exception ("Assertion, internal error: left is nullable, requires right to be as well");
  229. is_lifted = true;
  230. } else
  231. is_lifted = false;
  232. } else {
  233. //
  234. // FIXME: implement
  235. //
  236. is_lifted = false;
  237. }
  238. return new BinaryExpression (et, result, left, right, false, is_lifted, method, null);
  239. }
  240. static UnaryExpression MakeSimpleUnary (ExpressionType et, Expression expression, MethodInfo method)
  241. {
  242. Type result = method == null ? expression.Type : method.ReturnType;
  243. return new UnaryExpression (et, expression, result, method);
  244. }
  245. static BinaryExpression MakeBoolBinary (ExpressionType et, Expression left, Expression right, bool liftToNull, MethodInfo method)
  246. {
  247. Type result;
  248. Type ltype = left.Type;
  249. Type rtype = right.Type;
  250. bool lnullable = IsNullable (ltype);
  251. bool rnullable = IsNullable (rtype);
  252. bool is_lifted;
  253. //
  254. // Implement the rules as described in "Expression.Equal" method.
  255. //
  256. if (method == null){
  257. if (lnullable == false && rnullable == false){
  258. is_lifted = false;
  259. result = typeof (bool);
  260. } else if (lnullable && rnullable){
  261. is_lifted = true;
  262. result = liftToNull ? typeof(bool?) : typeof (bool);
  263. } else
  264. throw new Exception ("Internal error: this should have been caught in BinaryCoreCheck");
  265. } else {
  266. ParameterInfo [] pi = method.GetParameters ();
  267. Type mltype = pi [0].ParameterType;
  268. Type mrtype = pi [1].ParameterType;
  269. if (ltype == mltype && rtype == mrtype){
  270. is_lifted = false;
  271. result = method.ReturnType;
  272. }
  273. else if (ltype.IsValueType && rtype.IsValueType &&
  274. ((lnullable && GetNullableOf (ltype) == mltype) ||
  275. (rnullable && GetNullableOf (rtype) == mrtype))){
  276. is_lifted = true;
  277. if (method.ReturnType == typeof(bool)){
  278. result = liftToNull ? typeof(bool?) : typeof(bool);
  279. } else {
  280. //
  281. // This behavior is not documented: what
  282. // happens if the result is not typeof(bool), but
  283. // the parameters are nullable: the result
  284. // becomes nullable<returntype>
  285. //
  286. // See:
  287. // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=323139
  288. result = typeof (Nullable<>).MakeGenericType (method.ReturnType);
  289. //Type.GetType ("System.Nullable`1[" + method.ReturnType.ToString () + "]");
  290. }
  291. } else {
  292. is_lifted = false;
  293. result = method.ReturnType;
  294. }
  295. }
  296. return new BinaryExpression (et, result, left, right, liftToNull, is_lifted, method, null);
  297. }
  298. //
  299. // Arithmetic
  300. //
  301. public static BinaryExpression Add (Expression left, Expression right)
  302. {
  303. return Add (left, right, null);
  304. }
  305. public static BinaryExpression Add (Expression left, Expression right, MethodInfo method)
  306. {
  307. method = BinaryCoreCheck ("op_Addition", left, right, method);
  308. return MakeSimpleBinary (ExpressionType.Add, left, right, method);
  309. }
  310. public static BinaryExpression AddChecked (Expression left, Expression right)
  311. {
  312. return AddChecked (left, right, null);
  313. }
  314. public static BinaryExpression AddChecked (Expression left, Expression right, MethodInfo method)
  315. {
  316. method = BinaryCoreCheck ("op_Addition", left, right, method);
  317. //
  318. // The check in BinaryCoreCheck allows a bit more than we do
  319. // (byte, sbyte). Catch that here
  320. //
  321. if (method == null){
  322. Type ltype = left.Type;
  323. if (ltype == typeof (byte) || ltype == typeof (sbyte))
  324. throw new InvalidOperationException (String.Format ("SubtractChecked not defined for {0} and {1}", left.Type, right.Type));
  325. }
  326. return MakeSimpleBinary (ExpressionType.AddChecked, left, right, method);
  327. }
  328. public static BinaryExpression Subtract (Expression left, Expression right)
  329. {
  330. return Subtract (left, right, null);
  331. }
  332. public static BinaryExpression Subtract (Expression left, Expression right, MethodInfo method)
  333. {
  334. method = BinaryCoreCheck ("op_Subtraction", left, right, method);
  335. return MakeSimpleBinary (ExpressionType.Subtract, left, right, method);
  336. }
  337. public static BinaryExpression SubtractChecked (Expression left, Expression right)
  338. {
  339. return SubtractChecked (left, right, null);
  340. }
  341. public static BinaryExpression SubtractChecked (Expression left, Expression right, MethodInfo method)
  342. {
  343. method = BinaryCoreCheck ("op_Subtraction", left, right, method);
  344. //
  345. // The check in BinaryCoreCheck allows a bit more than we do
  346. // (byte, sbyte). Catch that here
  347. //
  348. if (method == null){
  349. Type ltype = left.Type;
  350. if (ltype == typeof (byte) || ltype == typeof (sbyte))
  351. throw new InvalidOperationException (String.Format ("SubtractChecked not defined for {0} and {1}", left.Type, right.Type));
  352. }
  353. return MakeSimpleBinary (ExpressionType.SubtractChecked, left, right, method);
  354. }
  355. public static BinaryExpression Modulo (Expression left, Expression right)
  356. {
  357. return Modulo (left, right, null);
  358. }
  359. public static BinaryExpression Modulo (Expression left, Expression right, MethodInfo method)
  360. {
  361. method = BinaryCoreCheck ("op_Modulus", left, right, method);
  362. return MakeSimpleBinary (ExpressionType.Modulo, left, right, method);
  363. }
  364. public static BinaryExpression Multiply (Expression left, Expression right)
  365. {
  366. return Multiply (left, right, null);
  367. }
  368. public static BinaryExpression Multiply (Expression left, Expression right, MethodInfo method)
  369. {
  370. method = BinaryCoreCheck ("op_Multiply", left, right, method);
  371. return MakeSimpleBinary (ExpressionType.Multiply, left, right, method);
  372. }
  373. public static BinaryExpression MultiplyChecked (Expression left, Expression right)
  374. {
  375. return MultiplyChecked (left, right, null);
  376. }
  377. public static BinaryExpression MultiplyChecked (Expression left, Expression right, MethodInfo method)
  378. {
  379. method = BinaryCoreCheck ("op_Multiply", left, right, method);
  380. return MakeSimpleBinary (ExpressionType.MultiplyChecked, left, right, method);
  381. }
  382. public static BinaryExpression Divide (Expression left, Expression right)
  383. {
  384. return Divide (left, right, null);
  385. }
  386. public static BinaryExpression Divide (Expression left, Expression right, MethodInfo method)
  387. {
  388. method = BinaryCoreCheck ("op_Division", left, right, method);
  389. return MakeSimpleBinary (ExpressionType.Divide, left, right, method);
  390. }
  391. public static BinaryExpression Power (Expression left, Expression right)
  392. {
  393. return Power (left, right, null);
  394. }
  395. public static BinaryExpression Power (Expression left, Expression right, MethodInfo method)
  396. {
  397. method = BinaryCoreCheck (null, left, right, method);
  398. if (left.Type != typeof (double))
  399. throw new InvalidOperationException ("Power only supports double arguments");
  400. return MakeSimpleBinary (ExpressionType.Power, left, right, method);
  401. }
  402. //
  403. // Bitwise
  404. //
  405. public static BinaryExpression And (Expression left, Expression right)
  406. {
  407. return And (left, right, null);
  408. }
  409. public static BinaryExpression And (Expression left, Expression right, MethodInfo method)
  410. {
  411. method = BinaryBitwiseCoreCheck ("op_BitwiseAnd", left, right, method);
  412. return MakeSimpleBinary (ExpressionType.And, left, right, method);
  413. }
  414. public static BinaryExpression Or (Expression left, Expression right)
  415. {
  416. return Or (left, right, null);
  417. }
  418. public static BinaryExpression Or (Expression left, Expression right, MethodInfo method)
  419. {
  420. method = BinaryBitwiseCoreCheck ("op_BitwiseOr", left, right, method);
  421. return MakeSimpleBinary (ExpressionType.Or, left, right, method);
  422. }
  423. public static BinaryExpression ExclusiveOr (Expression left, Expression right)
  424. {
  425. return ExclusiveOr (left, right, null);
  426. }
  427. public static BinaryExpression ExclusiveOr (Expression left, Expression right, MethodInfo method)
  428. {
  429. method = BinaryBitwiseCoreCheck ("op_ExclusiveOr", left, right, method);
  430. return MakeSimpleBinary (ExpressionType.ExclusiveOr, left, right, method);
  431. }
  432. public static BinaryExpression LeftShift (Expression left, Expression right)
  433. {
  434. return LeftShift (left, right, null);
  435. }
  436. public static BinaryExpression LeftShift (Expression left, Expression right, MethodInfo method)
  437. {
  438. method = BinaryBitwiseCoreCheck ("op_LeftShift", left, right, method);
  439. return MakeSimpleBinary (ExpressionType.LeftShift, left, right, method);
  440. }
  441. public static BinaryExpression RightShift (Expression left, Expression right)
  442. {
  443. return RightShift (left, right, null);
  444. }
  445. public static BinaryExpression RightShift (Expression left, Expression right, MethodInfo method)
  446. {
  447. method = BinaryCoreCheck ("op_RightShift", left, right, method);
  448. return MakeSimpleBinary (ExpressionType.RightShift, left, right, method);
  449. }
  450. //
  451. // Short-circuit
  452. //
  453. public static BinaryExpression AndAlso (Expression left, Expression right)
  454. {
  455. return AndAlso (left, right, null);
  456. }
  457. [MonoTODO]
  458. public static BinaryExpression AndAlso (Expression left, Expression right, MethodInfo method)
  459. {
  460. // This does not work with int, int pairs; Figure out when its valid
  461. throw new NotImplementedException ();
  462. }
  463. [MonoTODO]
  464. public static BinaryExpression OrElse (Expression left, Expression right)
  465. {
  466. throw new NotImplementedException ();
  467. }
  468. [MonoTODO]
  469. public static BinaryExpression OrElse (Expression left, Expression right, MethodInfo method)
  470. {
  471. throw new NotImplementedException ();
  472. }
  473. //
  474. // Comparison
  475. //
  476. public static BinaryExpression Equal (Expression left, Expression right)
  477. {
  478. return Equal (left, right, false, null);
  479. }
  480. public static BinaryExpression Equal (Expression left, Expression right, bool liftToNull, MethodInfo method)
  481. {
  482. method = BinaryCoreCheck ("op_Equality", left, right, method);
  483. return MakeBoolBinary (ExpressionType.Equal, left, right, liftToNull, method);
  484. }
  485. public static BinaryExpression NotEqual (Expression left, Expression right)
  486. {
  487. return NotEqual (left, right, false, null);
  488. }
  489. public static BinaryExpression NotEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  490. {
  491. method = BinaryCoreCheck ("op_Inequality", left, right, method);
  492. return MakeBoolBinary (ExpressionType.NotEqual, left, right, liftToNull, method);
  493. }
  494. public static BinaryExpression GreaterThan (Expression left, Expression right)
  495. {
  496. return GreaterThan (left, right, false, null);
  497. }
  498. public static BinaryExpression GreaterThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  499. {
  500. method = BinaryCoreCheck ("op_GreaterThan", left, right, method);
  501. return MakeBoolBinary (ExpressionType.GreaterThan, left, right, liftToNull, method);
  502. }
  503. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right)
  504. {
  505. return GreaterThanOrEqual (left, right, false, null);
  506. }
  507. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  508. {
  509. method = BinaryCoreCheck ("op_GreaterThanOrEqual", left, right, method);
  510. return MakeBoolBinary (ExpressionType.GreaterThanOrEqual, left, right, liftToNull, method);
  511. }
  512. public static BinaryExpression LessThan (Expression left, Expression right)
  513. {
  514. return LessThan (left, right, false, null);
  515. }
  516. public static BinaryExpression LessThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  517. {
  518. method = BinaryCoreCheck ("op_LessThan", left, right, method);
  519. return MakeBoolBinary (ExpressionType.LessThan, left, right, liftToNull, method);
  520. }
  521. public static BinaryExpression LessThanOrEqual (Expression left, Expression right)
  522. {
  523. return LessThanOrEqual (left, right, false, null);
  524. }
  525. public static BinaryExpression LessThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  526. {
  527. method = BinaryCoreCheck ("op_LessThanOrEqual", left, right, method);
  528. return MakeBoolBinary (ExpressionType.LessThanOrEqual, left, right, liftToNull, method);
  529. }
  530. //
  531. // Miscelaneous
  532. //
  533. static void ArrayCheck (Expression array)
  534. {
  535. if (array == null)
  536. throw new ArgumentNullException ("array");
  537. if (!array.Type.IsArray)
  538. throw new ArgumentException ("The array argument must be of type array");
  539. }
  540. public static BinaryExpression ArrayIndex (Expression array, Expression index)
  541. {
  542. ArrayCheck (array);
  543. if (index == null)
  544. throw new ArgumentNullException ("index");
  545. if (array.Type.GetArrayRank () != 1)
  546. throw new ArgumentException ("The array argument must be a single dimensional array");
  547. if (index.Type != typeof (int))
  548. throw new ArgumentException ("The index must be of type int");
  549. return new BinaryExpression (ExpressionType.ArrayIndex, array.Type.GetElementType (), array, index);
  550. }
  551. public static BinaryExpression Coalesce (Expression left, Expression right)
  552. {
  553. return Coalesce (left, right, null);
  554. }
  555. [MonoTODO]
  556. public static BinaryExpression Coalesce (Expression left, Expression right, LambdaExpression conversion)
  557. {
  558. BinaryCoreCheck (null, left, right, null);
  559. throw new NotImplementedException ();
  560. }
  561. //
  562. // MakeBinary constructors
  563. //
  564. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right)
  565. {
  566. return MakeBinary (binaryType, left, right, false, null);
  567. }
  568. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method)
  569. {
  570. return MakeBinary (binaryType, left, right, liftToNull, method, null);
  571. }
  572. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method, LambdaExpression conversion)
  573. {
  574. switch (binaryType) {
  575. case ExpressionType.Add:
  576. return Add (left, right, method);
  577. case ExpressionType.AddChecked:
  578. return AddChecked (left, right, method);
  579. case ExpressionType.AndAlso:
  580. return AndAlso (left, right);
  581. case ExpressionType.Coalesce:
  582. return Coalesce (left, right, conversion);
  583. case ExpressionType.Divide:
  584. return Divide (left, right, method);
  585. case ExpressionType.Equal:
  586. return Equal (left, right, liftToNull, method);
  587. case ExpressionType.ExclusiveOr:
  588. return ExclusiveOr (left, right, method);
  589. case ExpressionType.GreaterThan:
  590. return GreaterThan (left, right, liftToNull, method);
  591. case ExpressionType.GreaterThanOrEqual:
  592. return GreaterThanOrEqual (left, right, liftToNull, method);
  593. case ExpressionType.LeftShift:
  594. return LeftShift (left, right, method);
  595. case ExpressionType.LessThan:
  596. return LessThan (left, right, liftToNull, method);
  597. case ExpressionType.LessThanOrEqual:
  598. return LessThanOrEqual (left, right, liftToNull, method);
  599. case ExpressionType.Modulo:
  600. return Modulo (left, right, method);
  601. case ExpressionType.Multiply:
  602. return Multiply (left, right, method);
  603. case ExpressionType.MultiplyChecked:
  604. return MultiplyChecked (left, right, method);
  605. case ExpressionType.NotEqual:
  606. return NotEqual (left, right, liftToNull, method);
  607. case ExpressionType.OrElse:
  608. return OrElse (left, right);
  609. case ExpressionType.Power:
  610. return Power (left, right, method);
  611. case ExpressionType.RightShift:
  612. return RightShift (left, right, method);
  613. case ExpressionType.Subtract:
  614. return Subtract (left, right, method);
  615. case ExpressionType.SubtractChecked:
  616. return SubtractChecked (left, right, method);
  617. case ExpressionType.And:
  618. return And (left, right, method);
  619. case ExpressionType.Or:
  620. return Or (left, right, method);
  621. }
  622. throw new ArgumentException ("MakeBinary expect a binary node type");
  623. }
  624. #endregion
  625. public static MethodCallExpression ArrayIndex (Expression array, params Expression [] indexes)
  626. {
  627. return ArrayIndex (array, indexes as IEnumerable<Expression>);
  628. }
  629. public static MethodCallExpression ArrayIndex (Expression array, IEnumerable<Expression> indexes)
  630. {
  631. ArrayCheck (array);
  632. if (indexes == null)
  633. throw new ArgumentNullException ("indexes");
  634. var args = indexes.ToReadOnlyCollection ();
  635. if (array.Type.GetArrayRank () != args.Count)
  636. throw new ArgumentException ("The number of arguments doesn't match the rank of the array");
  637. foreach (var arg in args)
  638. if (arg.Type != typeof (int))
  639. throw new ArgumentException ("The index must be of type int");
  640. return Call (array, array.Type.GetMethod ("Get", PublicInstance), args);
  641. }
  642. public static UnaryExpression ArrayLength (Expression array)
  643. {
  644. if (array == null)
  645. throw new ArgumentNullException ("array");
  646. if (!array.Type.IsArray)
  647. throw new ArgumentException ("The type of the expression must me Array");
  648. if (array.Type.GetArrayRank () != 1)
  649. throw new ArgumentException ("The array must be a single dimensional array");
  650. return new UnaryExpression (ExpressionType.ArrayLength, array, typeof (int));
  651. }
  652. [MonoTODO]
  653. public static MemberAssignment Bind (MemberInfo member, Expression expression)
  654. {
  655. throw new NotImplementedException ();
  656. }
  657. [MonoTODO]
  658. public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
  659. {
  660. throw new NotImplementedException ();
  661. }
  662. public static MethodCallExpression Call (Expression instance, MethodInfo method)
  663. {
  664. return Call (instance, method, null as IEnumerable<Expression>);
  665. }
  666. public static MethodCallExpression Call (MethodInfo method, params Expression [] arguments)
  667. {
  668. return Call (null, method, arguments as IEnumerable<Expression>);
  669. }
  670. public static MethodCallExpression Call (Expression instance, MethodInfo method, params Expression [] arguments)
  671. {
  672. return Call (instance, method, arguments as IEnumerable<Expression>);
  673. }
  674. public static MethodCallExpression Call (Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
  675. {
  676. if (method == null)
  677. throw new ArgumentNullException ("method");
  678. if (instance == null && !method.IsStatic)
  679. throw new ArgumentNullException ("instance");
  680. if (instance != null && !method.DeclaringType.IsAssignableFrom (instance.Type))
  681. throw new ArgumentException ("Type is not assignable to the declaring type of the method");
  682. var args = arguments.ToReadOnlyCollection ();
  683. var parameters = method.GetParameters ();
  684. if (args.Count != parameters.Length)
  685. throw new ArgumentException ("The number of arguments doesn't match the number of parameters");
  686. // TODO: check for assignability of the arguments on the parameters
  687. return new MethodCallExpression (instance, method, args);
  688. }
  689. [MonoTODO]
  690. public static MethodCallExpression Call (Expression instance, string methodName, Type [] typeArguments, params Expression [] arguments)
  691. {
  692. throw new NotImplementedException ();
  693. }
  694. [MonoTODO]
  695. public static MethodCallExpression Call (Type type, string methodName, Type [] typeArguments, params Expression [] arguments)
  696. {
  697. throw new NotImplementedException ();
  698. }
  699. public static ConditionalExpression Condition (Expression test, Expression ifTrue, Expression ifFalse)
  700. {
  701. if (test == null)
  702. throw new ArgumentNullException ("test");
  703. if (ifTrue == null)
  704. throw new ArgumentNullException ("ifTrue");
  705. if (ifFalse == null)
  706. throw new ArgumentNullException ("ifFalse");
  707. if (test.Type != typeof (bool))
  708. throw new ArgumentException ("Test expression should be of type bool");
  709. if (ifTrue.Type != ifFalse.Type)
  710. throw new ArgumentException ("The ifTrue and ifFalse type do not match");
  711. return new ConditionalExpression (test, ifTrue, ifFalse);
  712. }
  713. public static ConstantExpression Constant (object value)
  714. {
  715. if (value == null)
  716. return new ConstantExpression (null, typeof (object));
  717. return Constant (value, value.GetType ());
  718. }
  719. public static ConstantExpression Constant (object value, Type type)
  720. {
  721. if (type == null)
  722. throw new ArgumentNullException ("type");
  723. //
  724. // value must be compatible with type, no conversions
  725. // are allowed
  726. //
  727. if (value == null){
  728. if (type.IsValueType && !IsNullable (type))
  729. throw new ArgumentException ();
  730. } else {
  731. if (!(type.IsValueType && IsNullable (type)) && value.GetType () != type)
  732. throw new ArgumentException ();
  733. }
  734. return new ConstantExpression (value, type);
  735. }
  736. [MonoTODO]
  737. public static UnaryExpression Convert (Expression expression, Type type)
  738. {
  739. throw new NotImplementedException ();
  740. }
  741. [MonoTODO]
  742. public static UnaryExpression Convert (Expression expression, Type type, MethodInfo method)
  743. {
  744. throw new NotImplementedException ();
  745. }
  746. [MonoTODO]
  747. public static UnaryExpression ConvertChecked (Expression expression, Type type)
  748. {
  749. throw new NotImplementedException ();
  750. }
  751. [MonoTODO]
  752. public static UnaryExpression ConvertChecked (Expression expression, Type type, MethodInfo method)
  753. {
  754. throw new NotImplementedException ();
  755. }
  756. [MonoTODO]
  757. public static ElementInit ElementInit (MethodInfo addMethod, params Expression [] arguments)
  758. {
  759. throw new NotImplementedException ();
  760. }
  761. [MonoTODO]
  762. public static ElementInit ElementInit (MethodInfo addMethod, IEnumerable<Expression> arguments)
  763. {
  764. throw new NotImplementedException ();
  765. }
  766. [MonoTODO]
  767. public static MemberExpression Field (Expression expression, FieldInfo field)
  768. {
  769. throw new NotImplementedException ();
  770. }
  771. [MonoTODO]
  772. public static MemberExpression Field (Expression expression, string fieldName)
  773. {
  774. throw new NotImplementedException ();
  775. }
  776. public static Type GetActionType (params Type [] typeArgs)
  777. {
  778. if (typeArgs == null)
  779. throw new ArgumentNullException ("typeArgs");
  780. if (typeArgs.Length > 4)
  781. throw new ArgumentException ("No Action type of this arity");
  782. if (typeArgs.Length == 0)
  783. return typeof (Action);
  784. Type action = null;
  785. switch (typeArgs.Length) {
  786. case 1:
  787. action = typeof (Action<>);
  788. break;
  789. case 2:
  790. action = typeof (Action<,>);
  791. break;
  792. case 3:
  793. action = typeof (Action<,,>);
  794. break;
  795. case 4:
  796. action = typeof (Action<,,,>);
  797. break;
  798. }
  799. return action.MakeGenericType (typeArgs);
  800. }
  801. public static Type GetFuncType (params Type [] typeArgs)
  802. {
  803. if (typeArgs == null)
  804. throw new ArgumentNullException ("typeArgs");
  805. if (typeArgs.Length < 1 || typeArgs.Length > 5)
  806. throw new ArgumentException ("No Func type of this arity");
  807. Type func = null;
  808. switch (typeArgs.Length) {
  809. case 1:
  810. func = typeof (Func<>);
  811. break;
  812. case 2:
  813. func = typeof (Func<,>);
  814. break;
  815. case 3:
  816. func = typeof (Func<,,>);
  817. break;
  818. case 4:
  819. func = typeof (Func<,,,>);
  820. break;
  821. case 5:
  822. func = typeof (Func<,,,,>);
  823. break;
  824. }
  825. return func.MakeGenericType (typeArgs);
  826. }
  827. [MonoTODO]
  828. public static InvocationExpression Invoke (Expression expression, params Expression [] arguments)
  829. {
  830. throw new NotImplementedException ();
  831. }
  832. [MonoTODO]
  833. public static InvocationExpression Invoke (Expression expression, IEnumerable<Expression> arguments)
  834. {
  835. throw new NotImplementedException ();
  836. }
  837. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, params ParameterExpression [] parameters)
  838. {
  839. if (body == null)
  840. throw new ArgumentNullException ("body");
  841. return new Expression<TDelegate> (body, parameters);
  842. }
  843. [MonoTODO]
  844. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, IEnumerable<ParameterExpression> parameters)
  845. {
  846. throw new NotImplementedException ();
  847. }
  848. [MonoTODO]
  849. public static LambdaExpression Lambda (Expression body, params ParameterExpression [] parameters)
  850. {
  851. throw new NotImplementedException ();
  852. }
  853. public static LambdaExpression Lambda (Type delegateType, Expression body, params ParameterExpression [] parameters)
  854. {
  855. return Lambda (delegateType, body, parameters as IEnumerable<ParameterExpression>);
  856. }
  857. [MonoTODO]
  858. public static LambdaExpression Lambda (Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
  859. {
  860. if (delegateType == null)
  861. throw new ArgumentNullException ("delegateType");
  862. if (body == null)
  863. throw new ArgumentNullException ("body");
  864. return new LambdaExpression (delegateType, body, parameters.ToReadOnlyCollection ());
  865. }
  866. public static MemberListBinding ListBind (MemberInfo member, params ElementInit [] initializers)
  867. {
  868. throw new NotImplementedException ();
  869. }
  870. [MonoTODO]
  871. public static MemberListBinding ListBind (MemberInfo member, IEnumerable<ElementInit> initializers)
  872. {
  873. throw new NotImplementedException ();
  874. }
  875. [MonoTODO]
  876. public static MemberListBinding ListBind (MethodInfo propertyAccessor, params ElementInit [] initializers)
  877. {
  878. throw new NotImplementedException ();
  879. }
  880. [MonoTODO]
  881. public static MemberListBinding ListBind (MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers)
  882. {
  883. throw new NotImplementedException ();
  884. }
  885. [MonoTODO]
  886. public static ListInitExpression ListInit (NewExpression newExpression, params ElementInit [] initializers)
  887. {
  888. throw new NotImplementedException ();
  889. }
  890. [MonoTODO]
  891. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<ElementInit> initializers)
  892. {
  893. throw new NotImplementedException ();
  894. }
  895. [MonoTODO]
  896. public static ListInitExpression ListInit (NewExpression newExpression, params Expression [] initializers)
  897. {
  898. throw new NotImplementedException ();
  899. }
  900. [MonoTODO]
  901. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<Expression> initializers)
  902. {
  903. throw new NotImplementedException ();
  904. }
  905. [MonoTODO]
  906. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, params Expression [] initializers)
  907. {
  908. throw new NotImplementedException ();
  909. }
  910. [MonoTODO]
  911. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, IEnumerable<Expression> initializers)
  912. {
  913. throw new NotImplementedException ();
  914. }
  915. public static MemberExpression MakeMemberAccess (Expression expression, MemberInfo member)
  916. {
  917. if (expression == null)
  918. throw new ArgumentNullException ("expression");
  919. if (member == null)
  920. throw new ArgumentNullException ("member");
  921. var field = member as FieldInfo;
  922. if (field != null)
  923. return Field (expression, field);
  924. var property = member as PropertyInfo;
  925. if (property != null)
  926. return Property (expression, property);
  927. throw new ArgumentException ("Member should either be a field or a property");
  928. }
  929. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type)
  930. {
  931. return MakeUnary (unaryType, operand, type, null);
  932. }
  933. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type, MethodInfo method)
  934. {
  935. switch (unaryType) {
  936. case ExpressionType.ArrayLength:
  937. return ArrayLength (operand);
  938. case ExpressionType.Convert:
  939. return Convert (operand, type, method);
  940. case ExpressionType.ConvertChecked:
  941. return ConvertChecked (operand, type, method);
  942. case ExpressionType.Negate:
  943. return Negate (operand, method);
  944. case ExpressionType.NegateChecked:
  945. return NegateChecked (operand, method);
  946. case ExpressionType.Not:
  947. return Not (operand, method);
  948. case ExpressionType.Quote:
  949. return Quote (operand);
  950. case ExpressionType.TypeAs:
  951. return TypeAs (operand, type);
  952. case ExpressionType.UnaryPlus:
  953. return UnaryPlus (operand, method);
  954. }
  955. throw new ArgumentException ("MakeUnary expect an unary operator");
  956. }
  957. [MonoTODO]
  958. public static MemberMemberBinding MemberBind (MemberInfo member, params MemberBinding [] binding)
  959. {
  960. throw new NotImplementedException ();
  961. }
  962. [MonoTODO]
  963. public static MemberMemberBinding MemberBind (MemberInfo member, IEnumerable<MemberBinding> binding)
  964. {
  965. throw new NotImplementedException ();
  966. }
  967. [MonoTODO]
  968. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, params MemberBinding [] binding)
  969. {
  970. throw new NotImplementedException ();
  971. }
  972. [MonoTODO]
  973. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, IEnumerable<MemberBinding> binding)
  974. {
  975. throw new NotImplementedException ();
  976. }
  977. [MonoTODO]
  978. public static MemberInitExpression MemberInit (NewExpression newExpression, params MemberBinding [] binding)
  979. {
  980. throw new NotImplementedException ();
  981. }
  982. [MonoTODO]
  983. public static MemberInitExpression MemberInit (NewExpression newExpression, IEnumerable<MemberBinding> binding)
  984. {
  985. throw new NotImplementedException ();
  986. }
  987. public static UnaryExpression Negate (Expression expression)
  988. {
  989. return Negate (expression, null);
  990. }
  991. public static UnaryExpression Negate (Expression expression, MethodInfo method)
  992. {
  993. method = UnaryCoreCheck ("op_UnaryNegation", expression, method);
  994. return MakeSimpleUnary (ExpressionType.Negate, expression, method);
  995. }
  996. public static UnaryExpression NegateChecked (Expression expression)
  997. {
  998. return NegateChecked (expression, null);
  999. }
  1000. public static UnaryExpression NegateChecked (Expression expression, MethodInfo method)
  1001. {
  1002. method = UnaryCoreCheck ("op_UnaryNegation", expression, method);
  1003. return MakeSimpleUnary (ExpressionType.Negate, expression, method);
  1004. }
  1005. [MonoTODO]
  1006. public static NewExpression New (ConstructorInfo constructor)
  1007. {
  1008. throw new NotImplementedException ();
  1009. }
  1010. [MonoTODO]
  1011. public static NewExpression New (Type type)
  1012. {
  1013. throw new NotImplementedException ();
  1014. }
  1015. [MonoTODO]
  1016. public static NewExpression New (ConstructorInfo constructor, params Expression [] arguments)
  1017. {
  1018. throw new NotImplementedException ();
  1019. }
  1020. [MonoTODO]
  1021. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments)
  1022. {
  1023. throw new NotImplementedException ();
  1024. }
  1025. [MonoTODO]
  1026. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, params MemberInfo [] members)
  1027. {
  1028. throw new NotImplementedException ();
  1029. }
  1030. [MonoTODO]
  1031. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, IEnumerable<MemberInfo> members)
  1032. {
  1033. throw new NotImplementedException ();
  1034. }
  1035. [MonoTODO]
  1036. public static NewArrayExpression NewArrayBounds (Type type, params Expression [] bounds)
  1037. {
  1038. throw new NotImplementedException ();
  1039. }
  1040. [MonoTODO]
  1041. public static NewArrayExpression NewArrayBounds (Type type, IEnumerable<Expression> bounds)
  1042. {
  1043. throw new NotImplementedException ();
  1044. }
  1045. [MonoTODO]
  1046. public static NewArrayExpression NewArrayInit (Type type, params Expression [] bounds)
  1047. {
  1048. throw new NotImplementedException ();
  1049. }
  1050. [MonoTODO]
  1051. public static NewArrayExpression NewArrayInit (Type type, IEnumerable<Expression> bounds)
  1052. {
  1053. throw new NotImplementedException ();
  1054. }
  1055. public static UnaryExpression Not (Expression expression)
  1056. {
  1057. return Not (expression, null);
  1058. }
  1059. public static UnaryExpression Not (Expression expression, MethodInfo method)
  1060. {
  1061. method = UnaryCoreCheck ("op_LogicalNot", expression, method);
  1062. return MakeSimpleUnary (ExpressionType.Not, expression, method);
  1063. }
  1064. public static ParameterExpression Parameter (Type type, string name)
  1065. {
  1066. if (type == null)
  1067. throw new ArgumentNullException ("type");
  1068. return new ParameterExpression (type, name);
  1069. }
  1070. [MonoTODO]
  1071. public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
  1072. {
  1073. throw new NotImplementedException ();
  1074. }
  1075. [MonoTODO]
  1076. public static MemberExpression Property (Expression expression, PropertyInfo property)
  1077. {
  1078. throw new NotImplementedException ();
  1079. }
  1080. [MonoTODO]
  1081. public static MemberExpression Property (Expression expression, string propertyName)
  1082. {
  1083. throw new NotImplementedException ();
  1084. }
  1085. [MonoTODO]
  1086. public static MemberExpression PropertyOrField (Expression expression, string propertyOrFieldName)
  1087. {
  1088. throw new NotImplementedException ();
  1089. }
  1090. public static UnaryExpression Quote (Expression expression)
  1091. {
  1092. if (expression == null)
  1093. throw new ArgumentNullException ("expression");
  1094. return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType ());
  1095. }
  1096. public static UnaryExpression TypeAs (Expression expression, Type type)
  1097. {
  1098. if (expression == null)
  1099. throw new ArgumentNullException ("expression");
  1100. if (type == null)
  1101. throw new ArgumentNullException ("type");
  1102. if (type.IsValueType && !IsNullable (type))
  1103. throw new ArgumentException ("TypeAs expect a reference or a nullable type");
  1104. return new UnaryExpression (ExpressionType.TypeAs, expression, type);
  1105. }
  1106. public static TypeBinaryExpression TypeIs (Expression expression, Type type)
  1107. {
  1108. if (expression == null)
  1109. throw new ArgumentNullException ("expression");
  1110. if (type == null)
  1111. throw new ArgumentNullException ("type");
  1112. return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof (bool));
  1113. }
  1114. public static UnaryExpression UnaryPlus (Expression expression)
  1115. {
  1116. return UnaryPlus (expression, null);
  1117. }
  1118. public static UnaryExpression UnaryPlus (Expression expression, MethodInfo method)
  1119. {
  1120. method = UnaryCoreCheck ("op_UnaryPlus", expression, method);
  1121. return MakeSimpleUnary (ExpressionType.UnaryPlus, expression, method);
  1122. }
  1123. static bool IsNullable (Type type)
  1124. {
  1125. return type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>);
  1126. }
  1127. protected static bool IsUnsigned (Type t)
  1128. {
  1129. if (t.IsPointer)
  1130. return IsUnsigned (t.GetElementType ());
  1131. return t == typeof (ushort) || t == typeof (uint) || t == typeof (ulong) || t == typeof (byte);
  1132. }
  1133. //
  1134. // returns the T in a a Nullable<T> type.
  1135. //
  1136. static Type GetNullableOf (Type type)
  1137. {
  1138. return type.GetGenericArguments () [0];
  1139. }
  1140. //
  1141. // This method must be overwritten by derived classes to
  1142. // compile the expression
  1143. //
  1144. internal abstract void Emit (EmitContext ec);
  1145. }
  1146. }