blob: 3d6ebbce1b4efa5148e300956f34979e0092151a [file] [log] [blame]
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +00001//===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the debug info Metadata classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/DebugInfoMetadata.h"
15#include "LLVMContextImpl.h"
16#include "MetadataImpl.h"
17
18using namespace llvm;
19
20MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
21 unsigned Column, ArrayRef<Metadata *> MDs)
22 : MDNode(C, MDLocationKind, Storage, MDs) {
23 assert((MDs.size() == 1 || MDs.size() == 2) &&
24 "Expected a scope and optional inlined-at");
25
26 // Set line and column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000027 assert(Column < (1u << 16) && "Expected 16-bit column");
28
29 SubclassData32 = Line;
30 SubclassData16 = Column;
31}
32
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000033static void adjustColumn(unsigned &Column) {
34 // Set to unknown on overflow. We only have 16 bits to play with here.
35 if (Column >= (1u << 16))
36 Column = 0;
37}
38
39MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
40 unsigned Column, Metadata *Scope,
41 Metadata *InlinedAt, StorageType Storage,
42 bool ShouldCreate) {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +000043 // Fixup column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000044 adjustColumn(Column);
45
46 if (Storage == Uniqued) {
47 if (auto *N =
48 getUniqued(Context.pImpl->MDLocations,
49 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
50 return N;
51 if (!ShouldCreate)
52 return nullptr;
53 } else {
54 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
55 }
56
57 SmallVector<Metadata *, 2> Ops;
58 Ops.push_back(Scope);
59 if (InlinedAt)
60 Ops.push_back(InlinedAt);
61 return storeImpl(new (Ops.size())
62 MDLocation(Context, Storage, Line, Column, Ops),
63 Storage, Context.pImpl->MDLocations);
64}
65
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000066static StringRef getString(const MDString *S) {
67 if (S)
68 return S->getString();
69 return StringRef();
70}
71
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +000072#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000073static bool isCanonical(const MDString *S) {
74 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +000075}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +000076#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +000077
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000078GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000079 MDString *Header,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000080 ArrayRef<Metadata *> DwarfOps,
81 StorageType Storage,
82 bool ShouldCreate) {
83 unsigned Hash = 0;
84 if (Storage == Uniqued) {
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000085 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000086 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
87 return N;
88 if (!ShouldCreate)
89 return nullptr;
90 Hash = Key.getHash();
91 } else {
92 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
93 }
94
95 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000096 assert(isCanonical(Header) && "Expected canonical MDString");
97 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000098 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
99 Context, Storage, Hash, Tag, PreOps, DwarfOps),
100 Storage, Context.pImpl->GenericDebugNodes);
101}
102
103void GenericDebugNode::recalculateHash() {
104 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
105}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000106
107#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
108#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
109#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
110 do { \
111 if (Storage == Uniqued) { \
112 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
113 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
114 return N; \
115 if (!ShouldCreate) \
116 return nullptr; \
117 } else { \
118 assert(ShouldCreate && \
119 "Expected non-uniqued nodes to always be created"); \
120 } \
121 } while (false)
122#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
123 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
124 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
125 Storage, Context.pImpl->CLASS##s)
126#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
127 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
128 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000129#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
130 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
131 CLASS(Context, Storage, OPS), \
132 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000133
134MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
135 StorageType Storage, bool ShouldCreate) {
136 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
137 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
138}
139
140MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
141 MDString *Name, StorageType Storage,
142 bool ShouldCreate) {
143 assert(isCanonical(Name) && "Expected canonical MDString");
144 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
145 Metadata *Ops[] = {Name};
146 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
147}
148
149MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
150 MDString *Name, unsigned SizeInBits,
151 unsigned AlignInBits, unsigned Encoding,
152 StorageType Storage, bool ShouldCreate) {
153 assert(isCanonical(Name) && "Expected canonical MDString");
154 DEFINE_GETIMPL_LOOKUP(
155 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
156 Metadata *Ops[] = {nullptr, nullptr, Name};
157 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
158 Ops);
159}
160
161MDDerivedType *MDDerivedType::getImpl(
162 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
163 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits,
164 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags,
165 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
166 assert(isCanonical(Name) && "Expected canonical MDString");
167 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
168 BaseType, SizeInBits, AlignInBits,
169 OffsetInBits, Flags, ExtraData));
170 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
171 DEFINE_GETIMPL_STORE(
172 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
173 Ops);
174}
175
176MDCompositeType *MDCompositeType::getImpl(
177 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
178 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits,
179 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags,
180 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
181 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
182 bool ShouldCreate) {
183 assert(isCanonical(Name) && "Expected canonical MDString");
184 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
185 (Tag, getString(Name), File, Line, Scope, BaseType,
186 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
187 RuntimeLang, VTableHolder, TemplateParams,
188 getString(Identifier)));
189 Metadata *Ops[] = {File, Scope, Name, BaseType,
190 Elements, VTableHolder, TemplateParams, Identifier};
191 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
192 AlignInBits, OffsetInBits, Flags),
193 Ops);
194}
195
196MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
197 unsigned Flags, Metadata *TypeArray,
198 StorageType Storage,
199 bool ShouldCreate) {
200 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
201 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
202 TypeArray, nullptr, nullptr};
203 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
204}
205
206MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
207 MDString *Directory, StorageType Storage,
208 bool ShouldCreate) {
209 assert(isCanonical(Filename) && "Expected canonical MDString");
210 assert(isCanonical(Directory) && "Expected canonical MDString");
211 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
212 Metadata *NodeOps[] = {Filename, Directory};
213 Metadata *Ops[] = {MDTuple::get(Context, NodeOps)};
214 return storeImpl(new (ArrayRef<Metadata *>(Ops).size())
215 MDFile(Context, Storage, Ops),
216 Storage, Context.pImpl->MDFiles);
217}
218
219MDCompileUnit *MDCompileUnit::getImpl(
220 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
221 MDString *Producer, bool IsOptimized, MDString *Flags,
222 unsigned RuntimeVersion, MDString *SplitDebugFilename,
223 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
224 Metadata *Subprograms, Metadata *GlobalVariables,
225 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
226 assert(isCanonical(Producer) && "Expected canonical MDString");
227 assert(isCanonical(Flags) && "Expected canonical MDString");
228 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
229 DEFINE_GETIMPL_LOOKUP(
230 MDCompileUnit,
231 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
232 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
233 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
234 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
235 RetainedTypes, Subprograms, GlobalVariables,
236 ImportedEntities};
237 DEFINE_GETIMPL_STORE(
238 MDCompileUnit,
239 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
240}
241
242MDSubprogram *MDSubprogram::getImpl(
243 LLVMContext &Context, Metadata *Scope, MDString *Name,
244 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
245 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
246 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
247 unsigned Flags, bool IsOptimized, Metadata *Function,
248 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
249 StorageType Storage, bool ShouldCreate) {
250 assert(isCanonical(Name) && "Expected canonical MDString");
251 assert(isCanonical(LinkageName) && "Expected canonical MDString");
252 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
253 (Scope, getString(Name), getString(LinkageName), File,
254 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
255 ContainingType, Virtuality, VirtualIndex, Flags,
256 IsOptimized, Function, TemplateParams, Declaration,
257 Variables));
258 Metadata *Ops[] = {File, Scope, Name, Name,
259 LinkageName, Type, ContainingType, Function,
260 TemplateParams, Declaration, Variables};
261 DEFINE_GETIMPL_STORE(MDSubprogram,
262 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
263 IsLocalToUnit, IsDefinition, IsOptimized),
264 Ops);
265}
266
267MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
268 Metadata *File, unsigned Line,
269 unsigned Column, StorageType Storage,
270 bool ShouldCreate) {
271 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
272 Metadata *Ops[] = {File, Scope};
273 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
274}
275
276MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
277 Metadata *Scope, Metadata *File,
278 unsigned Discriminator,
279 StorageType Storage,
280 bool ShouldCreate) {
281 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
282 Metadata *Ops[] = {File, Scope};
283 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
284}
285
286MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
287 Metadata *File, MDString *Name, unsigned Line,
288 StorageType Storage, bool ShouldCreate) {
289 assert(isCanonical(Name) && "Expected canonical MDString");
290 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
291 Metadata *Ops[] = {File, Scope, Name};
292 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
293}
294
295MDTemplateTypeParameter *
296MDTemplateTypeParameter::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000297 MDString *Name, Metadata *Type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000298 StorageType Storage, bool ShouldCreate) {
299 assert(isCanonical(Name) && "Expected canonical MDString");
300 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000301 (Scope, getString(Name), Type));
302 Metadata *Ops[] = {Scope, Name, Type};
303 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000304}
305
306MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
307 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000308 Metadata *Type, Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000309 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000310 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
311 (Tag, Scope, getString(Name), Type, Value));
312 Metadata *Ops[] = {Scope, Name, Type, Value};
313 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000314}
315
316MDGlobalVariable *
317MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
318 MDString *LinkageName, Metadata *File, unsigned Line,
319 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
320 Metadata *Variable,
321 Metadata *StaticDataMemberDeclaration,
322 StorageType Storage, bool ShouldCreate) {
323 assert(isCanonical(Name) && "Expected canonical MDString");
324 assert(isCanonical(LinkageName) && "Expected canonical MDString");
325 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
326 (Scope, getString(Name), getString(LinkageName), File,
327 Line, Type, IsLocalToUnit, IsDefinition, Variable,
328 StaticDataMemberDeclaration));
329 Metadata *Ops[] = {Scope, Name, File, Type,
330 Name, LinkageName, Variable, StaticDataMemberDeclaration};
331 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
332 Ops);
333}
334
335MDLocalVariable *MDLocalVariable::getImpl(
336 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
337 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
338 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000339 // Truncate Arg to 8 bits.
340 //
341 // FIXME: This is gross (and should be changed to an assert or removed), but
342 // it matches historical behaviour for now.
343 Arg &= (1u << 8) - 1;
344
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000345 assert(isCanonical(Name) && "Expected canonical MDString");
346 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
347 Line, Type, Arg, Flags, InlinedAt));
348 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
349 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
350}
351
352MDExpression *MDExpression::getImpl(LLVMContext &Context,
353 ArrayRef<uint64_t> Elements,
354 StorageType Storage, bool ShouldCreate) {
355 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
356 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
357}
358
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000359unsigned MDExpression::ExprOperand::getSize() const {
360 switch (getOp()) {
361 case dwarf::DW_OP_bit_piece:
362 return 3;
363 case dwarf::DW_OP_plus:
364 return 2;
365 default:
366 return 1;
367 }
368}
369
370bool MDExpression::isValid() const {
371 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
372 // Check that there's space for the operand.
373 if (I->get() + I->getSize() > E->get())
374 return false;
375
376 // Check that the operand is valid.
377 switch (I->getOp()) {
378 default:
379 return false;
380 case dwarf::DW_OP_bit_piece:
381 // Piece expressions must be at the end.
382 return I->get() + I->getSize() == E->get();
383 case dwarf::DW_OP_plus:
384 case dwarf::DW_OP_deref:
385 break;
386 }
387 }
388 return true;
389}
390
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000391MDObjCProperty *MDObjCProperty::getImpl(
392 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
393 MDString *GetterName, MDString *SetterName, unsigned Attributes,
394 Metadata *Type, StorageType Storage, bool ShouldCreate) {
395 assert(isCanonical(Name) && "Expected canonical MDString");
396 assert(isCanonical(GetterName) && "Expected canonical MDString");
397 assert(isCanonical(SetterName) && "Expected canonical MDString");
398 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
399 (getString(Name), File, Line, getString(GetterName),
400 getString(SetterName), Attributes, Type));
401 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
402 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
403}
404
405MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
406 Metadata *Scope, Metadata *Entity,
407 unsigned Line, MDString *Name,
408 StorageType Storage,
409 bool ShouldCreate) {
410 assert(isCanonical(Name) && "Expected canonical MDString");
411 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
412 (Tag, Scope, Entity, Line, getString(Name)));
413 Metadata *Ops[] = {Scope, Entity, Name};
414 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);
415}