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