Expression.cs 47 KB

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