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