2003-02-10 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
17 namespace System.Reflection.Emit {
18         public sealed class ConstructorBuilder : ConstructorInfo {
19                 private RuntimeMethodHandle mhandle;
20                 private ILGenerator ilgen;
21                 private Type[] parameters;
22                 private MethodAttributes attrs;
23                 private MethodImplAttributes iattrs;
24                 private int table_idx;
25                 private CallingConventions call_conv;
26                 private TypeBuilder type;
27                 private ParameterBuilder[] pinfo;
28                 private CustomAttributeBuilder[] cattrs;
29                 private bool init_locals = true;
30
31                 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
32                         attrs = attributes | MethodAttributes.SpecialName;
33                         call_conv = callingConvention;
34                         if (parameterTypes != null) {
35                                 this.parameters = new Type [parameterTypes.Length];
36                                 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
37                         }
38                         type = tb;
39                         table_idx = get_next_table_index (this, 0x06, true);
40                 }
41                 
42                 public bool InitLocals {
43                         get {return init_locals;}
44                         set {init_locals = value;}
45                 }
46
47                 internal TypeBuilder TypeBuilder {
48                         get {return type;}
49                 }
50                 
51                 public override MethodImplAttributes GetMethodImplementationFlags() {
52                         return iattrs;
53                 }
54                 public override ParameterInfo[] GetParameters() {
55                         if (parameters == null)
56                                 return null;
57
58                         ParameterInfo[] retval = new ParameterInfo [parameters.Length];
59                         for (int i = 0; i < parameters.Length; i++) {
60                                 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i+1], parameters [i], this, i + 1);
61                         }
62
63                         return retval;
64                 }
65                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
66                         throw not_supported ();
67                 }
68                 public override object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) {
69                         throw not_supported ();
70                 }
71
72                 public override RuntimeMethodHandle MethodHandle { 
73                         get {
74                                 throw not_supported ();
75                         }
76                 }
77
78                 public override MethodAttributes Attributes { 
79                         get {return attrs;} 
80                 }
81                 public override Type ReflectedType { get {return type;}}
82                 public override Type DeclaringType { get {return type;}}
83                 public Type ReturnType { get {return null;}}
84                 public override string Name { 
85                         get {return (attrs & MethodAttributes.Static) != 0 ? ".cctor" : ".ctor";}
86                 }
87                 public string Signature {
88                         get {return "constructor signature";}
89                 }
90
91                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
92                 }
93
94                 [MonoTODO]
95                 public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string strParamName)
96                 {
97                         if ((iSequence < 1) || (iSequence > parameters.Length))
98                                 throw new ArgumentOutOfRangeException ("iSequence");
99                         if (type.is_created)
100                                 throw not_after_created ();
101
102                         ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
103                         // check iSequence
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 }