/3rd_party/llvm/include/llvm/Analysis/DebugInfo.h

https://code.google.com/p/softart/ · C++ Header · 850 lines · 584 code · 132 blank · 134 comment · 67 complexity · 5ace5e7d939e2881607cd38c7a7e1fba MD5 · raw file

  1. //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines a bunch of datatypes that are useful for creating and
  11. // walking debug info in LLVM IR form. They essentially provide wrappers around
  12. // the information in the global variables that's needed when constructing the
  13. // DWARF information.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_ANALYSIS_DEBUGINFO_H
  17. #define LLVM_ANALYSIS_DEBUGINFO_H
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Support/Dwarf.h"
  22. namespace llvm {
  23. class BasicBlock;
  24. class Constant;
  25. class Function;
  26. class GlobalVariable;
  27. class Module;
  28. class Type;
  29. class Value;
  30. class DbgDeclareInst;
  31. class Instruction;
  32. class MDNode;
  33. class NamedMDNode;
  34. class LLVMContext;
  35. class raw_ostream;
  36. class DIFile;
  37. class DISubprogram;
  38. class DILexicalBlock;
  39. class DILexicalBlockFile;
  40. class DIVariable;
  41. class DIType;
  42. /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
  43. /// This should not be stored in a container, because underly MDNode may
  44. /// change in certain situations.
  45. class DIDescriptor {
  46. public:
  47. enum {
  48. FlagPrivate = 1 << 0,
  49. FlagProtected = 1 << 1,
  50. FlagFwdDecl = 1 << 2,
  51. FlagAppleBlock = 1 << 3,
  52. FlagBlockByrefStruct = 1 << 4,
  53. FlagVirtual = 1 << 5,
  54. FlagArtificial = 1 << 6,
  55. FlagExplicit = 1 << 7,
  56. FlagPrototyped = 1 << 8,
  57. FlagObjcClassComplete = 1 << 9
  58. };
  59. protected:
  60. const MDNode *DbgNode;
  61. StringRef getStringField(unsigned Elt) const;
  62. unsigned getUnsignedField(unsigned Elt) const {
  63. return (unsigned)getUInt64Field(Elt);
  64. }
  65. uint64_t getUInt64Field(unsigned Elt) const;
  66. DIDescriptor getDescriptorField(unsigned Elt) const;
  67. template <typename DescTy>
  68. DescTy getFieldAs(unsigned Elt) const {
  69. return DescTy(getDescriptorField(Elt));
  70. }
  71. GlobalVariable *getGlobalVariableField(unsigned Elt) const;
  72. Constant *getConstantField(unsigned Elt) const;
  73. Function *getFunctionField(unsigned Elt) const;
  74. public:
  75. explicit DIDescriptor() : DbgNode(0) {}
  76. explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
  77. explicit DIDescriptor(const DIFile F);
  78. explicit DIDescriptor(const DISubprogram F);
  79. explicit DIDescriptor(const DILexicalBlockFile F);
  80. explicit DIDescriptor(const DILexicalBlock F);
  81. explicit DIDescriptor(const DIVariable F);
  82. explicit DIDescriptor(const DIType F);
  83. bool Verify() const { return DbgNode != 0; }
  84. operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
  85. MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
  86. unsigned getVersion() const {
  87. return getUnsignedField(0) & LLVMDebugVersionMask;
  88. }
  89. unsigned getTag() const {
  90. return getUnsignedField(0) & ~LLVMDebugVersionMask;
  91. }
  92. /// print - print descriptor.
  93. void print(raw_ostream &OS) const;
  94. /// dump - print descriptor to dbgs() with a newline.
  95. void dump() const;
  96. bool isDerivedType() const;
  97. bool isCompositeType() const;
  98. bool isBasicType() const;
  99. bool isVariable() const;
  100. bool isSubprogram() const;
  101. bool isGlobalVariable() const;
  102. bool isScope() const;
  103. bool isFile() const;
  104. bool isCompileUnit() const;
  105. bool isNameSpace() const;
  106. bool isLexicalBlockFile() const;
  107. bool isLexicalBlock() const;
  108. bool isSubrange() const;
  109. bool isEnumerator() const;
  110. bool isType() const;
  111. bool isGlobal() const;
  112. bool isUnspecifiedParameter() const;
  113. bool isTemplateTypeParameter() const;
  114. bool isTemplateValueParameter() const;
  115. };
  116. /// DISubrange - This is used to represent ranges, for array bounds.
  117. class DISubrange : public DIDescriptor {
  118. public:
  119. explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
  120. int64_t getLo() const { return (int64_t)getUInt64Field(1); }
  121. int64_t getHi() const { return (int64_t)getUInt64Field(2); }
  122. };
  123. /// DIArray - This descriptor holds an array of descriptors.
  124. class DIArray : public DIDescriptor {
  125. public:
  126. explicit DIArray(const MDNode *N = 0)
  127. : DIDescriptor(N) {}
  128. unsigned getNumElements() const;
  129. DIDescriptor getElement(unsigned Idx) const {
  130. return getDescriptorField(Idx);
  131. }
  132. };
  133. /// DIScope - A base class for various scopes.
  134. class DIScope : public DIDescriptor {
  135. public:
  136. explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
  137. virtual ~DIScope() {}
  138. StringRef getFilename() const;
  139. StringRef getDirectory() const;
  140. };
  141. /// DICompileUnit - A wrapper for a compile unit.
  142. class DICompileUnit : public DIScope {
  143. public:
  144. explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
  145. unsigned getLanguage() const { return getUnsignedField(2); }
  146. StringRef getFilename() const { return getStringField(3); }
  147. StringRef getDirectory() const { return getStringField(4); }
  148. StringRef getProducer() const { return getStringField(5); }
  149. /// isMain - Each input file is encoded as a separate compile unit in LLVM
  150. /// debugging information output. However, many target specific tool chains
  151. /// prefer to encode only one compile unit in an object file. In this
  152. /// situation, the LLVM code generator will include debugging information
  153. /// entities in the compile unit that is marked as main compile unit. The
  154. /// code generator accepts maximum one main compile unit per module. If a
  155. /// module does not contain any main compile unit then the code generator
  156. /// will emit multiple compile units in the output object file.
  157. bool isMain() const { return getUnsignedField(6) != 0; }
  158. bool isOptimized() const { return getUnsignedField(7) != 0; }
  159. StringRef getFlags() const { return getStringField(8); }
  160. unsigned getRunTimeVersion() const { return getUnsignedField(9); }
  161. DIArray getEnumTypes() const;
  162. DIArray getRetainedTypes() const;
  163. DIArray getSubprograms() const;
  164. DIArray getGlobalVariables() const;
  165. /// Verify - Verify that a compile unit is well formed.
  166. bool Verify() const;
  167. /// print - print compile unit.
  168. void print(raw_ostream &OS) const;
  169. /// dump - print compile unit to dbgs() with a newline.
  170. void dump() const;
  171. };
  172. /// DIFile - This is a wrapper for a file.
  173. class DIFile : public DIScope {
  174. public:
  175. explicit DIFile(const MDNode *N = 0) : DIScope(N) {
  176. if (DbgNode && !isFile())
  177. DbgNode = 0;
  178. }
  179. StringRef getFilename() const { return getStringField(1); }
  180. StringRef getDirectory() const { return getStringField(2); }
  181. DICompileUnit getCompileUnit() const{
  182. assert (getVersion() <= LLVMDebugVersion10 && "Invalid CompileUnit!");
  183. return getFieldAs<DICompileUnit>(3);
  184. }
  185. };
  186. /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
  187. /// FIXME: it seems strange that this doesn't have either a reference to the
  188. /// type/precision or a file/line pair for location info.
  189. class DIEnumerator : public DIDescriptor {
  190. public:
  191. explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
  192. StringRef getName() const { return getStringField(1); }
  193. uint64_t getEnumValue() const { return getUInt64Field(2); }
  194. };
  195. /// DIType - This is a wrapper for a type.
  196. /// FIXME: Types should be factored much better so that CV qualifiers and
  197. /// others do not require a huge and empty descriptor full of zeros.
  198. class DIType : public DIScope {
  199. public:
  200. protected:
  201. // This ctor is used when the Tag has already been validated by a derived
  202. // ctor.
  203. DIType(const MDNode *N, bool, bool) : DIScope(N) {}
  204. public:
  205. /// Verify - Verify that a type descriptor is well formed.
  206. bool Verify() const;
  207. public:
  208. explicit DIType(const MDNode *N);
  209. explicit DIType() {}
  210. virtual ~DIType() {}
  211. DIScope getContext() const { return getFieldAs<DIScope>(1); }
  212. StringRef getName() const { return getStringField(2); }
  213. DICompileUnit getCompileUnit() const{
  214. assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
  215. if (getVersion() == llvm::LLVMDebugVersion7)
  216. return getFieldAs<DICompileUnit>(3);
  217. return getFieldAs<DIFile>(3).getCompileUnit();
  218. }
  219. DIFile getFile() const { return getFieldAs<DIFile>(3); }
  220. unsigned getLineNumber() const { return getUnsignedField(4); }
  221. uint64_t getSizeInBits() const { return getUInt64Field(5); }
  222. uint64_t getAlignInBits() const { return getUInt64Field(6); }
  223. // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
  224. // carry this is just plain insane.
  225. uint64_t getOffsetInBits() const { return getUInt64Field(7); }
  226. unsigned getFlags() const { return getUnsignedField(8); }
  227. bool isPrivate() const {
  228. return (getFlags() & FlagPrivate) != 0;
  229. }
  230. bool isProtected() const {
  231. return (getFlags() & FlagProtected) != 0;
  232. }
  233. bool isForwardDecl() const {
  234. return (getFlags() & FlagFwdDecl) != 0;
  235. }
  236. // isAppleBlock - Return true if this is the Apple Blocks extension.
  237. bool isAppleBlockExtension() const {
  238. return (getFlags() & FlagAppleBlock) != 0;
  239. }
  240. bool isBlockByrefStruct() const {
  241. return (getFlags() & FlagBlockByrefStruct) != 0;
  242. }
  243. bool isVirtual() const {
  244. return (getFlags() & FlagVirtual) != 0;
  245. }
  246. bool isArtificial() const {
  247. return (getFlags() & FlagArtificial) != 0;
  248. }
  249. bool isObjcClassComplete() const {
  250. return (getFlags() & FlagObjcClassComplete) != 0;
  251. }
  252. bool isValid() const {
  253. return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
  254. }
  255. StringRef getDirectory() const {
  256. if (getVersion() == llvm::LLVMDebugVersion7)
  257. return getCompileUnit().getDirectory();
  258. return getFieldAs<DIFile>(3).getDirectory();
  259. }
  260. StringRef getFilename() const {
  261. if (getVersion() == llvm::LLVMDebugVersion7)
  262. return getCompileUnit().getFilename();
  263. return getFieldAs<DIFile>(3).getFilename();
  264. }
  265. /// isUnsignedDIType - Return true if type encoding is unsigned.
  266. bool isUnsignedDIType();
  267. /// replaceAllUsesWith - Replace all uses of debug info referenced by
  268. /// this descriptor.
  269. void replaceAllUsesWith(DIDescriptor &D);
  270. void replaceAllUsesWith(MDNode *D);
  271. /// print - print type.
  272. void print(raw_ostream &OS) const;
  273. /// dump - print type to dbgs() with a newline.
  274. void dump() const;
  275. };
  276. /// DIBasicType - A basic type, like 'int' or 'float'.
  277. class DIBasicType : public DIType {
  278. public:
  279. explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
  280. unsigned getEncoding() const { return getUnsignedField(9); }
  281. /// Verify - Verify that a basic type descriptor is well formed.
  282. bool Verify() const;
  283. /// print - print basic type.
  284. void print(raw_ostream &OS) const;
  285. /// dump - print basic type to dbgs() with a newline.
  286. void dump() const;
  287. };
  288. /// DIDerivedType - A simple derived type, like a const qualified type,
  289. /// a typedef, a pointer or reference, etc.
  290. class DIDerivedType : public DIType {
  291. protected:
  292. explicit DIDerivedType(const MDNode *N, bool, bool)
  293. : DIType(N, true, true) {}
  294. public:
  295. explicit DIDerivedType(const MDNode *N = 0)
  296. : DIType(N, true, true) {}
  297. DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
  298. /// getOriginalTypeSize - If this type is derived from a base type then
  299. /// return base type size.
  300. uint64_t getOriginalTypeSize() const;
  301. StringRef getObjCPropertyName() const { return getStringField(10); }
  302. StringRef getObjCPropertyGetterName() const {
  303. return getStringField(11);
  304. }
  305. StringRef getObjCPropertySetterName() const {
  306. return getStringField(12);
  307. }
  308. bool isReadOnlyObjCProperty() {
  309. return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
  310. }
  311. bool isReadWriteObjCProperty() {
  312. return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
  313. }
  314. bool isAssignObjCProperty() {
  315. return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
  316. }
  317. bool isRetainObjCProperty() {
  318. return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
  319. }
  320. bool isCopyObjCProperty() {
  321. return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
  322. }
  323. bool isNonAtomicObjCProperty() {
  324. return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
  325. }
  326. /// Verify - Verify that a derived type descriptor is well formed.
  327. bool Verify() const;
  328. /// print - print derived type.
  329. void print(raw_ostream &OS) const;
  330. /// dump - print derived type to dbgs() with a newline.
  331. void dump() const;
  332. };
  333. /// DICompositeType - This descriptor holds a type that can refer to multiple
  334. /// other types, like a function or struct.
  335. /// FIXME: Why is this a DIDerivedType??
  336. class DICompositeType : public DIDerivedType {
  337. public:
  338. explicit DICompositeType(const MDNode *N = 0)
  339. : DIDerivedType(N, true, true) {
  340. if (N && !isCompositeType())
  341. DbgNode = 0;
  342. }
  343. DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
  344. unsigned getRunTimeLang() const { return getUnsignedField(11); }
  345. DICompositeType getContainingType() const {
  346. return getFieldAs<DICompositeType>(12);
  347. }
  348. DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
  349. /// Verify - Verify that a composite type descriptor is well formed.
  350. bool Verify() const;
  351. /// print - print composite type.
  352. void print(raw_ostream &OS) const;
  353. /// dump - print composite type to dbgs() with a newline.
  354. void dump() const;
  355. };
  356. /// DITemplateTypeParameter - This is a wrapper for template type parameter.
  357. class DITemplateTypeParameter : public DIDescriptor {
  358. public:
  359. explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
  360. DIScope getContext() const { return getFieldAs<DIScope>(1); }
  361. StringRef getName() const { return getStringField(2); }
  362. DIType getType() const { return getFieldAs<DIType>(3); }
  363. StringRef getFilename() const {
  364. return getFieldAs<DIFile>(4).getFilename();
  365. }
  366. StringRef getDirectory() const {
  367. return getFieldAs<DIFile>(4).getDirectory();
  368. }
  369. unsigned getLineNumber() const { return getUnsignedField(5); }
  370. unsigned getColumnNumber() const { return getUnsignedField(6); }
  371. };
  372. /// DITemplateValueParameter - This is a wrapper for template value parameter.
  373. class DITemplateValueParameter : public DIDescriptor {
  374. public:
  375. explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
  376. DIScope getContext() const { return getFieldAs<DIScope>(1); }
  377. StringRef getName() const { return getStringField(2); }
  378. DIType getType() const { return getFieldAs<DIType>(3); }
  379. uint64_t getValue() const { return getUInt64Field(4); }
  380. StringRef getFilename() const {
  381. return getFieldAs<DIFile>(5).getFilename();
  382. }
  383. StringRef getDirectory() const {
  384. return getFieldAs<DIFile>(5).getDirectory();
  385. }
  386. unsigned getLineNumber() const { return getUnsignedField(6); }
  387. unsigned getColumnNumber() const { return getUnsignedField(7); }
  388. };
  389. /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
  390. class DISubprogram : public DIScope {
  391. public:
  392. explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
  393. DIScope getContext() const { return getFieldAs<DIScope>(2); }
  394. StringRef getName() const { return getStringField(3); }
  395. StringRef getDisplayName() const { return getStringField(4); }
  396. StringRef getLinkageName() const { return getStringField(5); }
  397. DICompileUnit getCompileUnit() const{
  398. assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
  399. if (getVersion() == llvm::LLVMDebugVersion7)
  400. return getFieldAs<DICompileUnit>(6);
  401. return getFieldAs<DIFile>(6).getCompileUnit();
  402. }
  403. unsigned getLineNumber() const { return getUnsignedField(7); }
  404. DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
  405. /// getReturnTypeName - Subprogram return types are encoded either as
  406. /// DIType or as DICompositeType.
  407. StringRef getReturnTypeName() const {
  408. DICompositeType DCT(getFieldAs<DICompositeType>(8));
  409. if (DCT.Verify()) {
  410. DIArray A = DCT.getTypeArray();
  411. DIType T(A.getElement(0));
  412. return T.getName();
  413. }
  414. DIType T(getFieldAs<DIType>(8));
  415. return T.getName();
  416. }
  417. /// isLocalToUnit - Return true if this subprogram is local to the current
  418. /// compile unit, like 'static' in C.
  419. unsigned isLocalToUnit() const { return getUnsignedField(9); }
  420. unsigned isDefinition() const { return getUnsignedField(10); }
  421. unsigned getVirtuality() const { return getUnsignedField(11); }
  422. unsigned getVirtualIndex() const { return getUnsignedField(12); }
  423. DICompositeType getContainingType() const {
  424. return getFieldAs<DICompositeType>(13);
  425. }
  426. unsigned isArtificial() const {
  427. if (getVersion() <= llvm::LLVMDebugVersion8)
  428. return getUnsignedField(14);
  429. return (getUnsignedField(14) & FlagArtificial) != 0;
  430. }
  431. /// isPrivate - Return true if this subprogram has "private"
  432. /// access specifier.
  433. bool isPrivate() const {
  434. if (getVersion() <= llvm::LLVMDebugVersion8)
  435. return false;
  436. return (getUnsignedField(14) & FlagPrivate) != 0;
  437. }
  438. /// isProtected - Return true if this subprogram has "protected"
  439. /// access specifier.
  440. bool isProtected() const {
  441. if (getVersion() <= llvm::LLVMDebugVersion8)
  442. return false;
  443. return (getUnsignedField(14) & FlagProtected) != 0;
  444. }
  445. /// isExplicit - Return true if this subprogram is marked as explicit.
  446. bool isExplicit() const {
  447. if (getVersion() <= llvm::LLVMDebugVersion8)
  448. return false;
  449. return (getUnsignedField(14) & FlagExplicit) != 0;
  450. }
  451. /// isPrototyped - Return true if this subprogram is prototyped.
  452. bool isPrototyped() const {
  453. if (getVersion() <= llvm::LLVMDebugVersion8)
  454. return false;
  455. return (getUnsignedField(14) & FlagPrototyped) != 0;
  456. }
  457. unsigned isOptimized() const;
  458. StringRef getFilename() const {
  459. if (getVersion() == llvm::LLVMDebugVersion7)
  460. return getCompileUnit().getFilename();
  461. return getFieldAs<DIFile>(6).getFilename();
  462. }
  463. StringRef getDirectory() const {
  464. if (getVersion() == llvm::LLVMDebugVersion7)
  465. return getCompileUnit().getFilename();
  466. return getFieldAs<DIFile>(6).getDirectory();
  467. }
  468. /// Verify - Verify that a subprogram descriptor is well formed.
  469. bool Verify() const;
  470. /// print - print subprogram.
  471. void print(raw_ostream &OS) const;
  472. /// dump - print subprogram to dbgs() with a newline.
  473. void dump() const;
  474. /// describes - Return true if this subprogram provides debugging
  475. /// information for the function F.
  476. bool describes(const Function *F);
  477. Function *getFunction() const { return getFunctionField(16); }
  478. DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
  479. DISubprogram getFunctionDeclaration() const {
  480. return getFieldAs<DISubprogram>(18);
  481. }
  482. MDNode *getVariablesNodes() const;
  483. DIArray getVariables() const;
  484. };
  485. /// DIGlobalVariable - This is a wrapper for a global variable.
  486. class DIGlobalVariable : public DIDescriptor {
  487. public:
  488. explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
  489. DIScope getContext() const { return getFieldAs<DIScope>(2); }
  490. StringRef getName() const { return getStringField(3); }
  491. StringRef getDisplayName() const { return getStringField(4); }
  492. StringRef getLinkageName() const { return getStringField(5); }
  493. DICompileUnit getCompileUnit() const{
  494. assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
  495. if (getVersion() == llvm::LLVMDebugVersion7)
  496. return getFieldAs<DICompileUnit>(6);
  497. DIFile F = getFieldAs<DIFile>(6);
  498. return F.getCompileUnit();
  499. }
  500. StringRef getFilename() const {
  501. if (getVersion() <= llvm::LLVMDebugVersion10)
  502. return getContext().getFilename();
  503. return getFieldAs<DIFile>(6).getFilename();
  504. }
  505. StringRef getDirectory() const {
  506. if (getVersion() <= llvm::LLVMDebugVersion10)
  507. return getContext().getDirectory();
  508. return getFieldAs<DIFile>(6).getDirectory();
  509. }
  510. unsigned getLineNumber() const { return getUnsignedField(7); }
  511. DIType getType() const { return getFieldAs<DIType>(8); }
  512. unsigned isLocalToUnit() const { return getUnsignedField(9); }
  513. unsigned isDefinition() const { return getUnsignedField(10); }
  514. GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
  515. Constant *getConstant() const { return getConstantField(11); }
  516. /// Verify - Verify that a global variable descriptor is well formed.
  517. bool Verify() const;
  518. /// print - print global variable.
  519. void print(raw_ostream &OS) const;
  520. /// dump - print global variable to dbgs() with a newline.
  521. void dump() const;
  522. };
  523. /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
  524. /// global etc).
  525. class DIVariable : public DIDescriptor {
  526. public:
  527. explicit DIVariable(const MDNode *N = 0)
  528. : DIDescriptor(N) {}
  529. DIScope getContext() const { return getFieldAs<DIScope>(1); }
  530. StringRef getName() const { return getStringField(2); }
  531. DICompileUnit getCompileUnit() const{
  532. assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
  533. if (getVersion() == llvm::LLVMDebugVersion7)
  534. return getFieldAs<DICompileUnit>(3);
  535. DIFile F = getFieldAs<DIFile>(3);
  536. return F.getCompileUnit();
  537. }
  538. unsigned getLineNumber() const {
  539. return (getUnsignedField(4) << 8) >> 8;
  540. }
  541. unsigned getArgNumber() const {
  542. unsigned L = getUnsignedField(4);
  543. return L >> 24;
  544. }
  545. DIType getType() const { return getFieldAs<DIType>(5); }
  546. /// isArtificial - Return true if this variable is marked as "artificial".
  547. bool isArtificial() const {
  548. if (getVersion() <= llvm::LLVMDebugVersion8)
  549. return false;
  550. return (getUnsignedField(6) & FlagArtificial) != 0;
  551. }
  552. /// getInlinedAt - If this variable is inlined then return inline location.
  553. MDNode *getInlinedAt() const;
  554. /// Verify - Verify that a variable descriptor is well formed.
  555. bool Verify() const;
  556. /// HasComplexAddr - Return true if the variable has a complex address.
  557. bool hasComplexAddress() const {
  558. return getNumAddrElements() > 0;
  559. }
  560. unsigned getNumAddrElements() const;
  561. uint64_t getAddrElement(unsigned Idx) const {
  562. if (getVersion() <= llvm::LLVMDebugVersion8)
  563. return getUInt64Field(Idx+6);
  564. if (getVersion() == llvm::LLVMDebugVersion9)
  565. return getUInt64Field(Idx+7);
  566. return getUInt64Field(Idx+8);
  567. }
  568. /// isBlockByrefVariable - Return true if the variable was declared as
  569. /// a "__block" variable (Apple Blocks).
  570. bool isBlockByrefVariable() const {
  571. return getType().isBlockByrefStruct();
  572. }
  573. /// isInlinedFnArgument - Return trule if this variable provides debugging
  574. /// information for an inlined function arguments.
  575. bool isInlinedFnArgument(const Function *CurFn);
  576. /// print - print variable.
  577. void print(raw_ostream &OS) const;
  578. void printExtendedName(raw_ostream &OS) const;
  579. /// dump - print variable to dbgs() with a newline.
  580. void dump() const;
  581. };
  582. /// DILexicalBlock - This is a wrapper for a lexical block.
  583. class DILexicalBlock : public DIScope {
  584. public:
  585. explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
  586. DIScope getContext() const { return getFieldAs<DIScope>(1); }
  587. unsigned getLineNumber() const { return getUnsignedField(2); }
  588. unsigned getColumnNumber() const { return getUnsignedField(3); }
  589. StringRef getDirectory() const {
  590. StringRef dir = getFieldAs<DIFile>(4).getDirectory();
  591. return !dir.empty() ? dir : getContext().getDirectory();
  592. }
  593. StringRef getFilename() const {
  594. StringRef filename = getFieldAs<DIFile>(4).getFilename();
  595. return !filename.empty() ? filename : getContext().getFilename();
  596. }
  597. };
  598. /// DILexicalBlockFile - This is a wrapper for a lexical block with
  599. /// a filename change.
  600. class DILexicalBlockFile : public DIScope {
  601. public:
  602. explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
  603. DIScope getContext() const { return getScope().getContext(); }
  604. unsigned getLineNumber() const { return getScope().getLineNumber(); }
  605. unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
  606. StringRef getDirectory() const {
  607. StringRef dir = getFieldAs<DIFile>(2).getDirectory();
  608. return !dir.empty() ? dir : getContext().getDirectory();
  609. }
  610. StringRef getFilename() const {
  611. StringRef filename = getFieldAs<DIFile>(2).getFilename();
  612. assert(!filename.empty() && "Why'd you create this then?");
  613. return filename;
  614. }
  615. DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(1); }
  616. };
  617. /// DINameSpace - A wrapper for a C++ style name space.
  618. class DINameSpace : public DIScope {
  619. public:
  620. explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
  621. DIScope getContext() const { return getFieldAs<DIScope>(1); }
  622. StringRef getName() const { return getStringField(2); }
  623. StringRef getDirectory() const {
  624. return getFieldAs<DIFile>(3).getDirectory();
  625. }
  626. StringRef getFilename() const {
  627. return getFieldAs<DIFile>(3).getFilename();
  628. }
  629. DICompileUnit getCompileUnit() const{
  630. assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
  631. if (getVersion() == llvm::LLVMDebugVersion7)
  632. return getFieldAs<DICompileUnit>(3);
  633. return getFieldAs<DIFile>(3).getCompileUnit();
  634. }
  635. unsigned getLineNumber() const { return getUnsignedField(4); }
  636. bool Verify() const;
  637. };
  638. /// DILocation - This object holds location information. This object
  639. /// is not associated with any DWARF tag.
  640. class DILocation : public DIDescriptor {
  641. public:
  642. explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
  643. unsigned getLineNumber() const { return getUnsignedField(0); }
  644. unsigned getColumnNumber() const { return getUnsignedField(1); }
  645. DIScope getScope() const { return getFieldAs<DIScope>(2); }
  646. DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
  647. StringRef getFilename() const { return getScope().getFilename(); }
  648. StringRef getDirectory() const { return getScope().getDirectory(); }
  649. bool Verify() const;
  650. };
  651. /// getDISubprogram - Find subprogram that is enclosing this scope.
  652. DISubprogram getDISubprogram(const MDNode *Scope);
  653. /// getDICompositeType - Find underlying composite type.
  654. DICompositeType getDICompositeType(DIType T);
  655. /// isSubprogramContext - Return true if Context is either a subprogram
  656. /// or another context nested inside a subprogram.
  657. bool isSubprogramContext(const MDNode *Context);
  658. /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
  659. /// to hold function specific information.
  660. NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
  661. /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
  662. /// suitable to hold function specific information.
  663. NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
  664. /// createInlinedVariable - Create a new inlined variable based on current
  665. /// variable.
  666. /// @param DV Current Variable.
  667. /// @param InlinedScope Location at current variable is inlined.
  668. DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
  669. LLVMContext &VMContext);
  670. /// cleanseInlinedVariable - Remove inlined scope from the variable.
  671. DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
  672. class DebugInfoFinder {
  673. public:
  674. /// processModule - Process entire module and collect debug info
  675. /// anchors.
  676. void processModule(Module &M);
  677. private:
  678. /// processType - Process DIType.
  679. void processType(DIType DT);
  680. /// processLexicalBlock - Process DILexicalBlock.
  681. void processLexicalBlock(DILexicalBlock LB);
  682. /// processSubprogram - Process DISubprogram.
  683. void processSubprogram(DISubprogram SP);
  684. /// processDeclare - Process DbgDeclareInst.
  685. void processDeclare(DbgDeclareInst *DDI);
  686. /// processLocation - Process DILocation.
  687. void processLocation(DILocation Loc);
  688. /// addCompileUnit - Add compile unit into CUs.
  689. bool addCompileUnit(DICompileUnit CU);
  690. /// addGlobalVariable - Add global variable into GVs.
  691. bool addGlobalVariable(DIGlobalVariable DIG);
  692. // addSubprogram - Add subprgoram into SPs.
  693. bool addSubprogram(DISubprogram SP);
  694. /// addType - Add type into Tys.
  695. bool addType(DIType DT);
  696. public:
  697. typedef SmallVector<MDNode *, 8>::const_iterator iterator;
  698. iterator compile_unit_begin() const { return CUs.begin(); }
  699. iterator compile_unit_end() const { return CUs.end(); }
  700. iterator subprogram_begin() const { return SPs.begin(); }
  701. iterator subprogram_end() const { return SPs.end(); }
  702. iterator global_variable_begin() const { return GVs.begin(); }
  703. iterator global_variable_end() const { return GVs.end(); }
  704. iterator type_begin() const { return TYs.begin(); }
  705. iterator type_end() const { return TYs.end(); }
  706. unsigned compile_unit_count() const { return CUs.size(); }
  707. unsigned global_variable_count() const { return GVs.size(); }
  708. unsigned subprogram_count() const { return SPs.size(); }
  709. unsigned type_count() const { return TYs.size(); }
  710. private:
  711. SmallVector<MDNode *, 8> CUs; // Compile Units
  712. SmallVector<MDNode *, 8> SPs; // Subprograms
  713. SmallVector<MDNode *, 8> GVs; // Global Variables;
  714. SmallVector<MDNode *, 8> TYs; // Types
  715. SmallPtrSet<MDNode *, 64> NodesSeen;
  716. };
  717. } // end namespace llvm
  718. #endif