Thu Jul 25 13:57:46 CEST 2002 Paolo Molaro <lupus@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                 internal Type corlib_object_type = typeof (System.Object);
38                 internal Type corlib_value_type = typeof (System.ValueType);
39                 internal Type corlib_enum_type = typeof (System.Enum);
40                 private int[] table_indexes;
41                 internal ArrayList methods;
42
43                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
44                 private static extern void basic_init (AssemblyBuilder ab);
45                 
46                 internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
47                         name = n.Name;
48                         dir = directory;
49                         basic_init (this);
50                 }
51
52                 internal int get_next_table_index (object obj, int table, bool inc) {
53                         if (table_indexes == null) {
54                                 table_indexes = new int [64];
55                                 for (int i=0; i < 64; ++i)
56                                         table_indexes [i] = 1;
57                                 /* allow room for .<Module> in TypeDef table */
58                                 table_indexes [0x02] = 2;
59                         }
60                         // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
61                         if (inc) {
62                                 if ((table == 0x06) && (methods != null))
63                                         methods.Add (obj);
64                                 return table_indexes [table]++;
65                         }
66                         return table_indexes [table];
67                 }
68
69                 public override string CodeBase {
70                         get {
71                                 return null;
72                         }
73                 }
74                 
75                 public override MethodInfo EntryPoint {
76                         get {
77                                 return entry_point;
78                         }
79                 }
80
81                 public override string Location {
82                         get {
83                                 return null;
84                         }
85                 }
86
87                 public void AddResourceFile (string name, string fileName)
88                 {
89                         AddResourceFile (name, fileName, ResourceAttributes.Public);
90                 }
91
92                 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
93                 {
94                         if (resources != null) {
95                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
96                                 System.Array.Copy(resources, new_r, resources.Length);
97                                 resources = new_r;
98                         } else {
99                                 resources = new MonoResource [1];
100                         }
101                         int p = resources.Length - 1;
102                         resources [p].name = name;
103                         resources [p].filename = fileName;
104                         resources [p].attrs = attribute;
105                 }
106
107                 public ModuleBuilder DefineDynamicModule (string name)
108                 {
109                         return DefineDynamicModule (name, name, false);
110                 }
111
112                 public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
113                 {
114                         return DefineDynamicModule (name, name, emitSymbolInfo);
115                 }
116
117                 public ModuleBuilder DefineDynamicModule(string name, string fileName)
118                 {
119                         return DefineDynamicModule (name, fileName, false);
120                 }
121
122                 public ModuleBuilder DefineDynamicModule (string name, string fileName,
123                                                           bool emitSymbolInfo)
124                 {
125                         ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo);
126
127                         if (modules != null) {
128                                 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
129                                 System.Array.Copy(modules, new_modules, modules.Length);
130                                 new_modules [modules.Length] = r;
131                                 modules = new_modules;
132                         } else {
133                                 modules = new ModuleBuilder [1];
134                                 modules [0] = r;
135                         }
136                         return r;
137                 }
138
139                 public IResourceWriter DefineResource (string name, string description, string fileName)
140                 {
141                         return DefineResource (name, description, fileName, ResourceAttributes.Public);
142                 }
143
144                 public IResourceWriter DefineResource (string name, string description,
145                                                        string fileName, ResourceAttributes attribute)
146                 {
147                         return null;
148                 }
149
150                 public void DefineUnmanagedResource (byte[] resource)
151                 {
152                 }
153
154                 public void DefineUnmanagedResource (string resourceFileName)
155                 {
156                 }
157
158                 public void DefineVersionInfoResource ()
159                 {
160                 }
161
162                 public void DefineVersionInfoResource (string product, string productVersion,
163                                                        string company, string copyright, string trademark)
164                 {
165                 }
166
167                 public ModuleBuilder GetDynamicModule (string name)
168                 {
169                         return null;
170                 }
171
172                 public override Type[] GetExportedTypes ()
173                 {
174                         return null;
175                 }
176
177                 public override FileStream GetFile (string name)
178                 {
179                         return null;
180                 }
181
182                 /*public virtual FileStream[] GetFiles() {
183                         return null;
184                 }
185                 public override FileStream[] GetFiles(bool getResourceModules) {
186                         return null;
187                 }*/
188
189                 /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
190                   {
191                         return null;
192                 }
193                 public virtual string[] GetManifestResourceNames() {
194                         return null;
195                 }
196                 public virtual Stream GetManifestResourceStream(string name) {
197                         return null;
198                 }
199                 public virtual Stream GetManifestResourceStream(Type type, string name) {
200                         return null;
201                 }*/
202
203                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
204                 private static extern int getUSIndex (AssemblyBuilder ab, string str);
205
206                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
207                 private static extern int getToken (AssemblyBuilder ab, MemberInfo member);
208
209                 internal int GetToken (string str) {
210                         return getUSIndex (this, str);
211                 }
212                 
213                 internal int GetToken (MemberInfo member) {
214                         return getToken (this, member);
215                 }
216                 
217                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
218                 private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset);
219
220                 public void Save (string assemblyFileName)
221                 {
222                         byte[] buf = new byte [65536];
223                         FileStream file;
224                         int count, offset;
225
226                         if (dir != null) {
227                                 assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
228                         }
229
230                         file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
231
232                         offset = 0;
233                         while ((count = getDataChunk (this, buf, offset)) != 0) {
234                                 file.Write (buf, 0, count);
235                                 offset += count;
236                         }
237                         file.Close ();
238                 }
239
240                 public void SetEntryPoint (MethodInfo entryMethod)
241                 {
242                         entry_point = entryMethod;
243                 }
244
245                 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
246                 {
247                         entry_point = entryMethod;
248                 }
249
250                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
251                         if (cattrs != null) {
252                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
253                                 cattrs.CopyTo (new_array, 0);
254                                 new_array [cattrs.Length] = customBuilder;
255                                 cattrs = new_array;
256                         } else {
257                                 cattrs = new CustomAttributeBuilder [1];
258                                 cattrs [0] = customBuilder;
259                         }
260                 }
261                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
262                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
263                 }
264
265                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
266                         this.corlib_object_type = corlib_object_type;
267                         this.corlib_value_type = corlib_value_type;
268                         this.corlib_enum_type = corlib_enum_type;
269                 }
270         }
271 }