ModuleBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. using Mono.CSharp.Debugger;
  17. namespace System.Reflection.Emit {
  18. public class ModuleBuilder : Module {
  19. private TypeBuilder[] types;
  20. private CustomAttributeBuilder[] cattrs;
  21. private byte[] guid;
  22. private int table_idx;
  23. internal AssemblyBuilder assemblyb;
  24. private MethodBuilder[] global_methods;
  25. private FieldBuilder[] global_fields;
  26. private TypeBuilder global_type;
  27. private Type global_type_created;
  28. private bool is_main_module;
  29. internal IMonoSymbolWriter symbol_writer;
  30. Hashtable name_cache;
  31. internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname, bool emitSymbolInfo, bool isMainModule) {
  32. this.name = this.scopename = name;
  33. this.fqname = fullyqname;
  34. this.assembly = this.assemblyb = assb;
  35. this.is_main_module = isMainModule;
  36. guid = Guid.NewGuid().ToByteArray ();
  37. table_idx = get_next_table_index (this, 0x00, true);
  38. name_cache = new Hashtable ();
  39. if (emitSymbolInfo)
  40. GetSymbolWriter (fullyqname);
  41. }
  42. internal void GetSymbolWriter (string filename)
  43. {
  44. Assembly assembly;
  45. try {
  46. assembly = Assembly.Load ("Mono.CSharp.Debugger");
  47. } catch (FileNotFoundException) {
  48. return;
  49. }
  50. Type type = assembly.GetType ("Mono.CSharp.Debugger.MonoSymbolWriter");
  51. if (type == null)
  52. return;
  53. if (assemblyb.methods == null)
  54. assemblyb.methods = new ArrayList ();
  55. // First get the constructor.
  56. {
  57. Type[] arg_types = new Type [2];
  58. arg_types [0] = typeof (ModuleBuilder);
  59. arg_types [1] = typeof (ArrayList);
  60. ConstructorInfo constructor = type.GetConstructor (arg_types);
  61. object[] args = new object [2];
  62. args [0] = this;
  63. args [1] = assemblyb.methods;
  64. if (constructor == null)
  65. return;
  66. Object instance = constructor.Invoke (args);
  67. if (instance == null)
  68. return;
  69. if (!(instance is IMonoSymbolWriter))
  70. return;
  71. symbol_writer = (IMonoSymbolWriter) instance;
  72. }
  73. }
  74. public override string FullyQualifiedName {get { return fqname;}}
  75. public void CreateGlobalFunctions ()
  76. {
  77. if (global_type_created != null)
  78. throw new InvalidOperationException ("global methods already created");
  79. if (global_type != null)
  80. global_type_created = global_type.CreateType ();
  81. }
  82. public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
  83. if (name == null)
  84. throw new ArgumentNullException ("name");
  85. if (global_type_created != null)
  86. throw new InvalidOperationException ("global fields already created");
  87. if (global_type == null)
  88. global_type = new TypeBuilder (this, 0);
  89. FieldBuilder fb = global_type.DefineInitializedData (name, data, attributes);
  90. if (global_fields != null) {
  91. FieldBuilder[] new_fields = new FieldBuilder [global_fields.Length+1];
  92. System.Array.Copy (global_fields, new_fields, global_fields.Length);
  93. new_fields [global_fields.Length] = fb;
  94. global_fields = new_fields;
  95. } else {
  96. global_fields = new FieldBuilder [1];
  97. global_fields [0] = fb;
  98. }
  99. return fb;
  100. }
  101. public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
  102. if (name == null)
  103. throw new ArgumentNullException ("name");
  104. if (global_type_created != null)
  105. throw new InvalidOperationException ("global fields already created");
  106. if (global_type == null)
  107. global_type = new TypeBuilder (this, 0);
  108. FieldBuilder fb = global_type.DefineUninitializedData (name, size, attributes);
  109. if (global_fields != null) {
  110. FieldBuilder[] new_fields = new FieldBuilder [global_fields.Length+1];
  111. System.Array.Copy (global_fields, new_fields, global_fields.Length);
  112. new_fields [global_fields.Length] = fb;
  113. global_fields = new_fields;
  114. } else {
  115. global_fields = new FieldBuilder [1];
  116. global_fields [0] = fb;
  117. }
  118. return fb;
  119. }
  120. public MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
  121. {
  122. return DefineGlobalMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
  123. }
  124. public MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
  125. {
  126. if (name == null)
  127. throw new ArgumentNullException ("name");
  128. if ((attributes & MethodAttributes.Static) == 0)
  129. throw new ArgumentException ("global methods must be static");
  130. if (global_type_created != null)
  131. throw new InvalidOperationException ("global methods already created");
  132. if (global_type == null)
  133. global_type = new TypeBuilder (this, 0);
  134. MethodBuilder mb = global_type.DefineMethod (name, attributes, callingConvention, returnType, parameterTypes);
  135. if (global_methods != null) {
  136. MethodBuilder[] new_methods = new MethodBuilder [global_methods.Length+1];
  137. System.Array.Copy (global_methods, new_methods, global_methods.Length);
  138. new_methods [global_methods.Length] = mb;
  139. global_methods = new_methods;
  140. } else {
  141. global_methods = new MethodBuilder [1];
  142. global_methods [0] = mb;
  143. }
  144. return mb;
  145. }
  146. [MonoTODO]
  147. public TypeBuilder DefineType (string name) {
  148. // FIXME: LAMESPEC: what other attributes should we use here as default?
  149. return DefineType (name, TypeAttributes.Public, typeof(object), null);
  150. }
  151. public TypeBuilder DefineType (string name, TypeAttributes attr) {
  152. return DefineType (name, attr, typeof(object), null);
  153. }
  154. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
  155. return DefineType (name, attr, parent, null);
  156. }
  157. private TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
  158. TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces, packsize, typesize);
  159. if (types != null) {
  160. TypeBuilder[] new_types = new TypeBuilder [types.Length + 1];
  161. System.Array.Copy (types, new_types, types.Length);
  162. new_types [types.Length] = res;
  163. types = new_types;
  164. } else {
  165. types = new TypeBuilder [1];
  166. types [0] = res;
  167. }
  168. name_cache.Add (name, res);
  169. return res;
  170. }
  171. internal void RegisterTypeName (TypeBuilder tb, string name) {
  172. name_cache.Add (name, tb);
  173. }
  174. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
  175. return DefineType (name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
  176. }
  177. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
  178. return DefineType (name, attr, parent, null, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
  179. }
  180. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
  181. return DefineType (name, attr, parent, null, packsize, TypeBuilder.UnspecifiedTypeSize);
  182. }
  183. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
  184. return DefineType (name, attr, parent, null, packsize, typesize);
  185. }
  186. public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  187. return new MonoArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes);
  188. }
  189. public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
  190. EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
  191. return eb;
  192. }
  193. public override Type GetType( string className) {
  194. return GetType (className, false, false);
  195. }
  196. public override Type GetType( string className, bool ignoreCase) {
  197. return GetType (className, false, ignoreCase);
  198. }
  199. private TypeBuilder search_in_array (TypeBuilder[] arr, string className) {
  200. int i;
  201. for (i = 0; i < arr.Length; ++i) {
  202. if (String.Compare (className, arr [i].FullName, true) == 0) {
  203. return arr [i];
  204. }
  205. }
  206. return null;
  207. }
  208. private TypeBuilder search_nested_in_array (TypeBuilder[] arr, string className) {
  209. int i;
  210. for (i = 0; i < arr.Length; ++i) {
  211. if (String.Compare (className, arr [i].Name, true) == 0)
  212. return arr [i];
  213. }
  214. return null;
  215. }
  216. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  217. private static extern Type create_modified_type (TypeBuilder tb, string modifiers);
  218. static char[] type_modifiers = {'&', '[', '*'};
  219. private TypeBuilder GetMaybeNested (TypeBuilder t, string className) {
  220. int subt;
  221. string pname, rname;
  222. subt = className.IndexOf ('+');
  223. if (subt < 0) {
  224. if (t.subtypes != null)
  225. return search_nested_in_array (t.subtypes, className);
  226. return null;
  227. }
  228. if (t.subtypes != null) {
  229. pname = className.Substring (0, subt);
  230. rname = className.Substring (subt + 1);
  231. TypeBuilder result = search_nested_in_array (t.subtypes, pname);
  232. if (result != null)
  233. return GetMaybeNested (result, rname);
  234. }
  235. return null;
  236. }
  237. public override Type GetType (string className, bool throwOnError, bool ignoreCase) {
  238. int subt;
  239. string orig = className;
  240. string modifiers;
  241. TypeBuilder result = null;
  242. if (types == null && throwOnError)
  243. throw new TypeLoadException (className);
  244. subt = className.IndexOfAny (type_modifiers);
  245. if (subt >= 0) {
  246. modifiers = className.Substring (subt);
  247. className = className.Substring (0, subt);
  248. } else
  249. modifiers = null;
  250. if (!ignoreCase) {
  251. result = name_cache [className] as TypeBuilder;
  252. } else {
  253. subt = className.IndexOf ('+');
  254. if (subt < 0) {
  255. if (types != null)
  256. result = search_in_array (types, className);
  257. } else {
  258. string pname, rname;
  259. pname = className.Substring (0, subt);
  260. rname = className.Substring (subt + 1);
  261. result = search_in_array (types, pname);
  262. if (result != null)
  263. result = GetMaybeNested (result, rname);
  264. }
  265. }
  266. if ((result == null) && throwOnError)
  267. throw new TypeLoadException (orig);
  268. if (result != null && (modifiers != null))
  269. return create_modified_type (result, modifiers);
  270. return result;
  271. }
  272. internal int get_next_table_index (object obj, int table, bool inc) {
  273. return assemblyb.get_next_table_index (obj, table, inc);
  274. }
  275. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  276. if (cattrs != null) {
  277. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  278. cattrs.CopyTo (new_array, 0);
  279. new_array [cattrs.Length] = customBuilder;
  280. cattrs = new_array;
  281. } else {
  282. cattrs = new CustomAttributeBuilder [1];
  283. cattrs [0] = customBuilder;
  284. }
  285. }
  286. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  287. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  288. }
  289. public ISymbolWriter GetSymWriter () {
  290. return symbol_writer;
  291. }
  292. public ISymbolDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) {
  293. if (symbol_writer == null)
  294. throw new InvalidOperationException ();
  295. return symbol_writer.DefineDocument (url, language, languageVendor, documentType);
  296. }
  297. public override Type [] GetTypes ()
  298. {
  299. if (types == null)
  300. return new TypeBuilder [0];
  301. int n = types.Length;
  302. TypeBuilder [] copy = new TypeBuilder [n];
  303. Array.Copy (types, copy, n);
  304. return copy;
  305. }
  306. internal void Save ()
  307. {
  308. if (symbol_writer != null) {
  309. string res_name;
  310. if (is_main_module)
  311. res_name = "MonoSymbolFile";
  312. else
  313. res_name = "MonoSymbolFile:" + fqname;
  314. byte[] data = symbol_writer.CreateSymbolFile (assemblyb);
  315. assemblyb.EmbedResource (res_name, data, ResourceAttributes.Public);
  316. }
  317. }
  318. }
  319. }