Expression.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. [MonoTODO]
  1076. public static NewArrayExpression NewArrayBounds (Type type, params Expression [] bounds)
  1077. {
  1078. throw new NotImplementedException ();
  1079. }
  1080. [MonoTODO]
  1081. public static NewArrayExpression NewArrayBounds (Type type, IEnumerable<Expression> bounds)
  1082. {
  1083. throw new NotImplementedException ();
  1084. }
  1085. [MonoTODO]
  1086. public static NewArrayExpression NewArrayInit (Type type, params Expression [] bounds)
  1087. {
  1088. throw new NotImplementedException ();
  1089. }
  1090. [MonoTODO]
  1091. public static NewArrayExpression NewArrayInit (Type type, IEnumerable<Expression> bounds)
  1092. {
  1093. throw new NotImplementedException ();
  1094. }
  1095. public static UnaryExpression Not (Expression expression)
  1096. {
  1097. return Not (expression, null);
  1098. }
  1099. public static UnaryExpression Not (Expression expression, MethodInfo method)
  1100. {
  1101. method = UnaryCoreCheck ("op_LogicalNot", expression, method);
  1102. return MakeSimpleUnary (ExpressionType.Not, expression, method);
  1103. }
  1104. public static ParameterExpression Parameter (Type type, string name)
  1105. {
  1106. if (type == null)
  1107. throw new ArgumentNullException ("type");
  1108. return new ParameterExpression (type, name);
  1109. }
  1110. [MonoTODO]
  1111. public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
  1112. {
  1113. throw new NotImplementedException ();
  1114. }
  1115. [MonoTODO]
  1116. public static MemberExpression Property (Expression expression, PropertyInfo property)
  1117. {
  1118. throw new NotImplementedException ();
  1119. }
  1120. [MonoTODO]
  1121. public static MemberExpression Property (Expression expression, string propertyName)
  1122. {
  1123. throw new NotImplementedException ();
  1124. }
  1125. [MonoTODO]
  1126. public static MemberExpression PropertyOrField (Expression expression, string propertyOrFieldName)
  1127. {
  1128. throw new NotImplementedException ();
  1129. }
  1130. public static UnaryExpression Quote (Expression expression)
  1131. {
  1132. if (expression == null)
  1133. throw new ArgumentNullException ("expression");
  1134. return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType ());
  1135. }
  1136. public static UnaryExpression TypeAs (Expression expression, Type type)
  1137. {
  1138. if (expression == null)
  1139. throw new ArgumentNullException ("expression");
  1140. if (type == null)
  1141. throw new ArgumentNullException ("type");
  1142. if (type.IsValueType && !IsNullable (type))
  1143. throw new ArgumentException ("TypeAs expect a reference or a nullable type");
  1144. return new UnaryExpression (ExpressionType.TypeAs, expression, type);
  1145. }
  1146. public static TypeBinaryExpression TypeIs (Expression expression, Type type)
  1147. {
  1148. if (expression == null)
  1149. throw new ArgumentNullException ("expression");
  1150. if (type == null)
  1151. throw new ArgumentNullException ("type");
  1152. return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof (bool));
  1153. }
  1154. public static UnaryExpression UnaryPlus (Expression expression)
  1155. {
  1156. return UnaryPlus (expression, null);
  1157. }
  1158. public static UnaryExpression UnaryPlus (Expression expression, MethodInfo method)
  1159. {
  1160. method = UnaryCoreCheck ("op_UnaryPlus", expression, method);
  1161. return MakeSimpleUnary (ExpressionType.UnaryPlus, expression, method);
  1162. }
  1163. internal static bool IsNullable (Type type)
  1164. {
  1165. return type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>);
  1166. }
  1167. internal static bool IsUnsigned (Type t)
  1168. {
  1169. if (t.IsPointer)
  1170. return IsUnsigned (t.GetElementType ());
  1171. return t == typeof (ushort) || t == typeof (uint) || t == typeof (ulong) || t == typeof (byte);
  1172. }
  1173. //
  1174. // returns the T in a a Nullable<T> type.
  1175. //
  1176. internal static Type GetNullableOf (Type type)
  1177. {
  1178. return type.GetGenericArguments () [0];
  1179. }
  1180. //
  1181. // This method must be overwritten by derived classes to
  1182. // compile the expression
  1183. //
  1184. internal abstract void Emit (EmitContext ec);
  1185. }
  1186. }