2003-01-17 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / MethodBuilder.cs
1
2 //
3 // System.Reflection.Emit/MethodBuilder.cs
4 //
5 // Author:
6 //   Paolo Molaro (lupus@ximian.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 using System;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
17
18 namespace System.Reflection.Emit {
19         public sealed class MethodBuilder : MethodInfo {
20                 private RuntimeMethodHandle mhandle;
21                 private Type rtype;
22                 private Type[] parameters;
23                 private MethodAttributes attrs;
24                 private MethodImplAttributes iattrs;
25                 private string name;
26                 private int table_idx;
27                 private byte[] code;
28                 private ILGenerator ilgen;
29                 private TypeBuilder type;
30                 private ParameterBuilder[] pinfo;
31                 private CustomAttributeBuilder[] cattrs;
32                 private MethodInfo override_method;
33                 private string pi_dll;
34                 private string pi_entry;
35                 private CharSet ncharset;
36                 private CallingConvention native_cc;
37                 private CallingConventions call_conv;
38                 private bool init_locals = true;
39
40                 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
41                         this.name = name;
42                         this.attrs = attributes;
43                         this.call_conv = callingConvention;
44                         this.rtype = returnType;
45                         if (parameterTypes != null) {
46                                 this.parameters = new Type [parameterTypes.Length];
47                                 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
48                         }
49                         type = tb;
50                         table_idx = get_next_table_index (this, 0x06, true);
51                         //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
52                 }
53
54                 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, 
55                         CallingConventions callingConvention, Type returnType, Type[] parameterTypes, 
56                         String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset) 
57                         : this (tb, name, attributes, callingConvention, returnType, parameterTypes) {
58                         pi_dll = dllName;
59                         pi_entry = entryName;
60                         native_cc = nativeCConv;
61                         ncharset = nativeCharset;
62                 }
63
64                 public bool InitLocals {
65                         get {return init_locals;}
66                         set {init_locals = value;}
67                 }
68
69                 internal TypeBuilder TypeBuilder {
70                         get {return type;}
71                 }
72                 
73                 public override Type ReturnType {get {return rtype;}}
74                 public override Type ReflectedType {get {return type;}}
75                 public override Type DeclaringType {get {return type;}}
76                 public override string Name {get {return name;}}
77                 public override RuntimeMethodHandle MethodHandle {get {return mhandle;}}
78                 public override MethodAttributes Attributes {get {return attrs;}}
79                 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
80                         get {return null;}
81                 }
82                 public MethodToken GetToken() {
83                         return new MethodToken(0x06000000 | table_idx);
84                 }
85                 
86                 public override MethodInfo GetBaseDefinition() {
87                         return null;
88                 }
89                 public override MethodImplAttributes GetMethodImplementationFlags() {
90                         return iattrs;
91                 }
92                 public override ParameterInfo[] GetParameters() {
93                         if (parameters == null)
94                                 return null;
95
96                         ParameterInfo[] retval = new ParameterInfo [parameters.Length];
97                         for (int i = 0; i < parameters.Length; i++) {
98                                 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
99                         }
100                         return retval;
101                 }
102                 
103                 public void CreateMethodBody( byte[] il, int count) {
104                         code = new byte [count];
105                         System.Array.Copy(il, code, count);
106                 }
107                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
108                         return null;
109                 }
110                 public override bool IsDefined (Type attribute_type, bool inherit) {
111                         return false;
112                 }
113                 public override object[] GetCustomAttributes( bool inherit) {
114                         return null;
115                 }
116                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
117                         return null;
118                 }
119                 public ILGenerator GetILGenerator () {
120                         return GetILGenerator (64);
121                 }
122                 public ILGenerator GetILGenerator (int size) {
123                         ilgen = new ILGenerator (this, size);
124                         return ilgen;
125                 }
126                 
127                 [MonoTODO]
128                 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
129                 {
130                         ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
131                         // check position
132                         if (pinfo == null)
133                                 pinfo = new ParameterBuilder [parameters.Length + 1];
134                         pinfo [position] = pb;
135                         return pb;
136                 }
137
138                 internal void fixup () {
139                         if (ilgen != null)
140                                 ilgen.label_fixup ();
141                 }
142
143                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
144                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
145                         if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
146                                 byte[] data = customBuilder.Data;
147                                 int impla; // the (stupid) ctor takes a short or an int ... 
148                                 impla = (int)data [2];
149                                 impla |= ((int)data [3]) << 8;
150                                 SetImplementationFlags ((MethodImplAttributes)impla);
151                                 return;
152                         }
153                         if (cattrs != null) {
154                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
155                                 cattrs.CopyTo (new_array, 0);
156                                 new_array [cattrs.Length] = customBuilder;
157                                 cattrs = new_array;
158                         } else {
159                                 cattrs = new CustomAttributeBuilder [1];
160                                 cattrs [0] = customBuilder;
161                         }
162                 }
163                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
164                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
165                 }
166                 public void SetImplementationFlags( MethodImplAttributes attributes) {
167                         iattrs = attributes;
168                 }
169                 internal override int get_next_table_index (object obj, int table, bool inc) {
170                         return type.get_next_table_index (obj, table, inc);
171                 }
172
173                 internal void set_override (MethodInfo mdecl) {
174                         override_method = mdecl;
175                 }
176         }
177 }
178