ModuleBuilder.cs 11 KB

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