DI: Remove unnecessary DICompositeTypeBase
[oota-llvm.git] / lib / IR / DebugInfoMetadata.cpp
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   assert(Scope && "Expected scope");
49   if (Storage == Uniqued) {
50     if (auto *N =
51             getUniqued(Context.pImpl->DILocations,
52                        DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
53       return N;
54     if (!ShouldCreate)
55       return nullptr;
56   } else {
57     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
58   }
59
60   SmallVector<Metadata *, 2> Ops;
61   Ops.push_back(Scope);
62   if (InlinedAt)
63     Ops.push_back(InlinedAt);
64   return storeImpl(new (Ops.size())
65                        DILocation(Context, Storage, Line, Column, Ops),
66                    Storage, Context.pImpl->DILocations);
67 }
68
69 unsigned DILocation::computeNewDiscriminator() const {
70   // FIXME: This seems completely wrong.
71   //
72   //  1. If two modules are generated in the same context, then the second
73   //     Module will get different discriminators than it would have if it were
74   //     generated in its own context.
75   //  2. If this function is called after round-tripping to bitcode instead of
76   //     before, it will give a different (and potentially incorrect!) return.
77   //
78   // The discriminator should instead be calculated from local information
79   // where it's actually needed.  This logic should be moved to
80   // AddDiscriminators::runOnFunction(), where it doesn't pollute the
81   // LLVMContext.
82   std::pair<const char *, unsigned> Key(getFilename().data(), getLine());
83   return ++getContext().pImpl->DiscriminatorTable[Key];
84 }
85
86 unsigned DINode::getFlag(StringRef Flag) {
87   return StringSwitch<unsigned>(Flag)
88 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
89 #include "llvm/IR/DebugInfoFlags.def"
90       .Default(0);
91 }
92
93 const char *DINode::getFlagString(unsigned Flag) {
94   switch (Flag) {
95   default:
96     return "";
97 #define HANDLE_DI_FLAG(ID, NAME)                                               \
98   case Flag##NAME:                                                             \
99     return "DIFlag" #NAME;
100 #include "llvm/IR/DebugInfoFlags.def"
101   }
102 }
103
104 unsigned DINode::splitFlags(unsigned Flags,
105                             SmallVectorImpl<unsigned> &SplitFlags) {
106   // Accessibility flags need to be specially handled, since they're packed
107   // together.
108   if (unsigned A = Flags & FlagAccessibility) {
109     if (A == FlagPrivate)
110       SplitFlags.push_back(FlagPrivate);
111     else if (A == FlagProtected)
112       SplitFlags.push_back(FlagProtected);
113     else
114       SplitFlags.push_back(FlagPublic);
115     Flags &= ~A;
116   }
117
118 #define HANDLE_DI_FLAG(ID, NAME)                                               \
119   if (unsigned Bit = Flags & ID) {                                             \
120     SplitFlags.push_back(Bit);                                                 \
121     Flags &= ~Bit;                                                             \
122   }
123 #include "llvm/IR/DebugInfoFlags.def"
124
125   return Flags;
126 }
127
128 DIScopeRef DIScope::getScope() const {
129   if (auto *T = dyn_cast<DIType>(this))
130     return T->getScope();
131
132   if (auto *SP = dyn_cast<DISubprogram>(this))
133     return SP->getScope();
134
135   if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
136     return DIScopeRef(LB->getScope());
137
138   if (auto *NS = dyn_cast<DINamespace>(this))
139     return DIScopeRef(NS->getScope());
140
141   if (auto *M = dyn_cast<DIModule>(this))
142     return DIScopeRef(M->getScope());
143
144   assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
145          "Unhandled type of scope.");
146   return nullptr;
147 }
148
149 StringRef DIScope::getName() const {
150   if (auto *T = dyn_cast<DIType>(this))
151     return T->getName();
152   if (auto *SP = dyn_cast<DISubprogram>(this))
153     return SP->getName();
154   if (auto *NS = dyn_cast<DINamespace>(this))
155     return NS->getName();
156   if (auto *M = dyn_cast<DIModule>(this))
157     return M->getName();
158   assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
159           isa<DICompileUnit>(this)) &&
160          "Unhandled type of scope.");
161   return "";
162 }
163
164 static StringRef getString(const MDString *S) {
165   if (S)
166     return S->getString();
167   return StringRef();
168 }
169
170 #ifndef NDEBUG
171 static bool isCanonical(const MDString *S) {
172   return !S || !S->getString().empty();
173 }
174 #endif
175
176 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
177                                       MDString *Header,
178                                       ArrayRef<Metadata *> DwarfOps,
179                                       StorageType Storage, bool ShouldCreate) {
180   unsigned Hash = 0;
181   if (Storage == Uniqued) {
182     GenericDINodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
183     if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
184       return N;
185     if (!ShouldCreate)
186       return nullptr;
187     Hash = Key.getHash();
188   } else {
189     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
190   }
191
192   // Use a nullptr for empty headers.
193   assert(isCanonical(Header) && "Expected canonical MDString");
194   Metadata *PreOps[] = {Header};
195   return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
196                        Context, Storage, Hash, Tag, PreOps, DwarfOps),
197                    Storage, Context.pImpl->GenericDINodes);
198 }
199
200 void GenericDINode::recalculateHash() {
201   setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
202 }
203
204 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
205 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
206 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS)                                     \
207   do {                                                                         \
208     if (Storage == Uniqued) {                                                  \
209       if (auto *N = getUniqued(Context.pImpl->CLASS##s,                        \
210                                CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS))))         \
211         return N;                                                              \
212       if (!ShouldCreate)                                                       \
213         return nullptr;                                                        \
214     } else {                                                                   \
215       assert(ShouldCreate &&                                                   \
216              "Expected non-uniqued nodes to always be created");               \
217     }                                                                          \
218   } while (false)
219 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS)                                 \
220   return storeImpl(new (ArrayRef<Metadata *>(OPS).size())                      \
221                        CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
222                    Storage, Context.pImpl->CLASS##s)
223 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS)                               \
224   return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)),        \
225                    Storage, Context.pImpl->CLASS##s)
226 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS)                   \
227   return storeImpl(new (ArrayRef<Metadata *>(OPS).size())                      \
228                        CLASS(Context, Storage, OPS),                           \
229                    Storage, Context.pImpl->CLASS##s)
230
231 DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
232                                 StorageType Storage, bool ShouldCreate) {
233   DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
234   DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
235 }
236
237 DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
238                                     MDString *Name, StorageType Storage,
239                                     bool ShouldCreate) {
240   assert(isCanonical(Name) && "Expected canonical MDString");
241   DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, getString(Name)));
242   Metadata *Ops[] = {Name};
243   DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
244 }
245
246 DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
247                                   MDString *Name, uint64_t SizeInBits,
248                                   uint64_t AlignInBits, unsigned Encoding,
249                                   StorageType Storage, bool ShouldCreate) {
250   assert(isCanonical(Name) && "Expected canonical MDString");
251   DEFINE_GETIMPL_LOOKUP(
252       DIBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
253   Metadata *Ops[] = {nullptr, nullptr, Name};
254   DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
255                        Ops);
256 }
257
258 DIDerivedType *DIDerivedType::getImpl(
259     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
260     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
261     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
262     Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
263   assert(isCanonical(Name) && "Expected canonical MDString");
264   DEFINE_GETIMPL_LOOKUP(DIDerivedType, (Tag, getString(Name), File, Line, Scope,
265                                         BaseType, SizeInBits, AlignInBits,
266                                         OffsetInBits, Flags, ExtraData));
267   Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
268   DEFINE_GETIMPL_STORE(
269       DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
270       Ops);
271 }
272
273 DICompositeType *DICompositeType::getImpl(
274     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
275     unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
276     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
277     Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
278     Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
279     bool ShouldCreate) {
280   assert(isCanonical(Name) && "Expected canonical MDString");
281   DEFINE_GETIMPL_LOOKUP(DICompositeType,
282                         (Tag, getString(Name), File, Line, Scope, BaseType,
283                          SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
284                          RuntimeLang, VTableHolder, TemplateParams,
285                          getString(Identifier)));
286   Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
287                      Elements, VTableHolder, TemplateParams, Identifier};
288   DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
289                                          AlignInBits, OffsetInBits, Flags),
290                        Ops);
291 }
292
293 DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context,
294                                             unsigned Flags, Metadata *TypeArray,
295                                             StorageType Storage,
296                                             bool ShouldCreate) {
297   DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, TypeArray));
298   Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
299   DEFINE_GETIMPL_STORE(DISubroutineType, (Flags), Ops);
300 }
301
302 DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
303                         MDString *Directory, StorageType Storage,
304                         bool ShouldCreate) {
305   assert(isCanonical(Filename) && "Expected canonical MDString");
306   assert(isCanonical(Directory) && "Expected canonical MDString");
307   DEFINE_GETIMPL_LOOKUP(DIFile, (getString(Filename), getString(Directory)));
308   Metadata *Ops[] = {Filename, Directory};
309   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIFile, Ops);
310 }
311
312 DICompileUnit *DICompileUnit::getImpl(
313     LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
314     MDString *Producer, bool IsOptimized, MDString *Flags,
315     unsigned RuntimeVersion, MDString *SplitDebugFilename,
316     unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
317     Metadata *Subprograms, Metadata *GlobalVariables,
318     Metadata *ImportedEntities, uint64_t DWOId,
319     StorageType Storage, bool ShouldCreate) {
320   assert(isCanonical(Producer) && "Expected canonical MDString");
321   assert(isCanonical(Flags) && "Expected canonical MDString");
322   assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
323   DEFINE_GETIMPL_LOOKUP(
324       DICompileUnit,
325       (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
326        RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
327        RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, DWOId));
328   Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
329                      RetainedTypes, Subprograms, GlobalVariables,
330                      ImportedEntities};
331   DEFINE_GETIMPL_STORE(
332       DICompileUnit,
333       (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind, DWOId), Ops);
334 }
335
336 DISubprogram *DILocalScope::getSubprogram() const {
337   if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
338     return Block->getScope()->getSubprogram();
339   return const_cast<DISubprogram *>(cast<DISubprogram>(this));
340 }
341
342 DISubprogram *DISubprogram::getImpl(
343     LLVMContext &Context, Metadata *Scope, MDString *Name,
344     MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
345     bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
346     Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
347     unsigned Flags, bool IsOptimized, Metadata *Function,
348     Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
349     StorageType Storage, bool ShouldCreate) {
350   assert(isCanonical(Name) && "Expected canonical MDString");
351   assert(isCanonical(LinkageName) && "Expected canonical MDString");
352   DEFINE_GETIMPL_LOOKUP(DISubprogram,
353                         (Scope, getString(Name), getString(LinkageName), File,
354                          Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
355                          ContainingType, Virtuality, VirtualIndex, Flags,
356                          IsOptimized, Function, TemplateParams, Declaration,
357                          Variables));
358   Metadata *Ops[] = {File,           Scope,       Name,           Name,
359                      LinkageName,    Type,        ContainingType, Function,
360                      TemplateParams, Declaration, Variables};
361   DEFINE_GETIMPL_STORE(DISubprogram,
362                        (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
363                         IsLocalToUnit, IsDefinition, IsOptimized),
364                        Ops);
365 }
366
367 Function *DISubprogram::getFunction() const {
368   // FIXME: Should this be looking through bitcasts?
369   return dyn_cast_or_null<Function>(getFunctionConstant());
370 }
371
372 bool DISubprogram::describes(const Function *F) const {
373   assert(F && "Invalid function");
374   if (F == getFunction())
375     return true;
376   StringRef Name = getLinkageName();
377   if (Name.empty())
378     Name = getName();
379   return F->getName() == Name;
380 }
381
382 void DISubprogram::replaceFunction(Function *F) {
383   replaceFunction(F ? ConstantAsMetadata::get(F)
384                     : static_cast<ConstantAsMetadata *>(nullptr));
385 }
386
387 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
388                                         Metadata *File, unsigned Line,
389                                         unsigned Column, StorageType Storage,
390                                         bool ShouldCreate) {
391   assert(Scope && "Expected scope");
392   DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
393   Metadata *Ops[] = {File, Scope};
394   DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
395 }
396
397 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
398                                                 Metadata *Scope, Metadata *File,
399                                                 unsigned Discriminator,
400                                                 StorageType Storage,
401                                                 bool ShouldCreate) {
402   assert(Scope && "Expected scope");
403   DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
404   Metadata *Ops[] = {File, Scope};
405   DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
406 }
407
408 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
409                                   Metadata *File, MDString *Name, unsigned Line,
410                                   StorageType Storage, bool ShouldCreate) {
411   assert(isCanonical(Name) && "Expected canonical MDString");
412   DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, File, getString(Name), Line));
413   Metadata *Ops[] = {File, Scope, Name};
414   DEFINE_GETIMPL_STORE(DINamespace, (Line), Ops);
415 }
416
417 DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
418                             MDString *Name, MDString *ConfigurationMacros,
419                             MDString *IncludePath, MDString *ISysRoot,
420                             StorageType Storage, bool ShouldCreate) {
421   assert(isCanonical(Name) && "Expected canonical MDString");
422   DEFINE_GETIMPL_LOOKUP(DIModule,
423     (Scope, getString(Name), getString(ConfigurationMacros),
424      getString(IncludePath), getString(ISysRoot)));
425   Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
426   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
427 }
428
429 DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
430                                                           MDString *Name,
431                                                           Metadata *Type,
432                                                           StorageType Storage,
433                                                           bool ShouldCreate) {
434   assert(isCanonical(Name) && "Expected canonical MDString");
435   DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (getString(Name), Type));
436   Metadata *Ops[] = {Name, Type};
437   DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
438 }
439
440 DITemplateValueParameter *DITemplateValueParameter::getImpl(
441     LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
442     Metadata *Value, StorageType Storage, bool ShouldCreate) {
443   assert(isCanonical(Name) && "Expected canonical MDString");
444   DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter,
445                         (Tag, getString(Name), Type, Value));
446   Metadata *Ops[] = {Name, Type, Value};
447   DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
448 }
449
450 DIGlobalVariable *
451 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
452                           MDString *LinkageName, Metadata *File, unsigned Line,
453                           Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
454                           Metadata *Variable,
455                           Metadata *StaticDataMemberDeclaration,
456                           StorageType Storage, bool ShouldCreate) {
457   assert(isCanonical(Name) && "Expected canonical MDString");
458   assert(isCanonical(LinkageName) && "Expected canonical MDString");
459   DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
460                         (Scope, getString(Name), getString(LinkageName), File,
461                          Line, Type, IsLocalToUnit, IsDefinition, Variable,
462                          StaticDataMemberDeclaration));
463   Metadata *Ops[] = {Scope, Name,        File,     Type,
464                      Name,  LinkageName, Variable, StaticDataMemberDeclaration};
465   DEFINE_GETIMPL_STORE(DIGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
466                        Ops);
467 }
468
469 DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, unsigned Tag,
470                                           Metadata *Scope, MDString *Name,
471                                           Metadata *File, unsigned Line,
472                                           Metadata *Type, unsigned Arg,
473                                           unsigned Flags, StorageType Storage,
474                                           bool ShouldCreate) {
475   // 64K ought to be enough for any frontend.
476   assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
477
478   assert(Scope && "Expected scope");
479   assert(isCanonical(Name) && "Expected canonical MDString");
480   DEFINE_GETIMPL_LOOKUP(DILocalVariable, (Tag, Scope, getString(Name), File,
481                                           Line, Type, Arg, Flags));
482   Metadata *Ops[] = {Scope, Name, File, Type};
483   DEFINE_GETIMPL_STORE(DILocalVariable, (Tag, Line, Arg, Flags), Ops);
484 }
485
486 DIExpression *DIExpression::getImpl(LLVMContext &Context,
487                                     ArrayRef<uint64_t> Elements,
488                                     StorageType Storage, bool ShouldCreate) {
489   DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
490   DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
491 }
492
493 unsigned DIExpression::ExprOperand::getSize() const {
494   switch (getOp()) {
495   case dwarf::DW_OP_bit_piece:
496     return 3;
497   case dwarf::DW_OP_plus:
498     return 2;
499   default:
500     return 1;
501   }
502 }
503
504 bool DIExpression::isValid() const {
505   for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
506     // Check that there's space for the operand.
507     if (I->get() + I->getSize() > E->get())
508       return false;
509
510     // Check that the operand is valid.
511     switch (I->getOp()) {
512     default:
513       return false;
514     case dwarf::DW_OP_bit_piece:
515       // Piece expressions must be at the end.
516       return I->get() + I->getSize() == E->get();
517     case dwarf::DW_OP_plus:
518     case dwarf::DW_OP_deref:
519       break;
520     }
521   }
522   return true;
523 }
524
525 bool DIExpression::isBitPiece() const {
526   assert(isValid() && "Expected valid expression");
527   if (unsigned N = getNumElements())
528     if (N >= 3)
529       return getElement(N - 3) == dwarf::DW_OP_bit_piece;
530   return false;
531 }
532
533 uint64_t DIExpression::getBitPieceOffset() const {
534   assert(isBitPiece() && "Expected bit piece");
535   return getElement(getNumElements() - 2);
536 }
537
538 uint64_t DIExpression::getBitPieceSize() const {
539   assert(isBitPiece() && "Expected bit piece");
540   return getElement(getNumElements() - 1);
541 }
542
543 DIObjCProperty *DIObjCProperty::getImpl(
544     LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
545     MDString *GetterName, MDString *SetterName, unsigned Attributes,
546     Metadata *Type, StorageType Storage, bool ShouldCreate) {
547   assert(isCanonical(Name) && "Expected canonical MDString");
548   assert(isCanonical(GetterName) && "Expected canonical MDString");
549   assert(isCanonical(SetterName) && "Expected canonical MDString");
550   DEFINE_GETIMPL_LOOKUP(DIObjCProperty,
551                         (getString(Name), File, Line, getString(GetterName),
552                          getString(SetterName), Attributes, Type));
553   Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
554   DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
555 }
556
557 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
558                                             Metadata *Scope, Metadata *Entity,
559                                             unsigned Line, MDString *Name,
560                                             StorageType Storage,
561                                             bool ShouldCreate) {
562   assert(isCanonical(Name) && "Expected canonical MDString");
563   DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
564                         (Tag, Scope, Entity, Line, getString(Name)));
565   Metadata *Ops[] = {Scope, Entity, Name};
566   DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
567 }