blob: 3db9a3de92eeac512f3cae1d18a478fbe634fd32 [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
48 if (Storage == Uniqued) {
49 if (auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000050 getUniqued(Context.pImpl->DILocations,
51 DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000052 return N;
53 if (!ShouldCreate)
54 return nullptr;
55 } else {
56 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
57 }
58
59 SmallVector<Metadata *, 2> Ops;
60 Ops.push_back(Scope);
61 if (InlinedAt)
62 Ops.push_back(InlinedAt);
63 return storeImpl(new (Ops.size())
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000064 DILocation(Context, Storage, Line, Column, Ops),
65 Storage, Context.pImpl->DILocations);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000066}
67
Leny Kholodov5fcc4182016-09-06 10:46:28 +000068DINode::DIFlags DINode::getFlag(StringRef Flag) {
69 return StringSwitch<DIFlags>(Flag)
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000070#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
71#include "llvm/IR/DebugInfoFlags.def"
Leny Kholodov5fcc4182016-09-06 10:46:28 +000072 .Default(DINode::FlagZero);
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000073}
74
Mehdi Aminif42ec792016-10-01 05:57:50 +000075StringRef DINode::getFlagString(DIFlags Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000076 switch (Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000077#define HANDLE_DI_FLAG(ID, NAME) \
78 case Flag##NAME: \
79 return "DIFlag" #NAME;
80#include "llvm/IR/DebugInfoFlags.def"
81 }
Leny Kholodov5fcc4182016-09-06 10:46:28 +000082 return "";
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000083}
84
Leny Kholodov5fcc4182016-09-06 10:46:28 +000085DINode::DIFlags DINode::splitFlags(DIFlags Flags,
Leny Kholodov40c62352016-09-06 17:03:02 +000086 SmallVectorImpl<DIFlags> &SplitFlags) {
Bob Haarman26a87bd2016-10-25 22:11:52 +000087 // Flags that are packed together need to be specially handled, so
88 // that, for example, we emit "DIFlagPublic" and not
89 // "DIFlagPrivate | DIFlagProtected".
Leny Kholodov5fcc4182016-09-06 10:46:28 +000090 if (DIFlags A = Flags & FlagAccessibility) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000091 if (A == FlagPrivate)
92 SplitFlags.push_back(FlagPrivate);
93 else if (A == FlagProtected)
94 SplitFlags.push_back(FlagProtected);
95 else
96 SplitFlags.push_back(FlagPublic);
97 Flags &= ~A;
98 }
Leny Kholodov5fcc4182016-09-06 10:46:28 +000099 if (DIFlags R = Flags & FlagPtrToMemberRep) {
Reid Kleckner604105b2016-06-17 21:31:33 +0000100 if (R == FlagSingleInheritance)
101 SplitFlags.push_back(FlagSingleInheritance);
102 else if (R == FlagMultipleInheritance)
103 SplitFlags.push_back(FlagMultipleInheritance);
104 else
105 SplitFlags.push_back(FlagVirtualInheritance);
106 Flags &= ~R;
107 }
Bob Haarman26a87bd2016-10-25 22:11:52 +0000108 if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) {
109 Flags &= ~FlagIndirectVirtualBase;
110 SplitFlags.push_back(FlagIndirectVirtualBase);
111 }
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000112
113#define HANDLE_DI_FLAG(ID, NAME) \
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000114 if (DIFlags Bit = Flags & Flag##NAME) { \
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000115 SplitFlags.push_back(Bit); \
116 Flags &= ~Bit; \
117 }
118#include "llvm/IR/DebugInfoFlags.def"
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000119 return Flags;
120}
121
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000122DIScopeRef DIScope::getScope() const {
123 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000124 return T->getScope();
125
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000126 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000127 return SP->getScope();
128
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000129 if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000130 return LB->getScope();
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000131
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000132 if (auto *NS = dyn_cast<DINamespace>(this))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000133 return NS->getScope();
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000134
Adrian Prantlab1243f2015-06-29 23:03:47 +0000135 if (auto *M = dyn_cast<DIModule>(this))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000136 return M->getScope();
Adrian Prantlab1243f2015-06-29 23:03:47 +0000137
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000138 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000139 "Unhandled type of scope.");
140 return nullptr;
141}
142
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000143StringRef DIScope::getName() const {
144 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000145 return T->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000146 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000147 return SP->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000148 if (auto *NS = dyn_cast<DINamespace>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000149 return NS->getName();
Adrian Prantlab1243f2015-06-29 23:03:47 +0000150 if (auto *M = dyn_cast<DIModule>(this))
151 return M->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
153 isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000154 "Unhandled type of scope.");
155 return "";
156}
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000157
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000158#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000159static bool isCanonical(const MDString *S) {
160 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000161}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000162#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000163
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000164GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
165 MDString *Header,
166 ArrayRef<Metadata *> DwarfOps,
167 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000168 unsigned Hash = 0;
169 if (Storage == Uniqued) {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000170 GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000171 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000172 return N;
173 if (!ShouldCreate)
174 return nullptr;
175 Hash = Key.getHash();
176 } else {
177 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
178 }
179
180 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000181 assert(isCanonical(Header) && "Expected canonical MDString");
182 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000183 return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000184 Context, Storage, Hash, Tag, PreOps, DwarfOps),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000185 Storage, Context.pImpl->GenericDINodes);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000186}
187
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000188void GenericDINode::recalculateHash() {
189 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000190}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000191
192#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
193#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
194#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
195 do { \
196 if (Storage == Uniqued) { \
197 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
198 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
199 return N; \
200 if (!ShouldCreate) \
201 return nullptr; \
202 } else { \
203 assert(ShouldCreate && \
204 "Expected non-uniqued nodes to always be created"); \
205 } \
206 } while (false)
207#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
David Blaikie6662d6a2016-04-13 17:42:56 +0000208 return storeImpl(new (array_lengthof(OPS)) \
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000209 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
210 Storage, Context.pImpl->CLASS##s)
211#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
212 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
213 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000214#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
David Blaikie6662d6a2016-04-13 17:42:56 +0000215 return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS), \
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000216 Storage, Context.pImpl->CLASS##s)
Adrian Prantl9d2f0192017-04-26 23:59:52 +0000217#define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \
218 return storeImpl(new (NUM_OPS) \
219 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
220 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000221
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000222DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000223 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000224 DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
225 DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000226}
227
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000228DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000229 MDString *Name, StorageType Storage,
230 bool ShouldCreate) {
231 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000232 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000233 Metadata *Ops[] = {Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000234 DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000235}
236
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000237DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000238 MDString *Name, uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000239 uint32_t AlignInBits, unsigned Encoding,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000240 StorageType Storage, bool ShouldCreate) {
241 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000242 DEFINE_GETIMPL_LOOKUP(DIBasicType,
243 (Tag, Name, SizeInBits, AlignInBits, Encoding));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000244 Metadata *Ops[] = {nullptr, nullptr, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000245 DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000246 Ops);
247}
248
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000249DIDerivedType *DIDerivedType::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,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000252 uint32_t AlignInBits, uint64_t OffsetInBits,
253 Optional<unsigned> DWARFAddressSpace, DIFlags Flags, Metadata *ExtraData,
254 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000255 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000256 DEFINE_GETIMPL_LOOKUP(DIDerivedType,
257 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000258 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
259 ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000260 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
261 DEFINE_GETIMPL_STORE(
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000262 DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
263 DWARFAddressSpace, Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000264}
265
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000266DICompositeType *DICompositeType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000267 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000268 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000269 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000270 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
271 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
272 bool ShouldCreate) {
273 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000274
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000275 // Keep this in sync with buildODRType.
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000276 DEFINE_GETIMPL_LOOKUP(
277 DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
278 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
279 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000280 Metadata *Ops[] = {File, Scope, Name, BaseType,
281 Elements, VTableHolder, TemplateParams, Identifier};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000282 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000283 AlignInBits, OffsetInBits, Flags),
284 Ops);
285}
286
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000287DICompositeType *DICompositeType::buildODRType(
288 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
289 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
Victor Leschuk197aa312016-10-18 14:31:22 +0000290 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000291 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000292 Metadata *VTableHolder, Metadata *TemplateParams) {
293 assert(!Identifier.getString().empty() && "Expected valid identifier");
294 if (!Context.isODRUniquingDebugTypes())
295 return nullptr;
296 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
297 if (!CT)
298 return CT = DICompositeType::getDistinct(
299 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
300 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
301 VTableHolder, TemplateParams, &Identifier);
302
303 // Only mutate CT if it's a forward declaration and the new operands aren't.
304 assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
305 if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl))
306 return CT;
307
308 // Mutate CT in place. Keep this in sync with getImpl.
309 CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
310 Flags);
311 Metadata *Ops[] = {File, Scope, Name, BaseType,
312 Elements, VTableHolder, TemplateParams, &Identifier};
Simon Pilgrim1ec7dc72016-05-02 16:45:02 +0000313 assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() &&
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000314 "Mismatched number of operands");
315 for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I)
316 if (Ops[I] != CT->getOperand(I))
317 CT->setOperand(I, Ops[I]);
318 return CT;
319}
320
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +0000321DICompositeType *DICompositeType::getODRType(
322 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
323 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
Victor Leschuk197aa312016-10-18 14:31:22 +0000324 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000325 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +0000326 Metadata *VTableHolder, Metadata *TemplateParams) {
327 assert(!Identifier.getString().empty() && "Expected valid identifier");
328 if (!Context.isODRUniquingDebugTypes())
329 return nullptr;
330 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
331 if (!CT)
332 CT = DICompositeType::getDistinct(
333 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
334 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
335 TemplateParams, &Identifier);
336 return CT;
337}
338
339DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context,
340 MDString &Identifier) {
341 assert(!Identifier.getString().empty() && "Expected valid identifier");
342 if (!Context.isODRUniquingDebugTypes())
343 return nullptr;
344 return Context.pImpl->DITypeMap->lookup(&Identifier);
345}
346
Leny Kholodov40c62352016-09-06 17:03:02 +0000347DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,
348 uint8_t CC, Metadata *TypeArray,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000349 StorageType Storage,
350 bool ShouldCreate) {
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000351 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray));
Duncan P. N. Exon Smithb9e045a2015-07-24 20:56:36 +0000352 Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000353 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000354}
355
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000356static const char *ChecksumKindName[DIFile::CSK_Last + 1] = {
357 "CSK_None",
358 "CSK_MD5",
359 "CSK_SHA1"
360};
361
362DIFile::ChecksumKind DIFile::getChecksumKind(StringRef CSKindStr) {
363 return StringSwitch<DIFile::ChecksumKind>(CSKindStr)
364 .Case("CSK_MD5", DIFile::CSK_MD5)
365 .Case("CSK_SHA1", DIFile::CSK_SHA1)
366 .Default(DIFile::CSK_None);
367}
368
369StringRef DIFile::getChecksumKindAsString() const {
370 assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
371 return ChecksumKindName[CSKind];
372}
373
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000374DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000375 MDString *Directory, DIFile::ChecksumKind CSKind,
376 MDString *Checksum, StorageType Storage,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000377 bool ShouldCreate) {
378 assert(isCanonical(Filename) && "Expected canonical MDString");
379 assert(isCanonical(Directory) && "Expected canonical MDString");
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000380 assert(isCanonical(Checksum) && "Expected canonical MDString");
381 DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CSKind, Checksum));
382 Metadata *Ops[] = {Filename, Directory, Checksum};
383 DEFINE_GETIMPL_STORE(DIFile, (CSKind), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000384}
385
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000386DICompileUnit *DICompileUnit::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000387 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
388 MDString *Producer, bool IsOptimized, MDString *Flags,
389 unsigned RuntimeVersion, MDString *SplitDebugFilename,
390 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000391 Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
Dehao Chen0944a8c2017-02-01 22:45:09 +0000392 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
393 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000394 assert(Storage != Uniqued && "Cannot unique DICompileUnit");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000395 assert(isCanonical(Producer) && "Expected canonical MDString");
396 assert(isCanonical(Flags) && "Expected canonical MDString");
397 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000398
Adrian Prantl75819ae2016-04-15 15:57:41 +0000399 Metadata *Ops[] = {
400 File, Producer, Flags, SplitDebugFilename,
401 EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities,
402 Macros};
David Blaikiea01f2952016-08-24 18:29:49 +0000403 return storeImpl(new (array_lengthof(Ops))
404 DICompileUnit(Context, Storage, SourceLanguage,
405 IsOptimized, RuntimeVersion, EmissionKind,
Dehao Chen0944a8c2017-02-01 22:45:09 +0000406 DWOId, SplitDebugInlining,
407 DebugInfoForProfiling, Ops),
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000408 Storage);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000409}
410
Adrian Prantlb939a252016-03-31 23:56:58 +0000411Optional<DICompileUnit::DebugEmissionKind>
412DICompileUnit::getEmissionKind(StringRef Str) {
413 return StringSwitch<Optional<DebugEmissionKind>>(Str)
414 .Case("NoDebug", NoDebug)
415 .Case("FullDebug", FullDebug)
416 .Case("LineTablesOnly", LineTablesOnly)
417 .Default(None);
418}
419
420const char *DICompileUnit::EmissionKindString(DebugEmissionKind EK) {
421 switch (EK) {
422 case NoDebug: return "NoDebug";
423 case FullDebug: return "FullDebug";
424 case LineTablesOnly: return "LineTablesOnly";
425 }
426 return nullptr;
427}
428
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000429DISubprogram *DILocalScope::getSubprogram() const {
430 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000431 return Block->getScope()->getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000432 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000433}
434
Amjad Abouda5ba9912016-04-21 16:58:49 +0000435DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const {
436 if (auto *File = dyn_cast<DILexicalBlockFile>(this))
437 return File->getScope()->getNonLexicalBlockFileScope();
438 return const_cast<DILocalScope *>(this);
439}
440
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000441DISubprogram *DISubprogram::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000442 LLVMContext &Context, Metadata *Scope, MDString *Name,
443 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
444 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
445 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000446 int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000447 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000448 Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000449 assert(isCanonical(Name) && "Expected canonical MDString");
450 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000451 DEFINE_GETIMPL_LOOKUP(
Adrian Prantl1d12b882017-04-26 22:56:44 +0000452 DISubprogram, (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
453 IsDefinition, ScopeLine, ContainingType, Virtuality,
454 VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
455 TemplateParams, Declaration, Variables, ThrownTypes));
Adrian Prantl9d2f0192017-04-26 23:59:52 +0000456 SmallVector<Metadata *, 11> Ops = {
457 File, Scope, Name, LinkageName, Type, Unit,
458 Declaration, Variables, ContainingType, TemplateParams, ThrownTypes};
459 if (!ThrownTypes) {
460 Ops.pop_back();
461 if (!TemplateParams) {
462 Ops.pop_back();
463 if (!ContainingType)
464 Ops.pop_back();
465 }
466 }
467 DEFINE_GETIMPL_STORE_N(DISubprogram,
468 (Line, ScopeLine, Virtuality, VirtualIndex,
469 ThisAdjustment, Flags, IsLocalToUnit, IsDefinition,
470 IsOptimized),
471 Ops, Ops.size());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000472}
473
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000474bool DISubprogram::describes(const Function *F) const {
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000475 assert(F && "Invalid function");
Peter Collingbourned4bff302015-11-05 22:03:56 +0000476 if (F->getSubprogram() == this)
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000477 return true;
478 StringRef Name = getLinkageName();
479 if (Name.empty())
480 Name = getName();
481 return F->getName() == Name;
482}
483
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000484DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000485 Metadata *File, unsigned Line,
486 unsigned Column, StorageType Storage,
487 bool ShouldCreate) {
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +0000488 // Fixup column.
489 adjustColumn(Column);
490
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000491 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000492 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000493 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000494 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000495}
496
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000497DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000498 Metadata *Scope, Metadata *File,
499 unsigned Discriminator,
500 StorageType Storage,
501 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000502 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000503 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000504 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000505 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000506}
507
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000508DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000509 Metadata *File, MDString *Name, unsigned Line,
Adrian Prantldbfda632016-11-03 19:42:02 +0000510 bool ExportSymbols, StorageType Storage,
511 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000512 assert(isCanonical(Name) && "Expected canonical MDString");
Adrian Prantldbfda632016-11-03 19:42:02 +0000513 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, Name, Line, ExportSymbols));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000514 Metadata *Ops[] = {File, Scope, Name};
Adrian Prantldbfda632016-11-03 19:42:02 +0000515 DEFINE_GETIMPL_STORE(DINamespace, (Line, ExportSymbols), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000516}
517
Adrian Prantlab1243f2015-06-29 23:03:47 +0000518DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
519 MDString *Name, MDString *ConfigurationMacros,
520 MDString *IncludePath, MDString *ISysRoot,
521 StorageType Storage, bool ShouldCreate) {
522 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000523 DEFINE_GETIMPL_LOOKUP(
524 DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
Adrian Prantlab1243f2015-06-29 23:03:47 +0000525 Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
526 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
527}
528
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000529DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000530 MDString *Name,
531 Metadata *Type,
532 StorageType Storage,
533 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000534 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000535 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000536 Metadata *Ops[] = {Name, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000537 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000538}
539
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000540DITemplateValueParameter *DITemplateValueParameter::getImpl(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000541 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
542 Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000543 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000544 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000545 Metadata *Ops[] = {Name, Type, Value};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000546 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000547}
548
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000549DIGlobalVariable *
550DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000551 MDString *LinkageName, Metadata *File, unsigned Line,
552 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000553 Metadata *StaticDataMemberDeclaration,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000554 uint32_t AlignInBits, StorageType Storage,
555 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000556 assert(isCanonical(Name) && "Expected canonical MDString");
557 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000558 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000559 (Scope, Name, LinkageName, File, Line, Type,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000560 IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000561 StaticDataMemberDeclaration, AlignInBits));
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000562 Metadata *Ops[] = {
563 Scope, Name, File, Type, Name, LinkageName, StaticDataMemberDeclaration};
Victor Leschuk2ede1262016-10-20 00:13:12 +0000564 DEFINE_GETIMPL_STORE(DIGlobalVariable,
565 (Line, IsLocalToUnit, IsDefinition, AlignInBits),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000566 Ops);
567}
568
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000569DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
570 MDString *Name, Metadata *File,
571 unsigned Line, Metadata *Type,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000572 unsigned Arg, DIFlags Flags,
Victor Leschuka37660c2016-10-26 21:32:29 +0000573 uint32_t AlignInBits,
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000574 StorageType Storage,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000575 bool ShouldCreate) {
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +0000576 // 64K ought to be enough for any frontend.
577 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000578
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +0000579 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000580 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000581 DEFINE_GETIMPL_LOOKUP(DILocalVariable,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000582 (Scope, Name, File, Line, Type, Arg, Flags,
583 AlignInBits));
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000584 Metadata *Ops[] = {Scope, Name, File, Type};
Victor Leschuk2ede1262016-10-20 00:13:12 +0000585 DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000586}
587
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000588DIExpression *DIExpression::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000589 ArrayRef<uint64_t> Elements,
590 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000591 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
592 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000593}
594
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000595unsigned DIExpression::ExprOperand::getSize() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000596 switch (getOp()) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000597 case dwarf::DW_OP_LLVM_fragment:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000598 return 3;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000599 case dwarf::DW_OP_constu:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000600 case dwarf::DW_OP_plus:
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000601 case dwarf::DW_OP_minus:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000602 return 2;
603 default:
604 return 1;
605 }
606}
607
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000608bool DIExpression::isValid() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000609 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
610 // Check that there's space for the operand.
611 if (I->get() + I->getSize() > E->get())
612 return false;
613
614 // Check that the operand is valid.
615 switch (I->getOp()) {
616 default:
617 return false;
Adrian Prantl941fa752016-12-05 18:04:47 +0000618 case dwarf::DW_OP_LLVM_fragment:
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000619 // A fragment operator must appear at the end.
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000620 return I->get() + I->getSize() == E->get();
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000621 case dwarf::DW_OP_stack_value: {
622 // Must be the last one or followed by a DW_OP_LLVM_fragment.
623 if (I->get() + I->getSize() == E->get())
624 break;
625 auto J = I;
626 if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment)
627 return false;
628 break;
629 }
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000630 case dwarf::DW_OP_swap: {
631 // Must be more than one implicit element on the stack.
632
633 // FIXME: A better way to implement this would be to add a local variable
634 // that keeps track of the stack depth and introduce something like a
635 // DW_LLVM_OP_implicit_location as a placeholder for the location this
636 // DIExpression is attached to, or else pass the number of implicit stack
637 // elements into isValid.
638 if (getNumElements() == 1)
639 return false;
640 break;
641 }
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000642 case dwarf::DW_OP_constu:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000643 case dwarf::DW_OP_plus:
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000644 case dwarf::DW_OP_minus:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000645 case dwarf::DW_OP_deref:
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000646 case dwarf::DW_OP_xderef:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000647 break;
648 }
649 }
650 return true;
651}
652
Adrian Prantl49797ca2016-12-22 05:27:12 +0000653Optional<DIExpression::FragmentInfo>
654DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) {
655 for (auto I = Start; I != End; ++I)
656 if (I->getOp() == dwarf::DW_OP_LLVM_fragment) {
657 DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)};
658 return Info;
659 }
660 return None;
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000661}
662
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000663bool DIExpression::isConstant() const {
664 // Recognize DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment Len Ofs)?.
665 if (getNumElements() != 3 && getNumElements() != 6)
666 return false;
667 if (getElement(0) != dwarf::DW_OP_constu ||
668 getElement(2) != dwarf::DW_OP_stack_value)
669 return false;
670 if (getNumElements() == 6 && getElement(3) != dwarf::DW_OP_LLVM_fragment)
671 return false;
672 return true;
673}
674
675DIGlobalVariableExpression *
676DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable,
677 Metadata *Expression, StorageType Storage,
678 bool ShouldCreate) {
679 DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression));
680 Metadata *Ops[] = {Variable, Expression};
681 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops);
682}
683
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000684DIObjCProperty *DIObjCProperty::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000685 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
686 MDString *GetterName, MDString *SetterName, unsigned Attributes,
687 Metadata *Type, StorageType Storage, bool ShouldCreate) {
688 assert(isCanonical(Name) && "Expected canonical MDString");
689 assert(isCanonical(GetterName) && "Expected canonical MDString");
690 assert(isCanonical(SetterName) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000691 DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
692 SetterName, Attributes, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000693 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000694 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000695}
696
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000697DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000698 Metadata *Scope, Metadata *Entity,
699 unsigned Line, MDString *Name,
700 StorageType Storage,
701 bool ShouldCreate) {
702 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000703 DEFINE_GETIMPL_LOOKUP(DIImportedEntity, (Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000704 Metadata *Ops[] = {Scope, Entity, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000705 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000706}
Amjad Abouda9bcf162015-12-10 12:56:35 +0000707
708DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
709 unsigned Line, MDString *Name, MDString *Value,
710 StorageType Storage, bool ShouldCreate) {
711 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000712 DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
Amjad Abouda9bcf162015-12-10 12:56:35 +0000713 Metadata *Ops[] = { Name, Value };
714 DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
715}
716
717DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
718 unsigned Line, Metadata *File,
719 Metadata *Elements, StorageType Storage,
720 bool ShouldCreate) {
721 DEFINE_GETIMPL_LOOKUP(DIMacroFile,
722 (MIType, Line, File, Elements));
723 Metadata *Ops[] = { File, Elements };
724 DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
725}
726