Expression.cs 51 KB

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