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