Expression.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. if (oper_name == "op_BitwiseOr" || oper_name == "op_BitwiseAnd"){
  172. if (ultype == typeof (bool)){
  173. if (ultype == urtype && ltype == rtype)
  174. return null;
  175. }
  176. }
  177. if (oper_name == "op_LogicalAnd" || oper_name == "op_LogicalOr"){
  178. if (ultype == typeof (bool)){
  179. if (ultype == urtype && ltype == rtype)
  180. return null;
  181. method = GetBinaryOperator (oper_name, rtype, left, right);
  182. if (method != null)
  183. return method;
  184. }
  185. } else {
  186. // Use IsNumber to avoid expensive reflection.
  187. if (IsNumber (ultype)){
  188. if (ultype == urtype && ltype == rtype)
  189. return method;
  190. if (oper_name != null){
  191. method = GetBinaryOperator (oper_name, rtype, left, right);
  192. if (method != null)
  193. return method;
  194. }
  195. }
  196. }
  197. if (oper_name != null){
  198. method = GetBinaryOperator (oper_name, ltype, left, right);
  199. if (method != null)
  200. return method;
  201. }
  202. //
  203. // == and != allow reference types without operators defined.
  204. //
  205. if (!ltype.IsValueType && !rtype.IsValueType &&
  206. (oper_name == "op_Equality" || oper_name == "op_Inequality"))
  207. return null;
  208. throw new InvalidOperationException (
  209. String.Format ("Operation {0} not defined for {1} and {2}", oper_name != null ? oper_name.Substring (3) : "is", ltype, rtype));
  210. }
  211. }
  212. //
  213. // This is like BinaryCoreCheck, but if no method is used adds the restriction that
  214. // only ints and bools are allowed
  215. //
  216. static MethodInfo BinaryBitwiseCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
  217. {
  218. if (left == null)
  219. throw new ArgumentNullException ("left");
  220. if (right == null)
  221. throw new ArgumentNullException ("right");
  222. if (method == null){
  223. // avoid reflection shortcut and catches Ints/bools before we check Numbers in general
  224. if (left.Type == right.Type && (left.Type == typeof (bool) || IsInt (left.Type)))
  225. return method;
  226. }
  227. method = BinaryCoreCheck (oper_name, left, right, method);
  228. if (method == null){
  229. //
  230. // The check in BinaryCoreCheck allows a bit more than we do
  231. // (floats and doubles). Catch this here
  232. //
  233. if (left.Type == typeof(double) || left.Type == typeof(float))
  234. throw new InvalidOperationException ("Types not supported");
  235. }
  236. return method;
  237. }
  238. static BinaryExpression MakeSimpleBinary (ExpressionType et, Expression left, Expression right, MethodInfo method)
  239. {
  240. Type result = method == null ? left.Type : method.ReturnType;
  241. bool is_lifted;
  242. if (method == null){
  243. if (IsNullable (left.Type)){
  244. if (!IsNullable (right.Type))
  245. throw new Exception ("Assertion, internal error: left is nullable, requires right to be as well");
  246. is_lifted = true;
  247. } else
  248. is_lifted = false;
  249. } else {
  250. //
  251. // FIXME: implement
  252. //
  253. is_lifted = false;
  254. }
  255. return new BinaryExpression (et, result, left, right, false, is_lifted, method, null);
  256. }
  257. static UnaryExpression MakeSimpleUnary (ExpressionType et, Expression expression, MethodInfo method)
  258. {
  259. Type result = method == null ? expression.Type : method.ReturnType;
  260. return new UnaryExpression (et, expression, result, method);
  261. }
  262. static BinaryExpression MakeBoolBinary (ExpressionType et, Expression left, Expression right, bool liftToNull, MethodInfo method)
  263. {
  264. Type result;
  265. Type ltype = left.Type;
  266. Type rtype = right.Type;
  267. bool lnullable = IsNullable (ltype);
  268. bool rnullable = IsNullable (rtype);
  269. bool is_lifted;
  270. //
  271. // Implement the rules as described in "Expression.Equal" method.
  272. //
  273. if (method == null){
  274. if (lnullable == false && rnullable == false){
  275. is_lifted = false;
  276. result = typeof (bool);
  277. } else if (lnullable && rnullable){
  278. is_lifted = true;
  279. result = liftToNull ? typeof(bool?) : typeof (bool);
  280. } else
  281. throw new Exception ("Internal error: this should have been caught in BinaryCoreCheck");
  282. } else {
  283. ParameterInfo [] pi = method.GetParameters ();
  284. Type mltype = pi [0].ParameterType;
  285. Type mrtype = pi [1].ParameterType;
  286. if (ltype == mltype && rtype == mrtype){
  287. is_lifted = false;
  288. result = method.ReturnType;
  289. }
  290. else if (ltype.IsValueType && rtype.IsValueType &&
  291. ((lnullable && GetNullableOf (ltype) == mltype) ||
  292. (rnullable && GetNullableOf (rtype) == mrtype))){
  293. is_lifted = true;
  294. if (method.ReturnType == typeof(bool)){
  295. result = liftToNull ? typeof(bool?) : typeof(bool);
  296. } else {
  297. //
  298. // This behavior is not documented: what
  299. // happens if the result is not typeof(bool), but
  300. // the parameters are nullable: the result
  301. // becomes nullable<returntype>
  302. //
  303. // See:
  304. // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=323139
  305. result = typeof (Nullable<>).MakeGenericType (method.ReturnType);
  306. //Type.GetType ("System.Nullable`1[" + method.ReturnType.ToString () + "]");
  307. }
  308. } else {
  309. is_lifted = false;
  310. result = method.ReturnType;
  311. }
  312. }
  313. return new BinaryExpression (et, result, left, right, liftToNull, is_lifted, method, null);
  314. }
  315. //
  316. // Arithmetic
  317. //
  318. public static BinaryExpression Add (Expression left, Expression right)
  319. {
  320. return Add (left, right, null);
  321. }
  322. public static BinaryExpression Add (Expression left, Expression right, MethodInfo method)
  323. {
  324. method = BinaryCoreCheck ("op_Addition", left, right, method);
  325. return MakeSimpleBinary (ExpressionType.Add, left, right, method);
  326. }
  327. public static BinaryExpression AddChecked (Expression left, Expression right)
  328. {
  329. return AddChecked (left, right, null);
  330. }
  331. public static BinaryExpression AddChecked (Expression left, Expression right, MethodInfo method)
  332. {
  333. method = BinaryCoreCheck ("op_Addition", left, right, method);
  334. //
  335. // The check in BinaryCoreCheck allows a bit more than we do
  336. // (byte, sbyte). Catch that here
  337. //
  338. if (method == null){
  339. Type ltype = left.Type;
  340. if (ltype == typeof (byte) || ltype == typeof (sbyte))
  341. throw new InvalidOperationException (String.Format ("SubtractChecked not defined for {0} and {1}", left.Type, right.Type));
  342. }
  343. return MakeSimpleBinary (ExpressionType.AddChecked, left, right, method);
  344. }
  345. public static BinaryExpression Subtract (Expression left, Expression right)
  346. {
  347. return Subtract (left, right, null);
  348. }
  349. public static BinaryExpression Subtract (Expression left, Expression right, MethodInfo method)
  350. {
  351. method = BinaryCoreCheck ("op_Subtraction", left, right, method);
  352. return MakeSimpleBinary (ExpressionType.Subtract, left, right, method);
  353. }
  354. public static BinaryExpression SubtractChecked (Expression left, Expression right)
  355. {
  356. return SubtractChecked (left, right, null);
  357. }
  358. public static BinaryExpression SubtractChecked (Expression left, Expression right, MethodInfo method)
  359. {
  360. method = BinaryCoreCheck ("op_Subtraction", left, right, method);
  361. //
  362. // The check in BinaryCoreCheck allows a bit more than we do
  363. // (byte, sbyte). Catch that here
  364. //
  365. if (method == null){
  366. Type ltype = left.Type;
  367. if (ltype == typeof (byte) || ltype == typeof (sbyte))
  368. throw new InvalidOperationException (String.Format ("SubtractChecked not defined for {0} and {1}", left.Type, right.Type));
  369. }
  370. return MakeSimpleBinary (ExpressionType.SubtractChecked, left, right, method);
  371. }
  372. public static BinaryExpression Modulo (Expression left, Expression right)
  373. {
  374. return Modulo (left, right, null);
  375. }
  376. public static BinaryExpression Modulo (Expression left, Expression right, MethodInfo method)
  377. {
  378. method = BinaryCoreCheck ("op_Modulus", left, right, method);
  379. return MakeSimpleBinary (ExpressionType.Modulo, left, right, method);
  380. }
  381. public static BinaryExpression Multiply (Expression left, Expression right)
  382. {
  383. return Multiply (left, right, null);
  384. }
  385. public static BinaryExpression Multiply (Expression left, Expression right, MethodInfo method)
  386. {
  387. method = BinaryCoreCheck ("op_Multiply", left, right, method);
  388. return MakeSimpleBinary (ExpressionType.Multiply, left, right, method);
  389. }
  390. public static BinaryExpression MultiplyChecked (Expression left, Expression right)
  391. {
  392. return MultiplyChecked (left, right, null);
  393. }
  394. public static BinaryExpression MultiplyChecked (Expression left, Expression right, MethodInfo method)
  395. {
  396. method = BinaryCoreCheck ("op_Multiply", left, right, method);
  397. return MakeSimpleBinary (ExpressionType.MultiplyChecked, left, right, method);
  398. }
  399. public static BinaryExpression Divide (Expression left, Expression right)
  400. {
  401. return Divide (left, right, null);
  402. }
  403. public static BinaryExpression Divide (Expression left, Expression right, MethodInfo method)
  404. {
  405. method = BinaryCoreCheck ("op_Division", left, right, method);
  406. return MakeSimpleBinary (ExpressionType.Divide, left, right, method);
  407. }
  408. public static BinaryExpression Power (Expression left, Expression right)
  409. {
  410. return Power (left, right, null);
  411. }
  412. public static BinaryExpression Power (Expression left, Expression right, MethodInfo method)
  413. {
  414. method = BinaryCoreCheck (null, left, right, method);
  415. if (left.Type != typeof (double))
  416. throw new InvalidOperationException ("Power only supports double arguments");
  417. return MakeSimpleBinary (ExpressionType.Power, left, right, method);
  418. }
  419. //
  420. // Bitwise
  421. //
  422. public static BinaryExpression And (Expression left, Expression right)
  423. {
  424. return And (left, right, null);
  425. }
  426. public static BinaryExpression And (Expression left, Expression right, MethodInfo method)
  427. {
  428. method = BinaryBitwiseCoreCheck ("op_BitwiseAnd", left, right, method);
  429. return MakeSimpleBinary (ExpressionType.And, left, right, method);
  430. }
  431. public static BinaryExpression Or (Expression left, Expression right)
  432. {
  433. return Or (left, right, null);
  434. }
  435. public static BinaryExpression Or (Expression left, Expression right, MethodInfo method)
  436. {
  437. method = BinaryBitwiseCoreCheck ("op_BitwiseOr", left, right, method);
  438. return MakeSimpleBinary (ExpressionType.Or, left, right, method);
  439. }
  440. public static BinaryExpression ExclusiveOr (Expression left, Expression right)
  441. {
  442. return ExclusiveOr (left, right, null);
  443. }
  444. public static BinaryExpression ExclusiveOr (Expression left, Expression right, MethodInfo method)
  445. {
  446. method = BinaryBitwiseCoreCheck ("op_ExclusiveOr", left, right, method);
  447. return MakeSimpleBinary (ExpressionType.ExclusiveOr, left, right, method);
  448. }
  449. public static BinaryExpression LeftShift (Expression left, Expression right)
  450. {
  451. return LeftShift (left, right, null);
  452. }
  453. public static BinaryExpression LeftShift (Expression left, Expression right, MethodInfo method)
  454. {
  455. method = BinaryBitwiseCoreCheck ("op_LeftShift", left, right, method);
  456. return MakeSimpleBinary (ExpressionType.LeftShift, left, right, method);
  457. }
  458. public static BinaryExpression RightShift (Expression left, Expression right)
  459. {
  460. return RightShift (left, right, null);
  461. }
  462. public static BinaryExpression RightShift (Expression left, Expression right, MethodInfo method)
  463. {
  464. method = BinaryCoreCheck ("op_RightShift", left, right, method);
  465. return MakeSimpleBinary (ExpressionType.RightShift, left, right, method);
  466. }
  467. //
  468. // Short-circuit
  469. //
  470. public static BinaryExpression AndAlso (Expression left, Expression right)
  471. {
  472. return AndAlso (left, right, null);
  473. }
  474. public static BinaryExpression AndAlso (Expression left, Expression right, MethodInfo method)
  475. {
  476. method = BinaryCoreCheck ("op_LogicalAnd", left, right, method);
  477. return MakeBoolBinary (ExpressionType.AndAlso, left, right, false, method);
  478. }
  479. public static BinaryExpression OrElse (Expression left, Expression right)
  480. {
  481. return OrElse (left, right, null);
  482. }
  483. public static BinaryExpression OrElse (Expression left, Expression right, MethodInfo method)
  484. {
  485. method = BinaryCoreCheck ("op_LogicalOr", left, right, method);
  486. return MakeBoolBinary (ExpressionType.OrElse, left, right, false, method);
  487. }
  488. //
  489. // Comparison
  490. //
  491. public static BinaryExpression Equal (Expression left, Expression right)
  492. {
  493. return Equal (left, right, false, null);
  494. }
  495. public static BinaryExpression Equal (Expression left, Expression right, bool liftToNull, MethodInfo method)
  496. {
  497. method = BinaryCoreCheck ("op_Equality", left, right, method);
  498. return MakeBoolBinary (ExpressionType.Equal, left, right, liftToNull, method);
  499. }
  500. public static BinaryExpression NotEqual (Expression left, Expression right)
  501. {
  502. return NotEqual (left, right, false, null);
  503. }
  504. public static BinaryExpression NotEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  505. {
  506. method = BinaryCoreCheck ("op_Inequality", left, right, method);
  507. return MakeBoolBinary (ExpressionType.NotEqual, left, right, liftToNull, method);
  508. }
  509. public static BinaryExpression GreaterThan (Expression left, Expression right)
  510. {
  511. return GreaterThan (left, right, false, null);
  512. }
  513. public static BinaryExpression GreaterThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  514. {
  515. method = BinaryCoreCheck ("op_GreaterThan", left, right, method);
  516. return MakeBoolBinary (ExpressionType.GreaterThan, left, right, liftToNull, method);
  517. }
  518. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right)
  519. {
  520. return GreaterThanOrEqual (left, right, false, null);
  521. }
  522. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  523. {
  524. method = BinaryCoreCheck ("op_GreaterThanOrEqual", left, right, method);
  525. return MakeBoolBinary (ExpressionType.GreaterThanOrEqual, left, right, liftToNull, method);
  526. }
  527. public static BinaryExpression LessThan (Expression left, Expression right)
  528. {
  529. return LessThan (left, right, false, null);
  530. }
  531. public static BinaryExpression LessThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  532. {
  533. method = BinaryCoreCheck ("op_LessThan", left, right, method);
  534. return MakeBoolBinary (ExpressionType.LessThan, left, right, liftToNull, method);
  535. }
  536. public static BinaryExpression LessThanOrEqual (Expression left, Expression right)
  537. {
  538. return LessThanOrEqual (left, right, false, null);
  539. }
  540. public static BinaryExpression LessThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  541. {
  542. method = BinaryCoreCheck ("op_LessThanOrEqual", left, right, method);
  543. return MakeBoolBinary (ExpressionType.LessThanOrEqual, left, right, liftToNull, method);
  544. }
  545. //
  546. // Miscelaneous
  547. //
  548. static void ArrayCheck (Expression array)
  549. {
  550. if (array == null)
  551. throw new ArgumentNullException ("array");
  552. if (!array.Type.IsArray)
  553. throw new ArgumentException ("The array argument must be of type array");
  554. }
  555. public static BinaryExpression ArrayIndex (Expression array, Expression index)
  556. {
  557. ArrayCheck (array);
  558. if (index == null)
  559. throw new ArgumentNullException ("index");
  560. if (array.Type.GetArrayRank () != 1)
  561. throw new ArgumentException ("The array argument must be a single dimensional array");
  562. if (index.Type != typeof (int))
  563. throw new ArgumentException ("The index must be of type int");
  564. return new BinaryExpression (ExpressionType.ArrayIndex, array.Type.GetElementType (), array, index);
  565. }
  566. public static BinaryExpression Coalesce (Expression left, Expression right)
  567. {
  568. return Coalesce (left, right, null);
  569. }
  570. public static BinaryExpression Coalesce (Expression left, Expression right, LambdaExpression conversion)
  571. {
  572. if (left == null)
  573. throw new ArgumentNullException ("left");
  574. if (right == null)
  575. throw new ArgumentNullException ("right");
  576. //
  577. // First arg must ne nullable (either Nullable<T> or a reference type
  578. //
  579. if (left.Type.IsValueType && !IsNullable (left.Type))
  580. throw new InvalidOperationException ("Left expression can never be null");
  581. Type result = null;
  582. if (IsNullable (left.Type)){
  583. Type lbase = GetNullableOf (left.Type);
  584. if (!IsNullable (right.Type) && lbase.IsAssignableFrom (right.Type))
  585. result = lbase;
  586. }
  587. if (result == null && left.Type.IsAssignableFrom (right.Type))
  588. result = left.Type;
  589. if (result == null){
  590. if (IsNullable (left.Type) && right.Type.IsAssignableFrom (GetNullableOf (left.Type))){
  591. result = right.Type;
  592. }
  593. }
  594. if (result == null)
  595. throw new ArgumentException ("Incompatible argument types");
  596. //
  597. // FIXME: What do we do with "conversion"?
  598. //
  599. return new BinaryExpression (ExpressionType.Coalesce, result, left, right, false, false, null, conversion);
  600. }
  601. //
  602. // MakeBinary constructors
  603. //
  604. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right)
  605. {
  606. return MakeBinary (binaryType, left, right, false, null);
  607. }
  608. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method)
  609. {
  610. return MakeBinary (binaryType, left, right, liftToNull, method, null);
  611. }
  612. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method, LambdaExpression conversion)
  613. {
  614. switch (binaryType) {
  615. case ExpressionType.Add:
  616. return Add (left, right, method);
  617. case ExpressionType.AddChecked:
  618. return AddChecked (left, right, method);
  619. case ExpressionType.AndAlso:
  620. return AndAlso (left, right);
  621. case ExpressionType.Coalesce:
  622. return Coalesce (left, right, conversion);
  623. case ExpressionType.Divide:
  624. return Divide (left, right, method);
  625. case ExpressionType.Equal:
  626. return Equal (left, right, liftToNull, method);
  627. case ExpressionType.ExclusiveOr:
  628. return ExclusiveOr (left, right, method);
  629. case ExpressionType.GreaterThan:
  630. return GreaterThan (left, right, liftToNull, method);
  631. case ExpressionType.GreaterThanOrEqual:
  632. return GreaterThanOrEqual (left, right, liftToNull, method);
  633. case ExpressionType.LeftShift:
  634. return LeftShift (left, right, method);
  635. case ExpressionType.LessThan:
  636. return LessThan (left, right, liftToNull, method);
  637. case ExpressionType.LessThanOrEqual:
  638. return LessThanOrEqual (left, right, liftToNull, method);
  639. case ExpressionType.Modulo:
  640. return Modulo (left, right, method);
  641. case ExpressionType.Multiply:
  642. return Multiply (left, right, method);
  643. case ExpressionType.MultiplyChecked:
  644. return MultiplyChecked (left, right, method);
  645. case ExpressionType.NotEqual:
  646. return NotEqual (left, right, liftToNull, method);
  647. case ExpressionType.OrElse:
  648. return OrElse (left, right);
  649. case ExpressionType.Power:
  650. return Power (left, right, method);
  651. case ExpressionType.RightShift:
  652. return RightShift (left, right, method);
  653. case ExpressionType.Subtract:
  654. return Subtract (left, right, method);
  655. case ExpressionType.SubtractChecked:
  656. return SubtractChecked (left, right, method);
  657. case ExpressionType.And:
  658. return And (left, right, method);
  659. case ExpressionType.Or:
  660. return Or (left, right, method);
  661. }
  662. throw new ArgumentException ("MakeBinary expect a binary node type");
  663. }
  664. #endregion
  665. public static MethodCallExpression ArrayIndex (Expression array, params Expression [] indexes)
  666. {
  667. return ArrayIndex (array, indexes as IEnumerable<Expression>);
  668. }
  669. public static MethodCallExpression ArrayIndex (Expression array, IEnumerable<Expression> indexes)
  670. {
  671. ArrayCheck (array);
  672. if (indexes == null)
  673. throw new ArgumentNullException ("indexes");
  674. var args = indexes.ToReadOnlyCollection ();
  675. if (array.Type.GetArrayRank () != args.Count)
  676. throw new ArgumentException ("The number of arguments doesn't match the rank of the array");
  677. foreach (var arg in args)
  678. if (arg.Type != typeof (int))
  679. throw new ArgumentException ("The index must be of type int");
  680. return Call (array, array.Type.GetMethod ("Get", PublicInstance), args);
  681. }
  682. public static UnaryExpression ArrayLength (Expression array)
  683. {
  684. if (array == null)
  685. throw new ArgumentNullException ("array");
  686. if (!array.Type.IsArray)
  687. throw new ArgumentException ("The type of the expression must me Array");
  688. if (array.Type.GetArrayRank () != 1)
  689. throw new ArgumentException ("The array must be a single dimensional array");
  690. return new UnaryExpression (ExpressionType.ArrayLength, array, typeof (int));
  691. }
  692. [MonoTODO]
  693. public static MemberAssignment Bind (MemberInfo member, Expression expression)
  694. {
  695. throw new NotImplementedException ();
  696. }
  697. [MonoTODO]
  698. public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
  699. {
  700. throw new NotImplementedException ();
  701. }
  702. public static MethodCallExpression Call (Expression instance, MethodInfo method)
  703. {
  704. return Call (instance, method, null as IEnumerable<Expression>);
  705. }
  706. public static MethodCallExpression Call (MethodInfo method, params Expression [] arguments)
  707. {
  708. return Call (null, method, arguments as IEnumerable<Expression>);
  709. }
  710. public static MethodCallExpression Call (Expression instance, MethodInfo method, params Expression [] arguments)
  711. {
  712. return Call (instance, method, arguments as IEnumerable<Expression>);
  713. }
  714. public static MethodCallExpression Call (Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
  715. {
  716. if (method == null)
  717. throw new ArgumentNullException ("method");
  718. if (instance == null && !method.IsStatic)
  719. throw new ArgumentNullException ("instance");
  720. if (instance != null && !method.DeclaringType.IsAssignableFrom (instance.Type))
  721. throw new ArgumentException ("Type is not assignable to the declaring type of the method");
  722. var args = arguments.ToReadOnlyCollection ();
  723. var parameters = method.GetParameters ();
  724. if (args.Count != parameters.Length)
  725. throw new ArgumentException ("The number of arguments doesn't match the number of parameters");
  726. // TODO: check for assignability of the arguments on the parameters
  727. return new MethodCallExpression (instance, method, args);
  728. }
  729. [MonoTODO]
  730. public static MethodCallExpression Call (Expression instance, string methodName, Type [] typeArguments, params Expression [] arguments)
  731. {
  732. throw new NotImplementedException ();
  733. }
  734. [MonoTODO]
  735. public static MethodCallExpression Call (Type type, string methodName, Type [] typeArguments, params Expression [] arguments)
  736. {
  737. throw new NotImplementedException ();
  738. }
  739. public static ConditionalExpression Condition (Expression test, Expression ifTrue, Expression ifFalse)
  740. {
  741. if (test == null)
  742. throw new ArgumentNullException ("test");
  743. if (ifTrue == null)
  744. throw new ArgumentNullException ("ifTrue");
  745. if (ifFalse == null)
  746. throw new ArgumentNullException ("ifFalse");
  747. if (test.Type != typeof (bool))
  748. throw new ArgumentException ("Test expression should be of type bool");
  749. if (ifTrue.Type != ifFalse.Type)
  750. throw new ArgumentException ("The ifTrue and ifFalse type do not match");
  751. return new ConditionalExpression (test, ifTrue, ifFalse);
  752. }
  753. public static ConstantExpression Constant (object value)
  754. {
  755. if (value == null)
  756. return new ConstantExpression (null, typeof (object));
  757. return Constant (value, value.GetType ());
  758. }
  759. public static ConstantExpression Constant (object value, Type type)
  760. {
  761. if (type == null)
  762. throw new ArgumentNullException ("type");
  763. //
  764. // value must be compatible with type, no conversions
  765. // are allowed
  766. //
  767. if (value == null){
  768. if (type.IsValueType && !IsNullable (type))
  769. throw new ArgumentException ();
  770. } else {
  771. if (!(type.IsValueType && IsNullable (type)) && value.GetType () != type)
  772. throw new ArgumentException ();
  773. }
  774. return new ConstantExpression (value, type);
  775. }
  776. [MonoTODO]
  777. public static UnaryExpression Convert (Expression expression, Type type)
  778. {
  779. throw new NotImplementedException ();
  780. }
  781. [MonoTODO]
  782. public static UnaryExpression Convert (Expression expression, Type type, MethodInfo method)
  783. {
  784. throw new NotImplementedException ();
  785. }
  786. [MonoTODO]
  787. public static UnaryExpression ConvertChecked (Expression expression, Type type)
  788. {
  789. throw new NotImplementedException ();
  790. }
  791. [MonoTODO]
  792. public static UnaryExpression ConvertChecked (Expression expression, Type type, MethodInfo method)
  793. {
  794. throw new NotImplementedException ();
  795. }
  796. [MonoTODO]
  797. public static ElementInit ElementInit (MethodInfo addMethod, params Expression [] arguments)
  798. {
  799. throw new NotImplementedException ();
  800. }
  801. [MonoTODO]
  802. public static ElementInit ElementInit (MethodInfo addMethod, IEnumerable<Expression> arguments)
  803. {
  804. throw new NotImplementedException ();
  805. }
  806. [MonoTODO]
  807. public static MemberExpression Field (Expression expression, FieldInfo field)
  808. {
  809. throw new NotImplementedException ();
  810. }
  811. [MonoTODO]
  812. public static MemberExpression Field (Expression expression, string fieldName)
  813. {
  814. throw new NotImplementedException ();
  815. }
  816. public static Type GetActionType (params Type [] typeArgs)
  817. {
  818. if (typeArgs == null)
  819. throw new ArgumentNullException ("typeArgs");
  820. if (typeArgs.Length > 4)
  821. throw new ArgumentException ("No Action type of this arity");
  822. if (typeArgs.Length == 0)
  823. return typeof (Action);
  824. Type action = null;
  825. switch (typeArgs.Length) {
  826. case 1:
  827. action = typeof (Action<>);
  828. break;
  829. case 2:
  830. action = typeof (Action<,>);
  831. break;
  832. case 3:
  833. action = typeof (Action<,,>);
  834. break;
  835. case 4:
  836. action = typeof (Action<,,,>);
  837. break;
  838. }
  839. return action.MakeGenericType (typeArgs);
  840. }
  841. public static Type GetFuncType (params Type [] typeArgs)
  842. {
  843. if (typeArgs == null)
  844. throw new ArgumentNullException ("typeArgs");
  845. if (typeArgs.Length < 1 || typeArgs.Length > 5)
  846. throw new ArgumentException ("No Func type of this arity");
  847. Type func = null;
  848. switch (typeArgs.Length) {
  849. case 1:
  850. func = typeof (Func<>);
  851. break;
  852. case 2:
  853. func = typeof (Func<,>);
  854. break;
  855. case 3:
  856. func = typeof (Func<,,>);
  857. break;
  858. case 4:
  859. func = typeof (Func<,,,>);
  860. break;
  861. case 5:
  862. func = typeof (Func<,,,,>);
  863. break;
  864. }
  865. return func.MakeGenericType (typeArgs);
  866. }
  867. [MonoTODO]
  868. public static InvocationExpression Invoke (Expression expression, params Expression [] arguments)
  869. {
  870. throw new NotImplementedException ();
  871. }
  872. [MonoTODO]
  873. public static InvocationExpression Invoke (Expression expression, IEnumerable<Expression> arguments)
  874. {
  875. throw new NotImplementedException ();
  876. }
  877. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, params ParameterExpression [] parameters)
  878. {
  879. if (body == null)
  880. throw new ArgumentNullException ("body");
  881. return new Expression<TDelegate> (body, parameters);
  882. }
  883. [MonoTODO]
  884. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, IEnumerable<ParameterExpression> parameters)
  885. {
  886. throw new NotImplementedException ();
  887. }
  888. [MonoTODO]
  889. public static LambdaExpression Lambda (Expression body, params ParameterExpression [] parameters)
  890. {
  891. throw new NotImplementedException ();
  892. }
  893. public static LambdaExpression Lambda (Type delegateType, Expression body, params ParameterExpression [] parameters)
  894. {
  895. return Lambda (delegateType, body, parameters as IEnumerable<ParameterExpression>);
  896. }
  897. [MonoTODO]
  898. public static LambdaExpression Lambda (Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
  899. {
  900. if (delegateType == null)
  901. throw new ArgumentNullException ("delegateType");
  902. if (body == null)
  903. throw new ArgumentNullException ("body");
  904. return new LambdaExpression (delegateType, body, parameters.ToReadOnlyCollection ());
  905. }
  906. public static MemberListBinding ListBind (MemberInfo member, params ElementInit [] initializers)
  907. {
  908. throw new NotImplementedException ();
  909. }
  910. [MonoTODO]
  911. public static MemberListBinding ListBind (MemberInfo member, IEnumerable<ElementInit> initializers)
  912. {
  913. throw new NotImplementedException ();
  914. }
  915. [MonoTODO]
  916. public static MemberListBinding ListBind (MethodInfo propertyAccessor, params ElementInit [] initializers)
  917. {
  918. throw new NotImplementedException ();
  919. }
  920. [MonoTODO]
  921. public static MemberListBinding ListBind (MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers)
  922. {
  923. throw new NotImplementedException ();
  924. }
  925. [MonoTODO]
  926. public static ListInitExpression ListInit (NewExpression newExpression, params ElementInit [] initializers)
  927. {
  928. throw new NotImplementedException ();
  929. }
  930. [MonoTODO]
  931. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<ElementInit> initializers)
  932. {
  933. throw new NotImplementedException ();
  934. }
  935. [MonoTODO]
  936. public static ListInitExpression ListInit (NewExpression newExpression, params Expression [] initializers)
  937. {
  938. throw new NotImplementedException ();
  939. }
  940. [MonoTODO]
  941. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<Expression> initializers)
  942. {
  943. throw new NotImplementedException ();
  944. }
  945. [MonoTODO]
  946. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, params Expression [] initializers)
  947. {
  948. throw new NotImplementedException ();
  949. }
  950. [MonoTODO]
  951. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, IEnumerable<Expression> initializers)
  952. {
  953. throw new NotImplementedException ();
  954. }
  955. public static MemberExpression MakeMemberAccess (Expression expression, MemberInfo member)
  956. {
  957. if (expression == null)
  958. throw new ArgumentNullException ("expression");
  959. if (member == null)
  960. throw new ArgumentNullException ("member");
  961. var field = member as FieldInfo;
  962. if (field != null)
  963. return Field (expression, field);
  964. var property = member as PropertyInfo;
  965. if (property != null)
  966. return Property (expression, property);
  967. throw new ArgumentException ("Member should either be a field or a property");
  968. }
  969. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type)
  970. {
  971. return MakeUnary (unaryType, operand, type, null);
  972. }
  973. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type, MethodInfo method)
  974. {
  975. switch (unaryType) {
  976. case ExpressionType.ArrayLength:
  977. return ArrayLength (operand);
  978. case ExpressionType.Convert:
  979. return Convert (operand, type, method);
  980. case ExpressionType.ConvertChecked:
  981. return ConvertChecked (operand, type, method);
  982. case ExpressionType.Negate:
  983. return Negate (operand, method);
  984. case ExpressionType.NegateChecked:
  985. return NegateChecked (operand, method);
  986. case ExpressionType.Not:
  987. return Not (operand, method);
  988. case ExpressionType.Quote:
  989. return Quote (operand);
  990. case ExpressionType.TypeAs:
  991. return TypeAs (operand, type);
  992. case ExpressionType.UnaryPlus:
  993. return UnaryPlus (operand, method);
  994. }
  995. throw new ArgumentException ("MakeUnary expect an unary operator");
  996. }
  997. [MonoTODO]
  998. public static MemberMemberBinding MemberBind (MemberInfo member, params MemberBinding [] binding)
  999. {
  1000. throw new NotImplementedException ();
  1001. }
  1002. [MonoTODO]
  1003. public static MemberMemberBinding MemberBind (MemberInfo member, IEnumerable<MemberBinding> binding)
  1004. {
  1005. throw new NotImplementedException ();
  1006. }
  1007. [MonoTODO]
  1008. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, params MemberBinding [] binding)
  1009. {
  1010. throw new NotImplementedException ();
  1011. }
  1012. [MonoTODO]
  1013. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, IEnumerable<MemberBinding> binding)
  1014. {
  1015. throw new NotImplementedException ();
  1016. }
  1017. [MonoTODO]
  1018. public static MemberInitExpression MemberInit (NewExpression newExpression, params MemberBinding [] binding)
  1019. {
  1020. throw new NotImplementedException ();
  1021. }
  1022. [MonoTODO]
  1023. public static MemberInitExpression MemberInit (NewExpression newExpression, IEnumerable<MemberBinding> binding)
  1024. {
  1025. throw new NotImplementedException ();
  1026. }
  1027. public static UnaryExpression Negate (Expression expression)
  1028. {
  1029. return Negate (expression, null);
  1030. }
  1031. public static UnaryExpression Negate (Expression expression, MethodInfo method)
  1032. {
  1033. method = UnaryCoreCheck ("op_UnaryNegation", expression, method);
  1034. return MakeSimpleUnary (ExpressionType.Negate, expression, method);
  1035. }
  1036. public static UnaryExpression NegateChecked (Expression expression)
  1037. {
  1038. return NegateChecked (expression, null);
  1039. }
  1040. public static UnaryExpression NegateChecked (Expression expression, MethodInfo method)
  1041. {
  1042. method = UnaryCoreCheck ("op_UnaryNegation", expression, method);
  1043. return MakeSimpleUnary (ExpressionType.Negate, expression, method);
  1044. }
  1045. [MonoTODO]
  1046. public static NewExpression New (ConstructorInfo constructor)
  1047. {
  1048. throw new NotImplementedException ();
  1049. }
  1050. [MonoTODO]
  1051. public static NewExpression New (Type type)
  1052. {
  1053. throw new NotImplementedException ();
  1054. }
  1055. [MonoTODO]
  1056. public static NewExpression New (ConstructorInfo constructor, params Expression [] arguments)
  1057. {
  1058. throw new NotImplementedException ();
  1059. }
  1060. [MonoTODO]
  1061. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments)
  1062. {
  1063. throw new NotImplementedException ();
  1064. }
  1065. [MonoTODO]
  1066. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, params MemberInfo [] members)
  1067. {
  1068. throw new NotImplementedException ();
  1069. }
  1070. [MonoTODO]
  1071. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, IEnumerable<MemberInfo> members)
  1072. {
  1073. throw new NotImplementedException ();
  1074. }
  1075. public static NewArrayExpression NewArrayBounds (Type type, params Expression [] bounds)
  1076. {
  1077. return NewArrayBounds (type, bounds as IEnumerable<Expression>);
  1078. }
  1079. public static NewArrayExpression NewArrayBounds (Type type, IEnumerable<Expression> bounds)
  1080. {
  1081. if (type == null)
  1082. throw new ArgumentNullException ("type");
  1083. if (bounds == null)
  1084. throw new ArgumentNullException ("bounds");
  1085. var array_bounds = bounds.ToReadOnlyCollection ();
  1086. foreach (var expression in array_bounds)
  1087. if (!IsInt (expression.Type))
  1088. throw new ArgumentException ("The bounds collection can only contain expression of integers types");
  1089. return new NewArrayExpression (ExpressionType.NewArrayBounds, type.MakeArrayType (array_bounds.Count), array_bounds);
  1090. }
  1091. public static NewArrayExpression NewArrayInit (Type type, params Expression [] initializers)
  1092. {
  1093. return NewArrayInit (type, initializers as IEnumerable<Expression>);
  1094. }
  1095. public static NewArrayExpression NewArrayInit (Type type, IEnumerable<Expression> initializers)
  1096. {
  1097. if (type == null)
  1098. throw new ArgumentNullException ("type");
  1099. if (initializers == null)
  1100. throw new ArgumentNullException ("initializers");
  1101. var array_initializers = initializers.ToReadOnlyCollection ();
  1102. foreach (var expression in initializers) {
  1103. if (expression == null)
  1104. throw new ArgumentNullException ("initializers");
  1105. if (!type.IsAssignableFrom (expression.Type))
  1106. throw new InvalidOperationException ();
  1107. // TODO: Quote elements if type == typeof (Expression)
  1108. }
  1109. return new NewArrayExpression (ExpressionType.NewArrayInit, type.MakeArrayType (), array_initializers);
  1110. }
  1111. public static UnaryExpression Not (Expression expression)
  1112. {
  1113. return Not (expression, null);
  1114. }
  1115. public static UnaryExpression Not (Expression expression, MethodInfo method)
  1116. {
  1117. method = UnaryCoreCheck ("op_LogicalNot", expression, method);
  1118. return MakeSimpleUnary (ExpressionType.Not, expression, method);
  1119. }
  1120. public static ParameterExpression Parameter (Type type, string name)
  1121. {
  1122. if (type == null)
  1123. throw new ArgumentNullException ("type");
  1124. return new ParameterExpression (type, name);
  1125. }
  1126. [MonoTODO]
  1127. public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
  1128. {
  1129. throw new NotImplementedException ();
  1130. }
  1131. [MonoTODO]
  1132. public static MemberExpression Property (Expression expression, PropertyInfo property)
  1133. {
  1134. throw new NotImplementedException ();
  1135. }
  1136. [MonoTODO]
  1137. public static MemberExpression Property (Expression expression, string propertyName)
  1138. {
  1139. throw new NotImplementedException ();
  1140. }
  1141. [MonoTODO]
  1142. public static MemberExpression PropertyOrField (Expression expression, string propertyOrFieldName)
  1143. {
  1144. throw new NotImplementedException ();
  1145. }
  1146. public static UnaryExpression Quote (Expression expression)
  1147. {
  1148. if (expression == null)
  1149. throw new ArgumentNullException ("expression");
  1150. return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType ());
  1151. }
  1152. public static UnaryExpression TypeAs (Expression expression, Type type)
  1153. {
  1154. if (expression == null)
  1155. throw new ArgumentNullException ("expression");
  1156. if (type == null)
  1157. throw new ArgumentNullException ("type");
  1158. if (type.IsValueType && !IsNullable (type))
  1159. throw new ArgumentException ("TypeAs expect a reference or a nullable type");
  1160. return new UnaryExpression (ExpressionType.TypeAs, expression, type);
  1161. }
  1162. public static TypeBinaryExpression TypeIs (Expression expression, Type type)
  1163. {
  1164. if (expression == null)
  1165. throw new ArgumentNullException ("expression");
  1166. if (type == null)
  1167. throw new ArgumentNullException ("type");
  1168. return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof (bool));
  1169. }
  1170. public static UnaryExpression UnaryPlus (Expression expression)
  1171. {
  1172. return UnaryPlus (expression, null);
  1173. }
  1174. public static UnaryExpression UnaryPlus (Expression expression, MethodInfo method)
  1175. {
  1176. method = UnaryCoreCheck ("op_UnaryPlus", expression, method);
  1177. return MakeSimpleUnary (ExpressionType.UnaryPlus, expression, method);
  1178. }
  1179. internal static bool IsNullable (Type type)
  1180. {
  1181. return type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>);
  1182. }
  1183. internal static bool IsUnsigned (Type t)
  1184. {
  1185. if (t.IsPointer)
  1186. return IsUnsigned (t.GetElementType ());
  1187. return t == typeof (ushort) || t == typeof (uint) || t == typeof (ulong) || t == typeof (byte);
  1188. }
  1189. //
  1190. // returns the T in a a Nullable<T> type.
  1191. //
  1192. internal static Type GetNullableOf (Type type)
  1193. {
  1194. return type.GetGenericArguments () [0];
  1195. }
  1196. //
  1197. // This method must be overwritten by derived classes to
  1198. // compile the expression
  1199. //
  1200. internal abstract void Emit (EmitContext ec);
  1201. }
  1202. }