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