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