ModuleBuilder.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // System.Reflection.Emit/ModuleBuilder.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System;
  10. using System.Reflection;
  11. using System.Collections;
  12. using System.Runtime.CompilerServices;
  13. using System.Runtime.InteropServices;
  14. using System.Diagnostics.SymbolStore;
  15. using System.IO;
  16. namespace System.Reflection.Emit {
  17. public class ModuleBuilder : Module {
  18. private TypeBuilder[] types;
  19. private CustomAttributeBuilder[] cattrs;
  20. private byte[] guid;
  21. private int table_idx;
  22. private AssemblyBuilder assemblyb;
  23. private ISymbolWriter symbol_writer;
  24. private MethodInfo symwriter_define_local;
  25. Hashtable name_cache;
  26. internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname, bool emitSymbolInfo) {
  27. this.name = this.scopename = name;
  28. this.fqname = fullyqname;
  29. this.assembly = this.assemblyb = assb;
  30. guid = Guid.NewGuid().ToByteArray ();
  31. table_idx = get_next_table_index (this, 0x00, true);
  32. name_cache = new Hashtable ();
  33. if (emitSymbolInfo)
  34. GetSymbolWriter (fullyqname);
  35. }
  36. internal void GetSymbolWriter (string filename)
  37. {
  38. Assembly assembly;
  39. try {
  40. assembly = Assembly.Load ("Mono.CSharp.Debugger");
  41. } catch (FileNotFoundException) {
  42. return;
  43. }
  44. Type type = assembly.GetType ("Mono.CSharp.Debugger.MonoSymbolWriter");
  45. if (type == null)
  46. return;
  47. // First get the constructor.
  48. {
  49. Type[] arg_types = new Type [2];
  50. arg_types [0] = typeof (ModuleBuilder);
  51. arg_types [1] = typeof (string);
  52. ConstructorInfo constructor = type.GetConstructor (arg_types);
  53. object[] args = new object [2];
  54. args [0] = this;
  55. args [1] = filename;
  56. if (constructor == null)
  57. return;
  58. Object instance = constructor.Invoke (args);
  59. if (instance == null)
  60. return;
  61. if (!(instance is ISymbolWriter))
  62. return;
  63. symbol_writer = (ISymbolWriter) instance;
  64. }
  65. // Get the DefineLocalVariable method.
  66. {
  67. Type[] arg_types = new Type [6];
  68. arg_types [0] = typeof (string);
  69. arg_types [1] = typeof (LocalBuilder);
  70. arg_types [2] = typeof (FieldAttributes);
  71. arg_types [3] = typeof (int);
  72. arg_types [4] = typeof (int);
  73. arg_types [5] = typeof (int);
  74. symwriter_define_local = type.GetMethod ("DefineLocalVariable", arg_types);
  75. if (symwriter_define_local == null)
  76. throw new NotSupportedException ();
  77. }
  78. }
  79. internal void SymWriter_DefineLocalVariable (string name, LocalBuilder local,
  80. FieldAttributes attributes,
  81. int position, int startOffset, int endOffset)
  82. {
  83. if ((symbol_writer == null) || (symwriter_define_local == null))
  84. return;
  85. object[] args = new object [6];
  86. args [0] = name;
  87. args [1] = local;
  88. args [2] = attributes;
  89. args [3] = position;
  90. args [4] = startOffset;
  91. args [5] = endOffset;
  92. symwriter_define_local.Invoke (symbol_writer, args);
  93. }
  94. public override string FullyQualifiedName {get { return fqname;}}
  95. [MonoTODO]
  96. public TypeBuilder DefineType (string name) {
  97. // FIXME: LAMESPEC: what other attributes should we use here as default?
  98. return DefineType (name, TypeAttributes.Public, typeof(object), null);
  99. }
  100. public TypeBuilder DefineType (string name, TypeAttributes attr) {
  101. return DefineType (name, attr, typeof(object), null);
  102. }
  103. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
  104. return DefineType (name, attr, parent, null);
  105. }
  106. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
  107. TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces);
  108. if (types != null) {
  109. TypeBuilder[] new_types = new TypeBuilder [types.Length + 1];
  110. System.Array.Copy (types, new_types, types.Length);
  111. new_types [types.Length] = res;
  112. types = new_types;
  113. } else {
  114. types = new TypeBuilder [1];
  115. types [0] = res;
  116. }
  117. name_cache.Add (name, res);
  118. return res;
  119. }
  120. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
  121. return DefineType (name, attr, parent, null);
  122. }
  123. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
  124. return DefineType (name, attr, parent, null);
  125. }
  126. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
  127. return DefineType (name, attr, parent, null);
  128. }
  129. public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  130. return new MonoArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes);
  131. }
  132. public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
  133. EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
  134. return eb;
  135. }
  136. public override Type GetType( string className) {
  137. return GetType (className, false, false);
  138. }
  139. public override Type GetType( string className, bool ignoreCase) {
  140. return GetType (className, false, ignoreCase);
  141. }
  142. private TypeBuilder search_in_array (TypeBuilder[] arr, string className, bool ignoreCase) {
  143. int i;
  144. if (arr == types && !ignoreCase)
  145. return (TypeBuilder)name_cache [className];
  146. for (i = 0; i < arr.Length; ++i) {
  147. if (String.Compare (className, arr [i].FullName, ignoreCase) == 0) {
  148. return arr [i];
  149. }
  150. }
  151. return null;
  152. }
  153. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  154. private static extern Type create_modified_type (TypeBuilder tb, string modifiers);
  155. static char[] type_modifiers = {'&', '[', '*'};
  156. public override Type GetType( string className, bool throwOnError, bool ignoreCase) {
  157. int subt;
  158. string modifiers;
  159. TypeBuilder result = null;
  160. if (types == null && throwOnError)
  161. throw new TypeLoadException (className);
  162. subt = className.IndexOfAny (type_modifiers);
  163. if (subt >= 0) {
  164. modifiers = className.Substring (subt);
  165. className = className.Substring (0, subt);
  166. } else
  167. modifiers = null;
  168. subt = className.IndexOf ('+');
  169. if (subt < 0) {
  170. if (types != null)
  171. result = search_in_array (types, className, ignoreCase);
  172. } else {
  173. string pname, rname;
  174. pname = className.Substring (0, subt);
  175. rname = className.Substring (subt + 1);
  176. result = search_in_array (types, pname, ignoreCase);
  177. if ((result != null) && (result.subtypes != null))
  178. result = search_in_array (result.subtypes, rname, ignoreCase);
  179. else
  180. result = null;
  181. }
  182. if ((result == null) && throwOnError)
  183. throw new TypeLoadException (className);
  184. if (result != null && (modifiers != null))
  185. return create_modified_type (result, modifiers);
  186. return result;
  187. }
  188. internal int get_next_table_index (object obj, int table, bool inc) {
  189. return assemblyb.get_next_table_index (obj, table, inc);
  190. }
  191. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  192. if (cattrs != null) {
  193. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  194. cattrs.CopyTo (new_array, 0);
  195. new_array [cattrs.Length] = customBuilder;
  196. cattrs = new_array;
  197. } else {
  198. cattrs = new CustomAttributeBuilder [1];
  199. cattrs [0] = customBuilder;
  200. }
  201. }
  202. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  203. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  204. }
  205. public ISymbolWriter GetSymWriter () {
  206. return symbol_writer;
  207. }
  208. public ISymbolDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) {
  209. if (symbol_writer == null)
  210. throw new InvalidOperationException ();
  211. return symbol_writer.DefineDocument (url, language, languageVendor, documentType);
  212. }
  213. }
  214. }