ModuleBuilder.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. Hashtable name_cache;
  25. internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname, bool emitSymbolInfo) {
  26. this.name = this.scopename = name;
  27. this.fqname = fullyqname;
  28. this.assembly = this.assemblyb = assb;
  29. guid = Guid.NewGuid().ToByteArray ();
  30. table_idx = get_next_table_index (0x00, true);
  31. name_cache = new Hashtable ();
  32. if (emitSymbolInfo)
  33. symbol_writer = GetSymbolWriter (fullyqname);
  34. }
  35. internal ISymbolWriter GetSymbolWriter (string filename)
  36. {
  37. Assembly assembly;
  38. try {
  39. assembly = Assembly.Load ("Mono.CSharp.Debugger");
  40. } catch (FileNotFoundException) {
  41. return null;
  42. }
  43. Type type = assembly.GetType ("Mono.CSharp.Debugger.MonoSymbolWriter");
  44. if (type == null)
  45. return null;
  46. Type[] arg_types = new Type [1];
  47. arg_types [0] = typeof (string);
  48. ConstructorInfo constructor = type.GetConstructor (arg_types);
  49. object[] args = new object [1];
  50. args [0] = filename;
  51. if (constructor == null)
  52. return null;
  53. Object instance = constructor.Invoke (args);
  54. if (instance == null)
  55. return null;
  56. if (!(instance is ISymbolWriter))
  57. return null;
  58. return (ISymbolWriter) instance;
  59. }
  60. public override string FullyQualifiedName {get { return fqname;}}
  61. [MonoTODO]
  62. public TypeBuilder DefineType (string name) {
  63. // FIXME: LAMESPEC: what other attributes should we use here as default?
  64. return DefineType (name, TypeAttributes.Public, typeof(object), null);
  65. }
  66. public TypeBuilder DefineType (string name, TypeAttributes attr) {
  67. return DefineType (name, attr, typeof(object), null);
  68. }
  69. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
  70. return DefineType (name, attr, parent, null);
  71. }
  72. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
  73. TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces);
  74. if (types != null) {
  75. TypeBuilder[] new_types = new TypeBuilder [types.Length + 1];
  76. System.Array.Copy (types, new_types, types.Length);
  77. new_types [types.Length] = res;
  78. types = new_types;
  79. } else {
  80. types = new TypeBuilder [1];
  81. types [0] = res;
  82. }
  83. name_cache.Add (name, res);
  84. return res;
  85. }
  86. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
  87. return DefineType (name, attr, parent, null);
  88. }
  89. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
  90. return DefineType (name, attr, parent, null);
  91. }
  92. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
  93. return DefineType (name, attr, parent, null);
  94. }
  95. public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  96. return new MonoArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes);
  97. }
  98. public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
  99. EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
  100. return eb;
  101. }
  102. public override Type GetType( string className) {
  103. return GetType (className, false, false);
  104. }
  105. public override Type GetType( string className, bool ignoreCase) {
  106. return GetType (className, false, ignoreCase);
  107. }
  108. private TypeBuilder search_in_array (TypeBuilder[] arr, string className, bool ignoreCase) {
  109. int i;
  110. if (arr == types && !ignoreCase)
  111. return (TypeBuilder)name_cache [className];
  112. for (i = 0; i < arr.Length; ++i) {
  113. if (String.Compare (className, arr [i].FullName, ignoreCase) == 0) {
  114. return arr [i];
  115. }
  116. }
  117. return null;
  118. }
  119. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  120. private static extern Type create_modified_type (TypeBuilder tb, string modifiers);
  121. static char[] type_modifiers = {'&', '[', '*'};
  122. public override Type GetType( string className, bool throwOnError, bool ignoreCase) {
  123. int subt;
  124. string modifiers;
  125. TypeBuilder result = null;
  126. if (types == null && throwOnError)
  127. throw new TypeLoadException (className);
  128. subt = className.IndexOfAny (type_modifiers);
  129. if (subt >= 0) {
  130. modifiers = className.Substring (subt);
  131. className = className.Substring (0, subt);
  132. } else
  133. modifiers = null;
  134. subt = className.IndexOf ('+');
  135. if (subt < 0) {
  136. if (types != null)
  137. result = search_in_array (types, className, ignoreCase);
  138. } else {
  139. string pname, rname;
  140. pname = className.Substring (0, subt);
  141. rname = className.Substring (subt + 1);
  142. result = search_in_array (types, pname, ignoreCase);
  143. if ((result != null) && (result.subtypes != null))
  144. result = search_in_array (result.subtypes, rname, ignoreCase);
  145. else
  146. result = null;
  147. }
  148. if ((result == null) && throwOnError)
  149. throw new TypeLoadException (className);
  150. if (result != null && (modifiers != null))
  151. return create_modified_type (result, modifiers);
  152. return result;
  153. }
  154. internal int get_next_table_index (int table, bool inc) {
  155. return assemblyb.get_next_table_index (table, inc);
  156. }
  157. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  158. if (cattrs != null) {
  159. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  160. cattrs.CopyTo (new_array, 0);
  161. new_array [cattrs.Length] = customBuilder;
  162. cattrs = new_array;
  163. } else {
  164. cattrs = new CustomAttributeBuilder [1];
  165. cattrs [0] = customBuilder;
  166. }
  167. }
  168. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  169. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  170. }
  171. public ISymbolWriter GetSymWriter () {
  172. return symbol_writer;
  173. }
  174. public ISymbolDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) {
  175. if (symbol_writer == null)
  176. throw new InvalidOperationException ();
  177. return symbol_writer.DefineDocument (url, language, languageVendor, documentType);
  178. }
  179. }
  180. }