DebugInfoMetadata.h 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  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. void setScope(Metadata *scope) { setOperand(1, scope); } // HLSL Change
  456. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  457. void setFlags(unsigned NewFlags) {
  458. assert(!isUniqued() && "Cannot set flags on uniqued nodes");
  459. Flags = NewFlags;
  460. }
  461. bool isPrivate() const {
  462. return (getFlags() & FlagAccessibility) == FlagPrivate;
  463. }
  464. bool isProtected() const {
  465. return (getFlags() & FlagAccessibility) == FlagProtected;
  466. }
  467. bool isPublic() const {
  468. return (getFlags() & FlagAccessibility) == FlagPublic;
  469. }
  470. bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
  471. bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
  472. bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
  473. bool isVirtual() const { return getFlags() & FlagVirtual; }
  474. bool isArtificial() const { return getFlags() & FlagArtificial; }
  475. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  476. bool isObjcClassComplete() const {
  477. return getFlags() & FlagObjcClassComplete;
  478. }
  479. bool isVector() const { return getFlags() & FlagVector; }
  480. bool isStaticMember() const { return getFlags() & FlagStaticMember; }
  481. bool isLValueReference() const { return getFlags() & FlagLValueReference; }
  482. bool isRValueReference() const { return getFlags() & FlagRValueReference; }
  483. DITypeRef getRef() const { return DITypeRef::get(this); }
  484. static bool classof(const Metadata *MD) {
  485. switch (MD->getMetadataID()) {
  486. default:
  487. return false;
  488. case DIBasicTypeKind:
  489. case DIDerivedTypeKind:
  490. case DICompositeTypeKind:
  491. case DISubroutineTypeKind:
  492. return true;
  493. }
  494. }
  495. };
  496. /// \brief Basic type, like 'int' or 'float'.
  497. ///
  498. /// TODO: Split out DW_TAG_unspecified_type.
  499. /// TODO: Drop unused accessors.
  500. class DIBasicType : public DIType {
  501. friend class LLVMContextImpl;
  502. friend class MDNode;
  503. unsigned Encoding;
  504. DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
  505. uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
  506. ArrayRef<Metadata *> Ops)
  507. : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
  508. 0, Ops),
  509. Encoding(Encoding) {}
  510. ~DIBasicType() = default;
  511. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  512. StringRef Name, uint64_t SizeInBits,
  513. uint64_t AlignInBits, unsigned Encoding,
  514. StorageType Storage, bool ShouldCreate = true) {
  515. return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
  516. SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
  517. }
  518. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  519. MDString *Name, uint64_t SizeInBits,
  520. uint64_t AlignInBits, unsigned Encoding,
  521. StorageType Storage, bool ShouldCreate = true);
  522. TempDIBasicType cloneImpl() const {
  523. return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
  524. getAlignInBits(), getEncoding());
  525. }
  526. public:
  527. DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
  528. (Tag, Name, 0, 0, 0))
  529. DEFINE_MDNODE_GET(DIBasicType,
  530. (unsigned Tag, StringRef Name, uint64_t SizeInBits,
  531. uint64_t AlignInBits, unsigned Encoding),
  532. (Tag, Name, SizeInBits, AlignInBits, Encoding))
  533. DEFINE_MDNODE_GET(DIBasicType,
  534. (unsigned Tag, MDString *Name, uint64_t SizeInBits,
  535. uint64_t AlignInBits, unsigned Encoding),
  536. (Tag, Name, SizeInBits, AlignInBits, Encoding))
  537. TempDIBasicType clone() const { return cloneImpl(); }
  538. unsigned getEncoding() const { return Encoding; }
  539. static bool classof(const Metadata *MD) {
  540. return MD->getMetadataID() == DIBasicTypeKind;
  541. }
  542. };
  543. /// \brief Base class for DIDerivedType and DICompositeType.
  544. ///
  545. /// TODO: Delete; they're not really related.
  546. class DIDerivedTypeBase : public DIType {
  547. protected:
  548. DIDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
  549. unsigned Tag, unsigned Line, uint64_t SizeInBits,
  550. uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
  551. ArrayRef<Metadata *> Ops)
  552. : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
  553. Flags, Ops) {}
  554. ~DIDerivedTypeBase() = default;
  555. public:
  556. DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
  557. Metadata *getRawBaseType() const { return getOperand(3); }
  558. static bool classof(const Metadata *MD) {
  559. return MD->getMetadataID() == DIDerivedTypeKind ||
  560. MD->getMetadataID() == DICompositeTypeKind ||
  561. MD->getMetadataID() == DISubroutineTypeKind;
  562. }
  563. };
  564. /// \brief Derived types.
  565. ///
  566. /// This includes qualified types, pointers, references, friends, typedefs, and
  567. /// class members.
  568. ///
  569. /// TODO: Split out members (inheritance, fields, methods, etc.).
  570. class DIDerivedType : public DIDerivedTypeBase {
  571. friend class LLVMContextImpl;
  572. friend class MDNode;
  573. DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
  574. unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
  575. uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
  576. : DIDerivedTypeBase(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
  577. AlignInBits, OffsetInBits, Flags, Ops) {}
  578. ~DIDerivedType() = default;
  579. static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
  580. StringRef Name, DIFile *File, unsigned Line,
  581. DIScopeRef Scope, DITypeRef BaseType,
  582. uint64_t SizeInBits, uint64_t AlignInBits,
  583. uint64_t OffsetInBits, unsigned Flags,
  584. Metadata *ExtraData, StorageType Storage,
  585. bool ShouldCreate = true) {
  586. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
  587. Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
  588. Flags, ExtraData, Storage, ShouldCreate);
  589. }
  590. static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
  591. MDString *Name, Metadata *File, unsigned Line,
  592. Metadata *Scope, Metadata *BaseType,
  593. uint64_t SizeInBits, uint64_t AlignInBits,
  594. uint64_t OffsetInBits, unsigned Flags,
  595. Metadata *ExtraData, StorageType Storage,
  596. bool ShouldCreate = true);
  597. TempDIDerivedType cloneImpl() const {
  598. return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
  599. getScope(), getBaseType(), getSizeInBits(),
  600. getAlignInBits(), getOffsetInBits(), getFlags(),
  601. getExtraData());
  602. }
  603. public:
  604. DEFINE_MDNODE_GET(DIDerivedType,
  605. (unsigned Tag, MDString *Name, Metadata *File,
  606. unsigned Line, Metadata *Scope, Metadata *BaseType,
  607. uint64_t SizeInBits, uint64_t AlignInBits,
  608. uint64_t OffsetInBits, unsigned Flags,
  609. Metadata *ExtraData = nullptr),
  610. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  611. AlignInBits, OffsetInBits, Flags, ExtraData))
  612. DEFINE_MDNODE_GET(DIDerivedType,
  613. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  614. DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
  615. uint64_t AlignInBits, uint64_t OffsetInBits,
  616. unsigned Flags, Metadata *ExtraData = nullptr),
  617. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  618. AlignInBits, OffsetInBits, Flags, ExtraData))
  619. TempDIDerivedType clone() const { return cloneImpl(); }
  620. /// \brief Get extra data associated with this derived type.
  621. ///
  622. /// Class type for pointer-to-members, objective-c property node for ivars,
  623. /// or global constant wrapper for static members.
  624. ///
  625. /// TODO: Separate out types that need this extra operand: pointer-to-member
  626. /// types and member fields (static members and ivars).
  627. Metadata *getExtraData() const { return getRawExtraData(); }
  628. Metadata *getRawExtraData() const { return getOperand(4); }
  629. /// \brief Get casted version of extra data.
  630. /// @{
  631. DITypeRef getClassType() const {
  632. assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
  633. return DITypeRef(getExtraData());
  634. }
  635. DIObjCProperty *getObjCProperty() const {
  636. return dyn_cast_or_null<DIObjCProperty>(getExtraData());
  637. }
  638. Constant *getConstant() const {
  639. assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
  640. if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
  641. return C->getValue();
  642. return nullptr;
  643. }
  644. /// @}
  645. static bool classof(const Metadata *MD) {
  646. return MD->getMetadataID() == DIDerivedTypeKind;
  647. }
  648. };
  649. /// \brief Base class for DICompositeType and DISubroutineType.
  650. ///
  651. /// TODO: Delete; they're not really related.
  652. class DICompositeTypeBase : public DIDerivedTypeBase {
  653. unsigned RuntimeLang;
  654. protected:
  655. DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
  656. unsigned Tag, unsigned Line, unsigned RuntimeLang,
  657. uint64_t SizeInBits, uint64_t AlignInBits,
  658. uint64_t OffsetInBits, unsigned Flags,
  659. ArrayRef<Metadata *> Ops)
  660. : DIDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
  661. OffsetInBits, Flags, Ops),
  662. RuntimeLang(RuntimeLang) {}
  663. ~DICompositeTypeBase() = default;
  664. public:
  665. /// \brief Get the elements of the composite type.
  666. ///
  667. /// \note Calling this is only valid for \a DICompositeType. This assertion
  668. /// can be removed once \a DISubroutineType has been separated from
  669. /// "composite types".
  670. DINodeArray getElements() const {
  671. assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
  672. return cast_or_null<MDTuple>(getRawElements());
  673. }
  674. DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
  675. DITemplateParameterArray getTemplateParams() const {
  676. return cast_or_null<MDTuple>(getRawTemplateParams());
  677. }
  678. StringRef getIdentifier() const { return getStringOperand(7); }
  679. unsigned getRuntimeLang() const { return RuntimeLang; }
  680. Metadata *getRawElements() const { return getOperand(4); }
  681. Metadata *getRawVTableHolder() const { return getOperand(5); }
  682. Metadata *getRawTemplateParams() const { return getOperand(6); }
  683. MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
  684. /// \brief Replace operands.
  685. ///
  686. /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
  687. /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
  688. /// of its movement if necessary.
  689. /// @{
  690. void replaceElements(DINodeArray Elements) {
  691. #ifndef NDEBUG
  692. for (DINode *Op : getElements())
  693. assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
  694. "Lost a member during member list replacement");
  695. #endif
  696. replaceOperandWith(4, Elements.get());
  697. }
  698. void replaceVTableHolder(DITypeRef VTableHolder) {
  699. replaceOperandWith(5, VTableHolder);
  700. }
  701. void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
  702. replaceOperandWith(6, TemplateParams.get());
  703. }
  704. /// @}
  705. static bool classof(const Metadata *MD) {
  706. return MD->getMetadataID() == DICompositeTypeKind ||
  707. MD->getMetadataID() == DISubroutineTypeKind;
  708. }
  709. };
  710. /// \brief Composite types.
  711. ///
  712. /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
  713. /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
  714. class DICompositeType : public DICompositeTypeBase {
  715. friend class LLVMContextImpl;
  716. friend class MDNode;
  717. DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
  718. unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
  719. uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
  720. ArrayRef<Metadata *> Ops)
  721. : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
  722. RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
  723. Flags, Ops) {}
  724. ~DICompositeType() = default;
  725. static DICompositeType *
  726. getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
  727. unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
  728. uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
  729. uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
  730. DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
  731. StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
  732. return getImpl(
  733. Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
  734. BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
  735. RuntimeLang, VTableHolder, TemplateParams.get(),
  736. getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
  737. }
  738. static DICompositeType *
  739. getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
  740. unsigned Line, Metadata *Scope, Metadata *BaseType,
  741. uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
  742. unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
  743. Metadata *VTableHolder, Metadata *TemplateParams,
  744. MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
  745. TempDICompositeType cloneImpl() const {
  746. return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
  747. getScope(), getBaseType(), getSizeInBits(),
  748. getAlignInBits(), getOffsetInBits(), getFlags(),
  749. getElements(), getRuntimeLang(), getVTableHolder(),
  750. getTemplateParams(), getIdentifier());
  751. }
  752. public:
  753. DEFINE_MDNODE_GET(DICompositeType,
  754. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  755. DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
  756. uint64_t AlignInBits, uint64_t OffsetInBits,
  757. unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
  758. DITypeRef VTableHolder,
  759. DITemplateParameterArray TemplateParams = nullptr,
  760. StringRef Identifier = ""),
  761. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  762. AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
  763. VTableHolder, TemplateParams, Identifier))
  764. DEFINE_MDNODE_GET(DICompositeType,
  765. (unsigned Tag, MDString *Name, Metadata *File,
  766. unsigned Line, Metadata *Scope, Metadata *BaseType,
  767. uint64_t SizeInBits, uint64_t AlignInBits,
  768. uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
  769. unsigned RuntimeLang, Metadata *VTableHolder,
  770. Metadata *TemplateParams = nullptr,
  771. MDString *Identifier = nullptr),
  772. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  773. AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
  774. VTableHolder, TemplateParams, Identifier))
  775. TempDICompositeType clone() const { return cloneImpl(); }
  776. static bool classof(const Metadata *MD) {
  777. return MD->getMetadataID() == DICompositeTypeKind;
  778. }
  779. };
  780. template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
  781. if (N)
  782. if (auto *Composite = dyn_cast<DICompositeType>(N))
  783. if (auto *S = Composite->getRawIdentifier())
  784. return TypedDINodeRef<T>(S);
  785. return TypedDINodeRef<T>(N);
  786. }
  787. /// \brief Type array for a subprogram.
  788. ///
  789. /// TODO: Detach from CompositeType, and fold the array of types in directly
  790. /// as operands.
  791. class DISubroutineType : public DICompositeTypeBase {
  792. friend class LLVMContextImpl;
  793. friend class MDNode;
  794. DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
  795. ArrayRef<Metadata *> Ops)
  796. : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
  797. dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
  798. Ops) {}
  799. ~DISubroutineType() = default;
  800. static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
  801. DITypeRefArray TypeArray,
  802. StorageType Storage,
  803. bool ShouldCreate = true) {
  804. return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
  805. }
  806. static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
  807. Metadata *TypeArray, StorageType Storage,
  808. bool ShouldCreate = true);
  809. TempDISubroutineType cloneImpl() const {
  810. return getTemporary(getContext(), getFlags(), getTypeArray());
  811. }
  812. public:
  813. DEFINE_MDNODE_GET(DISubroutineType,
  814. (unsigned Flags, DITypeRefArray TypeArray),
  815. (Flags, TypeArray))
  816. DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
  817. (Flags, TypeArray))
  818. TempDISubroutineType clone() const { return cloneImpl(); }
  819. DITypeRefArray getTypeArray() const {
  820. return cast_or_null<MDTuple>(getRawTypeArray());
  821. }
  822. Metadata *getRawTypeArray() const { return getRawElements(); }
  823. static bool classof(const Metadata *MD) {
  824. return MD->getMetadataID() == DISubroutineTypeKind;
  825. }
  826. };
  827. /// \brief Compile unit.
  828. class DICompileUnit : public DIScope {
  829. friend class LLVMContextImpl;
  830. friend class MDNode;
  831. unsigned SourceLanguage;
  832. bool IsOptimized;
  833. unsigned RuntimeVersion;
  834. unsigned EmissionKind;
  835. uint64_t DWOId;
  836. DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
  837. bool IsOptimized, unsigned RuntimeVersion,
  838. unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
  839. : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
  840. SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
  841. RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
  842. DWOId(DWOId) {}
  843. ~DICompileUnit() = default;
  844. static DICompileUnit *
  845. getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
  846. StringRef Producer, bool IsOptimized, StringRef Flags,
  847. unsigned RuntimeVersion, StringRef SplitDebugFilename,
  848. unsigned EmissionKind, DICompositeTypeArray EnumTypes,
  849. DITypeArray RetainedTypes, DISubprogramArray Subprograms,
  850. DIGlobalVariableArray GlobalVariables,
  851. DIImportedEntityArray ImportedEntities, uint64_t DWOId,
  852. StorageType Storage, bool ShouldCreate = true) {
  853. return getImpl(Context, SourceLanguage, File,
  854. getCanonicalMDString(Context, Producer), IsOptimized,
  855. getCanonicalMDString(Context, Flags), RuntimeVersion,
  856. getCanonicalMDString(Context, SplitDebugFilename),
  857. EmissionKind, EnumTypes.get(), RetainedTypes.get(),
  858. Subprograms.get(), GlobalVariables.get(),
  859. ImportedEntities.get(), DWOId, Storage, ShouldCreate);
  860. }
  861. static DICompileUnit *
  862. getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
  863. MDString *Producer, bool IsOptimized, MDString *Flags,
  864. unsigned RuntimeVersion, MDString *SplitDebugFilename,
  865. unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
  866. Metadata *Subprograms, Metadata *GlobalVariables,
  867. Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
  868. bool ShouldCreate = true);
  869. TempDICompileUnit cloneImpl() const {
  870. return getTemporary(
  871. getContext(), getSourceLanguage(), getFile(), getProducer(),
  872. isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
  873. getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
  874. getGlobalVariables(), getImportedEntities(), DWOId);
  875. }
  876. public:
  877. DEFINE_MDNODE_GET(DICompileUnit,
  878. (unsigned SourceLanguage, DIFile *File, StringRef Producer,
  879. bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
  880. StringRef SplitDebugFilename, unsigned EmissionKind,
  881. DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
  882. DISubprogramArray Subprograms,
  883. DIGlobalVariableArray GlobalVariables,
  884. DIImportedEntityArray ImportedEntities, uint64_t DWOId),
  885. (SourceLanguage, File, Producer, IsOptimized, Flags,
  886. RuntimeVersion, SplitDebugFilename, EmissionKind,
  887. EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
  888. ImportedEntities, DWOId))
  889. DEFINE_MDNODE_GET(
  890. DICompileUnit,
  891. (unsigned SourceLanguage, Metadata *File, MDString *Producer,
  892. bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
  893. MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
  894. Metadata *RetainedTypes, Metadata *Subprograms,
  895. Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
  896. (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
  897. SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
  898. GlobalVariables, ImportedEntities, DWOId))
  899. TempDICompileUnit clone() const { return cloneImpl(); }
  900. unsigned getSourceLanguage() const { return SourceLanguage; }
  901. bool isOptimized() const { return IsOptimized; }
  902. unsigned getRuntimeVersion() const { return RuntimeVersion; }
  903. unsigned getEmissionKind() const { return EmissionKind; }
  904. StringRef getProducer() const { return getStringOperand(1); }
  905. StringRef getFlags() const { return getStringOperand(2); }
  906. StringRef getSplitDebugFilename() const { return getStringOperand(3); }
  907. DICompositeTypeArray getEnumTypes() const {
  908. return cast_or_null<MDTuple>(getRawEnumTypes());
  909. }
  910. DITypeArray getRetainedTypes() const {
  911. return cast_or_null<MDTuple>(getRawRetainedTypes());
  912. }
  913. DISubprogramArray getSubprograms() const {
  914. return cast_or_null<MDTuple>(getRawSubprograms());
  915. }
  916. DIGlobalVariableArray getGlobalVariables() const {
  917. return cast_or_null<MDTuple>(getRawGlobalVariables());
  918. }
  919. DIImportedEntityArray getImportedEntities() const {
  920. return cast_or_null<MDTuple>(getRawImportedEntities());
  921. }
  922. unsigned getDWOId() const { return DWOId; }
  923. MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
  924. MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
  925. MDString *getRawSplitDebugFilename() const {
  926. return getOperandAs<MDString>(3);
  927. }
  928. Metadata *getRawEnumTypes() const { return getOperand(4); }
  929. Metadata *getRawRetainedTypes() const { return getOperand(5); }
  930. Metadata *getRawSubprograms() const { return getOperand(6); }
  931. Metadata *getRawGlobalVariables() const { return getOperand(7); }
  932. Metadata *getRawImportedEntities() const { return getOperand(8); }
  933. /// \brief Replace arrays.
  934. ///
  935. /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
  936. /// deleted on a uniquing collision. In practice, uniquing collisions on \a
  937. /// DICompileUnit should be fairly rare.
  938. /// @{
  939. void replaceEnumTypes(DICompositeTypeArray N) {
  940. replaceOperandWith(4, N.get());
  941. }
  942. void replaceRetainedTypes(DITypeArray N) {
  943. replaceOperandWith(5, N.get());
  944. }
  945. void replaceSubprograms(DISubprogramArray N) {
  946. replaceOperandWith(6, N.get());
  947. }
  948. void replaceGlobalVariables(DIGlobalVariableArray N) {
  949. replaceOperandWith(7, N.get());
  950. }
  951. void replaceImportedEntities(DIImportedEntityArray N) {
  952. replaceOperandWith(8, N.get());
  953. }
  954. /// @}
  955. static bool classof(const Metadata *MD) {
  956. return MD->getMetadataID() == DICompileUnitKind;
  957. }
  958. };
  959. /// \brief A scope for locals.
  960. ///
  961. /// A legal scope for lexical blocks, local variables, and debug info
  962. /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
  963. /// DILexicalBlockFile.
  964. class DILocalScope : public DIScope {
  965. protected:
  966. DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  967. ArrayRef<Metadata *> Ops)
  968. : DIScope(C, ID, Storage, Tag, Ops) {}
  969. ~DILocalScope() = default;
  970. public:
  971. /// \brief Get the subprogram for this scope.
  972. ///
  973. /// Return this if it's an \a DISubprogram; otherwise, look up the scope
  974. /// chain.
  975. DISubprogram *getSubprogram() const;
  976. static bool classof(const Metadata *MD) {
  977. return MD->getMetadataID() == DISubprogramKind ||
  978. MD->getMetadataID() == DILexicalBlockKind ||
  979. MD->getMetadataID() == DILexicalBlockFileKind;
  980. }
  981. };
  982. /// \brief Debug location.
  983. ///
  984. /// A debug location in source code, used for debug info and otherwise.
  985. class DILocation : public MDNode {
  986. friend class LLVMContextImpl;
  987. friend class MDNode;
  988. DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
  989. unsigned Column, ArrayRef<Metadata *> MDs);
  990. ~DILocation() { dropAllReferences(); }
  991. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  992. unsigned Column, Metadata *Scope,
  993. Metadata *InlinedAt, StorageType Storage,
  994. bool ShouldCreate = true);
  995. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  996. unsigned Column, DILocalScope *Scope,
  997. DILocation *InlinedAt, StorageType Storage,
  998. bool ShouldCreate = true) {
  999. return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
  1000. static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
  1001. }
  1002. TempDILocation cloneImpl() const {
  1003. return getTemporary(getContext(), getLine(), getColumn(), getScope(),
  1004. getInlinedAt());
  1005. }
  1006. // Disallow replacing operands.
  1007. void replaceOperandWith(unsigned I, Metadata *New) = delete;
  1008. public:
  1009. DEFINE_MDNODE_GET(DILocation,
  1010. (unsigned Line, unsigned Column, Metadata *Scope,
  1011. Metadata *InlinedAt = nullptr),
  1012. (Line, Column, Scope, InlinedAt))
  1013. DEFINE_MDNODE_GET(DILocation,
  1014. (unsigned Line, unsigned Column, DILocalScope *Scope,
  1015. DILocation *InlinedAt = nullptr),
  1016. (Line, Column, Scope, InlinedAt))
  1017. /// \brief Return a (temporary) clone of this.
  1018. TempDILocation clone() const { return cloneImpl(); }
  1019. unsigned getLine() const { return SubclassData32; }
  1020. unsigned getColumn() const { return SubclassData16; }
  1021. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1022. DILocation *getInlinedAt() const {
  1023. return cast_or_null<DILocation>(getRawInlinedAt());
  1024. }
  1025. DIFile *getFile() const { return getScope()->getFile(); }
  1026. StringRef getFilename() const { return getScope()->getFilename(); }
  1027. StringRef getDirectory() const { return getScope()->getDirectory(); }
  1028. /// \brief Get the scope where this is inlined.
  1029. ///
  1030. /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
  1031. /// location.
  1032. DILocalScope *getInlinedAtScope() const {
  1033. if (auto *IA = getInlinedAt())
  1034. return IA->getInlinedAtScope();
  1035. return getScope();
  1036. }
  1037. /// \brief Check whether this can be discriminated from another location.
  1038. ///
  1039. /// Check \c this can be discriminated from \c RHS in a linetable entry.
  1040. /// Scope and inlined-at chains are not recorded in the linetable, so they
  1041. /// cannot be used to distinguish basic blocks.
  1042. ///
  1043. /// The current implementation is weaker than it should be, since it just
  1044. /// checks filename and line.
  1045. ///
  1046. /// FIXME: Add a check for getDiscriminator().
  1047. /// FIXME: Add a check for getColumn().
  1048. /// FIXME: Change the getFilename() check to getFile() (or add one for
  1049. /// getDirectory()).
  1050. bool canDiscriminate(const DILocation &RHS) const {
  1051. return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
  1052. }
  1053. /// \brief Get the DWARF discriminator.
  1054. ///
  1055. /// DWARF discriminators distinguish identical file locations between
  1056. /// instructions that are on different basic blocks.
  1057. inline unsigned getDiscriminator() const;
  1058. /// \brief Compute new discriminator in the given context.
  1059. ///
  1060. /// This modifies the \a LLVMContext that \c this is in to increment the next
  1061. /// discriminator for \c this's line/filename combination.
  1062. ///
  1063. /// FIXME: Delete this. See comments in implementation and at the only call
  1064. /// site in \a AddDiscriminators::runOnFunction().
  1065. unsigned computeNewDiscriminator() const;
  1066. Metadata *getRawScope() const { return getOperand(0); }
  1067. Metadata *getRawInlinedAt() const {
  1068. if (getNumOperands() == 2)
  1069. return getOperand(1);
  1070. return nullptr;
  1071. }
  1072. static bool classof(const Metadata *MD) {
  1073. return MD->getMetadataID() == DILocationKind;
  1074. }
  1075. };
  1076. /// \brief Subprogram description.
  1077. ///
  1078. /// TODO: Remove DisplayName. It's always equal to Name.
  1079. /// TODO: Split up flags.
  1080. class DISubprogram : public DILocalScope {
  1081. friend class LLVMContextImpl;
  1082. friend class MDNode;
  1083. unsigned Line;
  1084. unsigned ScopeLine;
  1085. unsigned Virtuality;
  1086. unsigned VirtualIndex;
  1087. unsigned Flags;
  1088. bool IsLocalToUnit;
  1089. bool IsDefinition;
  1090. bool IsOptimized;
  1091. DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
  1092. unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
  1093. unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
  1094. bool IsOptimized, ArrayRef<Metadata *> Ops)
  1095. : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
  1096. Ops),
  1097. Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
  1098. VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
  1099. IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
  1100. ~DISubprogram() = default;
  1101. static DISubprogram *
  1102. getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
  1103. StringRef LinkageName, DIFile *File, unsigned Line,
  1104. DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
  1105. unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
  1106. unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
  1107. Constant *Function, DITemplateParameterArray TemplateParams,
  1108. DISubprogram *Declaration, DILocalVariableArray Variables,
  1109. StorageType Storage, bool ShouldCreate = true) {
  1110. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1111. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  1112. IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
  1113. Virtuality, VirtualIndex, Flags, IsOptimized,
  1114. Function ? ConstantAsMetadata::get(Function) : nullptr,
  1115. TemplateParams.get(), Declaration, Variables.get(), Storage,
  1116. ShouldCreate);
  1117. }
  1118. static DISubprogram *
  1119. getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  1120. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  1121. bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
  1122. Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
  1123. unsigned Flags, bool IsOptimized, Metadata *Function,
  1124. Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
  1125. StorageType Storage, bool ShouldCreate = true);
  1126. TempDISubprogram cloneImpl() const {
  1127. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  1128. getFile(), getLine(), getType(), isLocalToUnit(),
  1129. isDefinition(), getScopeLine(), getContainingType(),
  1130. getVirtuality(), getVirtualIndex(), getFlags(),
  1131. isOptimized(), getFunctionConstant(),
  1132. getTemplateParams(), getDeclaration(), getVariables());
  1133. }
  1134. public:
  1135. DEFINE_MDNODE_GET(DISubprogram,
  1136. (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
  1137. DIFile *File, unsigned Line, DISubroutineType *Type,
  1138. bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
  1139. DITypeRef ContainingType, unsigned Virtuality,
  1140. unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
  1141. Constant *Function = nullptr,
  1142. DITemplateParameterArray TemplateParams = nullptr,
  1143. DISubprogram *Declaration = nullptr,
  1144. DILocalVariableArray Variables = nullptr),
  1145. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1146. IsDefinition, ScopeLine, ContainingType, Virtuality,
  1147. VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
  1148. Declaration, Variables))
  1149. DEFINE_MDNODE_GET(
  1150. DISubprogram,
  1151. (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
  1152. unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
  1153. unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
  1154. unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
  1155. Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
  1156. Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
  1157. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
  1158. ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
  1159. Function, TemplateParams, Declaration, Variables))
  1160. TempDISubprogram clone() const { return cloneImpl(); }
  1161. public:
  1162. unsigned getLine() const { return Line; }
  1163. unsigned getVirtuality() const { return Virtuality; }
  1164. unsigned getVirtualIndex() const { return VirtualIndex; }
  1165. unsigned getScopeLine() const { return ScopeLine; }
  1166. unsigned getFlags() const { return Flags; }
  1167. bool isLocalToUnit() const { return IsLocalToUnit; }
  1168. bool isDefinition() const { return IsDefinition; }
  1169. bool isOptimized() const { return IsOptimized; }
  1170. unsigned isArtificial() const { return getFlags() & FlagArtificial; }
  1171. bool isPrivate() const {
  1172. return (getFlags() & FlagAccessibility) == FlagPrivate;
  1173. }
  1174. bool isProtected() const {
  1175. return (getFlags() & FlagAccessibility) == FlagProtected;
  1176. }
  1177. bool isPublic() const {
  1178. return (getFlags() & FlagAccessibility) == FlagPublic;
  1179. }
  1180. bool isExplicit() const { return getFlags() & FlagExplicit; }
  1181. bool isPrototyped() const { return getFlags() & FlagPrototyped; }
  1182. /// \brief Check if this is reference-qualified.
  1183. ///
  1184. /// Return true if this subprogram is a C++11 reference-qualified non-static
  1185. /// member function (void foo() &).
  1186. unsigned isLValueReference() const {
  1187. return getFlags() & FlagLValueReference;
  1188. }
  1189. /// \brief Check if this is rvalue-reference-qualified.
  1190. ///
  1191. /// Return true if this subprogram is a C++11 rvalue-reference-qualified
  1192. /// non-static member function (void foo() &&).
  1193. unsigned isRValueReference() const {
  1194. return getFlags() & FlagRValueReference;
  1195. }
  1196. DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
  1197. StringRef getName() const { return getStringOperand(2); }
  1198. StringRef getDisplayName() const { return getStringOperand(3); }
  1199. StringRef getLinkageName() const { return getStringOperand(4); }
  1200. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1201. MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
  1202. DISubroutineType *getType() const {
  1203. return cast_or_null<DISubroutineType>(getRawType());
  1204. }
  1205. DITypeRef getContainingType() const {
  1206. return DITypeRef(getRawContainingType());
  1207. }
  1208. Constant *getFunctionConstant() const {
  1209. if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
  1210. return C->getValue();
  1211. return nullptr;
  1212. }
  1213. DITemplateParameterArray getTemplateParams() const {
  1214. return cast_or_null<MDTuple>(getRawTemplateParams());
  1215. }
  1216. DISubprogram *getDeclaration() const {
  1217. return cast_or_null<DISubprogram>(getRawDeclaration());
  1218. }
  1219. DILocalVariableArray getVariables() const {
  1220. return cast_or_null<MDTuple>(getRawVariables());
  1221. }
  1222. Metadata *getRawScope() const { return getOperand(1); }
  1223. Metadata *getRawType() const { return getOperand(5); }
  1224. Metadata *getRawContainingType() const { return getOperand(6); }
  1225. Metadata *getRawFunction() const { return getOperand(7); }
  1226. Metadata *getRawTemplateParams() const { return getOperand(8); }
  1227. Metadata *getRawDeclaration() const { return getOperand(9); }
  1228. Metadata *getRawVariables() const { return getOperand(10); }
  1229. /// \brief Get a pointer to the function this subprogram describes.
  1230. ///
  1231. /// This dyn_casts \a getFunctionConstant() to \a Function.
  1232. ///
  1233. /// FIXME: Should this be looking through bitcasts?
  1234. Function *getFunction() const;
  1235. /// \brief Replace the function.
  1236. ///
  1237. /// If \a isUniqued() and not \a isResolved(), this could node will be
  1238. /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
  1239. /// that's a problem.
  1240. /// @{
  1241. void replaceFunction(Function *F);
  1242. void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
  1243. void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
  1244. /// @}
  1245. /// \brief Check if this subprogram decribes the given function.
  1246. ///
  1247. /// FIXME: Should this be looking through bitcasts?
  1248. bool describes(const Function *F) const;
  1249. static bool classof(const Metadata *MD) {
  1250. return MD->getMetadataID() == DISubprogramKind;
  1251. }
  1252. };
  1253. class DILexicalBlockBase : public DILocalScope {
  1254. protected:
  1255. DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
  1256. ArrayRef<Metadata *> Ops)
  1257. : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
  1258. ~DILexicalBlockBase() = default;
  1259. public:
  1260. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1261. Metadata *getRawScope() const { return getOperand(1); }
  1262. /// \brief Forwarding accessors to LexicalBlock.
  1263. ///
  1264. /// TODO: Remove these and update code to use \a DILexicalBlock directly.
  1265. /// @{
  1266. inline unsigned getLine() const;
  1267. inline unsigned getColumn() const;
  1268. /// @}
  1269. static bool classof(const Metadata *MD) {
  1270. return MD->getMetadataID() == DILexicalBlockKind ||
  1271. MD->getMetadataID() == DILexicalBlockFileKind;
  1272. }
  1273. };
  1274. class DILexicalBlock : public DILexicalBlockBase {
  1275. friend class LLVMContextImpl;
  1276. friend class MDNode;
  1277. unsigned Line;
  1278. unsigned Column;
  1279. DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
  1280. unsigned Column, ArrayRef<Metadata *> Ops)
  1281. : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
  1282. Column(Column) {}
  1283. ~DILexicalBlock() = default;
  1284. static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1285. DIFile *File, unsigned Line, unsigned Column,
  1286. StorageType Storage,
  1287. bool ShouldCreate = true) {
  1288. return getImpl(Context, static_cast<Metadata *>(Scope),
  1289. static_cast<Metadata *>(File), Line, Column, Storage,
  1290. ShouldCreate);
  1291. }
  1292. static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
  1293. Metadata *File, unsigned Line, unsigned Column,
  1294. StorageType Storage, bool ShouldCreate = true);
  1295. TempDILexicalBlock cloneImpl() const {
  1296. return getTemporary(getContext(), getScope(), getFile(), getLine(),
  1297. getColumn());
  1298. }
  1299. public:
  1300. DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
  1301. unsigned Line, unsigned Column),
  1302. (Scope, File, Line, Column))
  1303. DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
  1304. unsigned Line, unsigned Column),
  1305. (Scope, File, Line, Column))
  1306. TempDILexicalBlock clone() const { return cloneImpl(); }
  1307. unsigned getLine() const { return Line; }
  1308. unsigned getColumn() const { return Column; }
  1309. static bool classof(const Metadata *MD) {
  1310. return MD->getMetadataID() == DILexicalBlockKind;
  1311. }
  1312. };
  1313. unsigned DILexicalBlockBase::getLine() const {
  1314. if (auto *N = dyn_cast<DILexicalBlock>(this))
  1315. return N->getLine();
  1316. return 0;
  1317. }
  1318. unsigned DILexicalBlockBase::getColumn() const {
  1319. if (auto *N = dyn_cast<DILexicalBlock>(this))
  1320. return N->getColumn();
  1321. return 0;
  1322. }
  1323. class DILexicalBlockFile : public DILexicalBlockBase {
  1324. friend class LLVMContextImpl;
  1325. friend class MDNode;
  1326. unsigned Discriminator;
  1327. DILexicalBlockFile(LLVMContext &C, StorageType Storage,
  1328. unsigned Discriminator, ArrayRef<Metadata *> Ops)
  1329. : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
  1330. Discriminator(Discriminator) {}
  1331. ~DILexicalBlockFile() = default;
  1332. static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1333. DIFile *File, unsigned Discriminator,
  1334. StorageType Storage,
  1335. bool ShouldCreate = true) {
  1336. return getImpl(Context, static_cast<Metadata *>(Scope),
  1337. static_cast<Metadata *>(File), Discriminator, Storage,
  1338. ShouldCreate);
  1339. }
  1340. static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
  1341. Metadata *File, unsigned Discriminator,
  1342. StorageType Storage,
  1343. bool ShouldCreate = true);
  1344. TempDILexicalBlockFile cloneImpl() const {
  1345. return getTemporary(getContext(), getScope(), getFile(),
  1346. getDiscriminator());
  1347. }
  1348. public:
  1349. DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
  1350. unsigned Discriminator),
  1351. (Scope, File, Discriminator))
  1352. DEFINE_MDNODE_GET(DILexicalBlockFile,
  1353. (Metadata * Scope, Metadata *File, unsigned Discriminator),
  1354. (Scope, File, Discriminator))
  1355. TempDILexicalBlockFile clone() const { return cloneImpl(); }
  1356. // TODO: Remove these once they're gone from DILexicalBlockBase.
  1357. unsigned getLine() const = delete;
  1358. unsigned getColumn() const = delete;
  1359. unsigned getDiscriminator() const { return Discriminator; }
  1360. static bool classof(const Metadata *MD) {
  1361. return MD->getMetadataID() == DILexicalBlockFileKind;
  1362. }
  1363. };
  1364. unsigned DILocation::getDiscriminator() const {
  1365. if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
  1366. return F->getDiscriminator();
  1367. return 0;
  1368. }
  1369. class DINamespace : public DIScope {
  1370. friend class LLVMContextImpl;
  1371. friend class MDNode;
  1372. unsigned Line;
  1373. DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
  1374. ArrayRef<Metadata *> Ops)
  1375. : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
  1376. Ops),
  1377. Line(Line) {}
  1378. ~DINamespace() = default;
  1379. static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
  1380. DIFile *File, StringRef Name, unsigned Line,
  1381. StorageType Storage, bool ShouldCreate = true) {
  1382. return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
  1383. Line, Storage, ShouldCreate);
  1384. }
  1385. static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
  1386. Metadata *File, MDString *Name, unsigned Line,
  1387. StorageType Storage, bool ShouldCreate = true);
  1388. TempDINamespace cloneImpl() const {
  1389. return getTemporary(getContext(), getScope(), getFile(), getName(),
  1390. getLine());
  1391. }
  1392. public:
  1393. DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
  1394. unsigned Line),
  1395. (Scope, File, Name, Line))
  1396. DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
  1397. MDString *Name, unsigned Line),
  1398. (Scope, File, Name, Line))
  1399. TempDINamespace clone() const { return cloneImpl(); }
  1400. unsigned getLine() const { return Line; }
  1401. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1402. StringRef getName() const { return getStringOperand(2); }
  1403. Metadata *getRawScope() const { return getOperand(1); }
  1404. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1405. static bool classof(const Metadata *MD) {
  1406. return MD->getMetadataID() == DINamespaceKind;
  1407. }
  1408. };
  1409. /// \brief A (clang) module that has been imported by the compile unit.
  1410. ///
  1411. class DIModule : public DIScope {
  1412. friend class LLVMContextImpl;
  1413. friend class MDNode;
  1414. DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
  1415. : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
  1416. ~DIModule() {}
  1417. static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
  1418. StringRef Name, StringRef ConfigurationMacros,
  1419. StringRef IncludePath, StringRef ISysRoot,
  1420. StorageType Storage, bool ShouldCreate = true) {
  1421. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1422. getCanonicalMDString(Context, ConfigurationMacros),
  1423. getCanonicalMDString(Context, IncludePath),
  1424. getCanonicalMDString(Context, ISysRoot),
  1425. Storage, ShouldCreate);
  1426. }
  1427. static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
  1428. MDString *Name, MDString *ConfigurationMacros,
  1429. MDString *IncludePath, MDString *ISysRoot,
  1430. StorageType Storage, bool ShouldCreate = true);
  1431. TempDIModule cloneImpl() const {
  1432. return getTemporary(getContext(), getScope(), getName(),
  1433. getConfigurationMacros(), getIncludePath(),
  1434. getISysRoot());
  1435. }
  1436. public:
  1437. DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
  1438. StringRef ConfigurationMacros, StringRef IncludePath,
  1439. StringRef ISysRoot),
  1440. (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
  1441. DEFINE_MDNODE_GET(DIModule,
  1442. (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
  1443. MDString *IncludePath, MDString *ISysRoot),
  1444. (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
  1445. TempDIModule clone() const { return cloneImpl(); }
  1446. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1447. StringRef getName() const { return getStringOperand(1); }
  1448. StringRef getConfigurationMacros() const { return getStringOperand(2); }
  1449. StringRef getIncludePath() const { return getStringOperand(3); }
  1450. StringRef getISysRoot() const { return getStringOperand(4); }
  1451. Metadata *getRawScope() const { return getOperand(0); }
  1452. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  1453. MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
  1454. MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
  1455. MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
  1456. static bool classof(const Metadata *MD) {
  1457. return MD->getMetadataID() == DIModuleKind;
  1458. }
  1459. };
  1460. /// \brief Base class for template parameters.
  1461. class DITemplateParameter : public DINode {
  1462. protected:
  1463. DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
  1464. unsigned Tag, ArrayRef<Metadata *> Ops)
  1465. : DINode(Context, ID, Storage, Tag, Ops) {}
  1466. ~DITemplateParameter() = default;
  1467. public:
  1468. StringRef getName() const { return getStringOperand(0); }
  1469. DITypeRef getType() const { return DITypeRef(getRawType()); }
  1470. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  1471. Metadata *getRawType() const { return getOperand(1); }
  1472. static bool classof(const Metadata *MD) {
  1473. return MD->getMetadataID() == DITemplateTypeParameterKind ||
  1474. MD->getMetadataID() == DITemplateValueParameterKind;
  1475. }
  1476. };
  1477. class DITemplateTypeParameter : public DITemplateParameter {
  1478. friend class LLVMContextImpl;
  1479. friend class MDNode;
  1480. DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
  1481. ArrayRef<Metadata *> Ops)
  1482. : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
  1483. dwarf::DW_TAG_template_type_parameter, Ops) {}
  1484. ~DITemplateTypeParameter() = default;
  1485. static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
  1486. DITypeRef Type, StorageType Storage,
  1487. bool ShouldCreate = true) {
  1488. return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
  1489. ShouldCreate);
  1490. }
  1491. static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
  1492. Metadata *Type, StorageType Storage,
  1493. bool ShouldCreate = true);
  1494. TempDITemplateTypeParameter cloneImpl() const {
  1495. return getTemporary(getContext(), getName(), getType());
  1496. }
  1497. public:
  1498. DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
  1499. (Name, Type))
  1500. DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
  1501. (Name, Type))
  1502. TempDITemplateTypeParameter clone() const { return cloneImpl(); }
  1503. static bool classof(const Metadata *MD) {
  1504. return MD->getMetadataID() == DITemplateTypeParameterKind;
  1505. }
  1506. };
  1507. class DITemplateValueParameter : public DITemplateParameter {
  1508. friend class LLVMContextImpl;
  1509. friend class MDNode;
  1510. DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
  1511. unsigned Tag, ArrayRef<Metadata *> Ops)
  1512. : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
  1513. Ops) {}
  1514. ~DITemplateValueParameter() = default;
  1515. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  1516. StringRef Name, DITypeRef Type,
  1517. Metadata *Value, StorageType Storage,
  1518. bool ShouldCreate = true) {
  1519. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
  1520. Value, Storage, ShouldCreate);
  1521. }
  1522. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  1523. MDString *Name, Metadata *Type,
  1524. Metadata *Value, StorageType Storage,
  1525. bool ShouldCreate = true);
  1526. TempDITemplateValueParameter cloneImpl() const {
  1527. return getTemporary(getContext(), getTag(), getName(), getType(),
  1528. getValue());
  1529. }
  1530. public:
  1531. DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
  1532. DITypeRef Type, Metadata *Value),
  1533. (Tag, Name, Type, Value))
  1534. DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
  1535. Metadata *Type, Metadata *Value),
  1536. (Tag, Name, Type, Value))
  1537. TempDITemplateValueParameter clone() const { return cloneImpl(); }
  1538. Metadata *getValue() const { return getOperand(2); }
  1539. static bool classof(const Metadata *MD) {
  1540. return MD->getMetadataID() == DITemplateValueParameterKind;
  1541. }
  1542. };
  1543. /// \brief Base class for variables.
  1544. ///
  1545. /// TODO: Hardcode to DW_TAG_variable.
  1546. class DIVariable : public DINode {
  1547. unsigned Line;
  1548. protected:
  1549. DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  1550. unsigned Line, ArrayRef<Metadata *> Ops)
  1551. : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
  1552. ~DIVariable() = default;
  1553. public:
  1554. unsigned getLine() const { return Line; }
  1555. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1556. StringRef getName() const { return getStringOperand(1); }
  1557. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  1558. DITypeRef getType() const { return DITypeRef(getRawType()); }
  1559. StringRef getFilename() const {
  1560. if (auto *F = getFile())
  1561. return F->getFilename();
  1562. return "";
  1563. }
  1564. StringRef getDirectory() const {
  1565. if (auto *F = getFile())
  1566. return F->getDirectory();
  1567. return "";
  1568. }
  1569. void setScope(DIScope *scope) { setOperand(0, scope); } // HLSL Change
  1570. Metadata *getRawScope() const { return getOperand(0); }
  1571. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  1572. Metadata *getRawFile() const { return getOperand(2); }
  1573. Metadata *getRawType() const { return getOperand(3); }
  1574. static bool classof(const Metadata *MD) {
  1575. return MD->getMetadataID() == DILocalVariableKind ||
  1576. MD->getMetadataID() == DIGlobalVariableKind;
  1577. }
  1578. };
  1579. /// \brief Global variables.
  1580. ///
  1581. /// TODO: Remove DisplayName. It's always equal to Name.
  1582. class DIGlobalVariable : public DIVariable {
  1583. friend class LLVMContextImpl;
  1584. friend class MDNode;
  1585. bool IsLocalToUnit;
  1586. bool IsDefinition;
  1587. DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
  1588. bool IsLocalToUnit, bool IsDefinition,
  1589. ArrayRef<Metadata *> Ops)
  1590. : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
  1591. Line, Ops),
  1592. IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
  1593. ~DIGlobalVariable() = default;
  1594. static DIGlobalVariable *
  1595. getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
  1596. StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
  1597. bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
  1598. DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
  1599. bool ShouldCreate = true) {
  1600. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1601. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  1602. IsLocalToUnit, IsDefinition,
  1603. Variable ? ConstantAsMetadata::get(Variable) : nullptr,
  1604. StaticDataMemberDeclaration, Storage, ShouldCreate);
  1605. }
  1606. static DIGlobalVariable *
  1607. getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  1608. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  1609. bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
  1610. Metadata *StaticDataMemberDeclaration, StorageType Storage,
  1611. bool ShouldCreate = true);
  1612. TempDIGlobalVariable cloneImpl() const {
  1613. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  1614. getFile(), getLine(), getType(), isLocalToUnit(),
  1615. isDefinition(), getVariable(),
  1616. getStaticDataMemberDeclaration());
  1617. }
  1618. public:
  1619. DEFINE_MDNODE_GET(DIGlobalVariable,
  1620. (DIScope * Scope, StringRef Name, StringRef LinkageName,
  1621. DIFile *File, unsigned Line, DITypeRef Type,
  1622. bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
  1623. DIDerivedType *StaticDataMemberDeclaration),
  1624. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1625. IsDefinition, Variable, StaticDataMemberDeclaration))
  1626. DEFINE_MDNODE_GET(DIGlobalVariable,
  1627. (Metadata * Scope, MDString *Name, MDString *LinkageName,
  1628. Metadata *File, unsigned Line, Metadata *Type,
  1629. bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
  1630. Metadata *StaticDataMemberDeclaration),
  1631. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1632. IsDefinition, Variable, StaticDataMemberDeclaration))
  1633. TempDIGlobalVariable clone() const { return cloneImpl(); }
  1634. bool isLocalToUnit() const { return IsLocalToUnit; }
  1635. bool isDefinition() const { return IsDefinition; }
  1636. StringRef getDisplayName() const { return getStringOperand(4); }
  1637. StringRef getLinkageName() const { return getStringOperand(5); }
  1638. Constant *getVariable() const {
  1639. if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
  1640. return dyn_cast<Constant>(C->getValue());
  1641. return nullptr;
  1642. }
  1643. DIDerivedType *getStaticDataMemberDeclaration() const {
  1644. return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
  1645. }
  1646. MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
  1647. Metadata *getRawVariable() const { return getOperand(6); }
  1648. Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
  1649. static bool classof(const Metadata *MD) {
  1650. return MD->getMetadataID() == DIGlobalVariableKind;
  1651. }
  1652. };
  1653. /// \brief Local variable.
  1654. ///
  1655. /// TODO: Split between arguments and otherwise.
  1656. /// TODO: Use \c DW_TAG_variable instead of fake tags.
  1657. /// TODO: Split up flags.
  1658. class DILocalVariable : public DIVariable {
  1659. friend class LLVMContextImpl;
  1660. friend class MDNode;
  1661. unsigned Arg;
  1662. unsigned Flags;
  1663. DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
  1664. unsigned Line, unsigned Arg, unsigned Flags,
  1665. ArrayRef<Metadata *> Ops)
  1666. : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
  1667. Flags(Flags) {}
  1668. ~DILocalVariable() = default;
  1669. static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
  1670. DIScope *Scope, StringRef Name, DIFile *File,
  1671. unsigned Line, DITypeRef Type, unsigned Arg,
  1672. unsigned Flags, StorageType Storage,
  1673. bool ShouldCreate = true) {
  1674. return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
  1675. File, Line, Type, Arg, Flags, Storage, ShouldCreate);
  1676. }
  1677. static DILocalVariable *
  1678. getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
  1679. Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
  1680. unsigned Flags, StorageType Storage, bool ShouldCreate = true);
  1681. TempDILocalVariable cloneImpl() const {
  1682. return getTemporary(getContext(), getTag(), getScope(), getName(),
  1683. getFile(), getLine(), getType(), getArg(), getFlags());
  1684. }
  1685. public:
  1686. DEFINE_MDNODE_GET(DILocalVariable,
  1687. (unsigned Tag, DILocalScope *Scope, StringRef Name,
  1688. DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
  1689. unsigned Flags),
  1690. (Tag, Scope, Name, File, Line, Type, Arg, Flags))
  1691. DEFINE_MDNODE_GET(DILocalVariable,
  1692. (unsigned Tag, Metadata *Scope, MDString *Name,
  1693. Metadata *File, unsigned Line, Metadata *Type,
  1694. unsigned Arg, unsigned Flags),
  1695. (Tag, Scope, Name, File, Line, Type, Arg, Flags))
  1696. TempDILocalVariable clone() const { return cloneImpl(); }
  1697. /// \brief Get the local scope for this variable.
  1698. ///
  1699. /// Variables must be defined in a local scope.
  1700. DILocalScope *getScope() const {
  1701. return cast<DILocalScope>(DIVariable::getScope());
  1702. }
  1703. unsigned getArg() const { return Arg; }
  1704. unsigned getFlags() const { return Flags; }
  1705. bool isArtificial() const { return getFlags() & FlagArtificial; }
  1706. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  1707. /// \brief Check that a location is valid for this variable.
  1708. ///
  1709. /// Check that \c DL exists, is in the same subprogram, and has the same
  1710. /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
  1711. /// to a \a DbgInfoIntrinsic.)
  1712. bool isValidLocationForIntrinsic(const DILocation *DL) const {
  1713. return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
  1714. }
  1715. static bool classof(const Metadata *MD) {
  1716. return MD->getMetadataID() == DILocalVariableKind;
  1717. }
  1718. };
  1719. /// \brief DWARF expression.
  1720. ///
  1721. /// This is (almost) a DWARF expression that modifies the location of a
  1722. /// variable or (or the location of a single piece of a variable).
  1723. ///
  1724. /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
  1725. /// and have DW_OP_plus consume the topmost elements on the stack.
  1726. ///
  1727. /// TODO: Co-allocate the expression elements.
  1728. /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
  1729. /// storage types.
  1730. class DIExpression : public MDNode {
  1731. friend class LLVMContextImpl;
  1732. friend class MDNode;
  1733. std::vector<uint64_t> Elements;
  1734. DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
  1735. : MDNode(C, DIExpressionKind, Storage, None),
  1736. Elements(Elements.begin(), Elements.end()) {}
  1737. ~DIExpression() = default;
  1738. static DIExpression *getImpl(LLVMContext &Context,
  1739. ArrayRef<uint64_t> Elements, StorageType Storage,
  1740. bool ShouldCreate = true);
  1741. TempDIExpression cloneImpl() const {
  1742. return getTemporary(getContext(), getElements());
  1743. }
  1744. public:
  1745. DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
  1746. TempDIExpression clone() const { return cloneImpl(); }
  1747. ArrayRef<uint64_t> getElements() const { return Elements; }
  1748. unsigned getNumElements() const { return Elements.size(); }
  1749. uint64_t getElement(unsigned I) const {
  1750. assert(I < Elements.size() && "Index out of range");
  1751. return Elements[I];
  1752. }
  1753. /// \brief Return whether this is a piece of an aggregate variable.
  1754. bool isBitPiece() const;
  1755. /// \brief Return the offset of this piece in bits.
  1756. uint64_t getBitPieceOffset() const;
  1757. /// \brief Return the size of this piece in bits.
  1758. uint64_t getBitPieceSize() const;
  1759. typedef ArrayRef<uint64_t>::iterator element_iterator;
  1760. element_iterator elements_begin() const { return getElements().begin(); }
  1761. element_iterator elements_end() const { return getElements().end(); }
  1762. /// \brief A lightweight wrapper around an expression operand.
  1763. ///
  1764. /// TODO: Store arguments directly and change \a DIExpression to store a
  1765. /// range of these.
  1766. class ExprOperand {
  1767. const uint64_t *Op;
  1768. public:
  1769. explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
  1770. const uint64_t *get() const { return Op; }
  1771. /// \brief Get the operand code.
  1772. uint64_t getOp() const { return *Op; }
  1773. /// \brief Get an argument to the operand.
  1774. ///
  1775. /// Never returns the operand itself.
  1776. uint64_t getArg(unsigned I) const { return Op[I + 1]; }
  1777. unsigned getNumArgs() const { return getSize() - 1; }
  1778. /// \brief Return the size of the operand.
  1779. ///
  1780. /// Return the number of elements in the operand (1 + args).
  1781. unsigned getSize() const;
  1782. };
  1783. /// \brief An iterator for expression operands.
  1784. class expr_op_iterator
  1785. : public std::iterator<std::input_iterator_tag, ExprOperand> {
  1786. ExprOperand Op;
  1787. public:
  1788. explicit expr_op_iterator(element_iterator I) : Op(I) {}
  1789. element_iterator getBase() const { return Op.get(); }
  1790. const ExprOperand &operator*() const { return Op; }
  1791. const ExprOperand *operator->() const { return &Op; }
  1792. expr_op_iterator &operator++() {
  1793. increment();
  1794. return *this;
  1795. }
  1796. expr_op_iterator operator++(int) {
  1797. expr_op_iterator T(*this);
  1798. increment();
  1799. return T;
  1800. }
  1801. /// \brief Get the next iterator.
  1802. ///
  1803. /// \a std::next() doesn't work because this is technically an
  1804. /// input_iterator, but it's a perfectly valid operation. This is an
  1805. /// accessor to provide the same functionality.
  1806. expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
  1807. bool operator==(const expr_op_iterator &X) const {
  1808. return getBase() == X.getBase();
  1809. }
  1810. bool operator!=(const expr_op_iterator &X) const {
  1811. return getBase() != X.getBase();
  1812. }
  1813. private:
  1814. void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
  1815. };
  1816. /// \brief Visit the elements via ExprOperand wrappers.
  1817. ///
  1818. /// These range iterators visit elements through \a ExprOperand wrappers.
  1819. /// This is not guaranteed to be a valid range unless \a isValid() gives \c
  1820. /// true.
  1821. ///
  1822. /// \pre \a isValid() gives \c true.
  1823. /// @{
  1824. expr_op_iterator expr_op_begin() const {
  1825. return expr_op_iterator(elements_begin());
  1826. }
  1827. expr_op_iterator expr_op_end() const {
  1828. return expr_op_iterator(elements_end());
  1829. }
  1830. /// @}
  1831. bool isValid() const;
  1832. static bool classof(const Metadata *MD) {
  1833. return MD->getMetadataID() == DIExpressionKind;
  1834. }
  1835. };
  1836. class DIObjCProperty : public DINode {
  1837. friend class LLVMContextImpl;
  1838. friend class MDNode;
  1839. unsigned Line;
  1840. unsigned Attributes;
  1841. DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
  1842. unsigned Attributes, ArrayRef<Metadata *> Ops)
  1843. : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
  1844. Ops),
  1845. Line(Line), Attributes(Attributes) {}
  1846. ~DIObjCProperty() = default;
  1847. static DIObjCProperty *
  1848. getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
  1849. StringRef GetterName, StringRef SetterName, unsigned Attributes,
  1850. DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
  1851. return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
  1852. getCanonicalMDString(Context, GetterName),
  1853. getCanonicalMDString(Context, SetterName), Attributes, Type,
  1854. Storage, ShouldCreate);
  1855. }
  1856. static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
  1857. Metadata *File, unsigned Line,
  1858. MDString *GetterName, MDString *SetterName,
  1859. unsigned Attributes, Metadata *Type,
  1860. StorageType Storage, bool ShouldCreate = true);
  1861. TempDIObjCProperty cloneImpl() const {
  1862. return getTemporary(getContext(), getName(), getFile(), getLine(),
  1863. getGetterName(), getSetterName(), getAttributes(),
  1864. getType());
  1865. }
  1866. public:
  1867. DEFINE_MDNODE_GET(DIObjCProperty,
  1868. (StringRef Name, DIFile *File, unsigned Line,
  1869. StringRef GetterName, StringRef SetterName,
  1870. unsigned Attributes, DITypeRef Type),
  1871. (Name, File, Line, GetterName, SetterName, Attributes,
  1872. Type))
  1873. DEFINE_MDNODE_GET(DIObjCProperty,
  1874. (MDString * Name, Metadata *File, unsigned Line,
  1875. MDString *GetterName, MDString *SetterName,
  1876. unsigned Attributes, Metadata *Type),
  1877. (Name, File, Line, GetterName, SetterName, Attributes,
  1878. Type))
  1879. TempDIObjCProperty clone() const { return cloneImpl(); }
  1880. unsigned getLine() const { return Line; }
  1881. unsigned getAttributes() const { return Attributes; }
  1882. StringRef getName() const { return getStringOperand(0); }
  1883. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  1884. StringRef getGetterName() const { return getStringOperand(2); }
  1885. StringRef getSetterName() const { return getStringOperand(3); }
  1886. DITypeRef getType() const { return DITypeRef(getRawType()); }
  1887. StringRef getFilename() const {
  1888. if (auto *F = getFile())
  1889. return F->getFilename();
  1890. return "";
  1891. }
  1892. StringRef getDirectory() const {
  1893. if (auto *F = getFile())
  1894. return F->getDirectory();
  1895. return "";
  1896. }
  1897. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  1898. Metadata *getRawFile() const { return getOperand(1); }
  1899. MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
  1900. MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
  1901. Metadata *getRawType() const { return getOperand(4); }
  1902. static bool classof(const Metadata *MD) {
  1903. return MD->getMetadataID() == DIObjCPropertyKind;
  1904. }
  1905. };
  1906. /// \brief An imported module (C++ using directive or similar).
  1907. class DIImportedEntity : public DINode {
  1908. friend class LLVMContextImpl;
  1909. friend class MDNode;
  1910. unsigned Line;
  1911. DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
  1912. unsigned Line, ArrayRef<Metadata *> Ops)
  1913. : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
  1914. ~DIImportedEntity() = default;
  1915. static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
  1916. DIScope *Scope, DINodeRef Entity,
  1917. unsigned Line, StringRef Name,
  1918. StorageType Storage,
  1919. bool ShouldCreate = true) {
  1920. return getImpl(Context, Tag, Scope, Entity, Line,
  1921. getCanonicalMDString(Context, Name), Storage, ShouldCreate);
  1922. }
  1923. static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
  1924. Metadata *Scope, Metadata *Entity,
  1925. unsigned Line, MDString *Name,
  1926. StorageType Storage,
  1927. bool ShouldCreate = true);
  1928. TempDIImportedEntity cloneImpl() const {
  1929. return getTemporary(getContext(), getTag(), getScope(), getEntity(),
  1930. getLine(), getName());
  1931. }
  1932. public:
  1933. DEFINE_MDNODE_GET(DIImportedEntity,
  1934. (unsigned Tag, DIScope *Scope, DINodeRef Entity,
  1935. unsigned Line, StringRef Name = ""),
  1936. (Tag, Scope, Entity, Line, Name))
  1937. DEFINE_MDNODE_GET(DIImportedEntity,
  1938. (unsigned Tag, Metadata *Scope, Metadata *Entity,
  1939. unsigned Line, MDString *Name),
  1940. (Tag, Scope, Entity, Line, Name))
  1941. TempDIImportedEntity clone() const { return cloneImpl(); }
  1942. unsigned getLine() const { return Line; }
  1943. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1944. DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
  1945. StringRef getName() const { return getStringOperand(2); }
  1946. Metadata *getRawScope() const { return getOperand(0); }
  1947. Metadata *getRawEntity() const { return getOperand(1); }
  1948. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1949. static bool classof(const Metadata *MD) {
  1950. return MD->getMetadataID() == DIImportedEntityKind;
  1951. }
  1952. };
  1953. } // end namespace llvm
  1954. #undef DEFINE_MDNODE_GET_UNPACK_IMPL
  1955. #undef DEFINE_MDNODE_GET_UNPACK
  1956. #undef DEFINE_MDNODE_GET
  1957. #endif