SignatureHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. // System.Reflection.Emit/SignatureHelper.cs
  25. //
  26. // Author:
  27. // Paolo Molaro ([email protected])
  28. //
  29. // (C) 2001 Ximian, Inc. http://www.ximian.com
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. namespace System.Reflection.Emit {
  38. #if NET_2_0
  39. [ComVisible (true)]
  40. [ComDefaultInterface (typeof (_SignatureHelper))]
  41. #endif
  42. [ClassInterface (ClassInterfaceType.None)]
  43. public sealed class SignatureHelper : _SignatureHelper {
  44. internal enum SignatureHelperType {
  45. HELPER_FIELD,
  46. HELPER_LOCAL,
  47. HELPER_METHOD,
  48. HELPER_PROPERTY
  49. }
  50. private ModuleBuilder module; // can be null in 2.0
  51. private Type[] arguments;
  52. private SignatureHelperType type;
  53. private Type returnType;
  54. private CallingConventions callConv;
  55. private CallingConvention unmanagedCallConv;
  56. private Type[][] modreqs;
  57. private Type[][] modopts;
  58. internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
  59. {
  60. this.type = type;
  61. this.module = module;
  62. }
  63. public static SignatureHelper GetFieldSigHelper (Module mod)
  64. {
  65. if (mod != null && !(mod is ModuleBuilder))
  66. throw new ArgumentException ("ModuleBuilder is expected");
  67. return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
  68. }
  69. public static SignatureHelper GetLocalVarSigHelper (Module mod)
  70. {
  71. if (mod != null && !(mod is ModuleBuilder))
  72. throw new ArgumentException ("ModuleBuilder is expected");
  73. return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
  74. }
  75. #if NET_2_0
  76. public static SignatureHelper GetLocalVarSigHelper ()
  77. {
  78. return new SignatureHelper (null, SignatureHelperType.HELPER_LOCAL);
  79. }
  80. public static SignatureHelper GetMethodSigHelper(CallingConventions callingConvention, Type returnType)
  81. {
  82. return GetMethodSigHelper (null, callingConvention, (CallingConvention)0, returnType, null);
  83. }
  84. public static SignatureHelper GetMethodSigHelper (CallingConvention unmanagedCallingConvention,
  85. Type returnType)
  86. {
  87. return GetMethodSigHelper (null, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
  88. }
  89. #endif
  90. public static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callingConvention, Type returnType)
  91. {
  92. return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
  93. }
  94. public static SignatureHelper GetMethodSigHelper( Module mod, CallingConvention unmanagedCallingConvention, Type returnType)
  95. {
  96. return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
  97. }
  98. public static SignatureHelper GetMethodSigHelper( Module mod, Type returnType, Type[] parameterTypes)
  99. {
  100. return GetMethodSigHelper (mod, CallingConventions.Standard,
  101. (CallingConvention)0, returnType,
  102. parameterTypes);
  103. }
  104. [MonoTODO("Not implemented")]
  105. public static SignatureHelper GetPropertySigHelper( Module mod, Type returnType, Type[] parameterTypes)
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. //
  110. // Grows the given array, and returns the index where the element
  111. // was added
  112. //
  113. static int AppendArray (ref Type [] array, Type t)
  114. {
  115. if (array != null) {
  116. Type[] new_a = new Type [array.Length + 1];
  117. System.Array.Copy (array, new_a, array.Length);
  118. new_a [array.Length] = t;
  119. array = new_a;
  120. return array.Length-1;
  121. } else {
  122. array = new Type [1];
  123. array [0] = t;
  124. return 0;
  125. }
  126. }
  127. #if NET_2_0
  128. //
  129. // Appends the given type array @t into the @array passed at
  130. // position @pos. If there is no array, it gets created
  131. //
  132. // This allows adding data to a null array at position 5 for
  133. // example, creating 4 empty slots before the slot where @t
  134. // is stored.
  135. //
  136. //
  137. static void AppendArrayAt (ref Type [][] array, Type [] t, int pos)
  138. {
  139. int top = Math.Max (pos, array == null ? 0 : array.Length);
  140. Type[][] new_a = new Type [top+1][];
  141. if (array != null)
  142. System.Array.Copy (array, new_a, top);
  143. new_a [pos] = t;
  144. array = new_a;
  145. }
  146. static void ValidateParameterModifiers(string name, Type [] parameter_modifiers)
  147. {
  148. foreach (Type modifier in parameter_modifiers){
  149. if (modifier == null)
  150. throw new ArgumentNullException (name);
  151. if (modifier.IsArray)
  152. throw new ArgumentException (Locale.GetText ("Array type not permitted"), name);
  153. if (modifier.ContainsGenericParameters)
  154. throw new ArgumentException (Locale.GetText ("Open Generic Type not permitted"), name);
  155. }
  156. }
  157. static void ValidateCustomModifier (int n, Type [][] custom_modifiers, string name)
  158. {
  159. if (custom_modifiers == null)
  160. return;
  161. if (custom_modifiers.Length != n)
  162. throw new ArgumentException (
  163. Locale.GetText (
  164. String.Format ("Custom modifiers length `{0}' does not match the size of the arguments")));
  165. foreach (Type [] parameter_modifiers in custom_modifiers){
  166. if (parameter_modifiers == null)
  167. continue;
  168. ValidateParameterModifiers (name, parameter_modifiers);
  169. }
  170. }
  171. static Exception MissingFeature ()
  172. {
  173. throw new NotImplementedException ("Mono does not currently support setting modOpt/modReq through SignatureHelper");
  174. }
  175. [MonoTODO("Currently we ignore requiredCustomModifiers and optionalCustomModifiers")]
  176. public void AddArguments (Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
  177. {
  178. if (arguments == null)
  179. throw new ArgumentNullException ("arguments");
  180. // For now
  181. if (requiredCustomModifiers != null || optionalCustomModifiers != null){
  182. throw MissingFeature();
  183. }
  184. ValidateCustomModifier (arguments.Length, requiredCustomModifiers, "requiredCustomModifiers");
  185. ValidateCustomModifier (arguments.Length, optionalCustomModifiers, "optionalCustomModifiers");
  186. for (int i = 0; i < arguments.Length; i++){
  187. AddArgument (arguments [i],
  188. requiredCustomModifiers != null ? requiredCustomModifiers [i] : null,
  189. optionalCustomModifiers != null ? optionalCustomModifiers [i] : null);
  190. }
  191. }
  192. [MonoTODO ("pinned is ignored")]
  193. public void AddArgument (Type argument, bool pinned)
  194. {
  195. AddArgument (argument);
  196. }
  197. public void AddArgument (Type argument, Type [] requiredCustomModifiers, Type [] optionalCustomModifiers)
  198. {
  199. if (argument == null)
  200. throw new ArgumentNullException ("argument");
  201. if (requiredCustomModifiers != null)
  202. ValidateParameterModifiers ("requiredCustomModifiers", requiredCustomModifiers);
  203. if (optionalCustomModifiers != null)
  204. ValidateParameterModifiers ("optionalCustomModifiers", optionalCustomModifiers);
  205. int p = AppendArray (ref arguments, argument);
  206. if (requiredCustomModifiers != null)
  207. AppendArrayAt (ref modreqs, requiredCustomModifiers, p);
  208. if (optionalCustomModifiers != null)
  209. AppendArrayAt (ref modopts, optionalCustomModifiers, p);
  210. }
  211. [MonoTODO("Not implemented")]
  212. public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType,
  213. Type [] requiredReturnTypeCustomModifiers,
  214. Type [] optionalReturnTypeCustomModifiers,
  215. Type [] parameterTypes,
  216. Type [] [] requiredParameterTypeCustomModifiers,
  217. Type [] [] optionalParameterTypeCustomModifiers)
  218. {
  219. throw new NotImplementedException ();
  220. }
  221. #endif
  222. public void AddArgument (Type clsArgument)
  223. {
  224. if (clsArgument == null)
  225. throw new ArgumentNullException ("argument");
  226. AppendArray (ref arguments, clsArgument);
  227. }
  228. [MonoTODO("Not implemented")]
  229. public void AddSentinel ()
  230. {
  231. throw new NotImplementedException ();
  232. }
  233. static bool CompareOK (Type [][] one, Type [][] two)
  234. {
  235. if (one == null){
  236. if (two == null)
  237. return true;
  238. return false;
  239. } else if (two == null)
  240. return false;
  241. if (one.Length != two.Length)
  242. return false;
  243. for (int i = 0; i < one.Length; i++){
  244. Type [] tone = one [i];
  245. Type [] ttwo = two [i];
  246. if (tone == null){
  247. if (ttwo == null)
  248. continue;
  249. } else if (ttwo == null)
  250. return false;
  251. if (tone.Length != ttwo.Length)
  252. return false;
  253. for (int j = 0; j < tone.Length; j++){
  254. Type uone = tone [j];
  255. Type utwo = ttwo [j];
  256. if (uone == null){
  257. if (utwo == null)
  258. continue;
  259. return false;
  260. } else if (utwo == null)
  261. return false;
  262. if (!uone.Equals (utwo))
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. public override bool Equals (object obj)
  269. {
  270. SignatureHelper other = obj as SignatureHelper;
  271. if (other == null)
  272. return false;
  273. if (other.module != module ||
  274. other.returnType != returnType ||
  275. other.callConv != callConv ||
  276. other.unmanagedCallConv != unmanagedCallConv)
  277. return false;
  278. if (arguments != null){
  279. if (other.arguments == null)
  280. return false;
  281. if (arguments.Length != other.arguments.Length)
  282. return false;
  283. for (int i = 0; i < arguments.Length; i++)
  284. if (!other.arguments [i].Equals (arguments [i]))
  285. return false;
  286. } else if (other.arguments != null)
  287. return false;
  288. return CompareOK (other.modreqs, modreqs) && CompareOK (other.modopts, modopts);
  289. }
  290. public override int GetHashCode ()
  291. {
  292. // Lame, but easy, and will work, and chances are
  293. // you will only need a few of these.
  294. return 0;
  295. }
  296. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  297. internal extern byte[] get_signature_local ();
  298. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  299. internal extern byte[] get_signature_field ();
  300. public byte[] GetSignature ()
  301. {
  302. switch (type) {
  303. case SignatureHelperType.HELPER_LOCAL:
  304. return get_signature_local ();
  305. case SignatureHelperType.HELPER_FIELD:
  306. return get_signature_field ();
  307. default:
  308. throw new NotImplementedException ();
  309. }
  310. }
  311. public override string ToString() {
  312. return "SignatureHelper";
  313. }
  314. internal static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callConv, CallingConvention unmanagedCallConv, Type returnType,
  315. Type [] parameters)
  316. {
  317. if (mod != null && !(mod is ModuleBuilder))
  318. throw new ArgumentException ("ModuleBuilder is expected");
  319. SignatureHelper helper =
  320. new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
  321. helper.returnType = returnType;
  322. helper.callConv = callConv;
  323. helper.unmanagedCallConv = unmanagedCallConv;
  324. if (parameters != null) {
  325. helper.arguments = new Type [parameters.Length];
  326. for (int i = 0; i < parameters.Length; ++i)
  327. helper.arguments [i] = parameters [i];
  328. }
  329. return helper;
  330. }
  331. void _SignatureHelper.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  332. {
  333. throw new NotImplementedException ();
  334. }
  335. void _SignatureHelper.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  336. {
  337. throw new NotImplementedException ();
  338. }
  339. void _SignatureHelper.GetTypeInfoCount (out uint pcTInfo)
  340. {
  341. throw new NotImplementedException ();
  342. }
  343. void _SignatureHelper.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  344. {
  345. throw new NotImplementedException ();
  346. }
  347. }
  348. }