Debug info: split out complex DIVariable address expressions into a
[oota-llvm.git] / include / llvm / IR / DebugInfo.h
1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
13 // DWARF information.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
19
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
28
29 namespace llvm {
30 class BasicBlock;
31 class Constant;
32 class Function;
33 class GlobalVariable;
34 class Module;
35 class Type;
36 class Value;
37 class DbgDeclareInst;
38 class DbgValueInst;
39 class Instruction;
40 class MDNode;
41 class MDString;
42 class NamedMDNode;
43 class LLVMContext;
44 class raw_ostream;
45
46 class DIFile;
47 class DISubprogram;
48 class DILexicalBlock;
49 class DILexicalBlockFile;
50 class DIVariable;
51 class DIType;
52 class DIScope;
53 class DIObjCProperty;
54
55 /// Maps from type identifier to the actual MDNode.
56 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
57
58 /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
59 /// This should not be stored in a container, because the underlying MDNode
60 /// may change in certain situations.
61 class DIDescriptor {
62   // Befriends DIRef so DIRef can befriend the protected member
63   // function: getFieldAs<DIRef>.
64   template <typename T> friend class DIRef;
65
66 public:
67   enum {
68     FlagPrivate           = 1 << 0,
69     FlagProtected         = 1 << 1,
70     FlagFwdDecl           = 1 << 2,
71     FlagAppleBlock        = 1 << 3,
72     FlagBlockByrefStruct  = 1 << 4,
73     FlagVirtual           = 1 << 5,
74     FlagArtificial        = 1 << 6,
75     FlagExplicit          = 1 << 7,
76     FlagPrototyped        = 1 << 8,
77     FlagObjcClassComplete = 1 << 9,
78     FlagObjectPointer     = 1 << 10,
79     FlagVector            = 1 << 11,
80     FlagStaticMember      = 1 << 12,
81     FlagIndirectVariable  = 1 << 13,
82     FlagLValueReference   = 1 << 14,
83     FlagRValueReference   = 1 << 15
84   };
85
86 protected:
87   const MDNode *DbgNode;
88
89   StringRef getStringField(unsigned Elt) const;
90   unsigned getUnsignedField(unsigned Elt) const {
91     return (unsigned)getUInt64Field(Elt);
92   }
93   uint64_t getUInt64Field(unsigned Elt) const;
94   int64_t getInt64Field(unsigned Elt) const;
95   DIDescriptor getDescriptorField(unsigned Elt) const;
96
97   template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
98     return DescTy(getDescriptorField(Elt));
99   }
100
101   GlobalVariable *getGlobalVariableField(unsigned Elt) const;
102   Constant *getConstantField(unsigned Elt) const;
103   Function *getFunctionField(unsigned Elt) const;
104   void replaceFunctionField(unsigned Elt, Function *F);
105
106 public:
107   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
108
109   bool Verify() const;
110
111   operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
112   MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
113
114   // An explicit operator bool so that we can do testing of DI values
115   // easily.
116   // FIXME: This operator bool isn't actually protecting anything at the
117   // moment due to the conversion operator above making DIDescriptor nodes
118   // implicitly convertable to bool.
119   LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
120
121   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
122   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
123
124   uint16_t getTag() const {
125     return getUnsignedField(0) & ~LLVMDebugVersionMask;
126   }
127
128   bool isDerivedType() const;
129   bool isCompositeType() const;
130   bool isBasicType() const;
131   bool 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;
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 /// DIArray - This descriptor holds an array of descriptors.
170 class DIArray : public DIDescriptor {
171 public:
172   explicit DIArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
173
174   unsigned getNumElements() const;
175   DIDescriptor getElement(unsigned Idx) const {
176     return getDescriptorField(Idx);
177   }
178 };
179
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;
186
187 public:
188   explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
189
190   StringRef getName() const { return getStringField(1); }
191   int64_t getEnumValue() const { return getInt64Field(2); }
192   bool Verify() const;
193 };
194
195 template <typename T> class DIRef;
196 typedef DIRef<DIScope> DIScopeRef;
197 typedef DIRef<DIType> DITypeRef;
198
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;
211
212 public:
213   explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
214
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;
222
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 };
227
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;
236
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);
241
242 public:
243   T resolve(const DITypeIdentifierMap &Map) const;
244   StringRef getName() const;
245   operator Value *() const { return const_cast<Value *>(Val); }
246 };
247
248 template <typename T>
249 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
250   if (!Val)
251     return T();
252
253   if (const MDNode *MD = dyn_cast<MDNode>(Val))
254     return T(MD);
255
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);
263 }
264
265 template <typename T> StringRef DIRef<T>::getName() const {
266   if (!Val)
267     return StringRef();
268
269   if (const MDNode *MD = dyn_cast<MDNode>(Val))
270     return T(MD).getName();
271
272   const MDString *MS = cast<MDString>(Val);
273   return MS->getString();
274 }
275
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);
280
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);
285
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;
293
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   }
301
302   /// Verify - Verify that a type descriptor is well formed.
303   bool Verify() const;
304
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(); }
339
340   /// replaceAllUsesWith - Replace all uses of debug info referenced by
341   /// this descriptor.
342   void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
343   void replaceAllUsesWith(MDNode *D);
344 };
345
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) {}
350
351   unsigned getEncoding() const { return getUnsignedField(9); }
352
353   /// Verify - Verify that a basic type descriptor is well formed.
354   bool Verify() const;
355 };
356
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;
363
364 public:
365   explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
366
367   DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
368
369   /// getObjCProperty - Return property node, if this ivar is
370   /// associated with one.
371   MDNode *getObjCProperty() const;
372
373   DITypeRef getClassType() const {
374     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
375     return getFieldAs<DITypeRef>(10);
376   }
377
378   Constant *getConstant() const {
379     assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
380     return getConstantField(10);
381   }
382
383   /// Verify - Verify that a derived type descriptor is well formed.
384   bool Verify() const;
385 };
386
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;
396
397 public:
398   explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
399
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;
407
408   /// Verify - Verify that a composite type descriptor is well formed.
409   bool Verify() const;
410 };
411
412 /// DIFile - This is a wrapper for a file.
413 class DIFile : public DIScope {
414   friend class DIDescriptor;
415
416 public:
417   explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
418   MDNode *getFileNode() const;
419   bool Verify() const;
420 };
421
422 /// DICompileUnit - A wrapper for a compile unit.
423 class DICompileUnit : public DIScope {
424   friend class DIDescriptor;
425   void printInternal(raw_ostream &OS) const;
426
427 public:
428   explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
429
430   dwarf::SourceLanguage getLanguage() const {
431     return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
432   }
433   StringRef getProducer() const { return getStringField(3); }
434
435   bool isOptimized() const { return getUnsignedField(4) != 0; }
436   StringRef getFlags() const { return getStringField(5); }
437   unsigned getRunTimeVersion() const { return getUnsignedField(6); }
438
439   DIArray getEnumTypes() const;
440   DIArray getRetainedTypes() const;
441   DIArray getSubprograms() const;
442   DIArray getGlobalVariables() const;
443   DIArray getImportedEntities() const;
444
445   StringRef getSplitDebugFilename() const { return getStringField(12); }
446   unsigned getEmissionKind() const { return getUnsignedField(13); }
447
448   /// Verify - Verify that a compile unit is well formed.
449   bool Verify() const;
450 };
451
452 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
453 class DISubprogram : public DIScope {
454   friend class DIDescriptor;
455   void printInternal(raw_ostream &OS) const;
456
457 public:
458   explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
459
460   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
461   StringRef getName() const { return getStringField(3); }
462   StringRef getDisplayName() const { return getStringField(4); }
463   StringRef getLinkageName() const { return getStringField(5); }
464   unsigned getLineNumber() const { return getUnsignedField(6); }
465   DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
466
467   /// isLocalToUnit - Return true if this subprogram is local to the current
468   /// compile unit, like 'static' in C.
469   unsigned isLocalToUnit() const { return getUnsignedField(8); }
470   unsigned isDefinition() const { return getUnsignedField(9); }
471
472   unsigned getVirtuality() const { return getUnsignedField(10); }
473   unsigned getVirtualIndex() const { return getUnsignedField(11); }
474
475   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
476
477   unsigned getFlags() const { return getUnsignedField(13); }
478
479   unsigned isArtificial() const {
480     return (getUnsignedField(13) & FlagArtificial) != 0;
481   }
482   /// isPrivate - Return true if this subprogram has "private"
483   /// access specifier.
484   bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
485   /// isProtected - Return true if this subprogram has "protected"
486   /// access specifier.
487   bool isProtected() const {
488     return (getUnsignedField(13) & FlagProtected) != 0;
489   }
490   /// isExplicit - Return true if this subprogram is marked as explicit.
491   bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
492   /// isPrototyped - Return true if this subprogram is prototyped.
493   bool isPrototyped() const {
494     return (getUnsignedField(13) & FlagPrototyped) != 0;
495   }
496
497   /// Return true if this subprogram is a C++11 reference-qualified
498   /// non-static member function (void foo() &).
499   unsigned isLValueReference() const {
500     return (getUnsignedField(13) & FlagLValueReference) != 0;
501   }
502
503   /// Return true if this subprogram is a C++11
504   /// rvalue-reference-qualified non-static member function
505   /// (void foo() &&).
506   unsigned isRValueReference() const {
507     return (getUnsignedField(13) & FlagRValueReference) != 0;
508   }
509
510   unsigned isOptimized() const;
511
512   /// Verify - Verify that a subprogram descriptor is well formed.
513   bool Verify() const;
514
515   /// describes - Return true if this subprogram provides debugging
516   /// information for the function F.
517   bool describes(const Function *F);
518
519   Function *getFunction() const { return getFunctionField(15); }
520   void replaceFunction(Function *F) { replaceFunctionField(15, F); }
521   DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
522   DISubprogram getFunctionDeclaration() const {
523     return getFieldAs<DISubprogram>(17);
524   }
525   MDNode *getVariablesNodes() const;
526   DIArray getVariables() const;
527
528   /// getScopeLineNumber - Get the beginning of the scope of the
529   /// function, not necessarily where the name of the program
530   /// starts.
531   unsigned getScopeLineNumber() const { return getUnsignedField(19); }
532 };
533
534 /// DILexicalBlock - This is a wrapper for a lexical block.
535 class DILexicalBlock : public DIScope {
536 public:
537   explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
538   DIScope getContext() const { return getFieldAs<DIScope>(2); }
539   unsigned getLineNumber() const { return getUnsignedField(3); }
540   unsigned getColumnNumber() const { return getUnsignedField(4); }
541   unsigned getDiscriminator() const { return getUnsignedField(5); }
542   bool Verify() const;
543 };
544
545 /// DILexicalBlockFile - This is a wrapper for a lexical block with
546 /// a filename change.
547 class DILexicalBlockFile : public DIScope {
548 public:
549   explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
550   DIScope getContext() const {
551     if (getScope().isSubprogram())
552       return getScope();
553     return getScope().getContext();
554   }
555   unsigned getLineNumber() const { return getScope().getLineNumber(); }
556   unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
557   DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
558   bool Verify() const;
559 };
560
561 /// DINameSpace - A wrapper for a C++ style name space.
562 class DINameSpace : public DIScope {
563   friend class DIDescriptor;
564   void printInternal(raw_ostream &OS) const;
565
566 public:
567   explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
568   DIScope getContext() const { return getFieldAs<DIScope>(2); }
569   StringRef getName() const { return getStringField(3); }
570   unsigned getLineNumber() const { return getUnsignedField(4); }
571   bool Verify() const;
572 };
573
574 /// DIUnspecifiedParameter - This is a wrapper for unspecified parameters.
575 class DIUnspecifiedParameter : public DIDescriptor {
576 public:
577   explicit DIUnspecifiedParameter(const MDNode *N = nullptr)
578     : DIDescriptor(N) {}
579   bool Verify() const;
580 };
581
582 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
583 class DITemplateTypeParameter : public DIDescriptor {
584 public:
585   explicit DITemplateTypeParameter(const MDNode *N = nullptr)
586     : DIDescriptor(N) {}
587
588   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
589   StringRef getName() const { return getStringField(2); }
590   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
591   StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
592   StringRef getDirectory() const {
593     return getFieldAs<DIFile>(4).getDirectory();
594   }
595   unsigned getLineNumber() const { return getUnsignedField(5); }
596   unsigned getColumnNumber() const { return getUnsignedField(6); }
597   bool Verify() const;
598 };
599
600 /// DITemplateValueParameter - This is a wrapper for template value parameter.
601 class DITemplateValueParameter : public DIDescriptor {
602 public:
603   explicit DITemplateValueParameter(const MDNode *N = nullptr)
604     : DIDescriptor(N) {}
605
606   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
607   StringRef getName() const { return getStringField(2); }
608   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
609   Value *getValue() const;
610   StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
611   StringRef getDirectory() const {
612     return getFieldAs<DIFile>(5).getDirectory();
613   }
614   unsigned getLineNumber() const { return getUnsignedField(6); }
615   unsigned getColumnNumber() const { return getUnsignedField(7); }
616   bool Verify() const;
617 };
618
619 /// DIGlobalVariable - This is a wrapper for a global variable.
620 class DIGlobalVariable : public DIDescriptor {
621   friend class DIDescriptor;
622   void printInternal(raw_ostream &OS) const;
623
624 public:
625   explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
626
627   DIScope getContext() const { return getFieldAs<DIScope>(2); }
628   StringRef getName() const { return getStringField(3); }
629   StringRef getDisplayName() const { return getStringField(4); }
630   StringRef getLinkageName() const { return getStringField(5); }
631   StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
632   StringRef getDirectory() const {
633     return getFieldAs<DIFile>(6).getDirectory();
634   }
635
636   unsigned getLineNumber() const { return getUnsignedField(7); }
637   DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
638   unsigned isLocalToUnit() const { return getUnsignedField(9); }
639   unsigned isDefinition() const { return getUnsignedField(10); }
640
641   GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
642   Constant *getConstant() const { return getConstantField(11); }
643   DIDerivedType getStaticDataMemberDeclaration() const {
644     return getFieldAs<DIDerivedType>(12);
645   }
646
647   /// Verify - Verify that a global variable descriptor is well formed.
648   bool Verify() const;
649 };
650
651 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
652 /// global etc).
653 class DIVariable : public DIDescriptor {
654   friend class DIDescriptor;
655   void printInternal(raw_ostream &OS) const;
656
657 public:
658   explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
659
660   DIScope getContext() const { return getFieldAs<DIScope>(1); }
661   StringRef getName() const { return getStringField(2); }
662   DIFile getFile() const { return getFieldAs<DIFile>(3); }
663   unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
664   unsigned getArgNumber() const {
665     unsigned L = getUnsignedField(4);
666     return L >> 24;
667   }
668   DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
669
670   /// isArtificial - Return true if this variable is marked as "artificial".
671   bool isArtificial() const {
672     return (getUnsignedField(6) & FlagArtificial) != 0;
673   }
674
675   bool isObjectPointer() const {
676     return (getUnsignedField(6) & FlagObjectPointer) != 0;
677   }
678
679   /// \brief Return true if this variable is represented as a pointer.
680   bool isIndirect() const {
681     return (getUnsignedField(6) & FlagIndirectVariable) != 0;
682   }
683
684   /// getInlinedAt - If this variable is inlined then return inline location.
685   MDNode *getInlinedAt() const;
686
687   /// Verify - Verify that a variable descriptor is well formed.
688   bool Verify() const;
689
690   /// HasComplexAddr - Return true if the variable has a complex address.
691   bool hasComplexAddress() const { return getNumAddrElements() > 0; }
692
693   /// \brief Return the size of this variable's complex address or
694   /// zero if there is none.
695   unsigned getNumAddrElements() const {
696     if (DbgNode->getNumOperands() < 9)
697       return 0;
698     return getDescriptorField(8)->getNumOperands();
699   }
700
701   /// \brief return the Idx'th complex address element.
702   uint64_t getAddrElement(unsigned Idx) const;
703
704   /// isBlockByrefVariable - Return true if the variable was declared as
705   /// a "__block" variable (Apple Blocks).
706   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
707     return (getType().resolve(Map)).isBlockByrefStruct();
708   }
709
710   /// isInlinedFnArgument - Return true if this variable provides debugging
711   /// information for an inlined function arguments.
712   bool isInlinedFnArgument(const Function *CurFn);
713
714   void printExtendedName(raw_ostream &OS) const;
715 };
716
717 /// DILocation - This object holds location information. This object
718 /// is not associated with any DWARF tag.
719 class DILocation : public DIDescriptor {
720 public:
721   explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
722
723   unsigned getLineNumber() const { return getUnsignedField(0); }
724   unsigned getColumnNumber() const { return getUnsignedField(1); }
725   DIScope getScope() const { return getFieldAs<DIScope>(2); }
726   DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
727   StringRef getFilename() const { return getScope().getFilename(); }
728   StringRef getDirectory() const { return getScope().getDirectory(); }
729   bool Verify() const;
730   bool atSameLineAs(const DILocation &Other) const {
731     return (getLineNumber() == Other.getLineNumber() &&
732             getFilename() == Other.getFilename());
733   }
734   /// getDiscriminator - DWARF discriminators are used to distinguish
735   /// identical file locations for instructions that are on different
736   /// basic blocks. If two instructions are inside the same lexical block
737   /// and are in different basic blocks, we create a new lexical block
738   /// with identical location as the original but with a different
739   /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
740   /// for details).
741   unsigned getDiscriminator() const {
742     // Since discriminators are associated with lexical blocks, make
743     // sure this location is a lexical block before retrieving its
744     // value.
745     return getScope().isLexicalBlock()
746                ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
747                : 0;
748   }
749   unsigned computeNewDiscriminator(LLVMContext &Ctx);
750   DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
751 };
752
753 class DIObjCProperty : public DIDescriptor {
754   friend class DIDescriptor;
755   void printInternal(raw_ostream &OS) const;
756
757 public:
758   explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
759
760   StringRef getObjCPropertyName() const { return getStringField(1); }
761   DIFile getFile() const { return getFieldAs<DIFile>(2); }
762   unsigned getLineNumber() const { return getUnsignedField(3); }
763
764   StringRef getObjCPropertyGetterName() const { return getStringField(4); }
765   StringRef getObjCPropertySetterName() const { return getStringField(5); }
766   bool isReadOnlyObjCProperty() const {
767     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
768   }
769   bool isReadWriteObjCProperty() const {
770     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
771   }
772   bool isAssignObjCProperty() const {
773     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
774   }
775   bool isRetainObjCProperty() const {
776     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
777   }
778   bool isCopyObjCProperty() const {
779     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
780   }
781   bool isNonAtomicObjCProperty() const {
782     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
783   }
784
785   /// Objective-C doesn't have an ODR, so there is no benefit in storing
786   /// the type as a DITypeRef here.
787   DIType getType() const { return getFieldAs<DIType>(7); }
788
789   /// Verify - Verify that a derived type descriptor is well formed.
790   bool Verify() const;
791 };
792
793 /// \brief An imported module (C++ using directive or similar).
794 class DIImportedEntity : public DIDescriptor {
795   friend class DIDescriptor;
796   void printInternal(raw_ostream &OS) const;
797
798 public:
799   explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
800   DIScope getContext() const { return getFieldAs<DIScope>(1); }
801   DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
802   unsigned getLineNumber() const { return getUnsignedField(3); }
803   StringRef getName() const { return getStringField(4); }
804   bool Verify() const;
805 };
806
807 /// getDISubprogram - Find subprogram that is enclosing this scope.
808 DISubprogram getDISubprogram(const MDNode *Scope);
809
810 /// getDICompositeType - Find underlying composite type.
811 DICompositeType getDICompositeType(DIType T);
812
813 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
814 /// to hold function specific information.
815 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
816
817 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
818 /// suitable to hold function specific information.
819 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
820
821 /// createInlinedVariable - Create a new inlined variable based on current
822 /// variable.
823 /// @param DV            Current Variable.
824 /// @param InlinedScope  Location at current variable is inlined.
825 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
826                                  LLVMContext &VMContext);
827
828 /// cleanseInlinedVariable - Remove inlined scope from the variable.
829 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
830
831 /// Construct DITypeIdentifierMap by going through retained types of each CU.
832 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
833
834 /// Strip debug info in the module if it exists.
835 /// To do this, we remove all calls to the debugger intrinsics and any named
836 /// metadata for debugging. We also remove debug locations for instructions.
837 /// Return true if module is modified.
838 bool StripDebugInfo(Module &M);
839
840 /// Return Debug Info Metadata Version by checking module flags.
841 unsigned getDebugMetadataVersionFromModule(const Module &M);
842
843 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
844 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
845 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
846 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
847 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
848 /// used by the CUs.
849 class DebugInfoFinder {
850 public:
851   DebugInfoFinder() : TypeMapInitialized(false) {}
852
853   /// processModule - Process entire module and collect debug info
854   /// anchors.
855   void processModule(const Module &M);
856
857   /// processDeclare - Process DbgDeclareInst.
858   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
859   /// Process DbgValueInst.
860   void processValue(const Module &M, const DbgValueInst *DVI);
861   /// processLocation - Process DILocation.
862   void processLocation(const Module &M, DILocation Loc);
863
864   /// Clear all lists.
865   void reset();
866
867 private:
868   /// Initialize TypeIdentifierMap.
869   void InitializeTypeMap(const Module &M);
870
871   /// processType - Process DIType.
872   void processType(DIType DT);
873
874   /// processSubprogram - Process DISubprogram.
875   void processSubprogram(DISubprogram SP);
876
877   void processScope(DIScope Scope);
878
879   /// addCompileUnit - Add compile unit into CUs.
880   bool addCompileUnit(DICompileUnit CU);
881
882   /// addGlobalVariable - Add global variable into GVs.
883   bool addGlobalVariable(DIGlobalVariable DIG);
884
885   // addSubprogram - Add subprogram into SPs.
886   bool addSubprogram(DISubprogram SP);
887
888   /// addType - Add type into Tys.
889   bool addType(DIType DT);
890
891   bool addScope(DIScope Scope);
892
893 public:
894   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
895   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
896   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
897   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
898   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
899
900   iterator_range<compile_unit_iterator> compile_units() const {
901     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
902   }
903
904   iterator_range<subprogram_iterator> subprograms() const {
905     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
906   }
907
908   iterator_range<global_variable_iterator> global_variables() const {
909     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
910   }
911
912   iterator_range<type_iterator> types() const {
913     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
914   }
915
916   iterator_range<scope_iterator> scopes() const {
917     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
918   }
919
920   unsigned compile_unit_count() const { return CUs.size(); }
921   unsigned global_variable_count() const { return GVs.size(); }
922   unsigned subprogram_count() const { return SPs.size(); }
923   unsigned type_count() const { return TYs.size(); }
924   unsigned scope_count() const { return Scopes.size(); }
925
926 private:
927   SmallVector<DICompileUnit, 8> CUs;    // Compile Units
928   SmallVector<DISubprogram, 8> SPs;    // Subprograms
929   SmallVector<DIGlobalVariable, 8> GVs;    // Global Variables;
930   SmallVector<DIType, 8> TYs;    // Types
931   SmallVector<DIScope, 8> Scopes; // Scopes
932   SmallPtrSet<MDNode *, 64> NodesSeen;
933   DITypeIdentifierMap TypeIdentifierMap;
934   /// Specify if TypeIdentifierMap is initialized.
935   bool TypeMapInitialized;
936 };
937 } // end namespace llvm
938
939 #endif