1 //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the debug info Metadata classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/DebugInfoMetadata.h"
15 #include "LLVMContextImpl.h"
16 #include "MetadataImpl.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/IR/Function.h"
22 MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
23 unsigned Column, ArrayRef<Metadata *> MDs)
24 : MDNode(C, MDLocationKind, Storage, MDs) {
25 assert((MDs.size() == 1 || MDs.size() == 2) &&
26 "Expected a scope and optional inlined-at");
28 // Set line and column.
29 assert(Column < (1u << 16) && "Expected 16-bit column");
31 SubclassData32 = Line;
32 SubclassData16 = Column;
35 static void adjustColumn(unsigned &Column) {
36 // Set to unknown on overflow. We only have 16 bits to play with here.
37 if (Column >= (1u << 16))
41 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
42 unsigned Column, Metadata *Scope,
43 Metadata *InlinedAt, StorageType Storage,
48 assert(Scope && "Expected scope");
49 if (Storage == Uniqued) {
51 getUniqued(Context.pImpl->MDLocations,
52 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
57 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
60 SmallVector<Metadata *, 2> Ops;
63 Ops.push_back(InlinedAt);
64 return storeImpl(new (Ops.size())
65 MDLocation(Context, Storage, Line, Column, Ops),
66 Storage, Context.pImpl->MDLocations);
69 unsigned DebugNode::getFlag(StringRef Flag) {
70 return StringSwitch<unsigned>(Flag)
71 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
72 #include "llvm/IR/DebugInfoFlags.def"
76 const char *DebugNode::getFlagString(unsigned Flag) {
80 #define HANDLE_DI_FLAG(ID, NAME) \
82 return "DIFlag" #NAME;
83 #include "llvm/IR/DebugInfoFlags.def"
87 unsigned DebugNode::splitFlags(unsigned Flags,
88 SmallVectorImpl<unsigned> &SplitFlags) {
89 // Accessibility flags need to be specially handled, since they're packed
91 if (unsigned A = Flags & FlagAccessibility) {
93 SplitFlags.push_back(FlagPrivate);
94 else if (A == FlagProtected)
95 SplitFlags.push_back(FlagProtected);
97 SplitFlags.push_back(FlagPublic);
101 #define HANDLE_DI_FLAG(ID, NAME) \
102 if (unsigned Bit = Flags & ID) { \
103 SplitFlags.push_back(Bit); \
106 #include "llvm/IR/DebugInfoFlags.def"
111 MDScopeRef MDScope::getScope() const {
112 if (auto *T = dyn_cast<MDType>(this))
113 return T->getScope();
115 if (auto *SP = dyn_cast<MDSubprogram>(this))
116 return SP->getScope();
118 if (auto *LB = dyn_cast<MDLexicalBlockBase>(this))
119 return MDScopeRef(LB->getScope());
121 if (auto *NS = dyn_cast<MDNamespace>(this))
122 return MDScopeRef(NS->getScope());
124 assert((isa<MDFile>(this) || isa<MDCompileUnit>(this)) &&
125 "Unhandled type of scope.");
129 StringRef MDScope::getName() const {
130 if (auto *T = dyn_cast<MDType>(this))
132 if (auto *SP = dyn_cast<MDSubprogram>(this))
133 return SP->getName();
134 if (auto *NS = dyn_cast<MDNamespace>(this))
135 return NS->getName();
136 assert((isa<MDLexicalBlockBase>(this) || isa<MDFile>(this) ||
137 isa<MDCompileUnit>(this)) &&
138 "Unhandled type of scope.");
142 static StringRef getString(const MDString *S) {
144 return S->getString();
149 static bool isCanonical(const MDString *S) {
150 return !S || !S->getString().empty();
154 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
156 ArrayRef<Metadata *> DwarfOps,
160 if (Storage == Uniqued) {
161 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
162 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
166 Hash = Key.getHash();
168 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
171 // Use a nullptr for empty headers.
172 assert(isCanonical(Header) && "Expected canonical MDString");
173 Metadata *PreOps[] = {Header};
174 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
175 Context, Storage, Hash, Tag, PreOps, DwarfOps),
176 Storage, Context.pImpl->GenericDebugNodes);
179 void GenericDebugNode::recalculateHash() {
180 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
183 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
184 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
185 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
187 if (Storage == Uniqued) { \
188 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
189 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
194 assert(ShouldCreate && \
195 "Expected non-uniqued nodes to always be created"); \
198 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
199 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
200 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
201 Storage, Context.pImpl->CLASS##s)
202 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
203 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
204 Storage, Context.pImpl->CLASS##s)
205 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
206 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
207 CLASS(Context, Storage, OPS), \
208 Storage, Context.pImpl->CLASS##s)
210 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
211 StorageType Storage, bool ShouldCreate) {
212 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
213 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
216 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
217 MDString *Name, StorageType Storage,
219 assert(isCanonical(Name) && "Expected canonical MDString");
220 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
221 Metadata *Ops[] = {Name};
222 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
225 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
226 MDString *Name, uint64_t SizeInBits,
227 uint64_t AlignInBits, unsigned Encoding,
228 StorageType Storage, bool ShouldCreate) {
229 assert(isCanonical(Name) && "Expected canonical MDString");
230 DEFINE_GETIMPL_LOOKUP(
231 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
232 Metadata *Ops[] = {nullptr, nullptr, Name};
233 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
237 MDDerivedType *MDDerivedType::getImpl(
238 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
239 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
240 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
241 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
242 assert(isCanonical(Name) && "Expected canonical MDString");
243 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
244 BaseType, SizeInBits, AlignInBits,
245 OffsetInBits, Flags, ExtraData));
246 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
247 DEFINE_GETIMPL_STORE(
248 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
252 MDCompositeType *MDCompositeType::getImpl(
253 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
254 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
255 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
256 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
257 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
259 assert(isCanonical(Name) && "Expected canonical MDString");
260 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
261 (Tag, getString(Name), File, Line, Scope, BaseType,
262 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
263 RuntimeLang, VTableHolder, TemplateParams,
264 getString(Identifier)));
265 Metadata *Ops[] = {File, Scope, Name, BaseType,
266 Elements, VTableHolder, TemplateParams, Identifier};
267 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
268 AlignInBits, OffsetInBits, Flags),
272 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
273 unsigned Flags, Metadata *TypeArray,
276 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
277 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
278 TypeArray, nullptr, nullptr, nullptr};
279 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
282 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
283 MDString *Directory, StorageType Storage,
285 assert(isCanonical(Filename) && "Expected canonical MDString");
286 assert(isCanonical(Directory) && "Expected canonical MDString");
287 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
288 Metadata *Ops[] = {Filename, Directory};
289 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops);
292 MDCompileUnit *MDCompileUnit::getImpl(
293 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
294 MDString *Producer, bool IsOptimized, MDString *Flags,
295 unsigned RuntimeVersion, MDString *SplitDebugFilename,
296 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
297 Metadata *Subprograms, Metadata *GlobalVariables,
298 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
299 assert(isCanonical(Producer) && "Expected canonical MDString");
300 assert(isCanonical(Flags) && "Expected canonical MDString");
301 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
302 DEFINE_GETIMPL_LOOKUP(
304 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
305 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
306 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
307 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
308 RetainedTypes, Subprograms, GlobalVariables,
310 DEFINE_GETIMPL_STORE(
312 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
315 MDSubprogram *MDLocalScope::getSubprogram() const {
316 if (auto *Block = dyn_cast<MDLexicalBlockBase>(this))
317 return Block->getScope()->getSubprogram();
318 return const_cast<MDSubprogram *>(cast<MDSubprogram>(this));
321 MDSubprogram *MDSubprogram::getImpl(
322 LLVMContext &Context, Metadata *Scope, MDString *Name,
323 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
324 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
325 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
326 unsigned Flags, bool IsOptimized, Metadata *Function,
327 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
328 StorageType Storage, bool ShouldCreate) {
329 assert(isCanonical(Name) && "Expected canonical MDString");
330 assert(isCanonical(LinkageName) && "Expected canonical MDString");
331 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
332 (Scope, getString(Name), getString(LinkageName), File,
333 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
334 ContainingType, Virtuality, VirtualIndex, Flags,
335 IsOptimized, Function, TemplateParams, Declaration,
337 Metadata *Ops[] = {File, Scope, Name, Name,
338 LinkageName, Type, ContainingType, Function,
339 TemplateParams, Declaration, Variables};
340 DEFINE_GETIMPL_STORE(MDSubprogram,
341 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
342 IsLocalToUnit, IsDefinition, IsOptimized),
346 Function *MDSubprogram::getFunction() const {
347 // FIXME: Should this be looking through bitcasts?
348 return dyn_cast_or_null<Function>(getFunctionConstant());
351 bool MDSubprogram::describes(const Function *F) const {
352 assert(F && "Invalid function");
353 if (F == getFunction())
355 StringRef Name = getLinkageName();
358 return F->getName() == Name;
361 void MDSubprogram::replaceFunction(Function *F) {
362 replaceFunction(F ? ConstantAsMetadata::get(F)
363 : static_cast<ConstantAsMetadata *>(nullptr));
366 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
367 Metadata *File, unsigned Line,
368 unsigned Column, StorageType Storage,
370 assert(Scope && "Expected scope");
371 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
372 Metadata *Ops[] = {File, Scope};
373 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
376 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
377 Metadata *Scope, Metadata *File,
378 unsigned Discriminator,
381 assert(Scope && "Expected scope");
382 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
383 Metadata *Ops[] = {File, Scope};
384 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
387 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
388 Metadata *File, MDString *Name, unsigned Line,
389 StorageType Storage, bool ShouldCreate) {
390 assert(isCanonical(Name) && "Expected canonical MDString");
391 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
392 Metadata *Ops[] = {File, Scope, Name};
393 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
396 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
401 assert(isCanonical(Name) && "Expected canonical MDString");
402 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
403 Metadata *Ops[] = {Name, Type};
404 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
407 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
408 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
409 Metadata *Value, StorageType Storage, bool ShouldCreate) {
410 assert(isCanonical(Name) && "Expected canonical MDString");
411 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
412 (Tag, getString(Name), Type, Value));
413 Metadata *Ops[] = {Name, Type, Value};
414 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
418 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
419 MDString *LinkageName, Metadata *File, unsigned Line,
420 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
422 Metadata *StaticDataMemberDeclaration,
423 StorageType Storage, bool ShouldCreate) {
424 assert(isCanonical(Name) && "Expected canonical MDString");
425 assert(isCanonical(LinkageName) && "Expected canonical MDString");
426 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
427 (Scope, getString(Name), getString(LinkageName), File,
428 Line, Type, IsLocalToUnit, IsDefinition, Variable,
429 StaticDataMemberDeclaration));
430 Metadata *Ops[] = {Scope, Name, File, Type,
431 Name, LinkageName, Variable, StaticDataMemberDeclaration};
432 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
436 MDLocalVariable *MDLocalVariable::getImpl(
437 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
438 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
439 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
440 // Truncate Arg to 8 bits.
442 // FIXME: This is gross (and should be changed to an assert or removed), but
443 // it matches historical behaviour for now.
444 Arg &= (1u << 8) - 1;
446 assert(Scope && "Expected scope");
447 assert(isCanonical(Name) && "Expected canonical MDString");
448 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
449 Line, Type, Arg, Flags, InlinedAt));
450 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
451 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
454 MDExpression *MDExpression::getImpl(LLVMContext &Context,
455 ArrayRef<uint64_t> Elements,
456 StorageType Storage, bool ShouldCreate) {
457 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
458 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
461 unsigned MDExpression::ExprOperand::getSize() const {
463 case dwarf::DW_OP_bit_piece:
465 case dwarf::DW_OP_plus:
472 bool MDExpression::isValid() const {
473 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
474 // Check that there's space for the operand.
475 if (I->get() + I->getSize() > E->get())
478 // Check that the operand is valid.
479 switch (I->getOp()) {
482 case dwarf::DW_OP_bit_piece:
483 // Piece expressions must be at the end.
484 return I->get() + I->getSize() == E->get();
485 case dwarf::DW_OP_plus:
486 case dwarf::DW_OP_deref:
493 bool MDExpression::isBitPiece() const {
494 assert(isValid() && "Expected valid expression");
495 if (unsigned N = getNumElements())
497 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
501 uint64_t MDExpression::getBitPieceOffset() const {
502 assert(isBitPiece() && "Expected bit piece");
503 return getElement(getNumElements() - 2);
506 uint64_t MDExpression::getBitPieceSize() const {
507 assert(isBitPiece() && "Expected bit piece");
508 return getElement(getNumElements() - 1);
511 MDObjCProperty *MDObjCProperty::getImpl(
512 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
513 MDString *GetterName, MDString *SetterName, unsigned Attributes,
514 Metadata *Type, StorageType Storage, bool ShouldCreate) {
515 assert(isCanonical(Name) && "Expected canonical MDString");
516 assert(isCanonical(GetterName) && "Expected canonical MDString");
517 assert(isCanonical(SetterName) && "Expected canonical MDString");
518 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
519 (getString(Name), File, Line, getString(GetterName),
520 getString(SetterName), Attributes, Type));
521 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
522 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
525 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
526 Metadata *Scope, Metadata *Entity,
527 unsigned Line, MDString *Name,
530 assert(isCanonical(Name) && "Expected canonical MDString");
531 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
532 (Tag, Scope, Entity, Line, getString(Name)));
533 Metadata *Ops[] = {Scope, Entity, Name};
534 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);