MDClassDefElems.cs 49 KB

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