SignatureHelper.cs 13 KB

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