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/IR/Function.h"
21 MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
22 unsigned Column, ArrayRef<Metadata *> MDs)
23 : MDNode(C, MDLocationKind, Storage, MDs) {
24 assert((MDs.size() == 1 || MDs.size() == 2) &&
25 "Expected a scope and optional inlined-at");
27 // Set line and column.
28 assert(Column < (1u << 16) && "Expected 16-bit column");
30 SubclassData32 = Line;
31 SubclassData16 = Column;
34 static void adjustColumn(unsigned &Column) {
35 // Set to unknown on overflow. We only have 16 bits to play with here.
36 if (Column >= (1u << 16))
40 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
41 unsigned Column, Metadata *Scope,
42 Metadata *InlinedAt, StorageType Storage,
47 assert(Scope && "Expected scope");
48 if (Storage == Uniqued) {
50 getUniqued(Context.pImpl->MDLocations,
51 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
56 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
59 SmallVector<Metadata *, 2> Ops;
62 Ops.push_back(InlinedAt);
63 return storeImpl(new (Ops.size())
64 MDLocation(Context, Storage, Line, Column, Ops),
65 Storage, Context.pImpl->MDLocations);
68 static StringRef getString(const MDString *S) {
70 return S->getString();
75 static bool isCanonical(const MDString *S) {
76 return !S || !S->getString().empty();
80 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
82 ArrayRef<Metadata *> DwarfOps,
86 if (Storage == Uniqued) {
87 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
88 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
94 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
97 // Use a nullptr for empty headers.
98 assert(isCanonical(Header) && "Expected canonical MDString");
99 Metadata *PreOps[] = {Header};
100 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
101 Context, Storage, Hash, Tag, PreOps, DwarfOps),
102 Storage, Context.pImpl->GenericDebugNodes);
105 void GenericDebugNode::recalculateHash() {
106 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
109 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
110 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
111 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
113 if (Storage == Uniqued) { \
114 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
115 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
120 assert(ShouldCreate && \
121 "Expected non-uniqued nodes to always be created"); \
124 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
125 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
126 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
127 Storage, Context.pImpl->CLASS##s)
128 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
129 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
130 Storage, Context.pImpl->CLASS##s)
131 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
132 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
133 CLASS(Context, Storage, OPS), \
134 Storage, Context.pImpl->CLASS##s)
136 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
137 StorageType Storage, bool ShouldCreate) {
138 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
139 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
142 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
143 MDString *Name, StorageType Storage,
145 assert(isCanonical(Name) && "Expected canonical MDString");
146 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
147 Metadata *Ops[] = {Name};
148 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
151 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
152 MDString *Name, uint64_t SizeInBits,
153 uint64_t AlignInBits, unsigned Encoding,
154 StorageType Storage, bool ShouldCreate) {
155 assert(isCanonical(Name) && "Expected canonical MDString");
156 DEFINE_GETIMPL_LOOKUP(
157 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
158 Metadata *Ops[] = {nullptr, nullptr, Name};
159 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
163 MDDerivedType *MDDerivedType::getImpl(
164 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
165 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
166 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
167 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
168 assert(isCanonical(Name) && "Expected canonical MDString");
169 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
170 BaseType, SizeInBits, AlignInBits,
171 OffsetInBits, Flags, ExtraData));
172 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
173 DEFINE_GETIMPL_STORE(
174 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
178 MDCompositeType *MDCompositeType::getImpl(
179 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
180 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
181 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
182 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
183 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
185 assert(isCanonical(Name) && "Expected canonical MDString");
186 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
187 (Tag, getString(Name), File, Line, Scope, BaseType,
188 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
189 RuntimeLang, VTableHolder, TemplateParams,
190 getString(Identifier)));
191 Metadata *Ops[] = {File, Scope, Name, BaseType,
192 Elements, VTableHolder, TemplateParams, Identifier};
193 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
194 AlignInBits, OffsetInBits, Flags),
198 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
199 unsigned Flags, Metadata *TypeArray,
202 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
203 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
204 TypeArray, nullptr, nullptr, nullptr};
205 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
208 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
209 MDString *Directory, StorageType Storage,
211 assert(isCanonical(Filename) && "Expected canonical MDString");
212 assert(isCanonical(Directory) && "Expected canonical MDString");
213 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
214 Metadata *Ops[] = {Filename, Directory};
215 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops);
218 MDCompileUnit *MDCompileUnit::getImpl(
219 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
220 MDString *Producer, bool IsOptimized, MDString *Flags,
221 unsigned RuntimeVersion, MDString *SplitDebugFilename,
222 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
223 Metadata *Subprograms, Metadata *GlobalVariables,
224 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
225 assert(isCanonical(Producer) && "Expected canonical MDString");
226 assert(isCanonical(Flags) && "Expected canonical MDString");
227 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
228 DEFINE_GETIMPL_LOOKUP(
230 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
231 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
232 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
233 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
234 RetainedTypes, Subprograms, GlobalVariables,
236 DEFINE_GETIMPL_STORE(
238 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
241 MDSubprogram *MDLocalScope::getSubprogram() const {
242 if (auto *Block = dyn_cast<MDLexicalBlockBase>(this))
243 return Block->getScope()->getSubprogram();
244 return const_cast<MDSubprogram *>(cast<MDSubprogram>(this));
247 MDSubprogram *MDSubprogram::getImpl(
248 LLVMContext &Context, Metadata *Scope, MDString *Name,
249 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
250 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
251 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
252 unsigned Flags, bool IsOptimized, Metadata *Function,
253 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
254 StorageType Storage, bool ShouldCreate) {
255 assert(isCanonical(Name) && "Expected canonical MDString");
256 assert(isCanonical(LinkageName) && "Expected canonical MDString");
257 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
258 (Scope, getString(Name), getString(LinkageName), File,
259 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
260 ContainingType, Virtuality, VirtualIndex, Flags,
261 IsOptimized, Function, TemplateParams, Declaration,
263 Metadata *Ops[] = {File, Scope, Name, Name,
264 LinkageName, Type, ContainingType, Function,
265 TemplateParams, Declaration, Variables};
266 DEFINE_GETIMPL_STORE(MDSubprogram,
267 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
268 IsLocalToUnit, IsDefinition, IsOptimized),
272 void MDSubprogram::replaceFunction(Function *F) {
273 replaceFunction(F ? ConstantAsMetadata::get(F)
274 : static_cast<ConstantAsMetadata *>(nullptr));
277 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
278 Metadata *File, unsigned Line,
279 unsigned Column, StorageType Storage,
281 assert(Scope && "Expected scope");
282 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
283 Metadata *Ops[] = {File, Scope};
284 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
287 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
288 Metadata *Scope, Metadata *File,
289 unsigned Discriminator,
292 assert(Scope && "Expected scope");
293 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
294 Metadata *Ops[] = {File, Scope};
295 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
298 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
299 Metadata *File, MDString *Name, unsigned Line,
300 StorageType Storage, bool ShouldCreate) {
301 assert(isCanonical(Name) && "Expected canonical MDString");
302 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
303 Metadata *Ops[] = {File, Scope, Name};
304 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
307 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
312 assert(isCanonical(Name) && "Expected canonical MDString");
313 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
314 Metadata *Ops[] = {Name, Type};
315 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
318 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
319 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
320 Metadata *Value, StorageType Storage, bool ShouldCreate) {
321 assert(isCanonical(Name) && "Expected canonical MDString");
322 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
323 (Tag, getString(Name), Type, Value));
324 Metadata *Ops[] = {Name, Type, Value};
325 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
329 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
330 MDString *LinkageName, Metadata *File, unsigned Line,
331 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
333 Metadata *StaticDataMemberDeclaration,
334 StorageType Storage, bool ShouldCreate) {
335 assert(isCanonical(Name) && "Expected canonical MDString");
336 assert(isCanonical(LinkageName) && "Expected canonical MDString");
337 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
338 (Scope, getString(Name), getString(LinkageName), File,
339 Line, Type, IsLocalToUnit, IsDefinition, Variable,
340 StaticDataMemberDeclaration));
341 Metadata *Ops[] = {Scope, Name, File, Type,
342 Name, LinkageName, Variable, StaticDataMemberDeclaration};
343 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
347 MDLocalVariable *MDLocalVariable::getImpl(
348 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
349 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
350 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
351 // Truncate Arg to 8 bits.
353 // FIXME: This is gross (and should be changed to an assert or removed), but
354 // it matches historical behaviour for now.
355 Arg &= (1u << 8) - 1;
357 assert(Scope && "Expected scope");
358 assert(isCanonical(Name) && "Expected canonical MDString");
359 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
360 Line, Type, Arg, Flags, InlinedAt));
361 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
362 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
365 MDExpression *MDExpression::getImpl(LLVMContext &Context,
366 ArrayRef<uint64_t> Elements,
367 StorageType Storage, bool ShouldCreate) {
368 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
369 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
372 unsigned MDExpression::ExprOperand::getSize() const {
374 case dwarf::DW_OP_bit_piece:
376 case dwarf::DW_OP_plus:
383 bool MDExpression::isValid() const {
384 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
385 // Check that there's space for the operand.
386 if (I->get() + I->getSize() > E->get())
389 // Check that the operand is valid.
390 switch (I->getOp()) {
393 case dwarf::DW_OP_bit_piece:
394 // Piece expressions must be at the end.
395 return I->get() + I->getSize() == E->get();
396 case dwarf::DW_OP_plus:
397 case dwarf::DW_OP_deref:
404 MDObjCProperty *MDObjCProperty::getImpl(
405 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
406 MDString *GetterName, MDString *SetterName, unsigned Attributes,
407 Metadata *Type, StorageType Storage, bool ShouldCreate) {
408 assert(isCanonical(Name) && "Expected canonical MDString");
409 assert(isCanonical(GetterName) && "Expected canonical MDString");
410 assert(isCanonical(SetterName) && "Expected canonical MDString");
411 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
412 (getString(Name), File, Line, getString(GetterName),
413 getString(SetterName), Attributes, Type));
414 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
415 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
418 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
419 Metadata *Scope, Metadata *Entity,
420 unsigned Line, MDString *Name,
423 assert(isCanonical(Name) && "Expected canonical MDString");
424 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
425 (Tag, Scope, Entity, Line, getString(Name)));
426 Metadata *Ops[] = {Scope, Entity, Name};
427 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);