9f90e13de77ac46a7a36f1e8a3dc43f8cb10313f
[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
39                 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
40                         this.name = name;
41                         this.attrs = attributes;
42                         this.call_conv = callingConvention;
43                         this.rtype = returnType;
44                         if (parameterTypes != null) {
45                                 this.parameters = new Type [parameterTypes.Length];
46                                 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
47                         }
48                         type = tb;
49                         table_idx = get_next_table_index (0x06, true);
50                         //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
51                 }
52
53                 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, 
54                         CallingConventions callingConvention, Type returnType, Type[] parameterTypes, 
55                         String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset) 
56                         : this (tb, name, attributes, callingConvention, returnType, parameterTypes) {
57                         pi_dll = dllName;
58                         pi_entry = entryName;
59                         native_cc = nativeCConv;
60                         ncharset = nativeCharset;
61                 }
62
63                 internal TypeBuilder TypeBuilder {
64                         get {return type;}
65                 }
66                 
67                 public override Type ReturnType {get {return rtype;}}
68                 public override Type ReflectedType {get {return type;}}
69                 public override Type DeclaringType {get {return type;}}
70                 public override string Name {get {return name;}}
71                 public override RuntimeMethodHandle MethodHandle {get {return mhandle;}}
72                 public override MethodAttributes Attributes {get {return attrs;}}
73                 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
74                         get {return null;}
75                 }
76                 public MethodToken GetToken() {
77                         return new MethodToken(0x06000000 | table_idx);
78                 }
79                 
80                 public override MethodInfo GetBaseDefinition() {
81                         return null;
82                 }
83                 public override MethodImplAttributes GetMethodImplementationFlags() {
84                         return iattrs;
85                 }
86                 public override ParameterInfo[] GetParameters() {
87                         return null;
88                 }
89                 
90                 public void CreateMethodBody( byte[] il, int count) {
91                         code = new byte [count];
92                         System.Array.Copy(il, code, count);
93                 }
94                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
95                         return null;
96                 }
97                 public override bool IsDefined (Type attribute_type, bool inherit) {
98                         return false;
99                 }
100                 public override object[] GetCustomAttributes( bool inherit) {
101                         return null;
102                 }
103                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
104                         return null;
105                 }
106                 public ILGenerator GetILGenerator () {
107                         return GetILGenerator (256);
108                 }
109                 internal ILGenerator GetILGenerator (int size) {
110                         ilgen = new ILGenerator (this, size);
111                         return ilgen;
112                 }
113                 
114                 [MonoTODO]
115                 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
116                 {
117                         ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
118                         // check position
119                         if (pinfo == null)
120                                 pinfo = new ParameterBuilder [parameters.Length + 1];
121                         pinfo [position] = pb;
122                         return pb;
123                 }
124
125                 internal void fixup () {
126                         if (ilgen != null)
127                                 ilgen.label_fixup ();
128                 }
129
130                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
131                         if (cattrs != null) {
132                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
133                                 cattrs.CopyTo (new_array, 0);
134                                 new_array [cattrs.Length] = customBuilder;
135                                 cattrs = new_array;
136                         } else {
137                                 cattrs = new CustomAttributeBuilder [1];
138                                 cattrs [0] = customBuilder;
139                         }
140                 }
141                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
142                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
143                 }
144                 public void SetImplementationFlags( MethodImplAttributes attributes) {
145                         iattrs = attributes;
146                 }
147                 internal override int get_next_table_index (int table, bool inc) {
148                         return type.get_next_table_index (table, inc);
149                 }
150
151                 internal void set_override (MethodInfo mdecl) {
152                         override_method = mdecl;
153                 }
154         }
155 }
156