ModuleBuilder.cs 21 KB

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