]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - include/llvm/IR/DebugInfo.h
[C++11] More 'nullptr' conversion or in some cases just using a boolean check instead...
[opencl/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 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
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"
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;
46 class DIFile;
47 class DISubprogram;
48 class DILexicalBlock;
49 class DILexicalBlockFile;
50 class DIVariable;
51 class DIType;
52 class DIScope;
53 class DIObjCProperty;
55 /// Maps from type identifier to the actual MDNode.
56 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
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;
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   };
86 protected:
87   const MDNode *DbgNode;
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;
97   template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
98     return DescTy(getDescriptorField(Elt));
99   }
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);
106 public:
107   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
109   bool Verify() const;
111   operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
112   MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
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; }
121   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
122   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
124   uint16_t getTag() const {
125     return getUnsignedField(0) & ~LLVMDebugVersionMask;
126   }
128   bool isDerivedType() const;
129   bool isCompositeType() const;
130   bool isBasicType() const;
131   bool isVariable() const;
132   bool isSubprogram() const;
133   bool isGlobalVariable() const;
134   bool isScope() const;
135   bool isFile() const;
136   bool isCompileUnit() const;
137   bool isNameSpace() const;
138   bool isLexicalBlockFile() const;
139   bool isLexicalBlock() const;
140   bool isSubrange() const;
141   bool isEnumerator() const;
142   bool isType() const;
143   bool isUnspecifiedParameter() const;
144   bool isTemplateTypeParameter() const;
145   bool isTemplateValueParameter() const;
146   bool isObjCProperty() const;
147   bool isImportedEntity() const;
149   /// print - print descriptor.
150   void print(raw_ostream &OS) const;
152   /// dump - print descriptor to dbgs() with a newline.
153   void dump() const;
154 };
156 /// DISubrange - This is used to represent ranges, for array bounds.
157 class DISubrange : public DIDescriptor {
158   friend class DIDescriptor;
159   void printInternal(raw_ostream &OS) const;
161 public:
162   explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
164   int64_t getLo() const { return getInt64Field(1); }
165   int64_t getCount() const { return getInt64Field(2); }
166   bool Verify() const;
167 };
169 /// DIArray - This descriptor holds an array of descriptors.
170 class DIArray : public DIDescriptor {
171 public:
172   explicit DIArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
174   unsigned getNumElements() const;
175   DIDescriptor getElement(unsigned Idx) const {
176     return getDescriptorField(Idx);
177   }
178 };
180 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
181 /// FIXME: it seems strange that this doesn't have either a reference to the
182 /// type/precision or a file/line pair for location info.
183 class DIEnumerator : public DIDescriptor {
184   friend class DIDescriptor;
185   void printInternal(raw_ostream &OS) const;
187 public:
188   explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
190   StringRef getName() const { return getStringField(1); }
191   int64_t getEnumValue() const { return getInt64Field(2); }
192   bool Verify() const;
193 };
195 template <typename T> class DIRef;
196 typedef DIRef<DIScope> DIScopeRef;
197 typedef DIRef<DIType> DITypeRef;
199 /// DIScope - A base class for various scopes.
200 ///
201 /// Although, implementation-wise, DIScope is the parent class of most
202 /// other DIxxx classes, including DIType and its descendants, most of
203 /// DIScope's descendants are not a substitutable subtype of
204 /// DIScope. The DIDescriptor::isScope() method only is true for
205 /// DIScopes that are scopes in the strict lexical scope sense
206 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
207 class DIScope : public DIDescriptor {
208 protected:
209   friend class DIDescriptor;
210   void printInternal(raw_ostream &OS) const;
212 public:
213   explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
215   /// Gets the parent scope for this scope node or returns a
216   /// default constructed scope.
217   DIScopeRef getContext() const;
218   /// If the scope node has a name, return that, else return an empty string.
219   StringRef getName() const;
220   StringRef getFilename() const;
221   StringRef getDirectory() const;
223   /// Generate a reference to this DIScope. Uses the type identifier instead
224   /// of the actual MDNode if possible, to help type uniquing.
225   DIScopeRef getRef() const;
226 };
228 /// Represents reference to a DIDescriptor, abstracts over direct and
229 /// identifier-based metadata references.
230 template <typename T> class DIRef {
231   template <typename DescTy>
232   friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
233   friend DIScopeRef DIScope::getContext() const;
234   friend DIScopeRef DIScope::getRef() const;
235   friend class DIType;
237   /// Val can be either a MDNode or a MDString, in the latter,
238   /// MDString specifies the type identifier.
239   const Value *Val;
240   explicit DIRef(const Value *V);
242 public:
243   T resolve(const DITypeIdentifierMap &Map) const;
244   StringRef getName() const;
245   operator Value *() const { return const_cast<Value *>(Val); }
246 };
248 template <typename T>
249 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
250   if (!Val)
251     return T();
253   if (const MDNode *MD = dyn_cast<MDNode>(Val))
254     return T(MD);
256   const MDString *MS = cast<MDString>(Val);
257   // Find the corresponding MDNode.
258   DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
259   assert(Iter != Map.end() && "Identifier not in the type map?");
260   assert(DIDescriptor(Iter->second).isType() &&
261          "MDNode in DITypeIdentifierMap should be a DIType.");
262   return T(Iter->second);
265 template <typename T> StringRef DIRef<T>::getName() const {
266   if (!Val)
267     return StringRef();
269   if (const MDNode *MD = dyn_cast<MDNode>(Val))
270     return T(MD).getName();
272   const MDString *MS = cast<MDString>(Val);
273   return MS->getString();
276 /// Specialize getFieldAs to handle fields that are references to DIScopes.
277 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
278 /// Specialize DIRef constructor for DIScopeRef.
279 template <> DIRef<DIScope>::DIRef(const Value *V);
281 /// Specialize getFieldAs to handle fields that are references to DITypes.
282 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
283 /// Specialize DIRef constructor for DITypeRef.
284 template <> DIRef<DIType>::DIRef(const Value *V);
286 /// DIType - This is a wrapper for a type.
287 /// FIXME: Types should be factored much better so that CV qualifiers and
288 /// others do not require a huge and empty descriptor full of zeros.
289 class DIType : public DIScope {
290 protected:
291   friend class DIDescriptor;
292   void printInternal(raw_ostream &OS) const;
294 public:
295   explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
296   operator DITypeRef () const {
297     assert(isType() &&
298            "constructing DITypeRef from an MDNode that is not a type");
299     return DITypeRef(&*getRef());
300   }
302   /// Verify - Verify that a type descriptor is well formed.
303   bool Verify() const;
305   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
306   StringRef getName() const { return getStringField(3); }
307   unsigned getLineNumber() const { return getUnsignedField(4); }
308   uint64_t getSizeInBits() const { return getUInt64Field(5); }
309   uint64_t getAlignInBits() const { return getUInt64Field(6); }
310   // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
311   // carry this is just plain insane.
312   uint64_t getOffsetInBits() const { return getUInt64Field(7); }
313   unsigned getFlags() const { return getUnsignedField(8); }
314   bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
315   bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
316   bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
317   // isAppleBlock - Return true if this is the Apple Blocks extension.
318   bool isAppleBlockExtension() const {
319     return (getFlags() & FlagAppleBlock) != 0;
320   }
321   bool isBlockByrefStruct() const {
322     return (getFlags() & FlagBlockByrefStruct) != 0;
323   }
324   bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
325   bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
326   bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
327   bool isObjcClassComplete() const {
328     return (getFlags() & FlagObjcClassComplete) != 0;
329   }
330   bool isVector() const { return (getFlags() & FlagVector) != 0; }
331   bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
332   bool isLValueReference() const {
333     return (getFlags() & FlagLValueReference) != 0;
334   }
335   bool isRValueReference() const {
336     return (getFlags() & FlagRValueReference) != 0;
337   }
338   bool isValid() const { return DbgNode && isType(); }
340   /// replaceAllUsesWith - Replace all uses of debug info referenced by
341   /// this descriptor.
342   void replaceAllUsesWith(DIDescriptor &D);
343   void replaceAllUsesWith(MDNode *D);
344 };
346 /// DIBasicType - A basic type, like 'int' or 'float'.
347 class DIBasicType : public DIType {
348 public:
349   explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
351   unsigned getEncoding() const { return getUnsignedField(9); }
353   /// Verify - Verify that a basic type descriptor is well formed.
354   bool Verify() const;
355 };
357 /// DIDerivedType - A simple derived type, like a const qualified type,
358 /// a typedef, a pointer or reference, et cetera.  Or, a data member of
359 /// a class/struct/union.
360 class DIDerivedType : public DIType {
361   friend class DIDescriptor;
362   void printInternal(raw_ostream &OS) const;
364 public:
365   explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
367   DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
369   /// getObjCProperty - Return property node, if this ivar is
370   /// associated with one.
371   MDNode *getObjCProperty() const;
373   DITypeRef getClassType() const {
374     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
375     return getFieldAs<DITypeRef>(10);
376   }
378   Constant *getConstant() const {
379     assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
380     return getConstantField(10);
381   }
383   /// Verify - Verify that a derived type descriptor is well formed.
384   bool Verify() const;
385 };
387 /// DICompositeType - This descriptor holds a type that can refer to multiple
388 /// other types, like a function or struct.
389 /// DICompositeType is derived from DIDerivedType because some
390 /// composite types (such as enums) can be derived from basic types
391 // FIXME: Make this derive from DIType directly & just store the
392 // base type in a single DIType field.
393 class DICompositeType : public DIDerivedType {
394   friend class DIDescriptor;
395   void printInternal(raw_ostream &OS) const;
397 public:
398   explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
400   DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
401   void setTypeArray(DIArray Elements, DIArray TParams = DIArray());
402   unsigned getRunTimeLang() const { return getUnsignedField(11); }
403   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
404   void setContainingType(DICompositeType ContainingType);
405   DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
406   MDString *getIdentifier() const;
408   /// Verify - Verify that a composite type descriptor is well formed.
409   bool Verify() const;
410 };
412 /// DIFile - This is a wrapper for a file.
413 class DIFile : public DIScope {
414   friend class DIDescriptor;
416 public:
417   explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
418   MDNode *getFileNode() const;
419   bool Verify() const;
420 };
422 /// DICompileUnit - A wrapper for a compile unit.
423 class DICompileUnit : public DIScope {
424   friend class DIDescriptor;
425   void printInternal(raw_ostream &OS) const;
427 public:
428   explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
430   unsigned getLanguage() const { return getUnsignedField(2); }
431   StringRef getProducer() const { return getStringField(3); }
433   bool isOptimized() const { return getUnsignedField(4) != 0; }
434   StringRef getFlags() const { return getStringField(5); }
435   unsigned getRunTimeVersion() const { return getUnsignedField(6); }
437   DIArray getEnumTypes() const;
438   DIArray getRetainedTypes() const;
439   DIArray getSubprograms() const;
440   DIArray getGlobalVariables() const;
441   DIArray getImportedEntities() const;
443   StringRef getSplitDebugFilename() const { return getStringField(12); }
444   unsigned getEmissionKind() const { return getUnsignedField(13); }
446   /// Verify - Verify that a compile unit is well formed.
447   bool Verify() const;
448 };
450 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
451 class DISubprogram : public DIScope {
452   friend class DIDescriptor;
453   void printInternal(raw_ostream &OS) const;
455 public:
456   explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
458   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
459   StringRef getName() const { return getStringField(3); }
460   StringRef getDisplayName() const { return getStringField(4); }
461   StringRef getLinkageName() const { return getStringField(5); }
462   unsigned getLineNumber() const { return getUnsignedField(6); }
463   DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
465   /// isLocalToUnit - Return true if this subprogram is local to the current
466   /// compile unit, like 'static' in C.
467   unsigned isLocalToUnit() const { return getUnsignedField(8); }
468   unsigned isDefinition() const { return getUnsignedField(9); }
470   unsigned getVirtuality() const { return getUnsignedField(10); }
471   unsigned getVirtualIndex() const { return getUnsignedField(11); }
473   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
475   unsigned getFlags() const { return getUnsignedField(13); }
477   unsigned isArtificial() const {
478     return (getUnsignedField(13) & FlagArtificial) != 0;
479   }
480   /// isPrivate - Return true if this subprogram has "private"
481   /// access specifier.
482   bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
483   /// isProtected - Return true if this subprogram has "protected"
484   /// access specifier.
485   bool isProtected() const {
486     return (getUnsignedField(13) & FlagProtected) != 0;
487   }
488   /// isExplicit - Return true if this subprogram is marked as explicit.
489   bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
490   /// isPrototyped - Return true if this subprogram is prototyped.
491   bool isPrototyped() const {
492     return (getUnsignedField(13) & FlagPrototyped) != 0;
493   }
495   /// Return true if this subprogram is a C++11 reference-qualified
496   /// non-static member function (void foo() &).
497   unsigned isLValueReference() const {
498     return (getUnsignedField(13) & FlagLValueReference) != 0;
499   }
501   /// Return true if this subprogram is a C++11
502   /// rvalue-reference-qualified non-static member function
503   /// (void foo() &&).
504   unsigned isRValueReference() const {
505     return (getUnsignedField(13) & FlagRValueReference) != 0;
506   }
508   unsigned isOptimized() const;
510   /// Verify - Verify that a subprogram descriptor is well formed.
511   bool Verify() const;
513   /// describes - Return true if this subprogram provides debugging
514   /// information for the function F.
515   bool describes(const Function *F);
517   Function *getFunction() const { return getFunctionField(15); }
518   void replaceFunction(Function *F) { replaceFunctionField(15, F); }
519   DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
520   DISubprogram getFunctionDeclaration() const {
521     return getFieldAs<DISubprogram>(17);
522   }
523   MDNode *getVariablesNodes() const;
524   DIArray getVariables() const;
526   /// getScopeLineNumber - Get the beginning of the scope of the
527   /// function, not necessarily where the name of the program
528   /// starts.
529   unsigned getScopeLineNumber() const { return getUnsignedField(19); }
530 };
532 /// DILexicalBlock - This is a wrapper for a lexical block.
533 class DILexicalBlock : public DIScope {
534 public:
535   explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
536   DIScope getContext() const { return getFieldAs<DIScope>(2); }
537   unsigned getLineNumber() const { return getUnsignedField(3); }
538   unsigned getColumnNumber() const { return getUnsignedField(4); }
539   unsigned getDiscriminator() const { return getUnsignedField(5); }
540   bool Verify() const;
541 };
543 /// DILexicalBlockFile - This is a wrapper for a lexical block with
544 /// a filename change.
545 class DILexicalBlockFile : public DIScope {
546 public:
547   explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
548   DIScope getContext() const {
549     if (getScope().isSubprogram())
550       return getScope();
551     return getScope().getContext();
552   }
553   unsigned getLineNumber() const { return getScope().getLineNumber(); }
554   unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
555   DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
556   bool Verify() const;
557 };
559 /// DINameSpace - A wrapper for a C++ style name space.
560 class DINameSpace : public DIScope {
561   friend class DIDescriptor;
562   void printInternal(raw_ostream &OS) const;
564 public:
565   explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
566   DIScope getContext() const { return getFieldAs<DIScope>(2); }
567   StringRef getName() const { return getStringField(3); }
568   unsigned getLineNumber() const { return getUnsignedField(4); }
569   bool Verify() const;
570 };
572 /// DIUnspecifiedParameter - This is a wrapper for unspecified parameters.
573 class DIUnspecifiedParameter : public DIDescriptor {
574 public:
575   explicit DIUnspecifiedParameter(const MDNode *N = nullptr)
576     : DIDescriptor(N) {}
577   bool Verify() const;
578 };
580 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
581 class DITemplateTypeParameter : public DIDescriptor {
582 public:
583   explicit DITemplateTypeParameter(const MDNode *N = nullptr)
584     : DIDescriptor(N) {}
586   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
587   StringRef getName() const { return getStringField(2); }
588   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
589   StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
590   StringRef getDirectory() const {
591     return getFieldAs<DIFile>(4).getDirectory();
592   }
593   unsigned getLineNumber() const { return getUnsignedField(5); }
594   unsigned getColumnNumber() const { return getUnsignedField(6); }
595   bool Verify() const;
596 };
598 /// DITemplateValueParameter - This is a wrapper for template value parameter.
599 class DITemplateValueParameter : public DIDescriptor {
600 public:
601   explicit DITemplateValueParameter(const MDNode *N = nullptr)
602     : DIDescriptor(N) {}
604   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
605   StringRef getName() const { return getStringField(2); }
606   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
607   Value *getValue() const;
608   StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
609   StringRef getDirectory() const {
610     return getFieldAs<DIFile>(5).getDirectory();
611   }
612   unsigned getLineNumber() const { return getUnsignedField(6); }
613   unsigned getColumnNumber() const { return getUnsignedField(7); }
614   bool Verify() const;
615 };
617 /// DIGlobalVariable - This is a wrapper for a global variable.
618 class DIGlobalVariable : public DIDescriptor {
619   friend class DIDescriptor;
620   void printInternal(raw_ostream &OS) const;
622 public:
623   explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
625   DIScope getContext() const { return getFieldAs<DIScope>(2); }
626   StringRef getName() const { return getStringField(3); }
627   StringRef getDisplayName() const { return getStringField(4); }
628   StringRef getLinkageName() const { return getStringField(5); }
629   StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
630   StringRef getDirectory() const {
631     return getFieldAs<DIFile>(6).getDirectory();
632   }
634   unsigned getLineNumber() const { return getUnsignedField(7); }
635   DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
636   unsigned isLocalToUnit() const { return getUnsignedField(9); }
637   unsigned isDefinition() const { return getUnsignedField(10); }
639   GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
640   Constant *getConstant() const { return getConstantField(11); }
641   DIDerivedType getStaticDataMemberDeclaration() const {
642     return getFieldAs<DIDerivedType>(12);
643   }
645   /// Verify - Verify that a global variable descriptor is well formed.
646   bool Verify() const;
647 };
649 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
650 /// global etc).
651 class DIVariable : public DIDescriptor {
652   friend class DIDescriptor;
653   void printInternal(raw_ostream &OS) const;
655 public:
656   explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
658   DIScope getContext() const { return getFieldAs<DIScope>(1); }
659   StringRef getName() const { return getStringField(2); }
660   DIFile getFile() const { return getFieldAs<DIFile>(3); }
661   unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
662   unsigned getArgNumber() const {
663     unsigned L = getUnsignedField(4);
664     return L >> 24;
665   }
666   DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
668   /// isArtificial - Return true if this variable is marked as "artificial".
669   bool isArtificial() const {
670     return (getUnsignedField(6) & FlagArtificial) != 0;
671   }
673   bool isObjectPointer() const {
674     return (getUnsignedField(6) & FlagObjectPointer) != 0;
675   }
677   /// \brief Return true if this variable is represented as a pointer.
678   bool isIndirect() const {
679     return (getUnsignedField(6) & FlagIndirectVariable) != 0;
680   }
682   /// getInlinedAt - If this variable is inlined then return inline location.
683   MDNode *getInlinedAt() const;
685   /// Verify - Verify that a variable descriptor is well formed.
686   bool Verify() const;
688   /// HasComplexAddr - Return true if the variable has a complex address.
689   bool hasComplexAddress() const { return getNumAddrElements() > 0; }
691   unsigned getNumAddrElements() const;
693   uint64_t getAddrElement(unsigned Idx) const {
694     return getUInt64Field(Idx + 8);
695   }
697   /// isBlockByrefVariable - Return true if the variable was declared as
698   /// a "__block" variable (Apple Blocks).
699   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
700     return (getType().resolve(Map)).isBlockByrefStruct();
701   }
703   /// isInlinedFnArgument - Return true if this variable provides debugging
704   /// information for an inlined function arguments.
705   bool isInlinedFnArgument(const Function *CurFn);
707   void printExtendedName(raw_ostream &OS) const;
708 };
710 /// DILocation - This object holds location information. This object
711 /// is not associated with any DWARF tag.
712 class DILocation : public DIDescriptor {
713 public:
714   explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
716   unsigned getLineNumber() const { return getUnsignedField(0); }
717   unsigned getColumnNumber() const { return getUnsignedField(1); }
718   DIScope getScope() const { return getFieldAs<DIScope>(2); }
719   DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
720   StringRef getFilename() const { return getScope().getFilename(); }
721   StringRef getDirectory() const { return getScope().getDirectory(); }
722   bool Verify() const;
723   bool atSameLineAs(const DILocation &Other) const {
724     return (getLineNumber() == Other.getLineNumber() &&
725             getFilename() == Other.getFilename());
726   }
727   /// getDiscriminator - DWARF discriminators are used to distinguish
728   /// identical file locations for instructions that are on different
729   /// basic blocks. If two instructions are inside the same lexical block
730   /// and are in different basic blocks, we create a new lexical block
731   /// with identical location as the original but with a different
732   /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
733   /// for details).
734   unsigned getDiscriminator() const {
735     // Since discriminators are associated with lexical blocks, make
736     // sure this location is a lexical block before retrieving its
737     // value.
738     return getScope().isLexicalBlock()
739                ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
740                : 0;
741   }
742   unsigned computeNewDiscriminator(LLVMContext &Ctx);
743   DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
744 };
746 class DIObjCProperty : public DIDescriptor {
747   friend class DIDescriptor;
748   void printInternal(raw_ostream &OS) const;
750 public:
751   explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
753   StringRef getObjCPropertyName() const { return getStringField(1); }
754   DIFile getFile() const { return getFieldAs<DIFile>(2); }
755   unsigned getLineNumber() const { return getUnsignedField(3); }
757   StringRef getObjCPropertyGetterName() const { return getStringField(4); }
758   StringRef getObjCPropertySetterName() const { return getStringField(5); }
759   bool isReadOnlyObjCProperty() const {
760     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
761   }
762   bool isReadWriteObjCProperty() const {
763     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
764   }
765   bool isAssignObjCProperty() const {
766     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
767   }
768   bool isRetainObjCProperty() const {
769     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
770   }
771   bool isCopyObjCProperty() const {
772     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
773   }
774   bool isNonAtomicObjCProperty() const {
775     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
776   }
778   /// Objective-C doesn't have an ODR, so there is no benefit in storing
779   /// the type as a DITypeRef here.
780   DIType getType() const { return getFieldAs<DIType>(7); }
782   /// Verify - Verify that a derived type descriptor is well formed.
783   bool Verify() const;
784 };
786 /// \brief An imported module (C++ using directive or similar).
787 class DIImportedEntity : public DIDescriptor {
788   friend class DIDescriptor;
789   void printInternal(raw_ostream &OS) const;
791 public:
792   explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
793   DIScope getContext() const { return getFieldAs<DIScope>(1); }
794   DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
795   unsigned getLineNumber() const { return getUnsignedField(3); }
796   StringRef getName() const { return getStringField(4); }
797   bool Verify() const;
798 };
800 /// getDISubprogram - Find subprogram that is enclosing this scope.
801 DISubprogram getDISubprogram(const MDNode *Scope);
803 /// getDICompositeType - Find underlying composite type.
804 DICompositeType getDICompositeType(DIType T);
806 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
807 /// to hold function specific information.
808 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
810 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
811 /// suitable to hold function specific information.
812 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
814 /// createInlinedVariable - Create a new inlined variable based on current
815 /// variable.
816 /// @param DV            Current Variable.
817 /// @param InlinedScope  Location at current variable is inlined.
818 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
819                                  LLVMContext &VMContext);
821 /// cleanseInlinedVariable - Remove inlined scope from the variable.
822 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
824 /// Construct DITypeIdentifierMap by going through retained types of each CU.
825 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
827 /// Strip debug info in the module if it exists.
828 /// To do this, we remove all calls to the debugger intrinsics and any named
829 /// metadata for debugging. We also remove debug locations for instructions.
830 /// Return true if module is modified.
831 bool StripDebugInfo(Module &M);
833 /// Return Debug Info Metadata Version by checking module flags.
834 unsigned getDebugMetadataVersionFromModule(const Module &M);
836 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
837 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
838 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
839 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
840 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
841 /// used by the CUs.
842 class DebugInfoFinder {
843 public:
844   DebugInfoFinder() : TypeMapInitialized(false) {}
846   /// processModule - Process entire module and collect debug info
847   /// anchors.
848   void processModule(const Module &M);
850   /// processDeclare - Process DbgDeclareInst.
851   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
852   /// Process DbgValueInst.
853   void processValue(const Module &M, const DbgValueInst *DVI);
854   /// processLocation - Process DILocation.
855   void processLocation(const Module &M, DILocation Loc);
857   /// Clear all lists.
858   void reset();
860 private:
861   /// Initialize TypeIdentifierMap.
862   void InitializeTypeMap(const Module &M);
864   /// processType - Process DIType.
865   void processType(DIType DT);
867   /// processSubprogram - Process DISubprogram.
868   void processSubprogram(DISubprogram SP);
870   void processScope(DIScope Scope);
872   /// addCompileUnit - Add compile unit into CUs.
873   bool addCompileUnit(DICompileUnit CU);
875   /// addGlobalVariable - Add global variable into GVs.
876   bool addGlobalVariable(DIGlobalVariable DIG);
878   // addSubprogram - Add subprogram into SPs.
879   bool addSubprogram(DISubprogram SP);
881   /// addType - Add type into Tys.
882   bool addType(DIType DT);
884   bool addScope(DIScope Scope);
886 public:
887   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
888   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
889   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
890   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
891   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
893   iterator_range<compile_unit_iterator> compile_units() const {
894     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
895   }
897   iterator_range<subprogram_iterator> subprograms() const {
898     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
899   }
901   iterator_range<global_variable_iterator> global_variables() const {
902     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
903   }
905   iterator_range<type_iterator> types() const {
906     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
907   }
909   iterator_range<scope_iterator> scopes() const {
910     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
911   }
913   unsigned compile_unit_count() const { return CUs.size(); }
914   unsigned global_variable_count() const { return GVs.size(); }
915   unsigned subprogram_count() const { return SPs.size(); }
916   unsigned type_count() const { return TYs.size(); }
917   unsigned scope_count() const { return Scopes.size(); }
919 private:
920   SmallVector<DICompileUnit, 8> CUs;    // Compile Units
921   SmallVector<DISubprogram, 8> SPs;    // Subprograms
922   SmallVector<DIGlobalVariable, 8> GVs;    // Global Variables;
923   SmallVector<DIType, 8> TYs;    // Types
924   SmallVector<DIScope, 8> Scopes; // Scopes
925   SmallPtrSet<MDNode *, 64> NodesSeen;
926   DITypeIdentifierMap TypeIdentifierMap;
927   /// Specify if TypeIdentifierMap is initialized.
928   bool TypeMapInitialized;
929 };
930 } // end namespace llvm
932 #endif