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