2003-05-19 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / SignatureHelper.cs
1
2 //
3 // System.Reflection.Emit/SignatureHelper.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         [Serializable]
20         public sealed class SignatureHelper {
21                 internal enum SignatureHelperType {
22                         HELPER_FIELD,
23                         HELPER_LOCAL,
24                         HELPER_METHOD,
25                         HELPER_PROPERTY
26                 }
27
28                 private ModuleBuilder module;
29                 private Type[] arguments;
30                 private SignatureHelperType type;
31                 private Type returnType;
32                 private CallingConventions callConv;
33                 private CallingConvention unmanagedCallConv;
34
35                 internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
36                 {
37                         this.type = type;
38                         this.module = module;
39                 }
40
41                 public static SignatureHelper GetFieldSigHelper (Module mod)
42                 {
43                         if (!(mod is ModuleBuilder))
44                                 throw new NotImplementedException ();
45
46                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
47                 }
48
49                 public static SignatureHelper GetLocalVarSigHelper (Module mod)
50                 {
51                         if (!(mod is ModuleBuilder))
52                                 throw new NotImplementedException ();
53
54                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
55                 }
56
57                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callingConvention, Type returnType)
58                 {
59                         return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
60                 }
61
62                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConvention unmanagedCallingConvention, Type returnType)
63                 {
64                         return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
65                 }
66
67                 public static SignatureHelper GetMethodSigHelper( Module mod, Type returnType, Type[] parameterTypes)
68                 {
69                         return GetMethodSigHelper (mod, CallingConventions.Standard, 
70                                                                            (CallingConvention)0, returnType, 
71                                                                            parameterTypes);
72                 }
73                 [MonoTODO]
74                 public static SignatureHelper GetPropertySigHelper( Module mod, Type returnType, Type[] parameterTypes)
75                 {
76                         throw new NotImplementedException ();
77                 }
78
79                 public void AddArgument (Type clsArgument)
80                 {
81                         if (arguments != null) {
82                                 Type[] new_a = new Type [arguments.Length + 1];
83                                 System.Array.Copy (arguments, new_a, arguments.Length);
84                                 new_a [arguments.Length] = clsArgument;
85                                 arguments = new_a;
86                         } else {
87                                 arguments = new Type [1];
88                                 arguments [0] = clsArgument;
89                         }
90                 }
91                 [MonoTODO]
92                 public void AddSentinel ()
93                 {
94                         throw new NotImplementedException ();
95                 }
96                 [MonoTODO]
97                 public override bool Equals (object obj)
98                 {
99                         throw new NotImplementedException ();
100                 }
101                 [MonoTODO]
102                 public override int GetHashCode ()
103                 {
104                         throw new NotImplementedException ();
105                 }
106
107                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108                 internal extern byte[] get_signature_local ();
109
110                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
111                 internal extern byte[] get_signature_field ();
112
113                 public byte[] GetSignature ()
114                 {
115                         switch (type) {
116                         case SignatureHelperType.HELPER_LOCAL:
117                                 return get_signature_local ();
118                         case SignatureHelperType.HELPER_FIELD:
119                                 return get_signature_field ();
120                         default:
121                                 throw new NotImplementedException ();
122                         }
123                 }
124
125                 public override string ToString() {
126                         return "SignatureHelper";
127                 }
128
129                 internal static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callConv, CallingConvention unmanagedCallConv, Type returnType,
130                                                                                                                    Type [] parameters)
131                 {
132                         if (!(mod is ModuleBuilder))
133                                 throw new NotImplementedException ();
134
135                         SignatureHelper helper = 
136                                 new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
137                         helper.returnType = returnType;
138                         helper.callConv = callConv;
139                         helper.unmanagedCallConv = unmanagedCallConv;
140
141                         if (parameters != null) {
142                                 helper.arguments = new Type [parameters.Length];
143                                 for (int i = 0; i < parameters.Length; ++i)
144                                         helper.arguments [i] = parameters [i];
145                         }
146
147                         return helper;
148                 }
149
150
151         }
152 }
153