Expression.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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 GetBinaryOperator (string oper_name, Type on_type, Expression left, Expression right)
  78. {
  79. MethodInfo [] methods = on_type.GetMethods (PublicStatic);
  80. foreach (MethodInfo m in methods){
  81. if (m.Name != oper_name)
  82. continue;
  83. ParameterInfo [] pi = m.GetParameters ();
  84. if (pi.Length != 2)
  85. continue;
  86. if (!pi [0].ParameterType.IsAssignableFrom (left.Type))
  87. continue;
  88. if (!pi [1].ParameterType.IsAssignableFrom (right.Type))
  89. continue;
  90. // Method has papers in order.
  91. return m;
  92. }
  93. return null;
  94. }
  95. //
  96. // Performs basic checks on the incoming expressions for binary expressions
  97. // and any provided MethodInfo.
  98. //
  99. static MethodInfo BinaryCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
  100. {
  101. if (left == null)
  102. throw new ArgumentNullException ("left");
  103. if (right == null)
  104. throw new ArgumentNullException ("right");
  105. if (method != null){
  106. if (method.ReturnType == typeof (void))
  107. throw new ArgumentException ("Specified method must return a value", "method");
  108. if (!method.IsStatic)
  109. throw new ArgumentException ("Method must be static", "method");
  110. ParameterInfo [] pi = method.GetParameters ();
  111. if (pi.Length != 2)
  112. throw new ArgumentException ("Must have only two parameters", "method");
  113. if (left.Type != pi [0].ParameterType)
  114. throw new InvalidOperationException ("left-side argument type does not match left expression type");
  115. if (right.Type != pi [1].ParameterType)
  116. throw new InvalidOperationException ("right-side argument type does not match right expression type");
  117. return method;
  118. } else {
  119. Type ltype = left.Type;
  120. Type rtype = right.Type;
  121. // Use IsNumber to avoid expensive reflection.
  122. if (IsNumber (ltype)){
  123. if (ltype == rtype)
  124. return method;
  125. if (oper_name != null){
  126. method = GetBinaryOperator (oper_name, rtype, left, right);
  127. if (method != null)
  128. return method;
  129. }
  130. }
  131. if (oper_name != null){
  132. method = GetBinaryOperator (oper_name, ltype, left, right);
  133. if (method != null)
  134. return method;
  135. }
  136. throw new InvalidOperationException (
  137. String.Format ("Operation {0} not defined for {1} and {2}", oper_name != null ? oper_name.Substring (3) : "is", ltype, rtype));
  138. }
  139. }
  140. //
  141. // This is like BinaryCoreCheck, but if no method is used adds the restriction that
  142. // only ints and bools are allowed
  143. //
  144. static MethodInfo BinaryBitwiseCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
  145. {
  146. if (left == null)
  147. throw new ArgumentNullException ("left");
  148. if (right == null)
  149. throw new ArgumentNullException ("right");
  150. if (method == null){
  151. // avoid reflection shortcut and catches Ints/bools before we check Numbers in general
  152. if (left.Type == right.Type && (left.Type == typeof (bool) || IsInt (left.Type)))
  153. return method;
  154. }
  155. method = BinaryCoreCheck (oper_name, left, right, method);
  156. if (method == null){
  157. //
  158. // The check in BinaryCoreCheck allows a bit more than we do
  159. // (floats and doubles). Catch this here
  160. //
  161. throw new InvalidOperationException ("Types not supported");
  162. }
  163. return method;
  164. }
  165. static BinaryExpression MakeSimpleBinary (ExpressionType et, Expression left, Expression right, MethodInfo method)
  166. {
  167. Type result = method == null ? left.Type : method.ReturnType;
  168. return new BinaryExpression (et, result, left, right, method);
  169. }
  170. static BinaryExpression MakeBoolBinary (ExpressionType et, Expression left, Expression right, bool liftToNull, MethodInfo method)
  171. {
  172. Type result = method == null ? typeof (bool) : method.ReturnType;
  173. return new BinaryExpression (et, result, left, right, method);
  174. }
  175. //
  176. // Arithmetic
  177. //
  178. public static BinaryExpression Add (Expression left, Expression right)
  179. {
  180. return Add (left, right, null);
  181. }
  182. public static BinaryExpression Add (Expression left, Expression right, MethodInfo method)
  183. {
  184. method = BinaryCoreCheck ("op_Addition", left, right, method);
  185. return MakeSimpleBinary (ExpressionType.Add, left, right, method);
  186. }
  187. public static BinaryExpression AddChecked (Expression left, Expression right)
  188. {
  189. return AddChecked (left, right, null);
  190. }
  191. public static BinaryExpression AddChecked (Expression left, Expression right, MethodInfo method)
  192. {
  193. method = BinaryCoreCheck ("op_Addition", left, right, method);
  194. return MakeSimpleBinary (ExpressionType.AddChecked, left, right, method);
  195. }
  196. public static BinaryExpression Subtract (Expression left, Expression right)
  197. {
  198. return Subtract (left, right, null);
  199. }
  200. public static BinaryExpression Subtract (Expression left, Expression right, MethodInfo method)
  201. {
  202. method = BinaryCoreCheck ("op_Subtraction", left, right, method);
  203. return MakeSimpleBinary (ExpressionType.Subtract, left, right, method);
  204. }
  205. public static BinaryExpression SubtractChecked (Expression left, Expression right)
  206. {
  207. return SubtractChecked (left, right, null);
  208. }
  209. public static BinaryExpression SubtractChecked (Expression left, Expression right, MethodInfo method)
  210. {
  211. method = BinaryCoreCheck ("op_Subtraction", left, right, method);
  212. //
  213. // The check in BinaryCoreCheck allows a bit more than we do
  214. // (byte, sbyte, short, ushort). Catch that here
  215. //
  216. if (method == null){
  217. Type ltype = left.Type;
  218. if (ltype == typeof (byte) || ltype == typeof (sbyte))
  219. throw new InvalidOperationException (String.Format ("SubtractChecked not defined for {0} and {1}", left.Type, right.Type));
  220. }
  221. return MakeSimpleBinary (ExpressionType.SubtractChecked, left, right, method);
  222. }
  223. public static BinaryExpression Modulo (Expression left, Expression right)
  224. {
  225. return Modulo (left, right, null);
  226. }
  227. public static BinaryExpression Modulo (Expression left, Expression right, MethodInfo method)
  228. {
  229. method = BinaryCoreCheck ("op_Modulus", left, right, method);
  230. return MakeSimpleBinary (ExpressionType.Modulo, left, right, method);
  231. }
  232. public static BinaryExpression Multiply (Expression left, Expression right)
  233. {
  234. return Multiply (left, right, null);
  235. }
  236. public static BinaryExpression Multiply (Expression left, Expression right, MethodInfo method)
  237. {
  238. method = BinaryCoreCheck ("op_Multiply", left, right, method);
  239. return MakeSimpleBinary (ExpressionType.Multiply, left, right, method);
  240. }
  241. public static BinaryExpression MultiplyChecked (Expression left, Expression right)
  242. {
  243. return MultiplyChecked (left, right, null);
  244. }
  245. public static BinaryExpression MultiplyChecked (Expression left, Expression right, MethodInfo method)
  246. {
  247. method = BinaryCoreCheck ("op_Multiply", left, right, method);
  248. return MakeSimpleBinary (ExpressionType.MultiplyChecked, left, right, method);
  249. }
  250. public static BinaryExpression Divide (Expression left, Expression right)
  251. {
  252. return Divide (left, right, null);
  253. }
  254. public static BinaryExpression Divide (Expression left, Expression right, MethodInfo method)
  255. {
  256. method = BinaryCoreCheck ("op_Division", left, right, method);
  257. return MakeSimpleBinary (ExpressionType.Divide, left, right, method);
  258. }
  259. public static BinaryExpression Power (Expression left, Expression right)
  260. {
  261. return Power (left, right, null);
  262. }
  263. public static BinaryExpression Power (Expression left, Expression right, MethodInfo method)
  264. {
  265. method = BinaryCoreCheck (null, left, right, method);
  266. if (left.Type != typeof (double))
  267. throw new InvalidOperationException ("Power only supports double arguments");
  268. return MakeSimpleBinary (ExpressionType.Power, left, right, method);
  269. }
  270. //
  271. // Bitwise
  272. //
  273. public static BinaryExpression And (Expression left, Expression right)
  274. {
  275. return And (left, right, null);
  276. }
  277. public static BinaryExpression And (Expression left, Expression right, MethodInfo method)
  278. {
  279. method = BinaryBitwiseCoreCheck ("op_BitwiseAnd", left, right, method);
  280. return MakeSimpleBinary (ExpressionType.And, left, right, method);
  281. }
  282. public static BinaryExpression Or (Expression left, Expression right)
  283. {
  284. return Or (left, right, null);
  285. }
  286. public static BinaryExpression Or (Expression left, Expression right, MethodInfo method)
  287. {
  288. method = BinaryBitwiseCoreCheck ("op_BitwiseOr", left, right, method);
  289. return MakeSimpleBinary (ExpressionType.Or, left, right, method);
  290. }
  291. public static BinaryExpression ExclusiveOr (Expression left, Expression right)
  292. {
  293. return ExclusiveOr (left, right, null);
  294. }
  295. public static BinaryExpression ExclusiveOr (Expression left, Expression right, MethodInfo method)
  296. {
  297. method = BinaryBitwiseCoreCheck ("op_ExclusiveOr", left, right, method);
  298. return MakeSimpleBinary (ExpressionType.ExclusiveOr, left, right, method);
  299. }
  300. public static BinaryExpression LeftShift (Expression left, Expression right)
  301. {
  302. return LeftShift (left, right, null);
  303. }
  304. public static BinaryExpression LeftShift (Expression left, Expression right, MethodInfo method)
  305. {
  306. method = BinaryBitwiseCoreCheck ("op_LeftShift", left, right, method);
  307. return MakeSimpleBinary (ExpressionType.LeftShift, left, right, method);
  308. }
  309. public static BinaryExpression RightShift (Expression left, Expression right)
  310. {
  311. return RightShift (left, right, null);
  312. }
  313. public static BinaryExpression RightShift (Expression left, Expression right, MethodInfo method)
  314. {
  315. method = BinaryCoreCheck ("op_RightShift", left, right, method);
  316. return MakeSimpleBinary (ExpressionType.RightShift, left, right, method);
  317. }
  318. //
  319. // Short-circuit
  320. //
  321. public static BinaryExpression AndAlso (Expression left, Expression right)
  322. {
  323. return AndAlso (left, right, null);
  324. }
  325. [MonoTODO]
  326. public static BinaryExpression AndAlso (Expression left, Expression right, MethodInfo method)
  327. {
  328. // This does not work with int, int pairs; Figure out when its valid
  329. throw new NotImplementedException ();
  330. }
  331. [MonoTODO]
  332. public static BinaryExpression OrElse (Expression left, Expression right)
  333. {
  334. throw new NotImplementedException ();
  335. }
  336. [MonoTODO]
  337. public static BinaryExpression OrElse (Expression left, Expression right, MethodInfo method)
  338. {
  339. throw new NotImplementedException ();
  340. }
  341. //
  342. // Comparison
  343. //
  344. public static BinaryExpression Equal (Expression left, Expression right)
  345. {
  346. return Equal (left, right, false, null);
  347. }
  348. public static BinaryExpression Equal (Expression left, Expression right, bool liftToNull, MethodInfo method)
  349. {
  350. method = BinaryCoreCheck ("op_Equality", left, right, method);
  351. return MakeBoolBinary (ExpressionType.Equal, left, right, liftToNull, method);
  352. }
  353. public static BinaryExpression NotEqual (Expression left, Expression right)
  354. {
  355. return NotEqual (left, right, false, null);
  356. }
  357. public static BinaryExpression NotEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  358. {
  359. method = BinaryCoreCheck ("op_Inequality", left, right, method);
  360. return MakeBoolBinary (ExpressionType.NotEqual, left, right, liftToNull, method);
  361. }
  362. public static BinaryExpression GreaterThan (Expression left, Expression right)
  363. {
  364. return GreaterThan (left, right, false, null);
  365. }
  366. public static BinaryExpression GreaterThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  367. {
  368. method = BinaryCoreCheck ("op_GreaterThan", left, right, method);
  369. return MakeBoolBinary (ExpressionType.GreaterThan, left, right, liftToNull, method);
  370. }
  371. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right)
  372. {
  373. return GreaterThanOrEqual (left, right, false, null);
  374. }
  375. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  376. {
  377. method = BinaryCoreCheck ("op_GreaterThanOrEqual", left, right, method);
  378. return MakeBoolBinary (ExpressionType.GreaterThanOrEqual, left, right, liftToNull, method);
  379. }
  380. public static BinaryExpression LessThan (Expression left, Expression right)
  381. {
  382. return LessThan (left, right, false, null);
  383. }
  384. public static BinaryExpression LessThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  385. {
  386. method = BinaryCoreCheck ("op_LessThan", left, right, method);
  387. return MakeBoolBinary (ExpressionType.LessThan, left, right, liftToNull, method);
  388. }
  389. public static BinaryExpression LessThanOrEqual (Expression left, Expression right)
  390. {
  391. return LessThanOrEqual (left, right, false, null);
  392. }
  393. public static BinaryExpression LessThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  394. {
  395. method = BinaryCoreCheck ("op_LessThanOrEqual", left, right, method);
  396. return MakeBoolBinary (ExpressionType.LessThanOrEqual, left, right, liftToNull, method);
  397. }
  398. //
  399. // Miscelaneous
  400. //
  401. static void ArrayCheck (Expression array)
  402. {
  403. if (array == null)
  404. throw new ArgumentNullException ("array");
  405. if (!array.Type.IsArray)
  406. throw new ArgumentException ("The array argument must be of type array");
  407. }
  408. public static BinaryExpression ArrayIndex (Expression array, Expression index)
  409. {
  410. ArrayCheck (array);
  411. if (index == null)
  412. throw new ArgumentNullException ("index");
  413. if (array.Type.GetArrayRank () != 1)
  414. throw new ArgumentException ("The array argument must be a single dimensional array");
  415. if (index.Type != typeof (int))
  416. throw new ArgumentException ("The index must be of type int");
  417. return new BinaryExpression (ExpressionType.ArrayIndex, array.Type.GetElementType (), array, index);
  418. }
  419. public static BinaryExpression Coalesce (Expression left, Expression right)
  420. {
  421. return Coalesce (left, right, null);
  422. }
  423. [MonoTODO]
  424. public static BinaryExpression Coalesce (Expression left, Expression right, LambdaExpression conversion)
  425. {
  426. BinaryCoreCheck (null, left, right, null);
  427. throw new NotImplementedException ();
  428. }
  429. //
  430. // MakeBinary constructors
  431. //
  432. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right)
  433. {
  434. return MakeBinary (binaryType, left, right, false, null);
  435. }
  436. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method)
  437. {
  438. return MakeBinary (binaryType, left, right, liftToNull, method, null);
  439. }
  440. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method, LambdaExpression conversion)
  441. {
  442. switch (binaryType) {
  443. case ExpressionType.Add:
  444. return Add (left, right, method);
  445. case ExpressionType.AddChecked:
  446. return AddChecked (left, right, method);
  447. case ExpressionType.AndAlso:
  448. return AndAlso (left, right);
  449. case ExpressionType.Coalesce:
  450. return Coalesce (left, right, conversion);
  451. case ExpressionType.Divide:
  452. return Divide (left, right, method);
  453. case ExpressionType.Equal:
  454. return Equal (left, right, liftToNull, method);
  455. case ExpressionType.ExclusiveOr:
  456. return ExclusiveOr (left, right, method);
  457. case ExpressionType.GreaterThan:
  458. return GreaterThan (left, right, liftToNull, method);
  459. case ExpressionType.GreaterThanOrEqual:
  460. return GreaterThanOrEqual (left, right, liftToNull, method);
  461. case ExpressionType.LeftShift:
  462. return LeftShift (left, right, method);
  463. case ExpressionType.LessThan:
  464. return LessThan (left, right, liftToNull, method);
  465. case ExpressionType.LessThanOrEqual:
  466. return LessThanOrEqual (left, right, liftToNull, method);
  467. case ExpressionType.Modulo:
  468. return Modulo (left, right, method);
  469. case ExpressionType.Multiply:
  470. return Multiply (left, right, method);
  471. case ExpressionType.MultiplyChecked:
  472. return MultiplyChecked (left, right, method);
  473. case ExpressionType.NotEqual:
  474. return NotEqual (left, right, liftToNull, method);
  475. case ExpressionType.OrElse:
  476. return OrElse (left, right);
  477. case ExpressionType.Power:
  478. return Power (left, right, method);
  479. case ExpressionType.RightShift:
  480. return RightShift (left, right, method);
  481. case ExpressionType.Subtract:
  482. return Subtract (left, right, method);
  483. case ExpressionType.SubtractChecked:
  484. return SubtractChecked (left, right, method);
  485. case ExpressionType.And:
  486. return And (left, right, method);
  487. case ExpressionType.Or:
  488. return Or (left, right, method);
  489. }
  490. throw new ArgumentException ("MakeBinary expect a binary node type");
  491. }
  492. #endregion
  493. public static MethodCallExpression ArrayIndex (Expression array, params Expression [] indexes)
  494. {
  495. return ArrayIndex (array, indexes as IEnumerable<Expression>);
  496. }
  497. public static MethodCallExpression ArrayIndex (Expression array, IEnumerable<Expression> indexes)
  498. {
  499. ArrayCheck (array);
  500. if (indexes == null)
  501. throw new ArgumentNullException ("indexes");
  502. var args = indexes.ToReadOnlyCollection ();
  503. if (array.Type.GetArrayRank () != args.Count)
  504. throw new ArgumentException ("The number of arguments doesn't match the rank of the array");
  505. foreach (var arg in args)
  506. if (arg.Type != typeof (int))
  507. throw new ArgumentException ("The index must be of type int");
  508. return Call (array, array.Type.GetMethod ("Get", PublicInstance), args);
  509. }
  510. public static UnaryExpression ArrayLength (Expression array)
  511. {
  512. if (array == null)
  513. throw new ArgumentNullException ("array");
  514. if (!array.Type.IsArray)
  515. throw new ArgumentException ("The type of the expression must me Array");
  516. if (array.Type.GetArrayRank () != 1)
  517. throw new ArgumentException ("The array must be a single dimensional array");
  518. return new UnaryExpression (ExpressionType.ArrayLength, array, typeof (int));
  519. }
  520. [MonoTODO]
  521. public static MemberAssignment Bind (MemberInfo member, Expression expression)
  522. {
  523. throw new NotImplementedException ();
  524. }
  525. [MonoTODO]
  526. public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
  527. {
  528. throw new NotImplementedException ();
  529. }
  530. public static MethodCallExpression Call (Expression instance, MethodInfo method)
  531. {
  532. return Call (instance, method, null as IEnumerable<Expression>);
  533. }
  534. public static MethodCallExpression Call (MethodInfo method, params Expression [] arguments)
  535. {
  536. return Call (null, method, arguments as IEnumerable<Expression>);
  537. }
  538. public static MethodCallExpression Call (Expression instance, MethodInfo method, params Expression [] arguments)
  539. {
  540. return Call (instance, method, arguments as IEnumerable<Expression>);
  541. }
  542. public static MethodCallExpression Call (Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
  543. {
  544. if (method == null)
  545. throw new ArgumentNullException ("method");
  546. if (instance == null && !method.IsStatic)
  547. throw new ArgumentNullException ("instance");
  548. if (instance != null && !method.DeclaringType.IsAssignableFrom (instance.Type))
  549. throw new ArgumentException ("Type is not assignable to the declaring type of the method");
  550. var args = arguments.ToReadOnlyCollection ();
  551. var parameters = method.GetParameters ();
  552. if (args.Count != parameters.Length)
  553. throw new ArgumentException ("The number of arguments doesn't match the number of parameters");
  554. // TODO: check for assignability of the arguments on the parameters
  555. return new MethodCallExpression (instance, method, args);
  556. }
  557. [MonoTODO]
  558. public static MethodCallExpression Call (Expression instance, string methodName, Type [] typeArguments, params Expression [] arguments)
  559. {
  560. throw new NotImplementedException ();
  561. }
  562. [MonoTODO]
  563. public static MethodCallExpression Call (Type type, string methodName, Type [] typeArguments, params Expression [] arguments)
  564. {
  565. throw new NotImplementedException ();
  566. }
  567. public static ConditionalExpression Condition (Expression test, Expression ifTrue, Expression ifFalse)
  568. {
  569. if (test == null)
  570. throw new ArgumentNullException ("test");
  571. if (ifTrue == null)
  572. throw new ArgumentNullException ("ifTrue");
  573. if (ifFalse == null)
  574. throw new ArgumentNullException ("ifFalse");
  575. if (test.Type != typeof (bool))
  576. throw new ArgumentException ("Test expression should be of type bool");
  577. if (ifTrue.Type != ifFalse.Type)
  578. throw new ArgumentException ("The ifTrue and ifFalse type do not match");
  579. return new ConditionalExpression (test, ifTrue, ifFalse);
  580. }
  581. public static ConstantExpression Constant (object value)
  582. {
  583. if (value == null)
  584. return new ConstantExpression (null, typeof (object));
  585. return Constant (value, value.GetType ());
  586. }
  587. public static ConstantExpression Constant (object value, Type type)
  588. {
  589. if (type == null)
  590. throw new ArgumentNullException ("type");
  591. //
  592. // value must be compatible with type, no conversions
  593. // are allowed
  594. //
  595. if (value == null){
  596. if (type.IsValueType && !IsNullable (type))
  597. throw new ArgumentException ();
  598. } else {
  599. if (!(type.IsValueType && IsNullable (type)) && value.GetType () != type)
  600. throw new ArgumentException ();
  601. }
  602. return new ConstantExpression (value, type);
  603. }
  604. [MonoTODO]
  605. public static UnaryExpression Convert (Expression expression, Type type)
  606. {
  607. throw new NotImplementedException ();
  608. }
  609. [MonoTODO]
  610. public static UnaryExpression Convert (Expression expression, Type type, MethodInfo method)
  611. {
  612. throw new NotImplementedException ();
  613. }
  614. [MonoTODO]
  615. public static UnaryExpression ConvertChecked (Expression expression, Type type)
  616. {
  617. throw new NotImplementedException ();
  618. }
  619. [MonoTODO]
  620. public static UnaryExpression ConvertChecked (Expression expression, Type type, MethodInfo method)
  621. {
  622. throw new NotImplementedException ();
  623. }
  624. [MonoTODO]
  625. public static ElementInit ElementInit (MethodInfo addMethod, params Expression [] arguments)
  626. {
  627. throw new NotImplementedException ();
  628. }
  629. [MonoTODO]
  630. public static ElementInit ElementInit (MethodInfo addMethod, IEnumerable<Expression> arguments)
  631. {
  632. throw new NotImplementedException ();
  633. }
  634. [MonoTODO]
  635. public static MemberExpression Field (Expression expression, FieldInfo field)
  636. {
  637. throw new NotImplementedException ();
  638. }
  639. [MonoTODO]
  640. public static MemberExpression Field (Expression expression, string fieldName)
  641. {
  642. throw new NotImplementedException ();
  643. }
  644. public static Type GetActionType (params Type [] typeArgs)
  645. {
  646. if (typeArgs == null)
  647. throw new ArgumentNullException ("typeArgs");
  648. if (typeArgs.Length > 4)
  649. throw new ArgumentException ("No Action type of this arity");
  650. if (typeArgs.Length == 0)
  651. return typeof (Action);
  652. Type action = null;
  653. switch (typeArgs.Length) {
  654. case 1:
  655. action = typeof (Action<>);
  656. break;
  657. case 2:
  658. action = typeof (Action<,>);
  659. break;
  660. case 3:
  661. action = typeof (Action<,,>);
  662. break;
  663. case 4:
  664. action = typeof (Action<,,,>);
  665. break;
  666. }
  667. return action.MakeGenericType (typeArgs);
  668. }
  669. public static Type GetFuncType (params Type [] typeArgs)
  670. {
  671. if (typeArgs == null)
  672. throw new ArgumentNullException ("typeArgs");
  673. if (typeArgs.Length < 1 || typeArgs.Length > 5)
  674. throw new ArgumentException ("No Func type of this arity");
  675. Type func = null;
  676. switch (typeArgs.Length) {
  677. case 1:
  678. func = typeof (Func<>);
  679. break;
  680. case 2:
  681. func = typeof (Func<,>);
  682. break;
  683. case 3:
  684. func = typeof (Func<,,>);
  685. break;
  686. case 4:
  687. func = typeof (Func<,,,>);
  688. break;
  689. case 5:
  690. func = typeof (Func<,,,,>);
  691. break;
  692. }
  693. return func.MakeGenericType (typeArgs);
  694. }
  695. [MonoTODO]
  696. public static InvocationExpression Invoke (Expression expression, params Expression [] arguments)
  697. {
  698. throw new NotImplementedException ();
  699. }
  700. [MonoTODO]
  701. public static InvocationExpression Invoke (Expression expression, IEnumerable<Expression> arguments)
  702. {
  703. throw new NotImplementedException ();
  704. }
  705. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, params ParameterExpression [] parameters)
  706. {
  707. if (body == null)
  708. throw new ArgumentNullException ("body");
  709. return new Expression<TDelegate> (body, parameters);
  710. }
  711. [MonoTODO]
  712. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, IEnumerable<ParameterExpression> parameters)
  713. {
  714. throw new NotImplementedException ();
  715. }
  716. [MonoTODO]
  717. public static LambdaExpression Lambda (Expression body, params ParameterExpression [] parameters)
  718. {
  719. throw new NotImplementedException ();
  720. }
  721. public static LambdaExpression Lambda (Type delegateType, Expression body, params ParameterExpression [] parameters)
  722. {
  723. return Lambda (delegateType, body, parameters as IEnumerable<ParameterExpression>);
  724. }
  725. [MonoTODO]
  726. public static LambdaExpression Lambda (Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
  727. {
  728. if (delegateType == null)
  729. throw new ArgumentNullException ("delegateType");
  730. if (body == null)
  731. throw new ArgumentNullException ("body");
  732. return new LambdaExpression (delegateType, body, parameters.ToReadOnlyCollection ());
  733. }
  734. public static MemberListBinding ListBind (MemberInfo member, params ElementInit [] initializers)
  735. {
  736. throw new NotImplementedException ();
  737. }
  738. [MonoTODO]
  739. public static MemberListBinding ListBind (MemberInfo member, IEnumerable<ElementInit> initializers)
  740. {
  741. throw new NotImplementedException ();
  742. }
  743. [MonoTODO]
  744. public static MemberListBinding ListBind (MethodInfo propertyAccessor, params ElementInit [] initializers)
  745. {
  746. throw new NotImplementedException ();
  747. }
  748. [MonoTODO]
  749. public static MemberListBinding ListBind (MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers)
  750. {
  751. throw new NotImplementedException ();
  752. }
  753. [MonoTODO]
  754. public static ListInitExpression ListInit (NewExpression newExpression, params ElementInit [] initializers)
  755. {
  756. throw new NotImplementedException ();
  757. }
  758. [MonoTODO]
  759. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<ElementInit> initializers)
  760. {
  761. throw new NotImplementedException ();
  762. }
  763. [MonoTODO]
  764. public static ListInitExpression ListInit (NewExpression newExpression, params Expression [] initializers)
  765. {
  766. throw new NotImplementedException ();
  767. }
  768. [MonoTODO]
  769. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<Expression> initializers)
  770. {
  771. throw new NotImplementedException ();
  772. }
  773. [MonoTODO]
  774. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, params Expression [] initializers)
  775. {
  776. throw new NotImplementedException ();
  777. }
  778. [MonoTODO]
  779. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, IEnumerable<Expression> initializers)
  780. {
  781. throw new NotImplementedException ();
  782. }
  783. public static MemberExpression MakeMemberAccess (Expression expression, MemberInfo member)
  784. {
  785. if (expression == null)
  786. throw new ArgumentNullException ("expression");
  787. if (member == null)
  788. throw new ArgumentNullException ("member");
  789. var field = member as FieldInfo;
  790. if (field != null)
  791. return Field (expression, field);
  792. var property = member as PropertyInfo;
  793. if (property != null)
  794. return Property (expression, property);
  795. throw new ArgumentException ("Member should either be a field or a property");
  796. }
  797. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type)
  798. {
  799. return MakeUnary (unaryType, operand, type, null);
  800. }
  801. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type, MethodInfo method)
  802. {
  803. switch (unaryType) {
  804. case ExpressionType.ArrayLength:
  805. return ArrayLength (operand);
  806. case ExpressionType.Convert:
  807. return Convert (operand, type, method);
  808. case ExpressionType.ConvertChecked:
  809. return ConvertChecked (operand, type, method);
  810. case ExpressionType.Negate:
  811. return Negate (operand, method);
  812. case ExpressionType.NegateChecked:
  813. return NegateChecked (operand, method);
  814. case ExpressionType.Not:
  815. return Not (operand, method);
  816. case ExpressionType.Quote:
  817. return Quote (operand);
  818. case ExpressionType.TypeAs:
  819. return TypeAs (operand, type);
  820. case ExpressionType.UnaryPlus:
  821. return UnaryPlus (operand, method);
  822. }
  823. throw new ArgumentException ("MakeUnary expect an unary operator");
  824. }
  825. [MonoTODO]
  826. public static MemberMemberBinding MemberBind (MemberInfo member, params MemberBinding [] binding)
  827. {
  828. throw new NotImplementedException ();
  829. }
  830. [MonoTODO]
  831. public static MemberMemberBinding MemberBind (MemberInfo member, IEnumerable<MemberBinding> binding)
  832. {
  833. throw new NotImplementedException ();
  834. }
  835. [MonoTODO]
  836. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, params MemberBinding [] binding)
  837. {
  838. throw new NotImplementedException ();
  839. }
  840. [MonoTODO]
  841. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, IEnumerable<MemberBinding> binding)
  842. {
  843. throw new NotImplementedException ();
  844. }
  845. [MonoTODO]
  846. public static MemberInitExpression MemberInit (NewExpression newExpression, params MemberBinding [] binding)
  847. {
  848. throw new NotImplementedException ();
  849. }
  850. [MonoTODO]
  851. public static MemberInitExpression MemberInit (NewExpression newExpression, IEnumerable<MemberBinding> binding)
  852. {
  853. throw new NotImplementedException ();
  854. }
  855. [MonoTODO]
  856. public static UnaryExpression Negate (Expression expression)
  857. {
  858. throw new NotImplementedException ();
  859. }
  860. [MonoTODO]
  861. public static UnaryExpression Negate (Expression expression, MethodInfo method)
  862. {
  863. throw new NotImplementedException ();
  864. }
  865. [MonoTODO]
  866. public static UnaryExpression NegateChecked (Expression expression)
  867. {
  868. throw new NotImplementedException ();
  869. }
  870. [MonoTODO]
  871. public static UnaryExpression NegateChecked (Expression expression, MethodInfo method)
  872. {
  873. throw new NotImplementedException ();
  874. }
  875. [MonoTODO]
  876. public static NewExpression New (ConstructorInfo constructor)
  877. {
  878. throw new NotImplementedException ();
  879. }
  880. [MonoTODO]
  881. public static NewExpression New (Type type)
  882. {
  883. throw new NotImplementedException ();
  884. }
  885. [MonoTODO]
  886. public static NewExpression New (ConstructorInfo constructor, params Expression [] arguments)
  887. {
  888. throw new NotImplementedException ();
  889. }
  890. [MonoTODO]
  891. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments)
  892. {
  893. throw new NotImplementedException ();
  894. }
  895. [MonoTODO]
  896. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, params MemberInfo [] members)
  897. {
  898. throw new NotImplementedException ();
  899. }
  900. [MonoTODO]
  901. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, IEnumerable<MemberInfo> members)
  902. {
  903. throw new NotImplementedException ();
  904. }
  905. [MonoTODO]
  906. public static NewArrayExpression NewArrayBounds (Type type, params Expression [] bounds)
  907. {
  908. throw new NotImplementedException ();
  909. }
  910. [MonoTODO]
  911. public static NewArrayExpression NewArrayBounds (Type type, IEnumerable<Expression> bounds)
  912. {
  913. throw new NotImplementedException ();
  914. }
  915. [MonoTODO]
  916. public static NewArrayExpression NewArrayInit (Type type, params Expression [] bounds)
  917. {
  918. throw new NotImplementedException ();
  919. }
  920. [MonoTODO]
  921. public static NewArrayExpression NewArrayInit (Type type, IEnumerable<Expression> bounds)
  922. {
  923. throw new NotImplementedException ();
  924. }
  925. [MonoTODO]
  926. public static UnaryExpression Not (Expression expression)
  927. {
  928. throw new NotImplementedException ();
  929. }
  930. [MonoTODO]
  931. public static UnaryExpression Not (Expression expression, MethodInfo method)
  932. {
  933. throw new NotImplementedException ();
  934. }
  935. public static ParameterExpression Parameter (Type type, string name)
  936. {
  937. if (type == null)
  938. throw new ArgumentNullException ("type");
  939. return new ParameterExpression (type, name);
  940. }
  941. [MonoTODO]
  942. public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
  943. {
  944. throw new NotImplementedException ();
  945. }
  946. [MonoTODO]
  947. public static MemberExpression Property (Expression expression, PropertyInfo property)
  948. {
  949. throw new NotImplementedException ();
  950. }
  951. [MonoTODO]
  952. public static MemberExpression Property (Expression expression, string propertyName)
  953. {
  954. throw new NotImplementedException ();
  955. }
  956. [MonoTODO]
  957. public static MemberExpression PropertyOrField (Expression expression, string propertyOrFieldName)
  958. {
  959. throw new NotImplementedException ();
  960. }
  961. public static UnaryExpression Quote (Expression expression)
  962. {
  963. if (expression == null)
  964. throw new ArgumentNullException ("expression");
  965. return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType ());
  966. }
  967. public static UnaryExpression TypeAs (Expression expression, Type type)
  968. {
  969. if (expression == null)
  970. throw new ArgumentNullException ("expression");
  971. if (type == null)
  972. throw new ArgumentNullException ("type");
  973. if (type.IsValueType && !IsNullable (type))
  974. throw new ArgumentException ("TypeAs expect a reference or a nullable type");
  975. return new UnaryExpression (ExpressionType.TypeAs, expression, type);
  976. }
  977. public static TypeBinaryExpression TypeIs (Expression expression, Type type)
  978. {
  979. if (expression == null)
  980. throw new ArgumentNullException ("expression");
  981. if (type == null)
  982. throw new ArgumentNullException ("type");
  983. return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof (bool));
  984. }
  985. [MonoTODO]
  986. public static UnaryExpression UnaryPlus (Expression expression)
  987. {
  988. throw new NotImplementedException ();
  989. }
  990. [MonoTODO]
  991. public static UnaryExpression UnaryPlus (Expression expression, MethodInfo method)
  992. {
  993. throw new NotImplementedException ();
  994. }
  995. static bool IsNullable (Type type)
  996. {
  997. return type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>);
  998. }
  999. //
  1000. // This method must be overwritten by derived classes to
  1001. // compile the expression
  1002. //
  1003. internal abstract void Emit (EmitContext ec);
  1004. }
  1005. }