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