2003-11-06 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / ConstructorBuilder.cs
1 //
2 // System.Reflection.Emit/ConstructorBuilder.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.Globalization;
14 using System.Security;
15 using System.Security.Permissions;
16 using System.Runtime.InteropServices;
17
18 namespace System.Reflection.Emit {
19
20         public sealed class ConstructorBuilder : ConstructorInfo {
21                 private RuntimeMethodHandle mhandle;
22                 private ILGenerator ilgen;
23                 private Type[] parameters;
24                 private MethodAttributes attrs;
25                 private MethodImplAttributes iattrs;
26                 private int table_idx;
27                 private CallingConventions call_conv;
28                 private TypeBuilder type;
29                 private ParameterBuilder[] pinfo;
30                 private CustomAttributeBuilder[] cattrs;
31                 private bool init_locals = true;
32
33                 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
34                         attrs = attributes | MethodAttributes.SpecialName;
35                         call_conv = callingConvention;
36                         if (parameterTypes != null) {
37                                 this.parameters = new Type [parameterTypes.Length];
38                                 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
39                         }
40                         type = tb;
41                         table_idx = get_next_table_index (this, 0x06, true);
42                 }
43                 
44                 public bool InitLocals {
45                         get {return init_locals;}
46                         set {init_locals = value;}
47                 }
48
49                 internal TypeBuilder TypeBuilder {
50                         get {return type;}
51                 }
52                 
53                 public override MethodImplAttributes GetMethodImplementationFlags() {
54                         return iattrs;
55                 }
56                 public override ParameterInfo[] GetParameters() {
57                         if (parameters == null)
58                                 return null;
59
60                         ParameterInfo[] retval = new ParameterInfo [parameters.Length];
61                         for (int i = 0; i < parameters.Length; i++) {
62                                 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i+1], parameters [i], this, i + 1);
63                         }
64
65                         return retval;
66                 }
67                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
68                         throw not_supported ();
69                 }
70                 public override object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) {
71                         throw not_supported ();
72                 }
73
74                 public override RuntimeMethodHandle MethodHandle { 
75                         get {
76                                 throw not_supported ();
77                         }
78                 }
79
80                 public override MethodAttributes Attributes { 
81                         get {return attrs;} 
82                 }
83                 public override Type ReflectedType { get {return type;}}
84                 public override Type DeclaringType { get {return type;}}
85                 public Type ReturnType { get {return null;}}
86                 public override string Name { 
87                         get {return (attrs & MethodAttributes.Static) != 0 ? ".cctor" : ".ctor";}
88                 }
89                 public string Signature {
90                         get {return "constructor signature";}
91                 }
92
93                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
94                 }
95
96                 public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string strParamName)
97                 {
98                         if ((iSequence < 1) || (iSequence > parameters.Length))
99                                 throw new ArgumentOutOfRangeException ("iSequence");
100                         if (type.is_created)
101                                 throw not_after_created ();
102
103                         ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
104                         if (pinfo == null)
105                                 pinfo = new ParameterBuilder [parameters.Length + 1];
106                         pinfo [iSequence] = pb;
107                         return pb;
108                 }
109
110                 public override bool IsDefined (Type attribute_type, bool inherit) {
111                         throw not_supported ();
112                 }
113
114                 public override object [] GetCustomAttributes (bool inherit) {
115                         throw not_supported ();
116                 }
117
118                 public override object [] GetCustomAttributes (Type attribute_type, bool inherit) {
119                         throw not_supported ();
120                 }
121
122                 public ILGenerator GetILGenerator () {
123                         return GetILGenerator (64);
124                 }
125                 internal ILGenerator GetILGenerator (int size) {
126                         ilgen = new ILGenerator (this, size);
127                         return ilgen;
128                 }
129
130                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
131                         if (customBuilder == null)
132                                 throw new ArgumentNullException ("customBuilder");
133
134                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
135                         if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
136                                 byte[] data = customBuilder.Data;
137                                 int impla; // the (stupid) ctor takes a short or an int ... 
138                                 impla = (int)data [2];
139                                 impla |= ((int)data [3]) << 8;
140                                 SetImplementationFlags ((MethodImplAttributes)impla);
141                                 return;
142                         }
143                         if (cattrs != null) {
144                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
145                                 cattrs.CopyTo (new_array, 0);
146                                 new_array [cattrs.Length] = customBuilder;
147                                 cattrs = new_array;
148                         } else {
149                                 cattrs = new CustomAttributeBuilder [1];
150                                 cattrs [0] = customBuilder;
151                         }
152                 }
153                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
154                         if (con == null)
155                                 throw new ArgumentNullException ("con");
156                         if (binaryAttribute == null)
157                                 throw new ArgumentNullException ("binaryAttribute");
158
159                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
160                 }
161                 public void SetImplementationFlags( MethodImplAttributes attributes) {
162                         if (type.is_created)
163                                 throw not_after_created ();
164
165                         iattrs = attributes;
166                 }
167                 public Module GetModule() {
168                         return type.Module;
169                 }
170                 public MethodToken GetToken() {
171                         return new MethodToken (0x06000000 | table_idx);
172                 }
173
174                 [MonoTODO]
175                 public void SetSymCustomAttribute( string name, byte[] data) {
176                         if (type.is_created)
177                                 throw not_after_created ();
178                 }
179
180                 public override string ToString() {
181                         return "constructor";
182                 }
183
184                 internal void fixup () {
185                         if (ilgen != null)
186                                 ilgen.label_fixup ();
187                 }
188                 internal override int get_next_table_index (object obj, int table, bool inc) {
189                         return type.get_next_table_index (obj, table, inc);
190                 }
191
192                 private Exception not_supported () {
193                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
194                 }
195
196                 private Exception not_after_created () {
197                         return new InvalidOperationException ("Unable to change after type has been created.");
198                 }
199         }
200 }