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