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