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