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"
112 static StringRef getString(const MDString *S) {
114 return S->getString();
119 static bool isCanonical(const MDString *S) {
120 return !S || !S->getString().empty();
124 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
126 ArrayRef<Metadata *> DwarfOps,
130 if (Storage == Uniqued) {
131 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
132 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
136 Hash = Key.getHash();
138 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
141 // Use a nullptr for empty headers.
142 assert(isCanonical(Header) && "Expected canonical MDString");
143 Metadata *PreOps[] = {Header};
144 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
145 Context, Storage, Hash, Tag, PreOps, DwarfOps),
146 Storage, Context.pImpl->GenericDebugNodes);
149 void GenericDebugNode::recalculateHash() {
150 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
153 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
154 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
155 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
157 if (Storage == Uniqued) { \
158 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
159 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
164 assert(ShouldCreate && \
165 "Expected non-uniqued nodes to always be created"); \
168 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
169 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
170 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
171 Storage, Context.pImpl->CLASS##s)
172 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
173 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
174 Storage, Context.pImpl->CLASS##s)
175 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
176 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
177 CLASS(Context, Storage, OPS), \
178 Storage, Context.pImpl->CLASS##s)
180 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
181 StorageType Storage, bool ShouldCreate) {
182 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
183 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
186 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
187 MDString *Name, StorageType Storage,
189 assert(isCanonical(Name) && "Expected canonical MDString");
190 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
191 Metadata *Ops[] = {Name};
192 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
195 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
196 MDString *Name, uint64_t SizeInBits,
197 uint64_t AlignInBits, unsigned Encoding,
198 StorageType Storage, bool ShouldCreate) {
199 assert(isCanonical(Name) && "Expected canonical MDString");
200 DEFINE_GETIMPL_LOOKUP(
201 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
202 Metadata *Ops[] = {nullptr, nullptr, Name};
203 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
207 MDDerivedType *MDDerivedType::getImpl(
208 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
209 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
210 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
211 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
212 assert(isCanonical(Name) && "Expected canonical MDString");
213 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
214 BaseType, SizeInBits, AlignInBits,
215 OffsetInBits, Flags, ExtraData));
216 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
217 DEFINE_GETIMPL_STORE(
218 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
222 MDCompositeType *MDCompositeType::getImpl(
223 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
224 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
225 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
226 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
227 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
229 assert(isCanonical(Name) && "Expected canonical MDString");
230 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
231 (Tag, getString(Name), File, Line, Scope, BaseType,
232 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
233 RuntimeLang, VTableHolder, TemplateParams,
234 getString(Identifier)));
235 Metadata *Ops[] = {File, Scope, Name, BaseType,
236 Elements, VTableHolder, TemplateParams, Identifier};
237 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
238 AlignInBits, OffsetInBits, Flags),
242 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
243 unsigned Flags, Metadata *TypeArray,
246 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
247 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
248 TypeArray, nullptr, nullptr, nullptr};
249 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
252 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
253 MDString *Directory, StorageType Storage,
255 assert(isCanonical(Filename) && "Expected canonical MDString");
256 assert(isCanonical(Directory) && "Expected canonical MDString");
257 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
258 Metadata *Ops[] = {Filename, Directory};
259 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops);
262 MDCompileUnit *MDCompileUnit::getImpl(
263 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
264 MDString *Producer, bool IsOptimized, MDString *Flags,
265 unsigned RuntimeVersion, MDString *SplitDebugFilename,
266 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
267 Metadata *Subprograms, Metadata *GlobalVariables,
268 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
269 assert(isCanonical(Producer) && "Expected canonical MDString");
270 assert(isCanonical(Flags) && "Expected canonical MDString");
271 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
272 DEFINE_GETIMPL_LOOKUP(
274 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
275 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
276 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
277 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
278 RetainedTypes, Subprograms, GlobalVariables,
280 DEFINE_GETIMPL_STORE(
282 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
285 MDSubprogram *MDLocalScope::getSubprogram() const {
286 if (auto *Block = dyn_cast<MDLexicalBlockBase>(this))
287 return Block->getScope()->getSubprogram();
288 return const_cast<MDSubprogram *>(cast<MDSubprogram>(this));
291 MDSubprogram *MDSubprogram::getImpl(
292 LLVMContext &Context, Metadata *Scope, MDString *Name,
293 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
294 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
295 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
296 unsigned Flags, bool IsOptimized, Metadata *Function,
297 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
298 StorageType Storage, bool ShouldCreate) {
299 assert(isCanonical(Name) && "Expected canonical MDString");
300 assert(isCanonical(LinkageName) && "Expected canonical MDString");
301 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
302 (Scope, getString(Name), getString(LinkageName), File,
303 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
304 ContainingType, Virtuality, VirtualIndex, Flags,
305 IsOptimized, Function, TemplateParams, Declaration,
307 Metadata *Ops[] = {File, Scope, Name, Name,
308 LinkageName, Type, ContainingType, Function,
309 TemplateParams, Declaration, Variables};
310 DEFINE_GETIMPL_STORE(MDSubprogram,
311 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
312 IsLocalToUnit, IsDefinition, IsOptimized),
316 void MDSubprogram::replaceFunction(Function *F) {
317 replaceFunction(F ? ConstantAsMetadata::get(F)
318 : static_cast<ConstantAsMetadata *>(nullptr));
321 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
322 Metadata *File, unsigned Line,
323 unsigned Column, StorageType Storage,
325 assert(Scope && "Expected scope");
326 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
327 Metadata *Ops[] = {File, Scope};
328 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
331 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
332 Metadata *Scope, Metadata *File,
333 unsigned Discriminator,
336 assert(Scope && "Expected scope");
337 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
338 Metadata *Ops[] = {File, Scope};
339 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
342 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
343 Metadata *File, MDString *Name, unsigned Line,
344 StorageType Storage, bool ShouldCreate) {
345 assert(isCanonical(Name) && "Expected canonical MDString");
346 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
347 Metadata *Ops[] = {File, Scope, Name};
348 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
351 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
356 assert(isCanonical(Name) && "Expected canonical MDString");
357 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
358 Metadata *Ops[] = {Name, Type};
359 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
362 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
363 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
364 Metadata *Value, StorageType Storage, bool ShouldCreate) {
365 assert(isCanonical(Name) && "Expected canonical MDString");
366 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
367 (Tag, getString(Name), Type, Value));
368 Metadata *Ops[] = {Name, Type, Value};
369 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
373 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
374 MDString *LinkageName, Metadata *File, unsigned Line,
375 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
377 Metadata *StaticDataMemberDeclaration,
378 StorageType Storage, bool ShouldCreate) {
379 assert(isCanonical(Name) && "Expected canonical MDString");
380 assert(isCanonical(LinkageName) && "Expected canonical MDString");
381 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
382 (Scope, getString(Name), getString(LinkageName), File,
383 Line, Type, IsLocalToUnit, IsDefinition, Variable,
384 StaticDataMemberDeclaration));
385 Metadata *Ops[] = {Scope, Name, File, Type,
386 Name, LinkageName, Variable, StaticDataMemberDeclaration};
387 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
391 MDLocalVariable *MDLocalVariable::getImpl(
392 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
393 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
394 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
395 // Truncate Arg to 8 bits.
397 // FIXME: This is gross (and should be changed to an assert or removed), but
398 // it matches historical behaviour for now.
399 Arg &= (1u << 8) - 1;
401 assert(Scope && "Expected scope");
402 assert(isCanonical(Name) && "Expected canonical MDString");
403 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
404 Line, Type, Arg, Flags, InlinedAt));
405 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
406 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
409 MDExpression *MDExpression::getImpl(LLVMContext &Context,
410 ArrayRef<uint64_t> Elements,
411 StorageType Storage, bool ShouldCreate) {
412 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
413 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
416 unsigned MDExpression::ExprOperand::getSize() const {
418 case dwarf::DW_OP_bit_piece:
420 case dwarf::DW_OP_plus:
427 bool MDExpression::isValid() const {
428 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
429 // Check that there's space for the operand.
430 if (I->get() + I->getSize() > E->get())
433 // Check that the operand is valid.
434 switch (I->getOp()) {
437 case dwarf::DW_OP_bit_piece:
438 // Piece expressions must be at the end.
439 return I->get() + I->getSize() == E->get();
440 case dwarf::DW_OP_plus:
441 case dwarf::DW_OP_deref:
448 MDObjCProperty *MDObjCProperty::getImpl(
449 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
450 MDString *GetterName, MDString *SetterName, unsigned Attributes,
451 Metadata *Type, StorageType Storage, bool ShouldCreate) {
452 assert(isCanonical(Name) && "Expected canonical MDString");
453 assert(isCanonical(GetterName) && "Expected canonical MDString");
454 assert(isCanonical(SetterName) && "Expected canonical MDString");
455 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
456 (getString(Name), File, Line, getString(GetterName),
457 getString(SetterName), Attributes, Type));
458 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
459 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
462 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
463 Metadata *Scope, Metadata *Entity,
464 unsigned Line, MDString *Name,
467 assert(isCanonical(Name) && "Expected canonical MDString");
468 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
469 (Tag, Scope, Entity, Line, getString(Name)));
470 Metadata *Ops[] = {Scope, Entity, Name};
471 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);