ModuleBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. internal IMonoSymbolWriter symbol_writer;
  29. Hashtable name_cache;
  30. bool transient;
  31. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  32. private static extern void basic_init (ModuleBuilder ab);
  33. internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname, bool emitSymbolInfo, bool transient) {
  34. this.name = this.scopename = name;
  35. this.fqname = fullyqname;
  36. this.assembly = this.assemblyb = assb;
  37. this.transient = transient;
  38. guid = Guid.NewGuid().ToByteArray ();
  39. table_idx = get_next_table_index (this, 0x00, true);
  40. name_cache = new Hashtable ();
  41. if (emitSymbolInfo)
  42. GetSymbolWriter (fullyqname);
  43. basic_init (this);
  44. }
  45. internal void GetSymbolWriter (string filename)
  46. {
  47. Assembly assembly;
  48. try {
  49. assembly = Assembly.Load ("Mono.CSharp.Debugger");
  50. } catch (FileNotFoundException) {
  51. return;
  52. }
  53. Type type = assembly.GetType ("Mono.CSharp.Debugger.MonoSymbolWriter");
  54. if (type == null)
  55. return;
  56. // First get the constructor.
  57. {
  58. Type[] arg_types = new Type [1];
  59. arg_types [0] = typeof (ModuleBuilder);
  60. ConstructorInfo constructor = type.GetConstructor (arg_types);
  61. object[] args = new object [1];
  62. args [0] = this;
  63. if (constructor == null)
  64. return;
  65. Object instance = constructor.Invoke (args);
  66. if (instance == null)
  67. return;
  68. if (!(instance is IMonoSymbolWriter))
  69. return;
  70. symbol_writer = (IMonoSymbolWriter) instance;
  71. }
  72. }
  73. public override string FullyQualifiedName {get { return fqname;}}
  74. public bool IsTransient () {
  75. return transient;
  76. }
  77. public void CreateGlobalFunctions ()
  78. {
  79. if (global_type_created != null)
  80. throw new InvalidOperationException ("global methods already created");
  81. if (global_type != null)
  82. global_type_created = global_type.CreateType ();
  83. }
  84. public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
  85. if (data == null)
  86. throw new ArgumentNullException ("data");
  87. FieldBuilder fb = DefineUninitializedData (name, data.Length,
  88. attributes | FieldAttributes.HasFieldRVA);
  89. fb.SetRVAData (data);
  90. return fb;
  91. }
  92. public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
  93. if (name == null)
  94. throw new ArgumentNullException ("name");
  95. if (global_type_created != null)
  96. throw new InvalidOperationException ("global fields already created");
  97. if (global_type == null)
  98. global_type = new TypeBuilder (this, 0);
  99. string typeName = "$ArrayType$" + size;
  100. Type datablobtype = GetType (typeName, false, false);
  101. if (datablobtype == null) {
  102. TypeBuilder tb = DefineType (typeName,
  103. TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
  104. assemblyb.corlib_value_type, null, PackingSize.Size1, size);
  105. tb.CreateType ();
  106. datablobtype = tb;
  107. }
  108. FieldBuilder fb = global_type.DefineField (name, datablobtype, attributes|FieldAttributes.Static);
  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, null);
  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 (assemblyb.mainModule == this)
  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. internal string FileName {
  319. get {
  320. return fqname;
  321. }
  322. }
  323. }
  324. }