]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGDebugInfo.h
Merge clang trunk r300422 and resolve conflicts.
[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   /// \return namespace descriptor for the given namespace decl.
198   llvm::DINamespace *getOrCreateNameSpace(const NamespaceDecl *N);
199   llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty,
200                                       QualType PointeeTy, llvm::DIFile *F);
201   llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache);
202
203   /// A helper function to create a subprogram for a single member
204   /// function GlobalDecl.
205   llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method,
206                                               llvm::DIFile *F,
207                                               llvm::DIType *RecordTy);
208
209   /// A helper function to collect debug info for C++ member
210   /// functions. This is used while creating debug info entry for a
211   /// Record.
212   void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F,
213                                  SmallVectorImpl<llvm::Metadata *> &E,
214                                  llvm::DIType *T);
215
216   /// A helper function to collect debug info for C++ base
217   /// classes. This is used while creating debug info entry for a
218   /// Record.
219   void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F,
220                        SmallVectorImpl<llvm::Metadata *> &EltTys,
221                        llvm::DIType *RecordTy);
222
223   /// Helper function for CollectCXXBases.
224   /// Adds debug info entries for types in Bases that are not in SeenTypes.
225   void CollectCXXBasesAux(const CXXRecordDecl *RD, llvm::DIFile *Unit,
226                           SmallVectorImpl<llvm::Metadata *> &EltTys,
227                           llvm::DIType *RecordTy,
228                           const CXXRecordDecl::base_class_const_range &Bases,
229                           llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
230                           llvm::DINode::DIFlags StartingFlags);
231
232   /// A helper function to collect template parameters.
233   llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList,
234                                           ArrayRef<TemplateArgument> TAList,
235                                           llvm::DIFile *Unit);
236   /// A helper function to collect debug info for function template
237   /// parameters.
238   llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD,
239                                                   llvm::DIFile *Unit);
240
241   /// A helper function to collect debug info for template
242   /// parameters.
243   llvm::DINodeArray
244   CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
245                            llvm::DIFile *F);
246
247   llvm::DIType *createFieldType(StringRef name, QualType type,
248                                 SourceLocation loc, AccessSpecifier AS,
249                                 uint64_t offsetInBits,
250                                 uint32_t AlignInBits,
251                                 llvm::DIFile *tunit, llvm::DIScope *scope,
252                                 const RecordDecl *RD = nullptr);
253
254   llvm::DIType *createFieldType(StringRef name, QualType type,
255                                 SourceLocation loc, AccessSpecifier AS,
256                                 uint64_t offsetInBits, llvm::DIFile *tunit,
257                                 llvm::DIScope *scope,
258                                 const RecordDecl *RD = nullptr) {
259     return createFieldType(name, type, loc, AS, offsetInBits, 0, tunit, scope,
260                            RD);
261   }
262
263   /// Create new bit field member.
264   llvm::DIType *createBitFieldType(const FieldDecl *BitFieldDecl,
265                                    llvm::DIScope *RecordTy,
266                                    const RecordDecl *RD);
267
268   /// Helpers for collecting fields of a record.
269   /// @{
270   void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
271                                  SmallVectorImpl<llvm::Metadata *> &E,
272                                  llvm::DIType *RecordTy);
273   llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var,
274                                                llvm::DIType *RecordTy,
275                                                const RecordDecl *RD);
276   void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
277                                 llvm::DIFile *F,
278                                 SmallVectorImpl<llvm::Metadata *> &E,
279                                 llvm::DIType *RecordTy, const RecordDecl *RD);
280   void CollectRecordNestedRecord(const RecordDecl *RD,
281                                  SmallVectorImpl<llvm::Metadata *> &E);
282   void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F,
283                            SmallVectorImpl<llvm::Metadata *> &E,
284                            llvm::DICompositeType *RecordTy);
285
286   /// If the C++ class has vtable info then insert appropriate debug
287   /// info entry in EltTys vector.
288   void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F,
289                          SmallVectorImpl<llvm::Metadata *> &EltTys,
290                          llvm::DICompositeType *RecordTy);
291   /// @}
292
293   /// Create a new lexical block node and push it on the stack.
294   void CreateLexicalBlock(SourceLocation Loc);
295
296   /// If target-specific LLVM \p AddressSpace directly maps to target-specific
297   /// DWARF address space, appends extended dereferencing mechanism to complex
298   /// expression \p Expr. Otherwise, does nothing.
299   ///
300   /// Extended dereferencing mechanism is has the following format:
301   ///     DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
302   void AppendAddressSpaceXDeref(unsigned AddressSpace,
303                                 SmallVectorImpl<int64_t> &Expr) const;
304
305 public:
306   CGDebugInfo(CodeGenModule &CGM);
307   ~CGDebugInfo();
308
309   void finalize();
310
311   /// Module debugging: Support for building PCMs.
312   /// @{
313   /// Set the main CU's DwoId field to \p Signature.
314   void setDwoId(uint64_t Signature);
315
316   /// When generating debug information for a clang module or
317   /// precompiled header, this module map will be used to determine
318   /// the module of origin of each Decl.
319   void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; }
320
321   /// When generating debug information for a clang module or
322   /// precompiled header, this module map will be used to determine
323   /// the module of origin of each Decl.
324   void setPCHDescriptor(ExternalASTSource::ASTSourceDescriptor PCH) {
325     PCHDescriptor = PCH;
326   }
327   /// @}
328
329   /// Update the current source location. If \arg loc is invalid it is
330   /// ignored.
331   void setLocation(SourceLocation Loc);
332
333   /// Return the current source location. This does not necessarily correspond
334   /// to the IRBuilder's current DebugLoc.
335   SourceLocation getLocation() const { return CurLoc; }
336
337   /// Update the current inline scope. All subsequent calls to \p EmitLocation
338   /// will create a location with this inlinedAt field.
339   void setInlinedAt(llvm::MDNode *InlinedAt) { CurInlinedAt = InlinedAt; }
340
341   /// \return the current inline scope.
342   llvm::MDNode *getInlinedAt() const { return CurInlinedAt; }
343
344   // Converts a SourceLocation to a DebugLoc
345   llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Loc);
346
347   /// Emit metadata to indicate a change in line/column information in
348   /// the source file. If the location is invalid, the previous
349   /// location will be reused.
350   void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
351
352   /// Emit a call to llvm.dbg.function.start to indicate
353   /// start of a new function.
354   /// \param Loc       The location of the function header.
355   /// \param ScopeLoc  The location of the function body.
356   void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
357                          SourceLocation ScopeLoc, QualType FnType,
358                          llvm::Function *Fn, CGBuilderTy &Builder);
359
360   /// Start a new scope for an inlined function.
361   void EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD);
362   /// End an inlined function scope.
363   void EmitInlineFunctionEnd(CGBuilderTy &Builder);
364
365   /// Emit debug info for a function declaration.
366   void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType);
367
368   /// Constructs the debug code for exiting a function.
369   void EmitFunctionEnd(CGBuilderTy &Builder);
370
371   /// Emit metadata to indicate the beginning of a new lexical block
372   /// and push the block onto the stack.
373   void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
374
375   /// Emit metadata to indicate the end of a new lexical block and pop
376   /// the current block.
377   void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
378
379   /// Emit call to \c llvm.dbg.declare for an automatic variable
380   /// declaration.
381   void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
382                                  CGBuilderTy &Builder);
383
384   /// Emit call to \c llvm.dbg.declare for an imported variable
385   /// declaration in a block.
386   void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
387                                          llvm::Value *storage,
388                                          CGBuilderTy &Builder,
389                                          const CGBlockInfo &blockInfo,
390                                          llvm::Instruction *InsertPoint = nullptr);
391
392   /// Emit call to \c llvm.dbg.declare for an argument variable
393   /// declaration.
394   void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
395                                 unsigned ArgNo, CGBuilderTy &Builder);
396
397   /// Emit call to \c llvm.dbg.declare for the block-literal argument
398   /// to a block invocation function.
399   void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
400                                             llvm::Value *Arg, unsigned ArgNo,
401                                             llvm::Value *LocalAddr,
402                                             CGBuilderTy &Builder);
403
404   /// Emit information about a global variable.
405   void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
406
407   /// Emit a constant global variable's debug info.
408   void EmitGlobalVariable(const ValueDecl *VD, const APValue &Init);
409
410   /// Emit C++ using directive.
411   void EmitUsingDirective(const UsingDirectiveDecl &UD);
412
413   /// Emit the type explicitly casted to.
414   void EmitExplicitCastType(QualType Ty);
415
416   /// Emit C++ using declaration.
417   void EmitUsingDecl(const UsingDecl &UD);
418
419   /// Emit an @import declaration.
420   void EmitImportDecl(const ImportDecl &ID);
421
422   /// Emit C++ namespace alias.
423   llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA);
424
425   /// Emit record type's standalone debug info.
426   llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L);
427
428   /// Emit an Objective-C interface type standalone debug info.
429   llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
430
431   /// Emit standalone debug info for a type.
432   llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);
433
434   void completeType(const EnumDecl *ED);
435   void completeType(const RecordDecl *RD);
436   void completeRequiredType(const RecordDecl *RD);
437   void completeClassData(const RecordDecl *RD);
438   void completeClass(const RecordDecl *RD);
439
440   void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD);
441   void completeUnusedClass(const CXXRecordDecl &D);
442
443   /// Create debug info for a macro defined by a #define directive or a macro
444   /// undefined by a #undef directive.
445   llvm::DIMacro *CreateMacro(llvm::DIMacroFile *Parent, unsigned MType,
446                              SourceLocation LineLoc, StringRef Name,
447                              StringRef Value);
448
449   /// Create debug info for a file referenced by an #include directive.
450   llvm::DIMacroFile *CreateTempMacroFile(llvm::DIMacroFile *Parent,
451                                          SourceLocation LineLoc,
452                                          SourceLocation FileLoc);
453 private:
454   /// Emit call to llvm.dbg.declare for a variable declaration.
455   void EmitDeclare(const VarDecl *decl, llvm::Value *AI,
456                    llvm::Optional<unsigned> ArgNo, CGBuilderTy &Builder);
457
458   /// Build up structure info for the byref.  See \a BuildByRefType.
459   llvm::DIType *EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
460                                              uint64_t *OffSet);
461
462   /// Get context info for the DeclContext of \p Decl.
463   llvm::DIScope *getDeclContextDescriptor(const Decl *D);
464   /// Get context info for a given DeclContext \p Decl.
465   llvm::DIScope *getContextDescriptor(const Decl *Context,
466                                       llvm::DIScope *Default);
467
468   llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl);
469
470   /// Create a forward decl for a RecordType in a given context.
471   llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *,
472                                                   llvm::DIScope *);
473
474   /// Return current directory name.
475   StringRef getCurrentDirname();
476
477   /// Create new compile unit.
478   void CreateCompileUnit();
479
480   /// Remap a given path with the current debug prefix map
481   std::string remapDIPath(StringRef) const;
482
483   /// Compute the file checksum debug info for input file ID.
484   llvm::DIFile::ChecksumKind computeChecksum(FileID FID,
485                                              SmallString<32> &Checksum) const;
486
487   /// Get the file debug info descriptor for the input location.
488   llvm::DIFile *getOrCreateFile(SourceLocation Loc);
489
490   /// Get the file info for main compile unit.
491   llvm::DIFile *getOrCreateMainFile();
492
493   /// Get the type from the cache or create a new type if necessary.
494   llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg);
495
496   /// Get a reference to a clang module.  If \p CreateSkeletonCU is true,
497   /// this also creates a split dwarf skeleton compile unit.
498   llvm::DIModule *
499   getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
500                        bool CreateSkeletonCU);
501
502   /// DebugTypeExtRefs: If \p D originated in a clang module, return it.
503   llvm::DIModule *getParentModuleOrNull(const Decl *D);
504
505   /// Get the type from the cache or create a new partial type if
506   /// necessary.
507   llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty,
508                                                 llvm::DIFile *F);
509
510   /// Create type metadata for a source language type.
511   llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg);
512
513   /// Create new member and increase Offset by FType's size.
514   llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType,
515                                  StringRef Name, uint64_t *Offset);
516
517   /// Retrieve the DIDescriptor, if any, for the canonical form of this
518   /// declaration.
519   llvm::DINode *getDeclarationOrDefinition(const Decl *D);
520
521   /// \return debug info descriptor to describe method
522   /// declaration for the given method definition.
523   llvm::DISubprogram *getFunctionDeclaration(const Decl *D);
524
525   /// \return debug info descriptor to describe in-class static data
526   /// member declaration for the given out-of-class definition.  If D
527   /// is an out-of-class definition of a static data member of a
528   /// class, find its corresponding in-class declaration.
529   llvm::DIDerivedType *
530   getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D);
531
532   /// Helper that either creates a forward declaration or a stub.
533   llvm::DISubprogram *getFunctionFwdDeclOrStub(GlobalDecl GD, bool Stub);
534
535   /// Create a subprogram describing the forward declaration
536   /// represented in the given FunctionDecl wrapped in a GlobalDecl.
537   llvm::DISubprogram *getFunctionForwardDeclaration(GlobalDecl GD);
538
539   /// Create a DISubprogram describing the function
540   /// represented in the given FunctionDecl wrapped in a GlobalDecl.
541   llvm::DISubprogram *getFunctionStub(GlobalDecl GD);
542
543   /// Create a global variable describing the forward declaration
544   /// represented in the given VarDecl.
545   llvm::DIGlobalVariable *
546   getGlobalVariableForwardDeclaration(const VarDecl *VD);
547
548   /// Return a global variable that represents one of the collection of global
549   /// variables created for an anonmyous union.
550   ///
551   /// Recursively collect all of the member fields of a global
552   /// anonymous decl and create static variables for them. The first
553   /// time this is called it needs to be on a union and then from
554   /// there we can have additional unnamed fields.
555   llvm::DIGlobalVariableExpression *
556   CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit,
557                          unsigned LineNo, StringRef LinkageName,
558                          llvm::GlobalVariable *Var, llvm::DIScope *DContext);
559
560   /// Get function name for the given FunctionDecl. If the name is
561   /// constructed on demand (e.g., C++ destructor) then the name is
562   /// stored on the side.
563   StringRef getFunctionName(const FunctionDecl *FD);
564
565   /// Returns the unmangled name of an Objective-C method.
566   /// This is the display name for the debugging info.
567   StringRef getObjCMethodName(const ObjCMethodDecl *FD);
568
569   /// Return selector name. This is used for debugging
570   /// info.
571   StringRef getSelectorName(Selector S);
572
573   /// Get class name including template argument list.
574   StringRef getClassName(const RecordDecl *RD);
575
576   /// Get the vtable name for the given class.
577   StringRef getVTableName(const CXXRecordDecl *Decl);
578
579   /// Get line number for the location. If location is invalid
580   /// then use current location.
581   unsigned getLineNumber(SourceLocation Loc);
582
583   /// Get column number for the location. If location is
584   /// invalid then use current location.
585   /// \param Force  Assume DebugColumnInfo option is true.
586   unsigned getColumnNumber(SourceLocation Loc, bool Force = false);
587
588   /// Collect various properties of a FunctionDecl.
589   /// \param GD  A GlobalDecl whose getDecl() must return a FunctionDecl.
590   void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
591                                 StringRef &Name, StringRef &LinkageName,
592                                 llvm::DIScope *&FDContext,
593                                 llvm::DINodeArray &TParamsArray,
594                                 llvm::DINode::DIFlags &Flags);
595
596   /// Collect various properties of a VarDecl.
597   void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
598                            unsigned &LineNo, QualType &T, StringRef &Name,
599                            StringRef &LinkageName, llvm::DIScope *&VDContext);
600
601   /// Allocate a copy of \p A using the DebugInfoNames allocator
602   /// and return a reference to it. If multiple arguments are given the strings
603   /// are concatenated.
604   StringRef internString(StringRef A, StringRef B = StringRef()) {
605     char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size());
606     if (!A.empty())
607       std::memcpy(Data, A.data(), A.size());
608     if (!B.empty())
609       std::memcpy(Data + A.size(), B.data(), B.size());
610     return StringRef(Data, A.size() + B.size());
611   }
612 };
613
614 /// A scoped helper to set the current debug location to the specified
615 /// location or preferred location of the specified Expr.
616 class ApplyDebugLocation {
617 private:
618   void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false);
619   ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty,
620                      SourceLocation TemporaryLocation);
621
622   llvm::DebugLoc OriginalLocation;
623   CodeGenFunction *CGF;
624
625 public:
626   /// Set the location to the (valid) TemporaryLocation.
627   ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation);
628   ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E);
629   ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc);
630   ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) {
631     Other.CGF = nullptr;
632   }
633
634   ~ApplyDebugLocation();
635
636   /// \brief Apply TemporaryLocation if it is valid. Otherwise switch
637   /// to an artificial debug location that has a valid scope, but no
638   /// line information.
639   ///
640   /// Artificial locations are useful when emitting compiler-generated
641   /// helper functions that have no source location associated with
642   /// them. The DWARF specification allows the compiler to use the
643   /// special line number 0 to indicate code that can not be
644   /// attributed to any source location. Note that passing an empty
645   /// SourceLocation to CGDebugInfo::setLocation() will result in the
646   /// last valid location being reused.
647   static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) {
648     return ApplyDebugLocation(CGF, false, SourceLocation());
649   }
650   /// \brief Apply TemporaryLocation if it is valid. Otherwise switch
651   /// to an artificial debug location that has a valid scope, but no
652   /// line information.
653   static ApplyDebugLocation
654   CreateDefaultArtificial(CodeGenFunction &CGF,
655                           SourceLocation TemporaryLocation) {
656     return ApplyDebugLocation(CGF, false, TemporaryLocation);
657   }
658
659   /// Set the IRBuilder to not attach debug locations.  Note that
660   /// passing an empty SourceLocation to \a CGDebugInfo::setLocation()
661   /// will result in the last valid location being reused.  Note that
662   /// all instructions that do not have a location at the beginning of
663   /// a function are counted towards to function prologue.
664   static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) {
665     return ApplyDebugLocation(CGF, true, SourceLocation());
666   }
667
668 };
669
670 /// A scoped helper to set the current debug location to an inlined location.
671 class ApplyInlineDebugLocation {
672   SourceLocation SavedLocation;
673   CodeGenFunction *CGF;
674
675 public:
676   /// Set up the CodeGenFunction's DebugInfo to produce inline locations for the
677   /// function \p InlinedFn. The current debug location becomes the inlined call
678   /// site of the inlined function.
679   ApplyInlineDebugLocation(CodeGenFunction &CGF, GlobalDecl InlinedFn);
680   /// Restore everything back to the orginial state.
681   ~ApplyInlineDebugLocation();
682 };
683
684 } // namespace CodeGen
685 } // namespace clang
686
687 #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H