DebugInfoMetadata.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the debug info Metadata classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/DebugInfoMetadata.h"
  14. #include "LLVMContextImpl.h"
  15. #include "MetadataImpl.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/IR/Function.h"
  18. using namespace llvm;
  19. DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
  20. unsigned Column, ArrayRef<Metadata *> MDs)
  21. : MDNode(C, DILocationKind, Storage, MDs) {
  22. assert((MDs.size() == 1 || MDs.size() == 2) &&
  23. "Expected a scope and optional inlined-at");
  24. // Set line and column.
  25. assert(Column < (1u << 16) && "Expected 16-bit column");
  26. SubclassData32 = Line;
  27. SubclassData16 = Column;
  28. }
  29. static void adjustColumn(unsigned &Column) {
  30. // Set to unknown on overflow. We only have 16 bits to play with here.
  31. if (Column >= (1u << 16))
  32. Column = 0;
  33. }
  34. DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
  35. unsigned Column, Metadata *Scope,
  36. Metadata *InlinedAt, StorageType Storage,
  37. bool ShouldCreate) {
  38. // Fixup column.
  39. adjustColumn(Column);
  40. assert(Scope && "Expected scope");
  41. if (Storage == Uniqued) {
  42. if (auto *N =
  43. getUniqued(Context.pImpl->DILocations,
  44. DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
  45. return N;
  46. if (!ShouldCreate)
  47. return nullptr;
  48. } else {
  49. assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
  50. }
  51. SmallVector<Metadata *, 2> Ops;
  52. Ops.push_back(Scope);
  53. if (InlinedAt)
  54. Ops.push_back(InlinedAt);
  55. return storeImpl(new (Ops.size())
  56. DILocation(Context, Storage, Line, Column, Ops),
  57. Storage, Context.pImpl->DILocations);
  58. }
  59. unsigned DILocation::computeNewDiscriminator() const {
  60. // FIXME: This seems completely wrong.
  61. //
  62. // 1. If two modules are generated in the same context, then the second
  63. // Module will get different discriminators than it would have if it were
  64. // generated in its own context.
  65. // 2. If this function is called after round-tripping to bitcode instead of
  66. // before, it will give a different (and potentially incorrect!) return.
  67. //
  68. // The discriminator should instead be calculated from local information
  69. // where it's actually needed. This logic should be moved to
  70. // AddDiscriminators::runOnFunction(), where it doesn't pollute the
  71. // LLVMContext.
  72. std::pair<const char *, unsigned> Key(getFilename().data(), getLine());
  73. return ++getContext().pImpl->DiscriminatorTable[Key];
  74. }
  75. unsigned DINode::getFlag(StringRef Flag) {
  76. return StringSwitch<unsigned>(Flag)
  77. #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
  78. #include "llvm/IR/DebugInfoFlags.def"
  79. .Default(0);
  80. }
  81. const char *DINode::getFlagString(unsigned Flag) {
  82. switch (Flag) {
  83. default:
  84. return "";
  85. #define HANDLE_DI_FLAG(ID, NAME) \
  86. case Flag##NAME: \
  87. return "DIFlag" #NAME;
  88. #include "llvm/IR/DebugInfoFlags.def"
  89. }
  90. }
  91. unsigned DINode::splitFlags(unsigned Flags,
  92. SmallVectorImpl<unsigned> &SplitFlags) {
  93. // Accessibility flags need to be specially handled, since they're packed
  94. // together.
  95. if (unsigned A = Flags & FlagAccessibility) {
  96. if (A == FlagPrivate)
  97. SplitFlags.push_back(FlagPrivate);
  98. else if (A == FlagProtected)
  99. SplitFlags.push_back(FlagProtected);
  100. else
  101. SplitFlags.push_back(FlagPublic);
  102. Flags &= ~A;
  103. }
  104. #define HANDLE_DI_FLAG(ID, NAME) \
  105. if (unsigned Bit = Flags & ID) { \
  106. SplitFlags.push_back(Bit); \
  107. Flags &= ~Bit; \
  108. }
  109. #include "llvm/IR/DebugInfoFlags.def"
  110. return Flags;
  111. }
  112. DIScopeRef DIScope::getScope() const {
  113. if (auto *T = dyn_cast<DIType>(this))
  114. return T->getScope();
  115. if (auto *SP = dyn_cast<DISubprogram>(this))
  116. return SP->getScope();
  117. if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
  118. return DIScopeRef(LB->getScope());
  119. if (auto *NS = dyn_cast<DINamespace>(this))
  120. return DIScopeRef(NS->getScope());
  121. if (auto *M = dyn_cast<DIModule>(this))
  122. return DIScopeRef(M->getScope());
  123. assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
  124. "Unhandled type of scope.");
  125. return nullptr;
  126. }
  127. StringRef DIScope::getName() const {
  128. if (auto *T = dyn_cast<DIType>(this))
  129. return T->getName();
  130. if (auto *SP = dyn_cast<DISubprogram>(this))
  131. return SP->getName();
  132. if (auto *NS = dyn_cast<DINamespace>(this))
  133. return NS->getName();
  134. if (auto *M = dyn_cast<DIModule>(this))
  135. return M->getName();
  136. assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
  137. isa<DICompileUnit>(this)) &&
  138. "Unhandled type of scope.");
  139. return "";
  140. }
  141. static StringRef getString(const MDString *S) {
  142. if (S)
  143. return S->getString();
  144. return StringRef();
  145. }
  146. #ifndef NDEBUG
  147. static bool isCanonical(const MDString *S) {
  148. return !S || !S->getString().empty();
  149. }
  150. #endif
  151. GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
  152. MDString *Header,
  153. ArrayRef<Metadata *> DwarfOps,
  154. StorageType Storage, bool ShouldCreate) {
  155. unsigned Hash = 0;
  156. if (Storage == Uniqued) {
  157. GenericDINodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
  158. if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
  159. return N;
  160. if (!ShouldCreate)
  161. return nullptr;
  162. Hash = Key.getHash();
  163. } else {
  164. assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
  165. }
  166. // Use a nullptr for empty headers.
  167. assert(isCanonical(Header) && "Expected canonical MDString");
  168. Metadata *PreOps[] = {Header};
  169. return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
  170. Context, Storage, Hash, Tag, PreOps, DwarfOps),
  171. Storage, Context.pImpl->GenericDINodes);
  172. }
  173. void GenericDINode::recalculateHash() {
  174. setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
  175. }
  176. #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
  177. #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
  178. #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
  179. do { \
  180. if (Storage == Uniqued) { \
  181. if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
  182. CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
  183. return N; \
  184. if (!ShouldCreate) \
  185. return nullptr; \
  186. } else { \
  187. assert(ShouldCreate && \
  188. "Expected non-uniqued nodes to always be created"); \
  189. } \
  190. } while (false)
  191. #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
  192. return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
  193. CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
  194. Storage, Context.pImpl->CLASS##s)
  195. #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
  196. return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
  197. Storage, Context.pImpl->CLASS##s)
  198. #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
  199. return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
  200. CLASS(Context, Storage, OPS), \
  201. Storage, Context.pImpl->CLASS##s)
  202. DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
  203. StorageType Storage, bool ShouldCreate) {
  204. DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
  205. DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
  206. }
  207. DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
  208. MDString *Name, StorageType Storage,
  209. bool ShouldCreate) {
  210. assert(isCanonical(Name) && "Expected canonical MDString");
  211. DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, getString(Name)));
  212. Metadata *Ops[] = {Name};
  213. DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
  214. }
  215. DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
  216. MDString *Name, uint64_t SizeInBits,
  217. uint64_t AlignInBits, unsigned Encoding,
  218. StorageType Storage, bool ShouldCreate) {
  219. assert(isCanonical(Name) && "Expected canonical MDString");
  220. DEFINE_GETIMPL_LOOKUP(
  221. DIBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
  222. Metadata *Ops[] = {nullptr, nullptr, Name};
  223. DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
  224. Ops);
  225. }
  226. DIDerivedType *DIDerivedType::getImpl(
  227. LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
  228. unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
  229. uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
  230. Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
  231. assert(isCanonical(Name) && "Expected canonical MDString");
  232. DEFINE_GETIMPL_LOOKUP(DIDerivedType, (Tag, getString(Name), File, Line, Scope,
  233. BaseType, SizeInBits, AlignInBits,
  234. OffsetInBits, Flags, ExtraData));
  235. Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
  236. DEFINE_GETIMPL_STORE(
  237. DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
  238. Ops);
  239. }
  240. DICompositeType *DICompositeType::getImpl(
  241. LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
  242. unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
  243. uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
  244. Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
  245. Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
  246. bool ShouldCreate) {
  247. assert(isCanonical(Name) && "Expected canonical MDString");
  248. DEFINE_GETIMPL_LOOKUP(DICompositeType,
  249. (Tag, getString(Name), File, Line, Scope, BaseType,
  250. SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
  251. RuntimeLang, VTableHolder, TemplateParams,
  252. getString(Identifier)));
  253. Metadata *Ops[] = {File, Scope, Name, BaseType,
  254. Elements, VTableHolder, TemplateParams, Identifier};
  255. DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
  256. AlignInBits, OffsetInBits, Flags),
  257. Ops);
  258. }
  259. DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context,
  260. unsigned Flags, Metadata *TypeArray,
  261. StorageType Storage,
  262. bool ShouldCreate) {
  263. DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, TypeArray));
  264. Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
  265. TypeArray, nullptr, nullptr, nullptr};
  266. DEFINE_GETIMPL_STORE(DISubroutineType, (Flags), Ops);
  267. }
  268. DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
  269. MDString *Directory, StorageType Storage,
  270. bool ShouldCreate) {
  271. assert(isCanonical(Filename) && "Expected canonical MDString");
  272. assert(isCanonical(Directory) && "Expected canonical MDString");
  273. DEFINE_GETIMPL_LOOKUP(DIFile, (getString(Filename), getString(Directory)));
  274. Metadata *Ops[] = {Filename, Directory};
  275. DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIFile, Ops);
  276. }
  277. DICompileUnit *DICompileUnit::getImpl(
  278. LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
  279. MDString *Producer, bool IsOptimized, MDString *Flags,
  280. unsigned RuntimeVersion, MDString *SplitDebugFilename,
  281. unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
  282. Metadata *Subprograms, Metadata *GlobalVariables,
  283. Metadata *ImportedEntities, uint64_t DWOId,
  284. StorageType Storage, bool ShouldCreate) {
  285. assert(isCanonical(Producer) && "Expected canonical MDString");
  286. assert(isCanonical(Flags) && "Expected canonical MDString");
  287. assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
  288. DEFINE_GETIMPL_LOOKUP(
  289. DICompileUnit,
  290. (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
  291. RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
  292. RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, DWOId));
  293. Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
  294. RetainedTypes, Subprograms, GlobalVariables,
  295. ImportedEntities};
  296. DEFINE_GETIMPL_STORE(
  297. DICompileUnit,
  298. (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind, DWOId), Ops);
  299. }
  300. DISubprogram *DILocalScope::getSubprogram() const {
  301. if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
  302. return Block->getScope()->getSubprogram();
  303. return const_cast<DISubprogram *>(cast<DISubprogram>(this));
  304. }
  305. DISubprogram *DISubprogram::getImpl(
  306. LLVMContext &Context, Metadata *Scope, MDString *Name,
  307. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  308. bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
  309. Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
  310. unsigned Flags, bool IsOptimized, Metadata *Function,
  311. Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
  312. StorageType Storage, bool ShouldCreate) {
  313. assert(isCanonical(Name) && "Expected canonical MDString");
  314. assert(isCanonical(LinkageName) && "Expected canonical MDString");
  315. DEFINE_GETIMPL_LOOKUP(DISubprogram,
  316. (Scope, getString(Name), getString(LinkageName), File,
  317. Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
  318. ContainingType, Virtuality, VirtualIndex, Flags,
  319. IsOptimized, Function, TemplateParams, Declaration,
  320. Variables));
  321. Metadata *Ops[] = {File, Scope, Name, Name,
  322. LinkageName, Type, ContainingType, Function,
  323. TemplateParams, Declaration, Variables};
  324. DEFINE_GETIMPL_STORE(DISubprogram,
  325. (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
  326. IsLocalToUnit, IsDefinition, IsOptimized),
  327. Ops);
  328. }
  329. Function *DISubprogram::getFunction() const {
  330. // FIXME: Should this be looking through bitcasts?
  331. return dyn_cast_or_null<Function>(getFunctionConstant());
  332. }
  333. bool DISubprogram::describes(const Function *F) const {
  334. assert(F && "Invalid function");
  335. if (F == getFunction())
  336. return true;
  337. StringRef Name = getLinkageName();
  338. if (Name.empty())
  339. Name = getName();
  340. return F->getName() == Name;
  341. }
  342. void DISubprogram::replaceFunction(Function *F) {
  343. replaceFunction(F ? ConstantAsMetadata::get(F)
  344. : static_cast<ConstantAsMetadata *>(nullptr));
  345. }
  346. DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
  347. Metadata *File, unsigned Line,
  348. unsigned Column, StorageType Storage,
  349. bool ShouldCreate) {
  350. assert(Scope && "Expected scope");
  351. DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
  352. Metadata *Ops[] = {File, Scope};
  353. DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
  354. }
  355. DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
  356. Metadata *Scope, Metadata *File,
  357. unsigned Discriminator,
  358. StorageType Storage,
  359. bool ShouldCreate) {
  360. assert(Scope && "Expected scope");
  361. DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
  362. Metadata *Ops[] = {File, Scope};
  363. DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
  364. }
  365. DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
  366. Metadata *File, MDString *Name, unsigned Line,
  367. StorageType Storage, bool ShouldCreate) {
  368. assert(isCanonical(Name) && "Expected canonical MDString");
  369. DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, getString(Name), Line));
  370. Metadata *Ops[] = {File, Scope, Name};
  371. DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
  372. }
  373. DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
  374. MDString *Name, MDString *ConfigurationMacros,
  375. MDString *IncludePath, MDString *ISysRoot,
  376. StorageType Storage, bool ShouldCreate) {
  377. assert(isCanonical(Name) && "Expected canonical MDString");
  378. DEFINE_GETIMPL_LOOKUP(DIModule,
  379. (Scope, getString(Name), getString(ConfigurationMacros),
  380. getString(IncludePath), getString(ISysRoot)));
  381. Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
  382. DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
  383. }
  384. DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
  385. MDString *Name,
  386. Metadata *Type,
  387. StorageType Storage,
  388. bool ShouldCreate) {
  389. assert(isCanonical(Name) && "Expected canonical MDString");
  390. DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (getString(Name), Type));
  391. Metadata *Ops[] = {Name, Type};
  392. DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
  393. }
  394. DITemplateValueParameter *DITemplateValueParameter::getImpl(
  395. LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
  396. Metadata *Value, StorageType Storage, bool ShouldCreate) {
  397. assert(isCanonical(Name) && "Expected canonical MDString");
  398. DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter,
  399. (Tag, getString(Name), Type, Value));
  400. Metadata *Ops[] = {Name, Type, Value};
  401. DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
  402. }
  403. DIGlobalVariable *
  404. DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  405. MDString *LinkageName, Metadata *File, unsigned Line,
  406. Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
  407. Metadata *Variable,
  408. Metadata *StaticDataMemberDeclaration,
  409. StorageType Storage, bool ShouldCreate) {
  410. assert(isCanonical(Name) && "Expected canonical MDString");
  411. assert(isCanonical(LinkageName) && "Expected canonical MDString");
  412. DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
  413. (Scope, getString(Name), getString(LinkageName), File,
  414. Line, Type, IsLocalToUnit, IsDefinition, Variable,
  415. StaticDataMemberDeclaration));
  416. Metadata *Ops[] = {Scope, Name, File, Type,
  417. Name, LinkageName, Variable, StaticDataMemberDeclaration};
  418. DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
  419. Ops);
  420. }
  421. DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, unsigned Tag,
  422. Metadata *Scope, MDString *Name,
  423. Metadata *File, unsigned Line,
  424. Metadata *Type, unsigned Arg,
  425. unsigned Flags, StorageType Storage,
  426. bool ShouldCreate) {
  427. // 64K ought to be enough for any frontend.
  428. assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
  429. assert(Scope && "Expected scope");
  430. assert(isCanonical(Name) && "Expected canonical MDString");
  431. DEFINE_GETIMPL_LOOKUP(DILocalVariable, (Tag, Scope, getString(Name), File,
  432. Line, Type, Arg, Flags));
  433. Metadata *Ops[] = {Scope, Name, File, Type};
  434. DEFINE_GETIMPL_STORE(DILocalVariable, (Tag, Line, Arg, Flags), Ops);
  435. }
  436. DIExpression *DIExpression::getImpl(LLVMContext &Context,
  437. ArrayRef<uint64_t> Elements,
  438. StorageType Storage, bool ShouldCreate) {
  439. DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
  440. DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
  441. }
  442. unsigned DIExpression::ExprOperand::getSize() const {
  443. switch (getOp()) {
  444. case dwarf::DW_OP_bit_piece:
  445. return 3;
  446. case dwarf::DW_OP_plus:
  447. return 2;
  448. default:
  449. return 1;
  450. }
  451. }
  452. bool DIExpression::isValid() const {
  453. for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
  454. // Check that there's space for the operand.
  455. if (I->get() + I->getSize() > E->get())
  456. return false;
  457. // Check that the operand is valid.
  458. switch (I->getOp()) {
  459. default:
  460. return false;
  461. case dwarf::DW_OP_bit_piece:
  462. // Piece expressions must be at the end.
  463. return I->get() + I->getSize() == E->get();
  464. case dwarf::DW_OP_plus:
  465. case dwarf::DW_OP_deref:
  466. break;
  467. }
  468. }
  469. return true;
  470. }
  471. bool DIExpression::isBitPiece() const {
  472. assert(isValid() && "Expected valid expression");
  473. if (unsigned N = getNumElements())
  474. if (N >= 3)
  475. return getElement(N - 3) == dwarf::DW_OP_bit_piece;
  476. return false;
  477. }
  478. uint64_t DIExpression::getBitPieceOffset() const {
  479. assert(isBitPiece() && "Expected bit piece");
  480. return getElement(getNumElements() - 2);
  481. }
  482. uint64_t DIExpression::getBitPieceSize() const {
  483. assert(isBitPiece() && "Expected bit piece");
  484. return getElement(getNumElements() - 1);
  485. }
  486. DIObjCProperty *DIObjCProperty::getImpl(
  487. LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
  488. MDString *GetterName, MDString *SetterName, unsigned Attributes,
  489. Metadata *Type, StorageType Storage, bool ShouldCreate) {
  490. assert(isCanonical(Name) && "Expected canonical MDString");
  491. assert(isCanonical(GetterName) && "Expected canonical MDString");
  492. assert(isCanonical(SetterName) && "Expected canonical MDString");
  493. DEFINE_GETIMPL_LOOKUP(DIObjCProperty,
  494. (getString(Name), File, Line, getString(GetterName),
  495. getString(SetterName), Attributes, Type));
  496. Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
  497. DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
  498. }
  499. DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
  500. Metadata *Scope, Metadata *Entity,
  501. unsigned Line, MDString *Name,
  502. StorageType Storage,
  503. bool ShouldCreate) {
  504. assert(isCanonical(Name) && "Expected canonical MDString");
  505. DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
  506. (Tag, Scope, Entity, Line, getString(Name)));
  507. Metadata *Ops[] = {Scope, Entity, Name};
  508. DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
  509. }