ModuleBuilder.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. }
  77. public override string FullyQualifiedName {get { return fqname;}}
  78. public bool IsTransient () {
  79. return transient;
  80. }
  81. public void CreateGlobalFunctions ()
  82. {
  83. if (global_type_created != null)
  84. throw new InvalidOperationException ("global methods already created");
  85. if (global_type != null)
  86. global_type_created = global_type.CreateType ();
  87. }
  88. public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
  89. if (data == null)
  90. throw new ArgumentNullException ("data");
  91. FieldBuilder fb = DefineUninitializedData (name, data.Length,
  92. attributes | FieldAttributes.HasFieldRVA);
  93. fb.SetRVAData (data);
  94. return fb;
  95. }
  96. public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
  97. if (name == null)
  98. throw new ArgumentNullException ("name");
  99. if (global_type_created != null)
  100. throw new InvalidOperationException ("global fields already created");
  101. if (global_type == null)
  102. global_type = new TypeBuilder (this, 0);
  103. string typeName = "$ArrayType$" + size;
  104. Type datablobtype = GetType (typeName, false, false);
  105. if (datablobtype == null) {
  106. TypeBuilder tb = DefineType (typeName,
  107. TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
  108. assemblyb.corlib_value_type, null, PackingSize.Size1, size);
  109. tb.CreateType ();
  110. datablobtype = tb;
  111. }
  112. FieldBuilder fb = global_type.DefineField (name, datablobtype, attributes|FieldAttributes.Static);
  113. if (global_fields != null) {
  114. FieldBuilder[] new_fields = new FieldBuilder [global_fields.Length+1];
  115. System.Array.Copy (global_fields, new_fields, global_fields.Length);
  116. new_fields [global_fields.Length] = fb;
  117. global_fields = new_fields;
  118. } else {
  119. global_fields = new FieldBuilder [1];
  120. global_fields [0] = fb;
  121. }
  122. return fb;
  123. }
  124. private void addGlobalMethod (MethodBuilder mb) {
  125. if (global_methods != null) {
  126. MethodBuilder[] new_methods = new MethodBuilder [global_methods.Length+1];
  127. System.Array.Copy (global_methods, new_methods, global_methods.Length);
  128. new_methods [global_methods.Length] = mb;
  129. global_methods = new_methods;
  130. } else {
  131. global_methods = new MethodBuilder [1];
  132. global_methods [0] = mb;
  133. }
  134. }
  135. public MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
  136. {
  137. return DefineGlobalMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
  138. }
  139. public MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  140. return DefineGlobalMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
  141. }
  142. #if NET_2_0 || BOOTSTRAP_NET_2_0
  143. public
  144. #else
  145. internal
  146. #endif
  147. MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
  148. {
  149. if (name == null)
  150. throw new ArgumentNullException ("name");
  151. if ((attributes & MethodAttributes.Static) == 0)
  152. throw new ArgumentException ("global methods must be static");
  153. if (global_type_created != null)
  154. throw new InvalidOperationException ("global methods already created");
  155. if (global_type == null)
  156. global_type = new TypeBuilder (this, 0);
  157. MethodBuilder mb = global_type.DefineMethod (name, attributes, callingConvention, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
  158. addGlobalMethod (mb);
  159. return mb;
  160. }
  161. public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
  162. return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet);
  163. }
  164. public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
  165. if (name == null)
  166. throw new ArgumentNullException ("name");
  167. if ((attributes & MethodAttributes.Static) == 0)
  168. throw new ArgumentException ("global methods must be static");
  169. if (global_type_created != null)
  170. throw new InvalidOperationException ("global methods already created");
  171. if (global_type == null)
  172. global_type = new TypeBuilder (this, 0);
  173. MethodBuilder mb = global_type.DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet);
  174. addGlobalMethod (mb);
  175. return mb;
  176. }
  177. public TypeBuilder DefineType (string name) {
  178. return DefineType (name, 0);
  179. }
  180. public TypeBuilder DefineType (string name, TypeAttributes attr) {
  181. return DefineType (name, attr, typeof(object), null);
  182. }
  183. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
  184. return DefineType (name, attr, parent, null);
  185. }
  186. private TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
  187. if (name_cache.Contains (name))
  188. throw new ArgumentException ("Duplicate type name within an assembly.");
  189. TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces, packsize, typesize, null);
  190. if (types != null) {
  191. if (types.Length == num_types) {
  192. TypeBuilder[] new_types = new TypeBuilder [types.Length * 2];
  193. System.Array.Copy (types, new_types, num_types);
  194. types = new_types;
  195. }
  196. } else {
  197. types = new TypeBuilder [1];
  198. }
  199. types [num_types] = res;
  200. num_types ++;
  201. name_cache.Add (name, res);
  202. return res;
  203. }
  204. internal void RegisterTypeName (TypeBuilder tb, string name) {
  205. name_cache.Add (name, tb);
  206. }
  207. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
  208. return DefineType (name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
  209. }
  210. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
  211. return DefineType (name, attr, parent, null, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
  212. }
  213. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
  214. return DefineType (name, attr, parent, null, packsize, TypeBuilder.UnspecifiedTypeSize);
  215. }
  216. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
  217. return DefineType (name, attr, parent, null, packsize, typesize);
  218. }
  219. public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  220. return new MonoArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes);
  221. }
  222. public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
  223. EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
  224. return eb;
  225. }
  226. public override Type GetType( string className) {
  227. return GetType (className, false, false);
  228. }
  229. public override Type GetType( string className, bool ignoreCase) {
  230. return GetType (className, false, ignoreCase);
  231. }
  232. private TypeBuilder search_in_array (TypeBuilder[] arr, int validElementsInArray, string className) {
  233. int i;
  234. for (i = 0; i < validElementsInArray; ++i) {
  235. if (String.Compare (className, arr [i].FullName, true, CultureInfo.InvariantCulture) == 0) {
  236. return arr [i];
  237. }
  238. }
  239. return null;
  240. }
  241. private TypeBuilder search_nested_in_array (TypeBuilder[] arr, int validElementsInArray, string className) {
  242. int i;
  243. for (i = 0; i < validElementsInArray; ++i) {
  244. if (String.Compare (className, arr [i].Name, true, CultureInfo.InvariantCulture) == 0)
  245. return arr [i];
  246. }
  247. return null;
  248. }
  249. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  250. private static extern Type create_modified_type (TypeBuilder tb, string modifiers);
  251. static readonly char [] type_modifiers = {'&', '[', '*'};
  252. private TypeBuilder GetMaybeNested (TypeBuilder t, string className) {
  253. int subt;
  254. string pname, rname;
  255. subt = className.IndexOf ('+');
  256. if (subt < 0) {
  257. if (t.subtypes != null)
  258. return search_nested_in_array (t.subtypes, t.subtypes.Length, className);
  259. return null;
  260. }
  261. if (t.subtypes != null) {
  262. pname = className.Substring (0, subt);
  263. rname = className.Substring (subt + 1);
  264. TypeBuilder result = search_nested_in_array (t.subtypes, t.subtypes.Length, pname);
  265. if (result != null)
  266. return GetMaybeNested (result, rname);
  267. }
  268. return null;
  269. }
  270. public override Type GetType (string className, bool throwOnError, bool ignoreCase) {
  271. int subt;
  272. string orig = className;
  273. string modifiers;
  274. TypeBuilder result = null;
  275. if (types == null && throwOnError)
  276. throw new TypeLoadException (className);
  277. subt = className.IndexOfAny (type_modifiers);
  278. if (subt >= 0) {
  279. modifiers = className.Substring (subt);
  280. className = className.Substring (0, subt);
  281. } else
  282. modifiers = null;
  283. if (!ignoreCase) {
  284. result = name_cache [className] as TypeBuilder;
  285. } else {
  286. subt = className.IndexOf ('+');
  287. if (subt < 0) {
  288. if (types != null)
  289. result = search_in_array (types, num_types, className);
  290. } else {
  291. string pname, rname;
  292. pname = className.Substring (0, subt);
  293. rname = className.Substring (subt + 1);
  294. result = search_in_array (types, num_types, pname);
  295. if (result != null)
  296. result = GetMaybeNested (result, rname);
  297. }
  298. }
  299. if ((result == null) && throwOnError)
  300. throw new TypeLoadException (orig);
  301. if (result != null && (modifiers != null))
  302. return create_modified_type (result, modifiers);
  303. return result;
  304. }
  305. internal int get_next_table_index (object obj, int table, bool inc) {
  306. if (table_indexes == null) {
  307. table_indexes = new int [64];
  308. for (int i=0; i < 64; ++i)
  309. table_indexes [i] = 1;
  310. /* allow room for .<Module> in TypeDef table */
  311. table_indexes [0x02] = 2;
  312. }
  313. // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
  314. if (inc)
  315. return table_indexes [table]++;
  316. return table_indexes [table];
  317. }
  318. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  319. if (cattrs != null) {
  320. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  321. cattrs.CopyTo (new_array, 0);
  322. new_array [cattrs.Length] = customBuilder;
  323. cattrs = new_array;
  324. } else {
  325. cattrs = new CustomAttributeBuilder [1];
  326. cattrs [0] = customBuilder;
  327. }
  328. }
  329. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  330. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  331. }
  332. public ISymbolWriter GetSymWriter () {
  333. return null;
  334. }
  335. public ISymbolDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) {
  336. return null;
  337. }
  338. public override Type [] GetTypes ()
  339. {
  340. if (types == null)
  341. return new TypeBuilder [0];
  342. int n = num_types;
  343. TypeBuilder [] copy = new TypeBuilder [n];
  344. Array.Copy (types, copy, n);
  345. return copy;
  346. }
  347. public IResourceWriter DefineResource (string name, string description, ResourceAttributes attribute)
  348. {
  349. if (name == null)
  350. throw new ArgumentNullException ("name");
  351. if (name == String.Empty)
  352. throw new ArgumentException ("name cannot be empty");
  353. if (transient)
  354. throw new InvalidOperationException ("The module is transient");
  355. if (!assemblyb.IsSave)
  356. throw new InvalidOperationException ("The assembly is transient");
  357. ResourceWriter writer = new ResourceWriter (new MemoryStream ());
  358. if (resource_writers == null)
  359. resource_writers = new ArrayList ();
  360. resource_writers.Add (writer);
  361. // The data is filled out later
  362. if (resources != null) {
  363. MonoResource[] new_r = new MonoResource [resources.Length + 1];
  364. System.Array.Copy(resources, new_r, resources.Length);
  365. resources = new_r;
  366. } else {
  367. resources = new MonoResource [1];
  368. }
  369. int p = resources.Length - 1;
  370. resources [p].name = name;
  371. resources [p].attrs = attribute;
  372. return writer;
  373. }
  374. public IResourceWriter DefineResource (string name, string description)
  375. {
  376. return DefineResource (name, description, ResourceAttributes.Public);
  377. }
  378. [MonoTODO]
  379. public void DefineUnmanagedResource (byte[] resource)
  380. {
  381. if (resource == null)
  382. throw new ArgumentNullException ("resource");
  383. throw new NotImplementedException ();
  384. }
  385. [MonoTODO]
  386. public void DefineUnmanagedResource (string resourceFileName)
  387. {
  388. if (resourceFileName == null)
  389. throw new ArgumentNullException ("resourceFileName");
  390. if (resourceFileName == String.Empty)
  391. throw new ArgumentException ("resourceFileName");
  392. if (!File.Exists (resourceFileName) || Directory.Exists (resourceFileName))
  393. throw new FileNotFoundException ("File '" + resourceFileName + "' does not exists or is a directory.");
  394. throw new NotImplementedException ();
  395. }
  396. [MonoTODO]
  397. public void SetSymCustomAttribute (string name, byte[] data)
  398. {
  399. throw new NotImplementedException ();
  400. }
  401. [MonoTODO]
  402. public void SetUserEntryPoint (MethodInfo entryPoint)
  403. {
  404. if (entryPoint == null)
  405. throw new ArgumentNullException ("entryPoint");
  406. if (entryPoint.DeclaringType.Module != this)
  407. throw new InvalidOperationException ("entryPoint is not contained in this module");
  408. throw new NotImplementedException ();
  409. }
  410. public MethodToken GetMethodToken (MethodInfo method)
  411. {
  412. if (method == null)
  413. throw new ArgumentNullException ("method");
  414. if (method.DeclaringType.Module != this)
  415. throw new InvalidOperationException ("The method is not in this module");
  416. return new MethodToken (GetToken (method));
  417. }
  418. public MethodToken GetArrayMethodToken (Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
  419. {
  420. return GetMethodToken (GetArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes));
  421. }
  422. public MethodToken GetConstructorToken (ConstructorInfo con)
  423. {
  424. if (con == null)
  425. throw new ArgumentNullException ("con");
  426. return new MethodToken (GetToken (con));
  427. }
  428. public FieldToken GetFieldToken (FieldInfo field)
  429. {
  430. if (field == null)
  431. throw new ArgumentNullException ("field");
  432. if (field.DeclaringType.Module != this)
  433. throw new InvalidOperationException ("The method is not in this module");
  434. return new FieldToken (GetToken (field));
  435. }
  436. [MonoTODO]
  437. public SignatureToken GetSignatureToken (byte[] sigBytes, int sigLength)
  438. {
  439. throw new NotImplementedException ();
  440. }
  441. public SignatureToken GetSignatureToken (SignatureHelper sigHelper)
  442. {
  443. if (sigHelper == null)
  444. throw new ArgumentNullException ("sigHelper");
  445. return new SignatureToken (GetToken (sigHelper));
  446. }
  447. public StringToken GetStringConstant (string str)
  448. {
  449. if (str == null)
  450. throw new ArgumentNullException ("str");
  451. return new StringToken (GetToken (str));
  452. }
  453. public TypeToken GetTypeToken (Type type)
  454. {
  455. if (type == null)
  456. throw new ArgumentNullException ("type");
  457. if (type.IsByRef)
  458. throw new ArgumentException ("type can't be a byref type", "type");
  459. if (!IsTransient () && (type.Module is ModuleBuilder) && ((ModuleBuilder)type.Module).IsTransient ())
  460. throw new InvalidOperationException ("a non-transient module can't reference a transient module");
  461. return new TypeToken (GetToken (type));
  462. }
  463. public TypeToken GetTypeToken (string type)
  464. {
  465. return GetTypeToken (GetType (name));
  466. }
  467. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  468. private static extern int getUSIndex (ModuleBuilder mb, string str);
  469. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  470. private static extern int getToken (ModuleBuilder mb, object obj);
  471. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  472. private static extern int getMethodToken (ModuleBuilder mb, MethodInfo method,
  473. Type[] opt_param_types);
  474. internal int GetToken (string str) {
  475. if (us_string_cache.Contains (str))
  476. return (int)us_string_cache [str];
  477. int result = getUSIndex (this, str);
  478. us_string_cache [str] = result;
  479. return result;
  480. }
  481. internal int GetToken (MemberInfo member) {
  482. return getToken (this, member);
  483. }
  484. internal int GetToken (MethodInfo method, Type[] opt_param_types) {
  485. return getMethodToken (this, method, opt_param_types);
  486. }
  487. internal int GetToken (SignatureHelper helper) {
  488. return getToken (this, helper);
  489. }
  490. internal TokenGenerator GetTokenGenerator () {
  491. if (token_gen == null)
  492. token_gen = new ModuleBuilderTokenGenerator (this);
  493. return token_gen;
  494. }
  495. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  496. private static extern void build_metadata (ModuleBuilder mb);
  497. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  498. private static extern int getDataChunk (ModuleBuilder mb, byte[] buf, int offset);
  499. internal void Save ()
  500. {
  501. if (transient && !is_main)
  502. return;
  503. if ((global_type != null) && (global_type_created == null))
  504. global_type_created = global_type.CreateType ();
  505. if (resource_writers != null) {
  506. for (int i = 0; i < resource_writers.Count; ++i) {
  507. ResourceWriter writer = (ResourceWriter)resource_writers [i];
  508. writer.Generate ();
  509. MemoryStream stream = (MemoryStream)writer.Stream;
  510. resources [i].data = new byte [stream.Length];
  511. stream.Seek (0, SeekOrigin.Begin);
  512. stream.Read (resources [i].data, 0, (int)stream.Length);
  513. }
  514. }
  515. build_metadata (this);
  516. string fileName = fqname;
  517. if (assemblyb.AssemblyDir != null)
  518. fileName = System.IO.Path.Combine (assemblyb.AssemblyDir, fileName);
  519. byte[] buf = new byte [65536];
  520. FileStream file;
  521. int count, offset;
  522. file = new FileStream (fileName, FileMode.Create, FileAccess.Write);
  523. offset = 0;
  524. while ((count = getDataChunk (this, buf, offset)) != 0) {
  525. file.Write (buf, 0, count);
  526. offset += count;
  527. }
  528. file.Close ();
  529. //
  530. // The constant 0x80000000 is internal to Mono, it means `make executable'
  531. //
  532. File.SetAttributes (fileName, (FileAttributes) (unchecked ((int) 0x80000000)));
  533. }
  534. internal string FileName {
  535. get {
  536. return fqname;
  537. }
  538. }
  539. internal bool IsMain {
  540. set {
  541. is_main = value;
  542. }
  543. }
  544. internal static Guid Mono_GetGuid (ModuleBuilder mb)
  545. {
  546. return new Guid (mb.guid);
  547. }
  548. }
  549. internal class ModuleBuilderTokenGenerator : TokenGenerator {
  550. private ModuleBuilder mb;
  551. public ModuleBuilderTokenGenerator (ModuleBuilder mb) {
  552. this.mb = mb;
  553. }
  554. public int GetToken (string str) {
  555. return mb.GetToken (str);
  556. }
  557. public int GetToken (MemberInfo member) {
  558. return mb.GetToken (member);
  559. }
  560. public int GetToken (MethodInfo method, Type[] opt_param_types) {
  561. return mb.GetToken (method, opt_param_types);
  562. }
  563. public int GetToken (SignatureHelper helper) {
  564. return mb.GetToken (helper);
  565. }
  566. }
  567. }