SignatureHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. #pragma warning disable 649
  57. private Type[][] modreqs;
  58. private Type[][] modopts;
  59. #pragma warning restore 649
  60. internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
  61. {
  62. this.type = type;
  63. this.module = module;
  64. }
  65. public static SignatureHelper GetFieldSigHelper (Module mod)
  66. {
  67. if (mod != null && !(mod is ModuleBuilder))
  68. throw new ArgumentException ("ModuleBuilder is expected");
  69. return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
  70. }
  71. public static SignatureHelper GetLocalVarSigHelper (Module mod)
  72. {
  73. if (mod != null && !(mod is ModuleBuilder))
  74. throw new ArgumentException ("ModuleBuilder is expected");
  75. return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
  76. }
  77. #if NET_2_0
  78. public static SignatureHelper GetLocalVarSigHelper ()
  79. {
  80. return new SignatureHelper (null, SignatureHelperType.HELPER_LOCAL);
  81. }
  82. public static SignatureHelper GetMethodSigHelper (CallingConventions callingConvention, Type returnType)
  83. {
  84. return GetMethodSigHelper (null, callingConvention, (CallingConvention)0, returnType, null);
  85. }
  86. public static SignatureHelper GetMethodSigHelper (CallingConvention unmanagedCallingConvention, Type returnType)
  87. {
  88. return GetMethodSigHelper (null, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
  89. }
  90. #endif
  91. public static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, Type returnType)
  92. {
  93. return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
  94. }
  95. public static SignatureHelper GetMethodSigHelper (Module mod, CallingConvention unmanagedCallConv, Type returnType)
  96. {
  97. return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallConv, returnType, null);
  98. }
  99. public static SignatureHelper GetMethodSigHelper (Module mod, Type returnType, Type[] parameterTypes)
  100. {
  101. return GetMethodSigHelper (mod, CallingConventions.Standard, (CallingConvention)0, returnType, parameterTypes);
  102. }
  103. [MonoTODO("Not implemented")]
  104. public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType, Type[] parameterTypes)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. //
  109. // Grows the given array, and returns the index where the element
  110. // was added
  111. //
  112. static int AppendArray (ref Type [] array, Type t)
  113. {
  114. if (array != null) {
  115. Type[] new_a = new Type [array.Length + 1];
  116. System.Array.Copy (array, new_a, array.Length);
  117. new_a [array.Length] = t;
  118. array = new_a;
  119. return array.Length-1;
  120. } else {
  121. array = new Type [1];
  122. array [0] = t;
  123. return 0;
  124. }
  125. }
  126. #if NET_2_0
  127. //
  128. // Appends the given type array @t into the @array passed at
  129. // position @pos. If there is no array, it gets created
  130. //
  131. // This allows adding data to a null array at position 5 for
  132. // example, creating 4 empty slots before the slot where @t
  133. // is stored.
  134. //
  135. //
  136. static void AppendArrayAt (ref Type [][] array, Type [] t, int pos)
  137. {
  138. int top = Math.Max (pos, array == null ? 0 : array.Length);
  139. Type[][] new_a = new Type [top+1][];
  140. if (array != null)
  141. System.Array.Copy (array, new_a, top);
  142. new_a [pos] = t;
  143. array = new_a;
  144. }
  145. static void ValidateParameterModifiers (string name, Type [] parameter_modifiers)
  146. {
  147. foreach (Type modifier in parameter_modifiers){
  148. if (modifier == null)
  149. throw new ArgumentNullException (name);
  150. if (modifier.IsArray)
  151. throw new ArgumentException (Locale.GetText ("Array type not permitted"), name);
  152. if (modifier.ContainsGenericParameters)
  153. throw new ArgumentException (Locale.GetText ("Open Generic Type not permitted"), name);
  154. }
  155. }
  156. static void ValidateCustomModifier (int n, Type [][] custom_modifiers, string name)
  157. {
  158. if (custom_modifiers == null)
  159. return;
  160. if (custom_modifiers.Length != n)
  161. throw new ArgumentException (
  162. Locale.GetText (
  163. String.Format ("Custom modifiers length `{0}' does not match the size of the arguments")));
  164. foreach (Type [] parameter_modifiers in custom_modifiers){
  165. if (parameter_modifiers == null)
  166. continue;
  167. ValidateParameterModifiers (name, parameter_modifiers);
  168. }
  169. }
  170. static Exception MissingFeature ()
  171. {
  172. throw new NotImplementedException ("Mono does not currently support setting modOpt/modReq through SignatureHelper");
  173. }
  174. [MonoTODO("Currently we ignore requiredCustomModifiers and optionalCustomModifiers")]
  175. public void AddArguments (Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
  176. {
  177. if (arguments == null)
  178. throw new ArgumentNullException ("arguments");
  179. // For now
  180. if (requiredCustomModifiers != null || optionalCustomModifiers != null){
  181. throw MissingFeature();
  182. }
  183. ValidateCustomModifier (arguments.Length, requiredCustomModifiers, "requiredCustomModifiers");
  184. ValidateCustomModifier (arguments.Length, optionalCustomModifiers, "optionalCustomModifiers");
  185. for (int i = 0; i < arguments.Length; i++){
  186. AddArgument (arguments [i],
  187. requiredCustomModifiers != null ? requiredCustomModifiers [i] : null,
  188. optionalCustomModifiers != null ? optionalCustomModifiers [i] : null);
  189. }
  190. }
  191. [MonoTODO ("pinned is ignored")]
  192. public void AddArgument (Type argument, bool pinned)
  193. {
  194. AddArgument (argument);
  195. }
  196. public void AddArgument (Type argument, Type [] requiredCustomModifiers, Type [] optionalCustomModifiers)
  197. {
  198. if (argument == null)
  199. throw new ArgumentNullException ("argument");
  200. if (requiredCustomModifiers != null)
  201. ValidateParameterModifiers ("requiredCustomModifiers", requiredCustomModifiers);
  202. if (optionalCustomModifiers != null)
  203. ValidateParameterModifiers ("optionalCustomModifiers", optionalCustomModifiers);
  204. int p = AppendArray (ref arguments, argument);
  205. if (requiredCustomModifiers != null)
  206. AppendArrayAt (ref modreqs, requiredCustomModifiers, p);
  207. if (optionalCustomModifiers != null)
  208. AppendArrayAt (ref modopts, optionalCustomModifiers, p);
  209. }
  210. [MonoTODO("Not implemented")]
  211. public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType,
  212. Type [] requiredReturnTypeCustomModifiers,
  213. Type [] optionalReturnTypeCustomModifiers,
  214. Type [] parameterTypes,
  215. Type [] [] requiredParameterTypeCustomModifiers,
  216. Type [] [] optionalParameterTypeCustomModifiers)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. #endif
  221. public void AddArgument (Type clsArgument)
  222. {
  223. if (clsArgument == null)
  224. throw new ArgumentNullException ("clsArgument");
  225. AppendArray (ref arguments, clsArgument);
  226. }
  227. [MonoTODO("Not implemented")]
  228. public void AddSentinel ()
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. static bool CompareOK (Type [][] one, Type [][] two)
  233. {
  234. if (one == null){
  235. if (two == null)
  236. return true;
  237. return false;
  238. } else if (two == null)
  239. return false;
  240. if (one.Length != two.Length)
  241. return false;
  242. for (int i = 0; i < one.Length; i++){
  243. Type [] tone = one [i];
  244. Type [] ttwo = two [i];
  245. if (tone == null){
  246. if (ttwo == null)
  247. continue;
  248. } else if (ttwo == null)
  249. return false;
  250. if (tone.Length != ttwo.Length)
  251. return false;
  252. for (int j = 0; j < tone.Length; j++){
  253. Type uone = tone [j];
  254. Type utwo = ttwo [j];
  255. if (uone == null){
  256. if (utwo == null)
  257. continue;
  258. return false;
  259. } else if (utwo == null)
  260. return false;
  261. if (!uone.Equals (utwo))
  262. return false;
  263. }
  264. }
  265. return true;
  266. }
  267. public override bool Equals (object obj)
  268. {
  269. SignatureHelper other = obj as SignatureHelper;
  270. if (other == null)
  271. return false;
  272. if (other.module != module ||
  273. other.returnType != returnType ||
  274. other.callConv != callConv ||
  275. other.unmanagedCallConv != unmanagedCallConv)
  276. return false;
  277. if (arguments != null){
  278. if (other.arguments == null)
  279. return false;
  280. if (arguments.Length != other.arguments.Length)
  281. return false;
  282. for (int i = 0; i < arguments.Length; i++)
  283. if (!other.arguments [i].Equals (arguments [i]))
  284. return false;
  285. } else if (other.arguments != null)
  286. return false;
  287. return CompareOK (other.modreqs, modreqs) && CompareOK (other.modopts, modopts);
  288. }
  289. public override int GetHashCode ()
  290. {
  291. // Lame, but easy, and will work, and chances are
  292. // you will only need a few of these.
  293. return 0;
  294. }
  295. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  296. internal extern byte[] get_signature_local ();
  297. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  298. internal extern byte[] get_signature_field ();
  299. public byte[] GetSignature ()
  300. {
  301. switch (type) {
  302. case SignatureHelperType.HELPER_LOCAL:
  303. return get_signature_local ();
  304. case SignatureHelperType.HELPER_FIELD:
  305. return get_signature_field ();
  306. default:
  307. throw new NotImplementedException ();
  308. }
  309. }
  310. public override string ToString() {
  311. return "SignatureHelper";
  312. }
  313. internal static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, CallingConvention unmanagedCallingConvention, Type returnType,
  314. Type [] parameters)
  315. {
  316. if (mod != null && !(mod is ModuleBuilder))
  317. throw new ArgumentException ("ModuleBuilder is expected");
  318. if (returnType == null)
  319. returnType = typeof (void);
  320. if (returnType.IsUserType)
  321. throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
  322. if (parameters != null) {
  323. for (int i = 0; i < parameters.Length; ++i)
  324. if (parameters [i].IsUserType)
  325. throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
  326. }
  327. SignatureHelper helper =
  328. new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
  329. helper.returnType = returnType;
  330. helper.callConv = callingConvention;
  331. helper.unmanagedCallConv = unmanagedCallingConvention;
  332. if (parameters != null) {
  333. helper.arguments = new Type [parameters.Length];
  334. for (int i = 0; i < parameters.Length; ++i)
  335. helper.arguments [i] = parameters [i];
  336. }
  337. return helper;
  338. }
  339. void _SignatureHelper.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  340. {
  341. throw new NotImplementedException ();
  342. }
  343. void _SignatureHelper.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  344. {
  345. throw new NotImplementedException ();
  346. }
  347. void _SignatureHelper.GetTypeInfoCount (out uint pcTInfo)
  348. {
  349. throw new NotImplementedException ();
  350. }
  351. void _SignatureHelper.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  352. {
  353. throw new NotImplementedException ();
  354. }
  355. }
  356. }