Home | History | Annotate | Download | only in CodeGen
      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 CLANG_CODEGEN_CGDEBUGINFO_H
     15 #define CLANG_CODEGEN_CGDEBUGINFO_H
     16 
     17 #include "clang/AST/Type.h"
     18 #include "clang/AST/Expr.h"
     19 #include "clang/Basic/SourceLocation.h"
     20 #include "llvm/ADT/DenseMap.h"
     21 #include "llvm/Analysis/DebugInfo.h"
     22 #include "llvm/Analysis/DIBuilder.h"
     23 #include "llvm/Support/ValueHandle.h"
     24 #include "llvm/Support/Allocator.h"
     25 
     26 #include "CGBuilder.h"
     27 
     28 namespace llvm {
     29   class MDNode;
     30 }
     31 
     32 namespace clang {
     33   class VarDecl;
     34   class ObjCInterfaceDecl;
     35   class ClassTemplateSpecializationDecl;
     36   class GlobalDecl;
     37 
     38 namespace CodeGen {
     39   class CodeGenModule;
     40   class CodeGenFunction;
     41   class CGBlockInfo;
     42 
     43 /// CGDebugInfo - This class gathers all debug information during compilation
     44 /// and is responsible for emitting to llvm globals or pass directly to
     45 /// the backend.
     46 class CGDebugInfo {
     47   CodeGenModule &CGM;
     48   llvm::DIBuilder DBuilder;
     49   llvm::DICompileUnit TheCU;
     50   SourceLocation CurLoc, PrevLoc;
     51   llvm::DIType VTablePtrType;
     52 
     53   /// TypeCache - Cache of previously constructed Types.
     54   llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
     55 
     56   bool BlockLiteralGenericSet;
     57   llvm::DIType BlockLiteralGeneric;
     58 
     59   // LexicalBlockStack - Keep track of our current nested lexical block.
     60   std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
     61   llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
     62   // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
     63   // beginning of a function. This is used to pop unbalanced regions at
     64   // the end of a function.
     65   std::vector<unsigned> FnBeginRegionCount;
     66 
     67   /// DebugInfoNames - This is a storage for names that are
     68   /// constructed on demand. For example, C++ destructors, C++ operators etc..
     69   llvm::BumpPtrAllocator DebugInfoNames;
     70   StringRef CWDName;
     71 
     72   llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
     73   llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
     74   llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
     75 
     76   /// Helper functions for getOrCreateType.
     77   llvm::DIType CreateType(const BuiltinType *Ty);
     78   llvm::DIType CreateType(const ComplexType *Ty);
     79   llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
     80   llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
     81   llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
     82                           llvm::DIFile F);
     83   llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
     84   llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
     85   llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
     86   llvm::DIType CreateType(const TagType *Ty);
     87   llvm::DIType CreateType(const RecordType *Ty);
     88   llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
     89   llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
     90   llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
     91   llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
     92   llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
     93   llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
     94   llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
     95   llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
     96   llvm::DIType CreateEnumType(const EnumDecl *ED);
     97   llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
     98                                      llvm::DIFile F);
     99   llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
    100                                        llvm::DIFile F);
    101   llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
    102   llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
    103   llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
    104   llvm::DIType CreatePointerLikeType(unsigned Tag,
    105                                      const Type *Ty, QualType PointeeTy,
    106                                      llvm::DIFile F);
    107 
    108   llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
    109                                              llvm::DIFile F,
    110                                              llvm::DIType RecordTy);
    111 
    112   void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
    113                                  llvm::DIFile F,
    114                                  SmallVectorImpl<llvm::Value *> &E,
    115                                  llvm::DIType T);
    116 
    117   void CollectCXXFriends(const CXXRecordDecl *Decl,
    118                        llvm::DIFile F,
    119                        SmallVectorImpl<llvm::Value *> &EltTys,
    120                        llvm::DIType RecordTy);
    121 
    122   void CollectCXXBases(const CXXRecordDecl *Decl,
    123                        llvm::DIFile F,
    124                        SmallVectorImpl<llvm::Value *> &EltTys,
    125                        llvm::DIType RecordTy);
    126 
    127   llvm::DIArray
    128   CollectTemplateParams(const TemplateParameterList *TPList,
    129                         const TemplateArgumentList &TAList,
    130                         llvm::DIFile Unit);
    131   llvm::DIArray
    132   CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
    133   llvm::DIArray
    134   CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
    135                            llvm::DIFile F);
    136 
    137   llvm::DIType createFieldType(StringRef name, QualType type,
    138                                uint64_t sizeInBitsOverride, SourceLocation loc,
    139                                AccessSpecifier AS, uint64_t offsetInBits,
    140                                llvm::DIFile tunit,
    141                                llvm::DIDescriptor scope);
    142   void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
    143                            SmallVectorImpl<llvm::Value *> &E,
    144                            llvm::DIType RecordTy);
    145 
    146   void CollectVTableInfo(const CXXRecordDecl *Decl,
    147                          llvm::DIFile F,
    148                          SmallVectorImpl<llvm::Value *> &EltTys);
    149 
    150   // CreateLexicalBlock - Create a new lexical block node and push it on
    151   // the stack.
    152   void CreateLexicalBlock(SourceLocation Loc);
    153 
    154 public:
    155   CGDebugInfo(CodeGenModule &CGM);
    156   ~CGDebugInfo();
    157   void finalize() { DBuilder.finalize(); }
    158 
    159   /// setLocation - Update the current source location. If \arg loc is
    160   /// invalid it is ignored.
    161   void setLocation(SourceLocation Loc);
    162 
    163   /// EmitLocation - Emit metadata to indicate a change in line/column
    164   /// information in the source file.
    165   void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
    166 
    167   /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
    168   /// start of a new function.
    169   void EmitFunctionStart(GlobalDecl GD, QualType FnType,
    170                          llvm::Function *Fn, CGBuilderTy &Builder);
    171 
    172   /// EmitFunctionEnd - Constructs the debug code for exiting a function.
    173   void EmitFunctionEnd(CGBuilderTy &Builder);
    174 
    175   /// UpdateCompletedType - Update type cache because the type is now
    176   /// translated.
    177   void UpdateCompletedType(const TagDecl *TD);
    178 
    179   /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
    180   /// new lexical block and push the block onto the stack.
    181   void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
    182 
    183   /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
    184   /// block and pop the current block.
    185   void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
    186 
    187   /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
    188   /// variable declaration.
    189   void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
    190                                  CGBuilderTy &Builder);
    191 
    192   /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
    193   /// imported variable declaration in a block.
    194   void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
    195                                          llvm::Value *storage,
    196                                          CGBuilderTy &Builder,
    197                                          const CGBlockInfo &blockInfo);
    198 
    199   /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
    200   /// variable declaration.
    201   void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
    202                                 unsigned ArgNo, CGBuilderTy &Builder);
    203 
    204   /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
    205   /// llvm.dbg.declare for the block-literal argument to a block
    206   /// invocation function.
    207   void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
    208                                             llvm::Value *addr,
    209                                             CGBuilderTy &Builder);
    210 
    211   /// EmitGlobalVariable - Emit information about a global variable.
    212   void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
    213 
    214   /// EmitGlobalVariable - Emit information about an objective-c interface.
    215   void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
    216 
    217   /// EmitGlobalVariable - Emit global variable's debug info.
    218   void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
    219 
    220   /// getOrCreateRecordType - Emit record type's standalone debug info.
    221   llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
    222 private:
    223   /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
    224   void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
    225                    unsigned ArgNo, CGBuilderTy &Builder);
    226 
    227   // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
    228   // See BuildByRefType.
    229   llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
    230                                             uint64_t *OffSet);
    231 
    232   /// getContextDescriptor - Get context info for the decl.
    233   llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
    234 
    235   /// getCurrentDirname - Return current directory name.
    236   StringRef getCurrentDirname();
    237 
    238   /// CreateCompileUnit - Create new compile unit.
    239   void CreateCompileUnit();
    240 
    241   /// getOrCreateFile - Get the file debug info descriptor for the input
    242   /// location.
    243   llvm::DIFile getOrCreateFile(SourceLocation Loc);
    244 
    245   /// getOrCreateMainFile - Get the file info for main compile unit.
    246   llvm::DIFile getOrCreateMainFile();
    247 
    248   /// getOrCreateType - Get the type from the cache or create a new type if
    249   /// necessary.
    250   llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
    251 
    252   /// CreateTypeNode - Create type metadata for a source language type.
    253   llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
    254 
    255   /// CreateMemberType - Create new member and increase Offset by FType's size.
    256   llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
    257                                 StringRef Name, uint64_t *Offset);
    258 
    259   /// getFunctionDeclaration - Return debug info descriptor to describe method
    260   /// declaration for the given method definition.
    261   llvm::DISubprogram getFunctionDeclaration(const Decl *D);
    262 
    263   /// getFunctionName - Get function name for the given FunctionDecl. If the
    264   /// name is constructred on demand (e.g. C++ destructor) then the name
    265   /// is stored on the side.
    266   StringRef getFunctionName(const FunctionDecl *FD);
    267 
    268   /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
    269   /// This is the display name for the debugging info.
    270   StringRef getObjCMethodName(const ObjCMethodDecl *FD);
    271 
    272   /// getSelectorName - Return selector name. This is used for debugging
    273   /// info.
    274   StringRef getSelectorName(Selector S);
    275 
    276   /// getClassName - Get class name including template argument list.
    277   StringRef getClassName(RecordDecl *RD);
    278 
    279   /// getVTableName - Get vtable name for the given Class.
    280   StringRef getVTableName(const CXXRecordDecl *Decl);
    281 
    282   /// getLineNumber - Get line number for the location. If location is invalid
    283   /// then use current location.
    284   unsigned getLineNumber(SourceLocation Loc);
    285 
    286   /// getColumnNumber - Get column number for the location. If location is
    287   /// invalid then use current location.
    288   unsigned getColumnNumber(SourceLocation Loc);
    289 };
    290 } // namespace CodeGen
    291 } // namespace clang
    292 
    293 
    294 #endif
    295