MDClassDefElems.cs 50 KB

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