DebugInfo: Move DILocation::computeNewDiscriminators()
[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/ADT/iterator_range.h"
25 #include "llvm/IR/DebugInfoMetadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include <iterator>
30
31 namespace llvm {
32 class BasicBlock;
33 class Constant;
34 class Function;
35 class GlobalVariable;
36 class Module;
37 class Type;
38 class Value;
39 class DbgDeclareInst;
40 class DbgValueInst;
41 class Instruction;
42 class Metadata;
43 class MDNode;
44 class MDString;
45 class NamedMDNode;
46 class LLVMContext;
47 class raw_ostream;
48
49 class DIFile;
50 class DISubprogram;
51 class DILexicalBlock;
52 class DILexicalBlockFile;
53 class DIVariable;
54 class DIType;
55 class DIScope;
56 class DIObjCProperty;
57
58 /// \brief Maps from type identifier to the actual MDNode.
59 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
60
61 /// \brief A thin wraper around MDNode to access encoded debug info.
62 ///
63 /// This should not be stored in a container, because the underlying MDNode may
64 /// change in certain situations.
65 class DIDescriptor {
66 public:
67   /// \brief Duplicated debug info flags.
68   ///
69   /// \see DebugNode::DIFlags.
70   enum {
71 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = DebugNode::Flag##NAME,
72 #include "llvm/IR/DebugInfoFlags.def"
73     FlagAccessibility = DebugNode::FlagAccessibility
74   };
75
76 protected:
77   const MDNode *DbgNode;
78
79 public:
80   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
81   DIDescriptor(const DebugNode *N) : DbgNode(N) {}
82
83   MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
84   operator MDNode *() const { return get(); }
85   MDNode *operator->() const { return get(); }
86   MDNode &operator*() const { return *get(); }
87
88   // An explicit operator bool so that we can do testing of DI values
89   // easily.
90   // FIXME: This operator bool isn't actually protecting anything at the
91   // moment due to the conversion operator above making DIDescriptor nodes
92   // implicitly convertable to bool.
93   explicit operator bool() const { return DbgNode != nullptr; }
94
95   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
96   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
97
98   uint16_t getTag() const {
99     if (auto *N = dyn_cast_or_null<DebugNode>(get()))
100       return N->getTag();
101     return 0;
102   }
103
104   void print(raw_ostream &OS) const;
105   void dump() const;
106 };
107
108 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC)                                      \
109   class DESC;                                                                  \
110   template <> struct simplify_type<const DESC>;                                \
111   template <> struct simplify_type<DESC>;
112 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
113 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
114 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
115 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
116 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
117 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
118 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
119 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
120 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
121 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
122 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
123 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
124 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
125 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
126 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
127 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
128 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
129 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
130 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
131 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
132 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
133 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
134 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
135 #undef DECLARE_SIMPLIFY_DESCRIPTOR
136
137 typedef DebugNodeArray DIArray;
138 typedef MDTypeRefArray DITypeArray;
139
140 /// \brief This is used to represent ranges, for array bounds.
141 class DISubrange : public DIDescriptor {
142 public:
143   DISubrange() = default;
144   DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
145
146   MDSubrange *get() const {
147     return cast_or_null<MDSubrange>(DIDescriptor::get());
148   }
149   operator MDSubrange *() const { return get(); }
150   MDSubrange *operator->() const { return get(); }
151   MDSubrange &operator*() const { return *get(); }
152
153   int64_t getLo() const { return get()->getLowerBound(); }
154   int64_t getCount() const { return get()->getCount(); }
155 };
156
157 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
158 ///
159 /// FIXME: it seems strange that this doesn't have either a reference to the
160 /// type/precision or a file/line pair for location info.
161 class DIEnumerator : public DIDescriptor {
162 public:
163   DIEnumerator() = default;
164   DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
165
166   MDEnumerator *get() const {
167     return cast_or_null<MDEnumerator>(DIDescriptor::get());
168   }
169   operator MDEnumerator *() const { return get(); }
170   MDEnumerator *operator->() const { return get(); }
171   MDEnumerator &operator*() const { return *get(); }
172
173   StringRef getName() const { return get()->getName(); }
174   int64_t getEnumValue() const { return get()->getValue(); }
175 };
176
177 template <typename T> class DIRef;
178 typedef DIRef<DIDescriptor> DIDescriptorRef;
179 typedef DIRef<DIScope> DIScopeRef;
180 typedef DIRef<DIType> DITypeRef;
181
182 /// \brief A base class for various scopes.
183 ///
184 /// Although, implementation-wise, DIScope is the parent class of most
185 /// other DIxxx classes, including DIType and its descendants, most of
186 /// DIScope's descendants are not a substitutable subtype of
187 /// DIScope. The DIDescriptor::isScope() method only is true for
188 /// DIScopes that are scopes in the strict lexical scope sense
189 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
190 class DIScope : public DIDescriptor {
191 public:
192   DIScope() = default;
193   DIScope(const MDScope *N) : DIDescriptor(N) {}
194
195   MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
196   operator MDScope *() const { return get(); }
197   MDScope *operator->() const { return get(); }
198   MDScope &operator*() const { return *get(); }
199
200   inline DIScopeRef getContext() const;
201   StringRef getName() const { return get()->getName(); }
202   StringRef getFilename() const { return get()->getFilename(); }
203   StringRef getDirectory() const { return get()->getDirectory(); }
204
205   /// \brief Generate a reference to this DIScope.
206   ///
207   /// Uses the type identifier instead of the actual MDNode if possible, to
208   /// help type uniquing.
209   DIScopeRef getRef() const;
210 };
211
212 /// \brief Represents reference to a DIDescriptor.
213 ///
214 /// Abstracts over direct and identifier-based metadata references.
215 template <typename T> class DIRef {
216   /// \brief Val can be either a MDNode or a MDString.
217   ///
218   /// In the latter, MDString specifies the type identifier.
219   const Metadata *Val;
220
221 public:
222   template <class U>
223   DIRef(const TypedDebugNodeRef<U> &Ref,
224         typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
225             nullptr)
226       : Val(Ref) {}
227
228   T resolve(const DITypeIdentifierMap &Map) const;
229   operator Metadata *() const { return const_cast<Metadata *>(Val); }
230 };
231
232 template <>
233 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
234 template <>
235 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
236 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
237
238 DIScopeRef DIScope::getContext() const { return get()->getScope(); }
239
240 /// \brief This is a wrapper for a type.
241 ///
242 /// FIXME: Types should be factored much better so that CV qualifiers and
243 /// others do not require a huge and empty descriptor full of zeros.
244 class DIType : public DIScope {
245 public:
246   DIType() = default;
247   DIType(const MDType *N) : DIScope(N) {}
248
249   MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
250   operator MDType *() const { return get(); }
251   MDType *operator->() const { return get(); }
252   MDType &operator*() const { return *get(); }
253
254   DIScopeRef getContext() const { return get()->getScope(); }
255   StringRef getName() const { return get()->getName(); }
256   unsigned getLineNumber() const { return get()->getLine(); }
257   uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
258   uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
259   // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
260   // carry this is just plain insane.
261   uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
262   unsigned getFlags() const { return get()->getFlags(); }
263
264   bool isPrivate() const { return get()->isPrivate(); }
265   bool isProtected() const { return get()->isProtected(); }
266   bool isPublic() const { return get()->isPublic(); }
267   bool isForwardDecl() const { return get()->isForwardDecl(); }
268   bool isAppleBlockExtension() const { return get()->isAppleBlockExtension(); }
269   bool isBlockByrefStruct() const { return get()->isBlockByrefStruct(); }
270   bool isVirtual() const { return get()->isVirtual(); }
271   bool isArtificial() const { return get()->isArtificial(); }
272   bool isObjectPointer() const { return get()->isObjectPointer(); }
273   bool isObjcClassComplete() const { return get()->isObjcClassComplete(); }
274   bool isVector() const { return get()->isVector(); }
275   bool isStaticMember() const { return get()->isStaticMember(); }
276   bool isLValueReference() const { return get()->isLValueReference(); }
277   bool isRValueReference() const { return get()->isRValueReference(); }
278
279   bool isValid() const { return DbgNode && isa<MDType>(*this); }
280 };
281
282 /// \brief A basic type, like 'int' or 'float'.
283 class DIBasicType : public DIType {
284 public:
285   DIBasicType() = default;
286   DIBasicType(const MDBasicType *N) : DIType(N) {}
287
288   MDBasicType *get() const {
289     return cast_or_null<MDBasicType>(DIDescriptor::get());
290   }
291   operator MDBasicType *() const { return get(); }
292   MDBasicType *operator->() const { return get(); }
293   MDBasicType &operator*() const { return *get(); }
294
295   unsigned getEncoding() const { return get()->getEncoding(); }
296 };
297
298 /// \brief A simple derived type
299 ///
300 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
301 /// Or, a data member of a class/struct/union.
302 class DIDerivedType : public DIType {
303 public:
304   DIDerivedType() = default;
305   DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
306
307   MDDerivedTypeBase *get() const {
308     return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
309   }
310   operator MDDerivedTypeBase *() const { return get(); }
311   MDDerivedTypeBase *operator->() const { return get(); }
312   MDDerivedTypeBase &operator*() const { return *get(); }
313
314   DITypeRef getTypeDerivedFrom() const { return get()->getBaseType(); }
315
316   /// \brief Return property node, if this ivar is associated with one.
317   MDObjCProperty *getObjCProperty() const {
318     return cast<MDDerivedType>(get())->getObjCProperty();
319   }
320
321   DITypeRef getClassType() const {
322     return cast<MDDerivedType>(get())->getClassType();
323   }
324
325   Constant *getConstant() const {
326     return cast<MDDerivedType>(get())->getConstant();
327   }
328 };
329
330 /// \brief Types that refer to multiple other types.
331 ///
332 /// This descriptor holds a type that can refer to multiple other types, like a
333 /// function or struct.
334 ///
335 /// DICompositeType is derived from DIDerivedType because some
336 /// composite types (such as enums) can be derived from basic types
337 // FIXME: Make this derive from DIType directly & just store the
338 // base type in a single DIType field.
339 class DICompositeType : public DIDerivedType {
340   friend class DIBuilder;
341
342 public:
343   DICompositeType() = default;
344   DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
345
346   MDCompositeTypeBase *get() const {
347     return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
348   }
349   operator MDCompositeTypeBase *() const { return get(); }
350   MDCompositeTypeBase *operator->() const { return get(); }
351   MDCompositeTypeBase &operator*() const { return *get(); }
352
353   DIArray getElements() const {
354     assert(!isa<MDSubroutineType>(*this) && "no elements for DISubroutineType");
355     return DIArray(get()->getElements());
356   }
357
358   unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
359   DITypeRef getContainingType() const { return get()->getVTableHolder(); }
360
361   DIArray getTemplateParams() const { return get()->getTemplateParams(); }
362   MDString *getIdentifier() const { return get()->getRawIdentifier(); }
363 };
364
365 class DISubroutineType : public DICompositeType {
366 public:
367   DISubroutineType() = default;
368   DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
369
370   MDSubroutineType *get() const {
371     return cast_or_null<MDSubroutineType>(DIDescriptor::get());
372   }
373   operator MDSubroutineType *() const { return get(); }
374   MDSubroutineType *operator->() const { return get(); }
375   MDSubroutineType &operator*() const { return *get(); }
376
377   MDTypeRefArray getTypeArray() const { return get()->getTypeArray(); }
378 };
379
380 /// \brief This is a wrapper for a file.
381 class DIFile : public DIScope {
382 public:
383   DIFile() = default;
384   DIFile(const MDFile *N) : DIScope(N) {}
385
386   MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
387   operator MDFile *() const { return get(); }
388   MDFile *operator->() const { return get(); }
389   MDFile &operator*() const { return *get(); }
390
391   /// \brief Retrieve the MDNode for the directory/file pair.
392   MDNode *getFileNode() const { return get(); }
393 };
394
395 /// \brief A wrapper for a compile unit.
396 class DICompileUnit : public DIScope {
397 public:
398   DICompileUnit() = default;
399   DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
400
401   MDCompileUnit *get() const {
402     return cast_or_null<MDCompileUnit>(DIDescriptor::get());
403   }
404   operator MDCompileUnit *() const { return get(); }
405   MDCompileUnit *operator->() const { return get(); }
406   MDCompileUnit &operator*() const { return *get(); }
407
408   dwarf::SourceLanguage getLanguage() const {
409     return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
410   }
411   StringRef getProducer() const { return get()->getProducer(); }
412   bool isOptimized() const { return get()->isOptimized(); }
413   StringRef getFlags() const { return get()->getFlags(); }
414   unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
415
416   DIArray getEnumTypes() const { return get()->getEnumTypes(); }
417   DIArray getRetainedTypes() const { return get()->getRetainedTypes(); }
418   DIArray getSubprograms() const { return get()->getSubprograms(); }
419   DIArray getGlobalVariables() const { return get()->getGlobalVariables(); }
420   DIArray getImportedEntities() const { return get()->getImportedEntities(); }
421
422   void replaceSubprograms(DIArray Subprograms);
423   void replaceGlobalVariables(DIArray GlobalVariables);
424
425   StringRef getSplitDebugFilename() const {
426     return get()->getSplitDebugFilename();
427   }
428   unsigned getEmissionKind() const { return get()->getEmissionKind(); }
429 };
430
431 /// \brief This is a wrapper for a subprogram (e.g. a function).
432 class DISubprogram : public DIScope {
433 public:
434   DISubprogram() = default;
435   DISubprogram(const MDSubprogram *N) : DIScope(N) {}
436
437   MDSubprogram *get() const {
438     return cast_or_null<MDSubprogram>(DIDescriptor::get());
439   }
440   operator MDSubprogram *() const { return get(); }
441   MDSubprogram *operator->() const { return get(); }
442   MDSubprogram &operator*() const { return *get(); }
443
444   StringRef getName() const { return get()->getName(); }
445   StringRef getDisplayName() const { return get()->getDisplayName(); }
446   StringRef getLinkageName() const { return get()->getLinkageName(); }
447   unsigned getLineNumber() const { return get()->getLine(); }
448
449   /// \brief Check if this is local (like 'static' in C).
450   unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
451   unsigned isDefinition() const { return get()->isDefinition(); }
452
453   unsigned getVirtuality() const { return get()->getVirtuality(); }
454   unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
455
456   unsigned getFlags() const { return get()->getFlags(); }
457
458   unsigned isOptimized() const { return get()->isOptimized(); }
459
460   /// \brief Get the beginning of the scope of the function (not the name).
461   unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
462
463   DIScopeRef getContext() const { return get()->getScope(); }
464   DISubroutineType getType() const { return get()->getType(); }
465
466   DITypeRef getContainingType() const { return get()->getContainingType(); }
467
468   /// \brief Check if this provides debugging information for the function F.
469   bool describes(const Function *F) const { return get()->describes(F); }
470
471   Function *getFunction() const { return get()->getFunction(); }
472
473   void replaceFunction(Function *F) { get()->replaceFunction(F); }
474   DIArray getTemplateParams() const { return get()->getTemplateParams(); }
475   DISubprogram getFunctionDeclaration() const {
476     return get()->getDeclaration();
477   }
478   DIArray getVariables() const { return DIArray(get()->getVariables()); }
479
480   unsigned isArtificial() const { return get()->isArtificial(); }
481   bool isPrivate() const { return get()->isPrivate(); }
482   bool isProtected() const { return get()->isProtected(); }
483   bool isPublic() const { return get()->isPublic(); }
484   bool isExplicit() const { return get()->isExplicit(); }
485   bool isPrototyped() const { return get()->isPrototyped(); }
486   unsigned isLValueReference() const { return get()->isLValueReference(); }
487   unsigned isRValueReference() const { return get()->isRValueReference(); }
488 };
489
490 /// \brief This is a wrapper for a lexical block.
491 class DILexicalBlock : public DIScope {
492 public:
493   DILexicalBlock() = default;
494   DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
495
496   MDLexicalBlockBase *get() const {
497     return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
498   }
499   operator MDLexicalBlockBase *() const { return get(); }
500   MDLexicalBlockBase *operator->() const { return get(); }
501   MDLexicalBlockBase &operator*() const { return *get(); }
502
503   DIScope getContext() const { return DIScope(get()->getScope()); }
504   unsigned getLineNumber() const {
505     if (auto *N = dyn_cast<MDLexicalBlock>(get()))
506       return N->getLine();
507     return 0;
508   }
509   unsigned getColumnNumber() const {
510     if (auto *N = dyn_cast<MDLexicalBlock>(get()))
511       return N->getColumn();
512     return 0;
513   }
514 };
515
516 /// \brief This is a wrapper for a lexical block with a filename change.
517 class DILexicalBlockFile : public DIScope {
518 public:
519   DILexicalBlockFile() = default;
520   DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
521
522   MDLexicalBlockFile *get() const {
523     return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
524   }
525   operator MDLexicalBlockFile *() const { return get(); }
526   MDLexicalBlockFile *operator->() const { return get(); }
527   MDLexicalBlockFile &operator*() const { return *get(); }
528
529   DIScope getContext() const { return get()->getScope(); }
530   unsigned getDiscriminator() const { return get()->getDiscriminator(); }
531 };
532
533 /// \brief A wrapper for a C++ style name space.
534 class DINameSpace : public DIScope {
535 public:
536   DINameSpace() = default;
537   DINameSpace(const MDNamespace *N) : DIScope(N) {}
538
539   MDNamespace *get() const {
540     return cast_or_null<MDNamespace>(DIDescriptor::get());
541   }
542   operator MDNamespace *() const { return get(); }
543   MDNamespace *operator->() const { return get(); }
544   MDNamespace &operator*() const { return *get(); }
545
546   StringRef getName() const { return get()->getName(); }
547   unsigned getLineNumber() const { return get()->getLine(); }
548   DIScope getContext() const { return DIScope(get()->getScope()); }
549 };
550
551 /// \brief This is a wrapper for template type parameter.
552 class DITemplateTypeParameter : public DIDescriptor {
553 public:
554   DITemplateTypeParameter() = default;
555   DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
556
557   MDTemplateTypeParameter *get() const {
558     return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
559   }
560   operator MDTemplateTypeParameter *() const { return get(); }
561   MDTemplateTypeParameter *operator->() const { return get(); }
562   MDTemplateTypeParameter &operator*() const { return *get(); }
563
564   StringRef getName() const { return get()->getName(); }
565   DITypeRef getType() const { return get()->getType(); }
566 };
567
568 /// \brief This is a wrapper for template value parameter.
569 class DITemplateValueParameter : public DIDescriptor {
570 public:
571   DITemplateValueParameter() = default;
572   DITemplateValueParameter(const MDTemplateValueParameter *N)
573       : DIDescriptor(N) {}
574
575   MDTemplateValueParameter *get() const {
576     return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
577   }
578   operator MDTemplateValueParameter *() const { return get(); }
579   MDTemplateValueParameter *operator->() const { return get(); }
580   MDTemplateValueParameter &operator*() const { return *get(); }
581
582   StringRef getName() const { return get()->getName(); }
583   DITypeRef getType() const { return get()->getType(); }
584   Metadata *getValue() const { return get()->getValue(); }
585 };
586
587 /// \brief This is a wrapper for a global variable.
588 class DIGlobalVariable : public DIDescriptor {
589   DIFile getFile() const { return DIFile(get()->getFile()); }
590
591 public:
592   DIGlobalVariable() = default;
593   DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
594
595   MDGlobalVariable *get() const {
596     return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
597   }
598   operator MDGlobalVariable *() const { return get(); }
599   MDGlobalVariable *operator->() const { return get(); }
600   MDGlobalVariable &operator*() const { return *get(); }
601
602   StringRef getName() const { return get()->getName(); }
603   StringRef getDisplayName() const { return get()->getDisplayName(); }
604   StringRef getLinkageName() const { return get()->getLinkageName(); }
605   unsigned getLineNumber() const { return get()->getLine(); }
606   unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
607   unsigned isDefinition() const { return get()->isDefinition(); }
608
609   DIScope getContext() const { return get()->getScope(); }
610   StringRef getFilename() const { return get()->getFilename(); }
611   StringRef getDirectory() const { return get()->getDirectory(); }
612   DITypeRef getType() const { return get()->getType(); }
613
614   Constant *getConstant() const { return get()->getVariable(); }
615   DIDerivedType getStaticDataMemberDeclaration() const {
616     return get()->getStaticDataMemberDeclaration();
617   }
618 };
619
620 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
621 class DIVariable : public DIDescriptor {
622   unsigned getFlags() const { return get()->getFlags(); }
623
624 public:
625   DIVariable() = default;
626   DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
627
628   MDLocalVariable *get() const {
629     return cast_or_null<MDLocalVariable>(DIDescriptor::get());
630   }
631   operator MDLocalVariable *() const { return get(); }
632   MDLocalVariable *operator->() const { return get(); }
633   MDLocalVariable &operator*() const { return *get(); }
634
635   StringRef getName() const { return get()->getName(); }
636   unsigned getLineNumber() const { return get()->getLine(); }
637   unsigned getArgNumber() const { return get()->getArg(); }
638
639   DIScope getContext() const { return get()->getScope(); }
640   DIFile getFile() const { return get()->getFile(); }
641   DITypeRef getType() const { return get()->getType(); }
642
643   bool isArtificial() const { return get()->isArtificial(); }
644   bool isObjectPointer() const { return get()->isObjectPointer(); }
645
646   /// \brief If this variable is inlined then return inline location.
647   MDNode *getInlinedAt() const { return get()->getInlinedAt(); }
648
649   /// \brief Check if this is a "__block" variable (Apple Blocks).
650   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
651     return (getType().resolve(Map)).isBlockByrefStruct();
652   }
653
654   void printExtendedName(raw_ostream &OS) const;
655 };
656
657 /// \brief A complex location expression in postfix notation.
658 ///
659 /// This is (almost) a DWARF expression that modifies the location of a
660 /// variable or (or the location of a single piece of a variable).
661 ///
662 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
663 /// and have DW_OP_plus consume the topmost elements on the stack.
664 class DIExpression : public DIDescriptor {
665 public:
666   DIExpression() = default;
667   DIExpression(const MDExpression *N) : DIDescriptor(N) {}
668
669   MDExpression *get() const {
670     return cast_or_null<MDExpression>(DIDescriptor::get());
671   }
672   operator MDExpression *() const { return get(); }
673   MDExpression *operator->() const { return get(); }
674   MDExpression &operator*() const { return *get(); }
675
676   unsigned getNumElements() const { return get()->getNumElements(); }
677   uint64_t getElement(unsigned I) const { return get()->getElement(I); }
678   bool isBitPiece() const { return get()->isBitPiece(); }
679   uint64_t getBitPieceOffset() const { return get()->getBitPieceOffset(); }
680   uint64_t getBitPieceSize() const { return get()->getBitPieceSize(); }
681 };
682
683 /// \brief This object holds location information.
684 ///
685 /// This object is not associated with any DWARF tag.
686 class DILocation : public DIDescriptor {
687 public:
688   DILocation() = default;
689   DILocation(const MDLocation *N) : DIDescriptor(N) {}
690
691   MDLocation *get() const {
692     return cast_or_null<MDLocation>(DIDescriptor::get());
693   }
694   operator MDLocation *() const { return get(); }
695   MDLocation *operator->() const { return get(); }
696   MDLocation &operator*() const { return *get(); }
697
698   unsigned getLineNumber() const { return get()->getLine(); }
699   unsigned getColumnNumber() const { return get()->getColumn(); }
700   DIScope getScope() const { return DIScope(get()->getScope()); }
701   DILocation getOrigLocation() const { return get()->getInlinedAt(); }
702   StringRef getFilename() const { return get()->getFilename(); }
703   StringRef getDirectory() const { return get()->getDirectory(); }
704   unsigned getDiscriminator() const { return get()->getDiscriminator(); }
705 };
706
707 class DIObjCProperty : public DIDescriptor {
708 public:
709   DIObjCProperty() = default;
710   DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
711
712   MDObjCProperty *get() const {
713     return cast_or_null<MDObjCProperty>(DIDescriptor::get());
714   }
715   operator MDObjCProperty *() const { return get(); }
716   MDObjCProperty *operator->() const { return get(); }
717   MDObjCProperty &operator*() const { return *get(); }
718
719   StringRef getObjCPropertyName() const { return get()->getName(); }
720   DIFile getFile() const { return get()->getFile(); }
721   unsigned getLineNumber() const { return get()->getLine(); }
722
723   StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
724   StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
725   unsigned getAttributes() const { return get()->getAttributes(); }
726
727   /// \brief Get the type.
728   ///
729   /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
730   /// the type as a DITypeRef here.
731   DIType getType() const { return get()->getType(); }
732 };
733
734 /// \brief An imported module (C++ using directive or similar).
735 class DIImportedEntity : public DIDescriptor {
736 public:
737   DIImportedEntity() = default;
738   DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
739
740   MDImportedEntity *get() const {
741     return cast_or_null<MDImportedEntity>(DIDescriptor::get());
742   }
743   operator MDImportedEntity *() const { return get(); }
744   MDImportedEntity *operator->() const { return get(); }
745   MDImportedEntity &operator*() const { return *get(); }
746
747   DIScope getContext() const { return get()->getScope(); }
748   DIDescriptorRef getEntity() const { return get()->getEntity(); }
749   unsigned getLineNumber() const { return get()->getLine(); }
750   StringRef getName() const { return get()->getName(); }
751 };
752
753 #define SIMPLIFY_DESCRIPTOR(DESC)                                              \
754   template <> struct simplify_type<const DESC> {                               \
755     typedef Metadata *SimpleType;                                              \
756     static SimpleType getSimplifiedValue(const DESC &DI) { return DI; }        \
757   };                                                                           \
758   template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
759 SIMPLIFY_DESCRIPTOR(DIDescriptor)
760 SIMPLIFY_DESCRIPTOR(DISubrange)
761 SIMPLIFY_DESCRIPTOR(DIEnumerator)
762 SIMPLIFY_DESCRIPTOR(DIScope)
763 SIMPLIFY_DESCRIPTOR(DIType)
764 SIMPLIFY_DESCRIPTOR(DIBasicType)
765 SIMPLIFY_DESCRIPTOR(DIDerivedType)
766 SIMPLIFY_DESCRIPTOR(DICompositeType)
767 SIMPLIFY_DESCRIPTOR(DISubroutineType)
768 SIMPLIFY_DESCRIPTOR(DIFile)
769 SIMPLIFY_DESCRIPTOR(DICompileUnit)
770 SIMPLIFY_DESCRIPTOR(DISubprogram)
771 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
772 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
773 SIMPLIFY_DESCRIPTOR(DINameSpace)
774 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
775 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
776 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
777 SIMPLIFY_DESCRIPTOR(DIVariable)
778 SIMPLIFY_DESCRIPTOR(DIExpression)
779 SIMPLIFY_DESCRIPTOR(DILocation)
780 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
781 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
782 #undef SIMPLIFY_DESCRIPTOR
783
784 /// \brief Find subprogram that is enclosing this scope.
785 DISubprogram getDISubprogram(const MDNode *Scope);
786
787 /// \brief Find debug info for a given function.
788 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
789 /// DISubprogram.
790 DISubprogram getDISubprogram(const Function *F);
791
792 /// \brief Find underlying composite type.
793 DICompositeType getDICompositeType(DIType T);
794
795 /// \brief Create a new inlined variable based on current variable.
796 ///
797 /// @param DV            Current Variable.
798 /// @param InlinedScope  Location at current variable is inlined.
799 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
800                                  LLVMContext &VMContext);
801
802 /// \brief Remove inlined scope from the variable.
803 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
804
805 /// \brief Generate map by visiting all retained types.
806 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
807
808 /// \brief Strip debug info in the module if it exists.
809 ///
810 /// To do this, we remove all calls to the debugger intrinsics and any named
811 /// metadata for debugging. We also remove debug locations for instructions.
812 /// Return true if module is modified.
813 bool StripDebugInfo(Module &M);
814 bool stripDebugInfo(Function &F);
815
816 /// \brief Return Debug Info Metadata Version by checking module flags.
817 unsigned getDebugMetadataVersionFromModule(const Module &M);
818
819 /// \brief Utility to find all debug info in a module.
820 ///
821 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
822 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
823 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
824 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
825 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
826 /// used by the CUs.
827 class DebugInfoFinder {
828 public:
829   DebugInfoFinder() : TypeMapInitialized(false) {}
830
831   /// \brief Process entire module and collect debug info anchors.
832   void processModule(const Module &M);
833
834   /// \brief Process DbgDeclareInst.
835   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
836   /// \brief Process DbgValueInst.
837   void processValue(const Module &M, const DbgValueInst *DVI);
838   /// \brief Process DILocation.
839   void processLocation(const Module &M, DILocation Loc);
840
841   /// \brief Clear all lists.
842   void reset();
843
844 private:
845   void InitializeTypeMap(const Module &M);
846
847   void processType(DIType DT);
848   void processSubprogram(DISubprogram SP);
849   void processScope(DIScope Scope);
850   bool addCompileUnit(DICompileUnit CU);
851   bool addGlobalVariable(DIGlobalVariable DIG);
852   bool addSubprogram(DISubprogram SP);
853   bool addType(DIType DT);
854   bool addScope(DIScope Scope);
855
856 public:
857   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
858   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
859   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
860       global_variable_iterator;
861   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
862   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
863
864   iterator_range<compile_unit_iterator> compile_units() const {
865     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
866   }
867
868   iterator_range<subprogram_iterator> subprograms() const {
869     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
870   }
871
872   iterator_range<global_variable_iterator> global_variables() const {
873     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
874   }
875
876   iterator_range<type_iterator> types() const {
877     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
878   }
879
880   iterator_range<scope_iterator> scopes() const {
881     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
882   }
883
884   unsigned compile_unit_count() const { return CUs.size(); }
885   unsigned global_variable_count() const { return GVs.size(); }
886   unsigned subprogram_count() const { return SPs.size(); }
887   unsigned type_count() const { return TYs.size(); }
888   unsigned scope_count() const { return Scopes.size(); }
889
890 private:
891   SmallVector<DICompileUnit, 8> CUs;
892   SmallVector<DISubprogram, 8> SPs;
893   SmallVector<DIGlobalVariable, 8> GVs;
894   SmallVector<DIType, 8> TYs;
895   SmallVector<DIScope, 8> Scopes;
896   SmallPtrSet<MDNode *, 64> NodesSeen;
897   DITypeIdentifierMap TypeIdentifierMap;
898
899   /// \brief Specify if TypeIdentifierMap is initialized.
900   bool TypeMapInitialized;
901 };
902
903 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
904
905 } // end namespace llvm
906
907 #endif