blob: 89111f73ae21e5acf8d457723082a0e408d20ee0 [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 DILocation::computeNewDiscriminator() const {
Duncan P. N. Exon Smith843237f2015-04-14 00:35:42 +000070 // FIXME: This seems completely wrong.
71 //
72 // 1. If two modules are generated in the same context, then the second
73 // Module will get different discriminators than it would have if it were
74 // generated in its own context.
75 // 2. If this function is called after round-tripping to bitcode instead of
76 // before, it will give a different (and potentially incorrect!) return.
77 //
78 // The discriminator should instead be calculated from local information
79 // where it's actually needed. This logic should be moved to
80 // AddDiscriminators::runOnFunction(), where it doesn't pollute the
81 // LLVMContext.
82 std::pair<const char *, unsigned> Key(getFilename().data(), getLine());
83 return ++getContext().pImpl->DiscriminatorTable[Key];
84}
85
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000086unsigned DINode::getFlag(StringRef Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000087 return StringSwitch<unsigned>(Flag)
88#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
89#include "llvm/IR/DebugInfoFlags.def"
90 .Default(0);
91}
92
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000093const char *DINode::getFlagString(unsigned Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000094 switch (Flag) {
95 default:
96 return "";
97#define HANDLE_DI_FLAG(ID, NAME) \
98 case Flag##NAME: \
99 return "DIFlag" #NAME;
100#include "llvm/IR/DebugInfoFlags.def"
101 }
102}
103
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000104unsigned DINode::splitFlags(unsigned Flags,
105 SmallVectorImpl<unsigned> &SplitFlags) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000106 // Accessibility flags need to be specially handled, since they're packed
107 // together.
108 if (unsigned A = Flags & FlagAccessibility) {
109 if (A == FlagPrivate)
110 SplitFlags.push_back(FlagPrivate);
111 else if (A == FlagProtected)
112 SplitFlags.push_back(FlagProtected);
113 else
114 SplitFlags.push_back(FlagPublic);
115 Flags &= ~A;
116 }
117
118#define HANDLE_DI_FLAG(ID, NAME) \
119 if (unsigned Bit = Flags & ID) { \
120 SplitFlags.push_back(Bit); \
121 Flags &= ~Bit; \
122 }
123#include "llvm/IR/DebugInfoFlags.def"
124
125 return Flags;
126}
127
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000128DIScopeRef DIScope::getScope() const {
129 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000130 return T->getScope();
131
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000132 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000133 return SP->getScope();
134
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000135 if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
136 return DIScopeRef(LB->getScope());
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000137
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000138 if (auto *NS = dyn_cast<DINamespace>(this))
139 return DIScopeRef(NS->getScope());
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000140
Adrian Prantlab1243f2015-06-29 23:03:47 +0000141 if (auto *M = dyn_cast<DIModule>(this))
142 return DIScopeRef(M->getScope());
143
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000144 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000145 "Unhandled type of scope.");
146 return nullptr;
147}
148
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149StringRef DIScope::getName() const {
150 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000151 return T->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000153 return SP->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000154 if (auto *NS = dyn_cast<DINamespace>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000155 return NS->getName();
Adrian Prantlab1243f2015-06-29 23:03:47 +0000156 if (auto *M = dyn_cast<DIModule>(this))
157 return M->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000158 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
159 isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000160 "Unhandled type of scope.");
161 return "";
162}
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000163
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000164static StringRef getString(const MDString *S) {
165 if (S)
166 return S->getString();
167 return StringRef();
168}
169
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000170#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000171static bool isCanonical(const MDString *S) {
172 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000173}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000174#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000175
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000176GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
177 MDString *Header,
178 ArrayRef<Metadata *> DwarfOps,
179 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000180 unsigned Hash = 0;
181 if (Storage == Uniqued) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000182 GenericDINodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
183 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000184 return N;
185 if (!ShouldCreate)
186 return nullptr;
187 Hash = Key.getHash();
188 } else {
189 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
190 }
191
192 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000193 assert(isCanonical(Header) && "Expected canonical MDString");
194 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000195 return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000196 Context, Storage, Hash, Tag, PreOps, DwarfOps),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000197 Storage, Context.pImpl->GenericDINodes);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000198}
199
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000200void GenericDINode::recalculateHash() {
201 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000202}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000203
204#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
205#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
206#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
207 do { \
208 if (Storage == Uniqued) { \
209 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
210 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
211 return N; \
212 if (!ShouldCreate) \
213 return nullptr; \
214 } else { \
215 assert(ShouldCreate && \
216 "Expected non-uniqued nodes to always be created"); \
217 } \
218 } while (false)
219#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
220 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
221 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
222 Storage, Context.pImpl->CLASS##s)
223#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
224 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
225 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000226#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
227 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
228 CLASS(Context, Storage, OPS), \
229 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000230
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000231DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000232 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000233 DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
234 DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000235}
236
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000237DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000238 MDString *Name, StorageType Storage,
239 bool ShouldCreate) {
240 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000241 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, getString(Name)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000242 Metadata *Ops[] = {Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243 DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000244}
245
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000246DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000247 MDString *Name, uint64_t SizeInBits,
248 uint64_t AlignInBits, unsigned Encoding,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000249 StorageType Storage, bool ShouldCreate) {
250 assert(isCanonical(Name) && "Expected canonical MDString");
251 DEFINE_GETIMPL_LOOKUP(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000252 DIBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000253 Metadata *Ops[] = {nullptr, nullptr, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000254 DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000255 Ops);
256}
257
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258DIDerivedType *DIDerivedType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000259 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000260 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
261 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000262 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
263 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000264 DEFINE_GETIMPL_LOOKUP(DIDerivedType, (Tag, getString(Name), File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000265 BaseType, SizeInBits, AlignInBits,
266 OffsetInBits, Flags, ExtraData));
267 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
268 DEFINE_GETIMPL_STORE(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000269 DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000270 Ops);
271}
272
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000273DICompositeType *DICompositeType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000274 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000275 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
276 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000277 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
278 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
279 bool ShouldCreate) {
280 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000281 DEFINE_GETIMPL_LOOKUP(DICompositeType,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000282 (Tag, getString(Name), File, Line, Scope, BaseType,
283 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
284 RuntimeLang, VTableHolder, TemplateParams,
285 getString(Identifier)));
286 Metadata *Ops[] = {File, Scope, Name, BaseType,
287 Elements, VTableHolder, TemplateParams, Identifier};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000288 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000289 AlignInBits, OffsetInBits, Flags),
290 Ops);
291}
292
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000293DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000294 unsigned Flags, Metadata *TypeArray,
295 StorageType Storage,
296 bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000297 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, TypeArray));
Duncan P. N. Exon Smithb9e045a2015-07-24 20:56:36 +0000298 Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000299 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000300}
301
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000302DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000303 MDString *Directory, StorageType Storage,
304 bool ShouldCreate) {
305 assert(isCanonical(Filename) && "Expected canonical MDString");
306 assert(isCanonical(Directory) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000307 DEFINE_GETIMPL_LOOKUP(DIFile, (getString(Filename), getString(Directory)));
Duncan P. N. Exon Smitha5c57cc2015-02-20 20:35:17 +0000308 Metadata *Ops[] = {Filename, Directory};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000309 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIFile, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000310}
311
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000312DICompileUnit *DICompileUnit::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000313 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
314 MDString *Producer, bool IsOptimized, MDString *Flags,
315 unsigned RuntimeVersion, MDString *SplitDebugFilename,
316 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
317 Metadata *Subprograms, Metadata *GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +0000318 Metadata *ImportedEntities, uint64_t DWOId,
319 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000320 assert(Storage != Uniqued && "Cannot unique DICompileUnit");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000321 assert(isCanonical(Producer) && "Expected canonical MDString");
322 assert(isCanonical(Flags) && "Expected canonical MDString");
323 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000324
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000325 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
326 RetainedTypes, Subprograms, GlobalVariables,
327 ImportedEntities};
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000328 return storeImpl(new (ArrayRef<Metadata *>(Ops).size()) DICompileUnit(
329 Context, Storage, SourceLanguage, IsOptimized,
330 RuntimeVersion, EmissionKind, DWOId, Ops),
331 Storage);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000332}
333
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000334DISubprogram *DILocalScope::getSubprogram() const {
335 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000336 return Block->getScope()->getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000337 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000338}
339
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000340DISubprogram *DISubprogram::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000341 LLVMContext &Context, Metadata *Scope, MDString *Name,
342 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
343 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
344 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
345 unsigned Flags, bool IsOptimized, Metadata *Function,
346 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
347 StorageType Storage, bool ShouldCreate) {
348 assert(isCanonical(Name) && "Expected canonical MDString");
349 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000350 DEFINE_GETIMPL_LOOKUP(DISubprogram,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000351 (Scope, getString(Name), getString(LinkageName), File,
352 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
353 ContainingType, Virtuality, VirtualIndex, Flags,
354 IsOptimized, Function, TemplateParams, Declaration,
355 Variables));
356 Metadata *Ops[] = {File, Scope, Name, Name,
357 LinkageName, Type, ContainingType, Function,
358 TemplateParams, Declaration, Variables};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000359 DEFINE_GETIMPL_STORE(DISubprogram,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000360 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
361 IsLocalToUnit, IsDefinition, IsOptimized),
362 Ops);
363}
364
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000365Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000366 // FIXME: Should this be looking through bitcasts?
367 return dyn_cast_or_null<Function>(getFunctionConstant());
368}
369
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000370bool DISubprogram::describes(const Function *F) const {
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000371 assert(F && "Invalid function");
372 if (F == getFunction())
373 return true;
374 StringRef Name = getLinkageName();
375 if (Name.empty())
376 Name = getName();
377 return F->getName() == Name;
378}
379
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000380void DISubprogram::replaceFunction(Function *F) {
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +0000381 replaceFunction(F ? ConstantAsMetadata::get(F)
382 : static_cast<ConstantAsMetadata *>(nullptr));
383}
384
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000385DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000386 Metadata *File, unsigned Line,
387 unsigned Column, StorageType Storage,
388 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000389 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000390 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000391 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000392 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000393}
394
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000395DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000396 Metadata *Scope, Metadata *File,
397 unsigned Discriminator,
398 StorageType Storage,
399 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000400 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000401 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000402 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000403 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000404}
405
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000406DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000407 Metadata *File, MDString *Name, unsigned Line,
408 StorageType Storage, bool ShouldCreate) {
409 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000410 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, getString(Name), Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000411 Metadata *Ops[] = {File, Scope, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000412 DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000413}
414
Adrian Prantlab1243f2015-06-29 23:03:47 +0000415DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
416 MDString *Name, MDString *ConfigurationMacros,
417 MDString *IncludePath, MDString *ISysRoot,
418 StorageType Storage, bool ShouldCreate) {
419 assert(isCanonical(Name) && "Expected canonical MDString");
420 DEFINE_GETIMPL_LOOKUP(DIModule,
421 (Scope, getString(Name), getString(ConfigurationMacros),
422 getString(IncludePath), getString(ISysRoot)));
423 Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
424 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
425}
426
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000427DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000428 MDString *Name,
429 Metadata *Type,
430 StorageType Storage,
431 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000432 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000433 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (getString(Name), Type));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000434 Metadata *Ops[] = {Name, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000435 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000436}
437
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000438DITemplateValueParameter *DITemplateValueParameter::getImpl(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000439 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
440 Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000441 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000442 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000443 (Tag, getString(Name), Type, Value));
444 Metadata *Ops[] = {Name, Type, Value};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000445 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000446}
447
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000448DIGlobalVariable *
449DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000450 MDString *LinkageName, Metadata *File, unsigned Line,
451 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
452 Metadata *Variable,
453 Metadata *StaticDataMemberDeclaration,
454 StorageType Storage, bool ShouldCreate) {
455 assert(isCanonical(Name) && "Expected canonical MDString");
456 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000457 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000458 (Scope, getString(Name), getString(LinkageName), File,
459 Line, Type, IsLocalToUnit, IsDefinition, Variable,
460 StaticDataMemberDeclaration));
461 Metadata *Ops[] = {Scope, Name, File, Type,
462 Name, LinkageName, Variable, StaticDataMemberDeclaration};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000463 DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000464 Ops);
465}
466
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000467DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
468 MDString *Name, Metadata *File,
469 unsigned Line, Metadata *Type,
470 unsigned Arg, unsigned Flags,
471 StorageType Storage,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000472 bool ShouldCreate) {
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +0000473 // 64K ought to be enough for any frontend.
474 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000475
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +0000476 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000477 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000478 DEFINE_GETIMPL_LOOKUP(DILocalVariable,
479 (Scope, getString(Name), File, Line, Type, Arg, Flags));
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000480 Metadata *Ops[] = {Scope, Name, File, Type};
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000481 DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000482}
483
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000484DIExpression *DIExpression::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000485 ArrayRef<uint64_t> Elements,
486 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000487 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
488 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000489}
490
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000491unsigned DIExpression::ExprOperand::getSize() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000492 switch (getOp()) {
493 case dwarf::DW_OP_bit_piece:
494 return 3;
495 case dwarf::DW_OP_plus:
496 return 2;
497 default:
498 return 1;
499 }
500}
501
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000502bool DIExpression::isValid() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000503 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
504 // Check that there's space for the operand.
505 if (I->get() + I->getSize() > E->get())
506 return false;
507
508 // Check that the operand is valid.
509 switch (I->getOp()) {
510 default:
511 return false;
512 case dwarf::DW_OP_bit_piece:
513 // Piece expressions must be at the end.
514 return I->get() + I->getSize() == E->get();
515 case dwarf::DW_OP_plus:
516 case dwarf::DW_OP_deref:
517 break;
518 }
519 }
520 return true;
521}
522
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000523bool DIExpression::isBitPiece() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000524 assert(isValid() && "Expected valid expression");
525 if (unsigned N = getNumElements())
526 if (N >= 3)
527 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
528 return false;
529}
530
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000531uint64_t DIExpression::getBitPieceOffset() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000532 assert(isBitPiece() && "Expected bit piece");
533 return getElement(getNumElements() - 2);
534}
535
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000536uint64_t DIExpression::getBitPieceSize() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000537 assert(isBitPiece() && "Expected bit piece");
538 return getElement(getNumElements() - 1);
539}
540
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000541DIObjCProperty *DIObjCProperty::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000542 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
543 MDString *GetterName, MDString *SetterName, unsigned Attributes,
544 Metadata *Type, StorageType Storage, bool ShouldCreate) {
545 assert(isCanonical(Name) && "Expected canonical MDString");
546 assert(isCanonical(GetterName) && "Expected canonical MDString");
547 assert(isCanonical(SetterName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000548 DEFINE_GETIMPL_LOOKUP(DIObjCProperty,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000549 (getString(Name), File, Line, getString(GetterName),
550 getString(SetterName), Attributes, Type));
551 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000552 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000553}
554
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000555DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000556 Metadata *Scope, Metadata *Entity,
557 unsigned Line, MDString *Name,
558 StorageType Storage,
559 bool ShouldCreate) {
560 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000561 DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000562 (Tag, Scope, Entity, Line, getString(Name)));
563 Metadata *Ops[] = {Scope, Entity, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000564 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000565}