2
0

MDTypeElems.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 all IL types
  26. /// </summary>
  27. public abstract class Type : MetaDataElement
  28. {
  29. protected byte typeIndex;
  30. /// <summary>
  31. /// The following is only used for TypeSpecs and ClassSpecs. kjg
  32. /// </summary>
  33. internal bool typeSpecAdded = false; // so that MetaDataOut can reset it
  34. /*-------------------- Constructors ---------------------------------*/
  35. internal Type(byte tyIx) { typeIndex = tyIx; }
  36. internal byte GetTypeIndex() { return typeIndex; }
  37. internal virtual bool SameType(Type tstType)
  38. {
  39. return this == tstType;
  40. }
  41. internal virtual void TypeSig(MemoryStream str)
  42. {
  43. throw new TypeSignatureException(this.GetType().AssemblyQualifiedName +
  44. " doesn't have a type signature!!");
  45. }
  46. public virtual string TypeName()
  47. {
  48. return "NoTypeName";
  49. }
  50. internal virtual void WriteType(CILWriter output)
  51. {
  52. throw new NotYetImplementedException("Writing types for CIL");
  53. }
  54. internal virtual void WriteName(CILWriter output)
  55. {
  56. WriteType(output);
  57. }
  58. internal virtual Type AddTypeSpec(MetaDataOut md)
  59. {
  60. if (!isDef()) BuildMDTables(md);
  61. return this;
  62. }
  63. }
  64. /**************************************************************************/
  65. /// <summary>
  66. /// Descriptor for a custom modifier of a type (modopt or modreq)
  67. /// </summary>
  68. public class CustomModifiedType : Type
  69. {
  70. Type type;
  71. Class cmodType;
  72. /*-------------------- Constructors ---------------------------------*/
  73. /// <summary>
  74. /// Create a new custom modifier for a type
  75. /// </summary>
  76. /// <param name="type">the type to be modified</param>
  77. /// <param name="cmod">the modifier</param>
  78. /// <param name="cmodType">the type reference to be associated with the type</param>
  79. public CustomModifiedType(Type type, CustomModifier cmod, Class cmodType)
  80. : base((byte)cmod)
  81. {
  82. this.type = type;
  83. this.cmodType = cmodType;
  84. }
  85. /*------------------------- public set and get methods --------------------------*/
  86. public void SetModifiedType(Type modType) { type = modType; }
  87. public Type GetModifiedType() { return type; }
  88. public void SetModifingType(Class mod) { cmodType = mod; }
  89. public Class GetModifingType() { return cmodType; }
  90. public void SetModifier(CustomModifier cmod) { typeIndex = (byte)cmod; }
  91. public CustomModifier GetModifier() { return (CustomModifier)typeIndex; }
  92. /*----------------------------- internal functions ------------------------------*/
  93. internal override bool SameType(Type tstType)
  94. {
  95. if (this == tstType) return true;
  96. if (tstType is CustomModifiedType)
  97. {
  98. CustomModifiedType cmTstType = (CustomModifiedType)tstType;
  99. return type.SameType(cmTstType.type) &&
  100. cmodType.SameType(cmTstType.cmodType);
  101. }
  102. return false;
  103. }
  104. internal sealed override void TypeSig(MemoryStream str)
  105. {
  106. str.WriteByte(typeIndex);
  107. MetaDataOut.CompressNum(BlobUtil.CompressUInt(cmodType.TypeDefOrRefToken()), str);
  108. type.TypeSig(str);
  109. }
  110. internal sealed override void BuildTables(MetaDataOut md)
  111. {
  112. if (!(cmodType is ClassDef))
  113. cmodType.BuildMDTables(md);
  114. if (!(type is ClassDef))
  115. type.BuildMDTables(md);
  116. }
  117. }
  118. /**************************************************************************/
  119. internal class Pinned : Type
  120. {
  121. internal Pinned() : base((byte)ElementType.Pinned) { }
  122. }
  123. /**************************************************************************/
  124. internal class Sentinel : Type
  125. {
  126. internal Sentinel() : base((byte)ElementType.Sentinel) { }
  127. }
  128. /**************************************************************************/
  129. public abstract class TypeSpec : Type
  130. {
  131. uint sigIx = 0;
  132. //internal bool typeSpecAdded = false; // so that MetaDataOut can reset it
  133. /*-------------------- Constructors ---------------------------------*/
  134. internal TypeSpec(byte typeIx)
  135. : base(typeIx)
  136. {
  137. tabIx = MDTable.TypeSpec;
  138. }
  139. internal static void Read(PEReader buff, TableRow[] specs)
  140. {
  141. for (int i = 0; i < specs.Length; i++)
  142. {
  143. specs[i] = new UnresolvedTypeSpec(buff, i);
  144. //specs[i] = buff.GetBlobType(null,null,buff.GetBlobIx());
  145. //if (specs[i] is GenericParam) {
  146. // Console.WriteLine("GenericParam in TypeSpec table at pos " + i);
  147. //}
  148. }
  149. }
  150. internal override sealed Type AddTypeSpec(MetaDataOut md)
  151. {
  152. if (typeSpecAdded) return this;
  153. md.AddToTable(MDTable.TypeSpec, this);
  154. BuildMDTables(md);
  155. typeSpecAdded = true;
  156. return this;
  157. }
  158. internal override void BuildSignatures(MetaDataOut md)
  159. {
  160. MemoryStream str = new MemoryStream();
  161. TypeSig(str);
  162. sigIx = md.AddToBlobHeap(str.ToArray());
  163. done = false;
  164. }
  165. internal static uint Size(MetaData md)
  166. {
  167. return md.BlobIndexSize();
  168. }
  169. internal sealed override void Write(PEWriter output)
  170. {
  171. //Console.WriteLine("Writing the blob index for a TypeSpec");
  172. output.BlobIndex(sigIx);
  173. }
  174. internal sealed override uint GetCodedIx(CIx code)
  175. {
  176. switch (code)
  177. {
  178. case (CIx.TypeDefOrRef): return 2;
  179. case (CIx.HasCustomAttr): return 13;
  180. case (CIx.MemberRefParent): return 4;
  181. }
  182. return 0;
  183. }
  184. }
  185. /**************************************************************************/
  186. /// <summary>
  187. /// Descriptor for the Primitive types defined in IL
  188. /// </summary>
  189. public class PrimitiveType : TypeSpec
  190. {
  191. private string name;
  192. private int systemTypeIndex;
  193. internal static int NumSystemTypes = 18;
  194. public static readonly PrimitiveType Void = new PrimitiveType(0x01, "Void", 0);
  195. public static readonly PrimitiveType Boolean = new PrimitiveType(0x02, "Boolean", 1);
  196. public static readonly PrimitiveType Char = new PrimitiveType(0x03, "Char", 2);
  197. public static readonly PrimitiveType Int8 = new PrimitiveType(0x04, "SByte", 3);
  198. public static readonly PrimitiveType UInt8 = new PrimitiveType(0x05, "Byte", 4);
  199. public static readonly PrimitiveType Int16 = new PrimitiveType(0x06, "Int16", 5);
  200. public static readonly PrimitiveType UInt16 = new PrimitiveType(0x07, "UInt16", 6);
  201. public static readonly PrimitiveType Int32 = new PrimitiveType(0x08, "Int32", 7);
  202. public static readonly PrimitiveType UInt32 = new PrimitiveType(0x09, "UInt32", 8);
  203. public static readonly PrimitiveType Int64 = new PrimitiveType(0x0A, "Int64", 9);
  204. public static readonly PrimitiveType UInt64 = new PrimitiveType(0x0B, "UInt64", 10);
  205. public static readonly PrimitiveType Float32 = new PrimitiveType(0x0C, "Single", 11);
  206. public static readonly PrimitiveType Float64 = new PrimitiveType(0x0D, "Double", 12);
  207. public static readonly PrimitiveType String = new PrimitiveType(0x0E, "String", 13);
  208. internal static readonly PrimitiveType Class = new PrimitiveType(0x12);
  209. public static readonly PrimitiveType TypedRef = new PrimitiveType(0x16, "TypedReference", 14);
  210. public static readonly PrimitiveType IntPtr = new PrimitiveType(0x18, "IntPtr", 15);
  211. public static readonly PrimitiveType UIntPtr = new PrimitiveType(0x19, "UIntPtr", 16);
  212. public static readonly PrimitiveType Object = new PrimitiveType(0x1C, "Object", 17);
  213. internal static readonly PrimitiveType ClassType = new PrimitiveType(0x50);
  214. internal static readonly PrimitiveType SZArray = new PrimitiveType(0x1D);
  215. public static readonly PrimitiveType NativeInt = IntPtr;
  216. public static readonly PrimitiveType NativeUInt = UIntPtr;
  217. internal static PrimitiveType[] primitives = {null,Void,Boolean,Char,Int8,UInt8,
  218. Int16,UInt16,Int32,UInt32,Int64,
  219. UInt64,Float32,Float64,String};
  220. /*-------------------- Constructors ---------------------------------*/
  221. internal PrimitiveType(byte typeIx) : base(typeIx) { }
  222. internal PrimitiveType(byte typeIx, string name, int STIx)
  223. : base(typeIx)
  224. {
  225. this.name = name;
  226. this.systemTypeIndex = STIx;
  227. }
  228. internal string GetName() { return name; }
  229. public override string TypeName()
  230. {
  231. if (typeIndex == 0x0E) return "System.String";
  232. return name;
  233. }
  234. internal int GetSystemTypeIx() { return systemTypeIndex; }
  235. internal sealed override void TypeSig(MemoryStream str)
  236. {
  237. str.WriteByte(typeIndex);
  238. }
  239. internal override void WriteType(CILWriter output)
  240. {
  241. //if (typeIndex == 0x0E) {
  242. // output.Write("[mscorlib]System.String");
  243. //} else
  244. switch (typeIndex)
  245. {
  246. case (0x1C): output.Write("[mscorlib]System.Object"); break;
  247. case (0x02): output.Write("bool"); break;
  248. case (0x0C): output.Write("float32"); break;
  249. case (0x0D): output.Write("float64"); break;
  250. default: output.Write(name.ToLower()); break;
  251. }
  252. }
  253. internal sealed override bool SameType(Type tstType)
  254. {
  255. if (tstType is SystemClass)
  256. return tstType.SameType(this);
  257. return this == tstType;
  258. }
  259. /* now done in MetaDataOut.WriteTildeStream
  260. internal static void ClearAddedFlags() { // KJG 18-April-2005
  261. for (int i = 0; i < primitives.Length; i++) {
  262. if (primitives[i] != null) primitives[i].typeSpecAdded = false;
  263. }
  264. }
  265. */
  266. }
  267. /**************************************************************************/
  268. /// <summary>
  269. /// Descriptor for a generic parameter for either a class or method.
  270. /// </summary>
  271. public class GenericParam : Type
  272. {
  273. private static readonly byte VAR = 0x13;
  274. private static readonly byte MVAR = 0x1E;
  275. ushort flags, index, kind = 0;
  276. uint parentIx, nameIx;
  277. string name;
  278. MetaDataElement parent;
  279. private ArrayList constraints = new ArrayList();
  280. internal static bool extraField = true;
  281. // There should only be one GenericParTypeSpec entry
  282. // int the metadata for each GenericParam.
  283. GenericParTypeSpec myTypeSpec;
  284. /*-------------------- Constructors ---------------------------------*/
  285. private GenericParam(uint index, byte elemIx)
  286. : base(elemIx)
  287. {
  288. this.index = (ushort)index;
  289. sortTable = true;
  290. }
  291. internal GenericParam(string name, MetaDataElement parent, int index)
  292. : base(VAR)
  293. {
  294. this.name = name;
  295. this.parent = parent;
  296. this.index = (ushort)index;
  297. if (parent is Method) typeIndex = MVAR;
  298. sortTable = true;
  299. tabIx = MDTable.GenericParam;
  300. }
  301. internal GenericParam(PEReader buff)
  302. : base(VAR)
  303. {
  304. index = buff.ReadUInt16();
  305. flags = buff.ReadUInt16();
  306. parentIx = buff.GetCodedIndex(CIx.TypeOrMethodDef);
  307. name = buff.GetString();
  308. if (extraField) kind = buff.ReadUInt16();
  309. sortTable = true;
  310. tabIx = MDTable.GenericParam;
  311. // resolve generic param immediately for signature resolution
  312. parent = buff.GetCodedElement(CIx.TypeOrMethodDef, parentIx);
  313. if (parent != null)
  314. {
  315. if (parent is MethodDef)
  316. {
  317. typeIndex = MVAR;
  318. ((MethodDef)parent).AddGenericParam(this);
  319. }
  320. else
  321. {
  322. ((ClassDef)parent).AddGenericParam(this);
  323. }
  324. }
  325. }
  326. internal GenericParam(string name)
  327. : base(MVAR)
  328. {
  329. this.name = name;
  330. sortTable = true;
  331. tabIx = MDTable.GenericParam;
  332. }
  333. internal static GenericParam AnonMethPar(uint ix)
  334. {
  335. return new GenericParam(ix, MVAR);
  336. }
  337. internal static GenericParam AnonClassPar(uint ix)
  338. {
  339. return new GenericParam(ix, VAR);
  340. }
  341. internal static void Read(PEReader buff, TableRow[] gpars)
  342. {
  343. for (int i = 0; i < gpars.Length; i++)
  344. gpars[i] = new GenericParam(buff);
  345. }
  346. /*------------------------- public set and get methods --------------------------*/
  347. /// <summary>
  348. /// Set the attribute for this generic parameter
  349. /// </summary>
  350. /// <param name="attr">the attribute</param>
  351. public void SetAttribute(GenericParamAttr attr)
  352. {
  353. flags = (ushort)attr;
  354. }
  355. /// <summary>
  356. /// Get the attribute for this generic parameter
  357. /// </summary>
  358. public GenericParamAttr GetAttribute()
  359. {
  360. return (GenericParamAttr)flags;
  361. }
  362. /// <summary>
  363. /// Add a type constraint to this generic parameter
  364. /// </summary>
  365. /// <param name="cType">class constraining the parameter type</param>
  366. public void AddConstraint(Class cType)
  367. {
  368. constraints.Add(cType);
  369. }
  370. /// <summary>
  371. /// Remove a constraint from this generic parameter
  372. /// </summary>
  373. /// <param name="cType">class type of constraint</param>
  374. public void RemoveConstraint(Class cType)
  375. {
  376. for (int i = 0; i < constraints.Count; i++)
  377. {
  378. if (constraints[i] == cType)
  379. {
  380. constraints.RemoveAt(i);
  381. return;
  382. }
  383. }
  384. }
  385. /// <summary>
  386. /// Return a constraint from the list
  387. /// </summary>
  388. /// <param name="i">constraint index</param>
  389. /// <returns></returns>
  390. public Class GetConstraint(int i)
  391. {
  392. return (Class)constraints[i];
  393. }
  394. /// <summary>
  395. /// Get the number of constrains on this GenericParam
  396. /// </summary>
  397. /// <returns></returns>
  398. public int GetConstraintCount()
  399. {
  400. return constraints.Count;
  401. }
  402. /// <summary>
  403. /// Get the name of this generic parameter
  404. /// </summary>
  405. /// <returns>generic parameter name</returns>
  406. public string GetName() { return name; }
  407. public MetaDataElement GetParent() { return parent; }
  408. public Class[] GetClassConstraints()
  409. {
  410. return (Class[])constraints.ToArray(typeof(Class)); // KJG 20-May-2005
  411. }
  412. /*----------------------------- internal functions ------------------------------*/
  413. internal uint Index
  414. {
  415. get { return index; }
  416. set { index = (ushort)value; }
  417. }
  418. internal void SetClassParam(Class paren, int ix)
  419. {
  420. typeIndex = VAR;
  421. parent = paren;
  422. index = (ushort)ix;
  423. }
  424. internal void SetMethParam(Method paren, int ix)
  425. {
  426. typeIndex = MVAR;
  427. parent = paren;
  428. index = (ushort)ix;
  429. }
  430. internal void CheckParent(MethodDef paren, PEReader buff)
  431. {
  432. if (paren == buff.GetCodedElement(CIx.TypeOrMethodDef, parentIx))
  433. {
  434. parent = paren;
  435. paren.InsertGenericParam(this);
  436. }
  437. }
  438. internal override void TypeSig(MemoryStream str)
  439. {
  440. str.WriteByte(typeIndex);
  441. str.WriteByte((byte)index);
  442. }
  443. internal static uint Size(MetaData md)
  444. {
  445. if (extraField)
  446. return 6 + md.CodedIndexSize(CIx.TypeOrMethodDef) + md.StringsIndexSize();
  447. else
  448. return 4 + md.CodedIndexSize(CIx.TypeOrMethodDef) + md.StringsIndexSize();
  449. }
  450. internal override Type AddTypeSpec(MetaDataOut md)
  451. {
  452. if (this.myTypeSpec == null) {
  453. this.myTypeSpec = new GenericParTypeSpec(this);
  454. md.AddToTable(MDTable.TypeSpec, this.myTypeSpec);
  455. }
  456. return this.myTypeSpec;
  457. }
  458. internal override uint SortKey()
  459. {
  460. return (parent.Row << MetaData.CIxShiftMap[(uint)CIx.TypeOrMethodDef])
  461. | parent.GetCodedIx(CIx.TypeOrMethodDef);
  462. }
  463. internal override void BuildTables(MetaDataOut md)
  464. {
  465. if (parent is MethodRef || parent is ClassRef) return; // don't add it - fix by CK
  466. md.AddToTable(MDTable.GenericParam, this);
  467. nameIx = md.AddToStringsHeap(name);
  468. for (int i = 0; i < constraints.Count; i++)
  469. {
  470. Class cClass = (Class)constraints[i];
  471. constraints[i] = new GenericParamConstraint(this, cClass);
  472. if (cClass is ClassRef) cClass.BuildMDTables(md);
  473. // Fix by CK - should be BuildTables too??
  474. if (cClass is ClassSpec) md.AddToTable(MDTable.TypeSpec, cClass);
  475. }
  476. }
  477. internal override void BuildCILInfo(CILWriter output)
  478. {
  479. for (int i = 0; i < constraints.Count; i++)
  480. {
  481. Class cClass = (Class)constraints[i];
  482. if (!cClass.isDef())
  483. {
  484. cClass.BuildCILInfo(output);
  485. }
  486. }
  487. }
  488. internal void AddConstraints(MetaDataOut md)
  489. {
  490. for (int i = 0; i < constraints.Count; i++)
  491. {
  492. md.AddToTable(MDTable.GenericParamConstraint, (GenericParamConstraint)constraints[i]);
  493. }
  494. }
  495. internal override void Write(PEWriter output)
  496. {
  497. output.Write(index);
  498. output.Write(flags);
  499. output.WriteCodedIndex(CIx.TypeOrMethodDef, parent);
  500. output.StringsIndex(nameIx);
  501. if (extraField) output.Write(kind);
  502. }
  503. }
  504. /**************************************************************************/
  505. internal class UnresolvedTypeSpec : TypeSpec
  506. {
  507. uint blobIx;
  508. internal UnresolvedTypeSpec(PEReader buff, int i)
  509. : base(0)
  510. {
  511. blobIx = buff.GetBlobIx();
  512. Row = (uint)i + 1;
  513. this.unresolved = true;
  514. }
  515. internal override void Resolve(PEReader buff)
  516. {
  517. buff.InsertInTable(MDTable.TypeSpec, Row, buff.GetBlobType(blobIx));
  518. this.unresolved = false;
  519. }
  520. }
  521. /**************************************************************************/
  522. /// <summary>
  523. /// Wrapper for Generic Parameter of TypeSpec type.
  524. /// </summary>
  525. public class GenericParTypeSpec : TypeSpec
  526. {
  527. GenericParam gPar;
  528. bool isClassPar;
  529. uint index;
  530. internal GenericParTypeSpec(GenericParam gPar)
  531. : base(gPar.GetTypeIndex())
  532. {
  533. this.gPar = gPar;
  534. }
  535. internal GenericParTypeSpec(int gpTypeIx, uint ix)
  536. : base((byte)gpTypeIx)
  537. {
  538. isClassPar = gpTypeIx == (int)ElementType.Var;
  539. index = ix;
  540. }
  541. internal GenericParam GetGenericParam(MethodDef meth)
  542. {
  543. if (gPar == null)
  544. {
  545. if (isClassPar)
  546. {
  547. ClassDef methClass = (ClassDef)meth.GetParent();
  548. gPar = methClass.GetGenericParam((int)index);
  549. }
  550. else
  551. {
  552. gPar = meth.GetGenericParam((int)index);
  553. }
  554. }
  555. return gPar;
  556. }
  557. internal override void TypeSig(MemoryStream str)
  558. {
  559. gPar.TypeSig(str);
  560. }
  561. }
  562. /**************************************************************************/
  563. /// <summary>
  564. /// The IL Array type: there are two sub-classes --
  565. /// BoundArrays, possibly multi dimensional arrays with bounds.
  566. /// ZeroBasedArrays, built-in 1-D arrays of the CLR
  567. /// </summary>
  568. public abstract class Array : TypeSpec
  569. {
  570. /// <summary>
  571. /// The element type of the array
  572. /// </summary>
  573. protected Type elemType;
  574. /*-------------------- Constructors ---------------------------------*/
  575. internal Array(Type eType, byte TypeId)
  576. : base(TypeId)
  577. {
  578. elemType = eType;
  579. tabIx = MDTable.TypeSpec;
  580. }
  581. public Type ElemType() { return elemType; }
  582. internal sealed override void BuildTables(MetaDataOut md)
  583. {
  584. if (!(elemType is ClassDef))
  585. elemType.BuildMDTables(md);
  586. }
  587. internal sealed override void BuildCILInfo(CILWriter output)
  588. {
  589. if (!(elemType is ClassDef))
  590. elemType.BuildCILInfo(output);
  591. }
  592. }
  593. /**************************************************************************/
  594. /// <summary>
  595. /// Arrays with one or more dimensions, with explicit bounds
  596. /// </summary>
  597. public class BoundArray : Array
  598. {
  599. int[] lowerBounds;
  600. int[] sizes;
  601. uint numDims;
  602. /*-------------------- Constructors ---------------------------------*/
  603. /// <summary>
  604. /// Create a new multi dimensional array type
  605. /// eg. elemType[1..5,3..10,5,,] would be
  606. /// new BoundArray(elemType,5,[1,3,0],[5,10,4])
  607. /// </summary>
  608. /// <param name="elementType">the type of the elements</param>
  609. /// <param name="dimensions">the number of dimensions</param>
  610. /// <param name="loBounds">lower bounds of dimensions</param>
  611. /// <param name="upBounds">upper bounds of dimensions</param>
  612. public BoundArray(
  613. Type elementType,
  614. int dimensions,
  615. int[] loBounds,
  616. int[] upBounds) : base(elementType, 0x14)
  617. {
  618. numDims = (uint)dimensions;
  619. lowerBounds = loBounds;
  620. if (loBounds.Length > dimensions)
  621. throw new TypeSignatureException("Array cannot have more bounds than rank");
  622. if (upBounds != null)
  623. {
  624. if (upBounds.Length > loBounds.Length)
  625. throw new TypeSignatureException("Array cannot have more upper than lower bounds");
  626. sizes = new int[upBounds.Length];
  627. for (int i = 0; i < upBounds.Length; i++)
  628. {
  629. sizes[i] = upBounds[i] - loBounds[i] + 1;
  630. }
  631. }
  632. }
  633. /// <summary>
  634. /// Create a new multi dimensional array type with low bounds
  635. /// specified but no sizes specified. C# arrays T[,] do this
  636. /// with implicit low bounds of zero, but no sizes
  637. /// </summary>
  638. /// <param name="elementType">the type of the elements</param>
  639. /// <param name="dimensions">the number of dimensions</param>
  640. /// <param name="bounds">the low bounds of the dimensions</param>
  641. public BoundArray(Type elementType, int dimensions, int[] bounds)
  642. : base(elementType, 0x14)
  643. {
  644. if (bounds.Length > dimensions)
  645. throw new TypeSignatureException("Array cannot have more bounds than rank");
  646. numDims = (uint)dimensions;
  647. lowerBounds = bounds;
  648. }
  649. /// <summary>
  650. /// Create a new multi dimensional array type
  651. /// eg. elemType[,,] would be new BoundArray(elemType,3)
  652. /// </summary>
  653. /// <param name="elementType">the type of the elements</param>
  654. /// <param name="dimensions">the number of dimensions</param>
  655. public BoundArray(Type elementType, int dimensions)
  656. : base(elementType, 0x14)
  657. {
  658. numDims = (uint)dimensions;
  659. }
  660. internal override bool SameType(Type tstType)
  661. {
  662. if (this == tstType) return true;
  663. if (!(tstType is BoundArray)) return false;
  664. BoundArray bArray = (BoundArray)tstType;
  665. if (elemType.SameType(bArray.ElemType()))
  666. return SameBounds(numDims, lowerBounds, sizes);
  667. return false;
  668. }
  669. internal bool SameBounds(uint dims, int[] lbounds, int[] sizs)
  670. {
  671. if (dims != numDims) return false;
  672. if (lowerBounds != null)
  673. {
  674. if ((lbounds == null) || (lowerBounds.Length != lbounds.Length)) return false;
  675. for (int i = 0; i < lowerBounds.Length; i++)
  676. if (lowerBounds[i] != lbounds[i]) return false;
  677. }
  678. else
  679. if (lbounds != null) return false;
  680. if (sizes != null)
  681. {
  682. if ((sizs == null) || (sizes.Length != sizs.Length)) return false;
  683. for (int i = 0; i < sizes.Length; i++)
  684. if (sizes[i] != sizs[i]) return false;
  685. }
  686. else
  687. if (sizs != null) return false;
  688. return true;
  689. }
  690. internal sealed override void TypeSig(MemoryStream str)
  691. {
  692. str.WriteByte(typeIndex);
  693. elemType.TypeSig(str);
  694. MetaDataOut.CompressNum(BlobUtil.CompressUInt(numDims), str);
  695. if ((sizes != null) && (sizes.Length > 0))
  696. {
  697. MetaDataOut.CompressNum(BlobUtil.CompressUInt((uint)sizes.Length), str);
  698. for (int i = 0; i < sizes.Length; i++)
  699. {
  700. MetaDataOut.CompressNum(BlobUtil.CompressUInt((uint)sizes[i]), str);
  701. }
  702. }
  703. else str.WriteByte(0);
  704. if ((lowerBounds != null) && (lowerBounds.Length > 0))
  705. {
  706. MetaDataOut.CompressNum(BlobUtil.CompressUInt((uint)lowerBounds.Length), str);
  707. for (int i = 0; i < lowerBounds.Length; i++)
  708. {
  709. MetaDataOut.CompressNum(BlobUtil.CompressInt(lowerBounds[i]), str);
  710. }
  711. }
  712. else str.WriteByte(0);
  713. }
  714. }
  715. /**************************************************************************/
  716. /// <summary>
  717. /// Single dimensional array with zero lower bound
  718. /// </summary>
  719. public class ZeroBasedArray : Array
  720. {
  721. /*-------------------- Constructors ---------------------------------*/
  722. /// <summary>
  723. /// Create a new array - elementType[]
  724. /// </summary>
  725. /// <param name="elementType">the type of the array elements</param>
  726. public ZeroBasedArray(Type elementType) : base(elementType, (byte)ElementType.SZArray) { }
  727. internal sealed override void TypeSig(MemoryStream str)
  728. {
  729. str.WriteByte(typeIndex);
  730. elemType.TypeSig(str);
  731. }
  732. internal override bool SameType(Type tstType)
  733. {
  734. if (this == tstType) return true;
  735. if (!(tstType is ZeroBasedArray)) return false;
  736. //return elemType == ((ZeroBasedArray)tstType).ElemType();
  737. return elemType.SameType(((ZeroBasedArray)tstType).ElemType());
  738. }
  739. internal override void WriteType(CILWriter output)
  740. {
  741. elemType.WriteType(output);
  742. output.Write("[]");
  743. }
  744. }
  745. /**************************************************************************/
  746. /// <summary>
  747. /// Descriptor for a FunctionPointer type
  748. /// </summary>
  749. ///
  750. public class MethPtrType : TypeSpec
  751. {
  752. // MethPtrType == FNPTR
  753. Method meth;
  754. MethSig mSig;
  755. /*-------------------- Constructors ---------------------------------*/
  756. /// <summary>
  757. /// Create a new function pointer type
  758. /// </summary>
  759. /// <param name="meth">the function to be referenced</param>
  760. public MethPtrType(Method meth)
  761. : base((byte)ElementType.FnPtr)
  762. {
  763. this.meth = meth;
  764. }
  765. internal MethPtrType(MethSig msig)
  766. : base((byte)ElementType.FnPtr)
  767. {
  768. mSig = msig;
  769. }
  770. internal sealed override void TypeSig(MemoryStream str)
  771. {
  772. str.WriteByte(typeIndex);
  773. if (meth == null)
  774. mSig.TypeSig(str);
  775. else
  776. meth.TypeSig(str);
  777. }
  778. internal override bool SameType(Type tstType)
  779. {
  780. if (this == tstType) return true;
  781. if (tstType is MethPtrType)
  782. {
  783. MethPtrType mpType = (MethPtrType)tstType;
  784. }
  785. return false;
  786. }
  787. internal sealed override void BuildTables(MetaDataOut md)
  788. {
  789. Type[] types = meth.GetParTypes();
  790. if (types != null)
  791. for (int i = 0; i < types.Length; i++)
  792. types[i].BuildMDTables(md);
  793. types = meth.GetOptParTypes();
  794. if (types != null)
  795. for (int i = 0; i < types.Length; i++)
  796. types[i].BuildMDTables(md);
  797. }
  798. internal sealed override void BuildCILInfo(CILWriter output)
  799. {
  800. Type[] types = meth.GetParTypes();
  801. if (types != null)
  802. for (int i = 0; i < types.Length; i++)
  803. types[i].BuildCILInfo(output);
  804. types = meth.GetOptParTypes();
  805. if (types != null)
  806. for (int i = 0; i < types.Length; i++)
  807. types[i].BuildCILInfo(output);
  808. }
  809. /* internal sealed override void BuildSignatures(MetaDataOut md) {
  810. if (sigIx == 0) {
  811. MemoryStream sig = new MemoryStream();
  812. TypeSig(sig);
  813. sigIx = md.AddToBlobHeap(sig.ToArray());
  814. }
  815. done = false;
  816. }
  817. */
  818. }
  819. /**************************************************************************/
  820. /// <summary>
  821. /// Descriptor for an pointer (type * or type &)
  822. /// </summary>
  823. public abstract class PtrType : TypeSpec
  824. {
  825. protected Type baseType;
  826. /*-------------------- Constructors ---------------------------------*/
  827. internal PtrType(Type bType, byte typeIx)
  828. : base(typeIx)
  829. {
  830. baseType = bType;
  831. }
  832. public Type GetBaseType() { return baseType; }
  833. internal sealed override void TypeSig(MemoryStream str)
  834. {
  835. str.WriteByte(typeIndex);
  836. baseType.TypeSig(str);
  837. }
  838. internal sealed override void BuildTables(MetaDataOut md)
  839. {
  840. if (!(baseType is ClassDef))
  841. baseType.BuildMDTables(md);
  842. }
  843. internal sealed override void BuildCILInfo(CILWriter output)
  844. {
  845. if (!(baseType is ClassDef))
  846. baseType.BuildCILInfo(output);
  847. }
  848. }
  849. /**************************************************************************/
  850. /// <summary>
  851. /// Descriptor for a managed pointer (type & or byref)
  852. /// </summary>
  853. public class ManagedPointer : PtrType
  854. { // <type> & (BYREF)
  855. /*-------------------- Constructors ---------------------------------*/
  856. /// <summary>
  857. /// Create new managed pointer to baseType
  858. /// </summary>
  859. /// <param name="bType">the base type of the pointer</param>
  860. public ManagedPointer(Type baseType) : base(baseType, 0x10) { }
  861. }
  862. /**************************************************************************/
  863. /// <summary>
  864. /// Descriptor for an unmanaged pointer (type *)
  865. /// </summary>
  866. public class UnmanagedPointer : PtrType
  867. { // PTR
  868. /*-------------------- Constructors ---------------------------------*/
  869. /// <summary>
  870. /// Create a new unmanaged pointer to baseType
  871. /// </summary>
  872. /// <param name="baseType">the base type of the pointer</param>
  873. public UnmanagedPointer(Type baseType) : base(baseType, 0x0F) { }
  874. }
  875. }