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 if (Storage == Uniqued) {
49 getUniqued(Context.pImpl->MDLocations,
50 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
55 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
58 SmallVector<Metadata *, 2> Ops;
61 Ops.push_back(InlinedAt);
62 return storeImpl(new (Ops.size())
63 MDLocation(Context, Storage, Line, Column, Ops),
64 Storage, Context.pImpl->MDLocations);
67 static StringRef getString(const MDString *S) {
69 return S->getString();
74 static bool isCanonical(const MDString *S) {
75 return !S || !S->getString().empty();
79 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
81 ArrayRef<Metadata *> DwarfOps,
85 if (Storage == Uniqued) {
86 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
87 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
93 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
96 // Use a nullptr for empty headers.
97 assert(isCanonical(Header) && "Expected canonical MDString");
98 Metadata *PreOps[] = {Header};
99 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
100 Context, Storage, Hash, Tag, PreOps, DwarfOps),
101 Storage, Context.pImpl->GenericDebugNodes);
104 void GenericDebugNode::recalculateHash() {
105 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
108 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
109 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
110 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
112 if (Storage == Uniqued) { \
113 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
114 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
119 assert(ShouldCreate && \
120 "Expected non-uniqued nodes to always be created"); \
123 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
124 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
125 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
126 Storage, Context.pImpl->CLASS##s)
127 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
128 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
129 Storage, Context.pImpl->CLASS##s)
130 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
131 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
132 CLASS(Context, Storage, OPS), \
133 Storage, Context.pImpl->CLASS##s)
135 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
136 StorageType Storage, bool ShouldCreate) {
137 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
138 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
141 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
142 MDString *Name, StorageType Storage,
144 assert(isCanonical(Name) && "Expected canonical MDString");
145 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
146 Metadata *Ops[] = {Name};
147 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
150 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
151 MDString *Name, uint64_t SizeInBits,
152 uint64_t AlignInBits, unsigned Encoding,
153 StorageType Storage, bool ShouldCreate) {
154 assert(isCanonical(Name) && "Expected canonical MDString");
155 DEFINE_GETIMPL_LOOKUP(
156 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
157 Metadata *Ops[] = {nullptr, nullptr, Name};
158 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
162 MDDerivedType *MDDerivedType::getImpl(
163 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
164 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
165 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
166 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
167 assert(isCanonical(Name) && "Expected canonical MDString");
168 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
169 BaseType, SizeInBits, AlignInBits,
170 OffsetInBits, Flags, ExtraData));
171 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
172 DEFINE_GETIMPL_STORE(
173 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
177 MDCompositeType *MDCompositeType::getImpl(
178 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
179 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
180 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
181 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
182 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
184 assert(isCanonical(Name) && "Expected canonical MDString");
185 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
186 (Tag, getString(Name), File, Line, Scope, BaseType,
187 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
188 RuntimeLang, VTableHolder, TemplateParams,
189 getString(Identifier)));
190 Metadata *Ops[] = {File, Scope, Name, BaseType,
191 Elements, VTableHolder, TemplateParams, Identifier};
192 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
193 AlignInBits, OffsetInBits, Flags),
197 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
198 unsigned Flags, Metadata *TypeArray,
201 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
202 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
203 TypeArray, nullptr, nullptr, nullptr};
204 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
207 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
208 MDString *Directory, StorageType Storage,
210 assert(isCanonical(Filename) && "Expected canonical MDString");
211 assert(isCanonical(Directory) && "Expected canonical MDString");
212 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
213 Metadata *Ops[] = {Filename, Directory};
214 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops);
217 MDCompileUnit *MDCompileUnit::getImpl(
218 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
219 MDString *Producer, bool IsOptimized, MDString *Flags,
220 unsigned RuntimeVersion, MDString *SplitDebugFilename,
221 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
222 Metadata *Subprograms, Metadata *GlobalVariables,
223 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
224 assert(isCanonical(Producer) && "Expected canonical MDString");
225 assert(isCanonical(Flags) && "Expected canonical MDString");
226 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
227 DEFINE_GETIMPL_LOOKUP(
229 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
230 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
231 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
232 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
233 RetainedTypes, Subprograms, GlobalVariables,
235 DEFINE_GETIMPL_STORE(
237 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
240 MDSubprogram *MDSubprogram::getImpl(
241 LLVMContext &Context, Metadata *Scope, MDString *Name,
242 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
243 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
244 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
245 unsigned Flags, bool IsOptimized, Metadata *Function,
246 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
247 StorageType Storage, bool ShouldCreate) {
248 assert(isCanonical(Name) && "Expected canonical MDString");
249 assert(isCanonical(LinkageName) && "Expected canonical MDString");
250 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
251 (Scope, getString(Name), getString(LinkageName), File,
252 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
253 ContainingType, Virtuality, VirtualIndex, Flags,
254 IsOptimized, Function, TemplateParams, Declaration,
256 Metadata *Ops[] = {File, Scope, Name, Name,
257 LinkageName, Type, ContainingType, Function,
258 TemplateParams, Declaration, Variables};
259 DEFINE_GETIMPL_STORE(MDSubprogram,
260 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
261 IsLocalToUnit, IsDefinition, IsOptimized),
265 void MDSubprogram::replaceFunction(Function *F) {
266 replaceFunction(F ? ConstantAsMetadata::get(F)
267 : static_cast<ConstantAsMetadata *>(nullptr));
270 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
271 Metadata *File, unsigned Line,
272 unsigned Column, StorageType Storage,
274 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
275 Metadata *Ops[] = {File, Scope};
276 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
279 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
280 Metadata *Scope, Metadata *File,
281 unsigned Discriminator,
284 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
285 Metadata *Ops[] = {File, Scope};
286 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
289 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
290 Metadata *File, MDString *Name, unsigned Line,
291 StorageType Storage, bool ShouldCreate) {
292 assert(isCanonical(Name) && "Expected canonical MDString");
293 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
294 Metadata *Ops[] = {File, Scope, Name};
295 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
298 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
303 assert(isCanonical(Name) && "Expected canonical MDString");
304 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
305 Metadata *Ops[] = {Name, Type};
306 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
309 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
310 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
311 Metadata *Value, StorageType Storage, bool ShouldCreate) {
312 assert(isCanonical(Name) && "Expected canonical MDString");
313 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
314 (Tag, getString(Name), Type, Value));
315 Metadata *Ops[] = {Name, Type, Value};
316 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
320 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
321 MDString *LinkageName, Metadata *File, unsigned Line,
322 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
324 Metadata *StaticDataMemberDeclaration,
325 StorageType Storage, bool ShouldCreate) {
326 assert(isCanonical(Name) && "Expected canonical MDString");
327 assert(isCanonical(LinkageName) && "Expected canonical MDString");
328 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
329 (Scope, getString(Name), getString(LinkageName), File,
330 Line, Type, IsLocalToUnit, IsDefinition, Variable,
331 StaticDataMemberDeclaration));
332 Metadata *Ops[] = {Scope, Name, File, Type,
333 Name, LinkageName, Variable, StaticDataMemberDeclaration};
334 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
338 MDLocalVariable *MDLocalVariable::getImpl(
339 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
340 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
341 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
342 // Truncate Arg to 8 bits.
344 // FIXME: This is gross (and should be changed to an assert or removed), but
345 // it matches historical behaviour for now.
346 Arg &= (1u << 8) - 1;
348 assert(isCanonical(Name) && "Expected canonical MDString");
349 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
350 Line, Type, Arg, Flags, InlinedAt));
351 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
352 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
355 MDExpression *MDExpression::getImpl(LLVMContext &Context,
356 ArrayRef<uint64_t> Elements,
357 StorageType Storage, bool ShouldCreate) {
358 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
359 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
362 unsigned MDExpression::ExprOperand::getSize() const {
364 case dwarf::DW_OP_bit_piece:
366 case dwarf::DW_OP_plus:
373 bool MDExpression::isValid() const {
374 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
375 // Check that there's space for the operand.
376 if (I->get() + I->getSize() > E->get())
379 // Check that the operand is valid.
380 switch (I->getOp()) {
383 case dwarf::DW_OP_bit_piece:
384 // Piece expressions must be at the end.
385 return I->get() + I->getSize() == E->get();
386 case dwarf::DW_OP_plus:
387 case dwarf::DW_OP_deref:
394 MDObjCProperty *MDObjCProperty::getImpl(
395 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
396 MDString *GetterName, MDString *SetterName, unsigned Attributes,
397 Metadata *Type, StorageType Storage, bool ShouldCreate) {
398 assert(isCanonical(Name) && "Expected canonical MDString");
399 assert(isCanonical(GetterName) && "Expected canonical MDString");
400 assert(isCanonical(SetterName) && "Expected canonical MDString");
401 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
402 (getString(Name), File, Line, getString(GetterName),
403 getString(SetterName), Attributes, Type));
404 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
405 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
408 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
409 Metadata *Scope, Metadata *Entity,
410 unsigned Line, MDString *Name,
413 assert(isCanonical(Name) && "Expected canonical MDString");
414 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
415 (Tag, Scope, Entity, Line, getString(Name)));
416 Metadata *Ops[] = {Scope, Entity, Name};
417 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);