ModuleBuilder.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if (assemblyb.methods == null)
  48. assemblyb.methods = new ArrayList ();
  49. // First get the constructor.
  50. {
  51. Type[] arg_types = new Type [3];
  52. arg_types [0] = typeof (ModuleBuilder);
  53. arg_types [1] = typeof (string);
  54. arg_types [2] = typeof (ArrayList);
  55. ConstructorInfo constructor = type.GetConstructor (arg_types);
  56. object[] args = new object [3];
  57. args [0] = this;
  58. args [1] = filename;
  59. args [2] = assemblyb.methods;
  60. if (constructor == null)
  61. return;
  62. Object instance = constructor.Invoke (args);
  63. if (instance == null)
  64. return;
  65. if (!(instance is ISymbolWriter))
  66. return;
  67. symbol_writer = (ISymbolWriter) instance;
  68. }
  69. // Get the DefineLocalVariable method.
  70. {
  71. Type[] arg_types = new Type [6];
  72. arg_types [0] = typeof (string);
  73. arg_types [1] = typeof (LocalBuilder);
  74. arg_types [2] = typeof (FieldAttributes);
  75. arg_types [3] = typeof (int);
  76. arg_types [4] = typeof (int);
  77. arg_types [5] = typeof (int);
  78. symwriter_define_local = type.GetMethod ("DefineLocalVariable", arg_types);
  79. if (symwriter_define_local == null)
  80. throw new NotSupportedException ();
  81. }
  82. }
  83. internal void SymWriter_DefineLocalVariable (string name, LocalBuilder local,
  84. FieldAttributes attributes,
  85. int position, int startOffset, int endOffset)
  86. {
  87. if ((symbol_writer == null) || (symwriter_define_local == null))
  88. return;
  89. object[] args = new object [6];
  90. args [0] = name;
  91. args [1] = local;
  92. args [2] = attributes;
  93. args [3] = position;
  94. args [4] = startOffset;
  95. args [5] = endOffset;
  96. symwriter_define_local.Invoke (symbol_writer, args);
  97. }
  98. public override string FullyQualifiedName {get { return fqname;}}
  99. [MonoTODO]
  100. public TypeBuilder DefineType (string name) {
  101. // FIXME: LAMESPEC: what other attributes should we use here as default?
  102. return DefineType (name, TypeAttributes.Public, typeof(object), null);
  103. }
  104. public TypeBuilder DefineType (string name, TypeAttributes attr) {
  105. return DefineType (name, attr, typeof(object), null);
  106. }
  107. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
  108. return DefineType (name, attr, parent, null);
  109. }
  110. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
  111. TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces);
  112. if (types != null) {
  113. TypeBuilder[] new_types = new TypeBuilder [types.Length + 1];
  114. System.Array.Copy (types, new_types, types.Length);
  115. new_types [types.Length] = res;
  116. types = new_types;
  117. } else {
  118. types = new TypeBuilder [1];
  119. types [0] = res;
  120. }
  121. name_cache.Add (name, res);
  122. return res;
  123. }
  124. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
  125. return DefineType (name, attr, parent, null);
  126. }
  127. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
  128. return DefineType (name, attr, parent, null);
  129. }
  130. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
  131. return DefineType (name, attr, parent, null);
  132. }
  133. public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  134. return new MonoArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes);
  135. }
  136. public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
  137. EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
  138. return eb;
  139. }
  140. public override Type GetType( string className) {
  141. return GetType (className, false, false);
  142. }
  143. public override Type GetType( string className, bool ignoreCase) {
  144. return GetType (className, false, ignoreCase);
  145. }
  146. private TypeBuilder search_in_array (TypeBuilder[] arr, string className, bool ignoreCase) {
  147. int i;
  148. if (arr == types && !ignoreCase)
  149. return (TypeBuilder)name_cache [className];
  150. for (i = 0; i < arr.Length; ++i) {
  151. if (String.Compare (className, arr [i].FullName, ignoreCase) == 0) {
  152. return arr [i];
  153. }
  154. }
  155. return null;
  156. }
  157. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  158. private static extern Type create_modified_type (TypeBuilder tb, string modifiers);
  159. static char[] type_modifiers = {'&', '[', '*'};
  160. public override Type GetType( string className, bool throwOnError, bool ignoreCase) {
  161. int subt;
  162. string modifiers;
  163. TypeBuilder result = null;
  164. if (types == null && throwOnError)
  165. throw new TypeLoadException (className);
  166. subt = className.IndexOfAny (type_modifiers);
  167. if (subt >= 0) {
  168. modifiers = className.Substring (subt);
  169. className = className.Substring (0, subt);
  170. } else
  171. modifiers = null;
  172. subt = className.IndexOf ('+');
  173. if (subt < 0) {
  174. if (types != null)
  175. result = search_in_array (types, className, ignoreCase);
  176. } else {
  177. string pname, rname;
  178. pname = className.Substring (0, subt);
  179. rname = className.Substring (subt + 1);
  180. result = search_in_array (types, pname, ignoreCase);
  181. if ((result != null) && (result.subtypes != null))
  182. result = search_in_array (result.subtypes, rname, ignoreCase);
  183. else
  184. result = null;
  185. }
  186. if ((result == null) && throwOnError)
  187. throw new TypeLoadException (className);
  188. if (result != null && (modifiers != null))
  189. return create_modified_type (result, modifiers);
  190. return result;
  191. }
  192. internal int get_next_table_index (object obj, int table, bool inc) {
  193. return assemblyb.get_next_table_index (obj, table, inc);
  194. }
  195. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  196. if (cattrs != null) {
  197. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  198. cattrs.CopyTo (new_array, 0);
  199. new_array [cattrs.Length] = customBuilder;
  200. cattrs = new_array;
  201. } else {
  202. cattrs = new CustomAttributeBuilder [1];
  203. cattrs [0] = customBuilder;
  204. }
  205. }
  206. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  207. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  208. }
  209. public ISymbolWriter GetSymWriter () {
  210. return symbol_writer;
  211. }
  212. public ISymbolDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) {
  213. if (symbol_writer == null)
  214. throw new InvalidOperationException ();
  215. return symbol_writer.DefineDocument (url, language, languageVendor, documentType);
  216. }
  217. }
  218. }