CodeExpression.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #if !FULL_AOT_RUNTIME
  24. using System;
  25. using System.Reflection;
  26. using System.Reflection.Emit;
  27. namespace Mono.CodeGeneration
  28. {
  29. public abstract class CodeExpression: CodeItem
  30. {
  31. internal CodeVariableReference var;
  32. public abstract Type GetResultType ();
  33. public virtual void GenerateAsStatement (ILGenerator gen)
  34. {
  35. Generate (gen);
  36. gen.Emit (OpCodes.Pop);
  37. }
  38. public CodeExpression CallToString ()
  39. {
  40. return new CodeMethodCall (this, "ToString");
  41. }
  42. public static CodeExpression AreEqual (CodeExpression e1, CodeExpression e2)
  43. {
  44. return new CodeEquals (e1, e2);
  45. }
  46. public static CodeExpression AreNotEqual (CodeExpression e1, CodeExpression e2)
  47. {
  48. return new CodeNotEquals (e1, e2);
  49. }
  50. public static CodeExpression IsGreaterThan (CodeExpression e1, CodeExpression e2)
  51. {
  52. return new CodeGreaterThan (e1, e2);
  53. }
  54. public static CodeExpression IsSmallerThan (CodeExpression e1, CodeExpression e2)
  55. {
  56. return new CodeLessThan (e1, e2);
  57. }
  58. public static CodeExpression IsGreaterEqualThan (CodeExpression e1, CodeExpression e2)
  59. {
  60. return new CodeGreaterEqualThan (e1, e2);
  61. }
  62. public static CodeExpression IsSmallerEqualThan (CodeExpression e1, CodeExpression e2)
  63. {
  64. return new CodeLessEqualThan (e1, e2);
  65. }
  66. public static CodeExpression Not (CodeExpression e)
  67. {
  68. return new CodeNot (e);
  69. }
  70. public static CodeExpression Add (CodeExpression e1, CodeExpression e2)
  71. {
  72. return new CodeAdd (e1, e2);
  73. }
  74. public static CodeExpression Subtract (CodeExpression e1, CodeExpression e2)
  75. {
  76. return new CodeSub (e1, e2);
  77. }
  78. public static CodeExpression Multiply (CodeExpression e1, CodeExpression e2)
  79. {
  80. return new CodeMul (e1, e2);
  81. }
  82. public static CodeExpression Divide (CodeExpression e1, CodeExpression e2)
  83. {
  84. return new CodeDiv (e1, e2);
  85. }
  86. public CodeExpression CastTo (Type type)
  87. {
  88. return new CodeCast (type, this);
  89. }
  90. public CodeExpression And (CodeExpression other)
  91. {
  92. return new CodeAnd (this, other);
  93. }
  94. public CodeExpression Is (Type type)
  95. {
  96. return new CodeIs (type, this);
  97. }
  98. public CodeExpression Call (string name, params CodeExpression[] parameters)
  99. {
  100. return new CodeMethodCall (this, name, parameters);
  101. }
  102. public CodeExpression Call (MethodInfo method, params CodeExpression[] parameters)
  103. {
  104. return new CodeMethodCall (this, method, parameters);
  105. }
  106. public CodeValueReference MemGet (string name)
  107. {
  108. MemberInfo[] mems = GetResultType().GetMember (name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  109. if (mems.Length == 0) throw new InvalidOperationException ("Field '" + name + "' not found in " + GetResultType());
  110. return MemGet (mems[0]);
  111. }
  112. public CodeValueReference MemGet (MemberInfo member)
  113. {
  114. if (member is FieldInfo)
  115. return new CodeFieldReference (this, (FieldInfo)member);
  116. else if (member is PropertyInfo)
  117. return new CodePropertyReference (this, (PropertyInfo)member);
  118. else
  119. throw new InvalidOperationException (member.Name + " is not either a field or a property");
  120. }
  121. public CodeValueReference this [CodeExpression index]
  122. {
  123. get { return new CodeArrayItem (this, index); }
  124. }
  125. public CodeValueReference this [string name]
  126. {
  127. get { return MemGet (name); }
  128. }
  129. public CodeValueReference this [FieldInfo field]
  130. {
  131. get { return new CodeFieldReference (this, field); }
  132. }
  133. public CodeValueReference this [PropertyInfo prop]
  134. {
  135. get { return new CodePropertyReference (this, prop); }
  136. }
  137. public CodeExpression ArrayLength
  138. {
  139. get { return new CodeArrayLength (this); }
  140. }
  141. public CodeExpression IsNull
  142. {
  143. get { return new CodeEquals (this, new CodeLiteral (null, this.GetResultType())); }
  144. }
  145. public static CodeExpression NullValue (Type type)
  146. {
  147. return new CodeLiteral (null, type);
  148. }
  149. public bool IsNumber
  150. {
  151. get {
  152. return CodeGenerationHelper.IsNumber (GetResultType ());
  153. }
  154. }
  155. }
  156. public abstract class CodeConditionExpression: CodeExpression
  157. {
  158. public virtual void GenerateForBranch (ILGenerator gen, Label label, bool jumpCase)
  159. {
  160. Generate (gen);
  161. if (jumpCase)
  162. gen.Emit (OpCodes.Brtrue, label);
  163. else
  164. gen.Emit (OpCodes.Brfalse, label);
  165. }
  166. }
  167. }
  168. #endif