MDClassDefElems.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. // This Class produces entries in the TypeDef table of the MetaData
  25. // in the PE meta data.
  26. // NOTE: Entry 0 in TypeDef table is always the pseudo class <module>
  27. // which is the parent for functions and variables declared a module level
  28. /// <summary>
  29. /// The descriptor for a class defined in the IL (.class) in the current assembly/module
  30. /// </summary>
  31. ///
  32. public class ClassDef : ClassDesc
  33. {
  34. private static readonly uint HasSecurity = 0x00040000;
  35. private static readonly uint NoSecurity = 0xFFFBFFFF;
  36. private static readonly uint VisibilityMask = 0x07;
  37. private static readonly uint LayoutMask = 0x18;
  38. private static readonly uint StringFormatMask = 0x030000;
  39. //private static readonly uint fieldListIx = 0, methListIx = 1, eventListIx = 2, propListIx = 3;
  40. //private static readonly uint numListIx = 4;
  41. protected PEFile scope;
  42. uint flags;
  43. Class superType;
  44. ArrayList security;
  45. ClassLayout layout;
  46. uint extendsIx;
  47. internal ClassRef refOf;
  48. internal uint eventIx = 0, propIx = 0;
  49. ArrayList events = new ArrayList();
  50. ArrayList properties = new ArrayList();
  51. ArrayList interfaces = new ArrayList();
  52. ArrayList methodImpls = new ArrayList();
  53. //uint[] interfaceIndexes;
  54. //private string[] eventNames, propertyNames, nestedNames;
  55. //internal string[][] names = new string[numListIx][];
  56. /*-------------------- Constructors ---------------------------------*/
  57. internal ClassDef(PEFile scope, TypeAttr attrSet, string nsName, string name)
  58. : base(nsName, name)
  59. {
  60. this.scope = scope;
  61. superType = MSCorLib.mscorlib.ObjectClass;
  62. flags = (uint)attrSet;
  63. tabIx = MDTable.TypeDef;
  64. }
  65. internal ClassDef(PEReader buff, uint row, bool isMSCorLib)
  66. {
  67. flags = buff.ReadUInt32();
  68. name = buff.GetString();
  69. nameSpace = buff.GetString();
  70. extendsIx = buff.GetCodedIndex(CIx.TypeDefOrRef);
  71. fieldIx = buff.GetIndex(MDTable.Field);
  72. methodIx = buff.GetIndex(MDTable.Method);
  73. this.Row = row;
  74. tabIx = MDTable.TypeDef;
  75. if (isMSCorLib && (name == "ValueType"))
  76. typeIndex = (byte)ElementType.ValueType;
  77. }
  78. internal static void Read(PEReader buff, TableRow[] typeDefs, bool isMSCorLib)
  79. {
  80. ClassDef prevDef = null;
  81. prevDef = new ClassDef(buff, 1, isMSCorLib);
  82. typeDefs[0] = prevDef;
  83. for (int i = 1; i < typeDefs.Length; i++)
  84. {
  85. ClassDef typeDef = new ClassDef(buff, (uint)i + 1, isMSCorLib);
  86. prevDef.fieldEndIx = typeDef.fieldIx;
  87. prevDef.methodEndIx = typeDef.methodIx;
  88. prevDef = typeDef;
  89. typeDefs[i] = typeDef;
  90. }
  91. prevDef.fieldEndIx = buff.GetTableSize(MDTable.Field) + 1;
  92. prevDef.methodEndIx = buff.GetTableSize(MDTable.Method) + 1;
  93. }
  94. private static uint GetParentClassIx(uint[] enclClasses, uint[] nestClasses, uint classIx)
  95. {
  96. if (enclClasses == null) return 0;
  97. for (uint i = 0; i < enclClasses.Length; i++)
  98. {
  99. if (nestClasses[i] == classIx)
  100. return enclClasses[i];
  101. }
  102. return 0;
  103. }
  104. internal static void GetClassRefs(PEReader buff, TableRow[] typeRefs, ReferenceScope paren, uint[] parIxs)
  105. {
  106. int num = typeRefs.Length;
  107. uint[] fieldStart = new uint[num + 1], methStart = new uint[num + 1], extends = new uint[num + 1];
  108. for (int i = 0; i < num; i++)
  109. {
  110. uint flags = buff.ReadUInt32();
  111. string name = buff.GetString();
  112. string nameSpace = buff.GetString();
  113. extends[i] = buff.GetCodedIndex(CIx.TypeDefOrRef);
  114. fieldStart[i] = buff.GetIndex(MDTable.Field);
  115. methStart[i] = buff.GetIndex(MDTable.Method);
  116. //Console.WriteLine("flags = " + Hex.Int(flags));
  117. if (i == 0) // ASSERT first entry is always <Module>
  118. typeRefs[i] = paren.GetDefaultClass();
  119. else if (isPublic(flags))
  120. {
  121. if (parIxs[i] != 0)
  122. {
  123. typeRefs[i] = new NestedClassRef(paren, nameSpace, name);
  124. }
  125. else
  126. {
  127. typeRefs[i] = paren.GetExistingClass(nameSpace, name);
  128. if (typeRefs[i] == null)
  129. {
  130. typeRefs[i] = new ClassRef(paren, nameSpace, name);
  131. paren.AddToClassList((ClassRef)typeRefs[i]);
  132. }
  133. }
  134. }
  135. }
  136. fieldStart[num] = buff.GetTableSize(MDTable.Field) + 1;
  137. methStart[num] = buff.GetTableSize(MDTable.Method) + 1;
  138. // Find Nested Classes
  139. for (int i = 0; i < typeRefs.Length; i++)
  140. {
  141. if ((typeRefs[i] != null) && (typeRefs[i] is NestedClassRef))
  142. {
  143. NestedClassRef nRef = (NestedClassRef)typeRefs[i];
  144. ClassRef nPar = (ClassRef)typeRefs[parIxs[i] - 1];
  145. if (nPar == null)
  146. { // parent is private, so ignore
  147. typeRefs[i] = null;
  148. }
  149. else
  150. {
  151. nRef.SetParent(nPar);
  152. nPar.AddToClassList(nRef);
  153. }
  154. }
  155. if (typeRefs[i] != null)
  156. {
  157. if (buff.GetCodedElement(CIx.TypeDefOrRef, extends[i]) == MSCorLib.mscorlib.ValueType())
  158. ((ClassRef)typeRefs[i]).MakeValueClass();
  159. buff.SetElementPosition(MDTable.Field, fieldStart[i]);
  160. FieldDef.GetFieldRefs(buff, fieldStart[i + 1] - fieldStart[i], (ClassRef)typeRefs[i]);
  161. buff.SetElementPosition(MDTable.Method, methStart[i]);
  162. MethodDef.GetMethodRefs(buff, methStart[i + 1] - methStart[i], (ClassRef)typeRefs[i]);
  163. }
  164. }
  165. }
  166. internal override void Resolve(PEReader buff)
  167. {
  168. buff.currentClassScope = this;
  169. superType = (Class)buff.GetCodedElement(CIx.TypeDefOrRef, extendsIx);
  170. if ((superType != null) && superType.isValueType())
  171. typeIndex = (byte)ElementType.ValueType;
  172. for (int i = 0; fieldIx < fieldEndIx; i++, fieldIx++)
  173. {
  174. FieldDef field = (FieldDef)buff.GetElement(MDTable.Field, fieldIx);
  175. field.SetParent(this);
  176. fields.Add(field);
  177. }
  178. for (int i = 0; methodIx < methodEndIx; i++, methodIx++)
  179. {
  180. MethodDef meth = (MethodDef)buff.GetElement(MDTable.Method, methodIx);
  181. if (Diag.DiagOn) Console.WriteLine("Adding method " + meth.Name() + " to class " + name);
  182. meth.SetParent(this);
  183. methods.Add(meth);
  184. }
  185. buff.currentClassScope = null;
  186. }
  187. internal void ChangeRefsToDefs(ClassDef newType, ClassDef[] oldTypes)
  188. {
  189. for (int i = 0; i < oldTypes.Length; i++)
  190. {
  191. for (int j = 0; j < oldTypes[i].fields.Count; j++)
  192. ((FieldDef)oldTypes[i].fields[j]).ChangeRefsToDefs(this, oldTypes);
  193. for (int j = 0; j < oldTypes[i].methods.Count; j++)
  194. ((MethodDef)oldTypes[i].methods[j]).ChangeRefsToDefs(this, oldTypes);
  195. for (int j = 0; j < oldTypes[i].events.Count; j++)
  196. ((Event)oldTypes[i].events[j]).ChangeRefsToDefs(this, oldTypes);
  197. for (int j = 0; j < oldTypes[i].properties.Count; j++)
  198. ((Property)oldTypes[i].properties[j]).ChangeRefsToDefs(this, oldTypes);
  199. for (int j = 0; j < oldTypes[i].interfaces.Count; j++)
  200. ((ClassDef)oldTypes[i].interfaces[j]).ChangeRefsToDefs(this, oldTypes);
  201. for (int j = 0; j < oldTypes[i].methodImpls.Count; j++)
  202. ((MethodImpl)oldTypes[i].methodImpls[j]).ChangeRefsToDefs(this, oldTypes);
  203. for (int j = 0; j < oldTypes[i].nestedClasses.Count; j++)
  204. ((ClassDef)oldTypes[i].nestedClasses[j]).ChangeRefsToDefs(this, oldTypes);
  205. }
  206. }
  207. public void MergeClasses(ClassDef[] classes)
  208. {
  209. ChangeRefsToDefs(this, classes);
  210. for (int i = 0; i < classes.Length; i++)
  211. {
  212. fields.AddRange(classes[i].fields);
  213. methods.AddRange(classes[i].methods);
  214. events.AddRange(classes[i].events);
  215. properties.AddRange(classes[i].properties);
  216. interfaces.AddRange(classes[i].interfaces);
  217. methodImpls.AddRange(classes[i].methodImpls);
  218. nestedClasses.AddRange(classes[i].nestedClasses);
  219. }
  220. }
  221. /*------------------------- public set and get methods --------------------------*/
  222. /// <summary>
  223. /// Fetch the PEFile which contains this class
  224. /// </summary>
  225. /// <returns>PEFile containing this class</returns>
  226. public virtual PEFile GetScope() { return scope; }
  227. public override MetaDataElement GetParent() { return scope; }
  228. /// <summary>
  229. /// Fetch or Get the superType for this class
  230. /// </summary>
  231. public Class SuperType
  232. {
  233. get { return superType; }
  234. set
  235. {
  236. superType = value;
  237. if (value == MSCorLib.mscorlib.ValueType())
  238. typeIndex = (byte)ElementType.ValueType;
  239. else
  240. typeIndex = (byte)ElementType.Class;
  241. }
  242. }
  243. /// <summary>
  244. /// Add an attribute to the attributes of this class
  245. /// </summary>
  246. /// <param name="ta">the attribute to be added</param>
  247. public void AddAttribute(TypeAttr ta)
  248. {
  249. flags |= (uint)ta;
  250. }
  251. /// <summary>
  252. /// Set the attributes of this class
  253. /// </summary>
  254. /// <param name="ta">class attributes</param>
  255. public void SetAttribute(TypeAttr ta)
  256. {
  257. flags = (uint)ta;
  258. }
  259. /// <summary>
  260. /// Get the attributes for this class
  261. /// </summary>
  262. /// <returns></returns>
  263. public TypeAttr GetAttributes() { return (TypeAttr)flags; }
  264. public GenericParam AddGenericParam(string name)
  265. {
  266. GenericParam gp = new GenericParam(name, this, genericParams.Count);
  267. genericParams.Add(gp);
  268. return gp;
  269. }
  270. public int GetGenericParamCount()
  271. {
  272. return genericParams.Count;
  273. }
  274. public GenericParam GetGenericParam(string name)
  275. {
  276. int pos = FindGenericParam(name);
  277. if (pos == -1) return null;
  278. return (GenericParam)genericParams[pos];
  279. }
  280. public void RemoveGenericParam(string name)
  281. {
  282. int pos = FindGenericParam(name);
  283. if (pos == -1) return;
  284. DeleteGenericParam(pos);
  285. }
  286. public void RemoveGenericParam(int ix)
  287. {
  288. if (ix >= genericParams.Count) return;
  289. DeleteGenericParam(ix);
  290. }
  291. public override ClassSpec Instantiate(Type[] genTypes)
  292. {
  293. if (genTypes == null) return null;
  294. if (genericParams.Count == 0)
  295. throw new Exception("Cannot instantiate non-generic class");
  296. if (genTypes.Length != genericParams.Count)
  297. throw new Exception("Wrong number of type parameters for instantiation\nNeeded "
  298. + genericParams.Count + " but got " + genTypes.Length);
  299. return new ClassSpec(this, genTypes);
  300. }
  301. /// <summary>
  302. /// Add an interface that is implemented by this class
  303. /// </summary>
  304. /// <param name="iFace">the interface that is implemented</param>
  305. public void AddImplementedInterface(Class iFace)
  306. {
  307. interfaces.Add(new InterfaceImpl(this, iFace));
  308. //metaData.AddToTable(MDTable.InterfaceImpl,new InterfaceImpl(this,iFace));
  309. }
  310. /// <summary>
  311. /// Get the interfaces implemented by this class
  312. /// </summary>
  313. /// <returns>List of implemented interfaces</returns>
  314. public Class[] GetInterfaces()
  315. {
  316. Class[] iFaces = new Class[interfaces.Count];
  317. for (int i = 0; i < iFaces.Length; i++)
  318. {
  319. iFaces[i] = ((InterfaceImpl)interfaces[i]).TheInterface();
  320. }
  321. return iFaces;
  322. }
  323. // FIXME: need a Setter for interfaces too!
  324. public void SetInterfaces(Class[] iFaces)
  325. {
  326. interfaces = new ArrayList(iFaces.Length);
  327. for (int i = 0; i < iFaces.Length; i++)
  328. interfaces.Add(iFaces[i]);
  329. }
  330. /// <summary>
  331. /// Add a field to this class
  332. /// </summary>
  333. /// <param name="name">field name</param>
  334. /// <param name="fType">field type</param>
  335. /// <returns>a descriptor for this new field</returns>
  336. public FieldDef AddField(string name, Type fType)
  337. {
  338. FieldDef field = (FieldDef)FindField(name);
  339. if (field != null)
  340. throw new DescriptorException("Field " + field.NameString());
  341. field = new FieldDef(name, fType, this);
  342. fields.Add(field);
  343. return field;
  344. }
  345. /// <summary>
  346. /// Add a field to this class
  347. /// </summary>
  348. /// <param name="fAtts">attributes for this field</param>
  349. /// <param name="name">field name</param>
  350. /// <param name="fType">field type</param>
  351. /// <returns>a descriptor for this new field</returns>
  352. public FieldDef AddField(FieldAttr fAtts, string name, Type fType)
  353. {
  354. FieldDef field = AddField(name, fType);
  355. field.SetFieldAttr(fAtts);
  356. return field;
  357. }
  358. /// <summary>
  359. /// Add a field to this class
  360. /// </summary>
  361. /// <param name="f">Descriptor for the field to be added</param>
  362. public void AddField(FieldDef f)
  363. {
  364. FieldDef field = (FieldDef)FindField(f.Name());
  365. if (field != null)
  366. throw new DescriptorException("Field " + field.NameString());
  367. f.SetParent(this);
  368. fields.Add(f);
  369. }
  370. /// <summary>
  371. /// Get the descriptor for the field of this class named "name"
  372. /// </summary>
  373. /// <param name="name">The field name</param>
  374. /// <returns>The descriptor for field "name"</returns>
  375. public FieldDef GetField(string name)
  376. {
  377. return (FieldDef)FindField(name);
  378. }
  379. /// <summary>
  380. /// Get the fields for this class
  381. /// </summary>
  382. /// <returns>List of fields of this class</returns>
  383. public FieldDef[] GetFields()
  384. {
  385. return (FieldDef[])fields.ToArray(typeof(FieldDef));
  386. }
  387. /// <summary>
  388. /// Add a method to this class
  389. /// </summary>
  390. /// <param name="name">method name</param>
  391. /// <param name="retType">return type</param>
  392. /// <param name="pars">parameters</param>
  393. /// <returns>a descriptor for this new method</returns>
  394. public MethodDef AddMethod(string name, Type retType, Param[] pars)
  395. {
  396. System.Diagnostics.Debug.Assert(retType != null);
  397. MethSig mSig = new MethSig(name);
  398. mSig.SetParTypes(pars);
  399. MethodDef meth = (MethodDef)GetMethod(mSig);
  400. if (meth != null)
  401. throw new DescriptorException("Method " + meth.NameString());
  402. mSig.retType = retType;
  403. meth = new MethodDef(this, mSig, pars);
  404. methods.Add(meth);
  405. return meth;
  406. }
  407. /// <summary>
  408. /// Add a method to this class
  409. /// </summary>
  410. /// <param name="name">method name</param>
  411. /// <param name="genPars">generic parameters</param>
  412. /// <param name="retType">return type</param>
  413. /// <param name="pars">parameters</param>
  414. /// <returns>a descriptor for this new method</returns>
  415. public MethodDef AddMethod(string name, GenericParam[] genPars, Type retType, Param[] pars)
  416. {
  417. MethodDef meth = AddMethod(name, retType, pars);
  418. if ((genPars != null) && (genPars.Length > 0))
  419. {
  420. for (int i = 0; i < genPars.Length; i++)
  421. {
  422. genPars[i].SetMethParam(meth, i);
  423. }
  424. meth.SetGenericParams(genPars);
  425. }
  426. return meth;
  427. }
  428. /// <summary>
  429. /// Add a method to this class
  430. /// </summary>
  431. /// <param name="mAtts">attributes for this method</param>
  432. /// <param name="iAtts">implementation attributes for this method</param>
  433. /// <param name="name">method name</param>
  434. /// <param name="retType">return type</param>
  435. /// <param name="pars">parameters</param>
  436. /// <returns>a descriptor for this new method</returns>
  437. public MethodDef AddMethod(MethAttr mAtts, ImplAttr iAtts, string name,
  438. Type retType, Param[] pars)
  439. {
  440. MethodDef meth = AddMethod(name, retType, pars);
  441. meth.AddMethAttribute(mAtts);
  442. meth.AddImplAttribute(iAtts);
  443. return meth;
  444. }
  445. /// <summary>
  446. /// Add a method to this class
  447. /// </summary>
  448. /// <param name="mAtts">attributes for this method</param>
  449. /// <param name="iAtts">implementation attributes for this method</param>
  450. /// <param name="name">method name</param>
  451. /// <param name="genPars">generic parameters</param>
  452. /// <param name="retType">return type</param>
  453. /// <param name="pars">parameters</param>
  454. /// <returns>a descriptor for this new method</returns>
  455. public MethodDef AddMethod(MethAttr mAtts, ImplAttr iAtts, string name,
  456. GenericParam[] genPars, Type retType, Param[] pars)
  457. {
  458. MethodDef meth = AddMethod(name, genPars, retType, pars);
  459. meth.AddMethAttribute(mAtts);
  460. meth.AddImplAttribute(iAtts);
  461. return meth;
  462. }
  463. /// <summary>
  464. /// Add a method to this class
  465. /// </summary>
  466. /// <param name="meth">Descriptor for the method to be added</param>
  467. public void AddMethod(MethodDef meth)
  468. {
  469. MethodDef m = (MethodDef)GetMethodDesc(meth.Name(), meth.GetParTypes());
  470. if (m != null)
  471. throw new DescriptorException("Method " + m.NameString());
  472. methods.Add(meth);
  473. meth.SetParent(this);
  474. }
  475. /// <summary>
  476. /// Get the descriptor for the method "name" of this class
  477. /// </summary>
  478. /// <param name="name">The name of the method to be retrieved</param>
  479. /// <returns>The method descriptor for "name"</returns>
  480. public MethodDef GetMethod(string name)
  481. {
  482. return (MethodDef)GetMethodDesc(name);
  483. }
  484. /// <summary>
  485. /// Get the descriptor for the method called "name" with the signature "parTypes"
  486. /// </summary>
  487. /// <param name="name">The name of the method</param>
  488. /// <param name="parTypes">The signature of the method</param>
  489. /// <returns>The method descriptor for name(parTypes)</returns>
  490. public MethodDef GetMethod(string name, Type[] parTypes)
  491. {
  492. return (MethodDef)GetMethodDesc(name, parTypes);
  493. }
  494. /// <summary>
  495. /// Get all the methods of this class called "name"
  496. /// </summary>
  497. /// <param name="name">The method name</param>
  498. /// <returns>List of methods called "name"</returns>
  499. public MethodDef[] GetMethods(string name)
  500. {
  501. ArrayList meths = GetMeths(name);
  502. return (MethodDef[])meths.ToArray(typeof(MethodDef));
  503. }
  504. /// <summary>
  505. /// Get all the methods for this class
  506. /// </summary>
  507. /// <returns>List of methods for this class</returns>
  508. public MethodDef[] GetMethods()
  509. {
  510. return (MethodDef[])methods.ToArray(typeof(MethodDef));
  511. }
  512. /// <summary>
  513. /// Add an event to this class
  514. /// </summary>
  515. /// <param name="name">event name</param>
  516. /// <param name="eType">event type</param>
  517. /// <returns>a descriptor for this new event</returns>
  518. public Event AddEvent(string name, Type eType)
  519. {
  520. Event e = (Event)FindFeature(name, events);
  521. if (e != null)
  522. throw new DescriptorException("Event " + e.NameString());
  523. e = new Event(name, eType, this);
  524. events.Add(e);
  525. return e;
  526. }
  527. /// <summary>
  528. /// Get the event "name" of this class
  529. /// </summary>
  530. /// <param name="name">The event name</param>
  531. /// <returns>The event desctiptor for "name"</returns>
  532. public Event GetEvent(string name)
  533. {
  534. return (Event)FindFeature(name, events);
  535. }
  536. /// <summary>
  537. /// Get all the events of this class
  538. /// </summary>
  539. /// <returns>List of events for this class</returns>
  540. public Event[] GetEvents()
  541. {
  542. return (Event[])events.ToArray(typeof(Event));
  543. }
  544. /// <summary>
  545. /// Remove the event "name" from this class
  546. /// </summary>
  547. /// <param name="name">The name of the event to be removed</param>
  548. public void RemoveEvent(string name)
  549. {
  550. Feature ev = FindFeature(name, events);
  551. if (ev != null) events.Remove(ev);
  552. }
  553. /// <summary>
  554. /// Add a property to this class
  555. /// </summary>
  556. /// <param name="name">property name</param>
  557. /// <param name="pars">parameters</param>
  558. /// <param name="retType">return type</param>
  559. /// <returns>a descriptor for this new property</returns>
  560. public Property AddProperty(string name, Type retType, Type[] pars)
  561. {
  562. Property p = (Property)FindFeature(name, properties);
  563. if (p != null)
  564. throw new DescriptorException("Property " + p.NameString());
  565. p = new Property(name, retType, pars, this);
  566. properties.Add(p);
  567. return p;
  568. }
  569. /// <summary>
  570. /// Get the property "name" for this class
  571. /// </summary>
  572. /// <param name="name">Descriptor for the property "name"</param>
  573. /// <returns></returns>
  574. public Property GetProperty(string name)
  575. {
  576. return (Property)FindFeature(name, properties);
  577. }
  578. /// <summary>
  579. /// Get all the properties for this class
  580. /// </summary>
  581. /// <returns>List of properties for this class</returns>
  582. public Property[] GetProperties()
  583. {
  584. return (Property[])properties.ToArray(typeof(Property));
  585. }
  586. /// <summary>
  587. /// Remove the property "name" from this class
  588. /// </summary>
  589. /// <param name="name">Name of the property to be removed</param>
  590. public void RemoveProperty(string name)
  591. {
  592. Feature prop = FindFeature(name, properties);
  593. if (prop != null) properties.Remove(prop);
  594. }
  595. /// <summary>
  596. /// Add a nested class to this class
  597. /// </summary>
  598. /// <param name="attrSet">attributes for this nested class</param>
  599. /// <param name="name">nested class name</param>
  600. /// <returns>a descriptor for this new nested class</returns>
  601. public NestedClassDef AddNestedClass(TypeAttr attrSet, string name)
  602. {
  603. NestedClassDef nClass = GetNestedClass(name);
  604. if (nClass != null)
  605. throw new DescriptorException("Nested Class " + nClass.NameString());
  606. nClass = new NestedClassDef(this, attrSet, name);
  607. nestedClasses.Add(nClass);
  608. return (nClass);
  609. }
  610. /// <summary>
  611. /// Add a nested class to this class
  612. /// </summary>
  613. /// <param name="attrSet">attributes for this nested class</param>
  614. /// <param name="name">nested class name</param>
  615. /// <param name="sType">super type of this nested class</param>
  616. /// <returns>a descriptor for this new nested class</returns>
  617. public NestedClassDef AddNestedClass(TypeAttr attrSet, string name, Class sType)
  618. {
  619. NestedClassDef nClass = AddNestedClass(attrSet, name);
  620. nClass.superType = sType;
  621. return (nClass);
  622. }
  623. /// <summary>
  624. /// Get the nested class called "name"
  625. /// </summary>
  626. /// <param name="name">The name of the nested class</param>
  627. /// <returns>Descriptor for the nested class</returns>
  628. public NestedClassDef GetNestedClass(string name)
  629. {
  630. //CheckNestedClassNames(MDTable.TypeDef);
  631. return (NestedClassDef)GetNested(name);
  632. }
  633. /// <summary>
  634. /// Add layout information for this class. This class must have the
  635. /// sequential or explicit attribute.
  636. /// </summary>
  637. /// <param name="packSize">packing size (.pack)</param>
  638. /// <param name="classSize">class size (.size)</param>
  639. public void AddLayoutInfo(int packSize, int classSize)
  640. {
  641. layout = new ClassLayout(packSize, classSize, this);
  642. }
  643. /// <summary>
  644. /// Get the pack size for this class (only valid for ExplicitLayout or SequentialLayout
  645. /// </summary>
  646. /// <returns>Class pack size</returns>
  647. public int GetPackSize()
  648. {
  649. if ((layout == null) && (((flags & (uint)TypeAttr.ExplicitLayout) != 0) ||
  650. ((flags & (uint)TypeAttr.SequentialLayout) != 0)) && (buffer != null))
  651. {
  652. buffer.SetElementPosition(MDTable.ClassLayout, 0);
  653. //layout = buffer.FindParent(MDTable.ClassLayout,this);
  654. }
  655. if (layout != null) return layout.GetPack();
  656. return 0;
  657. }
  658. /// <summary>
  659. /// Get the size of this class (only valid for ExplicitLayout or SequentialLayout
  660. /// </summary>
  661. /// <returns>The size of this class</returns>
  662. public int GetClassSize()
  663. {
  664. if (layout == null) return 0;
  665. return layout.GetSize();
  666. }
  667. /// <summary>
  668. /// Get the ClassRef for this ClassDef, if there is one
  669. /// </summary>
  670. public ClassRef RefOf
  671. {
  672. get
  673. {
  674. if (refOf == null)
  675. {
  676. ModuleRef modRef = scope.refOf;
  677. if (modRef != null)
  678. refOf = modRef.GetClass(name);
  679. }
  680. return refOf;
  681. }
  682. }
  683. /// <summary>
  684. /// Make a ClassRef for this ClassDef
  685. /// </summary>
  686. /// <returns>ClassRef equivalent to this ClassDef</returns>
  687. public virtual ClassRef MakeRefOf()
  688. {
  689. if (refOf == null)
  690. {
  691. Assembly assem = scope.GetThisAssembly();
  692. ReferenceScope scopeRef;
  693. if (assem != null)
  694. scopeRef = assem.MakeRefOf();
  695. else
  696. scopeRef = scope.MakeRefOf();
  697. refOf = scopeRef.GetClass(name);
  698. if (refOf == null)
  699. {
  700. refOf = new ClassRef(scopeRef, nameSpace, name);
  701. scopeRef.AddToClassList(refOf);
  702. }
  703. refOf.defOf = this;
  704. }
  705. return refOf;
  706. }
  707. /// <summary>
  708. /// Use a method as the implementation for another method (.override)
  709. /// </summary>
  710. /// <param name="decl">the method to be overridden</param>
  711. /// <param name="body">the implementation to be used</param>
  712. public void AddMethodOverride(Method decl, Method body)
  713. {
  714. methodImpls.Add(new MethodImpl(this, decl, body));
  715. }
  716. public void AddMethodOverride(MethodImpl mImpl)
  717. {
  718. methodImpls.Add(mImpl);
  719. mImpl.SetOwner(this);
  720. }
  721. public MethodImpl[] GetMethodOverrides()
  722. {
  723. return (MethodImpl[])methodImpls.ToArray(typeof(MethodImpl));
  724. }
  725. public void RemoveMethodOverride(MethodImpl mImpl)
  726. {
  727. if (methodImpls != null)
  728. methodImpls.Remove(mImpl);
  729. }
  730. /// <summary>
  731. /// Add security to this class
  732. /// </summary>
  733. /// <param name="act">The security action</param>
  734. /// <param name="permissionSet">Permission set</param>
  735. public void AddSecurity(SecurityAction act, byte[] permissionSet)
  736. {
  737. AddSecurity(new DeclSecurity(this, act, permissionSet));
  738. // securityActions = permissionSet;
  739. }
  740. /// <summary>
  741. /// Add security to this class
  742. /// </summary>
  743. /// <param name="sec">The descriptor for the security to add to this class</param>
  744. internal void AddSecurity(DeclSecurity sec)
  745. {
  746. flags |= HasSecurity;
  747. if (security == null) security = new ArrayList();
  748. security.Add(sec);
  749. }
  750. /// <summary>
  751. /// Get the security descriptor associated with this class
  752. /// </summary>
  753. /// <returns></returns>
  754. public DeclSecurity[] GetSecurity()
  755. {
  756. if (security == null) return null;
  757. return (DeclSecurity[])security.ToArray(typeof(DeclSecurity));
  758. }
  759. /// <summary>
  760. /// Remove the security associated with this class
  761. /// </summary>
  762. public void DeleteSecurity()
  763. {
  764. flags &= NoSecurity;
  765. security = null;
  766. }
  767. //public void AddLineInfo(int row, int col) { }
  768. /*----------------------------- internal functions ------------------------------*/
  769. internal bool isPublic()
  770. {
  771. uint vis = flags & VisibilityMask;
  772. return (vis > 0) && (vis != 3) && (vis != 5);
  773. }
  774. internal static bool isPublic(uint flags)
  775. {
  776. uint vis = flags & VisibilityMask;
  777. return (vis > 0) && (vis != 3) && (vis != 5);
  778. }
  779. internal static bool isNested(uint flags)
  780. {
  781. uint vis = flags & VisibilityMask;
  782. return vis > 1;
  783. }
  784. internal override bool isDef() { return true; }
  785. private Feature FindFeature(string name, ArrayList featureList)
  786. {
  787. if (featureList == null) return null;
  788. for (int i = 0; i < featureList.Count; i++)
  789. {
  790. if (((Feature)featureList[i]).Name() == name)
  791. {
  792. return (Feature)featureList[i];
  793. }
  794. }
  795. return null;
  796. }
  797. private int FindGenericParam(string name)
  798. {
  799. for (int i = 0; i < genericParams.Count; i++)
  800. {
  801. GenericParam gp = (GenericParam)genericParams[i];
  802. if (gp.GetName() == name) return i;
  803. }
  804. return -1;
  805. }
  806. internal ClassLayout Layout
  807. {
  808. set { layout = value; }
  809. get { return layout; }
  810. }
  811. internal void SetScope(PEFile mod) { scope = mod; }
  812. internal void AddImplementedInterface(InterfaceImpl iImpl)
  813. {
  814. interfaces.Add(iImpl);
  815. }
  816. internal NestedClassDef MakeNestedClass(ClassDef parent)
  817. {
  818. NestedClassDef nClass = new NestedClassDef(parent, (TypeAttr)flags, name);
  819. ClassDef tmp = nClass;
  820. tmp.fieldIx = fieldIx;
  821. tmp.fieldEndIx = fieldEndIx;
  822. tmp.methodIx = methodIx;
  823. tmp.methodEndIx = methodEndIx;
  824. tmp.extendsIx = extendsIx;
  825. tmp.Row = Row;
  826. parent.nestedClasses.Add(nClass);
  827. return nClass;
  828. }
  829. private void ReadSecurity()
  830. {
  831. //if ((security == null) && ((flags & HasSecurity) != 0) && (buffer != null))
  832. //security = buffer.FindParent(MDTable.DeclSecurity,this);
  833. }
  834. public override void MakeSpecial()
  835. {
  836. special = true;
  837. superType = null;
  838. flags = (uint)TypeAttr.Private;
  839. }
  840. internal void AddMethodImpl(MethodImpl impl)
  841. {
  842. methodImpls.Add(impl);
  843. }
  844. internal void AddEvent(Event ev)
  845. {
  846. if (ev == null) return;
  847. ev.SetParent(this);
  848. events.Add(ev);
  849. }
  850. internal void AddProperty(Property prop)
  851. {
  852. if (prop == null) return;
  853. prop.SetParent(this);
  854. properties.Add(prop);
  855. }
  856. internal void AddToFeatureList(ArrayList list, MDTable tabIx)
  857. {
  858. if (tabIx == MDTable.Event)
  859. {
  860. events.AddRange(list);
  861. }
  862. else
  863. {
  864. properties.AddRange(list);
  865. }
  866. }
  867. // fix for Whidbey bug
  868. internal void AddGenericsToTable(MetaDataOut md)
  869. {
  870. //for (int i=0; i < methods.Count; i++) {
  871. // ((MethodDef)methods[i]).AddGenericsToTable(md);
  872. //}
  873. for (int i = 0; i < genericParams.Count; i++)
  874. {
  875. md.AddToTable(MDTable.GenericParam, (GenericParam)genericParams[i]);
  876. }
  877. }
  878. internal sealed override void BuildTables(MetaDataOut md) {
  879. // The processing of superTypes must be done either entirely
  880. // before or entirely after the processing of this TypeDef.
  881. // Otherwise the method indices will not be monotonic in table-2.
  882. if (superType != null) {
  883. superType.BuildMDTables(md);
  884. if (superType is ClassSpec) md.ConditionalAddTypeSpec(superType);
  885. }
  886. //uint startT = md.TableIndex(MDTable.TypeDef);
  887. md.AddToTable(MDTable.TypeDef, this);
  888. for (int i = 0; i < genericParams.Count; i++) {
  889. ((GenericParam)genericParams[i]).BuildMDTables(md);
  890. }
  891. nameIx = md.AddToStringsHeap(name);
  892. nameSpaceIx = md.AddToStringsHeap(nameSpace);
  893. if (security != null) {
  894. for (int i = 0; i < security.Count; i++) {
  895. ((DeclSecurity)security[i]).BuildMDTables(md);
  896. }
  897. }
  898. if (layout != null) layout.BuildMDTables(md);
  899. //uint startM = md.TableIndex(MDTable.Method);
  900. // Console.WriteLine("adding methods " + methods.Count);
  901. methodIx = md.TableIndex(MDTable.Method);
  902. for (int i = 0; i < methods.Count; i++) {
  903. ((MethodDef)methods[i]).BuildMDTables(md);
  904. }
  905. //Console.WriteLine("Building tables for " + this.TypeName());
  906. //Console.WriteLine("tIx {0}, methods {1} - {2}",
  907. // Hex.Short((short)startT),
  908. // Hex.Short((short)startM),
  909. // Hex.Short((short)md.TableIndex(MDTable.Method)));
  910. // Console.WriteLine("adding fields");
  911. fieldIx = md.TableIndex(MDTable.Field);
  912. for (int i = 0; i < fields.Count; i++) {
  913. ((FieldDef)fields[i]).BuildMDTables(md);
  914. }
  915. // Console.WriteLine("adding interfaceimpls and methodimpls");
  916. if (interfaces.Count > 0) {
  917. for (int i = 0; i < interfaces.Count; i++) {
  918. ((InterfaceImpl)interfaces[i]).BuildMDTables(md);
  919. }
  920. }
  921. if (methodImpls.Count > 0) {
  922. for (int i = 0; i < methodImpls.Count; i++) {
  923. ((MethodImpl)methodImpls[i]).BuildMDTables(md);
  924. }
  925. }
  926. // Console.WriteLine("adding events and properties");
  927. if (events.Count > 0) {
  928. new MapElem(this, md.TableIndex(MDTable.Event), MDTable.EventMap).BuildMDTables(md);
  929. for (int i = 0; i < events.Count; i++) {
  930. ((Event)events[i]).BuildMDTables(md);
  931. }
  932. }
  933. if (properties.Count > 0) {
  934. new MapElem(this, md.TableIndex(MDTable.Property), MDTable.PropertyMap).BuildMDTables(md);
  935. for (int i = 0; i < properties.Count; i++) {
  936. ((Property)properties[i]).BuildMDTables(md);
  937. }
  938. }
  939. // Console.WriteLine("Adding nested classes");
  940. if (nestedClasses.Count > 0) {
  941. for (int i = 0; i < nestedClasses.Count; i++) {
  942. ClassDef nClass = (ClassDef)nestedClasses[i];
  943. nClass.BuildMDTables(md);
  944. new MapElem(nClass, this, MDTable.NestedClass).BuildTables(md);
  945. }
  946. }
  947. // Console.WriteLine("End of building tables");
  948. }
  949. internal override void BuildCILInfo(CILWriter output)
  950. {
  951. if ((superType != null) && !(superType is ClassDef))
  952. {
  953. superType.BuildCILInfo(output);
  954. }
  955. for (int i = 0; i < genericParams.Count; i++)
  956. {
  957. ((GenericParam)genericParams[i]).BuildCILInfo(output);
  958. }
  959. if (security != null)
  960. {
  961. for (int i = 0; i < security.Count; i++)
  962. {
  963. ((DeclSecurity)security[i]).BuildCILInfo(output);
  964. }
  965. }
  966. // Console.WriteLine("Building CIL info for " + name);
  967. // Console.WriteLine("adding methods " + methods.Count);
  968. for (int i = 0; i < methods.Count; i++)
  969. {
  970. ((MethodDef)methods[i]).BuildCILInfo(output);
  971. }
  972. // Console.WriteLine("adding fields");
  973. for (int i = 0; i < fields.Count; i++)
  974. {
  975. ((FieldDef)fields[i]).BuildCILInfo(output);
  976. }
  977. // Console.WriteLine("adding interfaceimpls and methodimpls");
  978. if (interfaces.Count > 0)
  979. {
  980. for (int i = 0; i < interfaces.Count; i++)
  981. {
  982. ((InterfaceImpl)interfaces[i]).BuildCILInfo(output);
  983. }
  984. }
  985. if (methodImpls.Count > 0)
  986. {
  987. for (int i = 0; i < methodImpls.Count; i++)
  988. {
  989. ((MethodImpl)methodImpls[i]).BuildCILInfo(output);
  990. }
  991. }
  992. for (int i = 0; i < events.Count; i++)
  993. {
  994. ((Event)events[i]).BuildCILInfo(output);
  995. }
  996. for (int i = 0; i < properties.Count; i++)
  997. {
  998. ((Property)properties[i]).BuildCILInfo(output);
  999. }
  1000. // Console.WriteLine("Adding nested classes");
  1001. for (int i = 0; i < nestedClasses.Count; i++)
  1002. {
  1003. ((ClassDef)nestedClasses[i]).BuildCILInfo(output);
  1004. }
  1005. // Console.WriteLine("End of building tables");
  1006. }
  1007. internal static uint Size(MetaData md)
  1008. {
  1009. return 4 + 2 * md.StringsIndexSize() +
  1010. md.CodedIndexSize(CIx.TypeDefOrRef) +
  1011. md.TableIndexSize(MDTable.Field) +
  1012. md.TableIndexSize(MDTable.Method);
  1013. }
  1014. internal sealed override void Write(PEWriter output)
  1015. {
  1016. output.Write(flags);
  1017. output.StringsIndex(nameIx);
  1018. output.StringsIndex(nameSpaceIx);
  1019. //if (superType != null)
  1020. // Console.WriteLine("getting coded index for superType of " + name + " = " + superType.GetCodedIx(CIx.TypeDefOrRef));
  1021. output.WriteCodedIndex(CIx.TypeDefOrRef, superType);
  1022. output.WriteIndex(MDTable.Field, fieldIx);
  1023. output.WriteIndex(MDTable.Method, methodIx);
  1024. }
  1025. internal override void WriteType(CILWriter output)
  1026. {
  1027. output.Write("class ");
  1028. WriteName(output);
  1029. }
  1030. internal override void WriteName(CILWriter output)
  1031. {
  1032. if ((nameSpace == null) || (nameSpace == ""))
  1033. {
  1034. output.Write(name);
  1035. }
  1036. else
  1037. {
  1038. output.Write(nameSpace + "." + name);
  1039. }
  1040. }
  1041. private void WriteFlags(CILWriter output)
  1042. {
  1043. uint vis = flags & VisibilityMask;
  1044. switch (vis)
  1045. {
  1046. case 0: output.Write("private "); break;
  1047. case 1: output.Write("public "); break;
  1048. case 2: output.Write("nested public "); break;
  1049. case 3: output.Write("nested private "); break;
  1050. case 4: output.Write("nested family "); break;
  1051. case 5: output.Write("nested assembly "); break;
  1052. case 6: output.Write("nested famandassem "); break;
  1053. case 7: output.Write("nested famorassem "); break;
  1054. }
  1055. uint layout = flags & LayoutMask;
  1056. if (layout == 0)
  1057. {
  1058. output.Write("auto ");
  1059. }
  1060. else if (layout == (uint)TypeAttr.ExplicitLayout)
  1061. {
  1062. output.Write("explicit ");
  1063. }
  1064. else
  1065. {
  1066. output.Write("sequential ");
  1067. }
  1068. if ((flags & (uint)TypeAttr.Interface) != 0)
  1069. {
  1070. output.Write("interface ");
  1071. }
  1072. if ((flags & (uint)TypeAttr.Abstract) != 0)
  1073. {
  1074. output.Write("abstract ");
  1075. }
  1076. else if ((flags & (uint)TypeAttr.Sealed) != 0)
  1077. {
  1078. output.Write("sealed ");
  1079. }
  1080. uint strForm = flags & StringFormatMask;
  1081. if (strForm == 0)
  1082. {
  1083. output.Write("ansi ");
  1084. }
  1085. else if (strForm == (uint)TypeAttr.UnicodeClass)
  1086. {
  1087. output.Write("unicode ");
  1088. }
  1089. else
  1090. {
  1091. output.Write("autochar ");
  1092. }
  1093. if ((flags & (uint)TypeAttr.BeforeFieldInit) != 0)
  1094. {
  1095. output.Write("beforefieldinit ");
  1096. }
  1097. if ((flags & (uint)TypeAttr.Serializable) != 0)
  1098. {
  1099. output.Write("serializable ");
  1100. }
  1101. if ((flags & (uint)TypeAttr.SpecialName) != 0)
  1102. {
  1103. output.Write("specialname ");
  1104. }
  1105. if ((flags & (uint)TypeAttr.RTSpecialName) != 0)
  1106. {
  1107. output.Write("rtsspecialname ");
  1108. }
  1109. }
  1110. internal override void Write(CILWriter output)
  1111. {
  1112. output.Write(".class ");
  1113. WriteFlags(output);
  1114. if ((nameSpace != null) && (nameSpace != ""))
  1115. {
  1116. output.Write(nameSpace + ".");
  1117. }
  1118. output.WriteLine(name);
  1119. if (superType != null)
  1120. {
  1121. output.Write(" extends ");
  1122. superType.WriteName(output);
  1123. }
  1124. if (interfaces.Count > 0)
  1125. {
  1126. output.Write(" implements ");
  1127. for (int i = 0; i < interfaces.Count; i++)
  1128. {
  1129. InterfaceImpl impl = (InterfaceImpl)interfaces[i];
  1130. if (i > 0) output.Write(", ");
  1131. impl.TheInterface().WriteName(output);
  1132. }
  1133. }
  1134. output.WriteLine();
  1135. output.WriteLine("{");
  1136. for (int i = 0; i < fields.Count; i++)
  1137. {
  1138. ((Field)fields[i]).Write(output);
  1139. output.WriteLine();
  1140. }
  1141. for (int i = 0; i < methods.Count; i++)
  1142. {
  1143. ((MethodDef)methods[i]).Write(output);
  1144. output.WriteLine();
  1145. }
  1146. for (int i = 0; i < methodImpls.Count; i++)
  1147. {
  1148. ((MethodImpl)methodImpls[i]).Write(output);
  1149. output.WriteLine();
  1150. }
  1151. for (int i = 0; i < events.Count; i++)
  1152. {
  1153. ((Event)events[i]).Write(output);
  1154. output.WriteLine();
  1155. }
  1156. for (int i = 0; i < properties.Count; i++)
  1157. {
  1158. ((Property)properties[i]).Write(output);
  1159. output.WriteLine();
  1160. }
  1161. output.WriteLine("}");
  1162. output.WriteLine();
  1163. }
  1164. internal sealed override uint TypeDefOrRefToken()
  1165. {
  1166. uint cIx = Row;
  1167. cIx = cIx << 2;
  1168. return cIx;
  1169. }
  1170. internal sealed override uint GetCodedIx(CIx code)
  1171. {
  1172. switch (code)
  1173. {
  1174. case (CIx.TypeDefOrRef): return 0;
  1175. case (CIx.HasCustomAttr): return 3;
  1176. case (CIx.HasDeclSecurity): return 0;
  1177. case (CIx.TypeOrMethodDef): return 0;
  1178. }
  1179. return 0;
  1180. }
  1181. internal override string ClassName()
  1182. {
  1183. return (nameSpace + "." + name);
  1184. }
  1185. internal override string NameString()
  1186. {
  1187. string nameString = "";
  1188. if (scope != null) nameString = "[" + scope.NameString() + "]";
  1189. if ((nameSpace != null) && (nameSpace.Length > 0)) nameString += nameSpace + ".";
  1190. nameString += name;
  1191. return nameString;
  1192. }
  1193. }
  1194. /**************************************************************************/
  1195. /// <summary>
  1196. /// Descriptor for a Nested Class defined in an assembly
  1197. /// </summary>
  1198. public class NestedClassDef : ClassDef
  1199. {
  1200. ClassDef parent;
  1201. /*-------------------- Constructors ---------------------------------*/
  1202. internal NestedClassDef(ClassDef parent, TypeAttr attrSet, string name)
  1203. : base(parent.GetScope(), attrSet, "", name)
  1204. {
  1205. this.parent = parent;
  1206. }
  1207. /// <summary>
  1208. /// Fetch the PEFile which contains this class
  1209. /// </summary>
  1210. /// <returns>PEFile containing this class</returns>
  1211. public override PEFile GetScope()
  1212. {
  1213. if (scope == null)
  1214. scope = parent.GetScope();
  1215. return scope;
  1216. }
  1217. /// <summary>
  1218. /// Get the enclosing class for this nested class
  1219. /// </summary>
  1220. /// <returns>ClassDef of the enclosing class</returns>
  1221. public ClassDef GetParentClass() { return parent; }
  1222. internal void SetParent(ClassDef par) { parent = par; }
  1223. internal override string ClassName()
  1224. {
  1225. string nameString = name;
  1226. if (parent != null) nameString = parent.TypeName() + "+" + name;
  1227. return nameString;
  1228. }
  1229. /// <returns>ClassRef equivalent to this ClassDef</returns>
  1230. public override ClassRef MakeRefOf()
  1231. {
  1232. if (refOf == null)
  1233. {
  1234. ClassRef parentRef = parent.MakeRefOf();
  1235. refOf = parentRef.GetNestedClass(name);
  1236. if (refOf == null)
  1237. {
  1238. refOf = parentRef.AddNestedClass(name);
  1239. }
  1240. refOf.defOf = this;
  1241. }
  1242. return refOf;
  1243. }
  1244. }
  1245. }