]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGDebugInfo.h
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / CodeGen / CGDebugInfo.h
1 //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- 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 is the source-level debug info generator for llvm translation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
16
17 #include "CGBuilder.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExternalASTSource.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "clang/Frontend/CodeGenOptions.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/IR/DIBuilder.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/Allocator.h"
31
32 namespace llvm {
33 class MDNode;
34 }
35
36 namespace clang {
37 class ClassTemplateSpecializationDecl;
38 class GlobalDecl;
39 class ModuleMap;
40 class ObjCInterfaceDecl;
41 class ObjCIvarDecl;
42 class UsingDecl;
43 class VarDecl;
44
45 namespace CodeGen {
46 class CodeGenModule;
47 class CodeGenFunction;
48 class CGBlockInfo;
49
50 /// This class gathers all debug information during compilation and is
51 /// responsible for emitting to llvm globals or pass directly to the
52 /// backend.
53 class CGDebugInfo {
54   friend class ApplyDebugLocation;
55   friend class SaveAndRestoreLocation;
56   CodeGenModule &CGM;
57   const codegenoptions::DebugInfoKind DebugKind;
58   bool DebugTypeExtRefs;
59   llvm::DIBuilder DBuilder;
60   llvm::DICompileUnit *TheCU = nullptr;
61   ModuleMap *ClangModuleMap = nullptr;
62   ExternalASTSource::ASTSourceDescriptor PCHDescriptor;
63   SourceLocation CurLoc;
64   llvm::MDNode *CurInlinedAt = nullptr;
65   llvm::DIType *VTablePtrType = nullptr;
66   llvm::DIType *ClassTy = nullptr;
67   llvm::DICompositeType *ObjTy = nullptr;
68   llvm::DIType *SelTy = nullptr;
69 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
70   llvm::DIType *SingletonId = nullptr;
71 #include "clang/Basic/OpenCLImageTypes.def"
72   llvm::DIType *OCLSamplerDITy = nullptr;
73   llvm::DIType *OCLEventDITy = nullptr;
74   llvm::DIType *OCLClkEventDITy = nullptr;
75   llvm::DIType *OCLQueueDITy = nullptr;
76   llvm::DIType *OCLNDRangeDITy = nullptr;
77   llvm::DIType *OCLReserveIDDITy = nullptr;
78
79   /// Cache of previously constructed Types.
80   llvm::DenseMap<const void *, llvm::TrackingMDRef> TypeCache;
81
82   llvm::SmallDenseMap<llvm::StringRef, llvm::StringRef> DebugPrefixMap;
83
84   struct ObjCInterfaceCacheEntry {
85     const ObjCInterfaceType *Type;
86     llvm::DIType *Decl;
87     llvm::DIFile *Unit;
88     ObjCInterfaceCacheEntry(const ObjCInterfaceType *Type, llvm::DIType *Decl,
89                             llvm::DIFile *Unit)
90         : Type(Type), Decl(Decl), Unit(Unit) {}
91   };
92
93   /// Cache of previously constructed interfaces which may change.
94   llvm::SmallVector<ObjCInterfaceCacheEntry, 32> ObjCInterfaceCache;
95
96   /// Cache of references to clang modules and precompiled headers.
97   llvm::DenseMap<const Module *, llvm::TrackingMDRef> ModuleCache;
98
99   /// List of interfaces we want to keep even if orphaned.
100   std::vector<void *> RetainedTypes;
101
102   /// Cache of forward declared types to RAUW at the end of compilation.
103   std::vector<std::pair<const TagType *, llvm::TrackingMDRef>> ReplaceMap;
104
105   /// Cache of replaceable forward declarations (functions and
106   /// variables) to RAUW at the end of compilation.
107   std::vector<std::pair<const DeclaratorDecl *, llvm::TrackingMDRef>>
108       FwdDeclReplaceMap;
109
110   /// Keep track of our current nested lexical block.
111   std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
112   llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
113   /// Keep track of LexicalBlockStack counter at the beginning of a
114   /// function. This is used to pop unbalanced regions at the end of a
115   /// function.
116   std::vector<unsigned> FnBeginRegionCount;
117
118   /// This is a storage for names that are constructed on demand. For
119   /// example, C++ destructors, C++ operators etc..
120   llvm::BumpPtrAllocator DebugInfoNames;
121   StringRef CWDName;
122
123   llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache;
124   llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache;
125   /// Cache declarations relevant to DW_TAG_imported_declarations (C++
126   /// using declarations) that aren't covered by other more specific caches.
127   llvm::DenseMap<const Decl *, llvm::TrackingMDRef> DeclCache;
128   llvm::DenseMap<const NamespaceDecl *, llvm::TrackingMDRef> NamespaceCache;
129   llvm::DenseMap<const NamespaceAliasDecl *, llvm::TrackingMDRef>
130       NamespaceAliasCache;
131   llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>>
132       StaticDataMemberCache;
133
134   /// Helper functions for getOrCreateType.
135   /// @{
136   /// Currently the checksum of an interface includes the number of
137   /// ivars and property accessors.
138   llvm::DIType *CreateType(const BuiltinType *Ty);
139   llvm::DIType *CreateType(const ComplexType *Ty);
140   llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg);
141   llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg);
142   llvm::DIType *CreateType(const TemplateSpecializationType *Ty,
143                            llvm::DIFile *Fg);
144   llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F);
145   llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F);
146   llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F);
147   llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F);
148   /// Get structure or union type.
149   llvm::DIType *CreateType(const RecordType *Tyg);
150   llvm::DIType *CreateTypeDefinition(const RecordType *Ty);
151   llvm::DICompositeType *CreateLimitedType(const RecordType *Ty);
152   void CollectContainingType(const CXXRecordDecl *RD,
153                              llvm::DICompositeType *CT);
154   /// Get Objective-C interface type.
155   llvm::DIType *CreateType(const ObjCInterfaceType *Ty, llvm::DIFile *F);
156   llvm::DIType *CreateTypeDefinition(const ObjCInterfaceType *Ty,
157                                      llvm::DIFile *F);
158   /// Get Objective-C object type.
159   llvm::DIType *CreateType(const ObjCObjectType *Ty, llvm::DIFile *F);
160   llvm::DIType *CreateType(const ObjCTypeParamType *Ty, llvm::DIFile *Unit);
161
162   llvm::DIType *CreateType(const VectorType *Ty, llvm::DIFile *F);
163   llvm::DIType *CreateType(const ArrayType *Ty, llvm::DIFile *F);
164   llvm::DIType *CreateType(const LValueReferenceType *Ty, llvm::DIFile *F);
165   llvm::DIType *CreateType(const RValueReferenceType *Ty, llvm::DIFile *Unit);
166   llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F);
167   llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F);
168   llvm::DIType *CreateType(const PipeType *Ty, llvm::DIFile *F);
169   /// Get enumeration type.
170   llvm::DIType *CreateEnumType(const EnumType *Ty);
171   llvm::DIType *CreateTypeDefinition(const EnumType *Ty);
172   /// Look up the completed type for a self pointer in the TypeCache and
173   /// create a copy of it with the ObjectPointer and Artificial flags
174   /// set. If the type is not cached, a new one is created. This should
175   /// never happen though, since creating a type for the implicit self
176   /// argument implies that we already parsed the interface definition
177   /// and the ivar declarations in the implementation.
178   llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty);
179   /// @}
180
181   /// Get the type from the cache or return null type if it doesn't
182   /// exist.
183   llvm::DIType *getTypeOrNull(const QualType);
184   /// Return the debug type for a C++ method.
185   /// \arg CXXMethodDecl is of FunctionType. This function type is
186   /// not updated to include implicit \c this pointer. Use this routine
187   /// to get a method type which includes \c this pointer.
188   llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method,
189                                                 llvm::DIFile *F);
190   llvm::DISubroutineType *
191   getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func,
192                                 llvm::DIFile *Unit);
193   llvm::DISubroutineType *
194   getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F);
195   /// \return debug info descriptor for vtable.
196   llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F);
197
198   /// \return namespace descriptor for the given namespace decl.
199   llvm::DINamespace *getOrCreateNamespace(const NamespaceDecl *N);
200   llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty,
201                                       QualType PointeeTy, llvm::DIFile *F);
202   llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache);
203
204   /// A helper function to create a subprogram for a single member
205   /// function GlobalDecl.
206   llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method,
207                                               llvm::DIFile *F,
208                                               llvm::DIType *RecordTy);
209
210   /// A helper function to collect debug info for C++ member
211   /// functions. This is used while creating debug info entry for a
212   /// Record.
213   void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F,
214                                  SmallVectorImpl<llvm::Metadata *> &E,
215                                  llvm::DIType *T);
216
217   /// A helper function to collect debug info for C++ base
218   /// classes. This is used while creating debug info entry for a
219   /// Record.
220   void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F,
221                        SmallVectorImpl<llvm::Metadata *> &EltTys,
222                        llvm::DIType *RecordTy);
223
224   /// Helper function for CollectCXXBases.
225   /// Adds debug info entries for types in Bases that are not in SeenTypes.
226   void CollectCXXBasesAux(const CXXRecordDecl *RD, llvm::DIFile *Unit,
227                           SmallVectorImpl<llvm::Metadata *> &EltTys,
228                           llvm::DIType *RecordTy,
229                           const CXXRecordDecl::base_class_const_range &Bases,
230                           llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
231                           llvm::DINode::DIFlags StartingFlags);
232
233   /// A helper function to collect template parameters.
234   llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList,
235                                           ArrayRef<TemplateArgument> TAList,
236                                           llvm::DIFile *Unit);
237   /// A helper function to collect debug info for function template
238   /// parameters.
239   llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD,
240                                                   llvm::DIFile *Unit);
241
242   /// A helper function to collect debug info for template
243   /// parameters.
244   llvm::DINodeArray
245   CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
246                            llvm::DIFile *F);
247
248   llvm::DIType *createFieldType(StringRef name, QualType type,
249                                 SourceLocation loc, AccessSpecifier AS,
250                                 uint64_t offsetInBits,
251                                 uint32_t AlignInBits,
252                                 llvm::DIFile *tunit, llvm::DIScope *scope,
253                                 const RecordDecl *RD = nullptr);
254
255   llvm::DIType *createFieldType(StringRef name, QualType type,
256                                 SourceLocation loc, AccessSpecifier AS,
257                                 uint64_t offsetInBits, llvm::DIFile *tunit,
258                                 llvm::DIScope *scope,
259                                 const RecordDecl *RD = nullptr) {
260     return createFieldType(name, type, loc, AS, offsetInBits, 0, tunit, scope,
261                            RD);
262   }
263
264   /// Create new bit field member.
265   llvm::DIType *createBitFieldType(const FieldDecl *BitFieldDecl,
266                                    llvm::DIScope *RecordTy,
267                                    const RecordDecl *RD);
268
269   /// Helpers for collecting fields of a record.
270   /// @{
271   void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
272                                  SmallVectorImpl<llvm::Metadata *> &E,
273                                  llvm::DIType *RecordTy);
274   llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var,
275                                                llvm::DIType *RecordTy,
276                                                const RecordDecl *RD);
277   void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
278                                 llvm::DIFile *F,
279                                 SmallVectorImpl<llvm::Metadata *> &E,
280                                 llvm::DIType *RecordTy, const RecordDecl *RD);
281   void CollectRecordNestedType(const TypeDecl *RD,
282                                SmallVectorImpl<llvm::Metadata *> &E);
283   void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F,
284                            SmallVectorImpl<llvm::Metadata *> &E,
285                            llvm::DICompositeType *RecordTy);
286
287   /// If the C++ class has vtable info then insert appropriate debug
288   /// info entry in EltTys vector.
289   void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F,
290                          SmallVectorImpl<llvm::Metadata *> &EltTys,
291                          llvm::DICompositeType *RecordTy);
292   /// @}
293
294   /// Create a new lexical block node and push it on the stack.
295   void CreateLexicalBlock(SourceLocation Loc);
296
297   /// If target-specific LLVM \p AddressSpace directly maps to target-specific
298   /// DWARF address space, appends extended dereferencing mechanism to complex
299   /// expression \p Expr. Otherwise, does nothing.
300   ///
301   /// Extended dereferencing mechanism is has the following format:
302   ///     DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
303   void AppendAddressSpaceXDeref(unsigned AddressSpace,
304                                 SmallVectorImpl<int64_t> &Expr) const;
305
306 public:
307   CGDebugInfo(CodeGenModule &CGM);
308   ~CGDebugInfo();
309
310   void finalize();
311
312   /// Module debugging: Support for building PCMs.
313   /// @{
314   /// Set the main CU's DwoId field to \p Signature.
315   void setDwoId(uint64_t Signature);
316
317   /// When generating debug information for a clang module or
318   /// precompiled header, this module map will be used to determine
319   /// the module of origin of each Decl.
320   void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; }
321
322   /// When generating debug information for a clang module or
323   /// precompiled header, this module map will be used to determine
324   /// the module of origin of each Decl.
325   void setPCHDescriptor(ExternalASTSource::ASTSourceDescriptor PCH) {
326     PCHDescriptor = PCH;
327   }
328   /// @}
329
330   /// Update the current source location. If \arg loc is invalid it is
331   /// ignored.
332   void setLocation(SourceLocation Loc);
333
334   /// Return the current source location. This does not necessarily correspond
335   /// to the IRBuilder's current DebugLoc.
336   SourceLocation getLocation() const { return CurLoc; }
337
338   /// Update the current inline scope. All subsequent calls to \p EmitLocation
339   /// will create a location with this inlinedAt field.
340   void setInlinedAt(llvm::MDNode *InlinedAt) { CurInlinedAt = InlinedAt; }
341
342   /// \return the current inline scope.
343   llvm::MDNode *getInlinedAt() const { return CurInlinedAt; }
344
345   // Converts a SourceLocation to a DebugLoc
346   llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Loc);
347
348   /// Emit metadata to indicate a change in line/column information in
349   /// the source file. If the location is invalid, the previous
350   /// location will be reused.
351   void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
352
353   /// Emit a call to llvm.dbg.function.start to indicate
354   /// start of a new function.
355   /// \param Loc       The location of the function header.
356   /// \param ScopeLoc  The location of the function body.
357   void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
358                          SourceLocation ScopeLoc, QualType FnType,
359                          llvm::Function *Fn, CGBuilderTy &Builder);
360
361   /// Start a new scope for an inlined function.
362   void EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD);
363   /// End an inlined function scope.
364   void EmitInlineFunctionEnd(CGBuilderTy &Builder);
365
366   /// Emit debug info for a function declaration.
367   void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType);
368
369   /// Constructs the debug code for exiting a function.
370   void EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn);
371
372   /// Emit metadata to indicate the beginning of a new lexical block
373   /// and push the block onto the stack.
374   void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
375
376   /// Emit metadata to indicate the end of a new lexical block and pop
377   /// the current block.
378   void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
379
380   /// Emit call to \c llvm.dbg.declare for an automatic variable
381   /// declaration.
382   void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
383                                  CGBuilderTy &Builder);
384
385   /// Emit call to \c llvm.dbg.declare for an imported variable
386   /// declaration in a block.
387   void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
388                                          llvm::Value *storage,
389                                          CGBuilderTy &Builder,
390                                          const CGBlockInfo &blockInfo,
391                                          llvm::Instruction *InsertPoint = nullptr);
392
393   /// Emit call to \c llvm.dbg.declare for an argument variable
394   /// declaration.
395   void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
396                                 unsigned ArgNo, CGBuilderTy &Builder);
397
398   /// Emit call to \c llvm.dbg.declare for the block-literal argument
399   /// to a block invocation function.
400   void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
401                                             StringRef Name, unsigned ArgNo,
402                                             llvm::AllocaInst *LocalAddr,
403                                             CGBuilderTy &Builder);
404
405   /// Emit information about a global variable.
406   void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
407
408   /// Emit a constant global variable's debug info.
409   void EmitGlobalVariable(const ValueDecl *VD, const APValue &Init);
410
411   /// Emit C++ using directive.
412   void EmitUsingDirective(const UsingDirectiveDecl &UD);
413
414   /// Emit the type explicitly casted to.
415   void EmitExplicitCastType(QualType Ty);
416
417   /// Emit C++ using declaration.
418   void EmitUsingDecl(const UsingDecl &UD);
419
420   /// Emit an @import declaration.
421   void EmitImportDecl(const ImportDecl &ID);
422
423   /// Emit C++ namespace alias.
424   llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA);
425
426   /// Emit record type's standalone debug info.
427   llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L);
428
429   /// Emit an Objective-C interface type standalone debug info.
430   llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
431
432   /// Emit standalone debug info for a type.
433   llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);
434
435   void completeType(const EnumDecl *ED);
436   void completeType(const RecordDecl *RD);
437   void completeRequiredType(const RecordDecl *RD);
438   void completeClassData(const RecordDecl *RD);
439   void completeClass(const RecordDecl *RD);
440
441   void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD);
442   void completeUnusedClass(const CXXRecordDecl &D);
443
444   /// Create debug info for a macro defined by a #define directive or a macro
445   /// undefined by a #undef directive.
446   llvm::DIMacro *CreateMacro(llvm::DIMacroFile *Parent, unsigned MType,
447                              SourceLocation LineLoc, StringRef Name,
448                              StringRef Value);
449
450   /// Create debug info for a file referenced by an #include directive.
451   llvm::DIMacroFile *CreateTempMacroFile(llvm::DIMacroFile *Parent,
452                                          SourceLocation LineLoc,
453                                          SourceLocation FileLoc);
454 private:
455   /// Emit call to llvm.dbg.declare for a variable declaration.
456   void EmitDeclare(const VarDecl *decl, llvm::Value *AI,
457                    llvm::Optional<unsigned> ArgNo, CGBuilderTy &Builder);
458
459   /// Build up structure info for the byref.  See \a BuildByRefType.
460   llvm::DIType *EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
461                                              uint64_t *OffSet);
462
463   /// Get context info for the DeclContext of \p Decl.
464   llvm::DIScope *getDeclContextDescriptor(const Decl *D);
465   /// Get context info for a given DeclContext \p Decl.
466   llvm::DIScope *getContextDescriptor(const Decl *Context,
467                                       llvm::DIScope *Default);
468
469   llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl);
470
471   /// Create a forward decl for a RecordType in a given context.
472   llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *,
473                                                   llvm::DIScope *);
474
475   /// Return current directory name.
476   StringRef getCurrentDirname();
477
478   /// Create new compile unit.
479   void CreateCompileUnit();
480
481   /// Remap a given path with the current debug prefix map
482   std::string remapDIPath(StringRef) const;
483
484   /// Compute the file checksum debug info for input file ID.
485   llvm::DIFile::ChecksumKind computeChecksum(FileID FID,
486                                              SmallString<32> &Checksum) const;
487
488   /// Get the file debug info descriptor for the input location.
489   llvm::DIFile *getOrCreateFile(SourceLocation Loc);
490
491   /// Get the file info for main compile unit.
492   llvm::DIFile *getOrCreateMainFile();
493
494   /// Get the type from the cache or create a new type if necessary.
495   llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg);
496
497   /// Get a reference to a clang module.  If \p CreateSkeletonCU is true,
498   /// this also creates a split dwarf skeleton compile unit.
499   llvm::DIModule *
500   getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
501                        bool CreateSkeletonCU);
502
503   /// DebugTypeExtRefs: If \p D originated in a clang module, return it.
504   llvm::DIModule *getParentModuleOrNull(const Decl *D);
505
506   /// Get the type from the cache or create a new partial type if
507   /// necessary.
508   llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty,
509                                                 llvm::DIFile *F);
510
511   /// Create type metadata for a source language type.
512   llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg);
513
514   /// Create new member and increase Offset by FType's size.
515   llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType,
516                                  StringRef Name, uint64_t *Offset);
517
518   /// Retrieve the DIDescriptor, if any, for the canonical form of this
519   /// declaration.
520   llvm::DINode *getDeclarationOrDefinition(const Decl *D);
521
522   /// \return debug info descriptor to describe method
523   /// declaration for the given method definition.
524   llvm::DISubprogram *getFunctionDeclaration(const Decl *D);
525
526   /// \return debug info descriptor to describe in-class static data
527   /// member declaration for the given out-of-class definition.  If D
528   /// is an out-of-class definition of a static data member of a
529   /// class, find its corresponding in-class declaration.
530   llvm::DIDerivedType *
531   getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D);
532
533   /// Helper that either creates a forward declaration or a stub.
534   llvm::DISubprogram *getFunctionFwdDeclOrStub(GlobalDecl GD, bool Stub);
535
536   /// Create a subprogram describing the forward declaration
537   /// represented in the given FunctionDecl wrapped in a GlobalDecl.
538   llvm::DISubprogram *getFunctionForwardDeclaration(GlobalDecl GD);
539
540   /// Create a DISubprogram describing the function
541   /// represented in the given FunctionDecl wrapped in a GlobalDecl.
542   llvm::DISubprogram *getFunctionStub(GlobalDecl GD);
543
544   /// Create a global variable describing the forward declaration
545   /// represented in the given VarDecl.
546   llvm::DIGlobalVariable *
547   getGlobalVariableForwardDeclaration(const VarDecl *VD);
548
549   /// Return a global variable that represents one of the collection of global
550   /// variables created for an anonmyous union.
551   ///
552   /// Recursively collect all of the member fields of a global
553   /// anonymous decl and create static variables for them. The first
554   /// time this is called it needs to be on a union and then from
555   /// there we can have additional unnamed fields.
556   llvm::DIGlobalVariableExpression *
557   CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit,
558                          unsigned LineNo, StringRef LinkageName,
559                          llvm::GlobalVariable *Var, llvm::DIScope *DContext);
560
561   /// Get the printing policy for producing names for debug info.
562   PrintingPolicy getPrintingPolicy() const;
563
564   /// Get function name for the given FunctionDecl. If the name is
565   /// constructed on demand (e.g., C++ destructor) then the name is
566   /// stored on the side.
567   StringRef getFunctionName(const FunctionDecl *FD);
568
569   /// Returns the unmangled name of an Objective-C method.
570   /// This is the display name for the debugging info.
571   StringRef getObjCMethodName(const ObjCMethodDecl *FD);
572
573   /// Return selector name. This is used for debugging
574   /// info.
575   StringRef getSelectorName(Selector S);
576
577   /// Get class name including template argument list.
578   StringRef getClassName(const RecordDecl *RD);
579
580   /// Get the vtable name for the given class.
581   StringRef getVTableName(const CXXRecordDecl *Decl);
582
583   /// Get line number for the location. If location is invalid
584   /// then use current location.
585   unsigned getLineNumber(SourceLocation Loc);
586
587   /// Get column number for the location. If location is
588   /// invalid then use current location.
589   /// \param Force  Assume DebugColumnInfo option is true.
590   unsigned getColumnNumber(SourceLocation Loc, bool Force = false);
591
592   /// Collect various properties of a FunctionDecl.
593   /// \param GD  A GlobalDecl whose getDecl() must return a FunctionDecl.
594   void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
595                                 StringRef &Name, StringRef &LinkageName,
596                                 llvm::DIScope *&FDContext,
597                                 llvm::DINodeArray &TParamsArray,
598                                 llvm::DINode::DIFlags &Flags);
599
600   /// Collect various properties of a VarDecl.
601   void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
602                            unsigned &LineNo, QualType &T, StringRef &Name,
603                            StringRef &LinkageName, llvm::DIScope *&VDContext);
604
605   /// Allocate a copy of \p A using the DebugInfoNames allocator
606   /// and return a reference to it. If multiple arguments are given the strings
607   /// are concatenated.
608   StringRef internString(StringRef A, StringRef B = StringRef()) {
609     char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size());
610     if (!A.empty())
611       std::memcpy(Data, A.data(), A.size());
612     if (!B.empty())
613       std::memcpy(Data + A.size(), B.data(), B.size());
614     return StringRef(Data, A.size() + B.size());
615   }
616 };
617
618 /// A scoped helper to set the current debug location to the specified
619 /// location or preferred location of the specified Expr.
620 class ApplyDebugLocation {
621 private:
622   void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false);
623   ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty,
624                      SourceLocation TemporaryLocation);
625
626   llvm::DebugLoc OriginalLocation;
627   CodeGenFunction *CGF;
628
629 public:
630   /// Set the location to the (valid) TemporaryLocation.
631   ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation);
632   ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E);
633   ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc);
634   ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) {
635     Other.CGF = nullptr;
636   }
637
638   ~ApplyDebugLocation();
639
640   /// \brief Apply TemporaryLocation if it is valid. Otherwise switch
641   /// to an artificial debug location that has a valid scope, but no
642   /// line information.
643   ///
644   /// Artificial locations are useful when emitting compiler-generated
645   /// helper functions that have no source location associated with
646   /// them. The DWARF specification allows the compiler to use the
647   /// special line number 0 to indicate code that can not be
648   /// attributed to any source location. Note that passing an empty
649   /// SourceLocation to CGDebugInfo::setLocation() will result in the
650   /// last valid location being reused.
651   static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) {
652     return ApplyDebugLocation(CGF, false, SourceLocation());
653   }
654   /// \brief Apply TemporaryLocation if it is valid. Otherwise switch
655   /// to an artificial debug location that has a valid scope, but no
656   /// line information.
657   static ApplyDebugLocation
658   CreateDefaultArtificial(CodeGenFunction &CGF,
659                           SourceLocation TemporaryLocation) {
660     return ApplyDebugLocation(CGF, false, TemporaryLocation);
661   }
662
663   /// Set the IRBuilder to not attach debug locations.  Note that
664   /// passing an empty SourceLocation to \a CGDebugInfo::setLocation()
665   /// will result in the last valid location being reused.  Note that
666   /// all instructions that do not have a location at the beginning of
667   /// a function are counted towards to function prologue.
668   static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) {
669     return ApplyDebugLocation(CGF, true, SourceLocation());
670   }
671
672 };
673
674 /// A scoped helper to set the current debug location to an inlined location.
675 class ApplyInlineDebugLocation {
676   SourceLocation SavedLocation;
677   CodeGenFunction *CGF;
678
679 public:
680   /// Set up the CodeGenFunction's DebugInfo to produce inline locations for the
681   /// function \p InlinedFn. The current debug location becomes the inlined call
682   /// site of the inlined function.
683   ApplyInlineDebugLocation(CodeGenFunction &CGF, GlobalDecl InlinedFn);
684   /// Restore everything back to the orginial state.
685   ~ApplyInlineDebugLocation();
686 };
687
688 } // namespace CodeGen
689 } // namespace clang
690
691 #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H