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 void MDSubprogram::replaceFunction(Function *F) {
352 replaceFunction(F ? ConstantAsMetadata::get(F)
353 : static_cast<ConstantAsMetadata *>(nullptr));
356 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
357 Metadata *File, unsigned Line,
358 unsigned Column, StorageType Storage,
360 assert(Scope && "Expected scope");
361 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
362 Metadata *Ops[] = {File, Scope};
363 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
366 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
367 Metadata *Scope, Metadata *File,
368 unsigned Discriminator,
371 assert(Scope && "Expected scope");
372 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
373 Metadata *Ops[] = {File, Scope};
374 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
377 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
378 Metadata *File, MDString *Name, unsigned Line,
379 StorageType Storage, bool ShouldCreate) {
380 assert(isCanonical(Name) && "Expected canonical MDString");
381 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
382 Metadata *Ops[] = {File, Scope, Name};
383 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
386 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
391 assert(isCanonical(Name) && "Expected canonical MDString");
392 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
393 Metadata *Ops[] = {Name, Type};
394 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
397 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
398 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
399 Metadata *Value, StorageType Storage, bool ShouldCreate) {
400 assert(isCanonical(Name) && "Expected canonical MDString");
401 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
402 (Tag, getString(Name), Type, Value));
403 Metadata *Ops[] = {Name, Type, Value};
404 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
408 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
409 MDString *LinkageName, Metadata *File, unsigned Line,
410 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
412 Metadata *StaticDataMemberDeclaration,
413 StorageType Storage, bool ShouldCreate) {
414 assert(isCanonical(Name) && "Expected canonical MDString");
415 assert(isCanonical(LinkageName) && "Expected canonical MDString");
416 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
417 (Scope, getString(Name), getString(LinkageName), File,
418 Line, Type, IsLocalToUnit, IsDefinition, Variable,
419 StaticDataMemberDeclaration));
420 Metadata *Ops[] = {Scope, Name, File, Type,
421 Name, LinkageName, Variable, StaticDataMemberDeclaration};
422 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
426 MDLocalVariable *MDLocalVariable::getImpl(
427 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
428 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
429 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
430 // Truncate Arg to 8 bits.
432 // FIXME: This is gross (and should be changed to an assert or removed), but
433 // it matches historical behaviour for now.
434 Arg &= (1u << 8) - 1;
436 assert(Scope && "Expected scope");
437 assert(isCanonical(Name) && "Expected canonical MDString");
438 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
439 Line, Type, Arg, Flags, InlinedAt));
440 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
441 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
444 MDExpression *MDExpression::getImpl(LLVMContext &Context,
445 ArrayRef<uint64_t> Elements,
446 StorageType Storage, bool ShouldCreate) {
447 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
448 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
451 unsigned MDExpression::ExprOperand::getSize() const {
453 case dwarf::DW_OP_bit_piece:
455 case dwarf::DW_OP_plus:
462 bool MDExpression::isValid() const {
463 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
464 // Check that there's space for the operand.
465 if (I->get() + I->getSize() > E->get())
468 // Check that the operand is valid.
469 switch (I->getOp()) {
472 case dwarf::DW_OP_bit_piece:
473 // Piece expressions must be at the end.
474 return I->get() + I->getSize() == E->get();
475 case dwarf::DW_OP_plus:
476 case dwarf::DW_OP_deref:
483 bool MDExpression::isBitPiece() const {
484 assert(isValid() && "Expected valid expression");
485 if (unsigned N = getNumElements())
487 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
491 uint64_t MDExpression::getBitPieceOffset() const {
492 assert(isBitPiece() && "Expected bit piece");
493 return getElement(getNumElements() - 2);
496 uint64_t MDExpression::getBitPieceSize() const {
497 assert(isBitPiece() && "Expected bit piece");
498 return getElement(getNumElements() - 1);
501 MDObjCProperty *MDObjCProperty::getImpl(
502 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
503 MDString *GetterName, MDString *SetterName, unsigned Attributes,
504 Metadata *Type, StorageType Storage, bool ShouldCreate) {
505 assert(isCanonical(Name) && "Expected canonical MDString");
506 assert(isCanonical(GetterName) && "Expected canonical MDString");
507 assert(isCanonical(SetterName) && "Expected canonical MDString");
508 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
509 (getString(Name), File, Line, getString(GetterName),
510 getString(SetterName), Attributes, Type));
511 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
512 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
515 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
516 Metadata *Scope, Metadata *Entity,
517 unsigned Line, MDString *Name,
520 assert(isCanonical(Name) && "Expected canonical MDString");
521 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
522 (Tag, Scope, Entity, Line, getString(Name)));
523 Metadata *Ops[] = {Scope, Entity, Name};
524 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);