2003-02-09 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
1 //
2 // System.Reflection.Emit/AssemblyBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Resources;
13 using System.IO;
14 using System.Security.Policy;
15 using System.Runtime.Serialization;
16 using System.Globalization;
17 using System.Runtime.CompilerServices;
18 using System.Collections;
19
20 namespace System.Reflection.Emit {
21
22         internal struct MonoResource {
23                 public byte[] data;
24                 public string name;
25                 public string filename;
26                 public ResourceAttributes attrs;
27         }
28
29         public sealed class AssemblyBuilder : Assembly {
30                 private IntPtr dynamic_assembly;
31                 private MethodInfo entry_point;
32                 private ModuleBuilder[] modules;
33                 private string name;
34                 private string dir;
35                 private CustomAttributeBuilder[] cattrs;
36                 private MonoResource[] resources;
37                 string keyfile;
38                 string version;
39                 string culture;
40                 uint algid;
41                 uint flags;
42                 PEFileKinds pekind = PEFileKinds.Dll;
43                 bool delay_sign;
44                 internal Type corlib_object_type = typeof (System.Object);
45                 internal Type corlib_value_type = typeof (System.ValueType);
46                 internal Type corlib_enum_type = typeof (System.Enum);
47                 private int[] table_indexes;
48                 Hashtable us_string_cache = new Hashtable ();
49
50                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51                 private static extern void basic_init (AssemblyBuilder ab);
52                 
53                 internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
54                         name = n.Name;
55                         dir = directory;
56                         basic_init (this);
57                 }
58
59                 internal int get_next_table_index (object obj, int table, bool inc) {
60                         if (table_indexes == null) {
61                                 table_indexes = new int [64];
62                                 for (int i=0; i < 64; ++i)
63                                         table_indexes [i] = 1;
64                                 /* allow room for .<Module> in TypeDef table */
65                                 table_indexes [0x02] = 2;
66                         }
67                         // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
68                         if (inc)
69                                 return table_indexes [table]++;
70                         return table_indexes [table];
71                 }
72
73                 public override string CodeBase {
74                         get {
75                                 return null;
76                         }
77                 }
78                 
79                 public override MethodInfo EntryPoint {
80                         get {
81                                 return entry_point;
82                         }
83                 }
84
85                 public override string Location {
86                         get {
87                                 return null;
88                         }
89                 }
90
91                 public void AddResourceFile (string name, string fileName)
92                 {
93                         AddResourceFile (name, fileName, ResourceAttributes.Public);
94                 }
95
96                 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
97                 {
98                         if (resources != null) {
99                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
100                                 System.Array.Copy(resources, new_r, resources.Length);
101                                 resources = new_r;
102                         } else {
103                                 resources = new MonoResource [1];
104                         }
105                         int p = resources.Length - 1;
106                         resources [p].name = name;
107                         resources [p].filename = fileName;
108                         resources [p].attrs = attribute;
109                 }
110
111                 public void EmbedResourceFile (string name, string fileName)
112                 {
113                         EmbedResourceFile (name, fileName, ResourceAttributes.Public);
114                 }
115
116                 public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
117                 {
118                         if (resources != null) {
119                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
120                                 System.Array.Copy(resources, new_r, resources.Length);
121                                 resources = new_r;
122                         } else {
123                                 resources = new MonoResource [1];
124                         }
125                         int p = resources.Length - 1;
126                         resources [p].name = name;
127                         resources [p].attrs = attribute;
128                         try {
129                                 FileStream s = new FileStream (fileName, FileMode.Open, FileAccess.Read);
130                                 long len = s.Length;
131                                 resources [p].data = new byte [len];
132                                 s.Read (resources [p].data, 0, (int)len);
133                                 s.Close ();
134                         } catch {
135                                 /* do something */
136                         }
137                 }
138
139                 internal void EmbedResource (string name, byte[] blob, ResourceAttributes attribute)
140                 {
141                         if (resources != null) {
142                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
143                                 System.Array.Copy(resources, new_r, resources.Length);
144                                 resources = new_r;
145                         } else {
146                                 resources = new MonoResource [1];
147                         }
148                         int p = resources.Length - 1;
149                         resources [p].name = name;
150                         resources [p].attrs = attribute;
151                         resources [p].data = blob;
152                 }
153
154                 public ModuleBuilder DefineDynamicModule (string name)
155                 {
156                         return DefineDynamicModule (name, name, false);
157                 }
158
159                 public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
160                 {
161                         return DefineDynamicModule (name, name, emitSymbolInfo);
162                 }
163
164                 public ModuleBuilder DefineDynamicModule(string name, string fileName)
165                 {
166                         return DefineDynamicModule (name, fileName, false);
167                 }
168
169                 public ModuleBuilder DefineDynamicModule (string name, string fileName,
170                                                           bool emitSymbolInfo)
171                 {
172                         ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo, modules == null);
173
174                         if (modules != null) {
175                                 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
176                                 System.Array.Copy(modules, new_modules, modules.Length);
177                                 new_modules [modules.Length] = r;
178                                 modules = new_modules;
179                         } else {
180                                 modules = new ModuleBuilder [1];
181                                 modules [0] = r;
182                         }
183                         return r;
184                 }
185
186                 public IResourceWriter DefineResource (string name, string description, string fileName)
187                 {
188                         return DefineResource (name, description, fileName, ResourceAttributes.Public);
189                 }
190
191                 public IResourceWriter DefineResource (string name, string description,
192                                                        string fileName, ResourceAttributes attribute)
193                 {
194                         return null;
195                 }
196
197                 public void DefineUnmanagedResource (byte[] resource)
198                 {
199                 }
200
201                 public void DefineUnmanagedResource (string resourceFileName)
202                 {
203                 }
204
205                 public void DefineVersionInfoResource ()
206                 {
207                 }
208
209                 public void DefineVersionInfoResource (string product, string productVersion,
210                                                        string company, string copyright, string trademark)
211                 {
212                 }
213
214                 public ModuleBuilder GetDynamicModule (string name)
215                 {
216                         return null;
217                 }
218
219                 public override Type[] GetExportedTypes ()
220                 {
221                         return null;
222                 }
223
224                 public override FileStream GetFile (string name)
225                 {
226                         return null;
227                 }
228
229                 /*public virtual FileStream[] GetFiles() {
230                         return null;
231                 }
232                 public override FileStream[] GetFiles(bool getResourceModules) {
233                         return null;
234                 }*/
235
236                 /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
237                   {
238                         return null;
239                 }
240                 public virtual string[] GetManifestResourceNames() {
241                         return null;
242                 }
243                 public virtual Stream GetManifestResourceStream(string name) {
244                         return null;
245                 }
246                 public virtual Stream GetManifestResourceStream(Type type, string name) {
247                         return null;
248                 }*/
249
250                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
251                 private static extern int getUSIndex (AssemblyBuilder ab, string str);
252
253                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
254                 private static extern int getToken (AssemblyBuilder ab, object obj);
255
256                 internal int GetToken (string str) {
257                         if (us_string_cache.Contains (str))
258                                 return (int)us_string_cache [str];
259                         int result = getUSIndex (this, str);
260                         us_string_cache [str] = result;
261                         return result;
262                 }
263                 
264                 internal int GetToken (MemberInfo member) {
265                         return getToken (this, member);
266                 }
267
268                 internal int GetToken (SignatureHelper helper) {
269                         return getToken (this, helper);
270                 }
271
272                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
273                 private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset);
274
275                 public void Save (string assemblyFileName)
276                 {
277                         byte[] buf = new byte [65536];
278                         FileStream file;
279                         int count, offset;
280
281                         build_metadata (this);
282
283                         foreach (ModuleBuilder module in modules)
284                                 module.Save ();
285
286                         if (dir != null) {
287                                 assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
288                         }
289
290                         file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
291
292                         offset = 0;
293                         while ((count = getDataChunk (this, buf, offset)) != 0) {
294                                 file.Write (buf, 0, count);
295                                 offset += count;
296                         }
297                         file.Close ();
298
299                         //
300                         // The constant 0x80000000 is internal to Mono, it means `make executable'
301                         //
302                         File.SetAttributes (assemblyFileName, (FileAttributes) (unchecked ((int) 0x80000000)));
303                 }
304
305                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
306                 private static extern void build_metadata (AssemblyBuilder ab);
307
308                 public void SetEntryPoint (MethodInfo entryMethod)
309                 {
310                         SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
311                 }
312
313                 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
314                 {
315                         entry_point = entryMethod;
316                         pekind = fileKind;
317                 }
318
319                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
320                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
321                         byte[] data;
322                         int len, pos;
323                         if (attrname == "System.Reflection.AssemblyVersionAttribute") {
324                                 data = customBuilder.Data;
325                                 pos = 2;
326                                 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
327                                 version = CustomAttributeBuilder.string_from_bytes (data, pos, len);
328                                 return;
329                         } else if (attrname == "System.Reflection.AssemblyKeyFileAttribute") {
330                                 data = customBuilder.Data;
331                                 pos = 2;
332                                 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
333                                 keyfile = CustomAttributeBuilder.string_from_bytes (data, pos, len);
334                         } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
335                                 data = customBuilder.Data;
336                                 pos = 2;
337                                 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
338                                 culture = CustomAttributeBuilder.string_from_bytes (data, pos, len);
339                         } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
340                                 data = customBuilder.Data;
341                                 pos = 2;
342                                 algid = (uint)data [pos];
343                                 algid |= ((uint)data [pos + 1]) << 8;
344                                 algid |= ((uint)data [pos + 2]) << 16;
345                                 algid |= ((uint)data [pos + 3]) << 24;
346                         } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") {
347                                 data = customBuilder.Data;
348                                 pos = 2;
349                                 flags = (uint)data [pos];
350                                 flags |= ((uint)data [pos + 1]) << 8;
351                                 flags |= ((uint)data [pos + 2]) << 16;
352                                 flags |= ((uint)data [pos + 3]) << 24;
353                                 return;
354                         } else if (attrname == "System.Reflection.AssemblyDelaySignAttribute") {
355                                 data = customBuilder.Data;
356                                 pos = 2;
357                                 delay_sign = data [2] != 0;
358                         }
359                         if (cattrs != null) {
360                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
361                                 cattrs.CopyTo (new_array, 0);
362                                 new_array [cattrs.Length] = customBuilder;
363                                 cattrs = new_array;
364                         } else {
365                                 cattrs = new CustomAttributeBuilder [1];
366                                 cattrs [0] = customBuilder;
367                         }
368                 }
369                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
370                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
371                 }
372
373                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
374                         this.corlib_object_type = corlib_object_type;
375                         this.corlib_value_type = corlib_value_type;
376                         this.corlib_enum_type = corlib_enum_type;
377                 }
378         }
379 }