MDMethodElems.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * PERWAPI - An API for Reading and Writing PE Files
  3. *
  4. * Copyright (c) Diane Corney, Queensland University of Technology, 2004.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the PERWAPI Copyright as included with this
  8. * distribution in the file PERWAPIcopyright.rtf.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY as is explained in the copyright notice.
  12. *
  13. * The author may be contacted at d.corney@qut.edu.au
  14. *
  15. * Version Date: 26/01/07
  16. */
  17. using System;
  18. using System.IO;
  19. using System.Collections;
  20. using System.Security.Cryptography;
  21. namespace QUT.PERWAPI
  22. {
  23. /**************************************************************************/
  24. /// <summary>
  25. /// Base class for Method Descriptors
  26. /// </summary>
  27. public abstract class Method : Member
  28. {
  29. protected MethSig sig;
  30. protected ArrayList genericParams;
  31. /*-------------------- Constructors ---------------------------------*/
  32. internal Method(string methName, Type rType, Class paren)
  33. : base(methName, paren)
  34. {
  35. sig = new MethSig(methName);
  36. sig.retType = rType;
  37. }
  38. internal Method(string name) : base(name) { }
  39. /// <summary>
  40. /// Add calling conventions to this method descriptor
  41. /// </summary>
  42. /// <param name="cconv"></param>
  43. public void AddCallConv(CallConv cconv)
  44. {
  45. sig.callConv |= cconv;
  46. }
  47. /// <summary>
  48. /// Get the calling conventions for this method
  49. /// </summary>
  50. /// <returns></returns>
  51. public CallConv GetCallConv()
  52. {
  53. return sig.callConv;
  54. }
  55. /// <summary>
  56. /// Set the return type
  57. /// </summary>
  58. /// <param name="retT">type returned</param>
  59. internal void AddRetType(Type retT)
  60. {
  61. System.Diagnostics.Debug.Assert(retT != null);
  62. sig.retType = retT;
  63. }
  64. /// <summary>
  65. /// Get the method return type
  66. /// </summary>
  67. /// <returns>method return type</returns>
  68. public Type GetRetType()
  69. {
  70. return sig.retType;
  71. }
  72. /// <summary>
  73. /// Get the types of the method parameters
  74. /// </summary>
  75. /// <returns>list of parameter types</returns>
  76. public Type[] GetParTypes()
  77. {
  78. return sig.parTypes;
  79. }
  80. /// <summary>
  81. /// Get the optional parameter types (for varargs)
  82. /// </summary>
  83. /// <returns>list of vararg types</returns>
  84. public Type[] GetOptParTypes()
  85. {
  86. return sig.optParTypes;
  87. }
  88. public int GetGenericParamCount()
  89. {
  90. return genericParams == null ? 0 : genericParams.Count;
  91. }
  92. /// <summary>
  93. /// Add a generic type to this method
  94. /// </summary>
  95. /// <param name="name">the name of the generic type</param>
  96. /// <returns>the descriptor for the generic type</returns>
  97. public GenericParam AddGenericParam(string name)
  98. {
  99. if (genericParams == null) genericParams = new ArrayList();
  100. GenericParam gp = new GenericParam(name, this, genericParams.Count);
  101. sig.callConv |= CallConv.Generic;
  102. genericParams.Add(gp);
  103. sig.numGenPars = (uint)genericParams.Count;
  104. return gp;
  105. }
  106. /// <summary>
  107. /// Get the descriptor for a generic type
  108. /// </summary>
  109. /// <param name="name">the name of the generic type</param>
  110. /// <returns>descriptor for generic type "name"</returns>
  111. public GenericParam GetGenericParam(string name)
  112. {
  113. int pos = FindGenericParam(name);
  114. if (pos == -1) return null;
  115. return (GenericParam)genericParams[pos];
  116. }
  117. public GenericParam GetGenericParam(int ix)
  118. {
  119. if ((genericParams == null) || (ix >= genericParams.Count)) return null;
  120. return (GenericParam)genericParams[ix];
  121. }
  122. public void RemoveGenericParam(string name)
  123. {
  124. int pos = FindGenericParam(name);
  125. if (pos == -1) return;
  126. DeleteGenericParam(pos);
  127. }
  128. public void RemoveGenericParam(int ix)
  129. {
  130. if (genericParams == null) return;
  131. if (ix >= genericParams.Count) return;
  132. DeleteGenericParam(ix);
  133. }
  134. public MethodSpec Instantiate(Type[] genTypes)
  135. {
  136. if (genTypes == null) return null;
  137. if ((genericParams == null) || (genericParams.Count == 0))
  138. throw new Exception("Cannot instantiate non-generic method");
  139. if (genTypes.Length != genericParams.Count)
  140. throw new Exception("Wrong number of type parameters for instantiation\nNeeded "
  141. + genericParams.Count + " but got " + genTypes.Length);
  142. return new MethodSpec(this, genTypes);
  143. }
  144. public GenericParam[] GetGenericParams()
  145. { // KJG June 2005
  146. if (genericParams == null) return null;
  147. return (GenericParam[])genericParams.ToArray(typeof(GenericParam));
  148. }
  149. /*------------------------- internal functions --------------------------*/
  150. internal abstract void TypeSig(MemoryStream sig);
  151. internal bool HasNameAndSig(string name, Type[] sigTypes)
  152. {
  153. if (this.name != name) return false;
  154. return sig.HasSig(sigTypes);
  155. }
  156. internal bool HasNameAndSig(string name, Type[] sigTypes, Type[] optPars)
  157. {
  158. if (this.name != name) return false;
  159. return sig.HasSig(sigTypes, optPars);
  160. }
  161. internal MethSig GetSig() { return sig; }
  162. internal void SetSig(MethSig sig)
  163. {
  164. this.sig = sig;
  165. this.sig.name = name;
  166. }
  167. internal override string NameString()
  168. {
  169. return parent.NameString() + sig.NameString();
  170. }
  171. private int FindGenericParam(string name)
  172. {
  173. if (genericParams == null) return -1;
  174. for (int i = 0; i < genericParams.Count; i++)
  175. {
  176. GenericParam gp = (GenericParam)genericParams[i];
  177. if (gp.GetName() == name) return i;
  178. }
  179. return -1;
  180. }
  181. private void DeleteGenericParam(int pos)
  182. {
  183. genericParams.RemoveAt(pos);
  184. for (int i = pos; i < genericParams.Count; i++)
  185. {
  186. GenericParam gp = (GenericParam)genericParams[i];
  187. gp.Index = (uint)i;
  188. }
  189. }
  190. internal void AddGenericParam(GenericParam par)
  191. {
  192. if (genericParams == null) genericParams = new ArrayList();
  193. genericParams.Add(par);
  194. //sig.callConv |= CallConv.Generic;
  195. //sig.numGenPars = (uint)genericParams.Count;
  196. }
  197. internal ArrayList GenericParams
  198. {
  199. get { return genericParams; }
  200. set { genericParams = value; }
  201. }
  202. internal void SetGenericParams(GenericParam[] pars)
  203. {
  204. genericParams = new ArrayList(pars);
  205. sig.callConv |= CallConv.Generic;
  206. sig.numGenPars = (uint)genericParams.Count;
  207. }
  208. internal override void WriteType(CILWriter output)
  209. {
  210. sig.WriteCallConv(output);
  211. sig.retType.WriteType(output);
  212. output.Write(" ");
  213. parent.WriteName(output);
  214. output.Write("::" + name);
  215. sig.WriteParTypes(output);
  216. }
  217. }
  218. /**************************************************************************/
  219. /// <summary>
  220. /// Descriptor for an Instantiation of a generic method
  221. /// </summary>
  222. public class MethodSpec : Method
  223. {
  224. Method methParent;
  225. uint instIx;
  226. Type[] instTypes;
  227. internal static byte GENERICINST = 0x0A;
  228. /*-------------------- Constructors ---------------------------------*/
  229. public MethodSpec(Method mParent, Type[] instTypes)
  230. : base(null)
  231. {
  232. this.methParent = mParent;
  233. this.instTypes = instTypes;
  234. tabIx = MDTable.MethodSpec;
  235. }
  236. internal MethodSpec(PEReader buff)
  237. : base(null)
  238. {
  239. parentIx = buff.GetCodedIndex(CIx.MethodDefOrRef);
  240. instIx = buff.GetBlobIx();
  241. tabIx = MDTable.MethodSpec;
  242. this.unresolved = true;
  243. }
  244. internal static void Read(PEReader buff, TableRow[] specs)
  245. {
  246. for (int i = 0; i < specs.Length; i++)
  247. specs[i] = new MethodSpec(buff);
  248. }
  249. internal override void Resolve(PEReader buff)
  250. {
  251. methParent = (Method)buff.GetCodedElement(CIx.MethodDefOrRef, parentIx);
  252. buff.currentMethodScope = methParent; // set scopes - Fix by CK
  253. buff.currentClassScope = (Class)methParent.GetParent();
  254. instTypes = buff.ReadMethSpecSig(instIx);
  255. this.unresolved = false;
  256. buff.currentMethodScope = null;
  257. buff.currentClassScope = null;
  258. }
  259. internal override void TypeSig(MemoryStream str)
  260. {
  261. str.WriteByte(GENERICINST);
  262. MetaDataOut.CompressNum(BlobUtil.CompressUInt((uint)instTypes.Length), str);
  263. for (int i = 0; i < instTypes.Length; i++)
  264. {
  265. instTypes[i].TypeSig(str);
  266. }
  267. }
  268. internal static uint Size(MetaData md)
  269. {
  270. return md.CodedIndexSize(CIx.MethodDefOrRef) + md.BlobIndexSize();
  271. }
  272. internal override void BuildTables(MetaDataOut md)
  273. {
  274. md.AddToTable(MDTable.MethodSpec, this);
  275. if (!(methParent is MethodDef)) // Never build a method def
  276. methParent.BuildMDTables(md);
  277. for (int i = 0; i < instTypes.Length; i++)
  278. {
  279. instTypes[i].BuildMDTables(md);
  280. }
  281. }
  282. internal override void BuildSignatures(MetaDataOut md)
  283. {
  284. MemoryStream outSig = new MemoryStream();
  285. TypeSig(outSig);
  286. instIx = md.AddToBlobHeap(outSig.ToArray());
  287. }
  288. internal override void Write(PEWriter output)
  289. {
  290. output.WriteCodedIndex(CIx.MethodDefOrRef, methParent);
  291. output.BlobIndex(instIx);
  292. }
  293. /*-------------------- Public Methods ------------------------------*/
  294. public Type[] GetGenericParamTypes()
  295. { // KJG 15 July 2005
  296. return instTypes;
  297. }
  298. public Method GetMethParent()
  299. { // KJG 15 July 2005
  300. return methParent;
  301. }
  302. }
  303. /**************************************************************************/
  304. /// <summary>
  305. /// Descriptor for a method defined in another assembly/module
  306. /// </summary>
  307. public class MethodRef : Method
  308. {
  309. internal MethodDef defOf;
  310. MethodDef varArgParent = null;
  311. /*-------------------- Constructors ---------------------------------*/
  312. internal MethodRef(Class paren, string name, Type retType, Type[] pars)
  313. : base(name, retType, paren)
  314. {
  315. sig.parTypes = pars;
  316. if (pars != null) sig.numPars = (uint)pars.Length;
  317. }
  318. internal MethodRef(uint parIx, string name, uint sigIx)
  319. : base(name)
  320. {
  321. this.parentIx = parIx;
  322. this.sigIx = sigIx;
  323. }
  324. internal MethodRef(MethSig sig)
  325. : base(sig.name)
  326. {
  327. this.sig = sig;
  328. }
  329. internal override void Resolve(PEReader buff)
  330. {
  331. if (sig == null)
  332. {
  333. buff.currentMethodScope = this;
  334. buff.currentClassScope = parent;
  335. sig = buff.ReadMethSig(this, name, sigIx);
  336. buff.currentMethodScope = null;
  337. buff.currentClassScope = null;
  338. }
  339. }
  340. internal override Member ResolveParent(PEReader buff)
  341. {
  342. if (parent != null) return this;
  343. buff.currentMethodScope = this;
  344. MetaDataElement paren = buff.GetCodedElement(CIx.MemberRefParent, parentIx);
  345. buff.currentMethodScope = null;
  346. if (paren is MethodDef)
  347. {
  348. parent = null;
  349. varArgParent = (MethodDef)paren;
  350. //this.sig = buff.ReadMethSig(this,name,sigIx);
  351. ((MethodDef)paren).AddVarArgSig(this);
  352. return this;
  353. }
  354. else if (paren is ClassSpec)
  355. {
  356. ((ClassSpec)paren).AddMethod(this);
  357. return this;
  358. }
  359. else if (paren is PrimitiveType)
  360. {
  361. paren = MSCorLib.mscorlib.GetDefaultClass();
  362. }
  363. else if (paren is ClassDef)
  364. {
  365. this.sig = buff.ReadMethSig(this, name, sigIx);
  366. return ((ClassDef)paren).GetMethod(this.sig);
  367. }
  368. else if (paren is TypeSpec)
  369. {
  370. paren = new ConstructedTypeSpec((TypeSpec)paren);
  371. //Console.WriteLine("Got TypeSpec as parent of Member");
  372. //return this;
  373. //throw new Exception("Got TypeSpec as parent of Member");
  374. //((TypeSpec)paren).AddMethod(buff,this);
  375. }
  376. if (paren is ReferenceScope)
  377. parent = ((ReferenceScope)paren).GetDefaultClass();
  378. parent = (Class)paren;
  379. //if ((MethodRef)parent.GetMethodDesc(name) != null) throw new PEFileException("Existing method!!");
  380. //sig = buff.ReadMethSig(this,name,sigIx);
  381. //MethodRef existing = (MethodRef)parent.GetMethod(sig);
  382. //if (existing != null)
  383. // return existing;
  384. parent.AddToMethodList(this);
  385. return this;
  386. }
  387. public void MakeVarArgMethod(MethodDef paren, Type[] optPars)
  388. {
  389. if (paren != null)
  390. {
  391. parent = null;
  392. varArgParent = paren;
  393. }
  394. sig.optParTypes = optPars;
  395. if (sig.optParTypes != null) sig.numOptPars = (uint)sig.optParTypes.Length;
  396. sig.callConv = CallConv.Vararg;
  397. }
  398. internal void MakeGenericPars(uint num)
  399. {
  400. // Experimental (kjg) 2007-09-03
  401. // It appears that for some system dll the MethodRef may not
  402. // have any generic params defined, but the methodSig does.
  403. if (genericParams == null)
  404. if (genericParams == null) genericParams = new ArrayList();
  405. for (int i = genericParams.Count; i < num; i++)
  406. {
  407. genericParams.Add(new GenericParam("GPar" + i, this, i));
  408. }
  409. // Previous code ...
  410. //if (genericParams != null) {
  411. // for (int i=genericParams.Count; i < num; i++) {
  412. // genericParams.Add(new GenericParam("GPar"+i,this,i));
  413. // }
  414. //}
  415. //sig.numGenPars = (uint)genericParams.Count;
  416. }
  417. /*------------------------- public set and get methods --------------------------*/
  418. /// <summary>
  419. /// Set the parameter types for this method
  420. /// </summary>
  421. /// <param name="pars">List of types of method parameters</param>
  422. public void SetParTypes(Type[] pars)
  423. {
  424. if (pars == null)
  425. {
  426. sig.numPars = 0;
  427. return;
  428. }
  429. sig.parTypes = pars;
  430. sig.numPars = (uint)pars.Length;
  431. }
  432. /// <summary>
  433. /// Set the list of optional parameter types for this method
  434. /// </summary>
  435. /// <param name="pars">list of optional parameter types</param>
  436. public void SetOptParTypes(Type[] pars)
  437. {
  438. if (pars == null)
  439. {
  440. sig.numOptPars = 0;
  441. return;
  442. }
  443. sig.optParTypes = pars;
  444. sig.numOptPars = (uint)sig.optParTypes.Length;
  445. }
  446. /*------------------------- internal functions --------------------------*/
  447. internal sealed override void TypeSig(MemoryStream sigStream)
  448. {
  449. sig.TypeSig(sigStream);
  450. }
  451. internal sealed override void BuildTables(MetaDataOut md)
  452. {
  453. md.AddToTable(MDTable.MemberRef, this);
  454. nameIx = md.AddToStringsHeap(name);
  455. if (parent != null)
  456. {
  457. if (parent is ClassSpec) md.AddToTable(MDTable.TypeSpec, parent);
  458. if (parent is ConstructedTypeSpec)
  459. md.AddToTable(MDTable.TypeSpec, ((ConstructedTypeSpec)parent).Spec);
  460. parent.BuildMDTables(md);
  461. }
  462. sig.BuildTables(md);
  463. }
  464. internal sealed override void BuildSignatures(MetaDataOut md)
  465. {
  466. sig.BuildSignatures(md);
  467. MemoryStream sigStream = new MemoryStream();
  468. TypeSig(sigStream);
  469. sigIx = md.AddToBlobHeap(sigStream.ToArray());
  470. done = false;
  471. }
  472. internal override void BuildCILInfo(CILWriter output)
  473. {
  474. parent.BuildCILInfo(output);
  475. }
  476. internal static uint Size(MetaData md)
  477. {
  478. return md.CodedIndexSize(CIx.MemberRefParent) + md.StringsIndexSize() + md.BlobIndexSize();
  479. }
  480. internal sealed override void Write(PEWriter output)
  481. {
  482. if (varArgParent != null)
  483. output.WriteCodedIndex(CIx.MemberRefParent, varArgParent);
  484. else if (parent is ConstructedTypeSpec)
  485. output.WriteCodedIndex(CIx.MemberRefParent, ((ConstructedTypeSpec)parent).Spec);
  486. else
  487. output.WriteCodedIndex(CIx.MemberRefParent, parent);
  488. output.StringsIndex(nameIx);
  489. output.BlobIndex(sigIx);
  490. }
  491. internal sealed override uint GetCodedIx(CIx code)
  492. {
  493. switch (code)
  494. {
  495. case (CIx.HasCustomAttr): return 6;
  496. case (CIx.MethodDefOrRef): return 1;
  497. case (CIx.CustomAttributeType): return 3;
  498. }
  499. return 0;
  500. }
  501. }
  502. /**************************************************************************/
  503. /// <summary>
  504. /// Descriptor for a method defined in THIS assembly/module
  505. /// IL .method
  506. /// </summary>
  507. public class MethodDef : Method
  508. {
  509. private static readonly ushort PInvokeImpl = 0x2000;
  510. private static readonly ushort NotPInvoke = 0xDFFF;
  511. private static readonly ushort HasSecurity = 0x4000;
  512. private static readonly ushort NoSecurity = 0xBFFF;
  513. //private static readonly uint UnmanagedExport = 0x0008;
  514. uint parIx = 0, textOffset = 0;
  515. internal MethodRef refOf;
  516. // The default max stack depth to be assigned when the depth can not be calculated.
  517. private static readonly int DefaultMaxStackDepth = 8;
  518. CILInstructions code;
  519. uint rva;
  520. Param[] parList;
  521. Local[] locals;
  522. bool initLocals;
  523. ushort methFlags = 0, implFlags = 0;
  524. int maxStack = 0, numLocals = 0;
  525. uint numPars = 0;
  526. bool entryPoint = false;
  527. internal LocalSig localSig;
  528. MethodRef varArgSig;
  529. ImplMap pinvokeImpl;
  530. ArrayList security = null;
  531. internal uint locToken = 0;
  532. /*-------------------- Constructors ---------------------------------*/
  533. internal MethodDef(string name, Type retType, Param[] pars, ClassDef paren)
  534. : base(name, retType, paren)
  535. {
  536. sig.SetParTypes(pars);
  537. parList = pars;
  538. parent = paren;
  539. tabIx = MDTable.Method;
  540. }
  541. internal MethodDef(ClassDef paren, MethSig mSig, Param[] pars)
  542. : base(mSig.name)
  543. {
  544. sig = mSig;
  545. parList = pars;
  546. parent = paren;
  547. tabIx = MDTable.Method;
  548. }
  549. internal MethodDef(ClassSpec paren, MethSig mSig, Param[] pars)
  550. : base(mSig.name)
  551. {
  552. parent = paren;
  553. parList = pars;
  554. sig = mSig;
  555. tabIx = MDTable.Method;
  556. }
  557. internal MethodDef(PEReader buff)
  558. : base(null)
  559. {
  560. rva = buff.ReadUInt32();
  561. implFlags = buff.ReadUInt16();
  562. methFlags = buff.ReadUInt16();
  563. name = buff.GetString();
  564. sigIx = buff.GetBlobIx();
  565. parIx = buff.GetIndex(MDTable.Param);
  566. tabIx = MDTable.Method;
  567. }
  568. internal static void Read(PEReader buff, TableRow[] methDefs)
  569. {
  570. MethodDef prevDef = null;
  571. prevDef = new MethodDef(buff);
  572. methDefs[0] = prevDef;
  573. for (int i = 1; i < methDefs.Length; i++)
  574. {
  575. prevDef.Row = (uint)i;
  576. MethodDef methDef = new MethodDef(buff);
  577. prevDef.numPars = methDef.parIx - prevDef.parIx;
  578. prevDef = methDef;
  579. methDefs[i] = methDef;
  580. }
  581. prevDef.Row = (uint)methDefs.Length;
  582. prevDef.numPars = (buff.GetTableSize(MDTable.Param) + 1) - prevDef.parIx;
  583. }
  584. internal static void GetMethodRefs(PEReader buff, uint num, ClassRef parent)
  585. {
  586. for (int i = 0; i < num; i++)
  587. {
  588. uint rva = buff.ReadUInt32();
  589. ushort implFlags = buff.ReadUInt16();
  590. ushort methFlags = buff.ReadUInt16();
  591. string name = buff.GetString();
  592. uint sigIx = buff.GetBlobIx();
  593. uint parIx = buff.GetIndex(MDTable.Param);
  594. if (IsPublicOrProtected(methFlags))
  595. {
  596. MethodRef mRef = new MethodRef(parIx, name, sigIx); // changed
  597. mRef.SetParent(parent);
  598. //Console.WriteLine(parent.NameString());
  599. MethSig mSig = buff.ReadMethSig(mRef, name, sigIx);
  600. //mSig.name = name;
  601. mRef.SetSig(mSig); // changed
  602. parent.AddToMethodList(mRef);
  603. }
  604. }
  605. }
  606. private void DoPars(PEReader buff, bool resolvePars)
  607. {
  608. if (sig == null) sig = buff.ReadMethSig(this, sigIx);
  609. sig.name = name;
  610. parList = new Param[sig.numPars];
  611. if (parIx >= buff.GetTableSize(MDTable.Param))
  612. {
  613. // EXPERIMENTAL kjg 19 November 2007
  614. // It is actually allowed that a method def does not
  615. // have corresponding Param metadata, provided the
  616. // parameter types may be constructed from the sig.
  617. for (uint i = 0; i < sig.numPars; i++)
  618. {
  619. parList[i] = Param.DefaultParam();
  620. parList[i].SetParType(sig.parTypes[i]);
  621. }
  622. }
  623. else
  624. {
  625. for (uint i = 0; i < sig.numPars; i++)
  626. {
  627. parList[i] = (Param)buff.GetElement(MDTable.Param, i + parIx);
  628. if (resolvePars) parList[i].Resolve(buff, i + parIx, sig.parTypes[i]);
  629. else parList[i].SetParType(sig.parTypes[i]);
  630. }
  631. }
  632. }
  633. private void DoCode(PEReader buff)
  634. {
  635. if (rva != 0)
  636. {
  637. if (Diag.DiagOn) Console.WriteLine("Reading byte codes for method " + name);
  638. buff.ReadByteCodes(this, rva);
  639. }
  640. }
  641. internal sealed override void Resolve(PEReader buff)
  642. {
  643. buff.currentMethodScope = this;
  644. buff.currentClassScope = parent;
  645. DoPars(buff, true);
  646. if (!buff.skipBody)
  647. {
  648. DoCode(buff);
  649. }
  650. buff.currentMethodScope = null;
  651. buff.currentClassScope = null;
  652. }
  653. /*------------------------- public set and get methods --------------------------*/
  654. /// <summary>
  655. /// Get the parameters of this method
  656. /// </summary>
  657. /// <returns>Array of params of this method</returns>
  658. public Param[] GetParams()
  659. {
  660. return parList;
  661. }
  662. /// <summary>
  663. /// Set the parameters for this method
  664. /// </summary>
  665. /// <param name="pars">Descriptors of the parameters for this method</param>
  666. public void SetParams(Param[] pars)
  667. {
  668. parList = pars;
  669. sig.SetParTypes(pars);
  670. }
  671. /// <summary>
  672. /// Add some attributes to this method descriptor
  673. /// </summary>
  674. /// <param name="ma">the attributes to be added</param>
  675. public void AddMethAttribute(MethAttr ma) { methFlags |= (ushort)ma; }
  676. /// <summary>
  677. /// Property to get and set the attributes for this method
  678. /// </summary>
  679. public MethAttr GetMethAttributes() { return (MethAttr)methFlags; }
  680. public void SetMethAttributes(MethAttr ma) { methFlags = (ushort)ma; }
  681. /// <summary>
  682. /// Add some implementation attributes to this method descriptor
  683. /// </summary>
  684. /// <param name="ia">the attributes to be added</param>
  685. public void AddImplAttribute(ImplAttr ia)
  686. {
  687. implFlags |= (ushort)ia;
  688. }
  689. /// <summary>
  690. /// Property to get and set the implementation attributes for this method
  691. /// </summary>
  692. public ImplAttr GetImplAttributes() { return (ImplAttr)implFlags; }
  693. public void SetImplAttributes(ImplAttr ia) { implFlags = (ushort)ia; }
  694. public void AddPInvokeInfo(ModuleRef scope, string methName,
  695. PInvokeAttr callAttr)
  696. {
  697. pinvokeImpl = new ImplMap((ushort)callAttr, this, methName, scope);
  698. methFlags |= PInvokeImpl;
  699. }
  700. public void RemovePInvokeInfo()
  701. {
  702. pinvokeImpl = null;
  703. methFlags &= NotPInvoke;
  704. }
  705. public void AddSecurity(SecurityAction act, byte[] permissionSet)
  706. {
  707. methFlags |= HasSecurity;
  708. if (security == null) security = new ArrayList();
  709. security.Add(new DeclSecurity(this, act, permissionSet));
  710. }
  711. public void AddSecurity(DeclSecurity sec)
  712. {
  713. methFlags |= HasSecurity;
  714. if (security == null) security = new ArrayList();
  715. security.Add(sec);
  716. }
  717. public DeclSecurity[] GetSecurity()
  718. {
  719. if (security == null) return null;
  720. return (DeclSecurity[])security.ToArray(typeof(DeclSecurity));
  721. }
  722. public void RemoveSecurity()
  723. {
  724. security = null;
  725. methFlags &= NoSecurity;
  726. }
  727. /// <summary>
  728. /// Set the maximum stack height for this method
  729. /// </summary>
  730. /// <param name="maxStack">the maximum height of the stack</param>
  731. public void SetMaxStack(int maxStack)
  732. {
  733. this.maxStack = maxStack;
  734. }
  735. /// <summary>
  736. /// Retrieve the maximum size of the stack for the code
  737. /// of this method
  738. /// </summary>
  739. /// <returns>max stack height for CIL codes</returns>
  740. public int GetMaxStack()
  741. {
  742. return maxStack;
  743. }
  744. /// <summary>
  745. /// Add local variables to this method
  746. /// </summary>
  747. /// <param name="locals">the locals to be added</param>
  748. /// <param name="initLocals">are locals initialised to default values</param>
  749. public void AddLocals(Local[] locals, bool initLocals)
  750. {
  751. if (locals == null) return;
  752. this.locals = locals;
  753. this.initLocals = initLocals;
  754. numLocals = locals.Length;
  755. for (int i = 0; i < numLocals; i++)
  756. {
  757. this.locals[i].SetIndex(i);
  758. }
  759. }
  760. /// <summary>
  761. /// Retrieve the locals for this method
  762. /// </summary>
  763. /// <returns>list of locals declared in this method</returns>
  764. public Local[] GetLocals() { return locals; }
  765. /// <summary>
  766. /// Remove all the locals from this method
  767. /// </summary>
  768. public void RemoveLocals()
  769. {
  770. locals = null;
  771. numLocals = 0;
  772. initLocals = false;
  773. }
  774. /// <summary>
  775. /// Mark this method as having an entry point
  776. /// </summary>
  777. public void DeclareEntryPoint() { entryPoint = true; }
  778. /// <summary>
  779. /// Does this method have an entrypoint?
  780. /// </summary>
  781. public bool HasEntryPoint() { return entryPoint; }
  782. /// <summary>
  783. /// Remove the entry point from this method
  784. /// </summary>
  785. public void RemoveEntryPoint() { entryPoint = false; }
  786. /// <summary>
  787. /// Create a code buffer for this method to add the IL instructions to
  788. /// </summary>
  789. /// <returns>a buffer for this method's IL instructions</returns>
  790. public CILInstructions CreateCodeBuffer()
  791. {
  792. code = new CILInstructions(this);
  793. return code;
  794. }
  795. /// <summary>
  796. /// Get the CIL code buffer for this method
  797. /// </summary>
  798. /// <returns>Code buffer for this method</returns>
  799. public CILInstructions GetCodeBuffer() { return code; }
  800. /// <summary>
  801. /// Make a method reference descriptor for this method to be used
  802. /// as a callsite signature for this vararg method
  803. /// </summary>
  804. /// <param name="optPars">the optional pars for the vararg method call</param>
  805. /// <returns></returns>
  806. public MethodRef MakeVarArgSignature(Type[] optPars)
  807. {
  808. MethSig mSig = new MethSig(name);
  809. mSig.parTypes = sig.parTypes;
  810. mSig.retType = sig.retType;
  811. varArgSig = new MethodRef(sig);
  812. varArgSig.MakeVarArgMethod(this, optPars);
  813. return varArgSig;
  814. }
  815. public MethodRef GetVarArgSignature()
  816. {
  817. return varArgSig;
  818. }
  819. /// <summary>
  820. /// Get the MethodRef equivalent to this MethodDef. Assumes
  821. /// that one has been created.
  822. /// </summary>
  823. /// <returns>MethodRef for this MethodDef</returns>
  824. public MethodRef RefOf() { return refOf; }
  825. /// <summary>
  826. /// Get the MethodRef equivalent to this MethodDef. If one
  827. /// does not exist, then create it.
  828. /// </summary>
  829. /// <returns>MethodRef for this MethodDef</returns>
  830. public MethodRef MakeRefOf()
  831. {
  832. if (refOf != null) return refOf;
  833. ClassRef parRef = ((ClassDef)parent).MakeRefOf();
  834. refOf = parRef.GetMethod(name, sig.parTypes);
  835. if (refOf == null)
  836. {
  837. Type rType = sig.MakeRefRetType();
  838. Type[] pTypes = sig.MakeRefParTypes();
  839. refOf = new MethodRef(parRef, name, rType, pTypes);
  840. refOf.defOf = this;
  841. refOf.AddCallConv(this.GetCallConv());
  842. }
  843. return refOf;
  844. }
  845. /*------------------------- internal functions --------------------------*/
  846. private static bool IsPublicOrProtected(ushort methFlags)
  847. {
  848. return (methFlags & (ushort)MethAttr.Public) == (ushort)MethAttr.Public ||
  849. (methFlags & (ushort)MethAttr.Family) == (ushort)MethAttr.Family;
  850. }
  851. internal void InsertGenericParam(GenericParam genPar)
  852. {
  853. if (genericParams == null) genericParams = new ArrayList();
  854. for (int i = 0; i < genericParams.Count - genPar.Index; i++)
  855. {
  856. genericParams.Add(null);
  857. }
  858. genericParams.Insert((int)genPar.Index, genPar);
  859. }
  860. internal override bool isDef() { return true; }
  861. internal PEFile GetScope()
  862. {
  863. return ((ClassDef)parent).GetScope();
  864. }
  865. internal void ChangeRefsToDefs(ClassDef newPar, ClassDef[] oldTypes)
  866. {
  867. parent = newPar;
  868. sig.ChangeParTypes(newPar, oldTypes);
  869. if (code != null)
  870. code.ChangeRefsToDefs(newPar, oldTypes);
  871. }
  872. internal void AddPInvokeInfo(ImplMap impl)
  873. {
  874. pinvokeImpl = impl;
  875. methFlags |= PInvokeImpl;
  876. }
  877. internal void AddVarArgSig(MethodRef meth)
  878. {
  879. varArgSig = meth;
  880. //meth.MakeVarArgMethod(this,null);
  881. }
  882. internal sealed override void TypeSig(MemoryStream sigStream)
  883. {
  884. sig.TypeSig(sigStream);
  885. }
  886. // fix for Whidbey bug
  887. internal void AddGenericsToTable(MetaDataOut md)
  888. {
  889. if (genericParams != null)
  890. {
  891. for (int i = 0; i < genericParams.Count; i++)
  892. {
  893. md.AddToTable(MDTable.GenericParam, (GenericParam)genericParams[i]);
  894. }
  895. }
  896. }
  897. internal sealed override void BuildTables(MetaDataOut md)
  898. {
  899. md.AddToTable(MDTable.Method, this);
  900. nameIx = md.AddToStringsHeap(name);
  901. if (genericParams != null)
  902. {
  903. for (int i = 0; i < genericParams.Count; i++)
  904. {
  905. ((GenericParam)genericParams[i]).BuildMDTables(md);
  906. }
  907. }
  908. if (security != null)
  909. {
  910. for (int i = 0; i < security.Count; i++)
  911. {
  912. ((DeclSecurity)security[i]).BuildMDTables(md);
  913. }
  914. }
  915. if (pinvokeImpl != null) pinvokeImpl.BuildMDTables(md);
  916. if (entryPoint) md.SetEntryPoint(this);
  917. if (locals != null)
  918. {
  919. localSig = new LocalSig(locals);
  920. localSig.BuildMDTables(md);
  921. }
  922. try
  923. {
  924. if (code != null)
  925. {
  926. if (code.IsEmpty())
  927. {
  928. code = null;
  929. }
  930. else
  931. {
  932. code.BuildTables(md);
  933. }
  934. }
  935. }
  936. catch (InstructionException ex)
  937. {
  938. throw new Exception(ex.AddMethodName(name));
  939. }
  940. parIx = md.TableIndex(MDTable.Param);
  941. for (int i = 0; i < sig.numPars; i++)
  942. {
  943. parList[i].seqNo = (ushort)(i + 1);
  944. parList[i].BuildMDTables(md);
  945. }
  946. sig.BuildTables(md);
  947. }
  948. internal sealed override void BuildCILInfo(CILWriter output)
  949. {
  950. if (genericParams != null)
  951. {
  952. for (int i = 0; i < genericParams.Count; i++)
  953. {
  954. ((GenericParam)genericParams[i]).BuildCILInfo(output);
  955. }
  956. }
  957. if (security != null)
  958. {
  959. for (int i = 0; i < security.Count; i++)
  960. {
  961. ((DeclSecurity)security[i]).BuildCILInfo(output);
  962. }
  963. }
  964. if (pinvokeImpl != null) pinvokeImpl.BuildCILInfo(output);
  965. if (locals != null)
  966. {
  967. for (int i = 0; i < locals.Length; i++)
  968. {
  969. locals[i].BuildCILInfo(output);
  970. }
  971. }
  972. try
  973. {
  974. if (code != null) code.BuildCILInfo(output);
  975. }
  976. catch (InstructionException ex)
  977. {
  978. throw new Exception(ex.AddMethodName(name));
  979. }
  980. sig.BuildCILInfo(output);
  981. }
  982. internal sealed override void BuildSignatures(MetaDataOut md)
  983. {
  984. if (locals != null)
  985. {
  986. localSig.BuildSignatures(md);
  987. locToken = localSig.Token();
  988. }
  989. if (code != null)
  990. {
  991. // If the stack depth has not been explicity set, try to work out what is needed.
  992. if (maxStack == 0)
  993. {
  994. try
  995. {
  996. // Set the flag to show if the return type is void or other.
  997. code.ReturnsVoid = GetRetType().SameType(PrimitiveType.Void);
  998. // Calculate the max stack depth
  999. maxStack = code.GetMaxStackDepthRequired();
  1000. }
  1001. catch (CouldNotFindMaxStackDepth)
  1002. {
  1003. // Could not find the depth, assign the default
  1004. maxStack = DefaultMaxStackDepth;
  1005. }
  1006. }
  1007. code.CheckCode(locToken, initLocals, maxStack, md);
  1008. textOffset = md.AddCode(code);
  1009. if (Diag.DiagOn) Console.WriteLine("code offset = " + textOffset);
  1010. }
  1011. sig.BuildSignatures(md);
  1012. MemoryStream outSig = new MemoryStream();
  1013. TypeSig(outSig);
  1014. sigIx = md.AddToBlobHeap(outSig.ToArray());
  1015. done = false;
  1016. }
  1017. internal static uint Size(MetaData md)
  1018. {
  1019. return 8 + md.StringsIndexSize() + md.BlobIndexSize() + md.TableIndexSize(MDTable.Param);
  1020. }
  1021. internal sealed override void Write(PEWriter output)
  1022. {
  1023. if (code == null) output.Write(0);
  1024. else output.WriteCodeRVA(textOffset);
  1025. output.Write(implFlags);
  1026. output.Write(methFlags);
  1027. output.StringsIndex(nameIx);
  1028. output.BlobIndex(sigIx);
  1029. output.WriteIndex(MDTable.Param, parIx);
  1030. }
  1031. internal override void Write(CILWriter output)
  1032. {
  1033. output.Write(" .method ");
  1034. WriteFlags(output, methFlags);
  1035. sig.Write(output);
  1036. output.Write(" " + name + "(");
  1037. if (parList != null)
  1038. {
  1039. for (int i = 0; i < parList.Length; i++)
  1040. {
  1041. parList[i].Write(output);
  1042. if (i < parList.Length - 1)
  1043. {
  1044. output.Write(", ");
  1045. }
  1046. }
  1047. }
  1048. output.Write(") ");
  1049. uint codeType = implFlags & (uint)0x11;
  1050. if (codeType == 0)
  1051. {
  1052. output.Write("cil ");
  1053. }
  1054. else if (codeType == 1)
  1055. {
  1056. output.Write("native ");
  1057. }
  1058. else if (codeType == 3)
  1059. {
  1060. output.Write("runtime ");
  1061. }
  1062. if ((implFlags & (uint)ImplAttr.Unmanaged) == 0)
  1063. {
  1064. output.Write("managed ");
  1065. }
  1066. else
  1067. {
  1068. output.Write("unmanaged ");
  1069. }
  1070. if ((implFlags & (uint)ImplAttr.ForwardRef) != 0)
  1071. {
  1072. output.Write("forwardref ");
  1073. }
  1074. if ((implFlags & (uint)ImplAttr.InternalCall) != 0)
  1075. {
  1076. output.Write("internalcall ");
  1077. }
  1078. if ((implFlags & (uint)ImplAttr.Synchronized) != 0)
  1079. {
  1080. output.Write("synchronized ");
  1081. }
  1082. if ((implFlags & (uint)ImplAttr.NoInLining) != 0)
  1083. {
  1084. output.Write("noinlining ");
  1085. }
  1086. output.WriteLine(" {");
  1087. if ((locals != null) && (locals.Length > 0))
  1088. {
  1089. output.Write(" .locals (");
  1090. for (int i = 0; i < locals.Length; i++)
  1091. {
  1092. if (i > 0)
  1093. {
  1094. output.Write(" ");
  1095. }
  1096. locals[i].Write(output);
  1097. if (i < locals.Length - 1)
  1098. {
  1099. output.WriteLine(",");
  1100. }
  1101. }
  1102. output.WriteLine(" )");
  1103. }
  1104. if (entryPoint)
  1105. {
  1106. output.WriteLine(" .entrypoint");
  1107. }
  1108. if (code != null) code.Write(output);
  1109. output.WriteLine(" }");
  1110. }
  1111. internal sealed override uint GetCodedIx(CIx code)
  1112. {
  1113. switch (code)
  1114. {
  1115. case (CIx.HasCustomAttr): return 0;
  1116. case (CIx.HasDeclSecurity): return 1;
  1117. case (CIx.MemberRefParent): return 3;
  1118. case (CIx.MethodDefOrRef): return 0;
  1119. case (CIx.MemberForwarded): return 1;
  1120. case (CIx.CustomAttributeType): return 2;
  1121. case (CIx.TypeOrMethodDef): return 1;
  1122. }
  1123. return 0;
  1124. }
  1125. }
  1126. }