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