Expression.cs 36 KB

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