blob: ed6206264b0966a4025165fa0c0f6f3f493cfa11 [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
22MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
23 unsigned Column, ArrayRef<Metadata *> MDs)
24 : MDNode(C, MDLocationKind, Storage, MDs) {
25 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
41MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
42 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 =
51 getUniqued(Context.pImpl->MDLocations,
52 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
53 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())
65 MDLocation(Context, Storage, Line, Column, Ops),
66 Storage, Context.pImpl->MDLocations);
67}
68
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000069unsigned DebugNode::getFlag(StringRef Flag) {
70 return StringSwitch<unsigned>(Flag)
71#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
72#include "llvm/IR/DebugInfoFlags.def"
73 .Default(0);
74}
75
76const char *DebugNode::getFlagString(unsigned Flag) {
77 switch (Flag) {
78 default:
79 return "";
80#define HANDLE_DI_FLAG(ID, NAME) \
81 case Flag##NAME: \
82 return "DIFlag" #NAME;
83#include "llvm/IR/DebugInfoFlags.def"
84 }
85}
86
87unsigned DebugNode::splitFlags(unsigned Flags,
88 SmallVectorImpl<unsigned> &SplitFlags) {
89 // Accessibility flags need to be specially handled, since they're packed
90 // together.
91 if (unsigned A = Flags & FlagAccessibility) {
92 if (A == FlagPrivate)
93 SplitFlags.push_back(FlagPrivate);
94 else if (A == FlagProtected)
95 SplitFlags.push_back(FlagProtected);
96 else
97 SplitFlags.push_back(FlagPublic);
98 Flags &= ~A;
99 }
100
101#define HANDLE_DI_FLAG(ID, NAME) \
102 if (unsigned Bit = Flags & ID) { \
103 SplitFlags.push_back(Bit); \
104 Flags &= ~Bit; \
105 }
106#include "llvm/IR/DebugInfoFlags.def"
107
108 return Flags;
109}
110
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000111MDScopeRef MDScope::getScope() const {
112 if (auto *T = dyn_cast<MDType>(this))
113 return T->getScope();
114
115 if (auto *SP = dyn_cast<MDSubprogram>(this))
116 return SP->getScope();
117
118 if (auto *LB = dyn_cast<MDLexicalBlockBase>(this))
119 return MDScopeRef(LB->getScope());
120
121 if (auto *NS = dyn_cast<MDNamespace>(this))
122 return MDScopeRef(NS->getScope());
123
124 assert((isa<MDFile>(this) || isa<MDCompileUnit>(this)) &&
125 "Unhandled type of scope.");
126 return nullptr;
127}
128
129StringRef MDScope::getName() const {
130 if (auto *T = dyn_cast<MDType>(this))
131 return T->getName();
132 if (auto *SP = dyn_cast<MDSubprogram>(this))
133 return SP->getName();
134 if (auto *NS = dyn_cast<MDNamespace>(this))
135 return NS->getName();
136 assert((isa<MDLexicalBlockBase>(this) || isa<MDFile>(this) ||
137 isa<MDCompileUnit>(this)) &&
138 "Unhandled type of scope.");
139 return "";
140}
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000141
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000142static StringRef getString(const MDString *S) {
143 if (S)
144 return S->getString();
145 return StringRef();
146}
147
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000148#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000149static bool isCanonical(const MDString *S) {
150 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000151}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000152#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000153
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000154GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000155 MDString *Header,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000156 ArrayRef<Metadata *> DwarfOps,
157 StorageType Storage,
158 bool ShouldCreate) {
159 unsigned Hash = 0;
160 if (Storage == Uniqued) {
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000161 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000162 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
163 return N;
164 if (!ShouldCreate)
165 return nullptr;
166 Hash = Key.getHash();
167 } else {
168 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
169 }
170
171 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000172 assert(isCanonical(Header) && "Expected canonical MDString");
173 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000174 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
175 Context, Storage, Hash, Tag, PreOps, DwarfOps),
176 Storage, Context.pImpl->GenericDebugNodes);
177}
178
179void GenericDebugNode::recalculateHash() {
180 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
181}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000182
183#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
184#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
185#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
186 do { \
187 if (Storage == Uniqued) { \
188 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
189 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
190 return N; \
191 if (!ShouldCreate) \
192 return nullptr; \
193 } else { \
194 assert(ShouldCreate && \
195 "Expected non-uniqued nodes to always be created"); \
196 } \
197 } while (false)
198#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
199 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
200 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
201 Storage, Context.pImpl->CLASS##s)
202#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
203 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
204 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000205#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
206 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
207 CLASS(Context, Storage, OPS), \
208 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000209
210MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
211 StorageType Storage, bool ShouldCreate) {
212 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
213 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
214}
215
216MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
217 MDString *Name, StorageType Storage,
218 bool ShouldCreate) {
219 assert(isCanonical(Name) && "Expected canonical MDString");
220 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
221 Metadata *Ops[] = {Name};
222 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
223}
224
225MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000226 MDString *Name, uint64_t SizeInBits,
227 uint64_t AlignInBits, unsigned Encoding,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000228 StorageType Storage, bool ShouldCreate) {
229 assert(isCanonical(Name) && "Expected canonical MDString");
230 DEFINE_GETIMPL_LOOKUP(
231 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
232 Metadata *Ops[] = {nullptr, nullptr, Name};
233 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
234 Ops);
235}
236
237MDDerivedType *MDDerivedType::getImpl(
238 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000239 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
240 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000241 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
242 assert(isCanonical(Name) && "Expected canonical MDString");
243 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
244 BaseType, SizeInBits, AlignInBits,
245 OffsetInBits, Flags, ExtraData));
246 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
247 DEFINE_GETIMPL_STORE(
248 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
249 Ops);
250}
251
252MDCompositeType *MDCompositeType::getImpl(
253 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000254 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
255 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000256 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
257 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
258 bool ShouldCreate) {
259 assert(isCanonical(Name) && "Expected canonical MDString");
260 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
261 (Tag, getString(Name), File, Line, Scope, BaseType,
262 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
263 RuntimeLang, VTableHolder, TemplateParams,
264 getString(Identifier)));
265 Metadata *Ops[] = {File, Scope, Name, BaseType,
266 Elements, VTableHolder, TemplateParams, Identifier};
267 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
268 AlignInBits, OffsetInBits, Flags),
269 Ops);
270}
271
272MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
273 unsigned Flags, Metadata *TypeArray,
274 StorageType Storage,
275 bool ShouldCreate) {
276 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
277 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +0000278 TypeArray, nullptr, nullptr, nullptr};
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000279 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
280}
281
282MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
283 MDString *Directory, StorageType Storage,
284 bool ShouldCreate) {
285 assert(isCanonical(Filename) && "Expected canonical MDString");
286 assert(isCanonical(Directory) && "Expected canonical MDString");
287 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
Duncan P. N. Exon Smitha5c57cc2015-02-20 20:35:17 +0000288 Metadata *Ops[] = {Filename, Directory};
289 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000290}
291
292MDCompileUnit *MDCompileUnit::getImpl(
293 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
294 MDString *Producer, bool IsOptimized, MDString *Flags,
295 unsigned RuntimeVersion, MDString *SplitDebugFilename,
296 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
297 Metadata *Subprograms, Metadata *GlobalVariables,
298 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
299 assert(isCanonical(Producer) && "Expected canonical MDString");
300 assert(isCanonical(Flags) && "Expected canonical MDString");
301 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
302 DEFINE_GETIMPL_LOOKUP(
303 MDCompileUnit,
304 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
305 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
306 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
307 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
308 RetainedTypes, Subprograms, GlobalVariables,
309 ImportedEntities};
310 DEFINE_GETIMPL_STORE(
311 MDCompileUnit,
312 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
313}
314
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000315MDSubprogram *MDLocalScope::getSubprogram() const {
316 if (auto *Block = dyn_cast<MDLexicalBlockBase>(this))
317 return Block->getScope()->getSubprogram();
318 return const_cast<MDSubprogram *>(cast<MDSubprogram>(this));
319}
320
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000321MDSubprogram *MDSubprogram::getImpl(
322 LLVMContext &Context, Metadata *Scope, MDString *Name,
323 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
324 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
325 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
326 unsigned Flags, bool IsOptimized, Metadata *Function,
327 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
328 StorageType Storage, bool ShouldCreate) {
329 assert(isCanonical(Name) && "Expected canonical MDString");
330 assert(isCanonical(LinkageName) && "Expected canonical MDString");
331 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
332 (Scope, getString(Name), getString(LinkageName), File,
333 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
334 ContainingType, Virtuality, VirtualIndex, Flags,
335 IsOptimized, Function, TemplateParams, Declaration,
336 Variables));
337 Metadata *Ops[] = {File, Scope, Name, Name,
338 LinkageName, Type, ContainingType, Function,
339 TemplateParams, Declaration, Variables};
340 DEFINE_GETIMPL_STORE(MDSubprogram,
341 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
342 IsLocalToUnit, IsDefinition, IsOptimized),
343 Ops);
344}
345
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000346Function *MDSubprogram::getFunction() const {
347 // FIXME: Should this be looking through bitcasts?
348 return dyn_cast_or_null<Function>(getFunctionConstant());
349}
350
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000351bool MDSubprogram::describes(const Function *F) const {
352 assert(F && "Invalid function");
353 if (F == getFunction())
354 return true;
355 StringRef Name = getLinkageName();
356 if (Name.empty())
357 Name = getName();
358 return F->getName() == Name;
359}
360
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +0000361void MDSubprogram::replaceFunction(Function *F) {
362 replaceFunction(F ? ConstantAsMetadata::get(F)
363 : static_cast<ConstantAsMetadata *>(nullptr));
364}
365
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000366MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
367 Metadata *File, unsigned Line,
368 unsigned Column, StorageType Storage,
369 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000370 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000371 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
372 Metadata *Ops[] = {File, Scope};
373 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
374}
375
376MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
377 Metadata *Scope, Metadata *File,
378 unsigned Discriminator,
379 StorageType Storage,
380 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000381 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000382 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
383 Metadata *Ops[] = {File, Scope};
384 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
385}
386
387MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
388 Metadata *File, MDString *Name, unsigned Line,
389 StorageType Storage, bool ShouldCreate) {
390 assert(isCanonical(Name) && "Expected canonical MDString");
391 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
392 Metadata *Ops[] = {File, Scope, Name};
393 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
394}
395
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000396MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
397 MDString *Name,
398 Metadata *Type,
399 StorageType Storage,
400 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000401 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000402 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
403 Metadata *Ops[] = {Name, Type};
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000404 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000405}
406
407MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000408 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
409 Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000410 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000411 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000412 (Tag, getString(Name), Type, Value));
413 Metadata *Ops[] = {Name, Type, Value};
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000414 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000415}
416
417MDGlobalVariable *
418MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
419 MDString *LinkageName, Metadata *File, unsigned Line,
420 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
421 Metadata *Variable,
422 Metadata *StaticDataMemberDeclaration,
423 StorageType Storage, bool ShouldCreate) {
424 assert(isCanonical(Name) && "Expected canonical MDString");
425 assert(isCanonical(LinkageName) && "Expected canonical MDString");
426 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
427 (Scope, getString(Name), getString(LinkageName), File,
428 Line, Type, IsLocalToUnit, IsDefinition, Variable,
429 StaticDataMemberDeclaration));
430 Metadata *Ops[] = {Scope, Name, File, Type,
431 Name, LinkageName, Variable, StaticDataMemberDeclaration};
432 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
433 Ops);
434}
435
436MDLocalVariable *MDLocalVariable::getImpl(
437 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
438 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
439 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000440 // Truncate Arg to 8 bits.
441 //
442 // FIXME: This is gross (and should be changed to an assert or removed), but
443 // it matches historical behaviour for now.
444 Arg &= (1u << 8) - 1;
445
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +0000446 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000447 assert(isCanonical(Name) && "Expected canonical MDString");
448 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
449 Line, Type, Arg, Flags, InlinedAt));
450 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
451 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
452}
453
454MDExpression *MDExpression::getImpl(LLVMContext &Context,
455 ArrayRef<uint64_t> Elements,
456 StorageType Storage, bool ShouldCreate) {
457 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
458 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
459}
460
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000461unsigned MDExpression::ExprOperand::getSize() const {
462 switch (getOp()) {
463 case dwarf::DW_OP_bit_piece:
464 return 3;
465 case dwarf::DW_OP_plus:
466 return 2;
467 default:
468 return 1;
469 }
470}
471
472bool MDExpression::isValid() const {
473 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
474 // Check that there's space for the operand.
475 if (I->get() + I->getSize() > E->get())
476 return false;
477
478 // Check that the operand is valid.
479 switch (I->getOp()) {
480 default:
481 return false;
482 case dwarf::DW_OP_bit_piece:
483 // Piece expressions must be at the end.
484 return I->get() + I->getSize() == E->get();
485 case dwarf::DW_OP_plus:
486 case dwarf::DW_OP_deref:
487 break;
488 }
489 }
490 return true;
491}
492
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000493bool MDExpression::isBitPiece() const {
494 assert(isValid() && "Expected valid expression");
495 if (unsigned N = getNumElements())
496 if (N >= 3)
497 return getElement(N - 3) == dwarf::DW_OP_bit_piece;
498 return false;
499}
500
501uint64_t MDExpression::getBitPieceOffset() const {
502 assert(isBitPiece() && "Expected bit piece");
503 return getElement(getNumElements() - 2);
504}
505
506uint64_t MDExpression::getBitPieceSize() const {
507 assert(isBitPiece() && "Expected bit piece");
508 return getElement(getNumElements() - 1);
509}
510
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000511MDObjCProperty *MDObjCProperty::getImpl(
512 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
513 MDString *GetterName, MDString *SetterName, unsigned Attributes,
514 Metadata *Type, StorageType Storage, bool ShouldCreate) {
515 assert(isCanonical(Name) && "Expected canonical MDString");
516 assert(isCanonical(GetterName) && "Expected canonical MDString");
517 assert(isCanonical(SetterName) && "Expected canonical MDString");
518 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
519 (getString(Name), File, Line, getString(GetterName),
520 getString(SetterName), Attributes, Type));
521 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
522 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
523}
524
525MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
526 Metadata *Scope, Metadata *Entity,
527 unsigned Line, MDString *Name,
528 StorageType Storage,
529 bool ShouldCreate) {
530 assert(isCanonical(Name) && "Expected canonical MDString");
531 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
532 (Tag, Scope, Entity, Line, getString(Name)));
533 Metadata *Ops[] = {Scope, Entity, Name};
534 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);
535}