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