ModuleBuilder.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. // System.Reflection.Emit/ModuleBuilder.cs
  25. //
  26. // Author:
  27. // Paolo Molaro ([email protected])
  28. //
  29. // (C) 2001 Ximian, Inc. http://www.ximian.com
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Collections;
  34. using System.Runtime.CompilerServices;
  35. using System.Runtime.InteropServices;
  36. using System.Diagnostics.SymbolStore;
  37. using System.IO;
  38. using System.Resources;
  39. using System.Globalization;
  40. namespace System.Reflection.Emit {
  41. public class ModuleBuilder : Module {
  42. #region Sync with reflection.h
  43. private IntPtr dynamic_image;
  44. private int num_types;
  45. private TypeBuilder[] types;
  46. private CustomAttributeBuilder[] cattrs;
  47. private byte[] guid;
  48. private int table_idx;
  49. internal AssemblyBuilder assemblyb;
  50. private MethodBuilder[] global_methods;
  51. private FieldBuilder[] global_fields;
  52. bool is_main;
  53. private MonoResource[] resources;
  54. #endregion
  55. private TypeBuilder global_type;
  56. private Type global_type_created;
  57. Hashtable name_cache;
  58. Hashtable us_string_cache = new Hashtable ();
  59. private int[] table_indexes;
  60. bool transient;
  61. ModuleBuilderTokenGenerator token_gen;
  62. ArrayList resource_writers = null;
  63. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  64. private static extern void basic_init (ModuleBuilder ab);
  65. internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname, bool emitSymbolInfo, bool transient) {
  66. this.name = this.scopename = name;
  67. this.fqname = fullyqname;
  68. this.assembly = this.assemblyb = assb;
  69. this.transient = transient;
  70. // to keep mcs fast we do not want CryptoConfig wo be involved to create the RNG
  71. guid = Guid.FastNewGuidArray ();
  72. // guid = Guid.NewGuid().ToByteArray ();
  73. table_idx = get_next_table_index (this, 0x00, true);
  74. name_cache = new Hashtable ();
  75. basic_init (this);
  76. CreateGlobalType ();
  77. }
  78. public override string FullyQualifiedName {get { return fqname;}}
  79. public bool IsTransient () {
  80. return transient;
  81. }
  82. public void CreateGlobalFunctions ()
  83. {
  84. if (global_type_created != null)
  85. throw new InvalidOperationException ("global methods already created");
  86. if (global_type != null)
  87. global_type_created = global_type.CreateType ();
  88. }
  89. public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
  90. if (data == null)
  91. throw new ArgumentNullException ("data");
  92. FieldBuilder fb = DefineUninitializedData (name, data.Length,
  93. attributes | FieldAttributes.HasFieldRVA);
  94. fb.SetRVAData (data);
  95. return fb;
  96. }
  97. public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
  98. if (name == null)
  99. throw new ArgumentNullException ("name");
  100. if (global_type_created != null)
  101. throw new InvalidOperationException ("global fields already created");
  102. if (global_type == null)
  103. global_type = new TypeBuilder (this, 0);
  104. string typeName = "$ArrayType$" + size;
  105. Type datablobtype = GetType (typeName, false, false);
  106. if (datablobtype == null) {
  107. TypeBuilder tb = DefineType (typeName,
  108. TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
  109. assemblyb.corlib_value_type, null, PackingSize.Size1, size);
  110. tb.CreateType ();
  111. datablobtype = tb;
  112. }
  113. FieldBuilder fb = global_type.DefineField (name, datablobtype, attributes|FieldAttributes.Static);
  114. if (global_fields != null) {
  115. FieldBuilder[] new_fields = new FieldBuilder [global_fields.Length+1];
  116. System.Array.Copy (global_fields, new_fields, global_fields.Length);
  117. new_fields [global_fields.Length] = fb;
  118. global_fields = new_fields;
  119. } else {
  120. global_fields = new FieldBuilder [1];
  121. global_fields [0] = fb;
  122. }
  123. return fb;
  124. }
  125. private void addGlobalMethod (MethodBuilder mb) {
  126. if (global_methods != null) {
  127. MethodBuilder[] new_methods = new MethodBuilder [global_methods.Length+1];
  128. System.Array.Copy (global_methods, new_methods, global_methods.Length);
  129. new_methods [global_methods.Length] = mb;
  130. global_methods = new_methods;
  131. } else {
  132. global_methods = new MethodBuilder [1];
  133. global_methods [0] = mb;
  134. }
  135. }
  136. public MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
  137. {
  138. return DefineGlobalMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
  139. }
  140. public MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  141. return DefineGlobalMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
  142. }
  143. #if NET_2_0 || BOOTSTRAP_NET_2_0
  144. public
  145. #else
  146. internal
  147. #endif
  148. MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
  149. {
  150. if (name == null)
  151. throw new ArgumentNullException ("name");
  152. if ((attributes & MethodAttributes.Static) == 0)
  153. throw new ArgumentException ("global methods must be static");
  154. if (global_type_created != null)
  155. throw new InvalidOperationException ("global methods already created");
  156. if (global_type == null)
  157. global_type = new TypeBuilder (this, 0);
  158. MethodBuilder mb = global_type.DefineMethod (name, attributes, callingConvention, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
  159. addGlobalMethod (mb);
  160. return mb;
  161. }
  162. public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
  163. return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet);
  164. }
  165. public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
  166. if (name == null)
  167. throw new ArgumentNullException ("name");
  168. if ((attributes & MethodAttributes.Static) == 0)
  169. throw new ArgumentException ("global methods must be static");
  170. if (global_type_created != null)
  171. throw new InvalidOperationException ("global methods already created");
  172. if (global_type == null)
  173. global_type = new TypeBuilder (this, 0);
  174. MethodBuilder mb = global_type.DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet);
  175. addGlobalMethod (mb);
  176. return mb;
  177. }
  178. public TypeBuilder DefineType (string name) {
  179. return DefineType (name, 0);
  180. }
  181. public TypeBuilder DefineType (string name, TypeAttributes attr) {
  182. if ((attr & TypeAttributes.Interface) != 0)
  183. return DefineType (name, attr, null, null);
  184. return DefineType (name, attr, typeof(object), null);
  185. }
  186. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
  187. return DefineType (name, attr, parent, null);
  188. }
  189. private void AddType (TypeBuilder tb)
  190. {
  191. if (types != null) {
  192. if (types.Length == num_types) {
  193. TypeBuilder[] new_types = new TypeBuilder [types.Length * 2];
  194. System.Array.Copy (types, new_types, num_types);
  195. types = new_types;
  196. }
  197. } else {
  198. types = new TypeBuilder [1];
  199. }
  200. types [num_types] = tb;
  201. num_types ++;
  202. }
  203. private TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
  204. if (name_cache.Contains (name))
  205. throw new ArgumentException ("Duplicate type name within an assembly.");
  206. TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces, packsize, typesize, null);
  207. AddType (res);
  208. name_cache.Add (name, res);
  209. return res;
  210. }
  211. internal void RegisterTypeName (TypeBuilder tb, string name) {
  212. name_cache.Add (name, tb);
  213. }
  214. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
  215. return DefineType (name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
  216. }
  217. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
  218. return DefineType (name, attr, parent, null, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
  219. }
  220. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
  221. return DefineType (name, attr, parent, null, packsize, TypeBuilder.UnspecifiedTypeSize);
  222. }
  223. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
  224. return DefineType (name, attr, parent, null, packsize, typesize);
  225. }
  226. public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  227. return new MonoArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes);
  228. }
  229. public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
  230. if (name_cache.Contains (name))
  231. throw new ArgumentException ("Duplicate type name within an assembly.");
  232. EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
  233. TypeBuilder res = eb.GetTypeBuilder ();
  234. AddType (res);
  235. name_cache.Add (name, res);
  236. return eb;
  237. }
  238. public override Type GetType( string className) {
  239. return GetType (className, false, false);
  240. }
  241. public override Type GetType( string className, bool ignoreCase) {
  242. return GetType (className, false, ignoreCase);
  243. }
  244. private TypeBuilder search_in_array (TypeBuilder[] arr, int validElementsInArray, string className) {
  245. int i;
  246. for (i = 0; i < validElementsInArray; ++i) {
  247. if (String.Compare (className, arr [i].FullName, true, CultureInfo.InvariantCulture) == 0) {
  248. return arr [i];
  249. }
  250. }
  251. return null;
  252. }
  253. private TypeBuilder search_nested_in_array (TypeBuilder[] arr, int validElementsInArray, string className) {
  254. int i;
  255. for (i = 0; i < validElementsInArray; ++i) {
  256. if (String.Compare (className, arr [i].Name, true, CultureInfo.InvariantCulture) == 0)
  257. return arr [i];
  258. }
  259. return null;
  260. }
  261. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  262. private static extern Type create_modified_type (TypeBuilder tb, string modifiers);
  263. static readonly char [] type_modifiers = {'&', '[', '*'};
  264. private TypeBuilder GetMaybeNested (TypeBuilder t, string className) {
  265. int subt;
  266. string pname, rname;
  267. subt = className.IndexOf ('+');
  268. if (subt < 0) {
  269. if (t.subtypes != null)
  270. return search_nested_in_array (t.subtypes, t.subtypes.Length, className);
  271. return null;
  272. }
  273. if (t.subtypes != null) {
  274. pname = className.Substring (0, subt);
  275. rname = className.Substring (subt + 1);
  276. TypeBuilder result = search_nested_in_array (t.subtypes, t.subtypes.Length, pname);
  277. if (result != null)
  278. return GetMaybeNested (result, rname);
  279. }
  280. return null;
  281. }
  282. public override Type GetType (string className, bool throwOnError, bool ignoreCase) {
  283. int subt;
  284. string orig = className;
  285. string modifiers;
  286. TypeBuilder result = null;
  287. if (types == null && throwOnError)
  288. throw new TypeLoadException (className);
  289. subt = className.IndexOfAny (type_modifiers);
  290. if (subt >= 0) {
  291. modifiers = className.Substring (subt);
  292. className = className.Substring (0, subt);
  293. } else
  294. modifiers = null;
  295. if (!ignoreCase) {
  296. result = name_cache [className] as TypeBuilder;
  297. } else {
  298. subt = className.IndexOf ('+');
  299. if (subt < 0) {
  300. if (types != null)
  301. result = search_in_array (types, num_types, className);
  302. } else {
  303. string pname, rname;
  304. pname = className.Substring (0, subt);
  305. rname = className.Substring (subt + 1);
  306. result = search_in_array (types, num_types, pname);
  307. if (result != null)
  308. result = GetMaybeNested (result, rname);
  309. }
  310. }
  311. if ((result == null) && throwOnError)
  312. throw new TypeLoadException (orig);
  313. if (result != null && (modifiers != null))
  314. return create_modified_type (result, modifiers);
  315. return result;
  316. }
  317. internal int get_next_table_index (object obj, int table, bool inc) {
  318. if (table_indexes == null) {
  319. table_indexes = new int [64];
  320. for (int i=0; i < 64; ++i)
  321. table_indexes [i] = 1;
  322. /* allow room for .<Module> in TypeDef table */
  323. table_indexes [0x02] = 2;
  324. }
  325. // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
  326. if (inc)
  327. return table_indexes [table]++;
  328. return table_indexes [table];
  329. }
  330. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  331. if (cattrs != null) {
  332. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  333. cattrs.CopyTo (new_array, 0);
  334. new_array [cattrs.Length] = customBuilder;
  335. cattrs = new_array;
  336. } else {
  337. cattrs = new CustomAttributeBuilder [1];
  338. cattrs [0] = customBuilder;
  339. }
  340. }
  341. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  342. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  343. }
  344. public ISymbolWriter GetSymWriter () {
  345. return null;
  346. }
  347. public ISymbolDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) {
  348. return null;
  349. }
  350. public override Type [] GetTypes ()
  351. {
  352. if (types == null)
  353. return new TypeBuilder [0];
  354. int n = num_types;
  355. TypeBuilder [] copy = new TypeBuilder [n];
  356. Array.Copy (types, copy, n);
  357. return copy;
  358. }
  359. public IResourceWriter DefineResource (string name, string description, ResourceAttributes attribute)
  360. {
  361. if (name == null)
  362. throw new ArgumentNullException ("name");
  363. if (name == String.Empty)
  364. throw new ArgumentException ("name cannot be empty");
  365. if (transient)
  366. throw new InvalidOperationException ("The module is transient");
  367. if (!assemblyb.IsSave)
  368. throw new InvalidOperationException ("The assembly is transient");
  369. ResourceWriter writer = new ResourceWriter (new MemoryStream ());
  370. if (resource_writers == null)
  371. resource_writers = new ArrayList ();
  372. resource_writers.Add (writer);
  373. // The data is filled out later
  374. if (resources != null) {
  375. MonoResource[] new_r = new MonoResource [resources.Length + 1];
  376. System.Array.Copy(resources, new_r, resources.Length);
  377. resources = new_r;
  378. } else {
  379. resources = new MonoResource [1];
  380. }
  381. int p = resources.Length - 1;
  382. resources [p].name = name;
  383. resources [p].attrs = attribute;
  384. return writer;
  385. }
  386. public IResourceWriter DefineResource (string name, string description)
  387. {
  388. return DefineResource (name, description, ResourceAttributes.Public);
  389. }
  390. [MonoTODO]
  391. public void DefineUnmanagedResource (byte[] resource)
  392. {
  393. if (resource == null)
  394. throw new ArgumentNullException ("resource");
  395. throw new NotImplementedException ();
  396. }
  397. [MonoTODO]
  398. public void DefineUnmanagedResource (string resourceFileName)
  399. {
  400. if (resourceFileName == null)
  401. throw new ArgumentNullException ("resourceFileName");
  402. if (resourceFileName == String.Empty)
  403. throw new ArgumentException ("resourceFileName");
  404. if (!File.Exists (resourceFileName) || Directory.Exists (resourceFileName))
  405. throw new FileNotFoundException ("File '" + resourceFileName + "' does not exists or is a directory.");
  406. throw new NotImplementedException ();
  407. }
  408. [MonoTODO]
  409. public void SetSymCustomAttribute (string name, byte[] data)
  410. {
  411. throw new NotImplementedException ();
  412. }
  413. [MonoTODO]
  414. public void SetUserEntryPoint (MethodInfo entryPoint)
  415. {
  416. if (entryPoint == null)
  417. throw new ArgumentNullException ("entryPoint");
  418. if (entryPoint.DeclaringType.Module != this)
  419. throw new InvalidOperationException ("entryPoint is not contained in this module");
  420. throw new NotImplementedException ();
  421. }
  422. public MethodToken GetMethodToken (MethodInfo method)
  423. {
  424. if (method == null)
  425. throw new ArgumentNullException ("method");
  426. if (method.DeclaringType.Module != this)
  427. throw new InvalidOperationException ("The method is not in this module");
  428. return new MethodToken (GetToken (method));
  429. }
  430. public MethodToken GetArrayMethodToken (Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
  431. {
  432. return GetMethodToken (GetArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes));
  433. }
  434. public MethodToken GetConstructorToken (ConstructorInfo con)
  435. {
  436. if (con == null)
  437. throw new ArgumentNullException ("con");
  438. return new MethodToken (GetToken (con));
  439. }
  440. public FieldToken GetFieldToken (FieldInfo field)
  441. {
  442. if (field == null)
  443. throw new ArgumentNullException ("field");
  444. if (field.DeclaringType.Module != this)
  445. throw new InvalidOperationException ("The method is not in this module");
  446. return new FieldToken (GetToken (field));
  447. }
  448. [MonoTODO]
  449. public SignatureToken GetSignatureToken (byte[] sigBytes, int sigLength)
  450. {
  451. throw new NotImplementedException ();
  452. }
  453. public SignatureToken GetSignatureToken (SignatureHelper sigHelper)
  454. {
  455. if (sigHelper == null)
  456. throw new ArgumentNullException ("sigHelper");
  457. return new SignatureToken (GetToken (sigHelper));
  458. }
  459. public StringToken GetStringConstant (string str)
  460. {
  461. if (str == null)
  462. throw new ArgumentNullException ("str");
  463. return new StringToken (GetToken (str));
  464. }
  465. public TypeToken GetTypeToken (Type type)
  466. {
  467. if (type == null)
  468. throw new ArgumentNullException ("type");
  469. if (type.IsByRef)
  470. throw new ArgumentException ("type can't be a byref type", "type");
  471. if (!IsTransient () && (type.Module is ModuleBuilder) && ((ModuleBuilder)type.Module).IsTransient ())
  472. throw new InvalidOperationException ("a non-transient module can't reference a transient module");
  473. return new TypeToken (GetToken (type));
  474. }
  475. public TypeToken GetTypeToken (string type)
  476. {
  477. return GetTypeToken (GetType (name));
  478. }
  479. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  480. private static extern int getUSIndex (ModuleBuilder mb, string str);
  481. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  482. private static extern int getToken (ModuleBuilder mb, object obj);
  483. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  484. private static extern int getMethodToken (ModuleBuilder mb, MethodInfo method,
  485. Type[] opt_param_types);
  486. internal int GetToken (string str) {
  487. if (us_string_cache.Contains (str))
  488. return (int)us_string_cache [str];
  489. int result = getUSIndex (this, str);
  490. us_string_cache [str] = result;
  491. return result;
  492. }
  493. internal int GetToken (MemberInfo member) {
  494. return getToken (this, member);
  495. }
  496. internal int GetToken (MethodInfo method, Type[] opt_param_types) {
  497. return getMethodToken (this, method, opt_param_types);
  498. }
  499. internal int GetToken (SignatureHelper helper) {
  500. return getToken (this, helper);
  501. }
  502. internal TokenGenerator GetTokenGenerator () {
  503. if (token_gen == null)
  504. token_gen = new ModuleBuilderTokenGenerator (this);
  505. return token_gen;
  506. }
  507. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  508. private static extern void build_metadata (ModuleBuilder mb);
  509. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  510. private extern void WriteToFile (IntPtr handle);
  511. internal void Save ()
  512. {
  513. if (transient && !is_main)
  514. return;
  515. if (types != null) {
  516. for (int i = 0; i < num_types; ++i)
  517. if (!types [i].is_created)
  518. throw new NotSupportedException ("Type '" + types [i].FullName + "' was not completed.");
  519. }
  520. if ((global_type != null) && (global_type_created == null))
  521. global_type_created = global_type.CreateType ();
  522. if (resource_writers != null) {
  523. for (int i = 0; i < resource_writers.Count; ++i) {
  524. ResourceWriter writer = (ResourceWriter)resource_writers [i];
  525. writer.Generate ();
  526. MemoryStream stream = (MemoryStream)writer.Stream;
  527. resources [i].data = new byte [stream.Length];
  528. stream.Seek (0, SeekOrigin.Begin);
  529. stream.Read (resources [i].data, 0, (int)stream.Length);
  530. }
  531. }
  532. build_metadata (this);
  533. string fileName = fqname;
  534. if (assemblyb.AssemblyDir != null)
  535. fileName = Path.Combine (assemblyb.AssemblyDir, fileName);
  536. using (FileStream file = new FileStream (fileName, FileMode.Create, FileAccess.Write))
  537. WriteToFile (file.Handle);
  538. //
  539. // The constant 0x80000000 is internal to Mono, it means `make executable'
  540. //
  541. File.SetAttributes (fileName, (FileAttributes) (unchecked ((int) 0x80000000)));
  542. }
  543. internal string FileName {
  544. get {
  545. return fqname;
  546. }
  547. }
  548. internal bool IsMain {
  549. set {
  550. is_main = value;
  551. }
  552. }
  553. internal void CreateGlobalType () {
  554. if (global_type == null)
  555. global_type = new TypeBuilder (this, 0);
  556. }
  557. internal static Guid Mono_GetGuid (ModuleBuilder mb)
  558. {
  559. return new Guid (mb.guid);
  560. }
  561. }
  562. internal class ModuleBuilderTokenGenerator : TokenGenerator {
  563. private ModuleBuilder mb;
  564. public ModuleBuilderTokenGenerator (ModuleBuilder mb) {
  565. this.mb = mb;
  566. }
  567. public int GetToken (string str) {
  568. return mb.GetToken (str);
  569. }
  570. public int GetToken (MemberInfo member) {
  571. return mb.GetToken (member);
  572. }
  573. public int GetToken (MethodInfo method, Type[] opt_param_types) {
  574. return mb.GetToken (method, opt_param_types);
  575. }
  576. public int GetToken (SignatureHelper helper) {
  577. return mb.GetToken (helper);
  578. }
  579. }
  580. }