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, StorageType Storage, bool ShouldCreate) {
315 assert(isCanonical(Producer) && "Expected canonical MDString");
316 assert(isCanonical(Flags) && "Expected canonical MDString");
317 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
318 DEFINE_GETIMPL_LOOKUP(
320 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
321 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
322 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
323 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
324 RetainedTypes, Subprograms, GlobalVariables,
326 DEFINE_GETIMPL_STORE(
328 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
331 DISubprogram *DILocalScope::getSubprogram() const {
332 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
333 return Block->getScope()->getSubprogram();
334 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
337 DISubprogram *DISubprogram::getImpl(
338 LLVMContext &Context, Metadata *Scope, MDString *Name,
339 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
340 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
341 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
342 unsigned Flags, bool IsOptimized, Metadata *Function,
343 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
344 StorageType Storage, bool ShouldCreate) {
345 assert(isCanonical(Name) && "Expected canonical MDString");
346 assert(isCanonical(LinkageName) && "Expected canonical MDString");
347 DEFINE_GETIMPL_LOOKUP(DISubprogram,
348 (Scope, getString(Name), getString(LinkageName), File,
349 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
350 ContainingType, Virtuality, VirtualIndex, Flags,
351 IsOptimized, Function, TemplateParams, Declaration,
353 Metadata *Ops[] = {File, Scope, Name, Name,
354 LinkageName, Type, ContainingType, Function,
355 TemplateParams, Declaration, Variables};
356 DEFINE_GETIMPL_STORE(DISubprogram,
357 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
358 IsLocalToUnit, IsDefinition, IsOptimized),
362 Function *DISubprogram::getFunction() const {
363 // FIXME: Should this be looking through bitcasts?
364 return dyn_cast_or_null<Function>(getFunctionConstant());
367 bool DISubprogram::describes(const Function *F) const {
368 assert(F && "Invalid function");
369 if (F == getFunction())
371 StringRef Name = getLinkageName();
374 return F->getName() == Name;
377 void DISubprogram::replaceFunction(Function *F) {
378 replaceFunction(F ? ConstantAsMetadata::get(F)
379 : static_cast<ConstantAsMetadata *>(nullptr));
382 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
383 Metadata *File, unsigned Line,
384 unsigned Column, StorageType Storage,
386 assert(Scope && "Expected scope");
387 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
388 Metadata *Ops[] = {File, Scope};
389 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
392 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
393 Metadata *Scope, Metadata *File,
394 unsigned Discriminator,
397 assert(Scope && "Expected scope");
398 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
399 Metadata *Ops[] = {File, Scope};
400 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
403 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
404 Metadata *File, MDString *Name, unsigned Line,
405 StorageType Storage, bool ShouldCreate) {
406 assert(isCanonical(Name) && "Expected canonical MDString");
407 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, getString(Name), Line));
408 Metadata *Ops[] = {File, Scope, Name};
409 DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
412 DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
417 assert(isCanonical(Name) && "Expected canonical MDString");
418 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (getString(Name), Type));
419 Metadata *Ops[] = {Name, Type};
420 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
423 DITemplateValueParameter *DITemplateValueParameter::getImpl(
424 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
425 Metadata *Value, StorageType Storage, bool ShouldCreate) {
426 assert(isCanonical(Name) && "Expected canonical MDString");
427 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter,
428 (Tag, getString(Name), Type, Value));
429 Metadata *Ops[] = {Name, Type, Value};
430 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
434 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
435 MDString *LinkageName, Metadata *File, unsigned Line,
436 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
438 Metadata *StaticDataMemberDeclaration,
439 StorageType Storage, bool ShouldCreate) {
440 assert(isCanonical(Name) && "Expected canonical MDString");
441 assert(isCanonical(LinkageName) && "Expected canonical MDString");
442 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
443 (Scope, getString(Name), getString(LinkageName), File,
444 Line, Type, IsLocalToUnit, IsDefinition, Variable,
445 StaticDataMemberDeclaration));
446 Metadata *Ops[] = {Scope, Name, File, Type,
447 Name, LinkageName, Variable, StaticDataMemberDeclaration};
448 DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
452 DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, unsigned Tag,
453 Metadata *Scope, MDString *Name,
454 Metadata *File, unsigned Line,
455 Metadata *Type, unsigned Arg,
456 unsigned Flags, StorageType Storage,
458 // 64K ought to be enough for any frontend.
459 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
461 assert(Scope && "Expected scope");
462 assert(isCanonical(Name) && "Expected canonical MDString");
463 DEFINE_GETIMPL_LOOKUP(DILocalVariable, (Tag, Scope, getString(Name), File,
464 Line, Type, Arg, Flags));
465 Metadata *Ops[] = {Scope, Name, File, Type};
466 DEFINE_GETIMPL_STORE(DILocalVariable, (Tag, Line, Arg, Flags), Ops);
469 DIExpression *DIExpression::getImpl(LLVMContext &Context,
470 ArrayRef<uint64_t> Elements,
471 StorageType Storage, bool ShouldCreate) {
472 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
473 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
476 unsigned DIExpression::ExprOperand::getSize() const {
478 case dwarf::DW_OP_bit_piece:
480 case dwarf::DW_OP_plus:
487 bool DIExpression::isValid() const {
488 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
489 // Check that there's space for the operand.
490 if (I->get() + I->getSize() > E->get())
493 // Check that the operand is valid.
494 switch (I->getOp()) {
497 case dwarf::DW_OP_bit_piece:
498 // Piece expressions must be at the end.
499 return I->get() + I->getSize() == E->get();
500 case dwarf::DW_OP_plus:
501 case dwarf::DW_OP_deref:
508 bool DIExpression::isBitPiece() const {
509 assert(isValid() && "Expected valid expression");
510 if (unsigned N = getNumElements())
512 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
516 uint64_t DIExpression::getBitPieceOffset() const {
517 assert(isBitPiece() && "Expected bit piece");
518 return getElement(getNumElements() - 2);
521 uint64_t DIExpression::getBitPieceSize() const {
522 assert(isBitPiece() && "Expected bit piece");
523 return getElement(getNumElements() - 1);
526 DIObjCProperty *DIObjCProperty::getImpl(
527 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
528 MDString *GetterName, MDString *SetterName, unsigned Attributes,
529 Metadata *Type, StorageType Storage, bool ShouldCreate) {
530 assert(isCanonical(Name) && "Expected canonical MDString");
531 assert(isCanonical(GetterName) && "Expected canonical MDString");
532 assert(isCanonical(SetterName) && "Expected canonical MDString");
533 DEFINE_GETIMPL_LOOKUP(DIObjCProperty,
534 (getString(Name), File, Line, getString(GetterName),
535 getString(SetterName), Attributes, Type));
536 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
537 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
540 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
541 Metadata *Scope, Metadata *Entity,
542 unsigned Line, MDString *Name,
545 assert(isCanonical(Name) && "Expected canonical MDString");
546 DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
547 (Tag, Scope, Entity, Line, getString(Name)));
548 Metadata *Ops[] = {Scope, Entity, Name};
549 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);