1 //===- 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"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/IR/Function.h"
19 
20 using namespace llvm;
21 
MDLocation(LLVMContext & C,StorageType Storage,unsigned Line,unsigned Column,ArrayRef<Metadata * > MDs)22 MDLocation::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.
29   assert(Column < (1u << 16) && "Expected 16-bit column");
30 
31   SubclassData32 = Line;
32   SubclassData16 = Column;
33 }
34 
adjustColumn(unsigned & Column)35 static 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 
getImpl(LLVMContext & Context,unsigned Line,unsigned Column,Metadata * Scope,Metadata * InlinedAt,StorageType Storage,bool ShouldCreate)41 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
42                                 unsigned Column, Metadata *Scope,
43                                 Metadata *InlinedAt, StorageType Storage,
44                                 bool ShouldCreate) {
45   // Fixup column.
46   adjustColumn(Column);
47 
48   assert(Scope && "Expected scope");
49   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 
computeNewDiscriminator() const69 unsigned MDLocation::computeNewDiscriminator() const {
70   // 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 
getFlag(StringRef Flag)86 unsigned DebugNode::getFlag(StringRef Flag) {
87   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 
getFlagString(unsigned Flag)93 const char *DebugNode::getFlagString(unsigned Flag) {
94   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 
splitFlags(unsigned Flags,SmallVectorImpl<unsigned> & SplitFlags)104 unsigned DebugNode::splitFlags(unsigned Flags,
105                                SmallVectorImpl<unsigned> &SplitFlags) {
106   // 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 
getScope() const128 MDScopeRef MDScope::getScope() const {
129   if (auto *T = dyn_cast<MDType>(this))
130     return T->getScope();
131 
132   if (auto *SP = dyn_cast<MDSubprogram>(this))
133     return SP->getScope();
134 
135   if (auto *LB = dyn_cast<MDLexicalBlockBase>(this))
136     return MDScopeRef(LB->getScope());
137 
138   if (auto *NS = dyn_cast<MDNamespace>(this))
139     return MDScopeRef(NS->getScope());
140 
141   assert((isa<MDFile>(this) || isa<MDCompileUnit>(this)) &&
142          "Unhandled type of scope.");
143   return nullptr;
144 }
145 
getName() const146 StringRef MDScope::getName() const {
147   if (auto *T = dyn_cast<MDType>(this))
148     return T->getName();
149   if (auto *SP = dyn_cast<MDSubprogram>(this))
150     return SP->getName();
151   if (auto *NS = dyn_cast<MDNamespace>(this))
152     return NS->getName();
153   assert((isa<MDLexicalBlockBase>(this) || isa<MDFile>(this) ||
154           isa<MDCompileUnit>(this)) &&
155          "Unhandled type of scope.");
156   return "";
157 }
158 
getString(const MDString * S)159 static StringRef getString(const MDString *S) {
160   if (S)
161     return S->getString();
162   return StringRef();
163 }
164 
165 #ifndef NDEBUG
isCanonical(const MDString * S)166 static bool isCanonical(const MDString *S) {
167   return !S || !S->getString().empty();
168 }
169 #endif
170 
getImpl(LLVMContext & Context,unsigned Tag,MDString * Header,ArrayRef<Metadata * > DwarfOps,StorageType Storage,bool ShouldCreate)171 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
172                                             MDString *Header,
173                                             ArrayRef<Metadata *> DwarfOps,
174                                             StorageType Storage,
175                                             bool ShouldCreate) {
176   unsigned Hash = 0;
177   if (Storage == Uniqued) {
178     GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
179     if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
180       return N;
181     if (!ShouldCreate)
182       return nullptr;
183     Hash = Key.getHash();
184   } else {
185     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
186   }
187 
188   // Use a nullptr for empty headers.
189   assert(isCanonical(Header) && "Expected canonical MDString");
190   Metadata *PreOps[] = {Header};
191   return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
192                        Context, Storage, Hash, Tag, PreOps, DwarfOps),
193                    Storage, Context.pImpl->GenericDebugNodes);
194 }
195 
recalculateHash()196 void GenericDebugNode::recalculateHash() {
197   setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
198 }
199 
200 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
201 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
202 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS)                                     \
203   do {                                                                         \
204     if (Storage == Uniqued) {                                                  \
205       if (auto *N = getUniqued(Context.pImpl->CLASS##s,                        \
206                                CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS))))         \
207         return N;                                                              \
208       if (!ShouldCreate)                                                       \
209         return nullptr;                                                        \
210     } else {                                                                   \
211       assert(ShouldCreate &&                                                   \
212              "Expected non-uniqued nodes to always be created");               \
213     }                                                                          \
214   } while (false)
215 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS)                                 \
216   return storeImpl(new (ArrayRef<Metadata *>(OPS).size())                      \
217                        CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
218                    Storage, Context.pImpl->CLASS##s)
219 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS)                               \
220   return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)),        \
221                    Storage, Context.pImpl->CLASS##s)
222 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS)                   \
223   return storeImpl(new (ArrayRef<Metadata *>(OPS).size())                      \
224                        CLASS(Context, Storage, OPS),                           \
225                    Storage, Context.pImpl->CLASS##s)
226 
getImpl(LLVMContext & Context,int64_t Count,int64_t Lo,StorageType Storage,bool ShouldCreate)227 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
228                                 StorageType Storage, bool ShouldCreate) {
229   DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
230   DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
231 }
232 
getImpl(LLVMContext & Context,int64_t Value,MDString * Name,StorageType Storage,bool ShouldCreate)233 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
234                                     MDString *Name, StorageType Storage,
235                                     bool ShouldCreate) {
236   assert(isCanonical(Name) && "Expected canonical MDString");
237   DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
238   Metadata *Ops[] = {Name};
239   DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
240 }
241 
getImpl(LLVMContext & Context,unsigned Tag,MDString * Name,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Encoding,StorageType Storage,bool ShouldCreate)242 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
243                                   MDString *Name, uint64_t SizeInBits,
244                                   uint64_t AlignInBits, unsigned Encoding,
245                                   StorageType Storage, bool ShouldCreate) {
246   assert(isCanonical(Name) && "Expected canonical MDString");
247   DEFINE_GETIMPL_LOOKUP(
248       MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
249   Metadata *Ops[] = {nullptr, nullptr, Name};
250   DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
251                        Ops);
252 }
253 
getImpl(LLVMContext & Context,unsigned Tag,MDString * Name,Metadata * File,unsigned Line,Metadata * Scope,Metadata * BaseType,uint64_t SizeInBits,uint64_t AlignInBits,uint64_t OffsetInBits,unsigned Flags,Metadata * ExtraData,StorageType Storage,bool ShouldCreate)254 MDDerivedType *MDDerivedType::getImpl(
255     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
256     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
257     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
258     Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
259   assert(isCanonical(Name) && "Expected canonical MDString");
260   DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
261                                         BaseType, SizeInBits, AlignInBits,
262                                         OffsetInBits, Flags, ExtraData));
263   Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
264   DEFINE_GETIMPL_STORE(
265       MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
266       Ops);
267 }
268 
getImpl(LLVMContext & Context,unsigned Tag,MDString * Name,Metadata * File,unsigned Line,Metadata * Scope,Metadata * BaseType,uint64_t SizeInBits,uint64_t AlignInBits,uint64_t OffsetInBits,unsigned Flags,Metadata * Elements,unsigned RuntimeLang,Metadata * VTableHolder,Metadata * TemplateParams,MDString * Identifier,StorageType Storage,bool ShouldCreate)269 MDCompositeType *MDCompositeType::getImpl(
270     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
271     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
272     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
273     Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
274     Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
275     bool ShouldCreate) {
276   assert(isCanonical(Name) && "Expected canonical MDString");
277   DEFINE_GETIMPL_LOOKUP(MDCompositeType,
278                         (Tag, getString(Name), File, Line, Scope, BaseType,
279                          SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
280                          RuntimeLang, VTableHolder, TemplateParams,
281                          getString(Identifier)));
282   Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
283                      Elements, VTableHolder, TemplateParams, Identifier};
284   DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
285                                          AlignInBits, OffsetInBits, Flags),
286                        Ops);
287 }
288 
getImpl(LLVMContext & Context,unsigned Flags,Metadata * TypeArray,StorageType Storage,bool ShouldCreate)289 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
290                                             unsigned Flags, Metadata *TypeArray,
291                                             StorageType Storage,
292                                             bool ShouldCreate) {
293   DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
294   Metadata *Ops[] = {nullptr,   nullptr, nullptr, nullptr,
295                      TypeArray, nullptr, nullptr, nullptr};
296   DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
297 }
298 
getImpl(LLVMContext & Context,MDString * Filename,MDString * Directory,StorageType Storage,bool ShouldCreate)299 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
300                         MDString *Directory, StorageType Storage,
301                         bool ShouldCreate) {
302   assert(isCanonical(Filename) && "Expected canonical MDString");
303   assert(isCanonical(Directory) && "Expected canonical MDString");
304   DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
305   Metadata *Ops[] = {Filename, Directory};
306   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops);
307 }
308 
getImpl(LLVMContext & Context,unsigned SourceLanguage,Metadata * File,MDString * Producer,bool IsOptimized,MDString * Flags,unsigned RuntimeVersion,MDString * SplitDebugFilename,unsigned EmissionKind,Metadata * EnumTypes,Metadata * RetainedTypes,Metadata * Subprograms,Metadata * GlobalVariables,Metadata * ImportedEntities,StorageType Storage,bool ShouldCreate)309 MDCompileUnit *MDCompileUnit::getImpl(
310     LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
311     MDString *Producer, bool IsOptimized, MDString *Flags,
312     unsigned RuntimeVersion, MDString *SplitDebugFilename,
313     unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
314     Metadata *Subprograms, Metadata *GlobalVariables,
315     Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
316   assert(isCanonical(Producer) && "Expected canonical MDString");
317   assert(isCanonical(Flags) && "Expected canonical MDString");
318   assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
319   DEFINE_GETIMPL_LOOKUP(
320       MDCompileUnit,
321       (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
322        RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
323        RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
324   Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
325                      RetainedTypes, Subprograms, GlobalVariables,
326                      ImportedEntities};
327   DEFINE_GETIMPL_STORE(
328       MDCompileUnit,
329       (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
330 }
331 
getSubprogram() const332 MDSubprogram *MDLocalScope::getSubprogram() const {
333   if (auto *Block = dyn_cast<MDLexicalBlockBase>(this))
334     return Block->getScope()->getSubprogram();
335   return const_cast<MDSubprogram *>(cast<MDSubprogram>(this));
336 }
337 
getImpl(LLVMContext & Context,Metadata * Scope,MDString * Name,MDString * LinkageName,Metadata * File,unsigned Line,Metadata * Type,bool IsLocalToUnit,bool IsDefinition,unsigned ScopeLine,Metadata * ContainingType,unsigned Virtuality,unsigned VirtualIndex,unsigned Flags,bool IsOptimized,Metadata * Function,Metadata * TemplateParams,Metadata * Declaration,Metadata * Variables,StorageType Storage,bool ShouldCreate)338 MDSubprogram *MDSubprogram::getImpl(
339     LLVMContext &Context, Metadata *Scope, MDString *Name,
340     MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
341     bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
342     Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
343     unsigned Flags, bool IsOptimized, Metadata *Function,
344     Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
345     StorageType Storage, bool ShouldCreate) {
346   assert(isCanonical(Name) && "Expected canonical MDString");
347   assert(isCanonical(LinkageName) && "Expected canonical MDString");
348   DEFINE_GETIMPL_LOOKUP(MDSubprogram,
349                         (Scope, getString(Name), getString(LinkageName), File,
350                          Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
351                          ContainingType, Virtuality, VirtualIndex, Flags,
352                          IsOptimized, Function, TemplateParams, Declaration,
353                          Variables));
354   Metadata *Ops[] = {File,           Scope,       Name,           Name,
355                      LinkageName,    Type,        ContainingType, Function,
356                      TemplateParams, Declaration, Variables};
357   DEFINE_GETIMPL_STORE(MDSubprogram,
358                        (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
359                         IsLocalToUnit, IsDefinition, IsOptimized),
360                        Ops);
361 }
362 
getFunction() const363 Function *MDSubprogram::getFunction() const {
364   // FIXME: Should this be looking through bitcasts?
365   return dyn_cast_or_null<Function>(getFunctionConstant());
366 }
367 
describes(const Function * F) const368 bool MDSubprogram::describes(const Function *F) const {
369   assert(F && "Invalid function");
370   if (F == getFunction())
371     return true;
372   StringRef Name = getLinkageName();
373   if (Name.empty())
374     Name = getName();
375   return F->getName() == Name;
376 }
377 
replaceFunction(Function * F)378 void MDSubprogram::replaceFunction(Function *F) {
379   replaceFunction(F ? ConstantAsMetadata::get(F)
380                     : static_cast<ConstantAsMetadata *>(nullptr));
381 }
382 
getImpl(LLVMContext & Context,Metadata * Scope,Metadata * File,unsigned Line,unsigned Column,StorageType Storage,bool ShouldCreate)383 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
384                                         Metadata *File, unsigned Line,
385                                         unsigned Column, StorageType Storage,
386                                         bool ShouldCreate) {
387   assert(Scope && "Expected scope");
388   DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
389   Metadata *Ops[] = {File, Scope};
390   DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
391 }
392 
getImpl(LLVMContext & Context,Metadata * Scope,Metadata * File,unsigned Discriminator,StorageType Storage,bool ShouldCreate)393 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
394                                                 Metadata *Scope, Metadata *File,
395                                                 unsigned Discriminator,
396                                                 StorageType Storage,
397                                                 bool ShouldCreate) {
398   assert(Scope && "Expected scope");
399   DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
400   Metadata *Ops[] = {File, Scope};
401   DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
402 }
403 
getImpl(LLVMContext & Context,Metadata * Scope,Metadata * File,MDString * Name,unsigned Line,StorageType Storage,bool ShouldCreate)404 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
405                                   Metadata *File, MDString *Name, unsigned Line,
406                                   StorageType Storage, bool ShouldCreate) {
407   assert(isCanonical(Name) && "Expected canonical MDString");
408   DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
409   Metadata *Ops[] = {File, Scope, Name};
410   DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
411 }
412 
getImpl(LLVMContext & Context,MDString * Name,Metadata * Type,StorageType Storage,bool ShouldCreate)413 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
414                                                           MDString *Name,
415                                                           Metadata *Type,
416                                                           StorageType Storage,
417                                                           bool ShouldCreate) {
418   assert(isCanonical(Name) && "Expected canonical MDString");
419   DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
420   Metadata *Ops[] = {Name, Type};
421   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
422 }
423 
getImpl(LLVMContext & Context,unsigned Tag,MDString * Name,Metadata * Type,Metadata * Value,StorageType Storage,bool ShouldCreate)424 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
425     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
426     Metadata *Value, StorageType Storage, bool ShouldCreate) {
427   assert(isCanonical(Name) && "Expected canonical MDString");
428   DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
429                         (Tag, getString(Name), Type, Value));
430   Metadata *Ops[] = {Name, Type, Value};
431   DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
432 }
433 
434 MDGlobalVariable *
getImpl(LLVMContext & Context,Metadata * Scope,MDString * Name,MDString * LinkageName,Metadata * File,unsigned Line,Metadata * Type,bool IsLocalToUnit,bool IsDefinition,Metadata * Variable,Metadata * StaticDataMemberDeclaration,StorageType Storage,bool ShouldCreate)435 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
436                           MDString *LinkageName, Metadata *File, unsigned Line,
437                           Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
438                           Metadata *Variable,
439                           Metadata *StaticDataMemberDeclaration,
440                           StorageType Storage, bool ShouldCreate) {
441   assert(isCanonical(Name) && "Expected canonical MDString");
442   assert(isCanonical(LinkageName) && "Expected canonical MDString");
443   DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
444                         (Scope, getString(Name), getString(LinkageName), File,
445                          Line, Type, IsLocalToUnit, IsDefinition, Variable,
446                          StaticDataMemberDeclaration));
447   Metadata *Ops[] = {Scope, Name,        File,     Type,
448                      Name,  LinkageName, Variable, StaticDataMemberDeclaration};
449   DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
450                        Ops);
451 }
452 
getImpl(LLVMContext & Context,unsigned Tag,Metadata * Scope,MDString * Name,Metadata * File,unsigned Line,Metadata * Type,unsigned Arg,unsigned Flags,StorageType Storage,bool ShouldCreate)453 MDLocalVariable *MDLocalVariable::getImpl(LLVMContext &Context, unsigned Tag,
454                                           Metadata *Scope, MDString *Name,
455                                           Metadata *File, unsigned Line,
456                                           Metadata *Type, unsigned Arg,
457                                           unsigned Flags, StorageType Storage,
458                                           bool ShouldCreate) {
459   // Truncate Arg to 8 bits.
460   //
461   // FIXME: This is gross (and should be changed to an assert or removed), but
462   // it matches historical behaviour for now.
463   Arg &= (1u << 8) - 1;
464 
465   assert(Scope && "Expected scope");
466   assert(isCanonical(Name) && "Expected canonical MDString");
467   DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
468                                           Line, Type, Arg, Flags));
469   Metadata *Ops[] = {Scope, Name, File, Type};
470   DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
471 }
472 
getImpl(LLVMContext & Context,ArrayRef<uint64_t> Elements,StorageType Storage,bool ShouldCreate)473 MDExpression *MDExpression::getImpl(LLVMContext &Context,
474                                     ArrayRef<uint64_t> Elements,
475                                     StorageType Storage, bool ShouldCreate) {
476   DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
477   DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
478 }
479 
getSize() const480 unsigned MDExpression::ExprOperand::getSize() const {
481   switch (getOp()) {
482   case dwarf::DW_OP_bit_piece:
483     return 3;
484   case dwarf::DW_OP_plus:
485     return 2;
486   default:
487     return 1;
488   }
489 }
490 
isValid() const491 bool MDExpression::isValid() const {
492   for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
493     // Check that there's space for the operand.
494     if (I->get() + I->getSize() > E->get())
495       return false;
496 
497     // Check that the operand is valid.
498     switch (I->getOp()) {
499     default:
500       return false;
501     case dwarf::DW_OP_bit_piece:
502       // Piece expressions must be at the end.
503       return I->get() + I->getSize() == E->get();
504     case dwarf::DW_OP_plus:
505     case dwarf::DW_OP_deref:
506       break;
507     }
508   }
509   return true;
510 }
511 
isBitPiece() const512 bool MDExpression::isBitPiece() const {
513   assert(isValid() && "Expected valid expression");
514   if (unsigned N = getNumElements())
515     if (N >= 3)
516       return getElement(N - 3) == dwarf::DW_OP_bit_piece;
517   return false;
518 }
519 
getBitPieceOffset() const520 uint64_t MDExpression::getBitPieceOffset() const {
521   assert(isBitPiece() && "Expected bit piece");
522   return getElement(getNumElements() - 2);
523 }
524 
getBitPieceSize() const525 uint64_t MDExpression::getBitPieceSize() const {
526   assert(isBitPiece() && "Expected bit piece");
527   return getElement(getNumElements() - 1);
528 }
529 
getImpl(LLVMContext & Context,MDString * Name,Metadata * File,unsigned Line,MDString * GetterName,MDString * SetterName,unsigned Attributes,Metadata * Type,StorageType Storage,bool ShouldCreate)530 MDObjCProperty *MDObjCProperty::getImpl(
531     LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
532     MDString *GetterName, MDString *SetterName, unsigned Attributes,
533     Metadata *Type, StorageType Storage, bool ShouldCreate) {
534   assert(isCanonical(Name) && "Expected canonical MDString");
535   assert(isCanonical(GetterName) && "Expected canonical MDString");
536   assert(isCanonical(SetterName) && "Expected canonical MDString");
537   DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
538                         (getString(Name), File, Line, getString(GetterName),
539                          getString(SetterName), Attributes, Type));
540   Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
541   DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
542 }
543 
getImpl(LLVMContext & Context,unsigned Tag,Metadata * Scope,Metadata * Entity,unsigned Line,MDString * Name,StorageType Storage,bool ShouldCreate)544 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
545                                             Metadata *Scope, Metadata *Entity,
546                                             unsigned Line, MDString *Name,
547                                             StorageType Storage,
548                                             bool ShouldCreate) {
549   assert(isCanonical(Name) && "Expected canonical MDString");
550   DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
551                         (Tag, Scope, Entity, Line, getString(Name)));
552   Metadata *Ops[] = {Scope, Entity, Name};
553   DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);
554 }
555