ModuleBuilder.cs 6.2 KB

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