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