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