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 "CGBuilder.h" 18 #include "clang/AST/Expr.h" 19 #include "clang/AST/Type.h" 20 #include "clang/Basic/SourceLocation.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/DIBuilder.h" 23 #include "llvm/DebugInfo.h" 24 #include "llvm/Support/Allocator.h" 25 #include "llvm/Support/ValueHandle.h" 26 27 namespace llvm { 28 class MDNode; 29 } 30 31 namespace clang { 32 class CXXMethodDecl; 33 class VarDecl; 34 class ObjCInterfaceDecl; 35 class ObjCIvarDecl; 36 class ClassTemplateSpecializationDecl; 37 class GlobalDecl; 38 class UsingDecl; 39 40 namespace CodeGen { 41 class CodeGenModule; 42 class CodeGenFunction; 43 class CGBlockInfo; 44 45 /// CGDebugInfo - This class gathers all debug information during compilation 46 /// and is responsible for emitting to llvm globals or pass directly to 47 /// the backend. 48 class CGDebugInfo { 49 CodeGenModule &CGM; 50 llvm::DIBuilder DBuilder; 51 llvm::DICompileUnit TheCU; 52 SourceLocation CurLoc, PrevLoc; 53 llvm::DIType VTablePtrType; 54 llvm::DIType ClassTy; 55 llvm::DICompositeType ObjTy; 56 llvm::DIType SelTy; 57 llvm::DIType OCLImage1dDITy, OCLImage1dArrayDITy, OCLImage1dBufferDITy; 58 llvm::DIType OCLImage2dDITy, OCLImage2dArrayDITy; 59 llvm::DIType OCLImage3dDITy; 60 llvm::DIType OCLEventDITy; 61 62 /// TypeCache - Cache of previously constructed Types. 63 llvm::DenseMap<void *, llvm::WeakVH> TypeCache; 64 65 /// ObjCInterfaceCache - Cache of previously constructed interfaces 66 /// which may change. Storing a pair of DIType and checksum. 67 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > > 68 ObjCInterfaceCache; 69 70 /// RetainedTypes - list of interfaces we want to keep even if orphaned. 71 std::vector<void *> RetainedTypes; 72 73 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes. 74 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache; 75 76 /// ReplaceMap - Cache of forward declared types to RAUW at the end of 77 /// compilation. 78 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap; 79 80 bool BlockLiteralGenericSet; 81 llvm::DIType BlockLiteralGeneric; 82 83 // LexicalBlockStack - Keep track of our current nested lexical block. 84 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack; 85 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap; 86 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the 87 // beginning of a function. This is used to pop unbalanced regions at 88 // the end of a function. 89 std::vector<unsigned> FnBeginRegionCount; 90 91 /// DebugInfoNames - This is a storage for names that are 92 /// constructed on demand. For example, C++ destructors, C++ operators etc.. 93 llvm::BumpPtrAllocator DebugInfoNames; 94 StringRef CWDName; 95 96 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache; 97 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache; 98 llvm::DenseMap<const Decl *, llvm::WeakVH> DeclCache; 99 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache; 100 llvm::DenseMap<const Decl *, llvm::WeakVH> StaticDataMemberCache; 101 102 /// Helper functions for getOrCreateType. 103 unsigned Checksum(const ObjCInterfaceDecl *InterfaceDecl); 104 llvm::DIType CreateType(const BuiltinType *Ty); 105 llvm::DIType CreateType(const ComplexType *Ty); 106 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F); 107 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F); 108 llvm::DIType CreateType(const ObjCObjectPointerType *Ty, 109 llvm::DIFile F); 110 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F); 111 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F); 112 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F); 113 llvm::DIType CreateType(const RecordType *Ty); 114 llvm::DIType CreateLimitedType(const RecordType *Ty); 115 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F); 116 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F); 117 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F); 118 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F); 119 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F); 120 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit); 121 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F); 122 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F); 123 llvm::DIType CreateEnumType(const EnumDecl *ED); 124 llvm::DIType CreateSelfType(const QualType &QualTy, llvm::DIType Ty); 125 llvm::DIType getTypeOrNull(const QualType); 126 llvm::DIType getCompletedTypeOrNull(const QualType); 127 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method, 128 llvm::DIFile F); 129 llvm::DIType getOrCreateInstanceMethodType( 130 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit); 131 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType, 132 llvm::DIFile F); 133 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F); 134 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N); 135 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F); 136 llvm::DIType CreatePointerLikeType(unsigned Tag, 137 const Type *Ty, QualType PointeeTy, 138 llvm::DIFile F); 139 140 llvm::Value *getCachedInterfaceTypeOrNull(const QualType Ty); 141 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache); 142 143 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method, 144 llvm::DIFile F, 145 llvm::DIType RecordTy); 146 147 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, 148 llvm::DIFile F, 149 SmallVectorImpl<llvm::Value *> &E, 150 llvm::DIType T); 151 152 void CollectCXXFriends(const CXXRecordDecl *Decl, 153 llvm::DIFile F, 154 SmallVectorImpl<llvm::Value *> &EltTys, 155 llvm::DIType RecordTy); 156 157 void CollectCXXBases(const CXXRecordDecl *Decl, 158 llvm::DIFile F, 159 SmallVectorImpl<llvm::Value *> &EltTys, 160 llvm::DIType RecordTy); 161 162 llvm::DIArray 163 CollectTemplateParams(const TemplateParameterList *TPList, 164 const TemplateArgumentList &TAList, 165 llvm::DIFile Unit); 166 llvm::DIArray 167 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit); 168 llvm::DIArray 169 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS, 170 llvm::DIFile F); 171 172 llvm::DIType createFieldType(StringRef name, QualType type, 173 uint64_t sizeInBitsOverride, SourceLocation loc, 174 AccessSpecifier AS, uint64_t offsetInBits, 175 llvm::DIFile tunit, 176 llvm::DIDescriptor scope); 177 178 // Helpers for collecting fields of a record. 179 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl, 180 SmallVectorImpl<llvm::Value *> &E, 181 llvm::DIType RecordTy); 182 void CollectRecordStaticField(const VarDecl *Var, 183 SmallVectorImpl<llvm::Value *> &E, 184 llvm::DIType RecordTy); 185 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits, 186 llvm::DIFile F, 187 SmallVectorImpl<llvm::Value *> &E, 188 llvm::DIType RecordTy); 189 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F, 190 SmallVectorImpl<llvm::Value *> &E, 191 llvm::DIType RecordTy); 192 193 void CollectVTableInfo(const CXXRecordDecl *Decl, 194 llvm::DIFile F, 195 SmallVectorImpl<llvm::Value *> &EltTys); 196 197 // CreateLexicalBlock - Create a new lexical block node and push it on 198 // the stack. 199 void CreateLexicalBlock(SourceLocation Loc); 200 201 public: 202 CGDebugInfo(CodeGenModule &CGM); 203 ~CGDebugInfo(); 204 205 void finalize(); 206 207 /// setLocation - Update the current source location. If \arg loc is 208 /// invalid it is ignored. 209 void setLocation(SourceLocation Loc); 210 211 /// EmitLocation - Emit metadata to indicate a change in line/column 212 /// information in the source file. 213 /// \param ForceColumnInfo Assume DebugColumnInfo option is true. 214 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc, 215 bool ForceColumnInfo = false); 216 217 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate 218 /// start of a new function. 219 void EmitFunctionStart(GlobalDecl GD, QualType FnType, 220 llvm::Function *Fn, CGBuilderTy &Builder); 221 222 /// EmitFunctionEnd - Constructs the debug code for exiting a function. 223 void EmitFunctionEnd(CGBuilderTy &Builder); 224 225 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a 226 /// new lexical block and push the block onto the stack. 227 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc); 228 229 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical 230 /// block and pop the current block. 231 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc); 232 233 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic 234 /// variable declaration. 235 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, 236 CGBuilderTy &Builder); 237 238 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an 239 /// imported variable declaration in a block. 240 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable, 241 llvm::Value *storage, 242 CGBuilderTy &Builder, 243 const CGBlockInfo &blockInfo); 244 245 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 246 /// variable declaration. 247 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 248 unsigned ArgNo, CGBuilderTy &Builder); 249 250 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to 251 /// llvm.dbg.declare for the block-literal argument to a block 252 /// invocation function. 253 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, 254 llvm::Value *Arg, 255 llvm::Value *LocalAddr, 256 CGBuilderTy &Builder); 257 258 /// EmitGlobalVariable - Emit information about a global variable. 259 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); 260 261 /// EmitGlobalVariable - Emit information about an objective-c interface. 262 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl); 263 264 /// EmitGlobalVariable - Emit global variable's debug info. 265 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init); 266 267 /// \brief - Emit C++ using directive. 268 void EmitUsingDirective(const UsingDirectiveDecl &UD); 269 270 /// \brief - Emit C++ using declaration. 271 void EmitUsingDecl(const UsingDecl &UD); 272 273 /// getOrCreateRecordType - Emit record type's standalone debug info. 274 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L); 275 276 /// getOrCreateInterfaceType - Emit an objective c interface type standalone 277 /// debug info. 278 llvm::DIType getOrCreateInterfaceType(QualType Ty, 279 SourceLocation Loc); 280 281 private: 282 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration. 283 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI, 284 unsigned ArgNo, CGBuilderTy &Builder); 285 286 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 287 // See BuildByRefType. 288 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD, 289 uint64_t *OffSet); 290 291 /// getContextDescriptor - Get context info for the decl. 292 llvm::DIScope getContextDescriptor(const Decl *Decl); 293 294 llvm::DIScope getCurrentContextDescriptor(const Decl *Decl); 295 296 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given 297 /// context. 298 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor); 299 300 /// createContextChain - Create a set of decls for the context chain. 301 llvm::DIDescriptor createContextChain(const Decl *Decl); 302 303 /// getCurrentDirname - Return current directory name. 304 StringRef getCurrentDirname(); 305 306 /// CreateCompileUnit - Create new compile unit. 307 void CreateCompileUnit(); 308 309 /// getOrCreateFile - Get the file debug info descriptor for the input 310 /// location. 311 llvm::DIFile getOrCreateFile(SourceLocation Loc); 312 313 /// getOrCreateMainFile - Get the file info for main compile unit. 314 llvm::DIFile getOrCreateMainFile(); 315 316 /// getOrCreateType - Get the type from the cache or create a new type if 317 /// necessary. 318 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F); 319 320 /// getOrCreateLimitedType - Get the type from the cache or create a new 321 /// partial type if necessary. 322 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F); 323 324 /// CreateTypeNode - Create type metadata for a source language type. 325 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F); 326 327 /// getObjCInterfaceDecl - return the underlying ObjCInterfaceDecl 328 /// if Ty is an ObjCInterface or a pointer to one. 329 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty); 330 331 /// CreateLimitedTypeNode - Create type metadata for a source language 332 /// type, but only partial types for records. 333 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F); 334 335 /// CreateMemberType - Create new member and increase Offset by FType's size. 336 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType, 337 StringRef Name, uint64_t *Offset); 338 339 /// \brief Retrieve the DIDescriptor, if any, for the canonical form of this 340 /// declaration. 341 llvm::DIDescriptor getDeclarationOrDefinition(const Decl *D); 342 343 /// getFunctionDeclaration - Return debug info descriptor to describe method 344 /// declaration for the given method definition. 345 llvm::DISubprogram getFunctionDeclaration(const Decl *D); 346 347 /// getStaticDataMemberDeclaration - Return debug info descriptor to 348 /// describe in-class static data member declaration for the given 349 /// out-of-class definition. 350 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D); 351 352 /// getFunctionName - Get function name for the given FunctionDecl. If the 353 /// name is constructred on demand (e.g. C++ destructor) then the name 354 /// is stored on the side. 355 StringRef getFunctionName(const FunctionDecl *FD); 356 357 /// getObjCMethodName - Returns the unmangled name of an Objective-C method. 358 /// This is the display name for the debugging info. 359 StringRef getObjCMethodName(const ObjCMethodDecl *FD); 360 361 /// getSelectorName - Return selector name. This is used for debugging 362 /// info. 363 StringRef getSelectorName(Selector S); 364 365 /// getClassName - Get class name including template argument list. 366 StringRef getClassName(const RecordDecl *RD); 367 368 /// getVTableName - Get vtable name for the given Class. 369 StringRef getVTableName(const CXXRecordDecl *Decl); 370 371 /// getLineNumber - Get line number for the location. If location is invalid 372 /// then use current location. 373 unsigned getLineNumber(SourceLocation Loc); 374 375 /// getColumnNumber - Get column number for the location. If location is 376 /// invalid then use current location. 377 /// \param Force Assume DebugColumnInfo option is true. 378 unsigned getColumnNumber(SourceLocation Loc, bool Force=false); 379 }; 380 } // namespace CodeGen 381 } // namespace clang 382 383 384 #endif 385