blob: c2ecd4e5fc818d14bebceb233496c3f4b77364e1 [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"
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000017#include "llvm/ADT/StringSwitch.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000018#include "llvm/IR/Function.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000019
20using namespace llvm;
21
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000022DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000023 unsigned Column, ArrayRef<Metadata *> MDs)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000024 : MDNode(C, DILocationKind, Storage, MDs) {
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000025 assert((MDs.size() == 1 || MDs.size() == 2) &&
26 "Expected a scope and optional inlined-at");
27
28 // Set line and column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000029 assert(Column < (1u << 16) && "Expected 16-bit column");
30
31 SubclassData32 = Line;
32 SubclassData16 = Column;
33}
34
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000035static void adjustColumn(unsigned &Column) {
36 // Set to unknown on overflow. We only have 16 bits to play with here.
37 if (Column >= (1u << 16))
38 Column = 0;
39}
40
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000041DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000042 unsigned Column, Metadata *Scope,
43 Metadata *InlinedAt, StorageType Storage,
44 bool ShouldCreate) {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +000045 // Fixup column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000046 adjustColumn(Column);
47
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +000048 assert(Scope && "Expected scope");
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000049 if (Storage == Uniqued) {
50 if (auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000051 getUniqued(Context.pImpl->DILocations,
52 DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000053 return N;
54 if (!ShouldCreate)
55 return nullptr;
56 } else {
57 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
58 }
59
60 SmallVector<Metadata *, 2> Ops;
61 Ops.push_back(Scope);
62 if (InlinedAt)
63 Ops.push_back(InlinedAt);
64 return storeImpl(new (Ops.size())
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000065 DILocation(Context, Storage, Line, Column, Ops),
66 Storage, Context.pImpl->DILocations);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000067}
68
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000069unsigned DINode::getFlag(StringRef Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000070 return StringSwitch<unsigned>(Flag)
71#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
72#include "llvm/IR/DebugInfoFlags.def"
73 .Default(0);
74}
75
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000076const char *DINode::getFlagString(unsigned Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000077 switch (Flag) {
78 default:
79 return "";
80#define HANDLE_DI_FLAG(ID, NAME) \
81 case Flag##NAME: \
82 return "DIFlag" #NAME;
83#include "llvm/IR/DebugInfoFlags.def"
84 }
85}
86
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000087unsigned DINode::splitFlags(unsigned Flags,
88 SmallVectorImpl<unsigned> &SplitFlags) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000089 // Accessibility flags need to be specially handled, since they're packed
90 // together.
91 if (unsigned A = Flags & FlagAccessibility) {
92 if (A == FlagPrivate)
93 SplitFlags.push_back(FlagPrivate);
94 else if (A == FlagProtected)
95 SplitFlags.push_back(FlagProtected);
96 else
97 SplitFlags.push_back(FlagPublic);
98 Flags &= ~A;
99 }
100
101#define HANDLE_DI_FLAG(ID, NAME) \
102 if (unsigned Bit = Flags & ID) { \
103 SplitFlags.push_back(Bit); \
104 Flags &= ~Bit; \
105 }
106#include "llvm/IR/DebugInfoFlags.def"
107
108 return Flags;
109}
110
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000111DIScopeRef DIScope::getScope() const {
112 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000113 return T->getScope();
114
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000115 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000116 return SP->getScope();
117
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000118 if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
119 return DIScopeRef(LB->getScope());
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000120
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000121 if (auto *NS = dyn_cast<DINamespace>(this))
122 return DIScopeRef(NS->getScope());
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000123
Adrian Prantlab1243f2015-06-29 23:03:47 +0000124 if (auto *M = dyn_cast<DIModule>(this))
125 return DIScopeRef(M->getScope());
126
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000127 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000128 "Unhandled type of scope.");
129 return nullptr;
130}
131
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000132StringRef DIScope::getName() const {
133 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000134 return T->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000135 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000136 return SP->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000137 if (auto *NS = dyn_cast<DINamespace>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000138 return NS->getName();
Adrian Prantlab1243f2015-06-29 23:03:47 +0000139 if (auto *M = dyn_cast<DIModule>(this))
140 return M->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000141 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
142 isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000143 "Unhandled type of scope.");
144 return "";
145}
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000146
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000147#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000148static bool isCanonical(const MDString *S) {
149 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000150}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000151#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000152
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000153GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
154 MDString *Header,
155 ArrayRef<Metadata *> DwarfOps,
156 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000157 unsigned Hash = 0;
158 if (Storage == Uniqued) {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000159 GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000160 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000161 return N;
162 if (!ShouldCreate)
163 return nullptr;
164 Hash = Key.getHash();
165 } else {
166 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
167 }
168
169 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000170 assert(isCanonical(Header) && "Expected canonical MDString");
171 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000172 return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000173 Context, Storage, Hash, Tag, PreOps, DwarfOps),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000174 Storage, Context.pImpl->GenericDINodes);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000175}
176
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000177void GenericDINode::recalculateHash() {
178 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000179}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000180
181#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
182#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
183#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
184 do { \
185 if (Storage == Uniqued) { \
186 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
187 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
188 return N; \
189 if (!ShouldCreate) \
190 return nullptr; \
191 } else { \
192 assert(ShouldCreate && \
193 "Expected non-uniqued nodes to always be created"); \
194 } \
195 } while (false)
196#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
David Blaikie6662d6a2016-04-13 17:42:56 +0000197 return storeImpl(new (array_lengthof(OPS)) \
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000198 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
199 Storage, Context.pImpl->CLASS##s)
200#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
201 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
202 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000203#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
David Blaikie6662d6a2016-04-13 17:42:56 +0000204 return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS), \
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000205 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000206
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000207DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000208 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000209 DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
210 DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000211}
212
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000213DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000214 MDString *Name, StorageType Storage,
215 bool ShouldCreate) {
216 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000217 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000218 Metadata *Ops[] = {Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000219 DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000220}
221
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000222DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000223 MDString *Name, uint64_t SizeInBits,
224 uint64_t AlignInBits, unsigned Encoding,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000225 StorageType Storage, bool ShouldCreate) {
226 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000227 DEFINE_GETIMPL_LOOKUP(DIBasicType,
228 (Tag, Name, SizeInBits, AlignInBits, Encoding));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000229 Metadata *Ops[] = {nullptr, nullptr, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000230 DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000231 Ops);
232}
233
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000234DIDerivedType *DIDerivedType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000235 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000236 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
237 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000238 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
239 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000240 DEFINE_GETIMPL_LOOKUP(DIDerivedType,
241 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
242 AlignInBits, OffsetInBits, Flags, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000243 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
244 DEFINE_GETIMPL_STORE(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000245 DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000246 Ops);
247}
248
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000249DICompositeType *DICompositeType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000250 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000251 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
252 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000253 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
254 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
255 bool ShouldCreate) {
256 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000257
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000258 // Keep this in sync with buildODRType.
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000259 DEFINE_GETIMPL_LOOKUP(
260 DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
261 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
262 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000263 Metadata *Ops[] = {File, Scope, Name, BaseType,
264 Elements, VTableHolder, TemplateParams, Identifier};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000265 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000266 AlignInBits, OffsetInBits, Flags),
267 Ops);
268}
269
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000270DICompositeType *DICompositeType::buildODRType(
271 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
272 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
273 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
274 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
275 Metadata *VTableHolder, Metadata *TemplateParams) {
276 assert(!Identifier.getString().empty() && "Expected valid identifier");
277 if (!Context.isODRUniquingDebugTypes())
278 return nullptr;
279 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
280 if (!CT)
281 return CT = DICompositeType::getDistinct(
282 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
283 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
284 VTableHolder, TemplateParams, &Identifier);
285
286 // Only mutate CT if it's a forward declaration and the new operands aren't.
287 assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
288 if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl))
289 return CT;
290
291 // Mutate CT in place. Keep this in sync with getImpl.
292 CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
293 Flags);
294 Metadata *Ops[] = {File, Scope, Name, BaseType,
295 Elements, VTableHolder, TemplateParams, &Identifier};
296 assert(std::end(Ops) - std::begin(Ops) == CT->getNumOperands() &&
297 "Mismatched number of operands");
298 for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I)
299 if (Ops[I] != CT->getOperand(I))
300 CT->setOperand(I, Ops[I]);
301 return CT;
302}
303
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +0000304DICompositeType *DICompositeType::getODRType(
305 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
306 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
307 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
308 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
309 Metadata *VTableHolder, Metadata *TemplateParams) {
310 assert(!Identifier.getString().empty() && "Expected valid identifier");
311 if (!Context.isODRUniquingDebugTypes())
312 return nullptr;
313 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
314 if (!CT)
315 CT = DICompositeType::getDistinct(
316 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
317 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
318 TemplateParams, &Identifier);
319 return CT;
320}
321
322DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context,
323 MDString &Identifier) {
324 assert(!Identifier.getString().empty() && "Expected valid identifier");
325 if (!Context.isODRUniquingDebugTypes())
326 return nullptr;
327 return Context.pImpl->DITypeMap->lookup(&Identifier);
328}
329
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000330DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000331 unsigned Flags, Metadata *TypeArray,
332 StorageType Storage,
333 bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000334 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, TypeArray));
Duncan P. N. Exon Smithb9e045a2015-07-24 20:56:36 +0000335 Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000336 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000337}
338
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000339DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000340 MDString *Directory, StorageType Storage,
341 bool ShouldCreate) {
342 assert(isCanonical(Filename) && "Expected canonical MDString");
343 assert(isCanonical(Directory) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000344 DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory));
Duncan P. N. Exon Smitha5c57cc2015-02-20 20:35:17 +0000345 Metadata *Ops[] = {Filename, Directory};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000346 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIFile, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000347}
348
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000349DICompileUnit *DICompileUnit::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000350 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
351 MDString *Producer, bool IsOptimized, MDString *Flags,
352 unsigned RuntimeVersion, MDString *SplitDebugFilename,
353 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000354 Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
355 uint64_t DWOId, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000356 assert(Storage != Uniqued && "Cannot unique DICompileUnit");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000357 assert(isCanonical(Producer) && "Expected canonical MDString");
358 assert(isCanonical(Flags) && "Expected canonical MDString");
359 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000360
Adrian Prantl75819ae2016-04-15 15:57:41 +0000361 Metadata *Ops[] = {
362 File, Producer, Flags, SplitDebugFilename,
363 EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities,
364 Macros};
David Blaikie6662d6a2016-04-13 17:42:56 +0000365 return storeImpl(new (array_lengthof(Ops)) DICompileUnit(
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000366 Context, Storage, SourceLanguage, IsOptimized,
367 RuntimeVersion, EmissionKind, DWOId, Ops),
368 Storage);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000369}
370
Adrian Prantlb939a252016-03-31 23:56:58 +0000371Optional<DICompileUnit::DebugEmissionKind>
372DICompileUnit::getEmissionKind(StringRef Str) {
373 return StringSwitch<Optional<DebugEmissionKind>>(Str)
374 .Case("NoDebug", NoDebug)
375 .Case("FullDebug", FullDebug)
376 .Case("LineTablesOnly", LineTablesOnly)
377 .Default(None);
378}
379
380const char *DICompileUnit::EmissionKindString(DebugEmissionKind EK) {
381 switch (EK) {
382 case NoDebug: return "NoDebug";
383 case FullDebug: return "FullDebug";
384 case LineTablesOnly: return "LineTablesOnly";
385 }
386 return nullptr;
387}
388
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389DISubprogram *DILocalScope::getSubprogram() const {
390 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000391 return Block->getScope()->getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000392 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000393}
394
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000395DISubprogram *DISubprogram::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000396 LLVMContext &Context, Metadata *Scope, MDString *Name,
397 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
398 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
399 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000400 unsigned Flags, bool IsOptimized, Metadata *Unit, Metadata *TemplateParams,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000401 Metadata *Declaration, Metadata *Variables, StorageType Storage,
402 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000403 assert(isCanonical(Name) && "Expected canonical MDString");
404 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000405 DEFINE_GETIMPL_LOOKUP(DISubprogram,
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000406 (Scope, Name, LinkageName, File, Line, Type,
407 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000408 Virtuality, VirtualIndex, Flags, IsOptimized, Unit,
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000409 TemplateParams, Declaration, Variables));
Adrian Prantl75819ae2016-04-15 15:57:41 +0000410 Metadata *Ops[] = {File, Scope, Name, Name,
411 LinkageName, Type, ContainingType, Unit,
412 TemplateParams, Declaration, Variables};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000413 DEFINE_GETIMPL_STORE(DISubprogram,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000414 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
415 IsLocalToUnit, IsDefinition, IsOptimized),
416 Ops);
417}
418
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000419bool DISubprogram::describes(const Function *F) const {
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000420 assert(F && "Invalid function");
Peter Collingbourned4bff302015-11-05 22:03:56 +0000421 if (F->getSubprogram() == this)
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000422 return true;
423 StringRef Name = getLinkageName();
424 if (Name.empty())
425 Name = getName();
426 return F->getName() == Name;
427}
428
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000429DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000430 Metadata *File, unsigned Line,
431 unsigned Column, StorageType Storage,
432 bool ShouldCreate) {
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +0000433 // Fixup column.
434 adjustColumn(Column);
435
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000436 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000437 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000438 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000439 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000440}
441
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000442DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000443 Metadata *Scope, Metadata *File,
444 unsigned Discriminator,
445 StorageType Storage,
446 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000447 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000448 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000449 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000450 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000451}
452
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000453DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000454 Metadata *File, MDString *Name, unsigned Line,
455 StorageType Storage, bool ShouldCreate) {
456 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000457 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000458 Metadata *Ops[] = {File, Scope, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000459 DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000460}
461
Adrian Prantlab1243f2015-06-29 23:03:47 +0000462DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
463 MDString *Name, MDString *ConfigurationMacros,
464 MDString *IncludePath, MDString *ISysRoot,
465 StorageType Storage, bool ShouldCreate) {
466 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000467 DEFINE_GETIMPL_LOOKUP(
468 DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
Adrian Prantlab1243f2015-06-29 23:03:47 +0000469 Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
470 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
471}
472
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000473DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000474 MDString *Name,
475 Metadata *Type,
476 StorageType Storage,
477 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000478 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000479 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000480 Metadata *Ops[] = {Name, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000481 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000482}
483
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000484DITemplateValueParameter *DITemplateValueParameter::getImpl(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000485 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
486 Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000487 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000488 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000489 Metadata *Ops[] = {Name, Type, Value};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000490 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000491}
492
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000493DIGlobalVariable *
494DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000495 MDString *LinkageName, Metadata *File, unsigned Line,
496 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
497 Metadata *Variable,
498 Metadata *StaticDataMemberDeclaration,
499 StorageType Storage, bool ShouldCreate) {
500 assert(isCanonical(Name) && "Expected canonical MDString");
501 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000502 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000503 (Scope, Name, LinkageName, File, Line, Type,
504 IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000505 StaticDataMemberDeclaration));
506 Metadata *Ops[] = {Scope, Name, File, Type,
507 Name, LinkageName, Variable, StaticDataMemberDeclaration};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000508 DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000509 Ops);
510}
511
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000512DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
513 MDString *Name, Metadata *File,
514 unsigned Line, Metadata *Type,
515 unsigned Arg, unsigned Flags,
516 StorageType Storage,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000517 bool ShouldCreate) {
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +0000518 // 64K ought to be enough for any frontend.
519 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000520
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +0000521 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000522 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000523 DEFINE_GETIMPL_LOOKUP(DILocalVariable,
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000524 (Scope, Name, File, Line, Type, Arg, Flags));
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000525 Metadata *Ops[] = {Scope, Name, File, Type};
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000526 DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000527}
528
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000529DIExpression *DIExpression::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000530 ArrayRef<uint64_t> Elements,
531 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000532 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
533 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000534}
535
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000536unsigned DIExpression::ExprOperand::getSize() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000537 switch (getOp()) {
538 case dwarf::DW_OP_bit_piece:
539 return 3;
540 case dwarf::DW_OP_plus:
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000541 case dwarf::DW_OP_minus:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000542 return 2;
543 default:
544 return 1;
545 }
546}
547
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000548bool DIExpression::isValid() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000549 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
550 // Check that there's space for the operand.
551 if (I->get() + I->getSize() > E->get())
552 return false;
553
554 // Check that the operand is valid.
555 switch (I->getOp()) {
556 default:
557 return false;
558 case dwarf::DW_OP_bit_piece:
559 // Piece expressions must be at the end.
560 return I->get() + I->getSize() == E->get();
561 case dwarf::DW_OP_plus:
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000562 case dwarf::DW_OP_minus:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000563 case dwarf::DW_OP_deref:
564 break;
565 }
566 }
567 return true;
568}
569
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000570bool DIExpression::isBitPiece() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000571 assert(isValid() && "Expected valid expression");
572 if (unsigned N = getNumElements())
573 if (N >= 3)
574 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
575 return false;
576}
577
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000578uint64_t DIExpression::getBitPieceOffset() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000579 assert(isBitPiece() && "Expected bit piece");
580 return getElement(getNumElements() - 2);
581}
582
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000583uint64_t DIExpression::getBitPieceSize() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000584 assert(isBitPiece() && "Expected bit piece");
585 return getElement(getNumElements() - 1);
586}
587
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000588DIObjCProperty *DIObjCProperty::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000589 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
590 MDString *GetterName, MDString *SetterName, unsigned Attributes,
591 Metadata *Type, StorageType Storage, bool ShouldCreate) {
592 assert(isCanonical(Name) && "Expected canonical MDString");
593 assert(isCanonical(GetterName) && "Expected canonical MDString");
594 assert(isCanonical(SetterName) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000595 DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
596 SetterName, Attributes, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000597 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000598 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000599}
600
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000601DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000602 Metadata *Scope, Metadata *Entity,
603 unsigned Line, MDString *Name,
604 StorageType Storage,
605 bool ShouldCreate) {
606 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000607 DEFINE_GETIMPL_LOOKUP(DIImportedEntity, (Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000608 Metadata *Ops[] = {Scope, Entity, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000609 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000610}
Amjad Abouda9bcf162015-12-10 12:56:35 +0000611
612DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
613 unsigned Line, MDString *Name, MDString *Value,
614 StorageType Storage, bool ShouldCreate) {
615 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000616 DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
Amjad Abouda9bcf162015-12-10 12:56:35 +0000617 Metadata *Ops[] = { Name, Value };
618 DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
619}
620
621DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
622 unsigned Line, Metadata *File,
623 Metadata *Elements, StorageType Storage,
624 bool ShouldCreate) {
625 DEFINE_GETIMPL_LOOKUP(DIMacroFile,
626 (MIType, Line, File, Elements));
627 Metadata *Ops[] = { File, Elements };
628 DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
629}
630