e2d744fb91437a629b72e9f6a21b38b451d6ac5b
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
1 //
2 // System.Reflection.Emit/TypeBuilder.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.Reflection.Emit;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15
16 namespace System.Reflection.Emit {
17         public sealed class TypeBuilder : Type {
18         private string tname;
19         private string nspace;
20         private Type parent;
21         private Type[] interfaces;
22         private MethodBuilder[] methods;
23         private PropertyBuilder[] properties;
24         private FieldBuilder[] fields;
25         private TypeAttributes attrs;
26         private int table_idx;
27         internal ModuleBuilder pmodule;
28         private PackingSize packing_size;
29
30         public const int UnspecifiedTypeSize = -1;
31
32         internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces) {
33                         int sep_index;
34                         this.parent = parent;
35                         this.attrs = attr;
36                         packing_size = PackingSize.Unspecified;
37                         sep_index = name.LastIndexOf('.');
38                         if (sep_index != -1) {
39                                 this.tname = name.Substring (sep_index + 1);
40                                 this.nspace = name.Substring (0, sep_index);
41                         } else {
42                                 this.tname = name;
43                                 this.nspace = "";
44                         }
45                         if (interfaces != null) {
46                                 this.interfaces = new Type[interfaces.Length];
47                                 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
48                         }
49                         pmodule = mb;
50                         table_idx = mb.assemblyb.get_next_table_index (0x02, true);
51         }
52
53                 public override Assembly Assembly {get {return null;}}
54                 public override string AssemblyQualifiedName {get {return null;}}
55                 public override Type BaseType {get {return parent;}}
56                 public override Type DeclaringType {get {return null;}}
57                 public override string FullName {
58                         get {
59                                 if (nspace != null)
60                                         return String.Concat (nspace, ".", tname);
61                                 return tname;
62                         }
63                 }
64                 //public override Guid GUID {get {return null;}}
65                 public override Module Module {
66                         get {return pmodule;}
67                 }
68                 public override string Name {
69                         get {return tname;}
70                 }
71                 public override string Namespace {
72                         get {return nspace;}
73                 }
74                 public PackingSize PackingSize {
75                         get {return packing_size;}
76                 }
77                 public override Type ReflectedType {get {return null;}}
78                 public override MemberTypes MemberType { get {return (MemberTypes)0;}}
79
80                 public override bool IsDefined( Type attributeType, bool inherit) {
81                         return false;
82                 }
83                 public override object[] GetCustomAttributes(bool inherit) {
84                         return null;
85                 }
86                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
87                         return null;
88                 }
89                 
90                 public TypeBuilder DefineNestedType (string name) {
91                         // FIXME: LAMESPEC: what other attributes should we use here as default?
92                         return DefineNestedType (name, TypeAttributes.Public, typeof(object), null);
93                 }
94
95                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
96                         return DefineNestedType (name, attr, typeof(object), null);
97                 }
98
99                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
100                         return DefineNestedType (name, attr, parent, null);
101                 }
102
103                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
104                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces);
105                         /*if (types != null) {
106                                 TypeBuilder[] new_types = new TypeBuilder [types.Length];
107                                 System.Array.Copy (types, new_types, types.Length);
108                                 new_types [types.Length] = res;
109                                 types = new_types;
110                         } else {
111                                 types = new TypeBuilder [1];
112                                 types [0] = res;
113                         }*/
114                         return res;
115                 }
116
117                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
118                         return DefineNestedType (name, attr, parent, null);
119                 }
120
121                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
122                         return DefineNestedType (name, attr, parent, null);
123                 }
124
125                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
126                         return DefineNestedType (name, attr, parent, null);
127                 }
128
129                 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
130                         return null;
131                 }
132
133                 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
134                         return null;
135                 }
136
137                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
138                         return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
139                 }
140
141                 private void append_method (MethodBuilder mb) {
142                         if (methods != null) {
143                                 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
144                                 System.Array.Copy (methods, new_methods, methods.Length);
145                                 new_methods [methods.Length] = mb;
146                                 methods = new_methods;
147                         } else {
148                                 methods = new MethodBuilder [1];
149                                 methods [0] = mb;
150                         }
151                 }
152
153                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
154                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
155                         append_method (res);
156                         return res;
157                 }
158
159                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
160                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
161                                 dllName, entryName, nativeCallConv, nativeCharSet);
162                         append_method (res);
163                         return res;
164                 }
165
166                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
167                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
168                                 nativeCallConv, nativeCharSet);
169                 }
170
171                 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
172                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
173                         if (fields != null) {
174                                 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
175                                 System.Array.Copy (fields, new_fields, fields.Length);
176                                 new_fields [fields.Length] = res;
177                                 fields = new_fields;
178                         } else {
179                                 fields = new FieldBuilder [1];
180                                 fields [0] = res;
181                         }
182                         return res;
183                 }
184
185                 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
186                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
187
188                         if (properties != null) {
189                                 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
190                                 System.Array.Copy (properties, new_properties, properties.Length);
191                                 new_properties [properties.Length] = res;
192                                 properties = new_properties;
193                         } else {
194                                 properties = new PropertyBuilder [1];
195                                 properties [0] = res;
196                         }
197                         return res;
198                 }
199
200                 public Type CreateType() {
201                         if (methods != null) {
202                                 foreach (MethodBuilder method in methods) {
203                                         method.fixup ();
204                                 }
205                         }
206                         return null;
207                 }
208
209                 public override Type GetElementType () { return null; }
210
211                 public override Type[] GetInterfaces () { return null; }
212
213                 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
214
215                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
216                 }
217                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
218                 }
219
220                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
221                         return null;
222                 }
223
224                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
225                         return null;
226                 }
227
228                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
229                         return null;
230                 }
231
232         }
233 }