SignatureHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. [ComVisible (true)]
  39. [ComDefaultInterface (typeof (_SignatureHelper))]
  40. [ClassInterface (ClassInterfaceType.None)]
  41. public sealed class SignatureHelper : _SignatureHelper {
  42. internal enum SignatureHelperType {
  43. HELPER_FIELD,
  44. HELPER_LOCAL,
  45. HELPER_METHOD,
  46. HELPER_PROPERTY
  47. }
  48. private ModuleBuilder module; // can be null in 2.0
  49. private Type[] arguments;
  50. private SignatureHelperType type;
  51. private Type returnType;
  52. private CallingConventions callConv;
  53. private CallingConvention unmanagedCallConv;
  54. #pragma warning disable 649
  55. private Type[][] modreqs;
  56. private Type[][] modopts;
  57. #pragma warning restore 649
  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. public static SignatureHelper GetLocalVarSigHelper ()
  76. {
  77. return new SignatureHelper (null, SignatureHelperType.HELPER_LOCAL);
  78. }
  79. public static SignatureHelper GetMethodSigHelper (CallingConventions callingConvention, Type returnType)
  80. {
  81. return GetMethodSigHelper (null, callingConvention, (CallingConvention)0, returnType, null);
  82. }
  83. public static SignatureHelper GetMethodSigHelper (CallingConvention unmanagedCallingConvention, Type returnType)
  84. {
  85. return GetMethodSigHelper (null, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
  86. }
  87. public static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, Type returnType)
  88. {
  89. return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
  90. }
  91. public static SignatureHelper GetMethodSigHelper (Module mod, CallingConvention unmanagedCallConv, Type returnType)
  92. {
  93. return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallConv, returnType, null);
  94. }
  95. public static SignatureHelper GetMethodSigHelper (Module mod, Type returnType, Type[] parameterTypes)
  96. {
  97. return GetMethodSigHelper (mod, CallingConventions.Standard, (CallingConvention)0, returnType, parameterTypes);
  98. }
  99. [MonoTODO("Not implemented")]
  100. public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType, Type[] parameterTypes)
  101. {
  102. throw new NotImplementedException ();
  103. }
  104. [MonoTODO("Not implemented")]
  105. public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType,
  106. Type [] requiredReturnTypeCustomModifiers,
  107. Type [] optionalReturnTypeCustomModifiers,
  108. Type [] parameterTypes,
  109. Type [] [] requiredParameterTypeCustomModifiers,
  110. Type [] [] optionalParameterTypeCustomModifiers)
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. #if NET_4_0
  115. [MonoTODO("Not implemented")]
  116. public static SignatureHelper GetPropertySigHelper (Module mod,
  117. CallingConventions callingConvention,
  118. Type returnType,
  119. Type [] requiredReturnTypeCustomModifiers,
  120. Type [] optionalReturnTypeCustomModifiers,
  121. Type [] parameterTypes,
  122. Type [] [] requiredParameterTypeCustomModifiers,
  123. Type [] [] optionalParameterTypeCustomModifiers)
  124. {
  125. throw new NotImplementedException ();
  126. }
  127. #endif
  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. switch (type) {
  310. case SignatureHelperType.HELPER_LOCAL:
  311. return get_signature_local ();
  312. case SignatureHelperType.HELPER_FIELD:
  313. return get_signature_field ();
  314. default:
  315. throw new NotImplementedException ();
  316. }
  317. }
  318. public override string ToString() {
  319. return "SignatureHelper";
  320. }
  321. internal static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, CallingConvention unmanagedCallingConvention, Type returnType,
  322. Type [] parameters)
  323. {
  324. if (mod != null && !(mod is ModuleBuilder))
  325. throw new ArgumentException ("ModuleBuilder is expected");
  326. if (returnType == null)
  327. returnType = typeof (void);
  328. if (returnType.IsUserType)
  329. throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
  330. if (parameters != null) {
  331. for (int i = 0; i < parameters.Length; ++i)
  332. if (parameters [i].IsUserType)
  333. throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
  334. }
  335. SignatureHelper helper =
  336. new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
  337. helper.returnType = returnType;
  338. helper.callConv = callingConvention;
  339. helper.unmanagedCallConv = unmanagedCallingConvention;
  340. if (parameters != null) {
  341. helper.arguments = new Type [parameters.Length];
  342. for (int i = 0; i < parameters.Length; ++i)
  343. helper.arguments [i] = parameters [i];
  344. }
  345. return helper;
  346. }
  347. void _SignatureHelper.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  348. {
  349. throw new NotImplementedException ();
  350. }
  351. void _SignatureHelper.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  352. {
  353. throw new NotImplementedException ();
  354. }
  355. void _SignatureHelper.GetTypeInfoCount (out uint pcTInfo)
  356. {
  357. throw new NotImplementedException ();
  358. }
  359. void _SignatureHelper.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  360. {
  361. throw new NotImplementedException ();
  362. }
  363. }
  364. }