DebugInfoMetadata.h 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Declarations for metadata specific to debug info.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_DEBUGINFOMETADATA_H
  18. #define LLVM_IR_DEBUGINFOMETADATA_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/BitmaskEnum.h"
  21. #include "llvm/ADT/None.h"
  22. #include "llvm/ADT/Optional.h"
  23. #include "llvm/ADT/PointerUnion.h"
  24. #include "llvm/ADT/STLExtras.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/StringRef.h"
  27. #include "llvm/ADT/iterator_range.h"
  28. #include "llvm/BinaryFormat/Dwarf.h"
  29. #include "llvm/IR/Constants.h"
  30. #include "llvm/IR/Metadata.h"
  31. #include "llvm/Support/Casting.h"
  32. #include <cassert>
  33. #include <climits>
  34. #include <cstddef>
  35. #include <cstdint>
  36. #include <iterator>
  37. #include <type_traits>
  38. #include <vector>
  39. // Helper macros for defining get() overrides.
  40. #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
  41. #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
  42. #define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \
  43. static CLASS *getDistinct(LLVMContext &Context, \
  44. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  45. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
  46. } \
  47. static Temp##CLASS getTemporary(LLVMContext &Context, \
  48. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  49. return Temp##CLASS( \
  50. getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
  51. }
  52. #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
  53. static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  54. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
  55. } \
  56. static CLASS *getIfExists(LLVMContext &Context, \
  57. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  58. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
  59. /* ShouldCreate */ false); \
  60. } \
  61. DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
  62. namespace llvm {
  63. class DITypeRefArray {
  64. const MDTuple *N = nullptr;
  65. public:
  66. DITypeRefArray() = default;
  67. DITypeRefArray(const MDTuple *N) : N(N) {}
  68. explicit operator bool() const { return get(); }
  69. explicit operator MDTuple *() const { return get(); }
  70. MDTuple *get() const { return const_cast<MDTuple *>(N); }
  71. MDTuple *operator->() const { return get(); }
  72. MDTuple &operator*() const { return *get(); }
  73. // FIXME: Fix callers and remove condition on N.
  74. unsigned size() const { return N ? N->getNumOperands() : 0u; }
  75. DIType *operator[](unsigned I) const {
  76. return cast_or_null<DIType>(N->getOperand(I));
  77. }
  78. class iterator : std::iterator<std::input_iterator_tag, DIType *,
  79. std::ptrdiff_t, void, DIType *> {
  80. MDNode::op_iterator I = nullptr;
  81. public:
  82. iterator() = default;
  83. explicit iterator(MDNode::op_iterator I) : I(I) {}
  84. DIType *operator*() const { return cast_or_null<DIType>(*I); }
  85. iterator &operator++() {
  86. ++I;
  87. return *this;
  88. }
  89. iterator operator++(int) {
  90. iterator Temp(*this);
  91. ++I;
  92. return Temp;
  93. }
  94. bool operator==(const iterator &X) const { return I == X.I; }
  95. bool operator!=(const iterator &X) const { return I != X.I; }
  96. };
  97. // FIXME: Fix callers and remove condition on N.
  98. iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
  99. iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
  100. };
  101. /// Tagged DWARF-like metadata node.
  102. ///
  103. /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
  104. /// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's
  105. /// potentially used for non-DWARF output.
  106. class DINode : public MDNode {
  107. friend class LLVMContextImpl;
  108. friend class MDNode;
  109. protected:
  110. DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  111. ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
  112. : MDNode(C, ID, Storage, Ops1, Ops2) {
  113. assert(Tag < 1u << 16);
  114. SubclassData16 = Tag;
  115. }
  116. ~DINode() = default;
  117. template <class Ty> Ty *getOperandAs(unsigned I) const {
  118. return cast_or_null<Ty>(getOperand(I));
  119. }
  120. StringRef getStringOperand(unsigned I) const {
  121. if (auto *S = getOperandAs<MDString>(I))
  122. return S->getString();
  123. return StringRef();
  124. }
  125. static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
  126. if (S.empty())
  127. return nullptr;
  128. return MDString::get(Context, S);
  129. }
  130. /// Allow subclasses to mutate the tag.
  131. void setTag(unsigned Tag) { SubclassData16 = Tag; }
  132. public:
  133. unsigned getTag() const { return SubclassData16; }
  134. /// Debug info flags.
  135. ///
  136. /// The three accessibility flags are mutually exclusive and rolled together
  137. /// in the first two bits.
  138. enum DIFlags : uint32_t {
  139. #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
  140. #define DI_FLAG_LARGEST_NEEDED
  141. #include "llvm/IR/DebugInfoFlags.def"
  142. FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic,
  143. FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance |
  144. FlagVirtualInheritance,
  145. LLVM_MARK_AS_BITMASK_ENUM(FlagLargest)
  146. };
  147. static DIFlags getFlag(StringRef Flag);
  148. static StringRef getFlagString(DIFlags Flag);
  149. /// Split up a flags bitfield.
  150. ///
  151. /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
  152. /// any remaining (unrecognized) bits.
  153. static DIFlags splitFlags(DIFlags Flags,
  154. SmallVectorImpl<DIFlags> &SplitFlags);
  155. static bool classof(const Metadata *MD) {
  156. switch (MD->getMetadataID()) {
  157. default:
  158. return false;
  159. case GenericDINodeKind:
  160. case DISubrangeKind:
  161. case DIEnumeratorKind:
  162. case DIBasicTypeKind:
  163. case DIStringTypeKind:
  164. case DIDerivedTypeKind:
  165. case DICompositeTypeKind:
  166. case DISubroutineTypeKind:
  167. case DIFileKind:
  168. case DICompileUnitKind:
  169. case DISubprogramKind:
  170. case DILexicalBlockKind:
  171. case DILexicalBlockFileKind:
  172. case DINamespaceKind:
  173. case DICommonBlockKind:
  174. case DITemplateTypeParameterKind:
  175. case DITemplateValueParameterKind:
  176. case DIGlobalVariableKind:
  177. case DILocalVariableKind:
  178. case DILabelKind:
  179. case DIObjCPropertyKind:
  180. case DIImportedEntityKind:
  181. case DIModuleKind:
  182. case DIGenericSubrangeKind:
  183. return true;
  184. }
  185. }
  186. };
  187. /// Generic tagged DWARF-like metadata node.
  188. ///
  189. /// An un-specialized DWARF-like metadata node. The first operand is a
  190. /// (possibly empty) null-separated \a MDString header that contains arbitrary
  191. /// fields. The remaining operands are \a dwarf_operands(), and are pointers
  192. /// to other metadata.
  193. class GenericDINode : public DINode {
  194. friend class LLVMContextImpl;
  195. friend class MDNode;
  196. GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
  197. unsigned Tag, ArrayRef<Metadata *> Ops1,
  198. ArrayRef<Metadata *> Ops2)
  199. : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
  200. setHash(Hash);
  201. }
  202. ~GenericDINode() { dropAllReferences(); }
  203. void setHash(unsigned Hash) { SubclassData32 = Hash; }
  204. void recalculateHash();
  205. static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
  206. StringRef Header, ArrayRef<Metadata *> DwarfOps,
  207. StorageType Storage, bool ShouldCreate = true) {
  208. return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
  209. DwarfOps, Storage, ShouldCreate);
  210. }
  211. static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
  212. MDString *Header, ArrayRef<Metadata *> DwarfOps,
  213. StorageType Storage, bool ShouldCreate = true);
  214. TempGenericDINode cloneImpl() const {
  215. return getTemporary(getContext(), getTag(), getHeader(),
  216. SmallVector<Metadata *, 4>(dwarf_operands()));
  217. }
  218. public:
  219. unsigned getHash() const { return SubclassData32; }
  220. DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
  221. ArrayRef<Metadata *> DwarfOps),
  222. (Tag, Header, DwarfOps))
  223. DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
  224. ArrayRef<Metadata *> DwarfOps),
  225. (Tag, Header, DwarfOps))
  226. /// Return a (temporary) clone of this.
  227. TempGenericDINode clone() const { return cloneImpl(); }
  228. unsigned getTag() const { return SubclassData16; }
  229. StringRef getHeader() const { return getStringOperand(0); }
  230. MDString *getRawHeader() const { return getOperandAs<MDString>(0); }
  231. op_iterator dwarf_op_begin() const { return op_begin() + 1; }
  232. op_iterator dwarf_op_end() const { return op_end(); }
  233. op_range dwarf_operands() const {
  234. return op_range(dwarf_op_begin(), dwarf_op_end());
  235. }
  236. unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
  237. const MDOperand &getDwarfOperand(unsigned I) const {
  238. return getOperand(I + 1);
  239. }
  240. void replaceDwarfOperandWith(unsigned I, Metadata *New) {
  241. replaceOperandWith(I + 1, New);
  242. }
  243. static bool classof(const Metadata *MD) {
  244. return MD->getMetadataID() == GenericDINodeKind;
  245. }
  246. };
  247. /// Array subrange.
  248. ///
  249. /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
  250. /// type.
  251. class DISubrange : public DINode {
  252. friend class LLVMContextImpl;
  253. friend class MDNode;
  254. DISubrange(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
  255. : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops) {}
  256. ~DISubrange() = default;
  257. static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
  258. int64_t LowerBound, StorageType Storage,
  259. bool ShouldCreate = true);
  260. static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
  261. int64_t LowerBound, StorageType Storage,
  262. bool ShouldCreate = true);
  263. static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
  264. Metadata *LowerBound, Metadata *UpperBound,
  265. Metadata *Stride, StorageType Storage,
  266. bool ShouldCreate = true);
  267. TempDISubrange cloneImpl() const {
  268. return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(),
  269. getRawUpperBound(), getRawStride());
  270. }
  271. public:
  272. DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
  273. (Count, LowerBound))
  274. DEFINE_MDNODE_GET(DISubrange, (Metadata *CountNode, int64_t LowerBound = 0),
  275. (CountNode, LowerBound))
  276. DEFINE_MDNODE_GET(DISubrange,
  277. (Metadata * CountNode, Metadata *LowerBound,
  278. Metadata *UpperBound, Metadata *Stride),
  279. (CountNode, LowerBound, UpperBound, Stride))
  280. TempDISubrange clone() const { return cloneImpl(); }
  281. Metadata *getRawCountNode() const {
  282. return getOperand(0).get();
  283. }
  284. Metadata *getRawLowerBound() const { return getOperand(1).get(); }
  285. Metadata *getRawUpperBound() const { return getOperand(2).get(); }
  286. Metadata *getRawStride() const { return getOperand(3).get(); }
  287. typedef PointerUnion<ConstantInt*, DIVariable*> CountType;
  288. typedef PointerUnion<ConstantInt *, DIVariable *, DIExpression *> BoundType;
  289. CountType getCount() const;
  290. BoundType getLowerBound() const;
  291. BoundType getUpperBound() const;
  292. BoundType getStride() const;
  293. static bool classof(const Metadata *MD) {
  294. return MD->getMetadataID() == DISubrangeKind;
  295. }
  296. };
  297. class DIGenericSubrange : public DINode {
  298. friend class LLVMContextImpl;
  299. friend class MDNode;
  300. DIGenericSubrange(LLVMContext &C, StorageType Storage,
  301. ArrayRef<Metadata *> Ops)
  302. : DINode(C, DIGenericSubrangeKind, Storage,
  303. dwarf::DW_TAG_generic_subrange, Ops) {}
  304. ~DIGenericSubrange() = default;
  305. static DIGenericSubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
  306. Metadata *LowerBound, Metadata *UpperBound,
  307. Metadata *Stride, StorageType Storage,
  308. bool ShouldCreate = true);
  309. TempDIGenericSubrange cloneImpl() const {
  310. return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(),
  311. getRawUpperBound(), getRawStride());
  312. }
  313. public:
  314. DEFINE_MDNODE_GET(DIGenericSubrange,
  315. (Metadata * CountNode, Metadata *LowerBound,
  316. Metadata *UpperBound, Metadata *Stride),
  317. (CountNode, LowerBound, UpperBound, Stride))
  318. TempDIGenericSubrange clone() const { return cloneImpl(); }
  319. Metadata *getRawCountNode() const { return getOperand(0).get(); }
  320. Metadata *getRawLowerBound() const { return getOperand(1).get(); }
  321. Metadata *getRawUpperBound() const { return getOperand(2).get(); }
  322. Metadata *getRawStride() const { return getOperand(3).get(); }
  323. using BoundType = PointerUnion<DIVariable *, DIExpression *>;
  324. BoundType getCount() const;
  325. BoundType getLowerBound() const;
  326. BoundType getUpperBound() const;
  327. BoundType getStride() const;
  328. static bool classof(const Metadata *MD) {
  329. return MD->getMetadataID() == DIGenericSubrangeKind;
  330. }
  331. };
  332. /// Enumeration value.
  333. ///
  334. /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
  335. /// longer creates a type cycle.
  336. class DIEnumerator : public DINode {
  337. friend class LLVMContextImpl;
  338. friend class MDNode;
  339. APInt Value;
  340. DIEnumerator(LLVMContext &C, StorageType Storage, const APInt &Value,
  341. bool IsUnsigned, ArrayRef<Metadata *> Ops)
  342. : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
  343. Value(Value) {
  344. SubclassData32 = IsUnsigned;
  345. }
  346. DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
  347. bool IsUnsigned, ArrayRef<Metadata *> Ops)
  348. : DIEnumerator(C, Storage, APInt(64, Value, !IsUnsigned), IsUnsigned,
  349. Ops) {}
  350. ~DIEnumerator() = default;
  351. static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value,
  352. bool IsUnsigned, StringRef Name,
  353. StorageType Storage, bool ShouldCreate = true) {
  354. return getImpl(Context, Value, IsUnsigned,
  355. getCanonicalMDString(Context, Name), Storage, ShouldCreate);
  356. }
  357. static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value,
  358. bool IsUnsigned, MDString *Name,
  359. StorageType Storage, bool ShouldCreate = true);
  360. TempDIEnumerator cloneImpl() const {
  361. return getTemporary(getContext(), getValue(), isUnsigned(), getName());
  362. }
  363. public:
  364. DEFINE_MDNODE_GET(DIEnumerator,
  365. (int64_t Value, bool IsUnsigned, StringRef Name),
  366. (APInt(64, Value, !IsUnsigned), IsUnsigned, Name))
  367. DEFINE_MDNODE_GET(DIEnumerator,
  368. (int64_t Value, bool IsUnsigned, MDString *Name),
  369. (APInt(64, Value, !IsUnsigned), IsUnsigned, Name))
  370. DEFINE_MDNODE_GET(DIEnumerator,
  371. (APInt Value, bool IsUnsigned, StringRef Name),
  372. (Value, IsUnsigned, Name))
  373. DEFINE_MDNODE_GET(DIEnumerator,
  374. (APInt Value, bool IsUnsigned, MDString *Name),
  375. (Value, IsUnsigned, Name))
  376. TempDIEnumerator clone() const { return cloneImpl(); }
  377. const APInt &getValue() const { return Value; }
  378. bool isUnsigned() const { return SubclassData32; }
  379. StringRef getName() const { return getStringOperand(0); }
  380. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  381. static bool classof(const Metadata *MD) {
  382. return MD->getMetadataID() == DIEnumeratorKind;
  383. }
  384. };
  385. /// Base class for scope-like contexts.
  386. ///
  387. /// Base class for lexical scopes and types (which are also declaration
  388. /// contexts).
  389. ///
  390. /// TODO: Separate the concepts of declaration contexts and lexical scopes.
  391. class DIScope : public DINode {
  392. protected:
  393. DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  394. ArrayRef<Metadata *> Ops)
  395. : DINode(C, ID, Storage, Tag, Ops) {}
  396. ~DIScope() = default;
  397. public:
  398. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  399. inline StringRef getFilename() const;
  400. inline StringRef getDirectory() const;
  401. inline Optional<StringRef> getSource() const;
  402. StringRef getName() const;
  403. DIScope *getScope() const;
  404. /// Return the raw underlying file.
  405. ///
  406. /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
  407. /// \em is the file). If \c this is an \a DIFile, we need to return \c this.
  408. /// Otherwise, return the first operand, which is where all other subclasses
  409. /// store their file pointer.
  410. Metadata *getRawFile() const {
  411. return isa<DIFile>(this) ? const_cast<DIScope *>(this)
  412. : static_cast<Metadata *>(getOperand(0));
  413. }
  414. static bool classof(const Metadata *MD) {
  415. switch (MD->getMetadataID()) {
  416. default:
  417. return false;
  418. case DIBasicTypeKind:
  419. case DIStringTypeKind:
  420. case DIDerivedTypeKind:
  421. case DICompositeTypeKind:
  422. case DISubroutineTypeKind:
  423. case DIFileKind:
  424. case DICompileUnitKind:
  425. case DISubprogramKind:
  426. case DILexicalBlockKind:
  427. case DILexicalBlockFileKind:
  428. case DINamespaceKind:
  429. case DICommonBlockKind:
  430. case DIModuleKind:
  431. return true;
  432. }
  433. }
  434. };
  435. /// File.
  436. ///
  437. /// TODO: Merge with directory/file node (including users).
  438. /// TODO: Canonicalize paths on creation.
  439. class DIFile : public DIScope {
  440. friend class LLVMContextImpl;
  441. friend class MDNode;
  442. public:
  443. /// Which algorithm (e.g. MD5) a checksum was generated with.
  444. ///
  445. /// The encoding is explicit because it is used directly in Bitcode. The
  446. /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
  447. enum ChecksumKind {
  448. // The first variant was originally CSK_None, encoded as 0. The new
  449. // internal representation removes the need for this by wrapping the
  450. // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
  451. // encoding is reserved.
  452. CSK_MD5 = 1,
  453. CSK_SHA1 = 2,
  454. CSK_SHA256 = 3,
  455. CSK_Last = CSK_SHA256 // Should be last enumeration.
  456. };
  457. /// A single checksum, represented by a \a Kind and a \a Value (a string).
  458. template <typename T>
  459. struct ChecksumInfo {
  460. /// The kind of checksum which \a Value encodes.
  461. ChecksumKind Kind;
  462. /// The string value of the checksum.
  463. T Value;
  464. ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) { }
  465. ~ChecksumInfo() = default;
  466. bool operator==(const ChecksumInfo<T> &X) const {
  467. return Kind == X.Kind && Value == X.Value;
  468. }
  469. bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
  470. StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
  471. };
  472. private:
  473. Optional<ChecksumInfo<MDString *>> Checksum;
  474. Optional<MDString *> Source;
  475. DIFile(LLVMContext &C, StorageType Storage,
  476. Optional<ChecksumInfo<MDString *>> CS, Optional<MDString *> Src,
  477. ArrayRef<Metadata *> Ops)
  478. : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops),
  479. Checksum(CS), Source(Src) {}
  480. ~DIFile() = default;
  481. static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
  482. StringRef Directory,
  483. Optional<ChecksumInfo<StringRef>> CS,
  484. Optional<StringRef> Source,
  485. StorageType Storage, bool ShouldCreate = true) {
  486. Optional<ChecksumInfo<MDString *>> MDChecksum;
  487. if (CS)
  488. MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
  489. return getImpl(Context, getCanonicalMDString(Context, Filename),
  490. getCanonicalMDString(Context, Directory), MDChecksum,
  491. Source ? Optional<MDString *>(getCanonicalMDString(Context, *Source)) : None,
  492. Storage, ShouldCreate);
  493. }
  494. static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
  495. MDString *Directory,
  496. Optional<ChecksumInfo<MDString *>> CS,
  497. Optional<MDString *> Source, StorageType Storage,
  498. bool ShouldCreate = true);
  499. TempDIFile cloneImpl() const {
  500. return getTemporary(getContext(), getFilename(), getDirectory(),
  501. getChecksum(), getSource());
  502. }
  503. public:
  504. DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory,
  505. Optional<ChecksumInfo<StringRef>> CS = None,
  506. Optional<StringRef> Source = None),
  507. (Filename, Directory, CS, Source))
  508. DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory,
  509. Optional<ChecksumInfo<MDString *>> CS = None,
  510. Optional<MDString *> Source = None),
  511. (Filename, Directory, CS, Source))
  512. TempDIFile clone() const { return cloneImpl(); }
  513. StringRef getFilename() const { return getStringOperand(0); }
  514. StringRef getDirectory() const { return getStringOperand(1); }
  515. Optional<ChecksumInfo<StringRef>> getChecksum() const {
  516. Optional<ChecksumInfo<StringRef>> StringRefChecksum;
  517. if (Checksum)
  518. StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
  519. return StringRefChecksum;
  520. }
  521. Optional<StringRef> getSource() const {
  522. return Source ? Optional<StringRef>((*Source)->getString()) : None;
  523. }
  524. MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
  525. MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
  526. Optional<ChecksumInfo<MDString *>> getRawChecksum() const { return Checksum; }
  527. Optional<MDString *> getRawSource() const { return Source; }
  528. static StringRef getChecksumKindAsString(ChecksumKind CSKind);
  529. static Optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);
  530. static bool classof(const Metadata *MD) {
  531. return MD->getMetadataID() == DIFileKind;
  532. }
  533. };
  534. StringRef DIScope::getFilename() const {
  535. if (auto *F = getFile())
  536. return F->getFilename();
  537. return "";
  538. }
  539. StringRef DIScope::getDirectory() const {
  540. if (auto *F = getFile())
  541. return F->getDirectory();
  542. return "";
  543. }
  544. Optional<StringRef> DIScope::getSource() const {
  545. if (auto *F = getFile())
  546. return F->getSource();
  547. return None;
  548. }
  549. /// Base class for types.
  550. ///
  551. /// TODO: Remove the hardcoded name and context, since many types don't use
  552. /// them.
  553. /// TODO: Split up flags.
  554. class DIType : public DIScope {
  555. unsigned Line;
  556. DIFlags Flags;
  557. uint64_t SizeInBits;
  558. uint64_t OffsetInBits;
  559. uint32_t AlignInBits;
  560. protected:
  561. DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  562. unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
  563. uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
  564. : DIScope(C, ID, Storage, Tag, Ops) {
  565. init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
  566. }
  567. ~DIType() = default;
  568. void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
  569. uint64_t OffsetInBits, DIFlags Flags) {
  570. this->Line = Line;
  571. this->Flags = Flags;
  572. this->SizeInBits = SizeInBits;
  573. this->AlignInBits = AlignInBits;
  574. this->OffsetInBits = OffsetInBits;
  575. }
  576. /// Change fields in place.
  577. void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits,
  578. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) {
  579. assert(isDistinct() && "Only distinct nodes can mutate");
  580. setTag(Tag);
  581. init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
  582. }
  583. public:
  584. TempDIType clone() const {
  585. return TempDIType(cast<DIType>(MDNode::clone().release()));
  586. }
  587. unsigned getLine() const { return Line; }
  588. uint64_t getSizeInBits() const { return SizeInBits; }
  589. uint32_t getAlignInBits() const { return AlignInBits; }
  590. uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
  591. uint64_t getOffsetInBits() const { return OffsetInBits; }
  592. DIFlags getFlags() const { return Flags; }
  593. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  594. StringRef getName() const { return getStringOperand(2); }
  595. Metadata *getRawScope() const { return getOperand(1); }
  596. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  597. /// Returns a new temporary DIType with updated Flags
  598. TempDIType cloneWithFlags(DIFlags NewFlags) const {
  599. auto NewTy = clone();
  600. NewTy->Flags = NewFlags;
  601. return NewTy;
  602. }
  603. bool isPrivate() const {
  604. return (getFlags() & FlagAccessibility) == FlagPrivate;
  605. }
  606. bool isProtected() const {
  607. return (getFlags() & FlagAccessibility) == FlagProtected;
  608. }
  609. bool isPublic() const {
  610. return (getFlags() & FlagAccessibility) == FlagPublic;
  611. }
  612. bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
  613. bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
  614. bool isVirtual() const { return getFlags() & FlagVirtual; }
  615. bool isArtificial() const { return getFlags() & FlagArtificial; }
  616. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  617. bool isObjcClassComplete() const {
  618. return getFlags() & FlagObjcClassComplete;
  619. }
  620. bool isVector() const { return getFlags() & FlagVector; }
  621. bool isBitField() const { return getFlags() & FlagBitField; }
  622. bool isStaticMember() const { return getFlags() & FlagStaticMember; }
  623. bool isLValueReference() const { return getFlags() & FlagLValueReference; }
  624. bool isRValueReference() const { return getFlags() & FlagRValueReference; }
  625. bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
  626. bool isTypePassByReference() const {
  627. return getFlags() & FlagTypePassByReference;
  628. }
  629. bool isBigEndian() const { return getFlags() & FlagBigEndian; }
  630. bool isLittleEndian() const { return getFlags() & FlagLittleEndian; }
  631. bool getExportSymbols() const { return getFlags() & FlagExportSymbols; }
  632. static bool classof(const Metadata *MD) {
  633. switch (MD->getMetadataID()) {
  634. default:
  635. return false;
  636. case DIBasicTypeKind:
  637. case DIStringTypeKind:
  638. case DIDerivedTypeKind:
  639. case DICompositeTypeKind:
  640. case DISubroutineTypeKind:
  641. return true;
  642. }
  643. }
  644. };
  645. /// Basic type, like 'int' or 'float'.
  646. ///
  647. /// TODO: Split out DW_TAG_unspecified_type.
  648. /// TODO: Drop unused accessors.
  649. class DIBasicType : public DIType {
  650. friend class LLVMContextImpl;
  651. friend class MDNode;
  652. unsigned Encoding;
  653. DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
  654. uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
  655. DIFlags Flags, ArrayRef<Metadata *> Ops)
  656. : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
  657. Flags, Ops),
  658. Encoding(Encoding) {}
  659. ~DIBasicType() = default;
  660. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  661. StringRef Name, uint64_t SizeInBits,
  662. uint32_t AlignInBits, unsigned Encoding,
  663. DIFlags Flags, StorageType Storage,
  664. bool ShouldCreate = true) {
  665. return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
  666. SizeInBits, AlignInBits, Encoding, Flags, Storage,
  667. ShouldCreate);
  668. }
  669. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  670. MDString *Name, uint64_t SizeInBits,
  671. uint32_t AlignInBits, unsigned Encoding,
  672. DIFlags Flags, StorageType Storage,
  673. bool ShouldCreate = true);
  674. TempDIBasicType cloneImpl() const {
  675. return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
  676. getAlignInBits(), getEncoding(), getFlags());
  677. }
  678. public:
  679. DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
  680. (Tag, Name, 0, 0, 0, FlagZero))
  681. DEFINE_MDNODE_GET(DIBasicType,
  682. (unsigned Tag, StringRef Name, uint64_t SizeInBits),
  683. (Tag, Name, SizeInBits, 0, 0, FlagZero))
  684. DEFINE_MDNODE_GET(DIBasicType,
  685. (unsigned Tag, MDString *Name, uint64_t SizeInBits),
  686. (Tag, Name, SizeInBits, 0, 0, FlagZero))
  687. DEFINE_MDNODE_GET(DIBasicType,
  688. (unsigned Tag, StringRef Name, uint64_t SizeInBits,
  689. uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
  690. (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
  691. DEFINE_MDNODE_GET(DIBasicType,
  692. (unsigned Tag, MDString *Name, uint64_t SizeInBits,
  693. uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
  694. (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
  695. TempDIBasicType clone() const { return cloneImpl(); }
  696. unsigned getEncoding() const { return Encoding; }
  697. enum class Signedness { Signed, Unsigned };
  698. /// Return the signedness of this type, or None if this type is neither
  699. /// signed nor unsigned.
  700. Optional<Signedness> getSignedness() const;
  701. static bool classof(const Metadata *MD) {
  702. return MD->getMetadataID() == DIBasicTypeKind;
  703. }
  704. };
  705. /// String type, Fortran CHARACTER(n)
  706. class DIStringType : public DIType {
  707. friend class LLVMContextImpl;
  708. friend class MDNode;
  709. unsigned Encoding;
  710. DIStringType(LLVMContext &C, StorageType Storage, unsigned Tag,
  711. uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
  712. ArrayRef<Metadata *> Ops)
  713. : DIType(C, DIStringTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
  714. FlagZero, Ops),
  715. Encoding(Encoding) {}
  716. ~DIStringType() = default;
  717. static DIStringType *getImpl(LLVMContext &Context, unsigned Tag,
  718. StringRef Name, Metadata *StringLength,
  719. Metadata *StrLenExp, uint64_t SizeInBits,
  720. uint32_t AlignInBits, unsigned Encoding,
  721. StorageType Storage, bool ShouldCreate = true) {
  722. return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
  723. StringLength, StrLenExp, SizeInBits, AlignInBits, Encoding,
  724. Storage, ShouldCreate);
  725. }
  726. static DIStringType *getImpl(LLVMContext &Context, unsigned Tag,
  727. MDString *Name, Metadata *StringLength,
  728. Metadata *StrLenExp, uint64_t SizeInBits,
  729. uint32_t AlignInBits, unsigned Encoding,
  730. StorageType Storage, bool ShouldCreate = true);
  731. TempDIStringType cloneImpl() const {
  732. return getTemporary(getContext(), getTag(), getRawName(),
  733. getRawStringLength(), getRawStringLengthExp(),
  734. getSizeInBits(), getAlignInBits(), getEncoding());
  735. }
  736. public:
  737. DEFINE_MDNODE_GET(DIStringType,
  738. (unsigned Tag, StringRef Name, uint64_t SizeInBits,
  739. uint32_t AlignInBits),
  740. (Tag, Name, nullptr, nullptr, SizeInBits, AlignInBits, 0))
  741. DEFINE_MDNODE_GET(DIStringType,
  742. (unsigned Tag, MDString *Name, Metadata *StringLength,
  743. Metadata *StringLengthExp, uint64_t SizeInBits,
  744. uint32_t AlignInBits, unsigned Encoding),
  745. (Tag, Name, StringLength, StringLengthExp, SizeInBits,
  746. AlignInBits, Encoding))
  747. DEFINE_MDNODE_GET(DIStringType,
  748. (unsigned Tag, StringRef Name, Metadata *StringLength,
  749. Metadata *StringLengthExp, uint64_t SizeInBits,
  750. uint32_t AlignInBits, unsigned Encoding),
  751. (Tag, Name, StringLength, StringLengthExp, SizeInBits,
  752. AlignInBits, Encoding))
  753. TempDIStringType clone() const { return cloneImpl(); }
  754. static bool classof(const Metadata *MD) {
  755. return MD->getMetadataID() == DIStringTypeKind;
  756. }
  757. DIVariable *getStringLength() const {
  758. return cast_or_null<DIVariable>(getRawStringLength());
  759. }
  760. DIExpression *getStringLengthExp() const {
  761. return cast_or_null<DIExpression>(getRawStringLengthExp());
  762. }
  763. unsigned getEncoding() const { return Encoding; }
  764. Metadata *getRawStringLength() const { return getOperand(3); }
  765. Metadata *getRawStringLengthExp() const { return getOperand(4); }
  766. };
  767. /// Derived types.
  768. ///
  769. /// This includes qualified types, pointers, references, friends, typedefs, and
  770. /// class members.
  771. ///
  772. /// TODO: Split out members (inheritance, fields, methods, etc.).
  773. class DIDerivedType : public DIType {
  774. friend class LLVMContextImpl;
  775. friend class MDNode;
  776. /// The DWARF address space of the memory pointed to or referenced by a
  777. /// pointer or reference type respectively.
  778. Optional<unsigned> DWARFAddressSpace;
  779. DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
  780. unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
  781. uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace,
  782. DIFlags Flags, ArrayRef<Metadata *> Ops)
  783. : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
  784. AlignInBits, OffsetInBits, Flags, Ops),
  785. DWARFAddressSpace(DWARFAddressSpace) {}
  786. ~DIDerivedType() = default;
  787. static DIDerivedType *
  788. getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File,
  789. unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  790. uint32_t AlignInBits, uint64_t OffsetInBits,
  791. Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  792. Metadata *ExtraData, StorageType Storage, bool ShouldCreate = true) {
  793. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
  794. Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
  795. DWARFAddressSpace, Flags, ExtraData, Storage, ShouldCreate);
  796. }
  797. static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
  798. MDString *Name, Metadata *File, unsigned Line,
  799. Metadata *Scope, Metadata *BaseType,
  800. uint64_t SizeInBits, uint32_t AlignInBits,
  801. uint64_t OffsetInBits,
  802. Optional<unsigned> DWARFAddressSpace,
  803. DIFlags Flags, Metadata *ExtraData,
  804. StorageType Storage, bool ShouldCreate = true);
  805. TempDIDerivedType cloneImpl() const {
  806. return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
  807. getScope(), getBaseType(), getSizeInBits(),
  808. getAlignInBits(), getOffsetInBits(),
  809. getDWARFAddressSpace(), getFlags(), getExtraData());
  810. }
  811. public:
  812. DEFINE_MDNODE_GET(DIDerivedType,
  813. (unsigned Tag, MDString *Name, Metadata *File,
  814. unsigned Line, Metadata *Scope, Metadata *BaseType,
  815. uint64_t SizeInBits, uint32_t AlignInBits,
  816. uint64_t OffsetInBits,
  817. Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  818. Metadata *ExtraData = nullptr),
  819. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  820. AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
  821. ExtraData))
  822. DEFINE_MDNODE_GET(DIDerivedType,
  823. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  824. DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  825. uint32_t AlignInBits, uint64_t OffsetInBits,
  826. Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  827. Metadata *ExtraData = nullptr),
  828. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  829. AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
  830. ExtraData))
  831. TempDIDerivedType clone() const { return cloneImpl(); }
  832. /// Get the base type this is derived from.
  833. DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
  834. Metadata *getRawBaseType() const { return getOperand(3); }
  835. /// \returns The DWARF address space of the memory pointed to or referenced by
  836. /// a pointer or reference type respectively.
  837. Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; }
  838. /// Get extra data associated with this derived type.
  839. ///
  840. /// Class type for pointer-to-members, objective-c property node for ivars,
  841. /// global constant wrapper for static members, or virtual base pointer offset
  842. /// for inheritance.
  843. ///
  844. /// TODO: Separate out types that need this extra operand: pointer-to-member
  845. /// types and member fields (static members and ivars).
  846. Metadata *getExtraData() const { return getRawExtraData(); }
  847. Metadata *getRawExtraData() const { return getOperand(4); }
  848. /// Get casted version of extra data.
  849. /// @{
  850. DIType *getClassType() const {
  851. assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
  852. return cast_or_null<DIType>(getExtraData());
  853. }
  854. DIObjCProperty *getObjCProperty() const {
  855. return dyn_cast_or_null<DIObjCProperty>(getExtraData());
  856. }
  857. uint32_t getVBPtrOffset() const {
  858. assert(getTag() == dwarf::DW_TAG_inheritance);
  859. if (auto *CM = cast_or_null<ConstantAsMetadata>(getExtraData()))
  860. if (auto *CI = dyn_cast_or_null<ConstantInt>(CM->getValue()))
  861. return static_cast<uint32_t>(CI->getZExtValue());
  862. return 0;
  863. }
  864. Constant *getStorageOffsetInBits() const {
  865. assert(getTag() == dwarf::DW_TAG_member && isBitField());
  866. if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
  867. return C->getValue();
  868. return nullptr;
  869. }
  870. Constant *getConstant() const {
  871. assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
  872. if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
  873. return C->getValue();
  874. return nullptr;
  875. }
  876. Constant *getDiscriminantValue() const {
  877. assert(getTag() == dwarf::DW_TAG_member && !isStaticMember());
  878. if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
  879. return C->getValue();
  880. return nullptr;
  881. }
  882. /// @}
  883. static bool classof(const Metadata *MD) {
  884. return MD->getMetadataID() == DIDerivedTypeKind;
  885. }
  886. };
  887. /// Composite types.
  888. ///
  889. /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
  890. /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
  891. class DICompositeType : public DIType {
  892. friend class LLVMContextImpl;
  893. friend class MDNode;
  894. unsigned RuntimeLang;
  895. DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
  896. unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
  897. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  898. ArrayRef<Metadata *> Ops)
  899. : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
  900. AlignInBits, OffsetInBits, Flags, Ops),
  901. RuntimeLang(RuntimeLang) {}
  902. ~DICompositeType() = default;
  903. /// Change fields in place.
  904. void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
  905. uint64_t SizeInBits, uint32_t AlignInBits,
  906. uint64_t OffsetInBits, DIFlags Flags) {
  907. assert(isDistinct() && "Only distinct nodes can mutate");
  908. assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
  909. this->RuntimeLang = RuntimeLang;
  910. DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
  911. }
  912. static DICompositeType *
  913. getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
  914. unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  915. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  916. DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
  917. DITemplateParameterArray TemplateParams, StringRef Identifier,
  918. DIDerivedType *Discriminator, Metadata *DataLocation,
  919. Metadata *Associated, Metadata *Allocated, Metadata *Rank,
  920. StorageType Storage, bool ShouldCreate = true) {
  921. return getImpl(
  922. Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
  923. BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
  924. RuntimeLang, VTableHolder, TemplateParams.get(),
  925. getCanonicalMDString(Context, Identifier), Discriminator, DataLocation,
  926. Associated, Allocated, Rank, Storage, ShouldCreate);
  927. }
  928. static DICompositeType *
  929. getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
  930. unsigned Line, Metadata *Scope, Metadata *BaseType,
  931. uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
  932. DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
  933. Metadata *VTableHolder, Metadata *TemplateParams,
  934. MDString *Identifier, Metadata *Discriminator, Metadata *DataLocation,
  935. Metadata *Associated, Metadata *Allocated, Metadata *Rank,
  936. StorageType Storage, bool ShouldCreate = true);
  937. TempDICompositeType cloneImpl() const {
  938. return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
  939. getScope(), getBaseType(), getSizeInBits(),
  940. getAlignInBits(), getOffsetInBits(), getFlags(),
  941. getElements(), getRuntimeLang(), getVTableHolder(),
  942. getTemplateParams(), getIdentifier(),
  943. getDiscriminator(), getRawDataLocation(),
  944. getRawAssociated(), getRawAllocated(), getRawRank());
  945. }
  946. public:
  947. DEFINE_MDNODE_GET(
  948. DICompositeType,
  949. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  950. DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  951. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  952. DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
  953. DITemplateParameterArray TemplateParams = nullptr,
  954. StringRef Identifier = "", DIDerivedType *Discriminator = nullptr,
  955. Metadata *DataLocation = nullptr, Metadata *Associated = nullptr,
  956. Metadata *Allocated = nullptr, Metadata *Rank = nullptr),
  957. (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  958. OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams,
  959. Identifier, Discriminator, DataLocation, Associated, Allocated, Rank))
  960. DEFINE_MDNODE_GET(
  961. DICompositeType,
  962. (unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
  963. Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
  964. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  965. Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
  966. Metadata *TemplateParams = nullptr, MDString *Identifier = nullptr,
  967. Metadata *Discriminator = nullptr, Metadata *DataLocation = nullptr,
  968. Metadata *Associated = nullptr, Metadata *Allocated = nullptr,
  969. Metadata *Rank = nullptr),
  970. (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  971. OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams,
  972. Identifier, Discriminator, DataLocation, Associated, Allocated, Rank))
  973. TempDICompositeType clone() const { return cloneImpl(); }
  974. /// Get a DICompositeType with the given ODR identifier.
  975. ///
  976. /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
  977. /// DICompositeType for the given ODR \c Identifier. If none exists, creates
  978. /// a new node.
  979. ///
  980. /// Else, returns \c nullptr.
  981. static DICompositeType *
  982. getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
  983. MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
  984. Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
  985. uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
  986. unsigned RuntimeLang, Metadata *VTableHolder,
  987. Metadata *TemplateParams, Metadata *Discriminator,
  988. Metadata *DataLocation, Metadata *Associated, Metadata *Allocated,
  989. Metadata *Rank);
  990. static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
  991. MDString &Identifier);
  992. /// Build a DICompositeType with the given ODR identifier.
  993. ///
  994. /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If
  995. /// it doesn't exist, creates a new one. If it does exist and \a
  996. /// isForwardDecl(), and the new arguments would be a definition, mutates the
  997. /// the type in place. In either case, returns the type.
  998. ///
  999. /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
  1000. /// nullptr.
  1001. static DICompositeType *
  1002. buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
  1003. MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
  1004. Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
  1005. uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
  1006. unsigned RuntimeLang, Metadata *VTableHolder,
  1007. Metadata *TemplateParams, Metadata *Discriminator,
  1008. Metadata *DataLocation, Metadata *Associated,
  1009. Metadata *Allocated, Metadata *Rank);
  1010. DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
  1011. DINodeArray getElements() const {
  1012. return cast_or_null<MDTuple>(getRawElements());
  1013. }
  1014. DIType *getVTableHolder() const {
  1015. return cast_or_null<DIType>(getRawVTableHolder());
  1016. }
  1017. DITemplateParameterArray getTemplateParams() const {
  1018. return cast_or_null<MDTuple>(getRawTemplateParams());
  1019. }
  1020. StringRef getIdentifier() const { return getStringOperand(7); }
  1021. unsigned getRuntimeLang() const { return RuntimeLang; }
  1022. Metadata *getRawBaseType() const { return getOperand(3); }
  1023. Metadata *getRawElements() const { return getOperand(4); }
  1024. Metadata *getRawVTableHolder() const { return getOperand(5); }
  1025. Metadata *getRawTemplateParams() const { return getOperand(6); }
  1026. MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
  1027. Metadata *getRawDiscriminator() const { return getOperand(8); }
  1028. DIDerivedType *getDiscriminator() const { return getOperandAs<DIDerivedType>(8); }
  1029. Metadata *getRawDataLocation() const { return getOperand(9); }
  1030. DIVariable *getDataLocation() const {
  1031. return dyn_cast_or_null<DIVariable>(getRawDataLocation());
  1032. }
  1033. DIExpression *getDataLocationExp() const {
  1034. return dyn_cast_or_null<DIExpression>(getRawDataLocation());
  1035. }
  1036. Metadata *getRawAssociated() const { return getOperand(10); }
  1037. DIVariable *getAssociated() const {
  1038. return dyn_cast_or_null<DIVariable>(getRawAssociated());
  1039. }
  1040. DIExpression *getAssociatedExp() const {
  1041. return dyn_cast_or_null<DIExpression>(getRawAssociated());
  1042. }
  1043. Metadata *getRawAllocated() const { return getOperand(11); }
  1044. DIVariable *getAllocated() const {
  1045. return dyn_cast_or_null<DIVariable>(getRawAllocated());
  1046. }
  1047. DIExpression *getAllocatedExp() const {
  1048. return dyn_cast_or_null<DIExpression>(getRawAllocated());
  1049. }
  1050. Metadata *getRawRank() const { return getOperand(12); }
  1051. ConstantInt *getRankConst() const {
  1052. if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawRank()))
  1053. return dyn_cast_or_null<ConstantInt>(MD->getValue());
  1054. return nullptr;
  1055. }
  1056. DIExpression *getRankExp() const {
  1057. return dyn_cast_or_null<DIExpression>(getRawRank());
  1058. }
  1059. /// Replace operands.
  1060. ///
  1061. /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
  1062. /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
  1063. /// of its movement if necessary.
  1064. /// @{
  1065. void replaceElements(DINodeArray Elements) {
  1066. #ifndef NDEBUG
  1067. for (DINode *Op : getElements())
  1068. assert(is_contained(Elements->operands(), Op) &&
  1069. "Lost a member during member list replacement");
  1070. #endif
  1071. replaceOperandWith(4, Elements.get());
  1072. }
  1073. void replaceVTableHolder(DIType *VTableHolder) {
  1074. replaceOperandWith(5, VTableHolder);
  1075. }
  1076. void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
  1077. replaceOperandWith(6, TemplateParams.get());
  1078. }
  1079. /// @}
  1080. static bool classof(const Metadata *MD) {
  1081. return MD->getMetadataID() == DICompositeTypeKind;
  1082. }
  1083. };
  1084. /// Type array for a subprogram.
  1085. ///
  1086. /// TODO: Fold the array of types in directly as operands.
  1087. class DISubroutineType : public DIType {
  1088. friend class LLVMContextImpl;
  1089. friend class MDNode;
  1090. /// The calling convention used with DW_AT_calling_convention. Actually of
  1091. /// type dwarf::CallingConvention.
  1092. uint8_t CC;
  1093. DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags,
  1094. uint8_t CC, ArrayRef<Metadata *> Ops)
  1095. : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
  1096. 0, 0, 0, 0, Flags, Ops),
  1097. CC(CC) {}
  1098. ~DISubroutineType() = default;
  1099. static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
  1100. uint8_t CC, DITypeRefArray TypeArray,
  1101. StorageType Storage,
  1102. bool ShouldCreate = true) {
  1103. return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
  1104. }
  1105. static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
  1106. uint8_t CC, Metadata *TypeArray,
  1107. StorageType Storage,
  1108. bool ShouldCreate = true);
  1109. TempDISubroutineType cloneImpl() const {
  1110. return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
  1111. }
  1112. public:
  1113. DEFINE_MDNODE_GET(DISubroutineType,
  1114. (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
  1115. (Flags, CC, TypeArray))
  1116. DEFINE_MDNODE_GET(DISubroutineType,
  1117. (DIFlags Flags, uint8_t CC, Metadata *TypeArray),
  1118. (Flags, CC, TypeArray))
  1119. TempDISubroutineType clone() const { return cloneImpl(); }
  1120. uint8_t getCC() const { return CC; }
  1121. DITypeRefArray getTypeArray() const {
  1122. return cast_or_null<MDTuple>(getRawTypeArray());
  1123. }
  1124. Metadata *getRawTypeArray() const { return getOperand(3); }
  1125. static bool classof(const Metadata *MD) {
  1126. return MD->getMetadataID() == DISubroutineTypeKind;
  1127. }
  1128. };
  1129. /// Compile unit.
  1130. class DICompileUnit : public DIScope {
  1131. friend class LLVMContextImpl;
  1132. friend class MDNode;
  1133. public:
  1134. enum DebugEmissionKind : unsigned {
  1135. NoDebug = 0,
  1136. FullDebug,
  1137. LineTablesOnly,
  1138. DebugDirectivesOnly,
  1139. LastEmissionKind = DebugDirectivesOnly
  1140. };
  1141. enum class DebugNameTableKind : unsigned {
  1142. Default = 0,
  1143. GNU = 1,
  1144. None = 2,
  1145. LastDebugNameTableKind = None
  1146. };
  1147. static Optional<DebugEmissionKind> getEmissionKind(StringRef Str);
  1148. static const char *emissionKindString(DebugEmissionKind EK);
  1149. static Optional<DebugNameTableKind> getNameTableKind(StringRef Str);
  1150. static const char *nameTableKindString(DebugNameTableKind PK);
  1151. private:
  1152. unsigned SourceLanguage;
  1153. bool IsOptimized;
  1154. unsigned RuntimeVersion;
  1155. unsigned EmissionKind;
  1156. uint64_t DWOId;
  1157. bool SplitDebugInlining;
  1158. bool DebugInfoForProfiling;
  1159. unsigned NameTableKind;
  1160. bool RangesBaseAddress;
  1161. DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
  1162. bool IsOptimized, unsigned RuntimeVersion,
  1163. unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
  1164. bool DebugInfoForProfiling, unsigned NameTableKind,
  1165. bool RangesBaseAddress, ArrayRef<Metadata *> Ops)
  1166. : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
  1167. SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
  1168. RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
  1169. DWOId(DWOId), SplitDebugInlining(SplitDebugInlining),
  1170. DebugInfoForProfiling(DebugInfoForProfiling),
  1171. NameTableKind(NameTableKind), RangesBaseAddress(RangesBaseAddress) {
  1172. assert(Storage != Uniqued);
  1173. }
  1174. ~DICompileUnit() = default;
  1175. static DICompileUnit *
  1176. getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
  1177. StringRef Producer, bool IsOptimized, StringRef Flags,
  1178. unsigned RuntimeVersion, StringRef SplitDebugFilename,
  1179. unsigned EmissionKind, DICompositeTypeArray EnumTypes,
  1180. DIScopeArray RetainedTypes,
  1181. DIGlobalVariableExpressionArray GlobalVariables,
  1182. DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
  1183. uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
  1184. unsigned NameTableKind, bool RangesBaseAddress, StringRef SysRoot,
  1185. StringRef SDK, StorageType Storage, bool ShouldCreate = true) {
  1186. return getImpl(
  1187. Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
  1188. IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
  1189. getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
  1190. EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(),
  1191. ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining,
  1192. DebugInfoForProfiling, NameTableKind, RangesBaseAddress,
  1193. getCanonicalMDString(Context, SysRoot),
  1194. getCanonicalMDString(Context, SDK), Storage, ShouldCreate);
  1195. }
  1196. static DICompileUnit *
  1197. getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
  1198. MDString *Producer, bool IsOptimized, MDString *Flags,
  1199. unsigned RuntimeVersion, MDString *SplitDebugFilename,
  1200. unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
  1201. Metadata *GlobalVariables, Metadata *ImportedEntities,
  1202. Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
  1203. bool DebugInfoForProfiling, unsigned NameTableKind,
  1204. bool RangesBaseAddress, MDString *SysRoot, MDString *SDK,
  1205. StorageType Storage, bool ShouldCreate = true);
  1206. TempDICompileUnit cloneImpl() const {
  1207. return getTemporary(
  1208. getContext(), getSourceLanguage(), getFile(), getProducer(),
  1209. isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
  1210. getEmissionKind(), getEnumTypes(), getRetainedTypes(),
  1211. getGlobalVariables(), getImportedEntities(), getMacros(), DWOId,
  1212. getSplitDebugInlining(), getDebugInfoForProfiling(), getNameTableKind(),
  1213. getRangesBaseAddress(), getSysRoot(), getSDK());
  1214. }
  1215. public:
  1216. static void get() = delete;
  1217. static void getIfExists() = delete;
  1218. DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
  1219. DICompileUnit,
  1220. (unsigned SourceLanguage, DIFile *File, StringRef Producer,
  1221. bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
  1222. StringRef SplitDebugFilename, DebugEmissionKind EmissionKind,
  1223. DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
  1224. DIGlobalVariableExpressionArray GlobalVariables,
  1225. DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
  1226. uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
  1227. DebugNameTableKind NameTableKind, bool RangesBaseAddress,
  1228. StringRef SysRoot, StringRef SDK),
  1229. (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
  1230. SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
  1231. GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
  1232. DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress,
  1233. SysRoot, SDK))
  1234. DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
  1235. DICompileUnit,
  1236. (unsigned SourceLanguage, Metadata *File, MDString *Producer,
  1237. bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
  1238. MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
  1239. Metadata *RetainedTypes, Metadata *GlobalVariables,
  1240. Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
  1241. bool SplitDebugInlining, bool DebugInfoForProfiling,
  1242. unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot,
  1243. MDString *SDK),
  1244. (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
  1245. SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
  1246. GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
  1247. DebugInfoForProfiling, NameTableKind, RangesBaseAddress, SysRoot, SDK))
  1248. TempDICompileUnit clone() const { return cloneImpl(); }
  1249. unsigned getSourceLanguage() const { return SourceLanguage; }
  1250. bool isOptimized() const { return IsOptimized; }
  1251. unsigned getRuntimeVersion() const { return RuntimeVersion; }
  1252. DebugEmissionKind getEmissionKind() const {
  1253. return (DebugEmissionKind)EmissionKind;
  1254. }
  1255. bool isDebugDirectivesOnly() const {
  1256. return EmissionKind == DebugDirectivesOnly;
  1257. }
  1258. bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
  1259. DebugNameTableKind getNameTableKind() const {
  1260. return (DebugNameTableKind)NameTableKind;
  1261. }
  1262. bool getRangesBaseAddress() const { return RangesBaseAddress; }
  1263. StringRef getProducer() const { return getStringOperand(1); }
  1264. StringRef getFlags() const { return getStringOperand(2); }
  1265. StringRef getSplitDebugFilename() const { return getStringOperand(3); }
  1266. DICompositeTypeArray getEnumTypes() const {
  1267. return cast_or_null<MDTuple>(getRawEnumTypes());
  1268. }
  1269. DIScopeArray getRetainedTypes() const {
  1270. return cast_or_null<MDTuple>(getRawRetainedTypes());
  1271. }
  1272. DIGlobalVariableExpressionArray getGlobalVariables() const {
  1273. return cast_or_null<MDTuple>(getRawGlobalVariables());
  1274. }
  1275. DIImportedEntityArray getImportedEntities() const {
  1276. return cast_or_null<MDTuple>(getRawImportedEntities());
  1277. }
  1278. DIMacroNodeArray getMacros() const {
  1279. return cast_or_null<MDTuple>(getRawMacros());
  1280. }
  1281. uint64_t getDWOId() const { return DWOId; }
  1282. void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
  1283. bool getSplitDebugInlining() const { return SplitDebugInlining; }
  1284. void setSplitDebugInlining(bool SplitDebugInlining) {
  1285. this->SplitDebugInlining = SplitDebugInlining;
  1286. }
  1287. StringRef getSysRoot() const { return getStringOperand(9); }
  1288. StringRef getSDK() const { return getStringOperand(10); }
  1289. MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
  1290. MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
  1291. MDString *getRawSplitDebugFilename() const {
  1292. return getOperandAs<MDString>(3);
  1293. }
  1294. Metadata *getRawEnumTypes() const { return getOperand(4); }
  1295. Metadata *getRawRetainedTypes() const { return getOperand(5); }
  1296. Metadata *getRawGlobalVariables() const { return getOperand(6); }
  1297. Metadata *getRawImportedEntities() const { return getOperand(7); }
  1298. Metadata *getRawMacros() const { return getOperand(8); }
  1299. MDString *getRawSysRoot() const { return getOperandAs<MDString>(9); }
  1300. MDString *getRawSDK() const { return getOperandAs<MDString>(10); }
  1301. /// Replace arrays.
  1302. ///
  1303. /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
  1304. /// deleted on a uniquing collision. In practice, uniquing collisions on \a
  1305. /// DICompileUnit should be fairly rare.
  1306. /// @{
  1307. void replaceEnumTypes(DICompositeTypeArray N) {
  1308. replaceOperandWith(4, N.get());
  1309. }
  1310. void replaceRetainedTypes(DITypeArray N) {
  1311. replaceOperandWith(5, N.get());
  1312. }
  1313. void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
  1314. replaceOperandWith(6, N.get());
  1315. }
  1316. void replaceImportedEntities(DIImportedEntityArray N) {
  1317. replaceOperandWith(7, N.get());
  1318. }
  1319. void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
  1320. /// @}
  1321. static bool classof(const Metadata *MD) {
  1322. return MD->getMetadataID() == DICompileUnitKind;
  1323. }
  1324. };
  1325. /// A scope for locals.
  1326. ///
  1327. /// A legal scope for lexical blocks, local variables, and debug info
  1328. /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
  1329. /// DILexicalBlockFile.
  1330. class DILocalScope : public DIScope {
  1331. protected:
  1332. DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  1333. ArrayRef<Metadata *> Ops)
  1334. : DIScope(C, ID, Storage, Tag, Ops) {}
  1335. ~DILocalScope() = default;
  1336. public:
  1337. /// Get the subprogram for this scope.
  1338. ///
  1339. /// Return this if it's an \a DISubprogram; otherwise, look up the scope
  1340. /// chain.
  1341. DISubprogram *getSubprogram() const;
  1342. /// Get the first non DILexicalBlockFile scope of this scope.
  1343. ///
  1344. /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
  1345. /// scope chain.
  1346. DILocalScope *getNonLexicalBlockFileScope() const;
  1347. static bool classof(const Metadata *MD) {
  1348. return MD->getMetadataID() == DISubprogramKind ||
  1349. MD->getMetadataID() == DILexicalBlockKind ||
  1350. MD->getMetadataID() == DILexicalBlockFileKind;
  1351. }
  1352. };
  1353. /// Debug location.
  1354. ///
  1355. /// A debug location in source code, used for debug info and otherwise.
  1356. class DILocation : public MDNode {
  1357. friend class LLVMContextImpl;
  1358. friend class MDNode;
  1359. DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
  1360. unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
  1361. ~DILocation() { dropAllReferences(); }
  1362. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  1363. unsigned Column, Metadata *Scope,
  1364. Metadata *InlinedAt, bool ImplicitCode,
  1365. StorageType Storage, bool ShouldCreate = true);
  1366. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  1367. unsigned Column, DILocalScope *Scope,
  1368. DILocation *InlinedAt, bool ImplicitCode,
  1369. StorageType Storage, bool ShouldCreate = true) {
  1370. return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
  1371. static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
  1372. ShouldCreate);
  1373. }
  1374. /// With a given unsigned int \p U, use up to 13 bits to represent it.
  1375. /// old_bit 1~5 --> new_bit 1~5
  1376. /// old_bit 6~12 --> new_bit 7~13
  1377. /// new_bit_6 is 0 if higher bits (7~13) are all 0
  1378. static unsigned getPrefixEncodingFromUnsigned(unsigned U) {
  1379. U &= 0xfff;
  1380. return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
  1381. }
  1382. /// Reverse transformation as getPrefixEncodingFromUnsigned.
  1383. static unsigned getUnsignedFromPrefixEncoding(unsigned U) {
  1384. if (U & 1)
  1385. return 0;
  1386. U >>= 1;
  1387. return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
  1388. }
  1389. /// Returns the next component stored in discriminator.
  1390. static unsigned getNextComponentInDiscriminator(unsigned D) {
  1391. if ((D & 1) == 0)
  1392. return D >> ((D & 0x40) ? 14 : 7);
  1393. else
  1394. return D >> 1;
  1395. }
  1396. TempDILocation cloneImpl() const {
  1397. // Get the raw scope/inlinedAt since it is possible to invoke this on
  1398. // a DILocation containing temporary metadata.
  1399. return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
  1400. getRawInlinedAt(), isImplicitCode());
  1401. }
  1402. static unsigned encodeComponent(unsigned C) {
  1403. return (C == 0) ? 1U : (getPrefixEncodingFromUnsigned(C) << 1);
  1404. }
  1405. static unsigned encodingBits(unsigned C) {
  1406. return (C == 0) ? 1 : (C > 0x1f ? 14 : 7);
  1407. }
  1408. public:
  1409. // Disallow replacing operands.
  1410. void replaceOperandWith(unsigned I, Metadata *New) = delete;
  1411. DEFINE_MDNODE_GET(DILocation,
  1412. (unsigned Line, unsigned Column, Metadata *Scope,
  1413. Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
  1414. (Line, Column, Scope, InlinedAt, ImplicitCode))
  1415. DEFINE_MDNODE_GET(DILocation,
  1416. (unsigned Line, unsigned Column, DILocalScope *Scope,
  1417. DILocation *InlinedAt = nullptr,
  1418. bool ImplicitCode = false),
  1419. (Line, Column, Scope, InlinedAt, ImplicitCode))
  1420. /// Return a (temporary) clone of this.
  1421. TempDILocation clone() const { return cloneImpl(); }
  1422. unsigned getLine() const { return SubclassData32; }
  1423. unsigned getColumn() const { return SubclassData16; }
  1424. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1425. DILocation *getInlinedAt() const {
  1426. return cast_or_null<DILocation>(getRawInlinedAt());
  1427. }
  1428. /// Check if the location corresponds to an implicit code.
  1429. /// When the ImplicitCode flag is true, it means that the Instruction
  1430. /// with this DILocation has been added by the front-end but it hasn't been
  1431. /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
  1432. /// bracket). It's useful for code coverage to not show a counter on "empty"
  1433. /// lines.
  1434. bool isImplicitCode() const { return ImplicitCode; }
  1435. void setImplicitCode(bool ImplicitCode) { this->ImplicitCode = ImplicitCode; }
  1436. DIFile *getFile() const { return getScope()->getFile(); }
  1437. StringRef getFilename() const { return getScope()->getFilename(); }
  1438. StringRef getDirectory() const { return getScope()->getDirectory(); }
  1439. Optional<StringRef> getSource() const { return getScope()->getSource(); }
  1440. /// Get the scope where this is inlined.
  1441. ///
  1442. /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
  1443. /// location.
  1444. DILocalScope *getInlinedAtScope() const {
  1445. if (auto *IA = getInlinedAt())
  1446. return IA->getInlinedAtScope();
  1447. return getScope();
  1448. }
  1449. /// Get the DWARF discriminator.
  1450. ///
  1451. /// DWARF discriminators distinguish identical file locations between
  1452. /// instructions that are on different basic blocks.
  1453. ///
  1454. /// There are 3 components stored in discriminator, from lower bits:
  1455. ///
  1456. /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
  1457. /// that are defined by the same source line, but
  1458. /// different basic blocks.
  1459. /// Duplication factor: assigned by optimizations that will scale down
  1460. /// the execution frequency of the original IR.
  1461. /// Copy Identifier: assigned by optimizations that clones the IR.
  1462. /// Each copy of the IR will be assigned an identifier.
  1463. ///
  1464. /// Encoding:
  1465. ///
  1466. /// The above 3 components are encoded into a 32bit unsigned integer in
  1467. /// order. If the lowest bit is 1, the current component is empty, and the
  1468. /// next component will start in the next bit. Otherwise, the current
  1469. /// component is non-empty, and its content starts in the next bit. The
  1470. /// value of each components is either 5 bit or 12 bit: if the 7th bit
  1471. /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
  1472. /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
  1473. /// represent the component. Thus, the number of bits used for a component
  1474. /// is either 0 (if it and all the next components are empty); 1 - if it is
  1475. /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
  1476. /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
  1477. /// component is also capped at 0x1ff, even in the case when both first
  1478. /// components are 0, and we'd technically have 29 bits available.
  1479. ///
  1480. /// For precise control over the data being encoded in the discriminator,
  1481. /// use encodeDiscriminator/decodeDiscriminator.
  1482. inline unsigned getDiscriminator() const;
  1483. // For the regular discriminator, it stands for all empty components if all
  1484. // the lowest 3 bits are non-zero and all higher 29 bits are unused(zero by
  1485. // default). Here we fully leverage the higher 29 bits for pseudo probe use.
  1486. // This is the format:
  1487. // [2:0] - 0x7
  1488. // [31:3] - pseudo probe fields guaranteed to be non-zero as a whole
  1489. // So if the lower 3 bits is non-zero and the others has at least one
  1490. // non-zero bit, it guarantees to be a pseudo probe discriminator
  1491. inline static bool isPseudoProbeDiscriminator(unsigned Discriminator) {
  1492. return ((Discriminator & 0x7) == 0x7) && (Discriminator & 0xFFFFFFF8);
  1493. }
  1494. /// Returns a new DILocation with updated \p Discriminator.
  1495. inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
  1496. /// Returns a new DILocation with updated base discriminator \p BD. Only the
  1497. /// base discriminator is set in the new DILocation, the other encoded values
  1498. /// are elided.
  1499. /// If the discriminator cannot be encoded, the function returns None.
  1500. inline Optional<const DILocation *> cloneWithBaseDiscriminator(unsigned BD) const;
  1501. /// Returns the duplication factor stored in the discriminator, or 1 if no
  1502. /// duplication factor (or 0) is encoded.
  1503. inline unsigned getDuplicationFactor() const;
  1504. /// Returns the copy identifier stored in the discriminator.
  1505. inline unsigned getCopyIdentifier() const;
  1506. /// Returns the base discriminator stored in the discriminator.
  1507. inline unsigned getBaseDiscriminator() const;
  1508. /// Returns a new DILocation with duplication factor \p DF * current
  1509. /// duplication factor encoded in the discriminator. The current duplication
  1510. /// factor is as defined by getDuplicationFactor().
  1511. /// Returns None if encoding failed.
  1512. inline Optional<const DILocation *> cloneByMultiplyingDuplicationFactor(unsigned DF) const;
  1513. /// When two instructions are combined into a single instruction we also
  1514. /// need to combine the original locations into a single location.
  1515. ///
  1516. /// When the locations are the same we can use either location. When they
  1517. /// differ, we need a third location which is distinct from either. If they
  1518. /// have the same file/line but have a different discriminator we could
  1519. /// create a location with a new discriminator. If they are from different
  1520. /// files/lines the location is ambiguous and can't be represented in a line
  1521. /// entry. In this case, if \p GenerateLocation is true, we will set the
  1522. /// merged debug location as line 0 of the nearest common scope where the two
  1523. /// locations are inlined from.
  1524. ///
  1525. /// \p GenerateLocation: Whether the merged location can be generated when
  1526. /// \p LocA and \p LocB differ.
  1527. static const DILocation *getMergedLocation(const DILocation *LocA,
  1528. const DILocation *LocB);
  1529. /// Try to combine the vector of locations passed as input in a single one.
  1530. /// This function applies getMergedLocation() repeatedly left-to-right.
  1531. ///
  1532. /// \p Locs: The locations to be merged.
  1533. static
  1534. const DILocation *getMergedLocations(ArrayRef<const DILocation *> Locs);
  1535. /// Returns the base discriminator for a given encoded discriminator \p D.
  1536. static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
  1537. return getUnsignedFromPrefixEncoding(D);
  1538. }
  1539. /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor
  1540. /// have certain special case behavior (e.g. treating empty duplication factor
  1541. /// as the value '1').
  1542. /// This API, in conjunction with cloneWithDiscriminator, may be used to encode
  1543. /// the raw values provided. \p BD: base discriminator \p DF: duplication factor
  1544. /// \p CI: copy index
  1545. /// The return is None if the values cannot be encoded in 32 bits - for
  1546. /// example, values for BD or DF larger than 12 bits. Otherwise, the return
  1547. /// is the encoded value.
  1548. static Optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI);
  1549. /// Raw decoder for values in an encoded discriminator D.
  1550. static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
  1551. unsigned &CI);
  1552. /// Returns the duplication factor for a given encoded discriminator \p D, or
  1553. /// 1 if no value or 0 is encoded.
  1554. static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
  1555. D = getNextComponentInDiscriminator(D);
  1556. unsigned Ret = getUnsignedFromPrefixEncoding(D);
  1557. if (Ret == 0)
  1558. return 1;
  1559. return Ret;
  1560. }
  1561. /// Returns the copy identifier for a given encoded discriminator \p D.
  1562. static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
  1563. return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
  1564. getNextComponentInDiscriminator(D)));
  1565. }
  1566. Metadata *getRawScope() const { return getOperand(0); }
  1567. Metadata *getRawInlinedAt() const {
  1568. if (getNumOperands() == 2)
  1569. return getOperand(1);
  1570. return nullptr;
  1571. }
  1572. static bool classof(const Metadata *MD) {
  1573. return MD->getMetadataID() == DILocationKind;
  1574. }
  1575. };
  1576. /// Subprogram description.
  1577. class DISubprogram : public DILocalScope {
  1578. friend class LLVMContextImpl;
  1579. friend class MDNode;
  1580. unsigned Line;
  1581. unsigned ScopeLine;
  1582. unsigned VirtualIndex;
  1583. /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
  1584. /// of method overrides from secondary bases by this amount. It may be
  1585. /// negative.
  1586. int ThisAdjustment;
  1587. public:
  1588. /// Debug info subprogram flags.
  1589. enum DISPFlags : uint32_t {
  1590. #define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID,
  1591. #define DISP_FLAG_LARGEST_NEEDED
  1592. #include "llvm/IR/DebugInfoFlags.def"
  1593. SPFlagNonvirtual = SPFlagZero,
  1594. SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual,
  1595. LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest)
  1596. };
  1597. static DISPFlags getFlag(StringRef Flag);
  1598. static StringRef getFlagString(DISPFlags Flag);
  1599. /// Split up a flags bitfield for easier printing.
  1600. ///
  1601. /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
  1602. /// any remaining (unrecognized) bits.
  1603. static DISPFlags splitFlags(DISPFlags Flags,
  1604. SmallVectorImpl<DISPFlags> &SplitFlags);
  1605. // Helper for converting old bitfields to new flags word.
  1606. static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition,
  1607. bool IsOptimized,
  1608. unsigned Virtuality = SPFlagNonvirtual,
  1609. bool IsMainSubprogram = false) {
  1610. // We're assuming virtuality is the low-order field.
  1611. static_assert(
  1612. int(SPFlagVirtual) == int(dwarf::DW_VIRTUALITY_virtual) &&
  1613. int(SPFlagPureVirtual) == int(dwarf::DW_VIRTUALITY_pure_virtual),
  1614. "Virtuality constant mismatch");
  1615. return static_cast<DISPFlags>(
  1616. (Virtuality & SPFlagVirtuality) |
  1617. (IsLocalToUnit ? SPFlagLocalToUnit : SPFlagZero) |
  1618. (IsDefinition ? SPFlagDefinition : SPFlagZero) |
  1619. (IsOptimized ? SPFlagOptimized : SPFlagZero) |
  1620. (IsMainSubprogram ? SPFlagMainSubprogram : SPFlagZero));
  1621. }
  1622. private:
  1623. DIFlags Flags;
  1624. DISPFlags SPFlags;
  1625. DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
  1626. unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment,
  1627. DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops)
  1628. : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
  1629. Ops),
  1630. Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
  1631. ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags) {
  1632. static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
  1633. }
  1634. ~DISubprogram() = default;
  1635. static DISubprogram *
  1636. getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
  1637. StringRef LinkageName, DIFile *File, unsigned Line,
  1638. DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType,
  1639. unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
  1640. DISPFlags SPFlags, DICompileUnit *Unit,
  1641. DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
  1642. DINodeArray RetainedNodes, DITypeArray ThrownTypes,
  1643. StorageType Storage, bool ShouldCreate = true) {
  1644. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1645. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  1646. ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
  1647. Flags, SPFlags, Unit, TemplateParams.get(), Declaration,
  1648. RetainedNodes.get(), ThrownTypes.get(), Storage,
  1649. ShouldCreate);
  1650. }
  1651. static DISubprogram *getImpl(LLVMContext &Context, Metadata *Scope,
  1652. MDString *Name, MDString *LinkageName,
  1653. Metadata *File, unsigned Line, Metadata *Type,
  1654. unsigned ScopeLine, Metadata *ContainingType,
  1655. unsigned VirtualIndex, int ThisAdjustment,
  1656. DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
  1657. Metadata *TemplateParams, Metadata *Declaration,
  1658. Metadata *RetainedNodes, Metadata *ThrownTypes,
  1659. StorageType Storage, bool ShouldCreate = true);
  1660. TempDISubprogram cloneImpl() const {
  1661. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  1662. getFile(), getLine(), getType(), getScopeLine(),
  1663. getContainingType(), getVirtualIndex(),
  1664. getThisAdjustment(), getFlags(), getSPFlags(),
  1665. getUnit(), getTemplateParams(), getDeclaration(),
  1666. getRetainedNodes(), getThrownTypes());
  1667. }
  1668. public:
  1669. DEFINE_MDNODE_GET(
  1670. DISubprogram,
  1671. (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File,
  1672. unsigned Line, DISubroutineType *Type, unsigned ScopeLine,
  1673. DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
  1674. DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit,
  1675. DITemplateParameterArray TemplateParams = nullptr,
  1676. DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
  1677. DITypeArray ThrownTypes = nullptr),
  1678. (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
  1679. VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
  1680. Declaration, RetainedNodes, ThrownTypes))
  1681. DEFINE_MDNODE_GET(
  1682. DISubprogram,
  1683. (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
  1684. unsigned Line, Metadata *Type, unsigned ScopeLine,
  1685. Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
  1686. DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
  1687. Metadata *TemplateParams = nullptr, Metadata *Declaration = nullptr,
  1688. Metadata *RetainedNodes = nullptr, Metadata *ThrownTypes = nullptr),
  1689. (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
  1690. VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
  1691. Declaration, RetainedNodes, ThrownTypes))
  1692. TempDISubprogram clone() const { return cloneImpl(); }
  1693. /// Returns a new temporary DISubprogram with updated Flags
  1694. TempDISubprogram cloneWithFlags(DIFlags NewFlags) const {
  1695. auto NewSP = clone();
  1696. NewSP->Flags = NewFlags;
  1697. return NewSP;
  1698. }
  1699. public:
  1700. unsigned getLine() const { return Line; }
  1701. unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; }
  1702. unsigned getVirtualIndex() const { return VirtualIndex; }
  1703. int getThisAdjustment() const { return ThisAdjustment; }
  1704. unsigned getScopeLine() const { return ScopeLine; }
  1705. DIFlags getFlags() const { return Flags; }
  1706. DISPFlags getSPFlags() const { return SPFlags; }
  1707. bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; }
  1708. bool isDefinition() const { return getSPFlags() & SPFlagDefinition; }
  1709. bool isOptimized() const { return getSPFlags() & SPFlagOptimized; }
  1710. bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; }
  1711. bool isArtificial() const { return getFlags() & FlagArtificial; }
  1712. bool isPrivate() const {
  1713. return (getFlags() & FlagAccessibility) == FlagPrivate;
  1714. }
  1715. bool isProtected() const {
  1716. return (getFlags() & FlagAccessibility) == FlagProtected;
  1717. }
  1718. bool isPublic() const {
  1719. return (getFlags() & FlagAccessibility) == FlagPublic;
  1720. }
  1721. bool isExplicit() const { return getFlags() & FlagExplicit; }
  1722. bool isPrototyped() const { return getFlags() & FlagPrototyped; }
  1723. bool areAllCallsDescribed() const {
  1724. return getFlags() & FlagAllCallsDescribed;
  1725. }
  1726. bool isPure() const { return getSPFlags() & SPFlagPure; }
  1727. bool isElemental() const { return getSPFlags() & SPFlagElemental; }
  1728. bool isRecursive() const { return getSPFlags() & SPFlagRecursive; }
  1729. bool isObjCDirect() const { return getSPFlags() & SPFlagObjCDirect; }
  1730. /// Check if this is deleted member function.
  1731. ///
  1732. /// Return true if this subprogram is a C++11 special
  1733. /// member function declared deleted.
  1734. bool isDeleted() const { return getSPFlags() & SPFlagDeleted; }
  1735. /// Check if this is reference-qualified.
  1736. ///
  1737. /// Return true if this subprogram is a C++11 reference-qualified non-static
  1738. /// member function (void foo() &).
  1739. bool isLValueReference() const { return getFlags() & FlagLValueReference; }
  1740. /// Check if this is rvalue-reference-qualified.
  1741. ///
  1742. /// Return true if this subprogram is a C++11 rvalue-reference-qualified
  1743. /// non-static member function (void foo() &&).
  1744. bool isRValueReference() const { return getFlags() & FlagRValueReference; }
  1745. /// Check if this is marked as noreturn.
  1746. ///
  1747. /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
  1748. bool isNoReturn() const { return getFlags() & FlagNoReturn; }
  1749. // Check if this routine is a compiler-generated thunk.
  1750. //
  1751. // Returns true if this subprogram is a thunk generated by the compiler.
  1752. bool isThunk() const { return getFlags() & FlagThunk; }
  1753. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1754. StringRef getName() const { return getStringOperand(2); }
  1755. StringRef getLinkageName() const { return getStringOperand(3); }
  1756. DISubroutineType *getType() const {
  1757. return cast_or_null<DISubroutineType>(getRawType());
  1758. }
  1759. DIType *getContainingType() const {
  1760. return cast_or_null<DIType>(getRawContainingType());
  1761. }
  1762. DICompileUnit *getUnit() const {
  1763. return cast_or_null<DICompileUnit>(getRawUnit());
  1764. }
  1765. void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
  1766. DITemplateParameterArray getTemplateParams() const {
  1767. return cast_or_null<MDTuple>(getRawTemplateParams());
  1768. }
  1769. DISubprogram *getDeclaration() const {
  1770. return cast_or_null<DISubprogram>(getRawDeclaration());
  1771. }
  1772. DINodeArray getRetainedNodes() const {
  1773. return cast_or_null<MDTuple>(getRawRetainedNodes());
  1774. }
  1775. DITypeArray getThrownTypes() const {
  1776. return cast_or_null<MDTuple>(getRawThrownTypes());
  1777. }
  1778. Metadata *getRawScope() const { return getOperand(1); }
  1779. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1780. MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
  1781. Metadata *getRawType() const { return getOperand(4); }
  1782. Metadata *getRawUnit() const { return getOperand(5); }
  1783. Metadata *getRawDeclaration() const { return getOperand(6); }
  1784. Metadata *getRawRetainedNodes() const { return getOperand(7); }
  1785. Metadata *getRawContainingType() const {
  1786. return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
  1787. }
  1788. Metadata *getRawTemplateParams() const {
  1789. return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
  1790. }
  1791. Metadata *getRawThrownTypes() const {
  1792. return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
  1793. }
  1794. void replaceRawLinkageName(MDString *LinkageName) {
  1795. replaceOperandWith(3, LinkageName);
  1796. }
  1797. /// Check if this subprogram describes the given function.
  1798. ///
  1799. /// FIXME: Should this be looking through bitcasts?
  1800. bool describes(const Function *F) const;
  1801. static bool classof(const Metadata *MD) {
  1802. return MD->getMetadataID() == DISubprogramKind;
  1803. }
  1804. };
  1805. class DILexicalBlockBase : public DILocalScope {
  1806. protected:
  1807. DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
  1808. ArrayRef<Metadata *> Ops)
  1809. : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
  1810. ~DILexicalBlockBase() = default;
  1811. public:
  1812. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1813. Metadata *getRawScope() const { return getOperand(1); }
  1814. static bool classof(const Metadata *MD) {
  1815. return MD->getMetadataID() == DILexicalBlockKind ||
  1816. MD->getMetadataID() == DILexicalBlockFileKind;
  1817. }
  1818. };
  1819. class DILexicalBlock : public DILexicalBlockBase {
  1820. friend class LLVMContextImpl;
  1821. friend class MDNode;
  1822. unsigned Line;
  1823. uint16_t Column;
  1824. DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
  1825. unsigned Column, ArrayRef<Metadata *> Ops)
  1826. : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
  1827. Column(Column) {
  1828. assert(Column < (1u << 16) && "Expected 16-bit column");
  1829. }
  1830. ~DILexicalBlock() = default;
  1831. static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1832. DIFile *File, unsigned Line, unsigned Column,
  1833. StorageType Storage,
  1834. bool ShouldCreate = true) {
  1835. return getImpl(Context, static_cast<Metadata *>(Scope),
  1836. static_cast<Metadata *>(File), Line, Column, Storage,
  1837. ShouldCreate);
  1838. }
  1839. static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
  1840. Metadata *File, unsigned Line, unsigned Column,
  1841. StorageType Storage, bool ShouldCreate = true);
  1842. TempDILexicalBlock cloneImpl() const {
  1843. return getTemporary(getContext(), getScope(), getFile(), getLine(),
  1844. getColumn());
  1845. }
  1846. public:
  1847. DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
  1848. unsigned Line, unsigned Column),
  1849. (Scope, File, Line, Column))
  1850. DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
  1851. unsigned Line, unsigned Column),
  1852. (Scope, File, Line, Column))
  1853. TempDILexicalBlock clone() const { return cloneImpl(); }
  1854. unsigned getLine() const { return Line; }
  1855. unsigned getColumn() const { return Column; }
  1856. static bool classof(const Metadata *MD) {
  1857. return MD->getMetadataID() == DILexicalBlockKind;
  1858. }
  1859. };
  1860. class DILexicalBlockFile : public DILexicalBlockBase {
  1861. friend class LLVMContextImpl;
  1862. friend class MDNode;
  1863. unsigned Discriminator;
  1864. DILexicalBlockFile(LLVMContext &C, StorageType Storage,
  1865. unsigned Discriminator, ArrayRef<Metadata *> Ops)
  1866. : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
  1867. Discriminator(Discriminator) {}
  1868. ~DILexicalBlockFile() = default;
  1869. static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1870. DIFile *File, unsigned Discriminator,
  1871. StorageType Storage,
  1872. bool ShouldCreate = true) {
  1873. return getImpl(Context, static_cast<Metadata *>(Scope),
  1874. static_cast<Metadata *>(File), Discriminator, Storage,
  1875. ShouldCreate);
  1876. }
  1877. static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
  1878. Metadata *File, unsigned Discriminator,
  1879. StorageType Storage,
  1880. bool ShouldCreate = true);
  1881. TempDILexicalBlockFile cloneImpl() const {
  1882. return getTemporary(getContext(), getScope(), getFile(),
  1883. getDiscriminator());
  1884. }
  1885. public:
  1886. DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
  1887. unsigned Discriminator),
  1888. (Scope, File, Discriminator))
  1889. DEFINE_MDNODE_GET(DILexicalBlockFile,
  1890. (Metadata * Scope, Metadata *File, unsigned Discriminator),
  1891. (Scope, File, Discriminator))
  1892. TempDILexicalBlockFile clone() const { return cloneImpl(); }
  1893. // TODO: Remove these once they're gone from DILexicalBlockBase.
  1894. unsigned getLine() const = delete;
  1895. unsigned getColumn() const = delete;
  1896. unsigned getDiscriminator() const { return Discriminator; }
  1897. static bool classof(const Metadata *MD) {
  1898. return MD->getMetadataID() == DILexicalBlockFileKind;
  1899. }
  1900. };
  1901. unsigned DILocation::getDiscriminator() const {
  1902. if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
  1903. return F->getDiscriminator();
  1904. return 0;
  1905. }
  1906. const DILocation *
  1907. DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
  1908. DIScope *Scope = getScope();
  1909. // Skip all parent DILexicalBlockFile that already have a discriminator
  1910. // assigned. We do not want to have nested DILexicalBlockFiles that have
  1911. // mutliple discriminators because only the leaf DILexicalBlockFile's
  1912. // dominator will be used.
  1913. for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
  1914. LBF && LBF->getDiscriminator() != 0;
  1915. LBF = dyn_cast<DILexicalBlockFile>(Scope))
  1916. Scope = LBF->getScope();
  1917. DILexicalBlockFile *NewScope =
  1918. DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
  1919. return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
  1920. getInlinedAt());
  1921. }
  1922. unsigned DILocation::getBaseDiscriminator() const {
  1923. return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
  1924. }
  1925. unsigned DILocation::getDuplicationFactor() const {
  1926. return getDuplicationFactorFromDiscriminator(getDiscriminator());
  1927. }
  1928. unsigned DILocation::getCopyIdentifier() const {
  1929. return getCopyIdentifierFromDiscriminator(getDiscriminator());
  1930. }
  1931. Optional<const DILocation *> DILocation::cloneWithBaseDiscriminator(unsigned D) const {
  1932. unsigned BD, DF, CI;
  1933. decodeDiscriminator(getDiscriminator(), BD, DF, CI);
  1934. if (D == BD)
  1935. return this;
  1936. if (Optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI))
  1937. return cloneWithDiscriminator(*Encoded);
  1938. return None;
  1939. }
  1940. Optional<const DILocation *> DILocation::cloneByMultiplyingDuplicationFactor(unsigned DF) const {
  1941. DF *= getDuplicationFactor();
  1942. if (DF <= 1)
  1943. return this;
  1944. unsigned BD = getBaseDiscriminator();
  1945. unsigned CI = getCopyIdentifier();
  1946. if (Optional<unsigned> D = encodeDiscriminator(BD, DF, CI))
  1947. return cloneWithDiscriminator(*D);
  1948. return None;
  1949. }
  1950. class DINamespace : public DIScope {
  1951. friend class LLVMContextImpl;
  1952. friend class MDNode;
  1953. unsigned ExportSymbols : 1;
  1954. DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
  1955. ArrayRef<Metadata *> Ops)
  1956. : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
  1957. Ops),
  1958. ExportSymbols(ExportSymbols) {}
  1959. ~DINamespace() = default;
  1960. static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
  1961. StringRef Name, bool ExportSymbols,
  1962. StorageType Storage, bool ShouldCreate = true) {
  1963. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1964. ExportSymbols, Storage, ShouldCreate);
  1965. }
  1966. static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
  1967. MDString *Name, bool ExportSymbols,
  1968. StorageType Storage, bool ShouldCreate = true);
  1969. TempDINamespace cloneImpl() const {
  1970. return getTemporary(getContext(), getScope(), getName(),
  1971. getExportSymbols());
  1972. }
  1973. public:
  1974. DEFINE_MDNODE_GET(DINamespace,
  1975. (DIScope *Scope, StringRef Name, bool ExportSymbols),
  1976. (Scope, Name, ExportSymbols))
  1977. DEFINE_MDNODE_GET(DINamespace,
  1978. (Metadata *Scope, MDString *Name, bool ExportSymbols),
  1979. (Scope, Name, ExportSymbols))
  1980. TempDINamespace clone() const { return cloneImpl(); }
  1981. bool getExportSymbols() const { return ExportSymbols; }
  1982. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1983. StringRef getName() const { return getStringOperand(2); }
  1984. Metadata *getRawScope() const { return getOperand(1); }
  1985. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1986. static bool classof(const Metadata *MD) {
  1987. return MD->getMetadataID() == DINamespaceKind;
  1988. }
  1989. };
  1990. /// Represents a module in the programming language, for example, a Clang
  1991. /// module, or a Fortran module.
  1992. class DIModule : public DIScope {
  1993. friend class LLVMContextImpl;
  1994. friend class MDNode;
  1995. unsigned LineNo;
  1996. bool IsDecl;
  1997. DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo,
  1998. bool IsDecl, ArrayRef<Metadata *> Ops)
  1999. : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops),
  2000. LineNo(LineNo), IsDecl(IsDecl) {}
  2001. ~DIModule() = default;
  2002. static DIModule *getImpl(LLVMContext &Context, DIFile *File, DIScope *Scope,
  2003. StringRef Name, StringRef ConfigurationMacros,
  2004. StringRef IncludePath, StringRef APINotesFile,
  2005. unsigned LineNo, bool IsDecl, StorageType Storage,
  2006. bool ShouldCreate = true) {
  2007. return getImpl(Context, File, Scope, getCanonicalMDString(Context, Name),
  2008. getCanonicalMDString(Context, ConfigurationMacros),
  2009. getCanonicalMDString(Context, IncludePath),
  2010. getCanonicalMDString(Context, APINotesFile), LineNo, IsDecl,
  2011. Storage, ShouldCreate);
  2012. }
  2013. static DIModule *getImpl(LLVMContext &Context, Metadata *File,
  2014. Metadata *Scope, MDString *Name,
  2015. MDString *ConfigurationMacros, MDString *IncludePath,
  2016. MDString *APINotesFile, unsigned LineNo, bool IsDecl,
  2017. StorageType Storage, bool ShouldCreate = true);
  2018. TempDIModule cloneImpl() const {
  2019. return getTemporary(getContext(), getFile(), getScope(), getName(),
  2020. getConfigurationMacros(), getIncludePath(),
  2021. getAPINotesFile(), getLineNo(), getIsDecl());
  2022. }
  2023. public:
  2024. DEFINE_MDNODE_GET(DIModule,
  2025. (DIFile * File, DIScope *Scope, StringRef Name,
  2026. StringRef ConfigurationMacros, StringRef IncludePath,
  2027. StringRef APINotesFile, unsigned LineNo,
  2028. bool IsDecl = false),
  2029. (File, Scope, Name, ConfigurationMacros, IncludePath,
  2030. APINotesFile, LineNo, IsDecl))
  2031. DEFINE_MDNODE_GET(DIModule,
  2032. (Metadata * File, Metadata *Scope, MDString *Name,
  2033. MDString *ConfigurationMacros, MDString *IncludePath,
  2034. MDString *APINotesFile, unsigned LineNo,
  2035. bool IsDecl = false),
  2036. (File, Scope, Name, ConfigurationMacros, IncludePath,
  2037. APINotesFile, LineNo, IsDecl))
  2038. TempDIModule clone() const { return cloneImpl(); }
  2039. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2040. StringRef getName() const { return getStringOperand(2); }
  2041. StringRef getConfigurationMacros() const { return getStringOperand(3); }
  2042. StringRef getIncludePath() const { return getStringOperand(4); }
  2043. StringRef getAPINotesFile() const { return getStringOperand(5); }
  2044. unsigned getLineNo() const { return LineNo; }
  2045. bool getIsDecl() const { return IsDecl; }
  2046. Metadata *getRawScope() const { return getOperand(1); }
  2047. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  2048. MDString *getRawConfigurationMacros() const {
  2049. return getOperandAs<MDString>(3);
  2050. }
  2051. MDString *getRawIncludePath() const { return getOperandAs<MDString>(4); }
  2052. MDString *getRawAPINotesFile() const { return getOperandAs<MDString>(5); }
  2053. static bool classof(const Metadata *MD) {
  2054. return MD->getMetadataID() == DIModuleKind;
  2055. }
  2056. };
  2057. /// Base class for template parameters.
  2058. class DITemplateParameter : public DINode {
  2059. protected:
  2060. bool IsDefault;
  2061. DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
  2062. unsigned Tag, bool IsDefault, ArrayRef<Metadata *> Ops)
  2063. : DINode(Context, ID, Storage, Tag, Ops), IsDefault(IsDefault) {}
  2064. ~DITemplateParameter() = default;
  2065. public:
  2066. StringRef getName() const { return getStringOperand(0); }
  2067. DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
  2068. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  2069. Metadata *getRawType() const { return getOperand(1); }
  2070. bool isDefault() const { return IsDefault; }
  2071. static bool classof(const Metadata *MD) {
  2072. return MD->getMetadataID() == DITemplateTypeParameterKind ||
  2073. MD->getMetadataID() == DITemplateValueParameterKind;
  2074. }
  2075. };
  2076. class DITemplateTypeParameter : public DITemplateParameter {
  2077. friend class LLVMContextImpl;
  2078. friend class MDNode;
  2079. DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
  2080. bool IsDefault, ArrayRef<Metadata *> Ops)
  2081. : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
  2082. dwarf::DW_TAG_template_type_parameter, IsDefault,
  2083. Ops) {}
  2084. ~DITemplateTypeParameter() = default;
  2085. static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
  2086. DIType *Type, bool IsDefault,
  2087. StorageType Storage,
  2088. bool ShouldCreate = true) {
  2089. return getImpl(Context, getCanonicalMDString(Context, Name), Type,
  2090. IsDefault, Storage, ShouldCreate);
  2091. }
  2092. static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
  2093. Metadata *Type, bool IsDefault,
  2094. StorageType Storage,
  2095. bool ShouldCreate = true);
  2096. TempDITemplateTypeParameter cloneImpl() const {
  2097. return getTemporary(getContext(), getName(), getType(), isDefault());
  2098. }
  2099. public:
  2100. DEFINE_MDNODE_GET(DITemplateTypeParameter,
  2101. (StringRef Name, DIType *Type, bool IsDefault),
  2102. (Name, Type, IsDefault))
  2103. DEFINE_MDNODE_GET(DITemplateTypeParameter,
  2104. (MDString *Name, Metadata *Type, bool IsDefault),
  2105. (Name, Type, IsDefault))
  2106. TempDITemplateTypeParameter clone() const { return cloneImpl(); }
  2107. static bool classof(const Metadata *MD) {
  2108. return MD->getMetadataID() == DITemplateTypeParameterKind;
  2109. }
  2110. };
  2111. class DITemplateValueParameter : public DITemplateParameter {
  2112. friend class LLVMContextImpl;
  2113. friend class MDNode;
  2114. DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
  2115. unsigned Tag, bool IsDefault,
  2116. ArrayRef<Metadata *> Ops)
  2117. : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
  2118. IsDefault, Ops) {}
  2119. ~DITemplateValueParameter() = default;
  2120. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  2121. StringRef Name, DIType *Type,
  2122. bool IsDefault, Metadata *Value,
  2123. StorageType Storage,
  2124. bool ShouldCreate = true) {
  2125. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
  2126. IsDefault, Value, Storage, ShouldCreate);
  2127. }
  2128. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  2129. MDString *Name, Metadata *Type,
  2130. bool IsDefault, Metadata *Value,
  2131. StorageType Storage,
  2132. bool ShouldCreate = true);
  2133. TempDITemplateValueParameter cloneImpl() const {
  2134. return getTemporary(getContext(), getTag(), getName(), getType(),
  2135. isDefault(), getValue());
  2136. }
  2137. public:
  2138. DEFINE_MDNODE_GET(DITemplateValueParameter,
  2139. (unsigned Tag, StringRef Name, DIType *Type, bool IsDefault,
  2140. Metadata *Value),
  2141. (Tag, Name, Type, IsDefault, Value))
  2142. DEFINE_MDNODE_GET(DITemplateValueParameter,
  2143. (unsigned Tag, MDString *Name, Metadata *Type,
  2144. bool IsDefault, Metadata *Value),
  2145. (Tag, Name, Type, IsDefault, Value))
  2146. TempDITemplateValueParameter clone() const { return cloneImpl(); }
  2147. Metadata *getValue() const { return getOperand(2); }
  2148. static bool classof(const Metadata *MD) {
  2149. return MD->getMetadataID() == DITemplateValueParameterKind;
  2150. }
  2151. };
  2152. /// Base class for variables.
  2153. class DIVariable : public DINode {
  2154. unsigned Line;
  2155. uint32_t AlignInBits;
  2156. protected:
  2157. DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
  2158. ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0)
  2159. : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line),
  2160. AlignInBits(AlignInBits) {}
  2161. ~DIVariable() = default;
  2162. public:
  2163. unsigned getLine() const { return Line; }
  2164. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2165. StringRef getName() const { return getStringOperand(1); }
  2166. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2167. DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
  2168. uint32_t getAlignInBits() const { return AlignInBits; }
  2169. uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
  2170. /// Determines the size of the variable's type.
  2171. Optional<uint64_t> getSizeInBits() const;
  2172. /// Return the signedness of this variable's type, or None if this type is
  2173. /// neither signed nor unsigned.
  2174. Optional<DIBasicType::Signedness> getSignedness() const {
  2175. if (auto *BT = dyn_cast<DIBasicType>(getType()))
  2176. return BT->getSignedness();
  2177. return None;
  2178. }
  2179. StringRef getFilename() const {
  2180. if (auto *F = getFile())
  2181. return F->getFilename();
  2182. return "";
  2183. }
  2184. StringRef getDirectory() const {
  2185. if (auto *F = getFile())
  2186. return F->getDirectory();
  2187. return "";
  2188. }
  2189. Optional<StringRef> getSource() const {
  2190. if (auto *F = getFile())
  2191. return F->getSource();
  2192. return None;
  2193. }
  2194. Metadata *getRawScope() const { return getOperand(0); }
  2195. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  2196. Metadata *getRawFile() const { return getOperand(2); }
  2197. Metadata *getRawType() const { return getOperand(3); }
  2198. static bool classof(const Metadata *MD) {
  2199. return MD->getMetadataID() == DILocalVariableKind ||
  2200. MD->getMetadataID() == DIGlobalVariableKind;
  2201. }
  2202. };
  2203. /// DWARF expression.
  2204. ///
  2205. /// This is (almost) a DWARF expression that modifies the location of a
  2206. /// variable, or the location of a single piece of a variable, or (when using
  2207. /// DW_OP_stack_value) is the constant variable value.
  2208. ///
  2209. /// TODO: Co-allocate the expression elements.
  2210. /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
  2211. /// storage types.
  2212. class DIExpression : public MDNode {
  2213. friend class LLVMContextImpl;
  2214. friend class MDNode;
  2215. std::vector<uint64_t> Elements;
  2216. DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
  2217. : MDNode(C, DIExpressionKind, Storage, None),
  2218. Elements(Elements.begin(), Elements.end()) {}
  2219. ~DIExpression() = default;
  2220. static DIExpression *getImpl(LLVMContext &Context,
  2221. ArrayRef<uint64_t> Elements, StorageType Storage,
  2222. bool ShouldCreate = true);
  2223. TempDIExpression cloneImpl() const {
  2224. return getTemporary(getContext(), getElements());
  2225. }
  2226. public:
  2227. DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
  2228. TempDIExpression clone() const { return cloneImpl(); }
  2229. ArrayRef<uint64_t> getElements() const { return Elements; }
  2230. unsigned getNumElements() const { return Elements.size(); }
  2231. uint64_t getElement(unsigned I) const {
  2232. assert(I < Elements.size() && "Index out of range");
  2233. return Elements[I];
  2234. }
  2235. /// Determine whether this represents a standalone constant value.
  2236. bool isConstant() const;
  2237. /// Determine whether this represents a standalone signed constant value.
  2238. bool isSignedConstant() const;
  2239. using element_iterator = ArrayRef<uint64_t>::iterator;
  2240. element_iterator elements_begin() const { return getElements().begin(); }
  2241. element_iterator elements_end() const { return getElements().end(); }
  2242. /// A lightweight wrapper around an expression operand.
  2243. ///
  2244. /// TODO: Store arguments directly and change \a DIExpression to store a
  2245. /// range of these.
  2246. class ExprOperand {
  2247. const uint64_t *Op = nullptr;
  2248. public:
  2249. ExprOperand() = default;
  2250. explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
  2251. const uint64_t *get() const { return Op; }
  2252. /// Get the operand code.
  2253. uint64_t getOp() const { return *Op; }
  2254. /// Get an argument to the operand.
  2255. ///
  2256. /// Never returns the operand itself.
  2257. uint64_t getArg(unsigned I) const { return Op[I + 1]; }
  2258. unsigned getNumArgs() const { return getSize() - 1; }
  2259. /// Return the size of the operand.
  2260. ///
  2261. /// Return the number of elements in the operand (1 + args).
  2262. unsigned getSize() const;
  2263. /// Append the elements of this operand to \p V.
  2264. void appendToVector(SmallVectorImpl<uint64_t> &V) const {
  2265. V.append(get(), get() + getSize());
  2266. }
  2267. };
  2268. /// An iterator for expression operands.
  2269. class expr_op_iterator
  2270. : public std::iterator<std::input_iterator_tag, ExprOperand> {
  2271. ExprOperand Op;
  2272. public:
  2273. expr_op_iterator() = default;
  2274. explicit expr_op_iterator(element_iterator I) : Op(I) {}
  2275. element_iterator getBase() const { return Op.get(); }
  2276. const ExprOperand &operator*() const { return Op; }
  2277. const ExprOperand *operator->() const { return &Op; }
  2278. expr_op_iterator &operator++() {
  2279. increment();
  2280. return *this;
  2281. }
  2282. expr_op_iterator operator++(int) {
  2283. expr_op_iterator T(*this);
  2284. increment();
  2285. return T;
  2286. }
  2287. /// Get the next iterator.
  2288. ///
  2289. /// \a std::next() doesn't work because this is technically an
  2290. /// input_iterator, but it's a perfectly valid operation. This is an
  2291. /// accessor to provide the same functionality.
  2292. expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
  2293. bool operator==(const expr_op_iterator &X) const {
  2294. return getBase() == X.getBase();
  2295. }
  2296. bool operator!=(const expr_op_iterator &X) const {
  2297. return getBase() != X.getBase();
  2298. }
  2299. private:
  2300. void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
  2301. };
  2302. /// Visit the elements via ExprOperand wrappers.
  2303. ///
  2304. /// These range iterators visit elements through \a ExprOperand wrappers.
  2305. /// This is not guaranteed to be a valid range unless \a isValid() gives \c
  2306. /// true.
  2307. ///
  2308. /// \pre \a isValid() gives \c true.
  2309. /// @{
  2310. expr_op_iterator expr_op_begin() const {
  2311. return expr_op_iterator(elements_begin());
  2312. }
  2313. expr_op_iterator expr_op_end() const {
  2314. return expr_op_iterator(elements_end());
  2315. }
  2316. iterator_range<expr_op_iterator> expr_ops() const {
  2317. return {expr_op_begin(), expr_op_end()};
  2318. }
  2319. /// @}
  2320. bool isValid() const;
  2321. static bool classof(const Metadata *MD) {
  2322. return MD->getMetadataID() == DIExpressionKind;
  2323. }
  2324. /// Return whether the first element a DW_OP_deref.
  2325. bool startsWithDeref() const {
  2326. return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
  2327. }
  2328. /// Holds the characteristics of one fragment of a larger variable.
  2329. struct FragmentInfo {
  2330. uint64_t SizeInBits;
  2331. uint64_t OffsetInBits;
  2332. };
  2333. /// Retrieve the details of this fragment expression.
  2334. static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
  2335. expr_op_iterator End);
  2336. /// Retrieve the details of this fragment expression.
  2337. Optional<FragmentInfo> getFragmentInfo() const {
  2338. return getFragmentInfo(expr_op_begin(), expr_op_end());
  2339. }
  2340. /// Return whether this is a piece of an aggregate variable.
  2341. bool isFragment() const { return getFragmentInfo().hasValue(); }
  2342. /// Return whether this is an implicit location description.
  2343. bool isImplicit() const;
  2344. /// Return whether the location is computed on the expression stack, meaning
  2345. /// it cannot be a simple register location.
  2346. bool isComplex() const;
  2347. /// Append \p Ops with operations to apply the \p Offset.
  2348. static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
  2349. /// If this is a constant offset, extract it. If there is no expression,
  2350. /// return true with an offset of zero.
  2351. bool extractIfOffset(int64_t &Offset) const;
  2352. /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF
  2353. /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address
  2354. /// Space>.
  2355. static const DIExpression *extractAddressClass(const DIExpression *Expr,
  2356. unsigned &AddrClass);
  2357. /// Used for DIExpression::prepend.
  2358. enum PrependOps : uint8_t {
  2359. ApplyOffset = 0,
  2360. DerefBefore = 1 << 0,
  2361. DerefAfter = 1 << 1,
  2362. StackValue = 1 << 2,
  2363. EntryValue = 1 << 3
  2364. };
  2365. /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
  2366. /// into a stack value or/and an entry value.
  2367. static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
  2368. int64_t Offset = 0);
  2369. /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
  2370. /// stack value.
  2371. static DIExpression *prependOpcodes(const DIExpression *Expr,
  2372. SmallVectorImpl<uint64_t> &Ops,
  2373. bool StackValue = false,
  2374. bool EntryValue = false);
  2375. /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
  2376. /// returned expression is a stack value only if \p DIExpr is a stack value.
  2377. /// If \p DIExpr describes a fragment, the returned expression will describe
  2378. /// the same fragment.
  2379. static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops);
  2380. /// Convert \p DIExpr into a stack value if it isn't one already by appending
  2381. /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
  2382. /// If \p DIExpr describes a fragment, the returned expression will describe
  2383. /// the same fragment.
  2384. static DIExpression *appendToStack(const DIExpression *Expr,
  2385. ArrayRef<uint64_t> Ops);
  2386. /// Create a DIExpression to describe one part of an aggregate variable that
  2387. /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
  2388. /// will be appended to the elements of \c Expr. If \c Expr already contains
  2389. /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
  2390. /// into the existing fragment.
  2391. ///
  2392. /// \param OffsetInBits Offset of the piece in bits.
  2393. /// \param SizeInBits Size of the piece in bits.
  2394. /// \return Creating a fragment expression may fail if \c Expr
  2395. /// contains arithmetic operations that would be truncated.
  2396. static Optional<DIExpression *>
  2397. createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
  2398. unsigned SizeInBits);
  2399. /// Determine the relative position of the fragments passed in.
  2400. /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
  2401. /// 1 if this is entirely after Other.
  2402. static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) {
  2403. uint64_t l1 = A.OffsetInBits;
  2404. uint64_t l2 = B.OffsetInBits;
  2405. uint64_t r1 = l1 + A.SizeInBits;
  2406. uint64_t r2 = l2 + B.SizeInBits;
  2407. if (r1 <= l2)
  2408. return -1;
  2409. else if (r2 <= l1)
  2410. return 1;
  2411. else
  2412. return 0;
  2413. }
  2414. using ExtOps = std::array<uint64_t, 6>;
  2415. /// Returns the ops for a zero- or sign-extension in a DIExpression.
  2416. static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed);
  2417. /// Append a zero- or sign-extension to \p Expr. Converts the expression to a
  2418. /// stack value if it isn't one already.
  2419. static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize,
  2420. unsigned ToSize, bool Signed);
  2421. /// Check if fragments overlap between a pair of FragmentInfos.
  2422. static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) {
  2423. return fragmentCmp(A, B) == 0;
  2424. }
  2425. /// Determine the relative position of the fragments described by this
  2426. /// DIExpression and \p Other. Calls static fragmentCmp implementation.
  2427. int fragmentCmp(const DIExpression *Other) const {
  2428. auto Fragment1 = *getFragmentInfo();
  2429. auto Fragment2 = *Other->getFragmentInfo();
  2430. return fragmentCmp(Fragment1, Fragment2);
  2431. }
  2432. /// Check if fragments overlap between this DIExpression and \p Other.
  2433. bool fragmentsOverlap(const DIExpression *Other) const {
  2434. if (!isFragment() || !Other->isFragment())
  2435. return true;
  2436. return fragmentCmp(Other) == 0;
  2437. }
  2438. /// Check if the expression consists of exactly one entry value operand.
  2439. /// (This is the only configuration of entry values that is supported.)
  2440. bool isEntryValue() const {
  2441. return getNumElements() > 0 &&
  2442. getElement(0) == dwarf::DW_OP_LLVM_entry_value;
  2443. }
  2444. };
  2445. inline bool operator==(const DIExpression::FragmentInfo &A,
  2446. const DIExpression::FragmentInfo &B) {
  2447. return std::tie(A.SizeInBits, A.OffsetInBits) ==
  2448. std::tie(B.SizeInBits, B.OffsetInBits);
  2449. }
  2450. inline bool operator<(const DIExpression::FragmentInfo &A,
  2451. const DIExpression::FragmentInfo &B) {
  2452. return std::tie(A.SizeInBits, A.OffsetInBits) <
  2453. std::tie(B.SizeInBits, B.OffsetInBits);
  2454. }
  2455. template <> struct DenseMapInfo<DIExpression::FragmentInfo> {
  2456. using FragInfo = DIExpression::FragmentInfo;
  2457. static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max();
  2458. static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; }
  2459. static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; }
  2460. static unsigned getHashValue(const FragInfo &Frag) {
  2461. return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff);
  2462. }
  2463. static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; }
  2464. };
  2465. /// Global variables.
  2466. ///
  2467. /// TODO: Remove DisplayName. It's always equal to Name.
  2468. class DIGlobalVariable : public DIVariable {
  2469. friend class LLVMContextImpl;
  2470. friend class MDNode;
  2471. bool IsLocalToUnit;
  2472. bool IsDefinition;
  2473. DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
  2474. bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
  2475. ArrayRef<Metadata *> Ops)
  2476. : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
  2477. IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
  2478. ~DIGlobalVariable() = default;
  2479. static DIGlobalVariable *
  2480. getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
  2481. StringRef LinkageName, DIFile *File, unsigned Line, DIType *Type,
  2482. bool IsLocalToUnit, bool IsDefinition,
  2483. DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams,
  2484. uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true) {
  2485. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  2486. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  2487. IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
  2488. cast_or_null<Metadata>(TemplateParams), AlignInBits, Storage,
  2489. ShouldCreate);
  2490. }
  2491. static DIGlobalVariable *
  2492. getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  2493. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  2494. bool IsLocalToUnit, bool IsDefinition,
  2495. Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
  2496. uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true);
  2497. TempDIGlobalVariable cloneImpl() const {
  2498. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  2499. getFile(), getLine(), getType(), isLocalToUnit(),
  2500. isDefinition(), getStaticDataMemberDeclaration(),
  2501. getTemplateParams(), getAlignInBits());
  2502. }
  2503. public:
  2504. DEFINE_MDNODE_GET(DIGlobalVariable,
  2505. (DIScope * Scope, StringRef Name, StringRef LinkageName,
  2506. DIFile *File, unsigned Line, DIType *Type,
  2507. bool IsLocalToUnit, bool IsDefinition,
  2508. DIDerivedType *StaticDataMemberDeclaration,
  2509. MDTuple *TemplateParams, uint32_t AlignInBits),
  2510. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  2511. IsDefinition, StaticDataMemberDeclaration, TemplateParams,
  2512. AlignInBits))
  2513. DEFINE_MDNODE_GET(DIGlobalVariable,
  2514. (Metadata * Scope, MDString *Name, MDString *LinkageName,
  2515. Metadata *File, unsigned Line, Metadata *Type,
  2516. bool IsLocalToUnit, bool IsDefinition,
  2517. Metadata *StaticDataMemberDeclaration,
  2518. Metadata *TemplateParams, uint32_t AlignInBits),
  2519. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  2520. IsDefinition, StaticDataMemberDeclaration, TemplateParams,
  2521. AlignInBits))
  2522. TempDIGlobalVariable clone() const { return cloneImpl(); }
  2523. bool isLocalToUnit() const { return IsLocalToUnit; }
  2524. bool isDefinition() const { return IsDefinition; }
  2525. StringRef getDisplayName() const { return getStringOperand(4); }
  2526. StringRef getLinkageName() const { return getStringOperand(5); }
  2527. DIDerivedType *getStaticDataMemberDeclaration() const {
  2528. return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
  2529. }
  2530. MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
  2531. Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
  2532. Metadata *getRawTemplateParams() const { return getOperand(7); }
  2533. MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
  2534. static bool classof(const Metadata *MD) {
  2535. return MD->getMetadataID() == DIGlobalVariableKind;
  2536. }
  2537. };
  2538. class DICommonBlock : public DIScope {
  2539. unsigned LineNo;
  2540. friend class LLVMContextImpl;
  2541. friend class MDNode;
  2542. DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo,
  2543. ArrayRef<Metadata *> Ops)
  2544. : DIScope(Context, DICommonBlockKind, Storage, dwarf::DW_TAG_common_block,
  2545. Ops), LineNo(LineNo) {}
  2546. static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope,
  2547. DIGlobalVariable *Decl, StringRef Name,
  2548. DIFile *File, unsigned LineNo,
  2549. StorageType Storage,
  2550. bool ShouldCreate = true) {
  2551. return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name),
  2552. File, LineNo, Storage, ShouldCreate);
  2553. }
  2554. static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope,
  2555. Metadata *Decl, MDString *Name, Metadata *File,
  2556. unsigned LineNo,
  2557. StorageType Storage, bool ShouldCreate = true);
  2558. TempDICommonBlock cloneImpl() const {
  2559. return getTemporary(getContext(), getScope(), getDecl(), getName(),
  2560. getFile(), getLineNo());
  2561. }
  2562. public:
  2563. DEFINE_MDNODE_GET(DICommonBlock,
  2564. (DIScope *Scope, DIGlobalVariable *Decl, StringRef Name,
  2565. DIFile *File, unsigned LineNo),
  2566. (Scope, Decl, Name, File, LineNo))
  2567. DEFINE_MDNODE_GET(DICommonBlock,
  2568. (Metadata *Scope, Metadata *Decl, MDString *Name,
  2569. Metadata *File, unsigned LineNo),
  2570. (Scope, Decl, Name, File, LineNo))
  2571. TempDICommonBlock clone() const { return cloneImpl(); }
  2572. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2573. DIGlobalVariable *getDecl() const {
  2574. return cast_or_null<DIGlobalVariable>(getRawDecl());
  2575. }
  2576. StringRef getName() const { return getStringOperand(2); }
  2577. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2578. unsigned getLineNo() const { return LineNo; }
  2579. Metadata *getRawScope() const { return getOperand(0); }
  2580. Metadata *getRawDecl() const { return getOperand(1); }
  2581. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  2582. Metadata *getRawFile() const { return getOperand(3); }
  2583. static bool classof(const Metadata *MD) {
  2584. return MD->getMetadataID() == DICommonBlockKind;
  2585. }
  2586. };
  2587. /// Local variable.
  2588. ///
  2589. /// TODO: Split up flags.
  2590. class DILocalVariable : public DIVariable {
  2591. friend class LLVMContextImpl;
  2592. friend class MDNode;
  2593. unsigned Arg : 16;
  2594. DIFlags Flags;
  2595. DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
  2596. unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
  2597. ArrayRef<Metadata *> Ops)
  2598. : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
  2599. Arg(Arg), Flags(Flags) {
  2600. assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
  2601. }
  2602. ~DILocalVariable() = default;
  2603. static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
  2604. StringRef Name, DIFile *File, unsigned Line,
  2605. DIType *Type, unsigned Arg, DIFlags Flags,
  2606. uint32_t AlignInBits, StorageType Storage,
  2607. bool ShouldCreate = true) {
  2608. return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
  2609. Line, Type, Arg, Flags, AlignInBits, Storage, ShouldCreate);
  2610. }
  2611. static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
  2612. MDString *Name, Metadata *File, unsigned Line,
  2613. Metadata *Type, unsigned Arg, DIFlags Flags,
  2614. uint32_t AlignInBits, StorageType Storage,
  2615. bool ShouldCreate = true);
  2616. TempDILocalVariable cloneImpl() const {
  2617. return getTemporary(getContext(), getScope(), getName(), getFile(),
  2618. getLine(), getType(), getArg(), getFlags(),
  2619. getAlignInBits());
  2620. }
  2621. public:
  2622. DEFINE_MDNODE_GET(DILocalVariable,
  2623. (DILocalScope * Scope, StringRef Name, DIFile *File,
  2624. unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags,
  2625. uint32_t AlignInBits),
  2626. (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
  2627. DEFINE_MDNODE_GET(DILocalVariable,
  2628. (Metadata * Scope, MDString *Name, Metadata *File,
  2629. unsigned Line, Metadata *Type, unsigned Arg,
  2630. DIFlags Flags, uint32_t AlignInBits),
  2631. (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
  2632. TempDILocalVariable clone() const { return cloneImpl(); }
  2633. /// Get the local scope for this variable.
  2634. ///
  2635. /// Variables must be defined in a local scope.
  2636. DILocalScope *getScope() const {
  2637. return cast<DILocalScope>(DIVariable::getScope());
  2638. }
  2639. bool isParameter() const { return Arg; }
  2640. unsigned getArg() const { return Arg; }
  2641. DIFlags getFlags() const { return Flags; }
  2642. bool isArtificial() const { return getFlags() & FlagArtificial; }
  2643. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  2644. /// Check that a location is valid for this variable.
  2645. ///
  2646. /// Check that \c DL exists, is in the same subprogram, and has the same
  2647. /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
  2648. /// to a \a DbgInfoIntrinsic.)
  2649. bool isValidLocationForIntrinsic(const DILocation *DL) const {
  2650. return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
  2651. }
  2652. static bool classof(const Metadata *MD) {
  2653. return MD->getMetadataID() == DILocalVariableKind;
  2654. }
  2655. };
  2656. /// Label.
  2657. ///
  2658. class DILabel : public DINode {
  2659. friend class LLVMContextImpl;
  2660. friend class MDNode;
  2661. unsigned Line;
  2662. DILabel(LLVMContext &C, StorageType Storage, unsigned Line,
  2663. ArrayRef<Metadata *> Ops)
  2664. : DINode(C, DILabelKind, Storage, dwarf::DW_TAG_label, Ops), Line(Line) {}
  2665. ~DILabel() = default;
  2666. static DILabel *getImpl(LLVMContext &Context, DIScope *Scope,
  2667. StringRef Name, DIFile *File, unsigned Line,
  2668. StorageType Storage,
  2669. bool ShouldCreate = true) {
  2670. return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
  2671. Line, Storage, ShouldCreate);
  2672. }
  2673. static DILabel *getImpl(LLVMContext &Context, Metadata *Scope,
  2674. MDString *Name, Metadata *File, unsigned Line,
  2675. StorageType Storage,
  2676. bool ShouldCreate = true);
  2677. TempDILabel cloneImpl() const {
  2678. return getTemporary(getContext(), getScope(), getName(), getFile(),
  2679. getLine());
  2680. }
  2681. public:
  2682. DEFINE_MDNODE_GET(DILabel,
  2683. (DILocalScope * Scope, StringRef Name, DIFile *File,
  2684. unsigned Line),
  2685. (Scope, Name, File, Line))
  2686. DEFINE_MDNODE_GET(DILabel,
  2687. (Metadata * Scope, MDString *Name, Metadata *File,
  2688. unsigned Line),
  2689. (Scope, Name, File, Line))
  2690. TempDILabel clone() const { return cloneImpl(); }
  2691. /// Get the local scope for this label.
  2692. ///
  2693. /// Labels must be defined in a local scope.
  2694. DILocalScope *getScope() const {
  2695. return cast_or_null<DILocalScope>(getRawScope());
  2696. }
  2697. unsigned getLine() const { return Line; }
  2698. StringRef getName() const { return getStringOperand(1); }
  2699. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2700. Metadata *getRawScope() const { return getOperand(0); }
  2701. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  2702. Metadata *getRawFile() const { return getOperand(2); }
  2703. /// Check that a location is valid for this label.
  2704. ///
  2705. /// Check that \c DL exists, is in the same subprogram, and has the same
  2706. /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
  2707. /// to a \a DbgInfoIntrinsic.)
  2708. bool isValidLocationForIntrinsic(const DILocation *DL) const {
  2709. return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
  2710. }
  2711. static bool classof(const Metadata *MD) {
  2712. return MD->getMetadataID() == DILabelKind;
  2713. }
  2714. };
  2715. class DIObjCProperty : public DINode {
  2716. friend class LLVMContextImpl;
  2717. friend class MDNode;
  2718. unsigned Line;
  2719. unsigned Attributes;
  2720. DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
  2721. unsigned Attributes, ArrayRef<Metadata *> Ops)
  2722. : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
  2723. Ops),
  2724. Line(Line), Attributes(Attributes) {}
  2725. ~DIObjCProperty() = default;
  2726. static DIObjCProperty *
  2727. getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
  2728. StringRef GetterName, StringRef SetterName, unsigned Attributes,
  2729. DIType *Type, StorageType Storage, bool ShouldCreate = true) {
  2730. return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
  2731. getCanonicalMDString(Context, GetterName),
  2732. getCanonicalMDString(Context, SetterName), Attributes, Type,
  2733. Storage, ShouldCreate);
  2734. }
  2735. static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
  2736. Metadata *File, unsigned Line,
  2737. MDString *GetterName, MDString *SetterName,
  2738. unsigned Attributes, Metadata *Type,
  2739. StorageType Storage, bool ShouldCreate = true);
  2740. TempDIObjCProperty cloneImpl() const {
  2741. return getTemporary(getContext(), getName(), getFile(), getLine(),
  2742. getGetterName(), getSetterName(), getAttributes(),
  2743. getType());
  2744. }
  2745. public:
  2746. DEFINE_MDNODE_GET(DIObjCProperty,
  2747. (StringRef Name, DIFile *File, unsigned Line,
  2748. StringRef GetterName, StringRef SetterName,
  2749. unsigned Attributes, DIType *Type),
  2750. (Name, File, Line, GetterName, SetterName, Attributes,
  2751. Type))
  2752. DEFINE_MDNODE_GET(DIObjCProperty,
  2753. (MDString * Name, Metadata *File, unsigned Line,
  2754. MDString *GetterName, MDString *SetterName,
  2755. unsigned Attributes, Metadata *Type),
  2756. (Name, File, Line, GetterName, SetterName, Attributes,
  2757. Type))
  2758. TempDIObjCProperty clone() const { return cloneImpl(); }
  2759. unsigned getLine() const { return Line; }
  2760. unsigned getAttributes() const { return Attributes; }
  2761. StringRef getName() const { return getStringOperand(0); }
  2762. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2763. StringRef getGetterName() const { return getStringOperand(2); }
  2764. StringRef getSetterName() const { return getStringOperand(3); }
  2765. DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
  2766. StringRef getFilename() const {
  2767. if (auto *F = getFile())
  2768. return F->getFilename();
  2769. return "";
  2770. }
  2771. StringRef getDirectory() const {
  2772. if (auto *F = getFile())
  2773. return F->getDirectory();
  2774. return "";
  2775. }
  2776. Optional<StringRef> getSource() const {
  2777. if (auto *F = getFile())
  2778. return F->getSource();
  2779. return None;
  2780. }
  2781. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  2782. Metadata *getRawFile() const { return getOperand(1); }
  2783. MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
  2784. MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
  2785. Metadata *getRawType() const { return getOperand(4); }
  2786. static bool classof(const Metadata *MD) {
  2787. return MD->getMetadataID() == DIObjCPropertyKind;
  2788. }
  2789. };
  2790. /// An imported module (C++ using directive or similar).
  2791. class DIImportedEntity : public DINode {
  2792. friend class LLVMContextImpl;
  2793. friend class MDNode;
  2794. unsigned Line;
  2795. DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
  2796. unsigned Line, ArrayRef<Metadata *> Ops)
  2797. : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
  2798. ~DIImportedEntity() = default;
  2799. static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
  2800. DIScope *Scope, DINode *Entity, DIFile *File,
  2801. unsigned Line, StringRef Name,
  2802. StorageType Storage,
  2803. bool ShouldCreate = true) {
  2804. return getImpl(Context, Tag, Scope, Entity, File, Line,
  2805. getCanonicalMDString(Context, Name), Storage, ShouldCreate);
  2806. }
  2807. static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
  2808. Metadata *Scope, Metadata *Entity,
  2809. Metadata *File, unsigned Line,
  2810. MDString *Name, StorageType Storage,
  2811. bool ShouldCreate = true);
  2812. TempDIImportedEntity cloneImpl() const {
  2813. return getTemporary(getContext(), getTag(), getScope(), getEntity(),
  2814. getFile(), getLine(), getName());
  2815. }
  2816. public:
  2817. DEFINE_MDNODE_GET(DIImportedEntity,
  2818. (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
  2819. unsigned Line, StringRef Name = ""),
  2820. (Tag, Scope, Entity, File, Line, Name))
  2821. DEFINE_MDNODE_GET(DIImportedEntity,
  2822. (unsigned Tag, Metadata *Scope, Metadata *Entity,
  2823. Metadata *File, unsigned Line, MDString *Name),
  2824. (Tag, Scope, Entity, File, Line, Name))
  2825. TempDIImportedEntity clone() const { return cloneImpl(); }
  2826. unsigned getLine() const { return Line; }
  2827. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2828. DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
  2829. StringRef getName() const { return getStringOperand(2); }
  2830. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2831. Metadata *getRawScope() const { return getOperand(0); }
  2832. Metadata *getRawEntity() const { return getOperand(1); }
  2833. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  2834. Metadata *getRawFile() const { return getOperand(3); }
  2835. static bool classof(const Metadata *MD) {
  2836. return MD->getMetadataID() == DIImportedEntityKind;
  2837. }
  2838. };
  2839. /// A pair of DIGlobalVariable and DIExpression.
  2840. class DIGlobalVariableExpression : public MDNode {
  2841. friend class LLVMContextImpl;
  2842. friend class MDNode;
  2843. DIGlobalVariableExpression(LLVMContext &C, StorageType Storage,
  2844. ArrayRef<Metadata *> Ops)
  2845. : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
  2846. ~DIGlobalVariableExpression() = default;
  2847. static DIGlobalVariableExpression *
  2848. getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
  2849. StorageType Storage, bool ShouldCreate = true);
  2850. TempDIGlobalVariableExpression cloneImpl() const {
  2851. return getTemporary(getContext(), getVariable(), getExpression());
  2852. }
  2853. public:
  2854. DEFINE_MDNODE_GET(DIGlobalVariableExpression,
  2855. (Metadata * Variable, Metadata *Expression),
  2856. (Variable, Expression))
  2857. TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
  2858. Metadata *getRawVariable() const { return getOperand(0); }
  2859. DIGlobalVariable *getVariable() const {
  2860. return cast_or_null<DIGlobalVariable>(getRawVariable());
  2861. }
  2862. Metadata *getRawExpression() const { return getOperand(1); }
  2863. DIExpression *getExpression() const {
  2864. return cast<DIExpression>(getRawExpression());
  2865. }
  2866. static bool classof(const Metadata *MD) {
  2867. return MD->getMetadataID() == DIGlobalVariableExpressionKind;
  2868. }
  2869. };
  2870. /// Macro Info DWARF-like metadata node.
  2871. ///
  2872. /// A metadata node with a DWARF macro info (i.e., a constant named
  2873. /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
  2874. /// DIMacroNode
  2875. /// because it's potentially used for non-DWARF output.
  2876. class DIMacroNode : public MDNode {
  2877. friend class LLVMContextImpl;
  2878. friend class MDNode;
  2879. protected:
  2880. DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
  2881. ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
  2882. : MDNode(C, ID, Storage, Ops1, Ops2) {
  2883. assert(MIType < 1u << 16);
  2884. SubclassData16 = MIType;
  2885. }
  2886. ~DIMacroNode() = default;
  2887. template <class Ty> Ty *getOperandAs(unsigned I) const {
  2888. return cast_or_null<Ty>(getOperand(I));
  2889. }
  2890. StringRef getStringOperand(unsigned I) const {
  2891. if (auto *S = getOperandAs<MDString>(I))
  2892. return S->getString();
  2893. return StringRef();
  2894. }
  2895. static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
  2896. if (S.empty())
  2897. return nullptr;
  2898. return MDString::get(Context, S);
  2899. }
  2900. public:
  2901. unsigned getMacinfoType() const { return SubclassData16; }
  2902. static bool classof(const Metadata *MD) {
  2903. switch (MD->getMetadataID()) {
  2904. default:
  2905. return false;
  2906. case DIMacroKind:
  2907. case DIMacroFileKind:
  2908. return true;
  2909. }
  2910. }
  2911. };
  2912. class DIMacro : public DIMacroNode {
  2913. friend class LLVMContextImpl;
  2914. friend class MDNode;
  2915. unsigned Line;
  2916. DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
  2917. ArrayRef<Metadata *> Ops)
  2918. : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
  2919. ~DIMacro() = default;
  2920. static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
  2921. StringRef Name, StringRef Value, StorageType Storage,
  2922. bool ShouldCreate = true) {
  2923. return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
  2924. getCanonicalMDString(Context, Value), Storage, ShouldCreate);
  2925. }
  2926. static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
  2927. MDString *Name, MDString *Value, StorageType Storage,
  2928. bool ShouldCreate = true);
  2929. TempDIMacro cloneImpl() const {
  2930. return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
  2931. getValue());
  2932. }
  2933. public:
  2934. DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
  2935. StringRef Value = ""),
  2936. (MIType, Line, Name, Value))
  2937. DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
  2938. MDString *Value),
  2939. (MIType, Line, Name, Value))
  2940. TempDIMacro clone() const { return cloneImpl(); }
  2941. unsigned getLine() const { return Line; }
  2942. StringRef getName() const { return getStringOperand(0); }
  2943. StringRef getValue() const { return getStringOperand(1); }
  2944. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  2945. MDString *getRawValue() const { return getOperandAs<MDString>(1); }
  2946. static bool classof(const Metadata *MD) {
  2947. return MD->getMetadataID() == DIMacroKind;
  2948. }
  2949. };
  2950. class DIMacroFile : public DIMacroNode {
  2951. friend class LLVMContextImpl;
  2952. friend class MDNode;
  2953. unsigned Line;
  2954. DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
  2955. unsigned Line, ArrayRef<Metadata *> Ops)
  2956. : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
  2957. ~DIMacroFile() = default;
  2958. static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
  2959. unsigned Line, DIFile *File,
  2960. DIMacroNodeArray Elements, StorageType Storage,
  2961. bool ShouldCreate = true) {
  2962. return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
  2963. Elements.get(), Storage, ShouldCreate);
  2964. }
  2965. static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
  2966. unsigned Line, Metadata *File, Metadata *Elements,
  2967. StorageType Storage, bool ShouldCreate = true);
  2968. TempDIMacroFile cloneImpl() const {
  2969. return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
  2970. getElements());
  2971. }
  2972. public:
  2973. DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
  2974. DIMacroNodeArray Elements),
  2975. (MIType, Line, File, Elements))
  2976. DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
  2977. Metadata *File, Metadata *Elements),
  2978. (MIType, Line, File, Elements))
  2979. TempDIMacroFile clone() const { return cloneImpl(); }
  2980. void replaceElements(DIMacroNodeArray Elements) {
  2981. #ifndef NDEBUG
  2982. for (DIMacroNode *Op : getElements())
  2983. assert(is_contained(Elements->operands(), Op) &&
  2984. "Lost a macro node during macro node list replacement");
  2985. #endif
  2986. replaceOperandWith(1, Elements.get());
  2987. }
  2988. unsigned getLine() const { return Line; }
  2989. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2990. DIMacroNodeArray getElements() const {
  2991. return cast_or_null<MDTuple>(getRawElements());
  2992. }
  2993. Metadata *getRawFile() const { return getOperand(0); }
  2994. Metadata *getRawElements() const { return getOperand(1); }
  2995. static bool classof(const Metadata *MD) {
  2996. return MD->getMetadataID() == DIMacroFileKind;
  2997. }
  2998. };
  2999. /// Identifies a unique instance of a variable.
  3000. ///
  3001. /// Storage for identifying a potentially inlined instance of a variable,
  3002. /// or a fragment thereof. This guarantees that exactly one variable instance
  3003. /// may be identified by this class, even when that variable is a fragment of
  3004. /// an aggregate variable and/or there is another inlined instance of the same
  3005. /// source code variable nearby.
  3006. /// This class does not necessarily uniquely identify that variable: it is
  3007. /// possible that a DebugVariable with different parameters may point to the
  3008. /// same variable instance, but not that one DebugVariable points to multiple
  3009. /// variable instances.
  3010. class DebugVariable {
  3011. using FragmentInfo = DIExpression::FragmentInfo;
  3012. const DILocalVariable *Variable;
  3013. Optional<FragmentInfo> Fragment;
  3014. const DILocation *InlinedAt;
  3015. /// Fragment that will overlap all other fragments. Used as default when
  3016. /// caller demands a fragment.
  3017. static const FragmentInfo DefaultFragment;
  3018. public:
  3019. DebugVariable(const DILocalVariable *Var, Optional<FragmentInfo> FragmentInfo,
  3020. const DILocation *InlinedAt)
  3021. : Variable(Var), Fragment(FragmentInfo), InlinedAt(InlinedAt) {}
  3022. DebugVariable(const DILocalVariable *Var, const DIExpression *DIExpr,
  3023. const DILocation *InlinedAt)
  3024. : Variable(Var),
  3025. Fragment(DIExpr ? DIExpr->getFragmentInfo() : NoneType()),
  3026. InlinedAt(InlinedAt) {}
  3027. const DILocalVariable *getVariable() const { return Variable; }
  3028. const Optional<FragmentInfo> getFragment() const { return Fragment; }
  3029. const DILocation *getInlinedAt() const { return InlinedAt; }
  3030. const FragmentInfo getFragmentOrDefault() const {
  3031. return Fragment.getValueOr(DefaultFragment);
  3032. }
  3033. static bool isDefaultFragment(const FragmentInfo F) {
  3034. return F == DefaultFragment;
  3035. }
  3036. bool operator==(const DebugVariable &Other) const {
  3037. return std::tie(Variable, Fragment, InlinedAt) ==
  3038. std::tie(Other.Variable, Other.Fragment, Other.InlinedAt);
  3039. }
  3040. bool operator<(const DebugVariable &Other) const {
  3041. return std::tie(Variable, Fragment, InlinedAt) <
  3042. std::tie(Other.Variable, Other.Fragment, Other.InlinedAt);
  3043. }
  3044. };
  3045. template <> struct DenseMapInfo<DebugVariable> {
  3046. using FragmentInfo = DIExpression::FragmentInfo;
  3047. /// Empty key: no key should be generated that has no DILocalVariable.
  3048. static inline DebugVariable getEmptyKey() {
  3049. return DebugVariable(nullptr, NoneType(), nullptr);
  3050. }
  3051. /// Difference in tombstone is that the Optional is meaningful.
  3052. static inline DebugVariable getTombstoneKey() {
  3053. return DebugVariable(nullptr, {{0, 0}}, nullptr);
  3054. }
  3055. static unsigned getHashValue(const DebugVariable &D) {
  3056. unsigned HV = 0;
  3057. const Optional<FragmentInfo> Fragment = D.getFragment();
  3058. if (Fragment)
  3059. HV = DenseMapInfo<FragmentInfo>::getHashValue(*Fragment);
  3060. return hash_combine(D.getVariable(), HV, D.getInlinedAt());
  3061. }
  3062. static bool isEqual(const DebugVariable &A, const DebugVariable &B) {
  3063. return A == B;
  3064. }
  3065. };
  3066. } // end namespace llvm
  3067. #undef DEFINE_MDNODE_GET_UNPACK_IMPL
  3068. #undef DEFINE_MDNODE_GET_UNPACK
  3069. #undef DEFINE_MDNODE_GET
  3070. #endif // LLVM_IR_DEBUGINFOMETADATA_H
  3071. #ifdef __GNUC__
  3072. #pragma GCC diagnostic pop
  3073. #endif