clang API Documentation

CGDebugInfo.h
Go to the documentation of this file.
00001 //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This is the source level debug info generator for llvm translation.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef CLANG_CODEGEN_CGDEBUGINFO_H
00015 #define CLANG_CODEGEN_CGDEBUGINFO_H
00016 
00017 #include "clang/AST/Type.h"
00018 #include "clang/AST/Expr.h"
00019 #include "clang/Basic/SourceLocation.h"
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/Analysis/DebugInfo.h"
00022 #include "llvm/Analysis/DIBuilder.h"
00023 #include "llvm/Support/ValueHandle.h"
00024 #include "llvm/Support/Allocator.h"
00025 
00026 #include "CGBuilder.h"
00027 
00028 namespace llvm {
00029   class MDNode;
00030 }
00031 
00032 namespace clang {
00033   class VarDecl;
00034   class ObjCInterfaceDecl;
00035   class ClassTemplateSpecializationDecl;
00036   class GlobalDecl;
00037 
00038 namespace CodeGen {
00039   class CodeGenModule;
00040   class CodeGenFunction;
00041   class CGBlockInfo;
00042 
00043 /// CGDebugInfo - This class gathers all debug information during compilation
00044 /// and is responsible for emitting to llvm globals or pass directly to
00045 /// the backend.
00046 class CGDebugInfo {
00047   CodeGenModule &CGM;
00048   llvm::DIBuilder DBuilder;
00049   llvm::DICompileUnit TheCU;
00050   SourceLocation CurLoc, PrevLoc;
00051   llvm::DIType VTablePtrType;
00052   
00053   /// TypeCache - Cache of previously constructed Types.
00054   llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
00055 
00056   /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
00057   llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
00058 
00059   /// ReplaceMap - Cache of forward declared types to RAUW at the end of
00060   /// compilation.
00061   std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
00062 
00063   bool BlockLiteralGenericSet;
00064   llvm::DIType BlockLiteralGeneric;
00065 
00066   // LexicalBlockStack - Keep track of our current nested lexical block.
00067   std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
00068   llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
00069   // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
00070   // beginning of a function. This is used to pop unbalanced regions at
00071   // the end of a function.
00072   std::vector<unsigned> FnBeginRegionCount;
00073 
00074   /// DebugInfoNames - This is a storage for names that are
00075   /// constructed on demand. For example, C++ destructors, C++ operators etc..
00076   llvm::BumpPtrAllocator DebugInfoNames;
00077   StringRef CWDName;
00078 
00079   llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
00080   llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
00081   llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
00082 
00083   /// Helper functions for getOrCreateType.
00084   llvm::DIType CreateType(const BuiltinType *Ty);
00085   llvm::DIType CreateType(const ComplexType *Ty);
00086   llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
00087   llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
00088   llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
00089                           llvm::DIFile F);
00090   llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
00091   llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
00092   llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
00093   llvm::DIType CreateType(const RecordType *Ty);
00094   llvm::DIType CreateLimitedType(const RecordType *Ty);
00095   llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
00096   llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
00097   llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
00098   llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
00099   llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
00100   llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
00101   llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
00102   llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
00103   llvm::DIType CreateEnumType(const EnumDecl *ED);
00104   llvm::DIType getTypeOrNull(const QualType);
00105   llvm::DIType getCompletedTypeOrNull(const QualType);
00106   llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
00107                                      llvm::DIFile F);
00108   llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
00109                                        llvm::DIFile F);
00110   llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
00111   llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
00112   llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
00113   llvm::DIType CreatePointerLikeType(unsigned Tag,
00114                                      const Type *Ty, QualType PointeeTy,
00115                                      llvm::DIFile F);
00116   
00117   llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
00118                                              llvm::DIFile F,
00119                                              llvm::DIType RecordTy);
00120   
00121   void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
00122                                  llvm::DIFile F,
00123                                  SmallVectorImpl<llvm::Value *> &E,
00124                                  llvm::DIType T);
00125 
00126   void CollectCXXFriends(const CXXRecordDecl *Decl,
00127                        llvm::DIFile F,
00128                        SmallVectorImpl<llvm::Value *> &EltTys,
00129                        llvm::DIType RecordTy);
00130 
00131   void CollectCXXBases(const CXXRecordDecl *Decl,
00132                        llvm::DIFile F,
00133                        SmallVectorImpl<llvm::Value *> &EltTys,
00134                        llvm::DIType RecordTy);
00135   
00136   llvm::DIArray
00137   CollectTemplateParams(const TemplateParameterList *TPList,
00138                         const TemplateArgumentList &TAList,
00139                         llvm::DIFile Unit);
00140   llvm::DIArray
00141   CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
00142   llvm::DIArray 
00143   CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
00144                            llvm::DIFile F);
00145 
00146   llvm::DIType createFieldType(StringRef name, QualType type,
00147                                uint64_t sizeInBitsOverride, SourceLocation loc,
00148                                AccessSpecifier AS, uint64_t offsetInBits,
00149                                llvm::DIFile tunit,
00150                                llvm::DIDescriptor scope);
00151   void CollectRecordStaticVars(const RecordDecl *, llvm::DIType);
00152   void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
00153                            SmallVectorImpl<llvm::Value *> &E,
00154                            llvm::DIType RecordTy);
00155 
00156   void CollectVTableInfo(const CXXRecordDecl *Decl,
00157                          llvm::DIFile F,
00158                          SmallVectorImpl<llvm::Value *> &EltTys);
00159 
00160   // CreateLexicalBlock - Create a new lexical block node and push it on
00161   // the stack.
00162   void CreateLexicalBlock(SourceLocation Loc);
00163   
00164 public:
00165   CGDebugInfo(CodeGenModule &CGM);
00166   ~CGDebugInfo();
00167 
00168   void finalize(void);
00169 
00170   /// setLocation - Update the current source location. If \arg loc is
00171   /// invalid it is ignored.
00172   void setLocation(SourceLocation Loc);
00173 
00174   /// EmitLocation - Emit metadata to indicate a change in line/column
00175   /// information in the source file.
00176   void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
00177 
00178   /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
00179   /// start of a new function.
00180   void EmitFunctionStart(GlobalDecl GD, QualType FnType,
00181                          llvm::Function *Fn, CGBuilderTy &Builder);
00182 
00183   /// EmitFunctionEnd - Constructs the debug code for exiting a function.
00184   void EmitFunctionEnd(CGBuilderTy &Builder);
00185 
00186   /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
00187   /// new lexical block and push the block onto the stack.
00188   void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
00189 
00190   /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
00191   /// block and pop the current block.
00192   void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
00193 
00194   /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
00195   /// variable declaration.
00196   void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
00197                                  CGBuilderTy &Builder);
00198 
00199   /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
00200   /// imported variable declaration in a block.
00201   void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
00202                                          llvm::Value *storage,
00203                                          CGBuilderTy &Builder,
00204                                          const CGBlockInfo &blockInfo);
00205 
00206   /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
00207   /// variable declaration.
00208   void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
00209                                 unsigned ArgNo, CGBuilderTy &Builder);
00210 
00211   /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
00212   /// llvm.dbg.declare for the block-literal argument to a block
00213   /// invocation function.
00214   void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
00215                                             llvm::Value *addr,
00216                                             CGBuilderTy &Builder);
00217 
00218   /// EmitGlobalVariable - Emit information about a global variable.
00219   void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
00220 
00221   /// EmitGlobalVariable - Emit information about an objective-c interface.
00222   void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
00223 
00224   /// EmitGlobalVariable - Emit global variable's debug info.
00225   void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
00226 
00227   /// getOrCreateRecordType - Emit record type's standalone debug info. 
00228   llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
00229 
00230   /// getOrCreateInterfaceType - Emit an objective c interface type standalone
00231   /// debug info.
00232   llvm::DIType getOrCreateInterfaceType(QualType Ty,
00233           SourceLocation Loc);
00234 
00235 private:
00236   /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
00237   void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
00238                    unsigned ArgNo, CGBuilderTy &Builder);
00239 
00240   // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.  
00241   // See BuildByRefType.
00242   llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, 
00243                                             uint64_t *OffSet);
00244 
00245   /// getContextDescriptor - Get context info for the decl.
00246   llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
00247 
00248   /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
00249   /// context.
00250   llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
00251   
00252   /// createContextChain - Create a set of decls for the context chain.
00253   llvm::DIDescriptor createContextChain(const Decl *Decl);
00254 
00255   /// getCurrentDirname - Return current directory name.
00256   StringRef getCurrentDirname();
00257 
00258   /// CreateCompileUnit - Create new compile unit.
00259   void CreateCompileUnit();
00260 
00261   /// getOrCreateFile - Get the file debug info descriptor for the input 
00262   /// location.
00263   llvm::DIFile getOrCreateFile(SourceLocation Loc);
00264 
00265   /// getOrCreateMainFile - Get the file info for main compile unit.
00266   llvm::DIFile getOrCreateMainFile();
00267 
00268   /// getOrCreateType - Get the type from the cache or create a new type if
00269   /// necessary.
00270   llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
00271 
00272   /// getOrCreateLimitedType - Get the type from the cache or create a new
00273   /// partial type if necessary.
00274   llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
00275 
00276   /// CreateTypeNode - Create type metadata for a source language type.
00277   llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
00278 
00279   /// CreateLimitedTypeNode - Create type metadata for a source language
00280   /// type, but only partial types for records.
00281   llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
00282 
00283   /// CreateMemberType - Create new member and increase Offset by FType's size.
00284   llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
00285                                 StringRef Name, uint64_t *Offset);
00286 
00287   /// getFunctionDeclaration - Return debug info descriptor to describe method
00288   /// declaration for the given method definition.
00289   llvm::DISubprogram getFunctionDeclaration(const Decl *D);
00290 
00291   /// getFunctionName - Get function name for the given FunctionDecl. If the
00292   /// name is constructred on demand (e.g. C++ destructor) then the name
00293   /// is stored on the side.
00294   StringRef getFunctionName(const FunctionDecl *FD);
00295 
00296   /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
00297   /// This is the display name for the debugging info.  
00298   StringRef getObjCMethodName(const ObjCMethodDecl *FD);
00299 
00300   /// getSelectorName - Return selector name. This is used for debugging
00301   /// info.
00302   StringRef getSelectorName(Selector S);
00303 
00304   /// getClassName - Get class name including template argument list.
00305   StringRef getClassName(const RecordDecl *RD);
00306 
00307   /// getVTableName - Get vtable name for the given Class.
00308   StringRef getVTableName(const CXXRecordDecl *Decl);
00309 
00310   /// getLineNumber - Get line number for the location. If location is invalid
00311   /// then use current location.
00312   unsigned getLineNumber(SourceLocation Loc);
00313 
00314   /// getColumnNumber - Get column number for the location. If location is 
00315   /// invalid then use current location.
00316   unsigned getColumnNumber(SourceLocation Loc);
00317 };
00318 } // namespace CodeGen
00319 } // namespace clang
00320 
00321 
00322 #endif