blob: 85335d547b195459f2902f3fafcf30ee3cd9f44e [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
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000141 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000142 "Unhandled type of scope.");
143 return nullptr;
144}
145
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000146StringRef DIScope::getName() const {
147 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000148 return T->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000150 return SP->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000151 if (auto *NS = dyn_cast<DINamespace>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000152 return NS->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000153 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
154 isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000155 "Unhandled type of scope.");
156 return "";
157}
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000158
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000159static StringRef getString(const MDString *S) {
160 if (S)
161 return S->getString();
162 return StringRef();
163}
164
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000165#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000166static bool isCanonical(const MDString *S) {
167 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000168}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000169#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000170
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000171GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
172 MDString *Header,
173 ArrayRef<Metadata *> DwarfOps,
174 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000175 unsigned Hash = 0;
176 if (Storage == Uniqued) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000177 GenericDINodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
178 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000179 return N;
180 if (!ShouldCreate)
181 return nullptr;
182 Hash = Key.getHash();
183 } else {
184 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
185 }
186
187 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000188 assert(isCanonical(Header) && "Expected canonical MDString");
189 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000190 return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000191 Context, Storage, Hash, Tag, PreOps, DwarfOps),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000192 Storage, Context.pImpl->GenericDINodes);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000193}
194
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000195void GenericDINode::recalculateHash() {
196 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000197}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000198
199#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
200#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
201#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
202 do { \
203 if (Storage == Uniqued) { \
204 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
205 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
206 return N; \
207 if (!ShouldCreate) \
208 return nullptr; \
209 } else { \
210 assert(ShouldCreate && \
211 "Expected non-uniqued nodes to always be created"); \
212 } \
213 } while (false)
214#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
215 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
216 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
217 Storage, Context.pImpl->CLASS##s)
218#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
219 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
220 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000221#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
222 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
223 CLASS(Context, Storage, OPS), \
224 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000225
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000226DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000227 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000228 DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
229 DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000230}
231
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000232DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000233 MDString *Name, StorageType Storage,
234 bool ShouldCreate) {
235 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000236 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, getString(Name)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000237 Metadata *Ops[] = {Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238 DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000239}
240
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000241DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000242 MDString *Name, uint64_t SizeInBits,
243 uint64_t AlignInBits, unsigned Encoding,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000244 StorageType Storage, bool ShouldCreate) {
245 assert(isCanonical(Name) && "Expected canonical MDString");
246 DEFINE_GETIMPL_LOOKUP(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000247 DIBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000248 Metadata *Ops[] = {nullptr, nullptr, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000249 DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000250 Ops);
251}
252
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000253DIDerivedType *DIDerivedType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000254 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000255 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
256 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000257 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
258 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000259 DEFINE_GETIMPL_LOOKUP(DIDerivedType, (Tag, getString(Name), File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000260 BaseType, SizeInBits, AlignInBits,
261 OffsetInBits, Flags, ExtraData));
262 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
263 DEFINE_GETIMPL_STORE(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000264 DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000265 Ops);
266}
267
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000268DICompositeType *DICompositeType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000269 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000270 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
271 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000272 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
273 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
274 bool ShouldCreate) {
275 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000276 DEFINE_GETIMPL_LOOKUP(DICompositeType,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000277 (Tag, getString(Name), File, Line, Scope, BaseType,
278 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
279 RuntimeLang, VTableHolder, TemplateParams,
280 getString(Identifier)));
281 Metadata *Ops[] = {File, Scope, Name, BaseType,
282 Elements, VTableHolder, TemplateParams, Identifier};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000283 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000284 AlignInBits, OffsetInBits, Flags),
285 Ops);
286}
287
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000288DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000289 unsigned Flags, Metadata *TypeArray,
290 StorageType Storage,
291 bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000292 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000293 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +0000294 TypeArray, nullptr, nullptr, nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000295 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000296}
297
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000298DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000299 MDString *Directory, StorageType Storage,
300 bool ShouldCreate) {
301 assert(isCanonical(Filename) && "Expected canonical MDString");
302 assert(isCanonical(Directory) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000303 DEFINE_GETIMPL_LOOKUP(DIFile, (getString(Filename), getString(Directory)));
Duncan P. N. Exon Smitha5c57cc2015-02-20 20:35:17 +0000304 Metadata *Ops[] = {Filename, Directory};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000305 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIFile, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000306}
307
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000308DICompileUnit *DICompileUnit::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000309 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
310 MDString *Producer, bool IsOptimized, MDString *Flags,
311 unsigned RuntimeVersion, MDString *SplitDebugFilename,
312 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
313 Metadata *Subprograms, Metadata *GlobalVariables,
314 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
315 assert(isCanonical(Producer) && "Expected canonical MDString");
316 assert(isCanonical(Flags) && "Expected canonical MDString");
317 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
318 DEFINE_GETIMPL_LOOKUP(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000319 DICompileUnit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000320 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
321 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
322 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
323 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
324 RetainedTypes, Subprograms, GlobalVariables,
325 ImportedEntities};
326 DEFINE_GETIMPL_STORE(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000327 DICompileUnit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000328 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
329}
330
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000331DISubprogram *DILocalScope::getSubprogram() const {
332 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000333 return Block->getScope()->getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000334 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000335}
336
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000337DISubprogram *DISubprogram::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000338 LLVMContext &Context, Metadata *Scope, MDString *Name,
339 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
340 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
341 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
342 unsigned Flags, bool IsOptimized, Metadata *Function,
343 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
344 StorageType Storage, bool ShouldCreate) {
345 assert(isCanonical(Name) && "Expected canonical MDString");
346 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000347 DEFINE_GETIMPL_LOOKUP(DISubprogram,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000348 (Scope, getString(Name), getString(LinkageName), File,
349 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
350 ContainingType, Virtuality, VirtualIndex, Flags,
351 IsOptimized, Function, TemplateParams, Declaration,
352 Variables));
353 Metadata *Ops[] = {File, Scope, Name, Name,
354 LinkageName, Type, ContainingType, Function,
355 TemplateParams, Declaration, Variables};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000356 DEFINE_GETIMPL_STORE(DISubprogram,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000357 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
358 IsLocalToUnit, IsDefinition, IsOptimized),
359 Ops);
360}
361
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000362Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000363 // FIXME: Should this be looking through bitcasts?
364 return dyn_cast_or_null<Function>(getFunctionConstant());
365}
366
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000367bool DISubprogram::describes(const Function *F) const {
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000368 assert(F && "Invalid function");
369 if (F == getFunction())
370 return true;
371 StringRef Name = getLinkageName();
372 if (Name.empty())
373 Name = getName();
374 return F->getName() == Name;
375}
376
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000377void DISubprogram::replaceFunction(Function *F) {
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +0000378 replaceFunction(F ? ConstantAsMetadata::get(F)
379 : static_cast<ConstantAsMetadata *>(nullptr));
380}
381
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000382DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000383 Metadata *File, unsigned Line,
384 unsigned Column, StorageType Storage,
385 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000386 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000387 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000388 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000390}
391
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000392DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000393 Metadata *Scope, Metadata *File,
394 unsigned Discriminator,
395 StorageType Storage,
396 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000397 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000398 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000399 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000400 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000401}
402
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000403DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000404 Metadata *File, MDString *Name, unsigned Line,
405 StorageType Storage, bool ShouldCreate) {
406 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000407 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, getString(Name), Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000408 Metadata *Ops[] = {File, Scope, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000409 DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000410}
411
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000412DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000413 MDString *Name,
414 Metadata *Type,
415 StorageType Storage,
416 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000417 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000418 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (getString(Name), Type));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000419 Metadata *Ops[] = {Name, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000420 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000421}
422
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000423DITemplateValueParameter *DITemplateValueParameter::getImpl(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000424 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
425 Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000426 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000427 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000428 (Tag, getString(Name), Type, Value));
429 Metadata *Ops[] = {Name, Type, Value};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000430 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000431}
432
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000433DIGlobalVariable *
434DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000435 MDString *LinkageName, Metadata *File, unsigned Line,
436 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
437 Metadata *Variable,
438 Metadata *StaticDataMemberDeclaration,
439 StorageType Storage, bool ShouldCreate) {
440 assert(isCanonical(Name) && "Expected canonical MDString");
441 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000442 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000443 (Scope, getString(Name), getString(LinkageName), File,
444 Line, Type, IsLocalToUnit, IsDefinition, Variable,
445 StaticDataMemberDeclaration));
446 Metadata *Ops[] = {Scope, Name, File, Type,
447 Name, LinkageName, Variable, StaticDataMemberDeclaration};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000448 DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000449 Ops);
450}
451
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000452DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000453 Metadata *Scope, MDString *Name,
454 Metadata *File, unsigned Line,
455 Metadata *Type, unsigned Arg,
456 unsigned Flags, StorageType Storage,
457 bool ShouldCreate) {
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +0000458 // 64K ought to be enough for any frontend.
459 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000460
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +0000461 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000462 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000463 DEFINE_GETIMPL_LOOKUP(DILocalVariable, (Tag, Scope, getString(Name), File,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000464 Line, Type, Arg, Flags));
465 Metadata *Ops[] = {Scope, Name, File, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000466 DEFINE_GETIMPL_STORE(DILocalVariable, (Tag, Line, Arg, Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000467}
468
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000469DIExpression *DIExpression::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000470 ArrayRef<uint64_t> Elements,
471 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000472 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
473 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000474}
475
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000476unsigned DIExpression::ExprOperand::getSize() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000477 switch (getOp()) {
478 case dwarf::DW_OP_bit_piece:
479 return 3;
480 case dwarf::DW_OP_plus:
481 return 2;
482 default:
483 return 1;
484 }
485}
486
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000487bool DIExpression::isValid() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000488 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
489 // Check that there's space for the operand.
490 if (I->get() + I->getSize() > E->get())
491 return false;
492
493 // Check that the operand is valid.
494 switch (I->getOp()) {
495 default:
496 return false;
497 case dwarf::DW_OP_bit_piece:
498 // Piece expressions must be at the end.
499 return I->get() + I->getSize() == E->get();
500 case dwarf::DW_OP_plus:
501 case dwarf::DW_OP_deref:
502 break;
503 }
504 }
505 return true;
506}
507
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000508bool DIExpression::isBitPiece() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000509 assert(isValid() && "Expected valid expression");
510 if (unsigned N = getNumElements())
511 if (N >= 3)
512 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
513 return false;
514}
515
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000516uint64_t DIExpression::getBitPieceOffset() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000517 assert(isBitPiece() && "Expected bit piece");
518 return getElement(getNumElements() - 2);
519}
520
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000521uint64_t DIExpression::getBitPieceSize() const {
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000522 assert(isBitPiece() && "Expected bit piece");
523 return getElement(getNumElements() - 1);
524}
525
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000526DIObjCProperty *DIObjCProperty::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000527 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
528 MDString *GetterName, MDString *SetterName, unsigned Attributes,
529 Metadata *Type, StorageType Storage, bool ShouldCreate) {
530 assert(isCanonical(Name) && "Expected canonical MDString");
531 assert(isCanonical(GetterName) && "Expected canonical MDString");
532 assert(isCanonical(SetterName) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000533 DEFINE_GETIMPL_LOOKUP(DIObjCProperty,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000534 (getString(Name), File, Line, getString(GetterName),
535 getString(SetterName), Attributes, Type));
536 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000537 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000538}
539
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000540DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000541 Metadata *Scope, Metadata *Entity,
542 unsigned Line, MDString *Name,
543 StorageType Storage,
544 bool ShouldCreate) {
545 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000546 DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000547 (Tag, Scope, Entity, Line, getString(Name)));
548 Metadata *Ops[] = {Scope, Entity, Name};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000549 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000550}