Copied remotely
[mono.git] / mcs / class / corlib / System.Reflection.Emit / SignatureHelper.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection.Emit/SignatureHelper.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001 Ximian, Inc.  http://www.ximian.com
32 //
33
34 using System;
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40
41 namespace System.Reflection.Emit {
42
43         public sealed class SignatureHelper {
44                 internal enum SignatureHelperType {
45                         HELPER_FIELD,
46                         HELPER_LOCAL,
47                         HELPER_METHOD,
48                         HELPER_PROPERTY
49                 }
50
51                 private ModuleBuilder module;
52                 private Type[] arguments;
53                 private SignatureHelperType type;
54                 private Type returnType;
55                 private CallingConventions callConv;
56                 private CallingConvention unmanagedCallConv;
57
58                 internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
59                 {
60                         this.type = type;
61                         this.module = module;
62                 }
63
64                 public static SignatureHelper GetFieldSigHelper (Module mod)
65                 {
66                         if (!(mod is ModuleBuilder))
67                                 throw new NotImplementedException ();
68
69                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
70                 }
71
72                 public static SignatureHelper GetLocalVarSigHelper (Module mod)
73                 {
74                         if (!(mod is ModuleBuilder))
75                                 throw new NotImplementedException ();
76
77                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
78                 }
79
80                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callingConvention, Type returnType)
81                 {
82                         return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
83                 }
84
85                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConvention unmanagedCallingConvention, Type returnType)
86                 {
87                         return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
88                 }
89
90                 public static SignatureHelper GetMethodSigHelper( Module mod, Type returnType, Type[] parameterTypes)
91                 {
92                         return GetMethodSigHelper (mod, CallingConventions.Standard, 
93                                                                            (CallingConvention)0, returnType, 
94                                                                            parameterTypes);
95                 }
96                 [MonoTODO]
97                 public static SignatureHelper GetPropertySigHelper( Module mod, Type returnType, Type[] parameterTypes)
98                 {
99                         throw new NotImplementedException ();
100                 }
101
102                 public void AddArgument (Type clsArgument)
103                 {
104                         if (arguments != null) {
105                                 Type[] new_a = new Type [arguments.Length + 1];
106                                 System.Array.Copy (arguments, new_a, arguments.Length);
107                                 new_a [arguments.Length] = clsArgument;
108                                 arguments = new_a;
109                         } else {
110                                 arguments = new Type [1];
111                                 arguments [0] = clsArgument;
112                         }
113                 }
114                 [MonoTODO]
115                 public void AddSentinel ()
116                 {
117                         throw new NotImplementedException ();
118                 }
119                 [MonoTODO]
120                 public override bool Equals (object obj)
121                 {
122                         throw new NotImplementedException ();
123                 }
124                 [MonoTODO]
125                 public override int GetHashCode ()
126                 {
127                         throw new NotImplementedException ();
128                 }
129
130                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
131                 internal extern byte[] get_signature_local ();
132
133                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
134                 internal extern byte[] get_signature_field ();
135
136                 public byte[] GetSignature ()
137                 {
138                         switch (type) {
139                         case SignatureHelperType.HELPER_LOCAL:
140                                 return get_signature_local ();
141                         case SignatureHelperType.HELPER_FIELD:
142                                 return get_signature_field ();
143                         default:
144                                 throw new NotImplementedException ();
145                         }
146                 }
147
148                 public override string ToString() {
149                         return "SignatureHelper";
150                 }
151
152                 internal static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callConv, CallingConvention unmanagedCallConv, Type returnType,
153                                                                                                                    Type [] parameters)
154                 {
155                         if (!(mod is ModuleBuilder))
156                                 throw new NotImplementedException ();
157
158                         SignatureHelper helper = 
159                                 new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
160                         helper.returnType = returnType;
161                         helper.callConv = callConv;
162                         helper.unmanagedCallConv = unmanagedCallConv;
163
164                         if (parameters != null) {
165                                 helper.arguments = new Type [parameters.Length];
166                                 for (int i = 0; i < parameters.Length; ++i)
167                                         helper.arguments [i] = parameters [i];
168                         }
169
170                         return helper;
171                 }
172
173
174         }
175 }
176