MDTypeElems.cs 34 KB

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