CodeProperty.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.Collections;
  26. using System.IO;
  27. using System.Reflection;
  28. using System.Reflection.Emit;
  29. namespace Mono.CodeGeneration
  30. {
  31. public class CodeProperty
  32. {
  33. PropertyInfo propertyInfo;
  34. CodeBuilder get_builder;
  35. CodeBuilder set_builder;
  36. string name;
  37. PropertyAttributes attributes;
  38. MethodAttributes methodAttributes;
  39. Type returnType;
  40. TypeBuilder typeBuilder;
  41. Type[] parameterTypes;
  42. ArrayList customAttributes = new ArrayList ();
  43. CodeClass cls;
  44. internal static CodeProperty DefineProperty (CodeClass cls, string name, PropertyAttributes attributes, MethodAttributes methodAttributes, Type returnType, Type[] parameterTypes)
  45. {
  46. return new CodeProperty (cls, name, attributes, methodAttributes, returnType, parameterTypes);
  47. }
  48. internal CodeProperty (CodeClass cls, string name, PropertyAttributes attributes, MethodAttributes methodAttributes, Type returnType, Type[] parameterTypes)
  49. {
  50. this.cls = cls;
  51. this.typeBuilder = cls.TypeBuilder;
  52. this.name = name;
  53. this.attributes = attributes;
  54. this.methodAttributes = methodAttributes;
  55. this.returnType = returnType;
  56. this.parameterTypes = parameterTypes;
  57. PropertyBuilder pb = typeBuilder.DefineProperty (name, attributes, returnType, parameterTypes);
  58. pb.SetGetMethod (typeBuilder.DefineMethod ("get_" + name, methodAttributes, CallingConventions.Standard, returnType, Type.EmptyTypes));
  59. pb.SetSetMethod (typeBuilder.DefineMethod ("set_" + name, methodAttributes, CallingConventions.Standard, typeof (void), new Type [] {returnType}));
  60. get_builder = new CodeBuilder (cls);
  61. set_builder = new CodeBuilder (cls);
  62. propertyInfo = pb;
  63. }
  64. public TypeBuilder DeclaringType
  65. {
  66. get { return typeBuilder; }
  67. }
  68. public PropertyBuilder PropertyBuilder
  69. {
  70. get { return propertyInfo as PropertyBuilder; }
  71. }
  72. public string Name
  73. {
  74. get { return name; }
  75. }
  76. public PropertyAttributes Attributes
  77. {
  78. get { return attributes; }
  79. }
  80. public Type ReturnType
  81. {
  82. get { return returnType; }
  83. }
  84. public Type[] ParameterTypes
  85. {
  86. get { return parameterTypes; }
  87. }
  88. public CodeBuilder CodeBuilderGet
  89. {
  90. get { return get_builder; }
  91. }
  92. public CodeBuilder CodeBuilderSet
  93. {
  94. get { return set_builder; }
  95. }
  96. public bool IsStatic
  97. {
  98. get { return (methodAttributes & MethodAttributes.Static) != 0; }
  99. }
  100. public bool IsPublic
  101. {
  102. get { return (methodAttributes & MethodAttributes.Public) != 0; }
  103. }
  104. public CodeCustomAttribute CreateCustomAttribute (Type attributeType)
  105. {
  106. return CreateCustomAttribute (attributeType,
  107. Type.EmptyTypes, new object [0]);
  108. }
  109. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs)
  110. {
  111. return CreateCustomAttribute (attributeType,
  112. ctorArgTypes, ctorArgs, new string [0], new object [0]);
  113. }
  114. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs, string [] namedArgFieldNames, object [] namedArgValues)
  115. {
  116. CodeCustomAttribute cca = CodeCustomAttribute.Create (
  117. attributeType, ctorArgTypes, ctorArgs, namedArgFieldNames, namedArgValues);
  118. SetCustomAttribute (cca);
  119. return cca;
  120. }
  121. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, CodeLiteral [] ctorArgs, FieldInfo [] fields, CodeLiteral [] fieldValues)
  122. {
  123. CodeCustomAttribute cca = CodeCustomAttribute.Create (
  124. attributeType, ctorArgTypes, ctorArgs, fields, fieldValues);
  125. SetCustomAttribute (cca);
  126. return cca;
  127. }
  128. void SetCustomAttribute (CodeCustomAttribute cca)
  129. {
  130. PropertyBuilder.SetCustomAttribute (cca.Builder);
  131. customAttributes.Add (cca);
  132. }
  133. public string PrintCode ()
  134. {
  135. StringWriter sw = new StringWriter ();
  136. CodeWriter cw = new CodeWriter (sw);
  137. PrintCode (cw);
  138. return sw.ToString ();
  139. }
  140. public void PrintCode (CodeWriter cp)
  141. {
  142. cp.BeginLine ();
  143. foreach (CodeCustomAttribute a in customAttributes)
  144. a.PrintCode (cp);
  145. cp.BeginLine ();
  146. if (IsStatic)
  147. cp.Write ("static ");
  148. if (IsPublic)
  149. cp.Write ("public ");
  150. if (returnType != null) cp.Write (returnType + " ");
  151. cp.Write (name);
  152. if (parameterTypes.Length > 0) {
  153. cp.Write (name + " [");
  154. for (int n=0; n<parameterTypes.Length; n++) {
  155. if (n > 0) cp.Write (", ");
  156. cp.Write (parameterTypes[n] + " arg" + n);
  157. }
  158. cp.Write ("]");
  159. }
  160. cp.Write (" {");
  161. cp.EndLine ();
  162. cp.Indent ();
  163. cp.WriteLineInd ("get {");
  164. get_builder.PrintCode (cp);
  165. cp.WriteLineUnind ("}");
  166. cp.WriteLine ("set {");
  167. set_builder.PrintCode (cp);
  168. cp.WriteLine ("}");
  169. cp.WriteLineUnind ("}");
  170. }
  171. public CodeArgumentReference GetArg (int n)
  172. {
  173. if (n < 0 || n >= parameterTypes.Length)
  174. throw new InvalidOperationException ("Invalid argument number");
  175. int narg = IsStatic ? n : n + 1;
  176. return new CodeArgumentReference (parameterTypes[n], narg, "arg" + n);
  177. }
  178. public CodeArgumentReference GetThis ()
  179. {
  180. if (IsStatic)
  181. throw new InvalidOperationException ("'this' not available in static methods");
  182. return new CodeArgumentReference (DeclaringType, 0, "this");
  183. }
  184. public void Generate ()
  185. {
  186. ILGenerator gen;
  187. Label returnLabel;
  188. MethodBuilder mb;
  189. // getter
  190. mb = (MethodBuilder) propertyInfo.GetGetMethod ();
  191. if (mb != null) {
  192. gen = mb.GetILGenerator();
  193. returnLabel = gen.DefineLabel ();
  194. get_builder.ReturnLabel = returnLabel;
  195. get_builder.Generate (gen);
  196. gen.MarkLabel (returnLabel);
  197. gen.Emit (OpCodes.Ret);
  198. }
  199. // setter
  200. mb = (MethodBuilder) propertyInfo.GetSetMethod ();
  201. if (mb != null) {
  202. gen = mb.GetILGenerator();
  203. returnLabel = gen.DefineLabel ();
  204. set_builder.ReturnLabel = returnLabel;
  205. set_builder.Generate (gen);
  206. gen.MarkLabel (returnLabel);
  207. gen.Emit (OpCodes.Ret);
  208. }
  209. }
  210. public void UpdatePropertyInfo (Type type)
  211. {
  212. propertyInfo = type.GetProperty (propertyInfo.Name, parameterTypes);
  213. }
  214. }
  215. }
  216. #endif