Ooops.
[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 #if NET_2_0
43         [ComVisible (true)]
44         [ClassInterfaceAttribute (ClassInterfaceType.None)]
45         [ComDefaultInterfaceAttribute (typeof (_SignatureHelper))]
46 #endif
47         public sealed class SignatureHelper {
48                 internal enum SignatureHelperType {
49                         HELPER_FIELD,
50                         HELPER_LOCAL,
51                         HELPER_METHOD,
52                         HELPER_PROPERTY
53                 }
54
55                 private ModuleBuilder module;
56                 private Type[] arguments;
57                 private SignatureHelperType type;
58                 private Type returnType;
59                 private CallingConventions callConv;
60                 private CallingConvention unmanagedCallConv;
61
62                 internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
63                 {
64                         this.type = type;
65                         this.module = module;
66                 }
67
68                 public static SignatureHelper GetFieldSigHelper (Module mod)
69                 {
70                         if (!(mod is ModuleBuilder))
71                                 throw new NotImplementedException ();
72
73                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
74                 }
75
76                 public static SignatureHelper GetLocalVarSigHelper (Module mod)
77                 {
78                         if (!(mod is ModuleBuilder))
79                                 throw new NotImplementedException ();
80
81                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
82                 }
83
84                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callingConvention, Type returnType)
85                 {
86                         return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
87                 }
88
89                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConvention unmanagedCallingConvention, Type returnType)
90                 {
91                         return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
92                 }
93
94                 public static SignatureHelper GetMethodSigHelper( Module mod, Type returnType, Type[] parameterTypes)
95                 {
96                         return GetMethodSigHelper (mod, CallingConventions.Standard, 
97                                                                            (CallingConvention)0, returnType, 
98                                                                            parameterTypes);
99                 }
100                 [MonoTODO]
101                 public static SignatureHelper GetPropertySigHelper( Module mod, Type returnType, Type[] parameterTypes)
102                 {
103                         throw new NotImplementedException ();
104                 }
105
106                 public void AddArgument (Type clsArgument)
107                 {
108                         if (arguments != null) {
109                                 Type[] new_a = new Type [arguments.Length + 1];
110                                 System.Array.Copy (arguments, new_a, arguments.Length);
111                                 new_a [arguments.Length] = clsArgument;
112                                 arguments = new_a;
113                         } else {
114                                 arguments = new Type [1];
115                                 arguments [0] = clsArgument;
116                         }
117                 }
118                 [MonoTODO]
119                 public void AddSentinel ()
120                 {
121                         throw new NotImplementedException ();
122                 }
123                 [MonoTODO]
124                 public override bool Equals (object obj)
125                 {
126                         throw new NotImplementedException ();
127                 }
128                 [MonoTODO]
129                 public override int GetHashCode ()
130                 {
131                         throw new NotImplementedException ();
132                 }
133
134                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
135                 internal extern byte[] get_signature_local ();
136
137                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
138                 internal extern byte[] get_signature_field ();
139
140                 public byte[] GetSignature ()
141                 {
142                         switch (type) {
143                         case SignatureHelperType.HELPER_LOCAL:
144                                 return get_signature_local ();
145                         case SignatureHelperType.HELPER_FIELD:
146                                 return get_signature_field ();
147                         default:
148                                 throw new NotImplementedException ();
149                         }
150                 }
151
152                 public override string ToString() {
153                         return "SignatureHelper";
154                 }
155
156                 internal static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callConv, CallingConvention unmanagedCallConv, Type returnType,
157                                                                                                                    Type [] parameters)
158                 {
159                         if (!(mod is ModuleBuilder))
160                                 throw new NotImplementedException ();
161
162                         SignatureHelper helper = 
163                                 new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
164                         helper.returnType = returnType;
165                         helper.callConv = callConv;
166                         helper.unmanagedCallConv = unmanagedCallConv;
167
168                         if (parameters != null) {
169                                 helper.arguments = new Type [parameters.Length];
170                                 for (int i = 0; i < parameters.Length; ++i)
171                                         helper.arguments [i] = parameters [i];
172                         }
173
174                         return helper;
175                 }
176
177
178         }
179 }
180