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