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 DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
23 unsigned Column, ArrayRef<Metadata *> MDs)
24 : MDNode(C, DILocationKind, 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 DILocation *DILocation::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->DILocations,
52 DILocationInfo::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 DILocation(Context, Storage, Line, Column, Ops),
66 Storage, Context.pImpl->DILocations);
69 unsigned DILocation::computeNewDiscriminator() const {
70 // FIXME: This seems completely wrong.
72 // 1. If two modules are generated in the same context, then the second
73 // Module will get different discriminators than it would have if it were
74 // generated in its own context.
75 // 2. If this function is called after round-tripping to bitcode instead of
76 // before, it will give a different (and potentially incorrect!) return.
78 // The discriminator should instead be calculated from local information
79 // where it's actually needed. This logic should be moved to
80 // AddDiscriminators::runOnFunction(), where it doesn't pollute the
82 std::pair<const char *, unsigned> Key(getFilename().data(), getLine());
83 return ++getContext().pImpl->DiscriminatorTable[Key];
86 unsigned DINode::getFlag(StringRef Flag) {
87 return StringSwitch<unsigned>(Flag)
88 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
89 #include "llvm/IR/DebugInfoFlags.def"
93 const char *DINode::getFlagString(unsigned Flag) {
97 #define HANDLE_DI_FLAG(ID, NAME) \
99 return "DIFlag" #NAME;
100 #include "llvm/IR/DebugInfoFlags.def"
104 unsigned DINode::splitFlags(unsigned Flags,
105 SmallVectorImpl<unsigned> &SplitFlags) {
106 // Accessibility flags need to be specially handled, since they're packed
108 if (unsigned A = Flags & FlagAccessibility) {
109 if (A == FlagPrivate)
110 SplitFlags.push_back(FlagPrivate);
111 else if (A == FlagProtected)
112 SplitFlags.push_back(FlagProtected);
114 SplitFlags.push_back(FlagPublic);
118 #define HANDLE_DI_FLAG(ID, NAME) \
119 if (unsigned Bit = Flags & ID) { \
120 SplitFlags.push_back(Bit); \
123 #include "llvm/IR/DebugInfoFlags.def"
128 DIScopeRef DIScope::getScope() const {
129 if (auto *T = dyn_cast<DIType>(this))
130 return T->getScope();
132 if (auto *SP = dyn_cast<DISubprogram>(this))
133 return SP->getScope();
135 if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
136 return DIScopeRef(LB->getScope());
138 if (auto *NS = dyn_cast<DINamespace>(this))
139 return DIScopeRef(NS->getScope());
141 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
142 "Unhandled type of scope.");
146 StringRef DIScope::getName() const {
147 if (auto *T = dyn_cast<DIType>(this))
149 if (auto *SP = dyn_cast<DISubprogram>(this))
150 return SP->getName();
151 if (auto *NS = dyn_cast<DINamespace>(this))
152 return NS->getName();
153 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
154 isa<DICompileUnit>(this)) &&
155 "Unhandled type of scope.");
159 static StringRef getString(const MDString *S) {
161 return S->getString();
166 static bool isCanonical(const MDString *S) {
167 return !S || !S->getString().empty();
171 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
173 ArrayRef<Metadata *> DwarfOps,
174 StorageType Storage, bool ShouldCreate) {
176 if (Storage == Uniqued) {
177 GenericDINodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
178 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
182 Hash = Key.getHash();
184 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
187 // Use a nullptr for empty headers.
188 assert(isCanonical(Header) && "Expected canonical MDString");
189 Metadata *PreOps[] = {Header};
190 return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
191 Context, Storage, Hash, Tag, PreOps, DwarfOps),
192 Storage, Context.pImpl->GenericDINodes);
195 void GenericDINode::recalculateHash() {
196 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
199 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
200 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
201 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
203 if (Storage == Uniqued) { \
204 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
205 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
210 assert(ShouldCreate && \
211 "Expected non-uniqued nodes to always be created"); \
214 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
215 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
216 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
217 Storage, Context.pImpl->CLASS##s)
218 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
219 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
220 Storage, Context.pImpl->CLASS##s)
221 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
222 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
223 CLASS(Context, Storage, OPS), \
224 Storage, Context.pImpl->CLASS##s)
226 DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
227 StorageType Storage, bool ShouldCreate) {
228 DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
229 DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
232 DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
233 MDString *Name, StorageType Storage,
235 assert(isCanonical(Name) && "Expected canonical MDString");
236 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, getString(Name)));
237 Metadata *Ops[] = {Name};
238 DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
241 DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
242 MDString *Name, uint64_t SizeInBits,
243 uint64_t AlignInBits, unsigned Encoding,
244 StorageType Storage, bool ShouldCreate) {
245 assert(isCanonical(Name) && "Expected canonical MDString");
246 DEFINE_GETIMPL_LOOKUP(
247 DIBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
248 Metadata *Ops[] = {nullptr, nullptr, Name};
249 DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
253 DIDerivedType *DIDerivedType::getImpl(
254 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
255 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
256 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
257 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
258 assert(isCanonical(Name) && "Expected canonical MDString");
259 DEFINE_GETIMPL_LOOKUP(DIDerivedType, (Tag, getString(Name), File, Line, Scope,
260 BaseType, SizeInBits, AlignInBits,
261 OffsetInBits, Flags, ExtraData));
262 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
263 DEFINE_GETIMPL_STORE(
264 DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
268 DICompositeType *DICompositeType::getImpl(
269 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
270 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
271 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
272 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
273 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
275 assert(isCanonical(Name) && "Expected canonical MDString");
276 DEFINE_GETIMPL_LOOKUP(DICompositeType,
277 (Tag, getString(Name), File, Line, Scope, BaseType,
278 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
279 RuntimeLang, VTableHolder, TemplateParams,
280 getString(Identifier)));
281 Metadata *Ops[] = {File, Scope, Name, BaseType,
282 Elements, VTableHolder, TemplateParams, Identifier};
283 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
284 AlignInBits, OffsetInBits, Flags),
288 DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context,
289 unsigned Flags, Metadata *TypeArray,
292 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, TypeArray));
293 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
294 TypeArray, nullptr, nullptr, nullptr};
295 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags), Ops);
298 DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
299 MDString *Directory, StorageType Storage,
301 assert(isCanonical(Filename) && "Expected canonical MDString");
302 assert(isCanonical(Directory) && "Expected canonical MDString");
303 DEFINE_GETIMPL_LOOKUP(DIFile, (getString(Filename), getString(Directory)));
304 Metadata *Ops[] = {Filename, Directory};
305 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIFile, Ops);
308 DICompileUnit *DICompileUnit::getImpl(
309 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
310 MDString *Producer, bool IsOptimized, MDString *Flags,
311 unsigned RuntimeVersion, MDString *SplitDebugFilename,
312 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
313 Metadata *Subprograms, Metadata *GlobalVariables,
314 Metadata *ImportedEntities, uint64_t DWOId,
315 StorageType Storage, bool ShouldCreate) {
316 assert(isCanonical(Producer) && "Expected canonical MDString");
317 assert(isCanonical(Flags) && "Expected canonical MDString");
318 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
319 DEFINE_GETIMPL_LOOKUP(
321 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
322 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
323 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, DWOId));
324 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
325 RetainedTypes, Subprograms, GlobalVariables,
327 DEFINE_GETIMPL_STORE(
329 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind, DWOId), Ops);
332 DISubprogram *DILocalScope::getSubprogram() const {
333 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
334 return Block->getScope()->getSubprogram();
335 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
338 DISubprogram *DISubprogram::getImpl(
339 LLVMContext &Context, Metadata *Scope, MDString *Name,
340 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
341 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
342 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
343 unsigned Flags, bool IsOptimized, Metadata *Function,
344 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
345 StorageType Storage, bool ShouldCreate) {
346 assert(isCanonical(Name) && "Expected canonical MDString");
347 assert(isCanonical(LinkageName) && "Expected canonical MDString");
348 DEFINE_GETIMPL_LOOKUP(DISubprogram,
349 (Scope, getString(Name), getString(LinkageName), File,
350 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
351 ContainingType, Virtuality, VirtualIndex, Flags,
352 IsOptimized, Function, TemplateParams, Declaration,
354 Metadata *Ops[] = {File, Scope, Name, Name,
355 LinkageName, Type, ContainingType, Function,
356 TemplateParams, Declaration, Variables};
357 DEFINE_GETIMPL_STORE(DISubprogram,
358 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
359 IsLocalToUnit, IsDefinition, IsOptimized),
363 Function *DISubprogram::getFunction() const {
364 // FIXME: Should this be looking through bitcasts?
365 return dyn_cast_or_null<Function>(getFunctionConstant());
368 bool DISubprogram::describes(const Function *F) const {
369 assert(F && "Invalid function");
370 if (F == getFunction())
372 StringRef Name = getLinkageName();
375 return F->getName() == Name;
378 void DISubprogram::replaceFunction(Function *F) {
379 replaceFunction(F ? ConstantAsMetadata::get(F)
380 : static_cast<ConstantAsMetadata *>(nullptr));
383 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
384 Metadata *File, unsigned Line,
385 unsigned Column, StorageType Storage,
387 assert(Scope && "Expected scope");
388 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
389 Metadata *Ops[] = {File, Scope};
390 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
393 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
394 Metadata *Scope, Metadata *File,
395 unsigned Discriminator,
398 assert(Scope && "Expected scope");
399 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
400 Metadata *Ops[] = {File, Scope};
401 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
404 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
405 Metadata *File, MDString *Name, unsigned Line,
406 StorageType Storage, bool ShouldCreate) {
407 assert(isCanonical(Name) && "Expected canonical MDString");
408 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, getString(Name), Line));
409 Metadata *Ops[] = {File, Scope, Name};
410 DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
413 DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
418 assert(isCanonical(Name) && "Expected canonical MDString");
419 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (getString(Name), Type));
420 Metadata *Ops[] = {Name, Type};
421 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
424 DITemplateValueParameter *DITemplateValueParameter::getImpl(
425 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
426 Metadata *Value, StorageType Storage, bool ShouldCreate) {
427 assert(isCanonical(Name) && "Expected canonical MDString");
428 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter,
429 (Tag, getString(Name), Type, Value));
430 Metadata *Ops[] = {Name, Type, Value};
431 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
435 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
436 MDString *LinkageName, Metadata *File, unsigned Line,
437 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
439 Metadata *StaticDataMemberDeclaration,
440 StorageType Storage, bool ShouldCreate) {
441 assert(isCanonical(Name) && "Expected canonical MDString");
442 assert(isCanonical(LinkageName) && "Expected canonical MDString");
443 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
444 (Scope, getString(Name), getString(LinkageName), File,
445 Line, Type, IsLocalToUnit, IsDefinition, Variable,
446 StaticDataMemberDeclaration));
447 Metadata *Ops[] = {Scope, Name, File, Type,
448 Name, LinkageName, Variable, StaticDataMemberDeclaration};
449 DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
453 DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, unsigned Tag,
454 Metadata *Scope, MDString *Name,
455 Metadata *File, unsigned Line,
456 Metadata *Type, unsigned Arg,
457 unsigned Flags, StorageType Storage,
459 // 64K ought to be enough for any frontend.
460 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
462 assert(Scope && "Expected scope");
463 assert(isCanonical(Name) && "Expected canonical MDString");
464 DEFINE_GETIMPL_LOOKUP(DILocalVariable, (Tag, Scope, getString(Name), File,
465 Line, Type, Arg, Flags));
466 Metadata *Ops[] = {Scope, Name, File, Type};
467 DEFINE_GETIMPL_STORE(DILocalVariable, (Tag, Line, Arg, Flags), Ops);
470 DIExpression *DIExpression::getImpl(LLVMContext &Context,
471 ArrayRef<uint64_t> Elements,
472 StorageType Storage, bool ShouldCreate) {
473 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
474 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
477 unsigned DIExpression::ExprOperand::getSize() const {
479 case dwarf::DW_OP_bit_piece:
481 case dwarf::DW_OP_plus:
488 bool DIExpression::isValid() const {
489 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
490 // Check that there's space for the operand.
491 if (I->get() + I->getSize() > E->get())
494 // Check that the operand is valid.
495 switch (I->getOp()) {
498 case dwarf::DW_OP_bit_piece:
499 // Piece expressions must be at the end.
500 return I->get() + I->getSize() == E->get();
501 case dwarf::DW_OP_plus:
502 case dwarf::DW_OP_deref:
509 bool DIExpression::isBitPiece() const {
510 assert(isValid() && "Expected valid expression");
511 if (unsigned N = getNumElements())
513 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
517 uint64_t DIExpression::getBitPieceOffset() const {
518 assert(isBitPiece() && "Expected bit piece");
519 return getElement(getNumElements() - 2);
522 uint64_t DIExpression::getBitPieceSize() const {
523 assert(isBitPiece() && "Expected bit piece");
524 return getElement(getNumElements() - 1);
527 DIObjCProperty *DIObjCProperty::getImpl(
528 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
529 MDString *GetterName, MDString *SetterName, unsigned Attributes,
530 Metadata *Type, StorageType Storage, bool ShouldCreate) {
531 assert(isCanonical(Name) && "Expected canonical MDString");
532 assert(isCanonical(GetterName) && "Expected canonical MDString");
533 assert(isCanonical(SetterName) && "Expected canonical MDString");
534 DEFINE_GETIMPL_LOOKUP(DIObjCProperty,
535 (getString(Name), File, Line, getString(GetterName),
536 getString(SetterName), Attributes, Type));
537 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
538 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
541 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
542 Metadata *Scope, Metadata *Entity,
543 unsigned Line, MDString *Name,
546 assert(isCanonical(Name) && "Expected canonical MDString");
547 DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
548 (Tag, Scope, Entity, Line, getString(Name)));
549 Metadata *Ops[] = {Scope, Entity, Name};
550 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);