2
0

Local.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. namespace QUT.PERWAPI
  20. {
  21. /**************************************************************************/
  22. // Class to describe procedure locals
  23. /**************************************************************************/
  24. /// <summary>
  25. /// Descriptor for a local of a method
  26. /// </summary>
  27. public class Local
  28. {
  29. private static readonly byte PINNED = 0x45;
  30. string name;
  31. public Type type;
  32. bool pinned = false;
  33. int index = 0;
  34. /*-------------------- Constructors ---------------------------------*/
  35. /// <summary>
  36. /// Create a new local variable
  37. /// </summary>
  38. /// <param name="lName">name of the local variable</param>
  39. /// <param name="lType">type of the local variable</param>
  40. public Local(string lName, Type lType)
  41. {
  42. name = lName;
  43. type = lType;
  44. }
  45. /// <summary>
  46. /// Create a new local variable that is byref and/or pinned
  47. /// </summary>
  48. /// <param name="lName">local name</param>
  49. /// <param name="lType">local type</param>
  50. /// <param name="isPinned">has pinned attribute</param>
  51. public Local(string lName, Type lType, bool isPinned)
  52. {
  53. name = lName;
  54. type = lType;
  55. pinned = isPinned;
  56. }
  57. public int GetIndex() { return index; }
  58. /// <summary>
  59. /// The name of the local variable.
  60. /// </summary>
  61. public string Name { get { return name; } }
  62. public bool Pinned
  63. {
  64. get { return pinned; }
  65. set { pinned = value; }
  66. }
  67. /// <summary>
  68. /// Gets the signature for this local variable.
  69. /// </summary>
  70. /// <returns>A byte array of the signature.</returns>
  71. public byte[] GetSig()
  72. {
  73. MemoryStream str = new MemoryStream();
  74. type.TypeSig(str);
  75. return str.ToArray();
  76. }
  77. internal void SetIndex(int ix)
  78. {
  79. index = ix;
  80. }
  81. internal void TypeSig(MemoryStream str)
  82. {
  83. if (pinned) str.WriteByte(PINNED);
  84. type.TypeSig(str);
  85. }
  86. internal void BuildTables(MetaDataOut md)
  87. {
  88. if (!(type is ClassDef))
  89. type.BuildMDTables(md);
  90. }
  91. internal void BuildCILInfo(CILWriter output)
  92. {
  93. if (!(type is ClassDef))
  94. type.BuildCILInfo(output);
  95. }
  96. internal void Write(CILWriter output)
  97. {
  98. type.WriteType(output);
  99. output.Write("\t" + name);
  100. }
  101. }
  102. }