DebugInfoMetadata.h 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- 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. // Declarations for metadata specific to debug info.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_DEBUGINFOMETADATA_H
  14. #define LLVM_IR_DEBUGINFOMETADATA_H
  15. #include "llvm/IR/Metadata.h"
  16. #include "llvm/Support/Dwarf.h"
  17. // Helper macros for defining get() overrides.
  18. #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
  19. #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
  20. #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
  21. static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  22. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
  23. } \
  24. static CLASS *getIfExists(LLVMContext &Context, \
  25. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  26. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
  27. /* ShouldCreate */ false); \
  28. } \
  29. static CLASS *getDistinct(LLVMContext &Context, \
  30. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  31. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
  32. } \
  33. static Temp##CLASS getTemporary(LLVMContext &Context, \
  34. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  35. return Temp##CLASS( \
  36. getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
  37. }
  38. namespace llvm {
  39. /// \brief Pointer union between a subclass of DINode and MDString.
  40. ///
  41. /// \a DICompositeType can be referenced via an \a MDString unique identifier.
  42. /// This class allows some type safety in the face of that, requiring either a
  43. /// node of a particular type or an \a MDString.
  44. template <class T> class TypedDINodeRef {
  45. const Metadata *MD = nullptr;
  46. public:
  47. TypedDINodeRef() = default;
  48. TypedDINodeRef(std::nullptr_t) {}
  49. /// \brief Construct from a raw pointer.
  50. explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
  51. assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
  52. }
  53. template <class U>
  54. TypedDINodeRef(
  55. const TypedDINodeRef<U> &X,
  56. typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
  57. nullptr)
  58. : MD(X) {}
  59. operator Metadata *() const { return const_cast<Metadata *>(MD); }
  60. bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; };
  61. bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; };
  62. /// \brief Create a reference.
  63. ///
  64. /// Get a reference to \c N, using an \a MDString reference if available.
  65. static TypedDINodeRef get(const T *N);
  66. template <class MapTy> T *resolve(const MapTy &Map) const {
  67. if (!MD)
  68. return nullptr;
  69. if (auto *Typed = dyn_cast<T>(MD))
  70. return const_cast<T *>(Typed);
  71. auto *S = cast<MDString>(MD);
  72. auto I = Map.find(S);
  73. assert(I != Map.end() && "Missing identifier in type map");
  74. return cast<T>(I->second);
  75. }
  76. };
  77. typedef TypedDINodeRef<DINode> DINodeRef;
  78. typedef TypedDINodeRef<DIScope> DIScopeRef;
  79. typedef TypedDINodeRef<DIType> DITypeRef;
  80. class DITypeRefArray {
  81. const MDTuple *N = nullptr;
  82. public:
  83. DITypeRefArray(const MDTuple *N) : N(N) {}
  84. explicit operator bool() const { return get(); }
  85. explicit operator MDTuple *() const { return get(); }
  86. MDTuple *get() const { return const_cast<MDTuple *>(N); }
  87. MDTuple *operator->() const { return get(); }
  88. MDTuple &operator*() const { return *get(); }
  89. // FIXME: Fix callers and remove condition on N.
  90. unsigned size() const { return N ? N->getNumOperands() : 0u; }
  91. DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
  92. class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
  93. std::ptrdiff_t, void, DITypeRef> {
  94. MDNode::op_iterator I = nullptr;
  95. public:
  96. iterator() = default;
  97. explicit iterator(MDNode::op_iterator I) : I(I) {}
  98. DITypeRef operator*() const { return DITypeRef(*I); }
  99. iterator &operator++() {
  100. ++I;
  101. return *this;
  102. }
  103. iterator operator++(int) {
  104. iterator Temp(*this);
  105. ++I;
  106. return Temp;
  107. }
  108. bool operator==(const iterator &X) const { return I == X.I; }
  109. bool operator!=(const iterator &X) const { return I != X.I; }
  110. };
  111. // FIXME: Fix callers and remove condition on N.
  112. iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
  113. iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
  114. };
  115. /// \brief Tagged DWARF-like metadata node.
  116. ///
  117. /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
  118. /// defined in llvm/Support/Dwarf.h). Called \a DINode because it's
  119. /// potentially used for non-DWARF output.
  120. class DINode : public MDNode {
  121. friend class LLVMContextImpl;
  122. friend class MDNode;
  123. protected:
  124. DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  125. ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
  126. : MDNode(C, ID, Storage, Ops1, Ops2) {
  127. assert(Tag < 1u << 16);
  128. SubclassData16 = Tag;
  129. }
  130. ~DINode() = default;
  131. template <class Ty> Ty *getOperandAs(unsigned I) const {
  132. return cast_or_null<Ty>(getOperand(I));
  133. }
  134. StringRef getStringOperand(unsigned I) const {
  135. if (auto *S = getOperandAs<MDString>(I))
  136. return S->getString();
  137. return StringRef();
  138. }
  139. static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
  140. if (S.empty())
  141. return nullptr;
  142. return MDString::get(Context, S);
  143. }
  144. public:
  145. unsigned getTag() const { return SubclassData16; }
  146. /// \brief Debug info flags.
  147. ///
  148. /// The three accessibility flags are mutually exclusive and rolled together
  149. /// in the first two bits.
  150. enum DIFlags {
  151. #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
  152. #include "llvm/IR/DebugInfoFlags.def"
  153. FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
  154. };
  155. static unsigned getFlag(StringRef Flag);
  156. static const char *getFlagString(unsigned Flag);
  157. /// \brief Split up a flags bitfield.
  158. ///
  159. /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
  160. /// any remaining (unrecognized) bits.
  161. static unsigned splitFlags(unsigned Flags,
  162. SmallVectorImpl<unsigned> &SplitFlags);
  163. DINodeRef getRef() const { return DINodeRef::get(this); }
  164. static bool classof(const Metadata *MD) {
  165. switch (MD->getMetadataID()) {
  166. default:
  167. return false;
  168. case GenericDINodeKind:
  169. case DISubrangeKind:
  170. case DIEnumeratorKind:
  171. case DIBasicTypeKind:
  172. case DIDerivedTypeKind:
  173. case DICompositeTypeKind:
  174. case DISubroutineTypeKind:
  175. case DIFileKind:
  176. case DICompileUnitKind:
  177. case DISubprogramKind:
  178. case DILexicalBlockKind:
  179. case DILexicalBlockFileKind:
  180. case DINamespaceKind:
  181. case DITemplateTypeParameterKind:
  182. case DITemplateValueParameterKind:
  183. case DIGlobalVariableKind:
  184. case DILocalVariableKind:
  185. case DIObjCPropertyKind:
  186. case DIImportedEntityKind:
  187. case DIModuleKind:
  188. return true;
  189. }
  190. }
  191. };
  192. template <class T> struct simplify_type<const TypedDINodeRef<T>> {
  193. typedef Metadata *SimpleType;
  194. static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
  195. return MD;
  196. }
  197. };
  198. template <class T>
  199. struct simplify_type<TypedDINodeRef<T>>
  200. : simplify_type<const TypedDINodeRef<T>> {};
  201. /// \brief Generic tagged DWARF-like metadata node.
  202. ///
  203. /// An un-specialized DWARF-like metadata node. The first operand is a
  204. /// (possibly empty) null-separated \a MDString header that contains arbitrary
  205. /// fields. The remaining operands are \a dwarf_operands(), and are pointers
  206. /// to other metadata.
  207. class GenericDINode : public DINode {
  208. friend class LLVMContextImpl;
  209. friend class MDNode;
  210. GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
  211. unsigned Tag, ArrayRef<Metadata *> Ops1,
  212. ArrayRef<Metadata *> Ops2)
  213. : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
  214. setHash(Hash);
  215. }
  216. ~GenericDINode() { dropAllReferences(); }
  217. void setHash(unsigned Hash) { SubclassData32 = Hash; }
  218. void recalculateHash();
  219. static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
  220. StringRef Header, ArrayRef<Metadata *> DwarfOps,
  221. StorageType Storage, bool ShouldCreate = true) {
  222. return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
  223. DwarfOps, Storage, ShouldCreate);
  224. }
  225. static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
  226. MDString *Header, ArrayRef<Metadata *> DwarfOps,
  227. StorageType Storage, bool ShouldCreate = true);
  228. TempGenericDINode cloneImpl() const {
  229. return getTemporary(
  230. getContext(), getTag(), getHeader(),
  231. SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
  232. }
  233. public:
  234. unsigned getHash() const { return SubclassData32; }
  235. DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
  236. ArrayRef<Metadata *> DwarfOps),
  237. (Tag, Header, DwarfOps))
  238. DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
  239. ArrayRef<Metadata *> DwarfOps),
  240. (Tag, Header, DwarfOps))
  241. /// \brief Return a (temporary) clone of this.
  242. TempGenericDINode clone() const { return cloneImpl(); }
  243. unsigned getTag() const { return SubclassData16; }
  244. StringRef getHeader() const { return getStringOperand(0); }
  245. op_iterator dwarf_op_begin() const { return op_begin() + 1; }
  246. op_iterator dwarf_op_end() const { return op_end(); }
  247. op_range dwarf_operands() const {
  248. return op_range(dwarf_op_begin(), dwarf_op_end());
  249. }
  250. unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
  251. const MDOperand &getDwarfOperand(unsigned I) const {
  252. return getOperand(I + 1);
  253. }
  254. void replaceDwarfOperandWith(unsigned I, Metadata *New) {
  255. replaceOperandWith(I + 1, New);
  256. }
  257. static bool classof(const Metadata *MD) {
  258. return MD->getMetadataID() == GenericDINodeKind;
  259. }
  260. };
  261. /// \brief Array subrange.
  262. ///
  263. /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
  264. /// type.
  265. class DISubrange : public DINode {
  266. friend class LLVMContextImpl;
  267. friend class MDNode;
  268. int64_t Count;
  269. int64_t LowerBound;
  270. DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
  271. int64_t LowerBound)
  272. : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
  273. Count(Count), LowerBound(LowerBound) {}
  274. ~DISubrange() = default;
  275. static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
  276. int64_t LowerBound, StorageType Storage,
  277. bool ShouldCreate = true);
  278. TempDISubrange cloneImpl() const {
  279. return getTemporary(getContext(), getCount(), getLowerBound());
  280. }
  281. public:
  282. DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
  283. (Count, LowerBound))
  284. TempDISubrange clone() const { return cloneImpl(); }
  285. int64_t getLowerBound() const { return LowerBound; }
  286. int64_t getCount() const { return Count; }
  287. static bool classof(const Metadata *MD) {
  288. return MD->getMetadataID() == DISubrangeKind;
  289. }
  290. };
  291. /// \brief Enumeration value.
  292. ///
  293. /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
  294. /// longer creates a type cycle.
  295. class DIEnumerator : public DINode {
  296. friend class LLVMContextImpl;
  297. friend class MDNode;
  298. int64_t Value;
  299. DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
  300. ArrayRef<Metadata *> Ops)
  301. : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
  302. Value(Value) {}
  303. ~DIEnumerator() = default;
  304. static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
  305. StringRef Name, StorageType Storage,
  306. bool ShouldCreate = true) {
  307. return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
  308. ShouldCreate);
  309. }
  310. static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
  311. MDString *Name, StorageType Storage,
  312. bool ShouldCreate = true);
  313. TempDIEnumerator cloneImpl() const {
  314. return getTemporary(getContext(), getValue(), getName());
  315. }
  316. public:
  317. DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
  318. (Value, Name))
  319. DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
  320. (Value, Name))
  321. TempDIEnumerator clone() const { return cloneImpl(); }
  322. int64_t getValue() const { return Value; }
  323. StringRef getName() const { return getStringOperand(0); }
  324. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  325. static bool classof(const Metadata *MD) {
  326. return MD->getMetadataID() == DIEnumeratorKind;
  327. }
  328. };
  329. /// \brief Base class for scope-like contexts.
  330. ///
  331. /// Base class for lexical scopes and types (which are also declaration
  332. /// contexts).
  333. ///
  334. /// TODO: Separate the concepts of declaration contexts and lexical scopes.
  335. class DIScope : public DINode {
  336. protected:
  337. DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  338. ArrayRef<Metadata *> Ops)
  339. : DINode(C, ID, Storage, Tag, Ops) {}
  340. ~DIScope() = default;
  341. public:
  342. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  343. inline StringRef getFilename() const;
  344. inline StringRef getDirectory() const;
  345. StringRef getName() const;
  346. DIScopeRef getScope() const;
  347. /// \brief Return the raw underlying file.
  348. ///
  349. /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
  350. /// (it\em is the file). If \c this is an \a DIFile, we need to return \c
  351. /// this. Otherwise, return the first operand, which is where all other
  352. /// subclasses store their file pointer.
  353. Metadata *getRawFile() const {
  354. return isa<DIFile>(this) ? const_cast<DIScope *>(this)
  355. : static_cast<Metadata *>(getOperand(0));
  356. }
  357. DIScopeRef getRef() const { return DIScopeRef::get(this); }
  358. static bool classof(const Metadata *MD) {
  359. switch (MD->getMetadataID()) {
  360. default:
  361. return false;
  362. case DIBasicTypeKind:
  363. case DIDerivedTypeKind:
  364. case DICompositeTypeKind:
  365. case DISubroutineTypeKind:
  366. case DIFileKind:
  367. case DICompileUnitKind:
  368. case DISubprogramKind:
  369. case DILexicalBlockKind:
  370. case DILexicalBlockFileKind:
  371. case DINamespaceKind:
  372. case DIModuleKind:
  373. return true;
  374. }
  375. }
  376. };
  377. /// \brief File.
  378. ///
  379. /// TODO: Merge with directory/file node (including users).
  380. /// TODO: Canonicalize paths on creation.
  381. class DIFile : public DIScope {
  382. friend class LLVMContextImpl;
  383. friend class MDNode;
  384. DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
  385. : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
  386. ~DIFile() = default;
  387. static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
  388. StringRef Directory, StorageType Storage,
  389. bool ShouldCreate = true) {
  390. return getImpl(Context, getCanonicalMDString(Context, Filename),
  391. getCanonicalMDString(Context, Directory), Storage,
  392. ShouldCreate);
  393. }
  394. static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
  395. MDString *Directory, StorageType Storage,
  396. bool ShouldCreate = true);
  397. TempDIFile cloneImpl() const {
  398. return getTemporary(getContext(), getFilename(), getDirectory());
  399. }
  400. public:
  401. DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
  402. (Filename, Directory))
  403. DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
  404. (Filename, Directory))
  405. TempDIFile clone() const { return cloneImpl(); }
  406. StringRef getFilename() const { return getStringOperand(0); }
  407. StringRef getDirectory() const { return getStringOperand(1); }
  408. MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
  409. MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
  410. static bool classof(const Metadata *MD) {
  411. return MD->getMetadataID() == DIFileKind;
  412. }
  413. };
  414. StringRef DIScope::getFilename() const {
  415. if (auto *F = getFile())
  416. return F->getFilename();
  417. return "";
  418. }
  419. StringRef DIScope::getDirectory() const {
  420. if (auto *F = getFile())
  421. return F->getDirectory();
  422. return "";
  423. }
  424. /// \brief Base class for types.
  425. ///
  426. /// TODO: Remove the hardcoded name and context, since many types don't use
  427. /// them.
  428. /// TODO: Split up flags.
  429. class DIType : public DIScope {
  430. unsigned Line;
  431. unsigned Flags;
  432. uint64_t SizeInBits;
  433. uint64_t AlignInBits;
  434. uint64_t OffsetInBits;
  435. protected:
  436. DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  437. unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
  438. uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
  439. : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
  440. SizeInBits(SizeInBits), AlignInBits(AlignInBits),
  441. OffsetInBits(OffsetInBits) {}
  442. ~DIType() = default;
  443. public:
  444. TempDIType clone() const {
  445. return TempDIType(cast<DIType>(MDNode::clone().release()));
  446. }
  447. unsigned getLine() const { return Line; }
  448. uint64_t getSizeInBits() const { return SizeInBits; }
  449. uint64_t getAlignInBits() const { return AlignInBits; }
  450. uint64_t getOffsetInBits() const { return OffsetInBits; }
  451. unsigned getFlags() const { return Flags; }
  452. DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
  453. StringRef getName() const { return getStringOperand(2); }
  454. Metadata *getRawScope() const { return getOperand(1); }
  455. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  456. void setFlags(unsigned NewFlags) {
  457. assert(!isUniqued() && "Cannot set flags on uniqued nodes");
  458. Flags = NewFlags;
  459. }
  460. bool isPrivate() const {
  461. return (getFlags() & FlagAccessibility) == FlagPrivate;
  462. }
  463. bool isProtected() const {
  464. return (getFlags() & FlagAccessibility) == FlagProtected;
  465. }
  466. bool isPublic() const {
  467. return (getFlags() & FlagAccessibility) == FlagPublic;
  468. }
  469. bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
  470. bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
  471. bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
  472. bool isVirtual() const { return getFlags() & FlagVirtual; }
  473. bool isArtificial() const { return getFlags() & FlagArtificial; }
  474. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  475. bool isObjcClassComplete() const {
  476. return getFlags() & FlagObjcClassComplete;
  477. }
  478. bool isVector() const { return getFlags() & FlagVector; }
  479. bool isStaticMember() const { return getFlags() & FlagStaticMember; }
  480. bool isLValueReference() const { return getFlags() & FlagLValueReference; }
  481. bool isRValueReference() const { return getFlags() & FlagRValueReference; }
  482. DITypeRef getRef() const { return DITypeRef::get(this); }
  483. static bool classof(const Metadata *MD) {
  484. switch (MD->getMetadataID()) {
  485. default:
  486. return false;
  487. case DIBasicTypeKind:
  488. case DIDerivedTypeKind:
  489. case DICompositeTypeKind:
  490. case DISubroutineTypeKind:
  491. return true;
  492. }
  493. }
  494. };
  495. /// \brief Basic type, like 'int' or 'float'.
  496. ///
  497. /// TODO: Split out DW_TAG_unspecified_type.
  498. /// TODO: Drop unused accessors.
  499. class DIBasicType : public DIType {
  500. friend class LLVMContextImpl;
  501. friend class MDNode;
  502. unsigned Encoding;
  503. DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
  504. uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
  505. ArrayRef<Metadata *> Ops)
  506. : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
  507. 0, Ops),
  508. Encoding(Encoding) {}
  509. ~DIBasicType() = default;
  510. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  511. StringRef Name, uint64_t SizeInBits,
  512. uint64_t AlignInBits, unsigned Encoding,
  513. StorageType Storage, bool ShouldCreate = true) {
  514. return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
  515. SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
  516. }
  517. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  518. MDString *Name, uint64_t SizeInBits,
  519. uint64_t AlignInBits, unsigned Encoding,
  520. StorageType Storage, bool ShouldCreate = true);
  521. TempDIBasicType cloneImpl() const {
  522. return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
  523. getAlignInBits(), getEncoding());
  524. }
  525. public:
  526. DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
  527. (Tag, Name, 0, 0, 0))
  528. DEFINE_MDNODE_GET(DIBasicType,
  529. (unsigned Tag, StringRef Name, uint64_t SizeInBits,
  530. uint64_t AlignInBits, unsigned Encoding),
  531. (Tag, Name, SizeInBits, AlignInBits, Encoding))
  532. DEFINE_MDNODE_GET(DIBasicType,
  533. (unsigned Tag, MDString *Name, uint64_t SizeInBits,
  534. uint64_t AlignInBits, unsigned Encoding),
  535. (Tag, Name, SizeInBits, AlignInBits, Encoding))
  536. TempDIBasicType clone() const { return cloneImpl(); }
  537. unsigned getEncoding() const { return Encoding; }
  538. static bool classof(const Metadata *MD) {
  539. return MD->getMetadataID() == DIBasicTypeKind;
  540. }
  541. };
  542. /// \brief Base class for DIDerivedType and DICompositeType.
  543. ///
  544. /// TODO: Delete; they're not really related.
  545. class DIDerivedTypeBase : public DIType {
  546. protected:
  547. DIDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
  548. unsigned Tag, unsigned Line, uint64_t SizeInBits,
  549. uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
  550. ArrayRef<Metadata *> Ops)
  551. : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
  552. Flags, Ops) {}
  553. ~DIDerivedTypeBase() = default;
  554. public:
  555. DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
  556. Metadata *getRawBaseType() const { return getOperand(3); }
  557. static bool classof(const Metadata *MD) {
  558. return MD->getMetadataID() == DIDerivedTypeKind ||
  559. MD->getMetadataID() == DICompositeTypeKind ||
  560. MD->getMetadataID() == DISubroutineTypeKind;
  561. }
  562. };
  563. /// \brief Derived types.
  564. ///
  565. /// This includes qualified types, pointers, references, friends, typedefs, and
  566. /// class members.
  567. ///
  568. /// TODO: Split out members (inheritance, fields, methods, etc.).
  569. class DIDerivedType : public DIDerivedTypeBase {
  570. friend class LLVMContextImpl;
  571. friend class MDNode;
  572. DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
  573. unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
  574. uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
  575. : DIDerivedTypeBase(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
  576. AlignInBits, OffsetInBits, Flags, Ops) {}
  577. ~DIDerivedType() = default;
  578. static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
  579. StringRef Name, DIFile *File, unsigned Line,
  580. DIScopeRef Scope, DITypeRef BaseType,
  581. uint64_t SizeInBits, uint64_t AlignInBits,
  582. uint64_t OffsetInBits, unsigned Flags,
  583. Metadata *ExtraData, StorageType Storage,
  584. bool ShouldCreate = true) {
  585. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
  586. Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
  587. Flags, ExtraData, Storage, ShouldCreate);
  588. }
  589. static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
  590. MDString *Name, Metadata *File, unsigned Line,
  591. Metadata *Scope, Metadata *BaseType,
  592. uint64_t SizeInBits, uint64_t AlignInBits,
  593. uint64_t OffsetInBits, unsigned Flags,
  594. Metadata *ExtraData, StorageType Storage,
  595. bool ShouldCreate = true);
  596. TempDIDerivedType cloneImpl() const {
  597. return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
  598. getScope(), getBaseType(), getSizeInBits(),
  599. getAlignInBits(), getOffsetInBits(), getFlags(),
  600. getExtraData());
  601. }
  602. public:
  603. DEFINE_MDNODE_GET(DIDerivedType,
  604. (unsigned Tag, MDString *Name, Metadata *File,
  605. unsigned Line, Metadata *Scope, Metadata *BaseType,
  606. uint64_t SizeInBits, uint64_t AlignInBits,
  607. uint64_t OffsetInBits, unsigned Flags,
  608. Metadata *ExtraData = nullptr),
  609. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  610. AlignInBits, OffsetInBits, Flags, ExtraData))
  611. DEFINE_MDNODE_GET(DIDerivedType,
  612. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  613. DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
  614. uint64_t AlignInBits, uint64_t OffsetInBits,
  615. unsigned Flags, Metadata *ExtraData = nullptr),
  616. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  617. AlignInBits, OffsetInBits, Flags, ExtraData))
  618. TempDIDerivedType clone() const { return cloneImpl(); }
  619. /// \brief Get extra data associated with this derived type.
  620. ///
  621. /// Class type for pointer-to-members, objective-c property node for ivars,
  622. /// or global constant wrapper for static members.
  623. ///
  624. /// TODO: Separate out types that need this extra operand: pointer-to-member
  625. /// types and member fields (static members and ivars).
  626. Metadata *getExtraData() const { return getRawExtraData(); }
  627. Metadata *getRawExtraData() const { return getOperand(4); }
  628. /// \brief Get casted version of extra data.
  629. /// @{
  630. DITypeRef getClassType() const {
  631. assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
  632. return DITypeRef(getExtraData());
  633. }
  634. DIObjCProperty *getObjCProperty() const {
  635. return dyn_cast_or_null<DIObjCProperty>(getExtraData());
  636. }
  637. Constant *getConstant() const {
  638. assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
  639. if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
  640. return C->getValue();
  641. return nullptr;
  642. }
  643. /// @}
  644. static bool classof(const Metadata *MD) {
  645. return MD->getMetadataID() == DIDerivedTypeKind;
  646. }
  647. };
  648. /// \brief Base class for DICompositeType and DISubroutineType.
  649. ///
  650. /// TODO: Delete; they're not really related.
  651. class DICompositeTypeBase : public DIDerivedTypeBase {
  652. unsigned RuntimeLang;
  653. protected:
  654. DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
  655. unsigned Tag, unsigned Line, unsigned RuntimeLang,
  656. uint64_t SizeInBits, uint64_t AlignInBits,
  657. uint64_t OffsetInBits, unsigned Flags,
  658. ArrayRef<Metadata *> Ops)
  659. : DIDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
  660. OffsetInBits, Flags, Ops),
  661. RuntimeLang(RuntimeLang) {}
  662. ~DICompositeTypeBase() = default;
  663. public:
  664. /// \brief Get the elements of the composite type.
  665. ///
  666. /// \note Calling this is only valid for \a DICompositeType. This assertion
  667. /// can be removed once \a DISubroutineType has been separated from
  668. /// "composite types".
  669. DINodeArray getElements() const {
  670. assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
  671. return cast_or_null<MDTuple>(getRawElements());
  672. }
  673. DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
  674. DITemplateParameterArray getTemplateParams() const {
  675. return cast_or_null<MDTuple>(getRawTemplateParams());
  676. }
  677. StringRef getIdentifier() const { return getStringOperand(7); }
  678. unsigned getRuntimeLang() const { return RuntimeLang; }
  679. Metadata *getRawElements() const { return getOperand(4); }
  680. Metadata *getRawVTableHolder() const { return getOperand(5); }
  681. Metadata *getRawTemplateParams() const { return getOperand(6); }
  682. MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
  683. /// \brief Replace operands.
  684. ///
  685. /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
  686. /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
  687. /// of its movement if necessary.
  688. /// @{
  689. void replaceElements(DINodeArray Elements) {
  690. #ifndef NDEBUG
  691. for (DINode *Op : getElements())
  692. assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
  693. "Lost a member during member list replacement");
  694. #endif
  695. replaceOperandWith(4, Elements.get());
  696. }
  697. void replaceVTableHolder(DITypeRef VTableHolder) {
  698. replaceOperandWith(5, VTableHolder);
  699. }
  700. void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
  701. replaceOperandWith(6, TemplateParams.get());
  702. }
  703. /// @}
  704. static bool classof(const Metadata *MD) {
  705. return MD->getMetadataID() == DICompositeTypeKind ||
  706. MD->getMetadataID() == DISubroutineTypeKind;
  707. }
  708. };
  709. /// \brief Composite types.
  710. ///
  711. /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
  712. /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
  713. class DICompositeType : public DICompositeTypeBase {
  714. friend class LLVMContextImpl;
  715. friend class MDNode;
  716. DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
  717. unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
  718. uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
  719. ArrayRef<Metadata *> Ops)
  720. : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
  721. RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
  722. Flags, Ops) {}
  723. ~DICompositeType() = default;
  724. static DICompositeType *
  725. getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
  726. unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
  727. uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
  728. uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
  729. DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
  730. StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
  731. return getImpl(
  732. Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
  733. BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
  734. RuntimeLang, VTableHolder, TemplateParams.get(),
  735. getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
  736. }
  737. static DICompositeType *
  738. getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
  739. unsigned Line, Metadata *Scope, Metadata *BaseType,
  740. uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
  741. unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
  742. Metadata *VTableHolder, Metadata *TemplateParams,
  743. MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
  744. TempDICompositeType cloneImpl() const {
  745. return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
  746. getScope(), getBaseType(), getSizeInBits(),
  747. getAlignInBits(), getOffsetInBits(), getFlags(),
  748. getElements(), getRuntimeLang(), getVTableHolder(),
  749. getTemplateParams(), getIdentifier());
  750. }
  751. public:
  752. DEFINE_MDNODE_GET(DICompositeType,
  753. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  754. DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
  755. uint64_t AlignInBits, uint64_t OffsetInBits,
  756. unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
  757. DITypeRef VTableHolder,
  758. DITemplateParameterArray TemplateParams = nullptr,
  759. StringRef Identifier = ""),
  760. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  761. AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
  762. VTableHolder, TemplateParams, Identifier))
  763. DEFINE_MDNODE_GET(DICompositeType,
  764. (unsigned Tag, MDString *Name, Metadata *File,
  765. unsigned Line, Metadata *Scope, Metadata *BaseType,
  766. uint64_t SizeInBits, uint64_t AlignInBits,
  767. uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
  768. unsigned RuntimeLang, Metadata *VTableHolder,
  769. Metadata *TemplateParams = nullptr,
  770. MDString *Identifier = nullptr),
  771. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  772. AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
  773. VTableHolder, TemplateParams, Identifier))
  774. TempDICompositeType clone() const { return cloneImpl(); }
  775. static bool classof(const Metadata *MD) {
  776. return MD->getMetadataID() == DICompositeTypeKind;
  777. }
  778. };
  779. template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
  780. if (N)
  781. if (auto *Composite = dyn_cast<DICompositeType>(N))
  782. if (auto *S = Composite->getRawIdentifier())
  783. return TypedDINodeRef<T>(S);
  784. return TypedDINodeRef<T>(N);
  785. }
  786. /// \brief Type array for a subprogram.
  787. ///
  788. /// TODO: Detach from CompositeType, and fold the array of types in directly
  789. /// as operands.
  790. class DISubroutineType : public DICompositeTypeBase {
  791. friend class LLVMContextImpl;
  792. friend class MDNode;
  793. DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
  794. ArrayRef<Metadata *> Ops)
  795. : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
  796. dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
  797. Ops) {}
  798. ~DISubroutineType() = default;
  799. static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
  800. DITypeRefArray TypeArray,
  801. StorageType Storage,
  802. bool ShouldCreate = true) {
  803. return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
  804. }
  805. static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
  806. Metadata *TypeArray, StorageType Storage,
  807. bool ShouldCreate = true);
  808. TempDISubroutineType cloneImpl() const {
  809. return getTemporary(getContext(), getFlags(), getTypeArray());
  810. }
  811. public:
  812. DEFINE_MDNODE_GET(DISubroutineType,
  813. (unsigned Flags, DITypeRefArray TypeArray),
  814. (Flags, TypeArray))
  815. DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
  816. (Flags, TypeArray))
  817. TempDISubroutineType clone() const { return cloneImpl(); }
  818. DITypeRefArray getTypeArray() const {
  819. return cast_or_null<MDTuple>(getRawTypeArray());
  820. }
  821. Metadata *getRawTypeArray() const { return getRawElements(); }
  822. static bool classof(const Metadata *MD) {
  823. return MD->getMetadataID() == DISubroutineTypeKind;
  824. }
  825. };
  826. /// \brief Compile unit.
  827. class DICompileUnit : public DIScope {
  828. friend class LLVMContextImpl;
  829. friend class MDNode;
  830. unsigned SourceLanguage;
  831. bool IsOptimized;
  832. unsigned RuntimeVersion;
  833. unsigned EmissionKind;
  834. uint64_t DWOId;
  835. DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
  836. bool IsOptimized, unsigned RuntimeVersion,
  837. unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
  838. : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
  839. SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
  840. RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
  841. DWOId(DWOId) {}
  842. ~DICompileUnit() = default;
  843. static DICompileUnit *
  844. getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
  845. StringRef Producer, bool IsOptimized, StringRef Flags,
  846. unsigned RuntimeVersion, StringRef SplitDebugFilename,
  847. unsigned EmissionKind, DICompositeTypeArray EnumTypes,
  848. DITypeArray RetainedTypes, DISubprogramArray Subprograms,
  849. DIGlobalVariableArray GlobalVariables,
  850. DIImportedEntityArray ImportedEntities, uint64_t DWOId,
  851. StorageType Storage, bool ShouldCreate = true) {
  852. return getImpl(Context, SourceLanguage, File,
  853. getCanonicalMDString(Context, Producer), IsOptimized,
  854. getCanonicalMDString(Context, Flags), RuntimeVersion,
  855. getCanonicalMDString(Context, SplitDebugFilename),
  856. EmissionKind, EnumTypes.get(), RetainedTypes.get(),
  857. Subprograms.get(), GlobalVariables.get(),
  858. ImportedEntities.get(), DWOId, Storage, ShouldCreate);
  859. }
  860. static DICompileUnit *
  861. getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
  862. MDString *Producer, bool IsOptimized, MDString *Flags,
  863. unsigned RuntimeVersion, MDString *SplitDebugFilename,
  864. unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
  865. Metadata *Subprograms, Metadata *GlobalVariables,
  866. Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
  867. bool ShouldCreate = true);
  868. TempDICompileUnit cloneImpl() const {
  869. return getTemporary(
  870. getContext(), getSourceLanguage(), getFile(), getProducer(),
  871. isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
  872. getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
  873. getGlobalVariables(), getImportedEntities(), DWOId);
  874. }
  875. public:
  876. DEFINE_MDNODE_GET(DICompileUnit,
  877. (unsigned SourceLanguage, DIFile *File, StringRef Producer,
  878. bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
  879. StringRef SplitDebugFilename, unsigned EmissionKind,
  880. DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
  881. DISubprogramArray Subprograms,
  882. DIGlobalVariableArray GlobalVariables,
  883. DIImportedEntityArray ImportedEntities, uint64_t DWOId),
  884. (SourceLanguage, File, Producer, IsOptimized, Flags,
  885. RuntimeVersion, SplitDebugFilename, EmissionKind,
  886. EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
  887. ImportedEntities, DWOId))
  888. DEFINE_MDNODE_GET(
  889. DICompileUnit,
  890. (unsigned SourceLanguage, Metadata *File, MDString *Producer,
  891. bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
  892. MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
  893. Metadata *RetainedTypes, Metadata *Subprograms,
  894. Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
  895. (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
  896. SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
  897. GlobalVariables, ImportedEntities, DWOId))
  898. TempDICompileUnit clone() const { return cloneImpl(); }
  899. unsigned getSourceLanguage() const { return SourceLanguage; }
  900. bool isOptimized() const { return IsOptimized; }
  901. unsigned getRuntimeVersion() const { return RuntimeVersion; }
  902. unsigned getEmissionKind() const { return EmissionKind; }
  903. StringRef getProducer() const { return getStringOperand(1); }
  904. StringRef getFlags() const { return getStringOperand(2); }
  905. StringRef getSplitDebugFilename() const { return getStringOperand(3); }
  906. DICompositeTypeArray getEnumTypes() const {
  907. return cast_or_null<MDTuple>(getRawEnumTypes());
  908. }
  909. DITypeArray getRetainedTypes() const {
  910. return cast_or_null<MDTuple>(getRawRetainedTypes());
  911. }
  912. DISubprogramArray getSubprograms() const {
  913. return cast_or_null<MDTuple>(getRawSubprograms());
  914. }
  915. DIGlobalVariableArray getGlobalVariables() const {
  916. return cast_or_null<MDTuple>(getRawGlobalVariables());
  917. }
  918. DIImportedEntityArray getImportedEntities() const {
  919. return cast_or_null<MDTuple>(getRawImportedEntities());
  920. }
  921. unsigned getDWOId() const { return DWOId; }
  922. MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
  923. MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
  924. MDString *getRawSplitDebugFilename() const {
  925. return getOperandAs<MDString>(3);
  926. }
  927. Metadata *getRawEnumTypes() const { return getOperand(4); }
  928. Metadata *getRawRetainedTypes() const { return getOperand(5); }
  929. Metadata *getRawSubprograms() const { return getOperand(6); }
  930. Metadata *getRawGlobalVariables() const { return getOperand(7); }
  931. Metadata *getRawImportedEntities() const { return getOperand(8); }
  932. /// \brief Replace arrays.
  933. ///
  934. /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
  935. /// deleted on a uniquing collision. In practice, uniquing collisions on \a
  936. /// DICompileUnit should be fairly rare.
  937. /// @{
  938. void replaceEnumTypes(DICompositeTypeArray N) {
  939. replaceOperandWith(4, N.get());
  940. }
  941. void replaceRetainedTypes(DITypeArray N) {
  942. replaceOperandWith(5, N.get());
  943. }
  944. void replaceSubprograms(DISubprogramArray N) {
  945. replaceOperandWith(6, N.get());
  946. }
  947. void replaceGlobalVariables(DIGlobalVariableArray N) {
  948. replaceOperandWith(7, N.get());
  949. }
  950. void replaceImportedEntities(DIImportedEntityArray N) {
  951. replaceOperandWith(8, N.get());
  952. }
  953. /// @}
  954. static bool classof(const Metadata *MD) {
  955. return MD->getMetadataID() == DICompileUnitKind;
  956. }
  957. };
  958. /// \brief A scope for locals.
  959. ///
  960. /// A legal scope for lexical blocks, local variables, and debug info
  961. /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
  962. /// DILexicalBlockFile.
  963. class DILocalScope : public DIScope {
  964. protected:
  965. DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  966. ArrayRef<Metadata *> Ops)
  967. : DIScope(C, ID, Storage, Tag, Ops) {}
  968. ~DILocalScope() = default;
  969. public:
  970. /// \brief Get the subprogram for this scope.
  971. ///
  972. /// Return this if it's an \a DISubprogram; otherwise, look up the scope
  973. /// chain.
  974. DISubprogram *getSubprogram() const;
  975. static bool classof(const Metadata *MD) {
  976. return MD->getMetadataID() == DISubprogramKind ||
  977. MD->getMetadataID() == DILexicalBlockKind ||
  978. MD->getMetadataID() == DILexicalBlockFileKind;
  979. }
  980. };
  981. /// \brief Debug location.
  982. ///
  983. /// A debug location in source code, used for debug info and otherwise.
  984. class DILocation : public MDNode {
  985. friend class LLVMContextImpl;
  986. friend class MDNode;
  987. DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
  988. unsigned Column, ArrayRef<Metadata *> MDs);
  989. ~DILocation() { dropAllReferences(); }
  990. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  991. unsigned Column, Metadata *Scope,
  992. Metadata *InlinedAt, StorageType Storage,
  993. bool ShouldCreate = true);
  994. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  995. unsigned Column, DILocalScope *Scope,
  996. DILocation *InlinedAt, StorageType Storage,
  997. bool ShouldCreate = true) {
  998. return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
  999. static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
  1000. }
  1001. TempDILocation cloneImpl() const {
  1002. return getTemporary(getContext(), getLine(), getColumn(), getScope(),
  1003. getInlinedAt());
  1004. }
  1005. // Disallow replacing operands.
  1006. void replaceOperandWith(unsigned I, Metadata *New) = delete;
  1007. public:
  1008. DEFINE_MDNODE_GET(DILocation,
  1009. (unsigned Line, unsigned Column, Metadata *Scope,
  1010. Metadata *InlinedAt = nullptr),
  1011. (Line, Column, Scope, InlinedAt))
  1012. DEFINE_MDNODE_GET(DILocation,
  1013. (unsigned Line, unsigned Column, DILocalScope *Scope,
  1014. DILocation *InlinedAt = nullptr),
  1015. (Line, Column, Scope, InlinedAt))
  1016. /// \brief Return a (temporary) clone of this.
  1017. TempDILocation clone() const { return cloneImpl(); }
  1018. unsigned getLine() const { return SubclassData32; }
  1019. unsigned getColumn() const { return SubclassData16; }
  1020. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1021. DILocation *getInlinedAt() const {
  1022. return cast_or_null<DILocation>(getRawInlinedAt());
  1023. }
  1024. DIFile *getFile() const { return getScope()->getFile(); }
  1025. StringRef getFilename() const { return getScope()->getFilename(); }
  1026. StringRef getDirectory() const { return getScope()->getDirectory(); }
  1027. /// \brief Get the scope where this is inlined.
  1028. ///
  1029. /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
  1030. /// location.
  1031. DILocalScope *getInlinedAtScope() const {
  1032. if (auto *IA = getInlinedAt())
  1033. return IA->getInlinedAtScope();
  1034. return getScope();
  1035. }
  1036. /// \brief Check whether this can be discriminated from another location.
  1037. ///
  1038. /// Check \c this can be discriminated from \c RHS in a linetable entry.
  1039. /// Scope and inlined-at chains are not recorded in the linetable, so they
  1040. /// cannot be used to distinguish basic blocks.
  1041. ///
  1042. /// The current implementation is weaker than it should be, since it just
  1043. /// checks filename and line.
  1044. ///
  1045. /// FIXME: Add a check for getDiscriminator().
  1046. /// FIXME: Add a check for getColumn().
  1047. /// FIXME: Change the getFilename() check to getFile() (or add one for
  1048. /// getDirectory()).
  1049. bool canDiscriminate(const DILocation &RHS) const {
  1050. return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
  1051. }
  1052. /// \brief Get the DWARF discriminator.
  1053. ///
  1054. /// DWARF discriminators distinguish identical file locations between
  1055. /// instructions that are on different basic blocks.
  1056. inline unsigned getDiscriminator() const;
  1057. /// \brief Compute new discriminator in the given context.
  1058. ///
  1059. /// This modifies the \a LLVMContext that \c this is in to increment the next
  1060. /// discriminator for \c this's line/filename combination.
  1061. ///
  1062. /// FIXME: Delete this. See comments in implementation and at the only call
  1063. /// site in \a AddDiscriminators::runOnFunction().
  1064. unsigned computeNewDiscriminator() const;
  1065. Metadata *getRawScope() const { return getOperand(0); }
  1066. Metadata *getRawInlinedAt() const {
  1067. if (getNumOperands() == 2)
  1068. return getOperand(1);
  1069. return nullptr;
  1070. }
  1071. static bool classof(const Metadata *MD) {
  1072. return MD->getMetadataID() == DILocationKind;
  1073. }
  1074. };
  1075. /// \brief Subprogram description.
  1076. ///
  1077. /// TODO: Remove DisplayName. It's always equal to Name.
  1078. /// TODO: Split up flags.
  1079. class DISubprogram : public DILocalScope {
  1080. friend class LLVMContextImpl;
  1081. friend class MDNode;
  1082. unsigned Line;
  1083. unsigned ScopeLine;
  1084. unsigned Virtuality;
  1085. unsigned VirtualIndex;
  1086. unsigned Flags;
  1087. bool IsLocalToUnit;
  1088. bool IsDefinition;
  1089. bool IsOptimized;
  1090. DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
  1091. unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
  1092. unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
  1093. bool IsOptimized, ArrayRef<Metadata *> Ops)
  1094. : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
  1095. Ops),
  1096. Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
  1097. VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
  1098. IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
  1099. ~DISubprogram() = default;
  1100. static DISubprogram *
  1101. getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
  1102. StringRef LinkageName, DIFile *File, unsigned Line,
  1103. DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
  1104. unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
  1105. unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
  1106. Constant *Function, DITemplateParameterArray TemplateParams,
  1107. DISubprogram *Declaration, DILocalVariableArray Variables,
  1108. StorageType Storage, bool ShouldCreate = true) {
  1109. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1110. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  1111. IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
  1112. Virtuality, VirtualIndex, Flags, IsOptimized,
  1113. Function ? ConstantAsMetadata::get(Function) : nullptr,
  1114. TemplateParams.get(), Declaration, Variables.get(), Storage,
  1115. ShouldCreate);
  1116. }
  1117. static DISubprogram *
  1118. getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  1119. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  1120. bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
  1121. Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
  1122. unsigned Flags, bool IsOptimized, Metadata *Function,
  1123. Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
  1124. StorageType Storage, bool ShouldCreate = true);
  1125. TempDISubprogram cloneImpl() const {
  1126. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  1127. getFile(), getLine(), getType(), isLocalToUnit(),
  1128. isDefinition(), getScopeLine(), getContainingType(),
  1129. getVirtuality(), getVirtualIndex(), getFlags(),
  1130. isOptimized(), getFunctionConstant(),
  1131. getTemplateParams(), getDeclaration(), getVariables());
  1132. }
  1133. public:
  1134. DEFINE_MDNODE_GET(DISubprogram,
  1135. (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
  1136. DIFile *File, unsigned Line, DISubroutineType *Type,
  1137. bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
  1138. DITypeRef ContainingType, unsigned Virtuality,
  1139. unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
  1140. Constant *Function = nullptr,
  1141. DITemplateParameterArray TemplateParams = nullptr,
  1142. DISubprogram *Declaration = nullptr,
  1143. DILocalVariableArray Variables = nullptr),
  1144. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1145. IsDefinition, ScopeLine, ContainingType, Virtuality,
  1146. VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
  1147. Declaration, Variables))
  1148. DEFINE_MDNODE_GET(
  1149. DISubprogram,
  1150. (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
  1151. unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
  1152. unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
  1153. unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
  1154. Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
  1155. Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
  1156. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
  1157. ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
  1158. Function, TemplateParams, Declaration, Variables))
  1159. TempDISubprogram clone() const { return cloneImpl(); }
  1160. public:
  1161. unsigned getLine() const { return Line; }
  1162. unsigned getVirtuality() const { return Virtuality; }
  1163. unsigned getVirtualIndex() const { return VirtualIndex; }
  1164. unsigned getScopeLine() const { return ScopeLine; }
  1165. unsigned getFlags() const { return Flags; }
  1166. bool isLocalToUnit() const { return IsLocalToUnit; }
  1167. bool isDefinition() const { return IsDefinition; }
  1168. bool isOptimized() const { return IsOptimized; }
  1169. unsigned isArtificial() const { return getFlags() & FlagArtificial; }
  1170. bool isPrivate() const {
  1171. return (getFlags() & FlagAccessibility) == FlagPrivate;
  1172. }
  1173. bool isProtected() const {
  1174. return (getFlags() & FlagAccessibility) == FlagProtected;
  1175. }
  1176. bool isPublic() const {
  1177. return (getFlags() & FlagAccessibility) == FlagPublic;
  1178. }
  1179. bool isExplicit() const { return getFlags() & FlagExplicit; }
  1180. bool isPrototyped() const { return getFlags() & FlagPrototyped; }
  1181. /// \brief Check if this is reference-qualified.
  1182. ///
  1183. /// Return true if this subprogram is a C++11 reference-qualified non-static
  1184. /// member function (void foo() &).
  1185. unsigned isLValueReference() const {
  1186. return getFlags() & FlagLValueReference;
  1187. }
  1188. /// \brief Check if this is rvalue-reference-qualified.
  1189. ///
  1190. /// Return true if this subprogram is a C++11 rvalue-reference-qualified
  1191. /// non-static member function (void foo() &&).
  1192. unsigned isRValueReference() const {
  1193. return getFlags() & FlagRValueReference;
  1194. }
  1195. DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
  1196. StringRef getName() const { return getStringOperand(2); }
  1197. StringRef getDisplayName() const { return getStringOperand(3); }
  1198. StringRef getLinkageName() const { return getStringOperand(4); }
  1199. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1200. MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
  1201. DISubroutineType *getType() const {
  1202. return cast_or_null<DISubroutineType>(getRawType());
  1203. }
  1204. DITypeRef getContainingType() const {
  1205. return DITypeRef(getRawContainingType());
  1206. }
  1207. Constant *getFunctionConstant() const {
  1208. if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
  1209. return C->getValue();
  1210. return nullptr;
  1211. }
  1212. DITemplateParameterArray getTemplateParams() const {
  1213. return cast_or_null<MDTuple>(getRawTemplateParams());
  1214. }
  1215. DISubprogram *getDeclaration() const {
  1216. return cast_or_null<DISubprogram>(getRawDeclaration());
  1217. }
  1218. DILocalVariableArray getVariables() const {
  1219. return cast_or_null<MDTuple>(getRawVariables());
  1220. }
  1221. Metadata *getRawScope() const { return getOperand(1); }
  1222. Metadata *getRawType() const { return getOperand(5); }
  1223. Metadata *getRawContainingType() const { return getOperand(6); }
  1224. Metadata *getRawFunction() const { return getOperand(7); }
  1225. Metadata *getRawTemplateParams() const { return getOperand(8); }
  1226. Metadata *getRawDeclaration() const { return getOperand(9); }
  1227. Metadata *getRawVariables() const { return getOperand(10); }
  1228. /// \brief Get a pointer to the function this subprogram describes.
  1229. ///
  1230. /// This dyn_casts \a getFunctionConstant() to \a Function.
  1231. ///
  1232. /// FIXME: Should this be looking through bitcasts?
  1233. Function *getFunction() const;
  1234. /// \brief Replace the function.
  1235. ///
  1236. /// If \a isUniqued() and not \a isResolved(), this could node will be
  1237. /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
  1238. /// that's a problem.
  1239. /// @{
  1240. void replaceFunction(Function *F);
  1241. void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
  1242. void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
  1243. /// @}
  1244. /// \brief Check if this subprogram decribes the given function.
  1245. ///
  1246. /// FIXME: Should this be looking through bitcasts?
  1247. bool describes(const Function *F) const;
  1248. static bool classof(const Metadata *MD) {
  1249. return MD->getMetadataID() == DISubprogramKind;
  1250. }
  1251. };
  1252. class DILexicalBlockBase : public DILocalScope {
  1253. protected:
  1254. DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
  1255. ArrayRef<Metadata *> Ops)
  1256. : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
  1257. ~DILexicalBlockBase() = default;
  1258. public:
  1259. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1260. Metadata *getRawScope() const { return getOperand(1); }
  1261. /// \brief Forwarding accessors to LexicalBlock.
  1262. ///
  1263. /// TODO: Remove these and update code to use \a DILexicalBlock directly.
  1264. /// @{
  1265. inline unsigned getLine() const;
  1266. inline unsigned getColumn() const;
  1267. /// @}
  1268. static bool classof(const Metadata *MD) {
  1269. return MD->getMetadataID() == DILexicalBlockKind ||
  1270. MD->getMetadataID() == DILexicalBlockFileKind;
  1271. }
  1272. };
  1273. class DILexicalBlock : public DILexicalBlockBase {
  1274. friend class LLVMContextImpl;
  1275. friend class MDNode;
  1276. unsigned Line;
  1277. unsigned Column;
  1278. DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
  1279. unsigned Column, ArrayRef<Metadata *> Ops)
  1280. : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
  1281. Column(Column) {}
  1282. ~DILexicalBlock() = default;
  1283. static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1284. DIFile *File, unsigned Line, unsigned Column,
  1285. StorageType Storage,
  1286. bool ShouldCreate = true) {
  1287. return getImpl(Context, static_cast<Metadata *>(Scope),
  1288. static_cast<Metadata *>(File), Line, Column, Storage,
  1289. ShouldCreate);
  1290. }
  1291. static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
  1292. Metadata *File, unsigned Line, unsigned Column,
  1293. StorageType Storage, bool ShouldCreate = true);
  1294. TempDILexicalBlock cloneImpl() const {
  1295. return getTemporary(getContext(), getScope(), getFile(), getLine(),
  1296. getColumn());
  1297. }
  1298. public:
  1299. DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
  1300. unsigned Line, unsigned Column),
  1301. (Scope, File, Line, Column))
  1302. DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
  1303. unsigned Line, unsigned Column),
  1304. (Scope, File, Line, Column))
  1305. TempDILexicalBlock clone() const { return cloneImpl(); }
  1306. unsigned getLine() const { return Line; }
  1307. unsigned getColumn() const { return Column; }
  1308. static bool classof(const Metadata *MD) {
  1309. return MD->getMetadataID() == DILexicalBlockKind;
  1310. }
  1311. };
  1312. unsigned DILexicalBlockBase::getLine() const {
  1313. if (auto *N = dyn_cast<DILexicalBlock>(this))
  1314. return N->getLine();
  1315. return 0;
  1316. }
  1317. unsigned DILexicalBlockBase::getColumn() const {
  1318. if (auto *N = dyn_cast<DILexicalBlock>(this))
  1319. return N->getColumn();
  1320. return 0;
  1321. }
  1322. class DILexicalBlockFile : public DILexicalBlockBase {
  1323. friend class LLVMContextImpl;
  1324. friend class MDNode;
  1325. unsigned Discriminator;
  1326. DILexicalBlockFile(LLVMContext &C, StorageType Storage,
  1327. unsigned Discriminator, ArrayRef<Metadata *> Ops)
  1328. : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
  1329. Discriminator(Discriminator) {}
  1330. ~DILexicalBlockFile() = default;
  1331. static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1332. DIFile *File, unsigned Discriminator,
  1333. StorageType Storage,
  1334. bool ShouldCreate = true) {
  1335. return getImpl(Context, static_cast<Metadata *>(Scope),
  1336. static_cast<Metadata *>(File), Discriminator, Storage,
  1337. ShouldCreate);
  1338. }
  1339. static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
  1340. Metadata *File, unsigned Discriminator,
  1341. StorageType Storage,
  1342. bool ShouldCreate = true);
  1343. TempDILexicalBlockFile cloneImpl() const {
  1344. return getTemporary(getContext(), getScope(), getFile(),
  1345. getDiscriminator());
  1346. }
  1347. public:
  1348. DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
  1349. unsigned Discriminator),
  1350. (Scope, File, Discriminator))
  1351. DEFINE_MDNODE_GET(DILexicalBlockFile,
  1352. (Metadata * Scope, Metadata *File, unsigned Discriminator),
  1353. (Scope, File, Discriminator))
  1354. TempDILexicalBlockFile clone() const { return cloneImpl(); }
  1355. // TODO: Remove these once they're gone from DILexicalBlockBase.
  1356. unsigned getLine() const = delete;
  1357. unsigned getColumn() const = delete;
  1358. unsigned getDiscriminator() const { return Discriminator; }
  1359. static bool classof(const Metadata *MD) {
  1360. return MD->getMetadataID() == DILexicalBlockFileKind;
  1361. }
  1362. };
  1363. unsigned DILocation::getDiscriminator() const {
  1364. if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
  1365. return F->getDiscriminator();
  1366. return 0;
  1367. }
  1368. class DINamespace : public DIScope {
  1369. friend class LLVMContextImpl;
  1370. friend class MDNode;
  1371. unsigned Line;
  1372. DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
  1373. ArrayRef<Metadata *> Ops)
  1374. : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
  1375. Ops),
  1376. Line(Line) {}
  1377. ~DINamespace() = default;
  1378. static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
  1379. DIFile *File, StringRef Name, unsigned Line,
  1380. StorageType Storage, bool ShouldCreate = true) {
  1381. return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
  1382. Line, Storage, ShouldCreate);
  1383. }
  1384. static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
  1385. Metadata *File, MDString *Name, unsigned Line,
  1386. StorageType Storage, bool ShouldCreate = true);
  1387. TempDINamespace cloneImpl() const {
  1388. return getTemporary(getContext(), getScope(), getFile(), getName(),
  1389. getLine());
  1390. }
  1391. public:
  1392. DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
  1393. unsigned Line),
  1394. (Scope, File, Name, Line))
  1395. DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
  1396. MDString *Name, unsigned Line),
  1397. (Scope, File, Name, Line))
  1398. TempDINamespace clone() const { return cloneImpl(); }
  1399. unsigned getLine() const { return Line; }
  1400. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1401. StringRef getName() const { return getStringOperand(2); }
  1402. Metadata *getRawScope() const { return getOperand(1); }
  1403. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1404. static bool classof(const Metadata *MD) {
  1405. return MD->getMetadataID() == DINamespaceKind;
  1406. }
  1407. };
  1408. /// \brief A (clang) module that has been imported by the compile unit.
  1409. ///
  1410. class DIModule : public DIScope {
  1411. friend class LLVMContextImpl;
  1412. friend class MDNode;
  1413. DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
  1414. : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
  1415. ~DIModule() {}
  1416. static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
  1417. StringRef Name, StringRef ConfigurationMacros,
  1418. StringRef IncludePath, StringRef ISysRoot,
  1419. StorageType Storage, bool ShouldCreate = true) {
  1420. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1421. getCanonicalMDString(Context, ConfigurationMacros),
  1422. getCanonicalMDString(Context, IncludePath),
  1423. getCanonicalMDString(Context, ISysRoot),
  1424. Storage, ShouldCreate);
  1425. }
  1426. static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
  1427. MDString *Name, MDString *ConfigurationMacros,
  1428. MDString *IncludePath, MDString *ISysRoot,
  1429. StorageType Storage, bool ShouldCreate = true);
  1430. TempDIModule cloneImpl() const {
  1431. return getTemporary(getContext(), getScope(), getName(),
  1432. getConfigurationMacros(), getIncludePath(),
  1433. getISysRoot());
  1434. }
  1435. public:
  1436. DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
  1437. StringRef ConfigurationMacros, StringRef IncludePath,
  1438. StringRef ISysRoot),
  1439. (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
  1440. DEFINE_MDNODE_GET(DIModule,
  1441. (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
  1442. MDString *IncludePath, MDString *ISysRoot),
  1443. (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
  1444. TempDIModule clone() const { return cloneImpl(); }
  1445. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1446. StringRef getName() const { return getStringOperand(1); }
  1447. StringRef getConfigurationMacros() const { return getStringOperand(2); }
  1448. StringRef getIncludePath() const { return getStringOperand(3); }
  1449. StringRef getISysRoot() const { return getStringOperand(4); }
  1450. Metadata *getRawScope() const { return getOperand(0); }
  1451. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  1452. MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
  1453. MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
  1454. MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
  1455. static bool classof(const Metadata *MD) {
  1456. return MD->getMetadataID() == DIModuleKind;
  1457. }
  1458. };
  1459. /// \brief Base class for template parameters.
  1460. class DITemplateParameter : public DINode {
  1461. protected:
  1462. DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
  1463. unsigned Tag, ArrayRef<Metadata *> Ops)
  1464. : DINode(Context, ID, Storage, Tag, Ops) {}
  1465. ~DITemplateParameter() = default;
  1466. public:
  1467. StringRef getName() const { return getStringOperand(0); }
  1468. DITypeRef getType() const { return DITypeRef(getRawType()); }
  1469. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  1470. Metadata *getRawType() const { return getOperand(1); }
  1471. static bool classof(const Metadata *MD) {
  1472. return MD->getMetadataID() == DITemplateTypeParameterKind ||
  1473. MD->getMetadataID() == DITemplateValueParameterKind;
  1474. }
  1475. };
  1476. class DITemplateTypeParameter : public DITemplateParameter {
  1477. friend class LLVMContextImpl;
  1478. friend class MDNode;
  1479. DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
  1480. ArrayRef<Metadata *> Ops)
  1481. : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
  1482. dwarf::DW_TAG_template_type_parameter, Ops) {}
  1483. ~DITemplateTypeParameter() = default;
  1484. static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
  1485. DITypeRef Type, StorageType Storage,
  1486. bool ShouldCreate = true) {
  1487. return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
  1488. ShouldCreate);
  1489. }
  1490. static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
  1491. Metadata *Type, StorageType Storage,
  1492. bool ShouldCreate = true);
  1493. TempDITemplateTypeParameter cloneImpl() const {
  1494. return getTemporary(getContext(), getName(), getType());
  1495. }
  1496. public:
  1497. DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
  1498. (Name, Type))
  1499. DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
  1500. (Name, Type))
  1501. TempDITemplateTypeParameter clone() const { return cloneImpl(); }
  1502. static bool classof(const Metadata *MD) {
  1503. return MD->getMetadataID() == DITemplateTypeParameterKind;
  1504. }
  1505. };
  1506. class DITemplateValueParameter : public DITemplateParameter {
  1507. friend class LLVMContextImpl;
  1508. friend class MDNode;
  1509. DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
  1510. unsigned Tag, ArrayRef<Metadata *> Ops)
  1511. : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
  1512. Ops) {}
  1513. ~DITemplateValueParameter() = default;
  1514. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  1515. StringRef Name, DITypeRef Type,
  1516. Metadata *Value, StorageType Storage,
  1517. bool ShouldCreate = true) {
  1518. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
  1519. Value, Storage, ShouldCreate);
  1520. }
  1521. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  1522. MDString *Name, Metadata *Type,
  1523. Metadata *Value, StorageType Storage,
  1524. bool ShouldCreate = true);
  1525. TempDITemplateValueParameter cloneImpl() const {
  1526. return getTemporary(getContext(), getTag(), getName(), getType(),
  1527. getValue());
  1528. }
  1529. public:
  1530. DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
  1531. DITypeRef Type, Metadata *Value),
  1532. (Tag, Name, Type, Value))
  1533. DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
  1534. Metadata *Type, Metadata *Value),
  1535. (Tag, Name, Type, Value))
  1536. TempDITemplateValueParameter clone() const { return cloneImpl(); }
  1537. Metadata *getValue() const { return getOperand(2); }
  1538. static bool classof(const Metadata *MD) {
  1539. return MD->getMetadataID() == DITemplateValueParameterKind;
  1540. }
  1541. };
  1542. /// \brief Base class for variables.
  1543. ///
  1544. /// TODO: Hardcode to DW_TAG_variable.
  1545. class DIVariable : public DINode {
  1546. unsigned Line;
  1547. protected:
  1548. DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  1549. unsigned Line, ArrayRef<Metadata *> Ops)
  1550. : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
  1551. ~DIVariable() = default;
  1552. public:
  1553. unsigned getLine() const { return Line; }
  1554. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1555. StringRef getName() const { return getStringOperand(1); }
  1556. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  1557. DITypeRef getType() const { return DITypeRef(getRawType()); }
  1558. StringRef getFilename() const {
  1559. if (auto *F = getFile())
  1560. return F->getFilename();
  1561. return "";
  1562. }
  1563. StringRef getDirectory() const {
  1564. if (auto *F = getFile())
  1565. return F->getDirectory();
  1566. return "";
  1567. }
  1568. Metadata *getRawScope() const { return getOperand(0); }
  1569. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  1570. Metadata *getRawFile() const { return getOperand(2); }
  1571. Metadata *getRawType() const { return getOperand(3); }
  1572. static bool classof(const Metadata *MD) {
  1573. return MD->getMetadataID() == DILocalVariableKind ||
  1574. MD->getMetadataID() == DIGlobalVariableKind;
  1575. }
  1576. };
  1577. /// \brief Global variables.
  1578. ///
  1579. /// TODO: Remove DisplayName. It's always equal to Name.
  1580. class DIGlobalVariable : public DIVariable {
  1581. friend class LLVMContextImpl;
  1582. friend class MDNode;
  1583. bool IsLocalToUnit;
  1584. bool IsDefinition;
  1585. DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
  1586. bool IsLocalToUnit, bool IsDefinition,
  1587. ArrayRef<Metadata *> Ops)
  1588. : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
  1589. Line, Ops),
  1590. IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
  1591. ~DIGlobalVariable() = default;
  1592. static DIGlobalVariable *
  1593. getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
  1594. StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
  1595. bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
  1596. DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
  1597. bool ShouldCreate = true) {
  1598. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1599. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  1600. IsLocalToUnit, IsDefinition,
  1601. Variable ? ConstantAsMetadata::get(Variable) : nullptr,
  1602. StaticDataMemberDeclaration, Storage, ShouldCreate);
  1603. }
  1604. static DIGlobalVariable *
  1605. getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  1606. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  1607. bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
  1608. Metadata *StaticDataMemberDeclaration, StorageType Storage,
  1609. bool ShouldCreate = true);
  1610. TempDIGlobalVariable cloneImpl() const {
  1611. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  1612. getFile(), getLine(), getType(), isLocalToUnit(),
  1613. isDefinition(), getVariable(),
  1614. getStaticDataMemberDeclaration());
  1615. }
  1616. public:
  1617. DEFINE_MDNODE_GET(DIGlobalVariable,
  1618. (DIScope * Scope, StringRef Name, StringRef LinkageName,
  1619. DIFile *File, unsigned Line, DITypeRef Type,
  1620. bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
  1621. DIDerivedType *StaticDataMemberDeclaration),
  1622. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1623. IsDefinition, Variable, StaticDataMemberDeclaration))
  1624. DEFINE_MDNODE_GET(DIGlobalVariable,
  1625. (Metadata * Scope, MDString *Name, MDString *LinkageName,
  1626. Metadata *File, unsigned Line, Metadata *Type,
  1627. bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
  1628. Metadata *StaticDataMemberDeclaration),
  1629. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1630. IsDefinition, Variable, StaticDataMemberDeclaration))
  1631. TempDIGlobalVariable clone() const { return cloneImpl(); }
  1632. bool isLocalToUnit() const { return IsLocalToUnit; }
  1633. bool isDefinition() const { return IsDefinition; }
  1634. StringRef getDisplayName() const { return getStringOperand(4); }
  1635. StringRef getLinkageName() const { return getStringOperand(5); }
  1636. Constant *getVariable() const {
  1637. if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
  1638. return dyn_cast<Constant>(C->getValue());
  1639. return nullptr;
  1640. }
  1641. DIDerivedType *getStaticDataMemberDeclaration() const {
  1642. return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
  1643. }
  1644. MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
  1645. Metadata *getRawVariable() const { return getOperand(6); }
  1646. Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
  1647. static bool classof(const Metadata *MD) {
  1648. return MD->getMetadataID() == DIGlobalVariableKind;
  1649. }
  1650. };
  1651. /// \brief Local variable.
  1652. ///
  1653. /// TODO: Split between arguments and otherwise.
  1654. /// TODO: Use \c DW_TAG_variable instead of fake tags.
  1655. /// TODO: Split up flags.
  1656. class DILocalVariable : public DIVariable {
  1657. friend class LLVMContextImpl;
  1658. friend class MDNode;
  1659. unsigned Arg;
  1660. unsigned Flags;
  1661. DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
  1662. unsigned Line, unsigned Arg, unsigned Flags,
  1663. ArrayRef<Metadata *> Ops)
  1664. : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
  1665. Flags(Flags) {}
  1666. ~DILocalVariable() = default;
  1667. static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
  1668. DIScope *Scope, StringRef Name, DIFile *File,
  1669. unsigned Line, DITypeRef Type, unsigned Arg,
  1670. unsigned Flags, StorageType Storage,
  1671. bool ShouldCreate = true) {
  1672. return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
  1673. File, Line, Type, Arg, Flags, Storage, ShouldCreate);
  1674. }
  1675. static DILocalVariable *
  1676. getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
  1677. Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
  1678. unsigned Flags, StorageType Storage, bool ShouldCreate = true);
  1679. TempDILocalVariable cloneImpl() const {
  1680. return getTemporary(getContext(), getTag(), getScope(), getName(),
  1681. getFile(), getLine(), getType(), getArg(), getFlags());
  1682. }
  1683. public:
  1684. DEFINE_MDNODE_GET(DILocalVariable,
  1685. (unsigned Tag, DILocalScope *Scope, StringRef Name,
  1686. DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
  1687. unsigned Flags),
  1688. (Tag, Scope, Name, File, Line, Type, Arg, Flags))
  1689. DEFINE_MDNODE_GET(DILocalVariable,
  1690. (unsigned Tag, Metadata *Scope, MDString *Name,
  1691. Metadata *File, unsigned Line, Metadata *Type,
  1692. unsigned Arg, unsigned Flags),
  1693. (Tag, Scope, Name, File, Line, Type, Arg, Flags))
  1694. TempDILocalVariable clone() const { return cloneImpl(); }
  1695. /// \brief Get the local scope for this variable.
  1696. ///
  1697. /// Variables must be defined in a local scope.
  1698. DILocalScope *getScope() const {
  1699. return cast<DILocalScope>(DIVariable::getScope());
  1700. }
  1701. unsigned getArg() const { return Arg; }
  1702. unsigned getFlags() const { return Flags; }
  1703. bool isArtificial() const { return getFlags() & FlagArtificial; }
  1704. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  1705. /// \brief Check that a location is valid for this variable.
  1706. ///
  1707. /// Check that \c DL exists, is in the same subprogram, and has the same
  1708. /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
  1709. /// to a \a DbgInfoIntrinsic.)
  1710. bool isValidLocationForIntrinsic(const DILocation *DL) const {
  1711. return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
  1712. }
  1713. static bool classof(const Metadata *MD) {
  1714. return MD->getMetadataID() == DILocalVariableKind;
  1715. }
  1716. };
  1717. /// \brief DWARF expression.
  1718. ///
  1719. /// This is (almost) a DWARF expression that modifies the location of a
  1720. /// variable or (or the location of a single piece of a variable).
  1721. ///
  1722. /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
  1723. /// and have DW_OP_plus consume the topmost elements on the stack.
  1724. ///
  1725. /// TODO: Co-allocate the expression elements.
  1726. /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
  1727. /// storage types.
  1728. class DIExpression : public MDNode {
  1729. friend class LLVMContextImpl;
  1730. friend class MDNode;
  1731. std::vector<uint64_t> Elements;
  1732. DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
  1733. : MDNode(C, DIExpressionKind, Storage, None),
  1734. Elements(Elements.begin(), Elements.end()) {}
  1735. ~DIExpression() = default;
  1736. static DIExpression *getImpl(LLVMContext &Context,
  1737. ArrayRef<uint64_t> Elements, StorageType Storage,
  1738. bool ShouldCreate = true);
  1739. TempDIExpression cloneImpl() const {
  1740. return getTemporary(getContext(), getElements());
  1741. }
  1742. public:
  1743. DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
  1744. TempDIExpression clone() const { return cloneImpl(); }
  1745. ArrayRef<uint64_t> getElements() const { return Elements; }
  1746. unsigned getNumElements() const { return Elements.size(); }
  1747. uint64_t getElement(unsigned I) const {
  1748. assert(I < Elements.size() && "Index out of range");
  1749. return Elements[I];
  1750. }
  1751. /// \brief Return whether this is a piece of an aggregate variable.
  1752. bool isBitPiece() const;
  1753. /// \brief Return the offset of this piece in bits.
  1754. uint64_t getBitPieceOffset() const;
  1755. /// \brief Return the size of this piece in bits.
  1756. uint64_t getBitPieceSize() const;
  1757. typedef ArrayRef<uint64_t>::iterator element_iterator;
  1758. element_iterator elements_begin() const { return getElements().begin(); }
  1759. element_iterator elements_end() const { return getElements().end(); }
  1760. /// \brief A lightweight wrapper around an expression operand.
  1761. ///
  1762. /// TODO: Store arguments directly and change \a DIExpression to store a
  1763. /// range of these.
  1764. class ExprOperand {
  1765. const uint64_t *Op;
  1766. public:
  1767. explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
  1768. const uint64_t *get() const { return Op; }
  1769. /// \brief Get the operand code.
  1770. uint64_t getOp() const { return *Op; }
  1771. /// \brief Get an argument to the operand.
  1772. ///
  1773. /// Never returns the operand itself.
  1774. uint64_t getArg(unsigned I) const { return Op[I + 1]; }
  1775. unsigned getNumArgs() const { return getSize() - 1; }
  1776. /// \brief Return the size of the operand.
  1777. ///
  1778. /// Return the number of elements in the operand (1 + args).
  1779. unsigned getSize() const;
  1780. };
  1781. /// \brief An iterator for expression operands.
  1782. class expr_op_iterator
  1783. : public std::iterator<std::input_iterator_tag, ExprOperand> {
  1784. ExprOperand Op;
  1785. public:
  1786. explicit expr_op_iterator(element_iterator I) : Op(I) {}
  1787. element_iterator getBase() const { return Op.get(); }
  1788. const ExprOperand &operator*() const { return Op; }
  1789. const ExprOperand *operator->() const { return &Op; }
  1790. expr_op_iterator &operator++() {
  1791. increment();
  1792. return *this;
  1793. }
  1794. expr_op_iterator operator++(int) {
  1795. expr_op_iterator T(*this);
  1796. increment();
  1797. return T;
  1798. }
  1799. /// \brief Get the next iterator.
  1800. ///
  1801. /// \a std::next() doesn't work because this is technically an
  1802. /// input_iterator, but it's a perfectly valid operation. This is an
  1803. /// accessor to provide the same functionality.
  1804. expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
  1805. bool operator==(const expr_op_iterator &X) const {
  1806. return getBase() == X.getBase();
  1807. }
  1808. bool operator!=(const expr_op_iterator &X) const {
  1809. return getBase() != X.getBase();
  1810. }
  1811. private:
  1812. void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
  1813. };
  1814. /// \brief Visit the elements via ExprOperand wrappers.
  1815. ///
  1816. /// These range iterators visit elements through \a ExprOperand wrappers.
  1817. /// This is not guaranteed to be a valid range unless \a isValid() gives \c
  1818. /// true.
  1819. ///
  1820. /// \pre \a isValid() gives \c true.
  1821. /// @{
  1822. expr_op_iterator expr_op_begin() const {
  1823. return expr_op_iterator(elements_begin());
  1824. }
  1825. expr_op_iterator expr_op_end() const {
  1826. return expr_op_iterator(elements_end());
  1827. }
  1828. /// @}
  1829. bool isValid() const;
  1830. static bool classof(const Metadata *MD) {
  1831. return MD->getMetadataID() == DIExpressionKind;
  1832. }
  1833. };
  1834. class DIObjCProperty : public DINode {
  1835. friend class LLVMContextImpl;
  1836. friend class MDNode;
  1837. unsigned Line;
  1838. unsigned Attributes;
  1839. DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
  1840. unsigned Attributes, ArrayRef<Metadata *> Ops)
  1841. : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
  1842. Ops),
  1843. Line(Line), Attributes(Attributes) {}
  1844. ~DIObjCProperty() = default;
  1845. static DIObjCProperty *
  1846. getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
  1847. StringRef GetterName, StringRef SetterName, unsigned Attributes,
  1848. DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
  1849. return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
  1850. getCanonicalMDString(Context, GetterName),
  1851. getCanonicalMDString(Context, SetterName), Attributes, Type,
  1852. Storage, ShouldCreate);
  1853. }
  1854. static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
  1855. Metadata *File, unsigned Line,
  1856. MDString *GetterName, MDString *SetterName,
  1857. unsigned Attributes, Metadata *Type,
  1858. StorageType Storage, bool ShouldCreate = true);
  1859. TempDIObjCProperty cloneImpl() const {
  1860. return getTemporary(getContext(), getName(), getFile(), getLine(),
  1861. getGetterName(), getSetterName(), getAttributes(),
  1862. getType());
  1863. }
  1864. public:
  1865. DEFINE_MDNODE_GET(DIObjCProperty,
  1866. (StringRef Name, DIFile *File, unsigned Line,
  1867. StringRef GetterName, StringRef SetterName,
  1868. unsigned Attributes, DITypeRef Type),
  1869. (Name, File, Line, GetterName, SetterName, Attributes,
  1870. Type))
  1871. DEFINE_MDNODE_GET(DIObjCProperty,
  1872. (MDString * Name, Metadata *File, unsigned Line,
  1873. MDString *GetterName, MDString *SetterName,
  1874. unsigned Attributes, Metadata *Type),
  1875. (Name, File, Line, GetterName, SetterName, Attributes,
  1876. Type))
  1877. TempDIObjCProperty clone() const { return cloneImpl(); }
  1878. unsigned getLine() const { return Line; }
  1879. unsigned getAttributes() const { return Attributes; }
  1880. StringRef getName() const { return getStringOperand(0); }
  1881. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  1882. StringRef getGetterName() const { return getStringOperand(2); }
  1883. StringRef getSetterName() const { return getStringOperand(3); }
  1884. DITypeRef getType() const { return DITypeRef(getRawType()); }
  1885. StringRef getFilename() const {
  1886. if (auto *F = getFile())
  1887. return F->getFilename();
  1888. return "";
  1889. }
  1890. StringRef getDirectory() const {
  1891. if (auto *F = getFile())
  1892. return F->getDirectory();
  1893. return "";
  1894. }
  1895. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  1896. Metadata *getRawFile() const { return getOperand(1); }
  1897. MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
  1898. MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
  1899. Metadata *getRawType() const { return getOperand(4); }
  1900. static bool classof(const Metadata *MD) {
  1901. return MD->getMetadataID() == DIObjCPropertyKind;
  1902. }
  1903. };
  1904. /// \brief An imported module (C++ using directive or similar).
  1905. class DIImportedEntity : public DINode {
  1906. friend class LLVMContextImpl;
  1907. friend class MDNode;
  1908. unsigned Line;
  1909. DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
  1910. unsigned Line, ArrayRef<Metadata *> Ops)
  1911. : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
  1912. ~DIImportedEntity() = default;
  1913. static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
  1914. DIScope *Scope, DINodeRef Entity,
  1915. unsigned Line, StringRef Name,
  1916. StorageType Storage,
  1917. bool ShouldCreate = true) {
  1918. return getImpl(Context, Tag, Scope, Entity, Line,
  1919. getCanonicalMDString(Context, Name), Storage, ShouldCreate);
  1920. }
  1921. static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
  1922. Metadata *Scope, Metadata *Entity,
  1923. unsigned Line, MDString *Name,
  1924. StorageType Storage,
  1925. bool ShouldCreate = true);
  1926. TempDIImportedEntity cloneImpl() const {
  1927. return getTemporary(getContext(), getTag(), getScope(), getEntity(),
  1928. getLine(), getName());
  1929. }
  1930. public:
  1931. DEFINE_MDNODE_GET(DIImportedEntity,
  1932. (unsigned Tag, DIScope *Scope, DINodeRef Entity,
  1933. unsigned Line, StringRef Name = ""),
  1934. (Tag, Scope, Entity, Line, Name))
  1935. DEFINE_MDNODE_GET(DIImportedEntity,
  1936. (unsigned Tag, Metadata *Scope, Metadata *Entity,
  1937. unsigned Line, MDString *Name),
  1938. (Tag, Scope, Entity, Line, Name))
  1939. TempDIImportedEntity clone() const { return cloneImpl(); }
  1940. unsigned getLine() const { return Line; }
  1941. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1942. DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
  1943. StringRef getName() const { return getStringOperand(2); }
  1944. Metadata *getRawScope() const { return getOperand(0); }
  1945. Metadata *getRawEntity() const { return getOperand(1); }
  1946. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1947. static bool classof(const Metadata *MD) {
  1948. return MD->getMetadataID() == DIImportedEntityKind;
  1949. }
  1950. };
  1951. } // end namespace llvm
  1952. #undef DEFINE_MDNODE_GET_UNPACK_IMPL
  1953. #undef DEFINE_MDNODE_GET_UNPACK
  1954. #undef DEFINE_MDNODE_GET
  1955. #endif