CodeExpression.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. // Copyright (C) Lluis Sanchez Gual, 2004
  22. //
  23. using System;
  24. using System.Reflection;
  25. using System.Reflection.Emit;
  26. namespace Mono.CodeGeneration
  27. {
  28. public abstract class CodeExpression: CodeItem
  29. {
  30. internal CodeVariableReference var;
  31. public abstract Type GetResultType ();
  32. public virtual void GenerateAsStatement (ILGenerator gen)
  33. {
  34. Generate (gen);
  35. gen.Emit (OpCodes.Pop);
  36. }
  37. public CodeExpression CallToString ()
  38. {
  39. return new CodeMethodCall (this, "ToString");
  40. }
  41. public static CodeExpression AreEqual (CodeExpression e1, CodeExpression e2)
  42. {
  43. return new CodeEquals (e1, e2);
  44. }
  45. public static CodeExpression AreNotEqual (CodeExpression e1, CodeExpression e2)
  46. {
  47. return new CodeNotEquals (e1, e2);
  48. }
  49. public static CodeExpression IsGreaterThan (CodeExpression e1, CodeExpression e2)
  50. {
  51. return new CodeGreaterThan (e1, e2);
  52. }
  53. public static CodeExpression IsSmallerThan (CodeExpression e1, CodeExpression e2)
  54. {
  55. return new CodeLessThan (e1, e2);
  56. }
  57. public static CodeExpression IsGreaterEqualThan (CodeExpression e1, CodeExpression e2)
  58. {
  59. return new CodeGreaterEqualThan (e1, e2);
  60. }
  61. public static CodeExpression IsSmallerEqualThan (CodeExpression e1, CodeExpression e2)
  62. {
  63. return new CodeLessEqualThan (e1, e2);
  64. }
  65. public static CodeExpression Not (CodeExpression e)
  66. {
  67. return new CodeNot (e);
  68. }
  69. public static CodeExpression Add (CodeExpression e1, CodeExpression e2)
  70. {
  71. return new CodeAdd (e1, e2);
  72. }
  73. public static CodeExpression Subtract (CodeExpression e1, CodeExpression e2)
  74. {
  75. return new CodeSub (e1, e2);
  76. }
  77. public static CodeExpression Multiply (CodeExpression e1, CodeExpression e2)
  78. {
  79. return new CodeMul (e1, e2);
  80. }
  81. public static CodeExpression Divide (CodeExpression e1, CodeExpression e2)
  82. {
  83. return new CodeDiv (e1, e2);
  84. }
  85. public CodeExpression CastTo (Type type)
  86. {
  87. return new CodeCast (type, this);
  88. }
  89. public CodeExpression And (CodeExpression other)
  90. {
  91. return new CodeAnd (this, other);
  92. }
  93. public CodeExpression Is (Type type)
  94. {
  95. return new CodeIs (type, this);
  96. }
  97. public CodeExpression Call (string name, params CodeExpression[] parameters)
  98. {
  99. return new CodeMethodCall (this, name, parameters);
  100. }
  101. public CodeExpression Call (MethodInfo method, params CodeExpression[] parameters)
  102. {
  103. return new CodeMethodCall (this, method, parameters);
  104. }
  105. public CodeValueReference MemGet (string name)
  106. {
  107. MemberInfo[] mems = GetResultType().GetMember (name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  108. if (mems.Length == 0) throw new InvalidOperationException ("Field '" + name + "' not found in " + GetResultType());
  109. return MemGet (mems[0]);
  110. }
  111. public CodeValueReference MemGet (MemberInfo member)
  112. {
  113. if (member is FieldInfo)
  114. return new CodeFieldReference (this, (FieldInfo)member);
  115. else if (member is PropertyInfo)
  116. return new CodePropertyReference (this, (PropertyInfo)member);
  117. else
  118. throw new InvalidOperationException (member.Name + " is not either a field or a property");
  119. }
  120. public CodeValueReference this [CodeExpression index]
  121. {
  122. get { return new CodeArrayItem (this, index); }
  123. }
  124. public CodeValueReference this [string name]
  125. {
  126. get { return MemGet (name); }
  127. }
  128. public CodeValueReference this [FieldInfo field]
  129. {
  130. get { return new CodeFieldReference (this, field); }
  131. }
  132. public CodeValueReference this [PropertyInfo prop]
  133. {
  134. get { return new CodePropertyReference (this, prop); }
  135. }
  136. public CodeExpression ArrayLength
  137. {
  138. get { return new CodeArrayLength (this); }
  139. }
  140. public CodeExpression IsNull
  141. {
  142. get { return new CodeEquals (this, new CodeLiteral (null, this.GetResultType())); }
  143. }
  144. public static CodeExpression NullValue (Type type)
  145. {
  146. return new CodeLiteral (null, type);
  147. }
  148. public bool IsNumber
  149. {
  150. get {
  151. return CodeGenerationHelper.IsNumber (GetResultType ());
  152. }
  153. }
  154. }
  155. public abstract class CodeConditionExpression: CodeExpression
  156. {
  157. public virtual void GenerateForBranch (ILGenerator gen, Label label, bool jumpCase)
  158. {
  159. Generate (gen);
  160. if (jumpCase)
  161. gen.Emit (OpCodes.Brtrue, label);
  162. else
  163. gen.Emit (OpCodes.Brfalse, label);
  164. }
  165. }
  166. }