[Debug Info] add a template class DITypedArray.
[oota-llvm.git] / include / llvm / IR / DebugInfo.h
1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
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 defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
13 // DWARF information.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
19
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
28
29 namespace llvm {
30 class BasicBlock;
31 class Constant;
32 class Function;
33 class GlobalVariable;
34 class Module;
35 class Type;
36 class Value;
37 class DbgDeclareInst;
38 class DbgValueInst;
39 class Instruction;
40 class MDNode;
41 class MDString;
42 class NamedMDNode;
43 class LLVMContext;
44 class raw_ostream;
45
46 class DIFile;
47 class DISubprogram;
48 class DILexicalBlock;
49 class DILexicalBlockFile;
50 class DIVariable;
51 class DIType;
52 class DIScope;
53 class DIObjCProperty;
54
55 /// Maps from type identifier to the actual MDNode.
56 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
57
58 /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
59 /// This should not be stored in a container, because the underlying MDNode
60 /// may change in certain situations.
61 class DIDescriptor {
62   // Befriends DIRef so DIRef can befriend the protected member
63   // function: getFieldAs<DIRef>.
64   template <typename T> friend class DIRef;
65
66 public:
67   enum {
68     FlagPrivate           = 1 << 0,
69     FlagProtected         = 1 << 1,
70     FlagFwdDecl           = 1 << 2,
71     FlagAppleBlock        = 1 << 3,
72     FlagBlockByrefStruct  = 1 << 4,
73     FlagVirtual           = 1 << 5,
74     FlagArtificial        = 1 << 6,
75     FlagExplicit          = 1 << 7,
76     FlagPrototyped        = 1 << 8,
77     FlagObjcClassComplete = 1 << 9,
78     FlagObjectPointer     = 1 << 10,
79     FlagVector            = 1 << 11,
80     FlagStaticMember      = 1 << 12,
81     FlagIndirectVariable  = 1 << 13,
82     FlagLValueReference   = 1 << 14,
83     FlagRValueReference   = 1 << 15
84   };
85
86 protected:
87   const MDNode *DbgNode;
88
89   StringRef getStringField(unsigned Elt) const;
90   unsigned getUnsignedField(unsigned Elt) const {
91     return (unsigned)getUInt64Field(Elt);
92   }
93   uint64_t getUInt64Field(unsigned Elt) const;
94   int64_t getInt64Field(unsigned Elt) const;
95   DIDescriptor getDescriptorField(unsigned Elt) const;
96
97   template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
98     return DescTy(getDescriptorField(Elt));
99   }
100
101   GlobalVariable *getGlobalVariableField(unsigned Elt) const;
102   Constant *getConstantField(unsigned Elt) const;
103   Function *getFunctionField(unsigned Elt) const;
104   void replaceFunctionField(unsigned Elt, Function *F);
105
106 public:
107   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
108
109   bool Verify() const;
110
111   operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
112   MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
113
114   // An explicit operator bool so that we can do testing of DI values
115   // easily.
116   // FIXME: This operator bool isn't actually protecting anything at the
117   // moment due to the conversion operator above making DIDescriptor nodes
118   // implicitly convertable to bool.
119   LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
120
121   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
122   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
123
124   uint16_t getTag() const {
125     return getUnsignedField(0) & ~LLVMDebugVersionMask;
126   }
127
128   bool isDerivedType() const;
129   bool isCompositeType() const;
130   bool isBasicType() const;
131   bool isTrivialType() const;
132   bool isVariable() const;
133   bool isSubprogram() const;
134   bool isGlobalVariable() const;
135   bool isScope() const;
136   bool isFile() const;
137   bool isCompileUnit() const;
138   bool isNameSpace() const;
139   bool isLexicalBlockFile() const;
140   bool isLexicalBlock() const;
141   bool isSubrange() const;
142   bool isEnumerator() const;
143   bool isType() const;
144   bool isUnspecifiedParameter() const;
145   bool isTemplateTypeParameter() const;
146   bool isTemplateValueParameter() const;
147   bool isObjCProperty() const;
148   bool isImportedEntity() const;
149
150   /// print - print descriptor.
151   void print(raw_ostream &OS) const;
152
153   /// dump - print descriptor to dbgs() with a newline.
154   void dump() const;
155 };
156
157 /// DISubrange - This is used to represent ranges, for array bounds.
158 class DISubrange : public DIDescriptor {
159   friend class DIDescriptor;
160   void printInternal(raw_ostream &OS) const;
161
162 public:
163   explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
164
165   int64_t getLo() const { return getInt64Field(1); }
166   int64_t getCount() const { return getInt64Field(2); }
167   bool Verify() const;
168 };
169
170 /// DITypedArray - This descriptor holds an array of nodes with type T.
171 template <typename T> class DITypedArray : public DIDescriptor {
172 public:
173   explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
174   unsigned getNumElements() const {
175     return DbgNode ? DbgNode->getNumOperands() : 0;
176   }
177   T getElement(unsigned Idx) const {
178     return getFieldAs<T>(Idx);
179   }
180 };
181
182 typedef DITypedArray<DIDescriptor> DIArray;
183
184 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
185 /// FIXME: it seems strange that this doesn't have either a reference to the
186 /// type/precision or a file/line pair for location info.
187 class DIEnumerator : public DIDescriptor {
188   friend class DIDescriptor;
189   void printInternal(raw_ostream &OS) const;
190
191 public:
192   explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
193
194   StringRef getName() const { return getStringField(1); }
195   int64_t getEnumValue() const { return getInt64Field(2); }
196   bool Verify() const;
197 };
198
199 template <typename T> class DIRef;
200 typedef DIRef<DIScope> DIScopeRef;
201 typedef DIRef<DIType> DITypeRef;
202 typedef DITypedArray<DITypeRef> DITypeArray;
203
204 /// DIScope - A base class for various scopes.
205 ///
206 /// Although, implementation-wise, DIScope is the parent class of most
207 /// other DIxxx classes, including DIType and its descendants, most of
208 /// DIScope's descendants are not a substitutable subtype of
209 /// DIScope. The DIDescriptor::isScope() method only is true for
210 /// DIScopes that are scopes in the strict lexical scope sense
211 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
212 class DIScope : public DIDescriptor {
213 protected:
214   friend class DIDescriptor;
215   void printInternal(raw_ostream &OS) const;
216
217 public:
218   explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
219
220   /// Gets the parent scope for this scope node or returns a
221   /// default constructed scope.
222   DIScopeRef getContext() const;
223   /// If the scope node has a name, return that, else return an empty string.
224   StringRef getName() const;
225   StringRef getFilename() const;
226   StringRef getDirectory() const;
227
228   /// Generate a reference to this DIScope. Uses the type identifier instead
229   /// of the actual MDNode if possible, to help type uniquing.
230   DIScopeRef getRef() const;
231 };
232
233 /// Represents reference to a DIDescriptor, abstracts over direct and
234 /// identifier-based metadata references.
235 template <typename T> class DIRef {
236   template <typename DescTy>
237   friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
238   friend DIScopeRef DIScope::getContext() const;
239   friend DIScopeRef DIScope::getRef() const;
240   friend class DIType;
241
242   /// Val can be either a MDNode or a MDString, in the latter,
243   /// MDString specifies the type identifier.
244   const Value *Val;
245   explicit DIRef(const Value *V);
246
247 public:
248   T resolve(const DITypeIdentifierMap &Map) const;
249   StringRef getName() const;
250   operator Value *() const { return const_cast<Value *>(Val); }
251 };
252
253 template <typename T>
254 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
255   if (!Val)
256     return T();
257
258   if (const MDNode *MD = dyn_cast<MDNode>(Val))
259     return T(MD);
260
261   const MDString *MS = cast<MDString>(Val);
262   // Find the corresponding MDNode.
263   DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
264   assert(Iter != Map.end() && "Identifier not in the type map?");
265   assert(DIDescriptor(Iter->second).isType() &&
266          "MDNode in DITypeIdentifierMap should be a DIType.");
267   return T(Iter->second);
268 }
269
270 template <typename T> StringRef DIRef<T>::getName() const {
271   if (!Val)
272     return StringRef();
273
274   if (const MDNode *MD = dyn_cast<MDNode>(Val))
275     return T(MD).getName();
276
277   const MDString *MS = cast<MDString>(Val);
278   return MS->getString();
279 }
280
281 /// Specialize getFieldAs to handle fields that are references to DIScopes.
282 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
283 /// Specialize DIRef constructor for DIScopeRef.
284 template <> DIRef<DIScope>::DIRef(const Value *V);
285
286 /// Specialize getFieldAs to handle fields that are references to DITypes.
287 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
288 /// Specialize DIRef constructor for DITypeRef.
289 template <> DIRef<DIType>::DIRef(const Value *V);
290
291 /// DIType - This is a wrapper for a type.
292 /// FIXME: Types should be factored much better so that CV qualifiers and
293 /// others do not require a huge and empty descriptor full of zeros.
294 class DIType : public DIScope {
295 protected:
296   friend class DIDescriptor;
297   void printInternal(raw_ostream &OS) const;
298
299 public:
300   explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
301   operator DITypeRef () const {
302     assert(isType() &&
303            "constructing DITypeRef from an MDNode that is not a type");
304     return DITypeRef(&*getRef());
305   }
306
307   /// Verify - Verify that a type descriptor is well formed.
308   bool Verify() const;
309
310   DIScopeRef getContext() const { 
311     assert(!isTrivialType() && "no context for DITrivialType");
312     return getFieldAs<DIScopeRef>(2);
313   }
314   StringRef getName() const {
315     assert(!isTrivialType() && "no name for DITrivialType");
316     return getStringField(3);
317   }
318   unsigned getLineNumber() const {
319     assert(!isTrivialType() && "no line number for DITrivialType");
320     return getUnsignedField(4);
321   }
322   uint64_t getSizeInBits() const {
323     assert(!isTrivialType() && "no SizeInBits for DITrivialType");
324     return getUInt64Field(5);
325   }
326   uint64_t getAlignInBits() const {
327     assert(!isTrivialType() && "no AlignInBits for DITrivialType");
328     return getUInt64Field(6);
329   }
330   // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
331   // carry this is just plain insane.
332   uint64_t getOffsetInBits() const {
333     assert(!isTrivialType() && "no OffsetInBits for DITrivialType");
334     return getUInt64Field(7);
335   }
336   unsigned getFlags() const {
337     assert(!isTrivialType() && "no flag for DITrivialType");
338     return getUnsignedField(8);
339   }
340   bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
341   bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
342   bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
343   // isAppleBlock - Return true if this is the Apple Blocks extension.
344   bool isAppleBlockExtension() const {
345     return (getFlags() & FlagAppleBlock) != 0;
346   }
347   bool isBlockByrefStruct() const {
348     return (getFlags() & FlagBlockByrefStruct) != 0;
349   }
350   bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
351   bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
352   bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
353   bool isObjcClassComplete() const {
354     return (getFlags() & FlagObjcClassComplete) != 0;
355   }
356   bool isVector() const { return (getFlags() & FlagVector) != 0; }
357   bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
358   bool isLValueReference() const {
359     return (getFlags() & FlagLValueReference) != 0;
360   }
361   bool isRValueReference() const {
362     return (getFlags() & FlagRValueReference) != 0;
363   }
364   bool isValid() const { return DbgNode && isType(); }
365
366   /// replaceAllUsesWith - Replace all uses of debug info referenced by
367   /// this descriptor.
368   void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
369   void replaceAllUsesWith(MDNode *D);
370 };
371
372 class DITrivialType : public DIType {
373 public:
374   explicit DITrivialType(const MDNode *N = nullptr) : DIType(N) {}
375   bool Verify() const;
376 };
377
378 /// DIBasicType - A basic type, like 'int' or 'float'.
379 class DIBasicType : public DIType {
380 public:
381   explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
382
383   unsigned getEncoding() const { return getUnsignedField(9); }
384
385   /// Verify - Verify that a basic type descriptor is well formed.
386   bool Verify() const;
387 };
388
389 /// DIDerivedType - A simple derived type, like a const qualified type,
390 /// a typedef, a pointer or reference, et cetera.  Or, a data member of
391 /// a class/struct/union.
392 class DIDerivedType : public DIType {
393   friend class DIDescriptor;
394   void printInternal(raw_ostream &OS) const;
395
396 public:
397   explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
398
399   DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
400
401   /// getObjCProperty - Return property node, if this ivar is
402   /// associated with one.
403   MDNode *getObjCProperty() const;
404
405   DITypeRef getClassType() const {
406     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
407     return getFieldAs<DITypeRef>(10);
408   }
409
410   Constant *getConstant() const {
411     assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
412     return getConstantField(10);
413   }
414
415   /// Verify - Verify that a derived type descriptor is well formed.
416   bool Verify() const;
417 };
418
419 /// DICompositeType - This descriptor holds a type that can refer to multiple
420 /// other types, like a function or struct.
421 /// DICompositeType is derived from DIDerivedType because some
422 /// composite types (such as enums) can be derived from basic types
423 // FIXME: Make this derive from DIType directly & just store the
424 // base type in a single DIType field.
425 class DICompositeType : public DIDerivedType {
426   friend class DIDescriptor;
427   void printInternal(raw_ostream &OS) const;
428   void setArraysHelper(MDNode *Elements, MDNode *TParams);
429
430 public:
431   explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
432
433   DIArray getElements() const { return getFieldAs<DIArray>(10); }
434   template <typename T>
435   void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
436     assert((!TParams || DbgNode->getNumOperands() == 15) &&
437            "If you're setting the template parameters this should include a slot "
438            "for that!");
439     setArraysHelper(Elements, TParams);
440   }
441   unsigned getRunTimeLang() const { return getUnsignedField(11); }
442   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
443   void setContainingType(DICompositeType ContainingType);
444   DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
445   MDString *getIdentifier() const;
446
447   /// Verify - Verify that a composite type descriptor is well formed.
448   bool Verify() const;
449 };
450
451 /// DIFile - This is a wrapper for a file.
452 class DIFile : public DIScope {
453   friend class DIDescriptor;
454
455 public:
456   explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
457   MDNode *getFileNode() const;
458   bool Verify() const;
459 };
460
461 /// DICompileUnit - A wrapper for a compile unit.
462 class DICompileUnit : public DIScope {
463   friend class DIDescriptor;
464   void printInternal(raw_ostream &OS) const;
465
466 public:
467   explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
468
469   dwarf::SourceLanguage getLanguage() const {
470     return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
471   }
472   StringRef getProducer() const { return getStringField(3); }
473
474   bool isOptimized() const { return getUnsignedField(4) != 0; }
475   StringRef getFlags() const { return getStringField(5); }
476   unsigned getRunTimeVersion() const { return getUnsignedField(6); }
477
478   DIArray getEnumTypes() const;
479   DIArray getRetainedTypes() const;
480   DIArray getSubprograms() const;
481   DIArray getGlobalVariables() const;
482   DIArray getImportedEntities() const;
483
484   StringRef getSplitDebugFilename() const { return getStringField(12); }
485   unsigned getEmissionKind() const { return getUnsignedField(13); }
486
487   /// Verify - Verify that a compile unit is well formed.
488   bool Verify() const;
489 };
490
491 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
492 class DISubprogram : public DIScope {
493   friend class DIDescriptor;
494   void printInternal(raw_ostream &OS) const;
495
496 public:
497   explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
498
499   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
500   StringRef getName() const { return getStringField(3); }
501   StringRef getDisplayName() const { return getStringField(4); }
502   StringRef getLinkageName() const { return getStringField(5); }
503   unsigned getLineNumber() const { return getUnsignedField(6); }
504   DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
505
506   /// isLocalToUnit - Return true if this subprogram is local to the current
507   /// compile unit, like 'static' in C.
508   unsigned isLocalToUnit() const { return getUnsignedField(8); }
509   unsigned isDefinition() const { return getUnsignedField(9); }
510
511   unsigned getVirtuality() const { return getUnsignedField(10); }
512   unsigned getVirtualIndex() const { return getUnsignedField(11); }
513
514   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
515
516   unsigned getFlags() const { return getUnsignedField(13); }
517
518   unsigned isArtificial() const {
519     return (getUnsignedField(13) & FlagArtificial) != 0;
520   }
521   /// isPrivate - Return true if this subprogram has "private"
522   /// access specifier.
523   bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
524   /// isProtected - Return true if this subprogram has "protected"
525   /// access specifier.
526   bool isProtected() const {
527     return (getUnsignedField(13) & FlagProtected) != 0;
528   }
529   /// isExplicit - Return true if this subprogram is marked as explicit.
530   bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
531   /// isPrototyped - Return true if this subprogram is prototyped.
532   bool isPrototyped() const {
533     return (getUnsignedField(13) & FlagPrototyped) != 0;
534   }
535
536   /// Return true if this subprogram is a C++11 reference-qualified
537   /// non-static member function (void foo() &).
538   unsigned isLValueReference() const {
539     return (getUnsignedField(13) & FlagLValueReference) != 0;
540   }
541
542   /// Return true if this subprogram is a C++11
543   /// rvalue-reference-qualified non-static member function
544   /// (void foo() &&).
545   unsigned isRValueReference() const {
546     return (getUnsignedField(13) & FlagRValueReference) != 0;
547   }
548
549   unsigned isOptimized() const;
550
551   /// Verify - Verify that a subprogram descriptor is well formed.
552   bool Verify() const;
553
554   /// describes - Return true if this subprogram provides debugging
555   /// information for the function F.
556   bool describes(const Function *F);
557
558   Function *getFunction() const { return getFunctionField(15); }
559   void replaceFunction(Function *F) { replaceFunctionField(15, F); }
560   DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
561   DISubprogram getFunctionDeclaration() const {
562     return getFieldAs<DISubprogram>(17);
563   }
564   MDNode *getVariablesNodes() const;
565   DIArray getVariables() const;
566
567   /// getScopeLineNumber - Get the beginning of the scope of the
568   /// function, not necessarily where the name of the program
569   /// starts.
570   unsigned getScopeLineNumber() const { return getUnsignedField(19); }
571 };
572
573 /// DILexicalBlock - This is a wrapper for a lexical block.
574 class DILexicalBlock : public DIScope {
575 public:
576   explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
577   DIScope getContext() const { return getFieldAs<DIScope>(2); }
578   unsigned getLineNumber() const { return getUnsignedField(3); }
579   unsigned getColumnNumber() const { return getUnsignedField(4); }
580   unsigned getDiscriminator() const { return getUnsignedField(5); }
581   bool Verify() const;
582 };
583
584 /// DILexicalBlockFile - This is a wrapper for a lexical block with
585 /// a filename change.
586 class DILexicalBlockFile : public DIScope {
587 public:
588   explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
589   DIScope getContext() const {
590     if (getScope().isSubprogram())
591       return getScope();
592     return getScope().getContext();
593   }
594   unsigned getLineNumber() const { return getScope().getLineNumber(); }
595   unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
596   DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
597   bool Verify() const;
598 };
599
600 /// DINameSpace - A wrapper for a C++ style name space.
601 class DINameSpace : public DIScope {
602   friend class DIDescriptor;
603   void printInternal(raw_ostream &OS) const;
604
605 public:
606   explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
607   DIScope getContext() const { return getFieldAs<DIScope>(2); }
608   StringRef getName() const { return getStringField(3); }
609   unsigned getLineNumber() const { return getUnsignedField(4); }
610   bool Verify() const;
611 };
612
613 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
614 class DITemplateTypeParameter : public DIDescriptor {
615 public:
616   explicit DITemplateTypeParameter(const MDNode *N = nullptr)
617     : DIDescriptor(N) {}
618
619   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
620   StringRef getName() const { return getStringField(2); }
621   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
622   StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
623   StringRef getDirectory() const {
624     return getFieldAs<DIFile>(4).getDirectory();
625   }
626   unsigned getLineNumber() const { return getUnsignedField(5); }
627   unsigned getColumnNumber() const { return getUnsignedField(6); }
628   bool Verify() const;
629 };
630
631 /// DITemplateValueParameter - This is a wrapper for template value parameter.
632 class DITemplateValueParameter : public DIDescriptor {
633 public:
634   explicit DITemplateValueParameter(const MDNode *N = nullptr)
635     : DIDescriptor(N) {}
636
637   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
638   StringRef getName() const { return getStringField(2); }
639   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
640   Value *getValue() const;
641   StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
642   StringRef getDirectory() const {
643     return getFieldAs<DIFile>(5).getDirectory();
644   }
645   unsigned getLineNumber() const { return getUnsignedField(6); }
646   unsigned getColumnNumber() const { return getUnsignedField(7); }
647   bool Verify() const;
648 };
649
650 /// DIGlobalVariable - This is a wrapper for a global variable.
651 class DIGlobalVariable : public DIDescriptor {
652   friend class DIDescriptor;
653   void printInternal(raw_ostream &OS) const;
654
655 public:
656   explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
657
658   DIScope getContext() const { return getFieldAs<DIScope>(2); }
659   StringRef getName() const { return getStringField(3); }
660   StringRef getDisplayName() const { return getStringField(4); }
661   StringRef getLinkageName() const { return getStringField(5); }
662   StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
663   StringRef getDirectory() const {
664     return getFieldAs<DIFile>(6).getDirectory();
665   }
666
667   unsigned getLineNumber() const { return getUnsignedField(7); }
668   DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
669   unsigned isLocalToUnit() const { return getUnsignedField(9); }
670   unsigned isDefinition() const { return getUnsignedField(10); }
671
672   GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
673   Constant *getConstant() const { return getConstantField(11); }
674   DIDerivedType getStaticDataMemberDeclaration() const {
675     return getFieldAs<DIDerivedType>(12);
676   }
677
678   /// Verify - Verify that a global variable descriptor is well formed.
679   bool Verify() const;
680 };
681
682 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
683 /// global etc).
684 class DIVariable : public DIDescriptor {
685   friend class DIDescriptor;
686   void printInternal(raw_ostream &OS) const;
687
688 public:
689   explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
690
691   DIScope getContext() const { return getFieldAs<DIScope>(1); }
692   StringRef getName() const { return getStringField(2); }
693   DIFile getFile() const { return getFieldAs<DIFile>(3); }
694   unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
695   unsigned getArgNumber() const {
696     unsigned L = getUnsignedField(4);
697     return L >> 24;
698   }
699   DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
700
701   /// isArtificial - Return true if this variable is marked as "artificial".
702   bool isArtificial() const {
703     return (getUnsignedField(6) & FlagArtificial) != 0;
704   }
705
706   bool isObjectPointer() const {
707     return (getUnsignedField(6) & FlagObjectPointer) != 0;
708   }
709
710   /// \brief Return true if this variable is represented as a pointer.
711   bool isIndirect() const {
712     return (getUnsignedField(6) & FlagIndirectVariable) != 0;
713   }
714
715   /// getInlinedAt - If this variable is inlined then return inline location.
716   MDNode *getInlinedAt() const;
717
718   /// Verify - Verify that a variable descriptor is well formed.
719   bool Verify() const;
720
721   /// HasComplexAddr - Return true if the variable has a complex address.
722   bool hasComplexAddress() const { return getNumAddrElements() > 0; }
723
724   /// \brief Return the size of this variable's complex address or
725   /// zero if there is none.
726   unsigned getNumAddrElements() const {
727     if (DbgNode->getNumOperands() < 9)
728       return 0;
729     return getDescriptorField(8)->getNumOperands();
730   }
731
732   /// \brief return the Idx'th complex address element.
733   uint64_t getAddrElement(unsigned Idx) const;
734
735   /// isBlockByrefVariable - Return true if the variable was declared as
736   /// a "__block" variable (Apple Blocks).
737   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
738     return (getType().resolve(Map)).isBlockByrefStruct();
739   }
740
741   /// isInlinedFnArgument - Return true if this variable provides debugging
742   /// information for an inlined function arguments.
743   bool isInlinedFnArgument(const Function *CurFn);
744
745   void printExtendedName(raw_ostream &OS) const;
746 };
747
748 /// DILocation - This object holds location information. This object
749 /// is not associated with any DWARF tag.
750 class DILocation : public DIDescriptor {
751 public:
752   explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
753
754   unsigned getLineNumber() const { return getUnsignedField(0); }
755   unsigned getColumnNumber() const { return getUnsignedField(1); }
756   DIScope getScope() const { return getFieldAs<DIScope>(2); }
757   DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
758   StringRef getFilename() const { return getScope().getFilename(); }
759   StringRef getDirectory() const { return getScope().getDirectory(); }
760   bool Verify() const;
761   bool atSameLineAs(const DILocation &Other) const {
762     return (getLineNumber() == Other.getLineNumber() &&
763             getFilename() == Other.getFilename());
764   }
765   /// getDiscriminator - DWARF discriminators are used to distinguish
766   /// identical file locations for instructions that are on different
767   /// basic blocks. If two instructions are inside the same lexical block
768   /// and are in different basic blocks, we create a new lexical block
769   /// with identical location as the original but with a different
770   /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
771   /// for details).
772   unsigned getDiscriminator() const {
773     // Since discriminators are associated with lexical blocks, make
774     // sure this location is a lexical block before retrieving its
775     // value.
776     return getScope().isLexicalBlock()
777                ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
778                : 0;
779   }
780   unsigned computeNewDiscriminator(LLVMContext &Ctx);
781   DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
782 };
783
784 class DIObjCProperty : public DIDescriptor {
785   friend class DIDescriptor;
786   void printInternal(raw_ostream &OS) const;
787
788 public:
789   explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
790
791   StringRef getObjCPropertyName() const { return getStringField(1); }
792   DIFile getFile() const { return getFieldAs<DIFile>(2); }
793   unsigned getLineNumber() const { return getUnsignedField(3); }
794
795   StringRef getObjCPropertyGetterName() const { return getStringField(4); }
796   StringRef getObjCPropertySetterName() const { return getStringField(5); }
797   bool isReadOnlyObjCProperty() const {
798     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
799   }
800   bool isReadWriteObjCProperty() const {
801     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
802   }
803   bool isAssignObjCProperty() const {
804     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
805   }
806   bool isRetainObjCProperty() const {
807     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
808   }
809   bool isCopyObjCProperty() const {
810     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
811   }
812   bool isNonAtomicObjCProperty() const {
813     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
814   }
815
816   /// Objective-C doesn't have an ODR, so there is no benefit in storing
817   /// the type as a DITypeRef here.
818   DIType getType() const { return getFieldAs<DIType>(7); }
819
820   /// Verify - Verify that a derived type descriptor is well formed.
821   bool Verify() const;
822 };
823
824 /// \brief An imported module (C++ using directive or similar).
825 class DIImportedEntity : public DIDescriptor {
826   friend class DIDescriptor;
827   void printInternal(raw_ostream &OS) const;
828
829 public:
830   explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
831   DIScope getContext() const { return getFieldAs<DIScope>(1); }
832   DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
833   unsigned getLineNumber() const { return getUnsignedField(3); }
834   StringRef getName() const { return getStringField(4); }
835   bool Verify() const;
836 };
837
838 /// getDISubprogram - Find subprogram that is enclosing this scope.
839 DISubprogram getDISubprogram(const MDNode *Scope);
840
841 /// getDICompositeType - Find underlying composite type.
842 DICompositeType getDICompositeType(DIType T);
843
844 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
845 /// to hold function specific information.
846 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
847
848 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
849 /// suitable to hold function specific information.
850 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
851
852 /// createInlinedVariable - Create a new inlined variable based on current
853 /// variable.
854 /// @param DV            Current Variable.
855 /// @param InlinedScope  Location at current variable is inlined.
856 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
857                                  LLVMContext &VMContext);
858
859 /// cleanseInlinedVariable - Remove inlined scope from the variable.
860 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
861
862 /// Construct DITypeIdentifierMap by going through retained types of each CU.
863 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
864
865 /// Strip debug info in the module if it exists.
866 /// To do this, we remove all calls to the debugger intrinsics and any named
867 /// metadata for debugging. We also remove debug locations for instructions.
868 /// Return true if module is modified.
869 bool StripDebugInfo(Module &M);
870
871 /// Return Debug Info Metadata Version by checking module flags.
872 unsigned getDebugMetadataVersionFromModule(const Module &M);
873
874 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
875 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
876 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
877 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
878 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
879 /// used by the CUs.
880 class DebugInfoFinder {
881 public:
882   DebugInfoFinder() : TypeMapInitialized(false) {}
883
884   /// processModule - Process entire module and collect debug info
885   /// anchors.
886   void processModule(const Module &M);
887
888   /// processDeclare - Process DbgDeclareInst.
889   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
890   /// Process DbgValueInst.
891   void processValue(const Module &M, const DbgValueInst *DVI);
892   /// processLocation - Process DILocation.
893   void processLocation(const Module &M, DILocation Loc);
894
895   /// Clear all lists.
896   void reset();
897
898 private:
899   /// Initialize TypeIdentifierMap.
900   void InitializeTypeMap(const Module &M);
901
902   /// processType - Process DIType.
903   void processType(DIType DT);
904
905   /// processSubprogram - Process DISubprogram.
906   void processSubprogram(DISubprogram SP);
907
908   void processScope(DIScope Scope);
909
910   /// addCompileUnit - Add compile unit into CUs.
911   bool addCompileUnit(DICompileUnit CU);
912
913   /// addGlobalVariable - Add global variable into GVs.
914   bool addGlobalVariable(DIGlobalVariable DIG);
915
916   // addSubprogram - Add subprogram into SPs.
917   bool addSubprogram(DISubprogram SP);
918
919   /// addType - Add type into Tys.
920   bool addType(DIType DT);
921
922   bool addScope(DIScope Scope);
923
924 public:
925   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
926   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
927   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
928   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
929   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
930
931   iterator_range<compile_unit_iterator> compile_units() const {
932     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
933   }
934
935   iterator_range<subprogram_iterator> subprograms() const {
936     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
937   }
938
939   iterator_range<global_variable_iterator> global_variables() const {
940     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
941   }
942
943   iterator_range<type_iterator> types() const {
944     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
945   }
946
947   iterator_range<scope_iterator> scopes() const {
948     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
949   }
950
951   unsigned compile_unit_count() const { return CUs.size(); }
952   unsigned global_variable_count() const { return GVs.size(); }
953   unsigned subprogram_count() const { return SPs.size(); }
954   unsigned type_count() const { return TYs.size(); }
955   unsigned scope_count() const { return Scopes.size(); }
956
957 private:
958   SmallVector<DICompileUnit, 8> CUs;    // Compile Units
959   SmallVector<DISubprogram, 8> SPs;    // Subprograms
960   SmallVector<DIGlobalVariable, 8> GVs;    // Global Variables;
961   SmallVector<DIType, 8> TYs;    // Types
962   SmallVector<DIScope, 8> Scopes; // Scopes
963   SmallPtrSet<MDNode *, 64> NodesSeen;
964   DITypeIdentifierMap TypeIdentifierMap;
965   /// Specify if TypeIdentifierMap is initialized.
966   bool TypeMapInitialized;
967 };
968
969 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
970
971 } // end namespace llvm
972
973 #endif