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 
22 DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
23                        unsigned Column, ArrayRef<Metadata *> MDs)
24     : MDNode(C, DILocationKind, 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 
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 
41 DILocation *DILocation::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   if (Storage == Uniqued) {
49     if (auto *N =
50             getUniqued(Context.pImpl->DILocations,
51                        DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
52       return N;
53     if (!ShouldCreate)
54       return nullptr;
55   } else {
56     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
57   }
58 
59   SmallVector<Metadata *, 2> Ops;
60   Ops.push_back(Scope);
61   if (InlinedAt)
62     Ops.push_back(InlinedAt);
63   return storeImpl(new (Ops.size())
64                        DILocation(Context, Storage, Line, Column, Ops),
65                    Storage, Context.pImpl->DILocations);
66 }
67 
68 DINode::DIFlags DINode::getFlag(StringRef Flag) {
69   return StringSwitch<DIFlags>(Flag)
70 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
71 #include "llvm/IR/DebugInfoFlags.def"
72       .Default(DINode::FlagZero);
73 }
74 
75 StringRef DINode::getFlagString(DIFlags Flag) {
76   switch (Flag) {
77 #define HANDLE_DI_FLAG(ID, NAME)                                               \
78   case Flag##NAME:                                                             \
79     return "DIFlag" #NAME;
80 #include "llvm/IR/DebugInfoFlags.def"
81   }
82   return "";
83 }
84 
85 DINode::DIFlags DINode::splitFlags(DIFlags Flags,
86                                    SmallVectorImpl<DIFlags> &SplitFlags) {
87   // Accessibility and member pointer flags need to be specially handled, since
88   // they're packed together.
89   if (DIFlags A = Flags & FlagAccessibility) {
90     if (A == FlagPrivate)
91       SplitFlags.push_back(FlagPrivate);
92     else if (A == FlagProtected)
93       SplitFlags.push_back(FlagProtected);
94     else
95       SplitFlags.push_back(FlagPublic);
96     Flags &= ~A;
97   }
98   if (DIFlags R = Flags & FlagPtrToMemberRep) {
99     if (R == FlagSingleInheritance)
100       SplitFlags.push_back(FlagSingleInheritance);
101     else if (R == FlagMultipleInheritance)
102       SplitFlags.push_back(FlagMultipleInheritance);
103     else
104       SplitFlags.push_back(FlagVirtualInheritance);
105     Flags &= ~R;
106   }
107 
108 #define HANDLE_DI_FLAG(ID, NAME)                                               \
109   if (DIFlags Bit = Flags & Flag##NAME) {                                      \
110     SplitFlags.push_back(Bit);                                                 \
111     Flags &= ~Bit;                                                             \
112   }
113 #include "llvm/IR/DebugInfoFlags.def"
114   return Flags;
115 }
116 
117 DIScopeRef DIScope::getScope() const {
118   if (auto *T = dyn_cast<DIType>(this))
119     return T->getScope();
120 
121   if (auto *SP = dyn_cast<DISubprogram>(this))
122     return SP->getScope();
123 
124   if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
125     return LB->getScope();
126 
127   if (auto *NS = dyn_cast<DINamespace>(this))
128     return NS->getScope();
129 
130   if (auto *M = dyn_cast<DIModule>(this))
131     return M->getScope();
132 
133   assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
134          "Unhandled type of scope.");
135   return nullptr;
136 }
137 
138 StringRef DIScope::getName() const {
139   if (auto *T = dyn_cast<DIType>(this))
140     return T->getName();
141   if (auto *SP = dyn_cast<DISubprogram>(this))
142     return SP->getName();
143   if (auto *NS = dyn_cast<DINamespace>(this))
144     return NS->getName();
145   if (auto *M = dyn_cast<DIModule>(this))
146     return M->getName();
147   assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
148           isa<DICompileUnit>(this)) &&
149          "Unhandled type of scope.");
150   return "";
151 }
152 
153 #ifndef NDEBUG
154 static bool isCanonical(const MDString *S) {
155   return !S || !S->getString().empty();
156 }
157 #endif
158 
159 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
160                                       MDString *Header,
161                                       ArrayRef<Metadata *> DwarfOps,
162                                       StorageType Storage, bool ShouldCreate) {
163   unsigned Hash = 0;
164   if (Storage == Uniqued) {
165     GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
166     if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
167       return N;
168     if (!ShouldCreate)
169       return nullptr;
170     Hash = Key.getHash();
171   } else {
172     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
173   }
174 
175   // Use a nullptr for empty headers.
176   assert(isCanonical(Header) && "Expected canonical MDString");
177   Metadata *PreOps[] = {Header};
178   return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
179                        Context, Storage, Hash, Tag, PreOps, DwarfOps),
180                    Storage, Context.pImpl->GenericDINodes);
181 }
182 
183 void GenericDINode::recalculateHash() {
184   setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
185 }
186 
187 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
188 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
189 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS)                                     \
190   do {                                                                         \
191     if (Storage == Uniqued) {                                                  \
192       if (auto *N = getUniqued(Context.pImpl->CLASS##s,                        \
193                                CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS))))         \
194         return N;                                                              \
195       if (!ShouldCreate)                                                       \
196         return nullptr;                                                        \
197     } else {                                                                   \
198       assert(ShouldCreate &&                                                   \
199              "Expected non-uniqued nodes to always be created");               \
200     }                                                                          \
201   } while (false)
202 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS)                                 \
203   return storeImpl(new (array_lengthof(OPS))                                   \
204                        CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
205                    Storage, Context.pImpl->CLASS##s)
206 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS)                               \
207   return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)),        \
208                    Storage, Context.pImpl->CLASS##s)
209 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS)                   \
210   return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS),     \
211                    Storage, Context.pImpl->CLASS##s)
212 
213 DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
214                                 StorageType Storage, bool ShouldCreate) {
215   DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
216   DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
217 }
218 
219 DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
220                                     MDString *Name, StorageType Storage,
221                                     bool ShouldCreate) {
222   assert(isCanonical(Name) && "Expected canonical MDString");
223   DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, Name));
224   Metadata *Ops[] = {Name};
225   DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
226 }
227 
228 DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
229                                   MDString *Name, uint64_t SizeInBits,
230                                   uint64_t AlignInBits, unsigned Encoding,
231                                   StorageType Storage, bool ShouldCreate) {
232   assert(isCanonical(Name) && "Expected canonical MDString");
233   DEFINE_GETIMPL_LOOKUP(DIBasicType,
234                         (Tag, Name, SizeInBits, AlignInBits, Encoding));
235   Metadata *Ops[] = {nullptr, nullptr, Name};
236   DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
237                        Ops);
238 }
239 
240 DIDerivedType *DIDerivedType::getImpl(
241     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
242     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
243     uint64_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
244     Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
245   assert(isCanonical(Name) && "Expected canonical MDString");
246   DEFINE_GETIMPL_LOOKUP(DIDerivedType,
247                         (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
248                          AlignInBits, OffsetInBits, Flags, ExtraData));
249   Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
250   DEFINE_GETIMPL_STORE(
251       DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
252       Ops);
253 }
254 
255 DICompositeType *DICompositeType::getImpl(
256     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
257     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
258     uint64_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
259     Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
260     Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
261     bool ShouldCreate) {
262   assert(isCanonical(Name) && "Expected canonical MDString");
263 
264   // Keep this in sync with buildODRType.
265   DEFINE_GETIMPL_LOOKUP(
266       DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
267                         AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
268                         VTableHolder, TemplateParams, Identifier));
269   Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
270                      Elements, VTableHolder, TemplateParams, Identifier};
271   DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
272                                          AlignInBits, OffsetInBits, Flags),
273                        Ops);
274 }
275 
276 DICompositeType *DICompositeType::buildODRType(
277     LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
278     Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
279     uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
280     DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
281     Metadata *VTableHolder, Metadata *TemplateParams) {
282   assert(!Identifier.getString().empty() && "Expected valid identifier");
283   if (!Context.isODRUniquingDebugTypes())
284     return nullptr;
285   auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
286   if (!CT)
287     return CT = DICompositeType::getDistinct(
288                Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
289                AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
290                VTableHolder, TemplateParams, &Identifier);
291 
292   // Only mutate CT if it's a forward declaration and the new operands aren't.
293   assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
294   if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl))
295     return CT;
296 
297   // Mutate CT in place.  Keep this in sync with getImpl.
298   CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
299              Flags);
300   Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
301                      Elements, VTableHolder, TemplateParams, &Identifier};
302   assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() &&
303          "Mismatched number of operands");
304   for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I)
305     if (Ops[I] != CT->getOperand(I))
306       CT->setOperand(I, Ops[I]);
307   return CT;
308 }
309 
310 DICompositeType *DICompositeType::getODRType(
311     LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
312     Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
313     uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
314     DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
315     Metadata *VTableHolder, Metadata *TemplateParams) {
316   assert(!Identifier.getString().empty() && "Expected valid identifier");
317   if (!Context.isODRUniquingDebugTypes())
318     return nullptr;
319   auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
320   if (!CT)
321     CT = DICompositeType::getDistinct(
322         Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
323         AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
324         TemplateParams, &Identifier);
325   return CT;
326 }
327 
328 DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context,
329                                                      MDString &Identifier) {
330   assert(!Identifier.getString().empty() && "Expected valid identifier");
331   if (!Context.isODRUniquingDebugTypes())
332     return nullptr;
333   return Context.pImpl->DITypeMap->lookup(&Identifier);
334 }
335 
336 DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,
337                                             uint8_t CC, Metadata *TypeArray,
338                                             StorageType Storage,
339                                             bool ShouldCreate) {
340   DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray));
341   Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
342   DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops);
343 }
344 
345 DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
346                         MDString *Directory, StorageType Storage,
347                         bool ShouldCreate) {
348   assert(isCanonical(Filename) && "Expected canonical MDString");
349   assert(isCanonical(Directory) && "Expected canonical MDString");
350   DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory));
351   Metadata *Ops[] = {Filename, Directory};
352   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIFile, Ops);
353 }
354 
355 DICompileUnit *DICompileUnit::getImpl(
356     LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
357     MDString *Producer, bool IsOptimized, MDString *Flags,
358     unsigned RuntimeVersion, MDString *SplitDebugFilename,
359     unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
360     Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
361     uint64_t DWOId, bool SplitDebugInlining, StorageType Storage,
362     bool ShouldCreate) {
363   assert(Storage != Uniqued && "Cannot unique DICompileUnit");
364   assert(isCanonical(Producer) && "Expected canonical MDString");
365   assert(isCanonical(Flags) && "Expected canonical MDString");
366   assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
367 
368   Metadata *Ops[] = {
369       File,      Producer,      Flags,           SplitDebugFilename,
370       EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities,
371       Macros};
372   return storeImpl(new (array_lengthof(Ops))
373                        DICompileUnit(Context, Storage, SourceLanguage,
374                                      IsOptimized, RuntimeVersion, EmissionKind,
375                                      DWOId, SplitDebugInlining, Ops),
376                    Storage);
377 }
378 
379 Optional<DICompileUnit::DebugEmissionKind>
380 DICompileUnit::getEmissionKind(StringRef Str) {
381   return StringSwitch<Optional<DebugEmissionKind>>(Str)
382       .Case("NoDebug", NoDebug)
383       .Case("FullDebug", FullDebug)
384       .Case("LineTablesOnly", LineTablesOnly)
385       .Default(None);
386 }
387 
388 const char *DICompileUnit::EmissionKindString(DebugEmissionKind EK) {
389   switch (EK) {
390   case NoDebug:        return "NoDebug";
391   case FullDebug:      return "FullDebug";
392   case LineTablesOnly: return "LineTablesOnly";
393   }
394   return nullptr;
395 }
396 
397 DISubprogram *DILocalScope::getSubprogram() const {
398   if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
399     return Block->getScope()->getSubprogram();
400   return const_cast<DISubprogram *>(cast<DISubprogram>(this));
401 }
402 
403 DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const {
404   if (auto *File = dyn_cast<DILexicalBlockFile>(this))
405     return File->getScope()->getNonLexicalBlockFileScope();
406   return const_cast<DILocalScope *>(this);
407 }
408 
409 DISubprogram *DISubprogram::getImpl(
410     LLVMContext &Context, Metadata *Scope, MDString *Name,
411     MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
412     bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
413     Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
414     int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
415     Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
416     StorageType Storage, bool ShouldCreate) {
417   assert(isCanonical(Name) && "Expected canonical MDString");
418   assert(isCanonical(LinkageName) && "Expected canonical MDString");
419   DEFINE_GETIMPL_LOOKUP(
420       DISubprogram,
421       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
422        ScopeLine, ContainingType, Virtuality, VirtualIndex, ThisAdjustment,
423        Flags, IsOptimized, Unit, TemplateParams, Declaration, Variables));
424   Metadata *Ops[] = {File,           Scope,       Name,           Name,
425                      LinkageName,    Type,        ContainingType, Unit,
426                      TemplateParams, Declaration, Variables};
427   DEFINE_GETIMPL_STORE(DISubprogram, (Line, ScopeLine, Virtuality, VirtualIndex,
428                                       ThisAdjustment, Flags, IsLocalToUnit,
429                                       IsDefinition, IsOptimized),
430                        Ops);
431 }
432 
433 bool DISubprogram::describes(const Function *F) const {
434   assert(F && "Invalid function");
435   if (F->getSubprogram() == this)
436     return true;
437   StringRef Name = getLinkageName();
438   if (Name.empty())
439     Name = getName();
440   return F->getName() == Name;
441 }
442 
443 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
444                                         Metadata *File, unsigned Line,
445                                         unsigned Column, StorageType Storage,
446                                         bool ShouldCreate) {
447   // Fixup column.
448   adjustColumn(Column);
449 
450   assert(Scope && "Expected scope");
451   DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
452   Metadata *Ops[] = {File, Scope};
453   DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
454 }
455 
456 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
457                                                 Metadata *Scope, Metadata *File,
458                                                 unsigned Discriminator,
459                                                 StorageType Storage,
460                                                 bool ShouldCreate) {
461   assert(Scope && "Expected scope");
462   DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
463   Metadata *Ops[] = {File, Scope};
464   DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
465 }
466 
467 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
468                                   Metadata *File, MDString *Name, unsigned Line,
469                                   StorageType Storage, bool ShouldCreate) {
470   assert(isCanonical(Name) && "Expected canonical MDString");
471   DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, Name, Line));
472   Metadata *Ops[] = {File, Scope, Name};
473   DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
474 }
475 
476 DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
477                             MDString *Name, MDString *ConfigurationMacros,
478                             MDString *IncludePath, MDString *ISysRoot,
479                             StorageType Storage, bool ShouldCreate) {
480   assert(isCanonical(Name) && "Expected canonical MDString");
481   DEFINE_GETIMPL_LOOKUP(
482       DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
483   Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
484   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
485 }
486 
487 DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
488                                                           MDString *Name,
489                                                           Metadata *Type,
490                                                           StorageType Storage,
491                                                           bool ShouldCreate) {
492   assert(isCanonical(Name) && "Expected canonical MDString");
493   DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type));
494   Metadata *Ops[] = {Name, Type};
495   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
496 }
497 
498 DITemplateValueParameter *DITemplateValueParameter::getImpl(
499     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
500     Metadata *Value, StorageType Storage, bool ShouldCreate) {
501   assert(isCanonical(Name) && "Expected canonical MDString");
502   DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
503   Metadata *Ops[] = {Name, Type, Value};
504   DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
505 }
506 
507 DIGlobalVariable *
508 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
509                           MDString *LinkageName, Metadata *File, unsigned Line,
510                           Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
511                           Metadata *Variable,
512                           Metadata *StaticDataMemberDeclaration,
513                           StorageType Storage, bool ShouldCreate) {
514   assert(isCanonical(Name) && "Expected canonical MDString");
515   assert(isCanonical(LinkageName) && "Expected canonical MDString");
516   DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
517                         (Scope, Name, LinkageName, File, Line, Type,
518                          IsLocalToUnit, IsDefinition, Variable,
519                          StaticDataMemberDeclaration));
520   Metadata *Ops[] = {Scope, Name,        File,     Type,
521                      Name,  LinkageName, Variable, StaticDataMemberDeclaration};
522   DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
523                        Ops);
524 }
525 
526 DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
527                                           MDString *Name, Metadata *File,
528                                           unsigned Line, Metadata *Type,
529                                           unsigned Arg, DIFlags Flags,
530                                           StorageType Storage,
531                                           bool ShouldCreate) {
532   // 64K ought to be enough for any frontend.
533   assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
534 
535   assert(Scope && "Expected scope");
536   assert(isCanonical(Name) && "Expected canonical MDString");
537   DEFINE_GETIMPL_LOOKUP(DILocalVariable,
538                         (Scope, Name, File, Line, Type, Arg, Flags));
539   Metadata *Ops[] = {Scope, Name, File, Type};
540   DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags), Ops);
541 }
542 
543 DIExpression *DIExpression::getImpl(LLVMContext &Context,
544                                     ArrayRef<uint64_t> Elements,
545                                     StorageType Storage, bool ShouldCreate) {
546   DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
547   DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
548 }
549 
550 unsigned DIExpression::ExprOperand::getSize() const {
551   switch (getOp()) {
552   case dwarf::DW_OP_bit_piece:
553     return 3;
554   case dwarf::DW_OP_constu:
555   case dwarf::DW_OP_plus:
556   case dwarf::DW_OP_minus:
557     return 2;
558   default:
559     return 1;
560   }
561 }
562 
563 bool DIExpression::isValid() const {
564   for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
565     // Check that there's space for the operand.
566     if (I->get() + I->getSize() > E->get())
567       return false;
568 
569     // Check that the operand is valid.
570     switch (I->getOp()) {
571     default:
572       return false;
573     case dwarf::DW_OP_bit_piece:
574     case dwarf::DW_OP_stack_value:
575       // We only support bit piece and stack value expressions which appear at
576       // the end.
577       return I->get() + I->getSize() == E->get();
578     case dwarf::DW_OP_constu:
579     case dwarf::DW_OP_plus:
580     case dwarf::DW_OP_minus:
581     case dwarf::DW_OP_deref:
582       break;
583     }
584   }
585   return true;
586 }
587 
588 bool DIExpression::isBitPiece() const {
589   assert(isValid() && "Expected valid expression");
590   if (unsigned N = getNumElements())
591     if (N >= 3)
592       return getElement(N - 3) == dwarf::DW_OP_bit_piece;
593   return false;
594 }
595 
596 uint64_t DIExpression::getBitPieceOffset() const {
597   assert(isBitPiece() && "Expected bit piece");
598   return getElement(getNumElements() - 2);
599 }
600 
601 uint64_t DIExpression::getBitPieceSize() const {
602   assert(isBitPiece() && "Expected bit piece");
603   return getElement(getNumElements() - 1);
604 }
605 
606 DIObjCProperty *DIObjCProperty::getImpl(
607     LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
608     MDString *GetterName, MDString *SetterName, unsigned Attributes,
609     Metadata *Type, StorageType Storage, bool ShouldCreate) {
610   assert(isCanonical(Name) && "Expected canonical MDString");
611   assert(isCanonical(GetterName) && "Expected canonical MDString");
612   assert(isCanonical(SetterName) && "Expected canonical MDString");
613   DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
614                                          SetterName, Attributes, Type));
615   Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
616   DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
617 }
618 
619 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
620                                             Metadata *Scope, Metadata *Entity,
621                                             unsigned Line, MDString *Name,
622                                             StorageType Storage,
623                                             bool ShouldCreate) {
624   assert(isCanonical(Name) && "Expected canonical MDString");
625   DEFINE_GETIMPL_LOOKUP(DIImportedEntity, (Tag, Scope, Entity, Line, Name));
626   Metadata *Ops[] = {Scope, Entity, Name};
627   DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
628 }
629 
630 DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
631                           unsigned Line, MDString *Name, MDString *Value,
632                           StorageType Storage, bool ShouldCreate) {
633   assert(isCanonical(Name) && "Expected canonical MDString");
634   DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
635   Metadata *Ops[] = { Name, Value };
636   DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
637 }
638 
639 DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
640                                   unsigned Line, Metadata *File,
641                                   Metadata *Elements, StorageType Storage,
642                                   bool ShouldCreate) {
643   DEFINE_GETIMPL_LOOKUP(DIMacroFile,
644                         (MIType, Line, File, Elements));
645   Metadata *Ops[] = { File, Elements };
646   DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
647 }
648 
649