Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// |
| 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 coordinates the debug information generation while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGDebugInfo.h" |
| 15 | #include "CGBlocks.h" |
| 16 | #include "CGObjCRuntime.h" |
| 17 | #include "CodeGenFunction.h" |
| 18 | #include "CodeGenModule.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/DeclFriend.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
| 22 | #include "clang/AST/DeclTemplate.h" |
| 23 | #include "clang/AST/Expr.h" |
| 24 | #include "clang/AST/RecordLayout.h" |
| 25 | #include "clang/Basic/FileManager.h" |
| 26 | #include "clang/Basic/SourceManager.h" |
| 27 | #include "clang/Basic/Version.h" |
| 28 | #include "clang/Frontend/CodeGenOptions.h" |
| 29 | #include "llvm/ADT/SmallVector.h" |
| 30 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Constants.h" |
| 32 | #include "llvm/IR/DataLayout.h" |
| 33 | #include "llvm/IR/DerivedTypes.h" |
| 34 | #include "llvm/IR/Instructions.h" |
| 35 | #include "llvm/IR/Intrinsics.h" |
| 36 | #include "llvm/IR/Module.h" |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Dwarf.h" |
| 38 | #include "llvm/Support/FileSystem.h" |
| 39 | using namespace clang; |
| 40 | using namespace clang::CodeGen; |
| 41 | |
| 42 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
| 43 | : CGM(CGM), DBuilder(CGM.getModule()), |
| 44 | BlockLiteralGenericSet(false) { |
| 45 | CreateCompileUnit(); |
| 46 | } |
| 47 | |
| 48 | CGDebugInfo::~CGDebugInfo() { |
| 49 | assert(LexicalBlockStack.empty() && |
| 50 | "Region stack mismatch, stack not empty!"); |
| 51 | } |
| 52 | |
| 53 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 54 | // If the new location isn't valid return. |
| 55 | if (!Loc.isValid()) return; |
| 56 | |
| 57 | CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); |
| 58 | |
| 59 | // If we've changed files in the middle of a lexical scope go ahead |
| 60 | // and create a new lexical scope with file node if it's different |
| 61 | // from the one in the scope. |
| 62 | if (LexicalBlockStack.empty()) return; |
| 63 | |
| 64 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 65 | PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); |
| 66 | PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc); |
| 67 | |
| 68 | if (PCLoc.isInvalid() || PPLoc.isInvalid() || |
| 69 | !strcmp(PPLoc.getFilename(), PCLoc.getFilename())) |
| 70 | return; |
| 71 | |
| 72 | llvm::MDNode *LB = LexicalBlockStack.back(); |
| 73 | llvm::DIScope Scope = llvm::DIScope(LB); |
| 74 | if (Scope.isLexicalBlockFile()) { |
| 75 | llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB); |
| 76 | llvm::DIDescriptor D |
| 77 | = DBuilder.createLexicalBlockFile(LBF.getScope(), |
| 78 | getOrCreateFile(CurLoc)); |
| 79 | llvm::MDNode *N = D; |
| 80 | LexicalBlockStack.pop_back(); |
| 81 | LexicalBlockStack.push_back(N); |
| 82 | } else if (Scope.isLexicalBlock()) { |
| 83 | llvm::DIDescriptor D |
| 84 | = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)); |
| 85 | llvm::MDNode *N = D; |
| 86 | LexicalBlockStack.pop_back(); |
| 87 | LexicalBlockStack.push_back(N); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// getContextDescriptor - Get context info for the decl. |
| 92 | llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) { |
| 93 | if (!Context) |
| 94 | return TheCU; |
| 95 | |
| 96 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator |
| 97 | I = RegionMap.find(Context); |
| 98 | if (I != RegionMap.end()) { |
| 99 | llvm::Value *V = I->second; |
| 100 | return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V)); |
| 101 | } |
| 102 | |
| 103 | // Check namespace. |
| 104 | if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
| 105 | return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl)); |
| 106 | |
| 107 | if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) { |
| 108 | if (!RDecl->isDependentType()) { |
| 109 | llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), |
| 110 | getOrCreateMainFile()); |
| 111 | return llvm::DIDescriptor(Ty); |
| 112 | } |
| 113 | } |
| 114 | return TheCU; |
| 115 | } |
| 116 | |
| 117 | /// getFunctionName - Get function name for the given FunctionDecl. If the |
| 118 | /// name is constructred on demand (e.g. C++ destructor) then the name |
| 119 | /// is stored on the side. |
| 120 | StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
| 121 | assert (FD && "Invalid FunctionDecl!"); |
| 122 | IdentifierInfo *FII = FD->getIdentifier(); |
| 123 | FunctionTemplateSpecializationInfo *Info |
| 124 | = FD->getTemplateSpecializationInfo(); |
| 125 | if (!Info && FII) |
| 126 | return FII->getName(); |
| 127 | |
| 128 | // Otherwise construct human readable name for debug info. |
| 129 | std::string NS = FD->getNameAsString(); |
| 130 | |
| 131 | // Add any template specialization args. |
| 132 | if (Info) { |
| 133 | const TemplateArgumentList *TArgs = Info->TemplateArguments; |
| 134 | const TemplateArgument *Args = TArgs->data(); |
| 135 | unsigned NumArgs = TArgs->size(); |
| 136 | PrintingPolicy Policy(CGM.getLangOpts()); |
| 137 | NS += TemplateSpecializationType::PrintTemplateArgumentList(Args, |
| 138 | NumArgs, |
| 139 | Policy); |
| 140 | } |
| 141 | |
| 142 | // Copy this name on the side and use its reference. |
| 143 | char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); |
| 144 | memcpy(StrPtr, NS.data(), NS.length()); |
| 145 | return StringRef(StrPtr, NS.length()); |
| 146 | } |
| 147 | |
| 148 | StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { |
| 149 | SmallString<256> MethodName; |
| 150 | llvm::raw_svector_ostream OS(MethodName); |
| 151 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 152 | const DeclContext *DC = OMD->getDeclContext(); |
| 153 | if (const ObjCImplementationDecl *OID = |
| 154 | dyn_cast<const ObjCImplementationDecl>(DC)) { |
| 155 | OS << OID->getName(); |
| 156 | } else if (const ObjCInterfaceDecl *OID = |
| 157 | dyn_cast<const ObjCInterfaceDecl>(DC)) { |
| 158 | OS << OID->getName(); |
| 159 | } else if (const ObjCCategoryImplDecl *OCD = |
| 160 | dyn_cast<const ObjCCategoryImplDecl>(DC)){ |
| 161 | OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' << |
| 162 | OCD->getIdentifier()->getNameStart() << ')'; |
| 163 | } |
| 164 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 165 | |
| 166 | char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell()); |
| 167 | memcpy(StrPtr, MethodName.begin(), OS.tell()); |
| 168 | return StringRef(StrPtr, OS.tell()); |
| 169 | } |
| 170 | |
| 171 | /// getSelectorName - Return selector name. This is used for debugging |
| 172 | /// info. |
| 173 | StringRef CGDebugInfo::getSelectorName(Selector S) { |
| 174 | const std::string &SName = S.getAsString(); |
| 175 | char *StrPtr = DebugInfoNames.Allocate<char>(SName.size()); |
| 176 | memcpy(StrPtr, SName.data(), SName.size()); |
| 177 | return StringRef(StrPtr, SName.size()); |
| 178 | } |
| 179 | |
| 180 | /// getClassName - Get class name including template argument list. |
| 181 | StringRef |
| 182 | CGDebugInfo::getClassName(const RecordDecl *RD) { |
| 183 | const ClassTemplateSpecializationDecl *Spec |
| 184 | = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 185 | if (!Spec) |
| 186 | return RD->getName(); |
| 187 | |
| 188 | const TemplateArgument *Args; |
| 189 | unsigned NumArgs; |
| 190 | if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { |
| 191 | const TemplateSpecializationType *TST = |
| 192 | cast<TemplateSpecializationType>(TAW->getType()); |
| 193 | Args = TST->getArgs(); |
| 194 | NumArgs = TST->getNumArgs(); |
| 195 | } else { |
| 196 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 197 | Args = TemplateArgs.data(); |
| 198 | NumArgs = TemplateArgs.size(); |
| 199 | } |
| 200 | StringRef Name = RD->getIdentifier()->getName(); |
| 201 | PrintingPolicy Policy(CGM.getLangOpts()); |
| 202 | std::string TemplateArgList = |
| 203 | TemplateSpecializationType::PrintTemplateArgumentList(Args, NumArgs, Policy); |
| 204 | |
| 205 | // Copy this name on the side and use its reference. |
| 206 | size_t Length = Name.size() + TemplateArgList.size(); |
| 207 | char *StrPtr = DebugInfoNames.Allocate<char>(Length); |
| 208 | memcpy(StrPtr, Name.data(), Name.size()); |
| 209 | memcpy(StrPtr + Name.size(), TemplateArgList.data(), TemplateArgList.size()); |
| 210 | return StringRef(StrPtr, Length); |
| 211 | } |
| 212 | |
| 213 | /// getOrCreateFile - Get the file debug info descriptor for the input location. |
| 214 | llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
| 215 | if (!Loc.isValid()) |
| 216 | // If Location is not valid then use main input file. |
| 217 | return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); |
| 218 | |
| 219 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 220 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 221 | |
| 222 | if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty()) |
| 223 | // If the location is not valid then use main input file. |
| 224 | return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); |
| 225 | |
| 226 | // Cache the results. |
| 227 | const char *fname = PLoc.getFilename(); |
| 228 | llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = |
| 229 | DIFileCache.find(fname); |
| 230 | |
| 231 | if (it != DIFileCache.end()) { |
| 232 | // Verify that the information still exists. |
| 233 | if (llvm::Value *V = it->second) |
| 234 | return llvm::DIFile(cast<llvm::MDNode>(V)); |
| 235 | } |
| 236 | |
| 237 | llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname()); |
| 238 | |
| 239 | DIFileCache[fname] = F; |
| 240 | return F; |
| 241 | } |
| 242 | |
| 243 | /// getOrCreateMainFile - Get the file info for main compile unit. |
| 244 | llvm::DIFile CGDebugInfo::getOrCreateMainFile() { |
| 245 | return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); |
| 246 | } |
| 247 | |
| 248 | /// getLineNumber - Get line number for the location. If location is invalid |
| 249 | /// then use current location. |
| 250 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
| 251 | if (Loc.isInvalid() && CurLoc.isInvalid()) |
| 252 | return 0; |
| 253 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 254 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| 255 | return PLoc.isValid()? PLoc.getLine() : 0; |
| 256 | } |
| 257 | |
| 258 | /// getColumnNumber - Get column number for the location. |
| 259 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) { |
| 260 | // We may not want column information at all. |
| 261 | if (!CGM.getCodeGenOpts().DebugColumnInfo) |
| 262 | return 0; |
| 263 | |
| 264 | // If the location is invalid then use the current column. |
| 265 | if (Loc.isInvalid() && CurLoc.isInvalid()) |
| 266 | return 0; |
| 267 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 268 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| 269 | return PLoc.isValid()? PLoc.getColumn() : 0; |
| 270 | } |
| 271 | |
| 272 | StringRef CGDebugInfo::getCurrentDirname() { |
| 273 | if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) |
| 274 | return CGM.getCodeGenOpts().DebugCompilationDir; |
| 275 | |
| 276 | if (!CWDName.empty()) |
| 277 | return CWDName; |
| 278 | SmallString<256> CWD; |
| 279 | llvm::sys::fs::current_path(CWD); |
| 280 | char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size()); |
| 281 | memcpy(CompDirnamePtr, CWD.data(), CWD.size()); |
| 282 | return CWDName = StringRef(CompDirnamePtr, CWD.size()); |
| 283 | } |
| 284 | |
| 285 | /// CreateCompileUnit - Create new compile unit. |
| 286 | void CGDebugInfo::CreateCompileUnit() { |
| 287 | |
| 288 | // Get absolute path name. |
| 289 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 290 | std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| 291 | if (MainFileName.empty()) |
| 292 | MainFileName = "<unknown>"; |
| 293 | |
| 294 | // The main file name provided via the "-main-file-name" option contains just |
| 295 | // the file name itself with no path information. This file name may have had |
| 296 | // a relative path, so we look into the actual file entry for the main |
| 297 | // file to determine the real absolute path for the file. |
| 298 | std::string MainFileDir; |
| 299 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
| 300 | MainFileDir = MainFile->getDir()->getName(); |
| 301 | if (MainFileDir != ".") |
| 302 | MainFileName = MainFileDir + "/" + MainFileName; |
| 303 | } |
| 304 | |
| 305 | // Save filename string. |
| 306 | char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length()); |
| 307 | memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length()); |
| 308 | StringRef Filename(FilenamePtr, MainFileName.length()); |
| 309 | |
| 310 | unsigned LangTag; |
| 311 | const LangOptions &LO = CGM.getLangOpts(); |
| 312 | if (LO.CPlusPlus) { |
| 313 | if (LO.ObjC1) |
| 314 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 315 | else |
| 316 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 317 | } else if (LO.ObjC1) { |
| 318 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
| 319 | } else if (LO.C99) { |
| 320 | LangTag = llvm::dwarf::DW_LANG_C99; |
| 321 | } else { |
| 322 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 323 | } |
| 324 | |
| 325 | std::string Producer = getClangFullVersion(); |
| 326 | |
| 327 | // Figure out which version of the ObjC runtime we have. |
| 328 | unsigned RuntimeVers = 0; |
| 329 | if (LO.ObjC1) |
| 330 | RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1; |
| 331 | |
| 332 | // Create new compile unit. |
| 333 | DBuilder.createCompileUnit( |
| 334 | LangTag, Filename, getCurrentDirname(), |
| 335 | Producer, |
| 336 | LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); |
| 337 | // FIXME - Eliminate TheCU. |
| 338 | TheCU = llvm::DICompileUnit(DBuilder.getCU()); |
| 339 | } |
| 340 | |
| 341 | /// CreateType - Get the Basic type from the cache or create a new |
| 342 | /// one if necessary. |
| 343 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) { |
| 344 | unsigned Encoding = 0; |
| 345 | StringRef BTName; |
| 346 | switch (BT->getKind()) { |
| 347 | #define BUILTIN_TYPE(Id, SingletonId) |
| 348 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 349 | case BuiltinType::Id: |
| 350 | #include "clang/AST/BuiltinTypes.def" |
| 351 | case BuiltinType::Dependent: |
| 352 | llvm_unreachable("Unexpected builtin type"); |
| 353 | case BuiltinType::NullPtr: |
| 354 | return DBuilder. |
| 355 | createNullPtrType(BT->getName(CGM.getLangOpts())); |
| 356 | case BuiltinType::Void: |
| 357 | return llvm::DIType(); |
| 358 | case BuiltinType::ObjCClass: |
| 359 | if (ClassTy.Verify()) |
| 360 | return ClassTy; |
| 361 | ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 362 | "objc_class", TheCU, |
| 363 | getOrCreateMainFile(), 0); |
| 364 | return ClassTy; |
| 365 | case BuiltinType::ObjCId: { |
| 366 | // typedef struct objc_class *Class; |
| 367 | // typedef struct objc_object { |
| 368 | // Class isa; |
| 369 | // } *id; |
| 370 | |
| 371 | if (ObjTy.Verify()) |
| 372 | return ObjTy; |
| 373 | |
| 374 | if (!ClassTy.Verify()) |
| 375 | ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 376 | "objc_class", TheCU, |
| 377 | getOrCreateMainFile(), 0); |
| 378 | |
| 379 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 380 | |
| 381 | llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size); |
| 382 | |
| 383 | llvm::DIType FwdTy = DBuilder.createStructType(TheCU, "objc_object", |
| 384 | getOrCreateMainFile(), |
| 385 | 0, 0, 0, 0, |
| 386 | llvm::DIArray()); |
| 387 | |
| 388 | llvm::TrackingVH<llvm::MDNode> ObjNode(FwdTy); |
| 389 | SmallVector<llvm::Value *, 1> EltTys; |
| 390 | llvm::DIType FieldTy = |
| 391 | DBuilder.createMemberType(llvm::DIDescriptor(ObjNode), "isa", |
| 392 | getOrCreateMainFile(), 0, Size, |
| 393 | 0, 0, 0, ISATy); |
| 394 | EltTys.push_back(FieldTy); |
| 395 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
| 396 | |
| 397 | ObjNode->replaceOperandWith(10, Elements); |
| 398 | ObjTy = llvm::DIType(ObjNode); |
| 399 | return ObjTy; |
| 400 | } |
| 401 | case BuiltinType::ObjCSel: { |
| 402 | if (SelTy.Verify()) |
| 403 | return SelTy; |
| 404 | SelTy = |
| 405 | DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 406 | "objc_selector", TheCU, getOrCreateMainFile(), |
| 407 | 0); |
| 408 | return SelTy; |
| 409 | } |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 410 | |
| 411 | case BuiltinType::OCLImage1d: |
| 412 | return getOrCreateStructPtrType("opencl_image1d_t", |
| 413 | OCLImage1dDITy); |
| 414 | case BuiltinType::OCLImage1dArray: |
| 415 | return getOrCreateStructPtrType("opencl_image1d_array_t", |
| 416 | OCLImage1dArrayDITy); |
| 417 | case BuiltinType::OCLImage1dBuffer: |
| 418 | return getOrCreateStructPtrType("opencl_image1d_buffer_t", |
| 419 | OCLImage1dBufferDITy); |
| 420 | case BuiltinType::OCLImage2d: |
| 421 | return getOrCreateStructPtrType("opencl_image2d_t", |
| 422 | OCLImage2dDITy); |
| 423 | case BuiltinType::OCLImage2dArray: |
| 424 | return getOrCreateStructPtrType("opencl_image2d_array_t", |
| 425 | OCLImage2dArrayDITy); |
| 426 | case BuiltinType::OCLImage3d: |
| 427 | return getOrCreateStructPtrType("opencl_image3d_t", |
| 428 | OCLImage3dDITy); |
| 429 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 430 | case BuiltinType::UChar: |
| 431 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 432 | case BuiltinType::Char_S: |
| 433 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
| 434 | case BuiltinType::Char16: |
| 435 | case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break; |
| 436 | case BuiltinType::UShort: |
| 437 | case BuiltinType::UInt: |
| 438 | case BuiltinType::UInt128: |
| 439 | case BuiltinType::ULong: |
| 440 | case BuiltinType::WChar_U: |
| 441 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 442 | case BuiltinType::Short: |
| 443 | case BuiltinType::Int: |
| 444 | case BuiltinType::Int128: |
| 445 | case BuiltinType::Long: |
| 446 | case BuiltinType::WChar_S: |
| 447 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 448 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
| 449 | case BuiltinType::Half: |
| 450 | case BuiltinType::Float: |
| 451 | case BuiltinType::LongDouble: |
| 452 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
| 453 | } |
| 454 | |
| 455 | switch (BT->getKind()) { |
| 456 | case BuiltinType::Long: BTName = "long int"; break; |
| 457 | case BuiltinType::LongLong: BTName = "long long int"; break; |
| 458 | case BuiltinType::ULong: BTName = "long unsigned int"; break; |
| 459 | case BuiltinType::ULongLong: BTName = "long long unsigned int"; break; |
| 460 | default: |
| 461 | BTName = BT->getName(CGM.getLangOpts()); |
| 462 | break; |
| 463 | } |
| 464 | // Bit size, align and offset of the type. |
| 465 | uint64_t Size = CGM.getContext().getTypeSize(BT); |
| 466 | uint64_t Align = CGM.getContext().getTypeAlign(BT); |
| 467 | llvm::DIType DbgTy = |
| 468 | DBuilder.createBasicType(BTName, Size, Align, Encoding); |
| 469 | return DbgTy; |
| 470 | } |
| 471 | |
| 472 | llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) { |
| 473 | // Bit size, align and offset of the type. |
| 474 | unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; |
| 475 | if (Ty->isComplexIntegerType()) |
| 476 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
| 477 | |
| 478 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 479 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 480 | llvm::DIType DbgTy = |
| 481 | DBuilder.createBasicType("complex", Size, Align, Encoding); |
| 482 | |
| 483 | return DbgTy; |
| 484 | } |
| 485 | |
| 486 | /// CreateCVRType - Get the qualified type from the cache or create |
| 487 | /// a new one if necessary. |
| 488 | llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { |
| 489 | QualifierCollector Qc; |
| 490 | const Type *T = Qc.strip(Ty); |
| 491 | |
| 492 | // Ignore these qualifiers for now. |
| 493 | Qc.removeObjCGCAttr(); |
| 494 | Qc.removeAddressSpace(); |
| 495 | Qc.removeObjCLifetime(); |
| 496 | |
| 497 | // We will create one Derived type for one qualifier and recurse to handle any |
| 498 | // additional ones. |
| 499 | unsigned Tag; |
| 500 | if (Qc.hasConst()) { |
| 501 | Tag = llvm::dwarf::DW_TAG_const_type; |
| 502 | Qc.removeConst(); |
| 503 | } else if (Qc.hasVolatile()) { |
| 504 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
| 505 | Qc.removeVolatile(); |
| 506 | } else if (Qc.hasRestrict()) { |
| 507 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
| 508 | Qc.removeRestrict(); |
| 509 | } else { |
| 510 | assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| 511 | return getOrCreateType(QualType(T, 0), Unit); |
| 512 | } |
| 513 | |
| 514 | llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); |
| 515 | |
| 516 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 517 | // CVR derived types. |
| 518 | llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy); |
| 519 | |
| 520 | return DbgTy; |
| 521 | } |
| 522 | |
| 523 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
| 524 | llvm::DIFile Unit) { |
| 525 | llvm::DIType DbgTy = |
| 526 | CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 527 | Ty->getPointeeType(), Unit); |
| 528 | return DbgTy; |
| 529 | } |
| 530 | |
| 531 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
| 532 | llvm::DIFile Unit) { |
| 533 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 534 | Ty->getPointeeType(), Unit); |
| 535 | } |
| 536 | |
| 537 | // Creates a forward declaration for a RecordDecl in the given context. |
| 538 | llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD, |
| 539 | llvm::DIDescriptor Ctx) { |
| 540 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 541 | unsigned Line = getLineNumber(RD->getLocation()); |
| 542 | StringRef RDName = getClassName(RD); |
| 543 | |
| 544 | unsigned Tag = 0; |
| 545 | if (RD->isStruct() || RD->isInterface()) |
| 546 | Tag = llvm::dwarf::DW_TAG_structure_type; |
| 547 | else if (RD->isUnion()) |
| 548 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 549 | else { |
| 550 | assert(RD->isClass()); |
| 551 | Tag = llvm::dwarf::DW_TAG_class_type; |
| 552 | } |
| 553 | |
| 554 | // Create the type. |
| 555 | return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line); |
| 556 | } |
| 557 | |
| 558 | // Walk up the context chain and create forward decls for record decls, |
| 559 | // and normal descriptors for namespaces. |
| 560 | llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) { |
| 561 | if (!Context) |
| 562 | return TheCU; |
| 563 | |
| 564 | // See if we already have the parent. |
| 565 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator |
| 566 | I = RegionMap.find(Context); |
| 567 | if (I != RegionMap.end()) { |
| 568 | llvm::Value *V = I->second; |
| 569 | return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V)); |
| 570 | } |
| 571 | |
| 572 | // Check namespace. |
| 573 | if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
| 574 | return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl)); |
| 575 | |
| 576 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) { |
| 577 | if (!RD->isDependentType()) { |
| 578 | llvm::DIType Ty = getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD), |
| 579 | getOrCreateMainFile()); |
| 580 | return llvm::DIDescriptor(Ty); |
| 581 | } |
| 582 | } |
| 583 | return TheCU; |
| 584 | } |
| 585 | |
| 586 | /// CreatePointeeType - Create Pointee type. If Pointee is a record |
| 587 | /// then emit record's fwd if debug info size reduction is enabled. |
| 588 | llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy, |
| 589 | llvm::DIFile Unit) { |
| 590 | if (CGM.getCodeGenOpts().getDebugInfo() != CodeGenOptions::LimitedDebugInfo) |
| 591 | return getOrCreateType(PointeeTy, Unit); |
| 592 | |
| 593 | // Limit debug info for the pointee type. |
| 594 | |
| 595 | // If we have an existing type, use that, it's still smaller than creating |
| 596 | // a new type. |
| 597 | llvm::DIType Ty = getTypeOrNull(PointeeTy); |
| 598 | if (Ty.Verify()) return Ty; |
| 599 | |
| 600 | // Handle qualifiers. |
| 601 | if (PointeeTy.hasLocalQualifiers()) |
| 602 | return CreateQualifiedType(PointeeTy, Unit); |
| 603 | |
| 604 | if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) { |
| 605 | RecordDecl *RD = RTy->getDecl(); |
| 606 | llvm::DIDescriptor FDContext = |
| 607 | getContextDescriptor(cast<Decl>(RD->getDeclContext())); |
| 608 | llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext); |
| 609 | TypeCache[QualType(RTy, 0).getAsOpaquePtr()] = RetTy; |
| 610 | return RetTy; |
| 611 | } |
| 612 | return getOrCreateType(PointeeTy, Unit); |
| 613 | |
| 614 | } |
| 615 | |
| 616 | llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, |
| 617 | const Type *Ty, |
| 618 | QualType PointeeTy, |
| 619 | llvm::DIFile Unit) { |
| 620 | if (Tag == llvm::dwarf::DW_TAG_reference_type || |
| 621 | Tag == llvm::dwarf::DW_TAG_rvalue_reference_type) |
| 622 | return DBuilder.createReferenceType(Tag, |
| 623 | CreatePointeeType(PointeeTy, Unit)); |
| 624 | |
| 625 | // Bit size, align and offset of the type. |
| 626 | // Size is always the size of a pointer. We can't use getTypeSize here |
| 627 | // because that does not return the correct value for references. |
| 628 | unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
| 629 | uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS); |
| 630 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 631 | |
| 632 | return DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit), |
| 633 | Size, Align); |
| 634 | } |
| 635 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 636 | llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache) { |
| 637 | if (Cache.Verify()) |
| 638 | return Cache; |
| 639 | Cache = |
| 640 | DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 641 | Name, TheCU, getOrCreateMainFile(), |
| 642 | 0); |
| 643 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 644 | Cache = DBuilder.createPointerType(Cache, Size); |
| 645 | return Cache; |
| 646 | } |
| 647 | |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 648 | llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, |
| 649 | llvm::DIFile Unit) { |
| 650 | if (BlockLiteralGenericSet) |
| 651 | return BlockLiteralGeneric; |
| 652 | |
| 653 | SmallVector<llvm::Value *, 8> EltTys; |
| 654 | llvm::DIType FieldTy; |
| 655 | QualType FType; |
| 656 | uint64_t FieldSize, FieldOffset; |
| 657 | unsigned FieldAlign; |
| 658 | llvm::DIArray Elements; |
| 659 | llvm::DIType EltTy, DescTy; |
| 660 | |
| 661 | FieldOffset = 0; |
| 662 | FType = CGM.getContext().UnsignedLongTy; |
| 663 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| 664 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
| 665 | |
| 666 | Elements = DBuilder.getOrCreateArray(EltTys); |
| 667 | EltTys.clear(); |
| 668 | |
| 669 | unsigned Flags = llvm::DIDescriptor::FlagAppleBlock; |
| 670 | unsigned LineNo = getLineNumber(CurLoc); |
| 671 | |
| 672 | EltTy = DBuilder.createStructType(Unit, "__block_descriptor", |
| 673 | Unit, LineNo, FieldOffset, 0, |
| 674 | Flags, Elements); |
| 675 | |
| 676 | // Bit size, align and offset of the type. |
| 677 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 678 | |
| 679 | DescTy = DBuilder.createPointerType(EltTy, Size); |
| 680 | |
| 681 | FieldOffset = 0; |
| 682 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 683 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 684 | FType = CGM.getContext().IntTy; |
| 685 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 686 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
| 687 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 688 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
| 689 | |
| 690 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 691 | FieldTy = DescTy; |
| 692 | FieldSize = CGM.getContext().getTypeSize(Ty); |
| 693 | FieldAlign = CGM.getContext().getTypeAlign(Ty); |
| 694 | FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit, |
| 695 | LineNo, FieldSize, FieldAlign, |
| 696 | FieldOffset, 0, FieldTy); |
| 697 | EltTys.push_back(FieldTy); |
| 698 | |
| 699 | FieldOffset += FieldSize; |
| 700 | Elements = DBuilder.getOrCreateArray(EltTys); |
| 701 | |
| 702 | EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", |
| 703 | Unit, LineNo, FieldOffset, 0, |
| 704 | Flags, Elements); |
| 705 | |
| 706 | BlockLiteralGenericSet = true; |
| 707 | BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size); |
| 708 | return BlockLiteralGeneric; |
| 709 | } |
| 710 | |
| 711 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) { |
| 712 | // Typedefs are derived from some other type. If we have a typedef of a |
| 713 | // typedef, make sure to emit the whole chain. |
| 714 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
| 715 | if (!Src.Verify()) |
| 716 | return llvm::DIType(); |
| 717 | // We don't set size information, but do specify where the typedef was |
| 718 | // declared. |
| 719 | unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); |
| 720 | const TypedefNameDecl *TyDecl = Ty->getDecl(); |
| 721 | |
| 722 | llvm::DIDescriptor TypedefContext = |
| 723 | getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())); |
| 724 | |
| 725 | return |
| 726 | DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext); |
| 727 | } |
| 728 | |
| 729 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
| 730 | llvm::DIFile Unit) { |
| 731 | SmallVector<llvm::Value *, 16> EltTys; |
| 732 | |
| 733 | // Add the result type at least. |
| 734 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
| 735 | |
| 736 | // Set up remainder of arguments if there is a prototype. |
| 737 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
| 738 | if (isa<FunctionNoProtoType>(Ty)) |
| 739 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
| 740 | else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) { |
| 741 | for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i) |
| 742 | EltTys.push_back(getOrCreateType(FPT->getArgType(i), Unit)); |
| 743 | } |
| 744 | |
| 745 | llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys); |
| 746 | return DBuilder.createSubroutineType(Unit, EltTypeArray); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | void CGDebugInfo:: |
| 751 | CollectRecordStaticVars(const RecordDecl *RD, llvm::DIType FwdDecl) { |
| 752 | |
| 753 | for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end(); |
| 754 | I != E; ++I) |
| 755 | if (const VarDecl *V = dyn_cast<VarDecl>(*I)) { |
| 756 | if (V->getInit()) { |
| 757 | const APValue *Value = V->evaluateValue(); |
| 758 | if (Value && Value->isInt()) { |
| 759 | llvm::ConstantInt *CI |
| 760 | = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt()); |
| 761 | |
| 762 | // Create the descriptor for static variable. |
| 763 | llvm::DIFile VUnit = getOrCreateFile(V->getLocation()); |
| 764 | StringRef VName = V->getName(); |
| 765 | llvm::DIType VTy = getOrCreateType(V->getType(), VUnit); |
| 766 | // Do not use DIGlobalVariable for enums. |
| 767 | if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) { |
| 768 | DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit, |
| 769 | getLineNumber(V->getLocation()), |
| 770 | VTy, true, CI); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | llvm::DIType CGDebugInfo::createFieldType(StringRef name, |
| 778 | QualType type, |
| 779 | uint64_t sizeInBitsOverride, |
| 780 | SourceLocation loc, |
| 781 | AccessSpecifier AS, |
| 782 | uint64_t offsetInBits, |
| 783 | llvm::DIFile tunit, |
| 784 | llvm::DIDescriptor scope) { |
| 785 | llvm::DIType debugType = getOrCreateType(type, tunit); |
| 786 | |
| 787 | // Get the location for the field. |
| 788 | llvm::DIFile file = getOrCreateFile(loc); |
| 789 | unsigned line = getLineNumber(loc); |
| 790 | |
| 791 | uint64_t sizeInBits = 0; |
| 792 | unsigned alignInBits = 0; |
| 793 | if (!type->isIncompleteArrayType()) { |
| 794 | llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type); |
| 795 | |
| 796 | if (sizeInBitsOverride) |
| 797 | sizeInBits = sizeInBitsOverride; |
| 798 | } |
| 799 | |
| 800 | unsigned flags = 0; |
| 801 | if (AS == clang::AS_private) |
| 802 | flags |= llvm::DIDescriptor::FlagPrivate; |
| 803 | else if (AS == clang::AS_protected) |
| 804 | flags |= llvm::DIDescriptor::FlagProtected; |
| 805 | |
| 806 | return DBuilder.createMemberType(scope, name, file, line, sizeInBits, |
| 807 | alignInBits, offsetInBits, flags, debugType); |
| 808 | } |
| 809 | |
| 810 | /// CollectRecordFields - A helper function to collect debug info for |
| 811 | /// record fields. This is used while creating debug info entry for a Record. |
| 812 | void CGDebugInfo:: |
| 813 | CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit, |
| 814 | SmallVectorImpl<llvm::Value *> &elements, |
| 815 | llvm::DIType RecordTy) { |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 816 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); |
| 817 | const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record); |
| 818 | |
| 819 | // For C++11 Lambdas a Field will be the same as a Capture, but the Capture |
| 820 | // has the name and the location of the variable so we should iterate over |
| 821 | // both concurrently. |
| 822 | if (CXXDecl && CXXDecl->isLambda()) { |
| 823 | RecordDecl::field_iterator Field = CXXDecl->field_begin(); |
| 824 | unsigned fieldno = 0; |
| 825 | for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(), |
| 826 | E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) { |
| 827 | const LambdaExpr::Capture C = *I; |
| 828 | if (C.capturesVariable()) { |
| 829 | VarDecl *V = C.getCapturedVar(); |
| 830 | llvm::DIFile VUnit = getOrCreateFile(C.getLocation()); |
| 831 | StringRef VName = V->getName(); |
| 832 | uint64_t SizeInBitsOverride = 0; |
| 833 | if (Field->isBitField()) { |
| 834 | SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext()); |
| 835 | assert(SizeInBitsOverride && "found named 0-width bitfield"); |
| 836 | } |
| 837 | llvm::DIType fieldType |
| 838 | = createFieldType(VName, Field->getType(), SizeInBitsOverride, C.getLocation(), |
| 839 | Field->getAccess(), layout.getFieldOffset(fieldno), |
| 840 | VUnit, RecordTy); |
| 841 | elements.push_back(fieldType); |
| 842 | } else { |
| 843 | // TODO: Need to handle 'this' in some way by probably renaming the |
| 844 | // this of the lambda class and having a field member of 'this' or |
| 845 | // by using AT_object_pointer for the function and having that be |
| 846 | // used as 'this' for semantic references. |
| 847 | assert(C.capturesThis() && "Field that isn't captured and isn't this?"); |
| 848 | FieldDecl *f = *Field; |
| 849 | llvm::DIFile VUnit = getOrCreateFile(f->getLocation()); |
| 850 | QualType type = f->getType(); |
| 851 | llvm::DIType fieldType |
| 852 | = createFieldType("this", type, 0, f->getLocation(), f->getAccess(), |
Eric Christopher | fd5ac0d | 2013-01-04 17:59:07 +0000 | [diff] [blame] | 853 | layout.getFieldOffset(fieldno), VUnit, RecordTy); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 854 | |
| 855 | elements.push_back(fieldType); |
| 856 | } |
| 857 | } |
| 858 | } else { |
Eric Christopher | fd5ac0d | 2013-01-04 17:59:07 +0000 | [diff] [blame] | 859 | unsigned fieldNo = 0; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 860 | bool IsMsStruct = record->isMsStruct(CGM.getContext()); |
| 861 | const FieldDecl *LastFD = 0; |
| 862 | for (RecordDecl::field_iterator I = record->field_begin(), |
| 863 | E = record->field_end(); |
| 864 | I != E; ++I, ++fieldNo) { |
| 865 | FieldDecl *field = *I; |
| 866 | |
| 867 | if (IsMsStruct) { |
| 868 | // Zero-length bitfields following non-bitfield members are ignored |
| 869 | if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) { |
| 870 | --fieldNo; |
| 871 | continue; |
| 872 | } |
| 873 | LastFD = field; |
| 874 | } |
| 875 | |
| 876 | StringRef name = field->getName(); |
| 877 | QualType type = field->getType(); |
| 878 | |
| 879 | // Ignore unnamed fields unless they're anonymous structs/unions. |
| 880 | if (name.empty() && !type->isRecordType()) { |
| 881 | LastFD = field; |
| 882 | continue; |
| 883 | } |
| 884 | |
| 885 | uint64_t SizeInBitsOverride = 0; |
| 886 | if (field->isBitField()) { |
| 887 | SizeInBitsOverride = field->getBitWidthValue(CGM.getContext()); |
| 888 | assert(SizeInBitsOverride && "found named 0-width bitfield"); |
| 889 | } |
| 890 | |
| 891 | llvm::DIType fieldType |
| 892 | = createFieldType(name, type, SizeInBitsOverride, |
| 893 | field->getLocation(), field->getAccess(), |
| 894 | layout.getFieldOffset(fieldNo), tunit, RecordTy); |
| 895 | |
| 896 | elements.push_back(fieldType); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This |
| 902 | /// function type is not updated to include implicit "this" pointer. Use this |
| 903 | /// routine to get a method type which includes "this" pointer. |
| 904 | llvm::DIType |
| 905 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
| 906 | llvm::DIFile Unit) { |
David Blaikie | 9c78f9b | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 907 | const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>(); |
David Blaikie | 67f8b5e | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 908 | if (Method->isStatic()) |
David Blaikie | 9c78f9b | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 909 | return getOrCreateType(QualType(Func, 0), Unit); |
| 910 | return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()), |
| 911 | Func, Unit); |
| 912 | } |
David Blaikie | 67f8b5e | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 913 | |
David Blaikie | 9c78f9b | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 914 | llvm::DIType CGDebugInfo::getOrCreateInstanceMethodType( |
| 915 | QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) { |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 916 | // Add "this" pointer. |
David Blaikie | 9c78f9b | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 917 | llvm::DIArray Args = llvm::DICompositeType( |
| 918 | getOrCreateType(QualType(Func, 0), Unit)).getTypeArray(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 919 | assert (Args.getNumElements() && "Invalid number of arguments!"); |
| 920 | |
| 921 | SmallVector<llvm::Value *, 16> Elts; |
| 922 | |
| 923 | // First element is always return type. For 'void' functions it is NULL. |
| 924 | Elts.push_back(Args.getElement(0)); |
| 925 | |
David Blaikie | 67f8b5e | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 926 | // "this" pointer is always first argument. |
David Blaikie | 9c78f9b | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 927 | const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl(); |
David Blaikie | 67f8b5e | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 928 | if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| 929 | // Create pointer type directly in this case. |
| 930 | const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); |
| 931 | QualType PointeeTy = ThisPtrTy->getPointeeType(); |
| 932 | unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
| 933 | uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS); |
| 934 | uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy); |
| 935 | llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit); |
| 936 | llvm::DIType ThisPtrType = DBuilder.createPointerType(PointeeType, Size, Align); |
| 937 | TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; |
| 938 | // TODO: This and the artificial type below are misleading, the |
| 939 | // types aren't artificial the argument is, but the current |
| 940 | // metadata doesn't represent that. |
| 941 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| 942 | Elts.push_back(ThisPtrType); |
| 943 | } else { |
| 944 | llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit); |
| 945 | TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; |
| 946 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| 947 | Elts.push_back(ThisPtrType); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // Copy rest of the arguments. |
| 951 | for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) |
| 952 | Elts.push_back(Args.getElement(i)); |
| 953 | |
| 954 | llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); |
| 955 | |
| 956 | return DBuilder.createSubroutineType(Unit, EltTypeArray); |
| 957 | } |
| 958 | |
| 959 | /// isFunctionLocalClass - Return true if CXXRecordDecl is defined |
| 960 | /// inside a function. |
| 961 | static bool isFunctionLocalClass(const CXXRecordDecl *RD) { |
| 962 | if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext())) |
| 963 | return isFunctionLocalClass(NRD); |
| 964 | if (isa<FunctionDecl>(RD->getDeclContext())) |
| 965 | return true; |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | /// CreateCXXMemberFunction - A helper function to create a DISubprogram for |
| 970 | /// a single member function GlobalDecl. |
| 971 | llvm::DISubprogram |
| 972 | CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, |
| 973 | llvm::DIFile Unit, |
| 974 | llvm::DIType RecordTy) { |
| 975 | bool IsCtorOrDtor = |
| 976 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
| 977 | |
| 978 | StringRef MethodName = getFunctionName(Method); |
| 979 | llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); |
| 980 | |
| 981 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| 982 | // make sense to give a single ctor/dtor a linkage name. |
| 983 | StringRef MethodLinkageName; |
| 984 | if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) |
| 985 | MethodLinkageName = CGM.getMangledName(Method); |
| 986 | |
| 987 | // Get the location for the method. |
| 988 | llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| 989 | unsigned MethodLine = getLineNumber(Method->getLocation()); |
| 990 | |
| 991 | // Collect virtual method info. |
| 992 | llvm::DIType ContainingType; |
| 993 | unsigned Virtuality = 0; |
| 994 | unsigned VIndex = 0; |
| 995 | |
| 996 | if (Method->isVirtual()) { |
| 997 | if (Method->isPure()) |
| 998 | Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| 999 | else |
| 1000 | Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
| 1001 | |
| 1002 | // It doesn't make sense to give a virtual destructor a vtable index, |
| 1003 | // since a single destructor has two entries in the vtable. |
| 1004 | if (!isa<CXXDestructorDecl>(Method)) |
| 1005 | VIndex = CGM.getVTableContext().getMethodVTableIndex(Method); |
| 1006 | ContainingType = RecordTy; |
| 1007 | } |
| 1008 | |
| 1009 | unsigned Flags = 0; |
| 1010 | if (Method->isImplicit()) |
| 1011 | Flags |= llvm::DIDescriptor::FlagArtificial; |
| 1012 | AccessSpecifier Access = Method->getAccess(); |
| 1013 | if (Access == clang::AS_private) |
| 1014 | Flags |= llvm::DIDescriptor::FlagPrivate; |
| 1015 | else if (Access == clang::AS_protected) |
| 1016 | Flags |= llvm::DIDescriptor::FlagProtected; |
| 1017 | if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { |
| 1018 | if (CXXC->isExplicit()) |
| 1019 | Flags |= llvm::DIDescriptor::FlagExplicit; |
| 1020 | } else if (const CXXConversionDecl *CXXC = |
| 1021 | dyn_cast<CXXConversionDecl>(Method)) { |
| 1022 | if (CXXC->isExplicit()) |
| 1023 | Flags |= llvm::DIDescriptor::FlagExplicit; |
| 1024 | } |
| 1025 | if (Method->hasPrototype()) |
| 1026 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
| 1027 | |
| 1028 | llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit); |
| 1029 | llvm::DISubprogram SP = |
| 1030 | DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName, |
| 1031 | MethodDefUnit, MethodLine, |
| 1032 | MethodTy, /*isLocalToUnit=*/false, |
| 1033 | /* isDefinition=*/ false, |
| 1034 | Virtuality, VIndex, ContainingType, |
| 1035 | Flags, CGM.getLangOpts().Optimize, NULL, |
| 1036 | TParamsArray); |
| 1037 | |
| 1038 | SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP); |
| 1039 | |
| 1040 | return SP; |
| 1041 | } |
| 1042 | |
| 1043 | /// CollectCXXMemberFunctions - A helper function to collect debug info for |
| 1044 | /// C++ member functions. This is used while creating debug info entry for |
| 1045 | /// a Record. |
| 1046 | void CGDebugInfo:: |
| 1047 | CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, |
| 1048 | SmallVectorImpl<llvm::Value *> &EltTys, |
| 1049 | llvm::DIType RecordTy) { |
| 1050 | |
| 1051 | // Since we want more than just the individual member decls if we |
| 1052 | // have templated functions iterate over every declaration to gather |
| 1053 | // the functions. |
| 1054 | for(DeclContext::decl_iterator I = RD->decls_begin(), |
| 1055 | E = RD->decls_end(); I != E; ++I) { |
| 1056 | Decl *D = *I; |
| 1057 | if (D->isImplicit() && !D->isUsed()) |
| 1058 | continue; |
| 1059 | |
| 1060 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 1061 | EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); |
| 1062 | else if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) |
| 1063 | for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(), |
| 1064 | SE = FTD->spec_end(); SI != SE; ++SI) |
| 1065 | EltTys.push_back(CreateCXXMemberFunction(cast<CXXMethodDecl>(*SI), Unit, |
| 1066 | RecordTy)); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /// CollectCXXFriends - A helper function to collect debug info for |
| 1071 | /// C++ base classes. This is used while creating debug info entry for |
| 1072 | /// a Record. |
| 1073 | void CGDebugInfo:: |
| 1074 | CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit, |
| 1075 | SmallVectorImpl<llvm::Value *> &EltTys, |
| 1076 | llvm::DIType RecordTy) { |
| 1077 | for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(), |
| 1078 | BE = RD->friend_end(); BI != BE; ++BI) { |
| 1079 | if ((*BI)->isUnsupportedFriend()) |
| 1080 | continue; |
| 1081 | if (TypeSourceInfo *TInfo = (*BI)->getFriendType()) |
| 1082 | EltTys.push_back(DBuilder.createFriend(RecordTy, |
| 1083 | getOrCreateType(TInfo->getType(), |
| 1084 | Unit))); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /// CollectCXXBases - A helper function to collect debug info for |
| 1089 | /// C++ base classes. This is used while creating debug info entry for |
| 1090 | /// a Record. |
| 1091 | void CGDebugInfo:: |
| 1092 | CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, |
| 1093 | SmallVectorImpl<llvm::Value *> &EltTys, |
| 1094 | llvm::DIType RecordTy) { |
| 1095 | |
| 1096 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 1097 | for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), |
| 1098 | BE = RD->bases_end(); BI != BE; ++BI) { |
| 1099 | unsigned BFlags = 0; |
| 1100 | uint64_t BaseOffset; |
| 1101 | |
| 1102 | const CXXRecordDecl *Base = |
| 1103 | cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); |
| 1104 | |
| 1105 | if (BI->isVirtual()) { |
| 1106 | // virtual base offset offset is -ve. The code generator emits dwarf |
| 1107 | // expression where it expects +ve number. |
| 1108 | BaseOffset = |
| 1109 | 0 - CGM.getVTableContext() |
| 1110 | .getVirtualBaseOffsetOffset(RD, Base).getQuantity(); |
| 1111 | BFlags = llvm::DIDescriptor::FlagVirtual; |
| 1112 | } else |
| 1113 | BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base)); |
| 1114 | // FIXME: Inconsistent units for BaseOffset. It is in bytes when |
| 1115 | // BI->isVirtual() and bits when not. |
| 1116 | |
| 1117 | AccessSpecifier Access = BI->getAccessSpecifier(); |
| 1118 | if (Access == clang::AS_private) |
| 1119 | BFlags |= llvm::DIDescriptor::FlagPrivate; |
| 1120 | else if (Access == clang::AS_protected) |
| 1121 | BFlags |= llvm::DIDescriptor::FlagProtected; |
| 1122 | |
| 1123 | llvm::DIType DTy = |
| 1124 | DBuilder.createInheritance(RecordTy, |
| 1125 | getOrCreateType(BI->getType(), Unit), |
| 1126 | BaseOffset, BFlags); |
| 1127 | EltTys.push_back(DTy); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | /// CollectTemplateParams - A helper function to collect template parameters. |
| 1132 | llvm::DIArray CGDebugInfo:: |
| 1133 | CollectTemplateParams(const TemplateParameterList *TPList, |
| 1134 | const TemplateArgumentList &TAList, |
| 1135 | llvm::DIFile Unit) { |
| 1136 | SmallVector<llvm::Value *, 16> TemplateParams; |
| 1137 | for (unsigned i = 0, e = TAList.size(); i != e; ++i) { |
| 1138 | const TemplateArgument &TA = TAList[i]; |
| 1139 | const NamedDecl *ND = TPList->getParam(i); |
| 1140 | if (TA.getKind() == TemplateArgument::Type) { |
| 1141 | llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit); |
| 1142 | llvm::DITemplateTypeParameter TTP = |
| 1143 | DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy); |
| 1144 | TemplateParams.push_back(TTP); |
| 1145 | } else if (TA.getKind() == TemplateArgument::Integral) { |
| 1146 | llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit); |
| 1147 | llvm::DITemplateValueParameter TVP = |
| 1148 | DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy, |
| 1149 | TA.getAsIntegral().getZExtValue()); |
| 1150 | TemplateParams.push_back(TVP); |
| 1151 | } |
| 1152 | } |
| 1153 | return DBuilder.getOrCreateArray(TemplateParams); |
| 1154 | } |
| 1155 | |
| 1156 | /// CollectFunctionTemplateParams - A helper function to collect debug |
| 1157 | /// info for function template parameters. |
| 1158 | llvm::DIArray CGDebugInfo:: |
| 1159 | CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) { |
| 1160 | if (FD->getTemplatedKind() == |
| 1161 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
| 1162 | const TemplateParameterList *TList = |
| 1163 | FD->getTemplateSpecializationInfo()->getTemplate() |
| 1164 | ->getTemplateParameters(); |
| 1165 | return |
| 1166 | CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit); |
| 1167 | } |
| 1168 | return llvm::DIArray(); |
| 1169 | } |
| 1170 | |
| 1171 | /// CollectCXXTemplateParams - A helper function to collect debug info for |
| 1172 | /// template parameters. |
| 1173 | llvm::DIArray CGDebugInfo:: |
| 1174 | CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial, |
| 1175 | llvm::DIFile Unit) { |
| 1176 | llvm::PointerUnion<ClassTemplateDecl *, |
| 1177 | ClassTemplatePartialSpecializationDecl *> |
| 1178 | PU = TSpecial->getSpecializedTemplateOrPartial(); |
| 1179 | |
| 1180 | TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ? |
| 1181 | PU.get<ClassTemplateDecl *>()->getTemplateParameters() : |
| 1182 | PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters(); |
| 1183 | const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs(); |
| 1184 | return CollectTemplateParams(TPList, TAList, Unit); |
| 1185 | } |
| 1186 | |
| 1187 | /// getOrCreateVTablePtrType - Return debug info descriptor for vtable. |
| 1188 | llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { |
| 1189 | if (VTablePtrType.isValid()) |
| 1190 | return VTablePtrType; |
| 1191 | |
| 1192 | ASTContext &Context = CGM.getContext(); |
| 1193 | |
| 1194 | /* Function type */ |
| 1195 | llvm::Value *STy = getOrCreateType(Context.IntTy, Unit); |
| 1196 | llvm::DIArray SElements = DBuilder.getOrCreateArray(STy); |
| 1197 | llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements); |
| 1198 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
| 1199 | llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0, |
| 1200 | "__vtbl_ptr_type"); |
| 1201 | VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); |
| 1202 | return VTablePtrType; |
| 1203 | } |
| 1204 | |
| 1205 | /// getVTableName - Get vtable name for the given Class. |
| 1206 | StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
| 1207 | // Construct gdb compatible name name. |
| 1208 | std::string Name = "_vptr$" + RD->getNameAsString(); |
| 1209 | |
| 1210 | // Copy this name on the side and use its reference. |
| 1211 | char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); |
| 1212 | memcpy(StrPtr, Name.data(), Name.length()); |
| 1213 | return StringRef(StrPtr, Name.length()); |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | /// CollectVTableInfo - If the C++ class has vtable info then insert appropriate |
| 1218 | /// debug info entry in EltTys vector. |
| 1219 | void CGDebugInfo:: |
| 1220 | CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, |
| 1221 | SmallVectorImpl<llvm::Value *> &EltTys) { |
| 1222 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 1223 | |
| 1224 | // If there is a primary base then it will hold vtable info. |
| 1225 | if (RL.getPrimaryBase()) |
| 1226 | return; |
| 1227 | |
| 1228 | // If this class is not dynamic then there is not any vtable info to collect. |
| 1229 | if (!RD->isDynamicClass()) |
| 1230 | return; |
| 1231 | |
| 1232 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 1233 | llvm::DIType VPTR |
| 1234 | = DBuilder.createMemberType(Unit, getVTableName(RD), Unit, |
| 1235 | 0, Size, 0, 0, llvm::DIDescriptor::FlagArtificial, |
| 1236 | getOrCreateVTablePtrType(Unit)); |
| 1237 | EltTys.push_back(VPTR); |
| 1238 | } |
| 1239 | |
| 1240 | /// getOrCreateRecordType - Emit record type's standalone debug info. |
| 1241 | llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy, |
| 1242 | SourceLocation Loc) { |
| 1243 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 1244 | llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc)); |
| 1245 | return T; |
| 1246 | } |
| 1247 | |
| 1248 | /// getOrCreateInterfaceType - Emit an objective c interface type standalone |
| 1249 | /// debug info. |
| 1250 | llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D, |
| 1251 | SourceLocation Loc) { |
| 1252 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 1253 | llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc)); |
| 1254 | DBuilder.retainType(T); |
| 1255 | return T; |
| 1256 | } |
| 1257 | |
| 1258 | /// CreateType - get structure or union type. |
| 1259 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) { |
| 1260 | RecordDecl *RD = Ty->getDecl(); |
| 1261 | |
| 1262 | // Get overall information about the record type for the debug info. |
| 1263 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 1264 | |
| 1265 | // Records and classes and unions can all be recursive. To handle them, we |
| 1266 | // first generate a debug descriptor for the struct as a forward declaration. |
| 1267 | // Then (if it is a definition) we go through and get debug info for all of |
| 1268 | // its members. Finally, we create a descriptor for the complete type (which |
| 1269 | // may refer to the forward decl if the struct is recursive) and replace all |
| 1270 | // uses of the forward declaration with the final definition. |
| 1271 | |
| 1272 | llvm::DIType FwdDecl = getOrCreateLimitedType(QualType(Ty, 0), DefUnit); |
| 1273 | |
| 1274 | if (FwdDecl.isForwardDecl()) |
| 1275 | return FwdDecl; |
| 1276 | |
| 1277 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode(FwdDecl); |
| 1278 | |
| 1279 | // Push the struct on region stack. |
| 1280 | LexicalBlockStack.push_back(FwdDeclNode); |
| 1281 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
| 1282 | |
| 1283 | // Add this to the completed types cache since we're completing it. |
| 1284 | CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
| 1285 | |
| 1286 | // Convert all the elements. |
| 1287 | SmallVector<llvm::Value *, 16> EltTys; |
| 1288 | |
| 1289 | // Note: The split of CXXDecl information here is intentional, the |
| 1290 | // gdb tests will depend on a certain ordering at printout. The debug |
| 1291 | // information offsets are still correct if we merge them all together |
| 1292 | // though. |
| 1293 | const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
| 1294 | if (CXXDecl) { |
| 1295 | CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl); |
| 1296 | CollectVTableInfo(CXXDecl, DefUnit, EltTys); |
| 1297 | } |
| 1298 | |
| 1299 | // Collect static variables with initializers and other fields. |
| 1300 | CollectRecordStaticVars(RD, FwdDecl); |
| 1301 | CollectRecordFields(RD, DefUnit, EltTys, FwdDecl); |
| 1302 | llvm::DIArray TParamsArray; |
| 1303 | if (CXXDecl) { |
| 1304 | CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl); |
| 1305 | CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl); |
| 1306 | if (const ClassTemplateSpecializationDecl *TSpecial |
| 1307 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
| 1308 | TParamsArray = CollectCXXTemplateParams(TSpecial, DefUnit); |
| 1309 | } |
| 1310 | |
| 1311 | LexicalBlockStack.pop_back(); |
| 1312 | RegionMap.erase(Ty->getDecl()); |
| 1313 | |
| 1314 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
| 1315 | // FIXME: Magic numbers ahoy! These should be changed when we |
| 1316 | // get some enums in llvm/Analysis/DebugInfo.h to refer to |
| 1317 | // them. |
| 1318 | if (RD->isUnion()) |
| 1319 | FwdDeclNode->replaceOperandWith(10, Elements); |
| 1320 | else if (CXXDecl) { |
| 1321 | FwdDeclNode->replaceOperandWith(10, Elements); |
| 1322 | FwdDeclNode->replaceOperandWith(13, TParamsArray); |
| 1323 | } else |
| 1324 | FwdDeclNode->replaceOperandWith(10, Elements); |
| 1325 | |
| 1326 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDeclNode); |
| 1327 | return llvm::DIType(FwdDeclNode); |
| 1328 | } |
| 1329 | |
| 1330 | /// CreateType - get objective-c object type. |
| 1331 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty, |
| 1332 | llvm::DIFile Unit) { |
| 1333 | // Ignore protocols. |
| 1334 | return getOrCreateType(Ty->getBaseType(), Unit); |
| 1335 | } |
| 1336 | |
| 1337 | /// CreateType - get objective-c interface type. |
| 1338 | llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
| 1339 | llvm::DIFile Unit) { |
| 1340 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
| 1341 | if (!ID) |
| 1342 | return llvm::DIType(); |
| 1343 | |
| 1344 | // Get overall information about the record type for the debug info. |
| 1345 | llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); |
| 1346 | unsigned Line = getLineNumber(ID->getLocation()); |
| 1347 | unsigned RuntimeLang = TheCU.getLanguage(); |
| 1348 | |
| 1349 | // If this is just a forward declaration return a special forward-declaration |
| 1350 | // debug type since we won't be able to lay out the entire type. |
| 1351 | ObjCInterfaceDecl *Def = ID->getDefinition(); |
| 1352 | if (!Def) { |
| 1353 | llvm::DIType FwdDecl = |
| 1354 | DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 1355 | ID->getName(), TheCU, DefUnit, Line, |
| 1356 | RuntimeLang); |
| 1357 | return FwdDecl; |
| 1358 | } |
| 1359 | |
| 1360 | ID = Def; |
| 1361 | |
| 1362 | // Bit size, align and offset of the type. |
| 1363 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1364 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 1365 | |
| 1366 | unsigned Flags = 0; |
| 1367 | if (ID->getImplementation()) |
| 1368 | Flags |= llvm::DIDescriptor::FlagObjcClassComplete; |
| 1369 | |
| 1370 | llvm::DIType RealDecl = |
| 1371 | DBuilder.createStructType(Unit, ID->getName(), DefUnit, |
| 1372 | Line, Size, Align, Flags, |
| 1373 | llvm::DIArray(), RuntimeLang); |
| 1374 | |
| 1375 | // Otherwise, insert it into the CompletedTypeCache so that recursive uses |
| 1376 | // will find it and we're emitting the complete type. |
| 1377 | CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl; |
| 1378 | // Push the struct on region stack. |
| 1379 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode(RealDecl); |
| 1380 | |
| 1381 | LexicalBlockStack.push_back(FwdDeclNode); |
| 1382 | RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl); |
| 1383 | |
| 1384 | // Convert all the elements. |
| 1385 | SmallVector<llvm::Value *, 16> EltTys; |
| 1386 | |
| 1387 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
| 1388 | if (SClass) { |
| 1389 | llvm::DIType SClassTy = |
| 1390 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
| 1391 | if (!SClassTy.isValid()) |
| 1392 | return llvm::DIType(); |
| 1393 | |
| 1394 | llvm::DIType InhTag = |
| 1395 | DBuilder.createInheritance(RealDecl, SClassTy, 0, 0); |
| 1396 | EltTys.push_back(InhTag); |
| 1397 | } |
| 1398 | |
| 1399 | for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(), |
| 1400 | E = ID->prop_end(); I != E; ++I) { |
| 1401 | const ObjCPropertyDecl *PD = *I; |
| 1402 | SourceLocation Loc = PD->getLocation(); |
| 1403 | llvm::DIFile PUnit = getOrCreateFile(Loc); |
| 1404 | unsigned PLine = getLineNumber(Loc); |
| 1405 | ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| 1406 | ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
| 1407 | llvm::MDNode *PropertyNode = |
| 1408 | DBuilder.createObjCProperty(PD->getName(), |
| 1409 | PUnit, PLine, |
| 1410 | (Getter && Getter->isImplicit()) ? "" : |
| 1411 | getSelectorName(PD->getGetterName()), |
| 1412 | (Setter && Setter->isImplicit()) ? "" : |
| 1413 | getSelectorName(PD->getSetterName()), |
| 1414 | PD->getPropertyAttributes(), |
| 1415 | getOrCreateType(PD->getType(), PUnit)); |
| 1416 | EltTys.push_back(PropertyNode); |
| 1417 | } |
| 1418 | |
| 1419 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
| 1420 | unsigned FieldNo = 0; |
| 1421 | for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; |
| 1422 | Field = Field->getNextIvar(), ++FieldNo) { |
| 1423 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
| 1424 | if (!FieldTy.isValid()) |
| 1425 | return llvm::DIType(); |
| 1426 | |
| 1427 | StringRef FieldName = Field->getName(); |
| 1428 | |
| 1429 | // Ignore unnamed fields. |
| 1430 | if (FieldName.empty()) |
| 1431 | continue; |
| 1432 | |
| 1433 | // Get the location for the field. |
| 1434 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 1435 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
| 1436 | QualType FType = Field->getType(); |
| 1437 | uint64_t FieldSize = 0; |
| 1438 | unsigned FieldAlign = 0; |
| 1439 | |
| 1440 | if (!FType->isIncompleteArrayType()) { |
| 1441 | |
| 1442 | // Bit size, align and offset of the type. |
| 1443 | FieldSize = Field->isBitField() |
| 1444 | ? Field->getBitWidthValue(CGM.getContext()) |
| 1445 | : CGM.getContext().getTypeSize(FType); |
| 1446 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
| 1447 | } |
| 1448 | |
| 1449 | uint64_t FieldOffset; |
| 1450 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
| 1451 | // We don't know the runtime offset of an ivar if we're using the |
| 1452 | // non-fragile ABI. For bitfields, use the bit offset into the first |
| 1453 | // byte of storage of the bitfield. For other fields, use zero. |
| 1454 | if (Field->isBitField()) { |
| 1455 | FieldOffset = CGM.getObjCRuntime().ComputeBitfieldBitOffset( |
| 1456 | CGM, ID, Field); |
| 1457 | FieldOffset %= CGM.getContext().getCharWidth(); |
| 1458 | } else { |
| 1459 | FieldOffset = 0; |
| 1460 | } |
| 1461 | } else { |
| 1462 | FieldOffset = RL.getFieldOffset(FieldNo); |
| 1463 | } |
| 1464 | |
| 1465 | unsigned Flags = 0; |
| 1466 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
| 1467 | Flags = llvm::DIDescriptor::FlagProtected; |
| 1468 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
| 1469 | Flags = llvm::DIDescriptor::FlagPrivate; |
| 1470 | |
| 1471 | llvm::MDNode *PropertyNode = NULL; |
| 1472 | if (ObjCImplementationDecl *ImpD = ID->getImplementation()) { |
| 1473 | if (ObjCPropertyImplDecl *PImpD = |
| 1474 | ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { |
| 1475 | if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { |
| 1476 | SourceLocation Loc = PD->getLocation(); |
| 1477 | llvm::DIFile PUnit = getOrCreateFile(Loc); |
| 1478 | unsigned PLine = getLineNumber(Loc); |
| 1479 | ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| 1480 | ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
| 1481 | PropertyNode = |
| 1482 | DBuilder.createObjCProperty(PD->getName(), |
| 1483 | PUnit, PLine, |
| 1484 | (Getter && Getter->isImplicit()) ? "" : |
| 1485 | getSelectorName(PD->getGetterName()), |
| 1486 | (Setter && Setter->isImplicit()) ? "" : |
| 1487 | getSelectorName(PD->getSetterName()), |
| 1488 | PD->getPropertyAttributes(), |
| 1489 | getOrCreateType(PD->getType(), PUnit)); |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, |
| 1494 | FieldLine, FieldSize, FieldAlign, |
| 1495 | FieldOffset, Flags, FieldTy, |
| 1496 | PropertyNode); |
| 1497 | EltTys.push_back(FieldTy); |
| 1498 | } |
| 1499 | |
| 1500 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
| 1501 | FwdDeclNode->replaceOperandWith(10, Elements); |
| 1502 | |
| 1503 | LexicalBlockStack.pop_back(); |
| 1504 | return llvm::DIType(FwdDeclNode); |
| 1505 | } |
| 1506 | |
| 1507 | llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) { |
| 1508 | llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
| 1509 | int64_t Count = Ty->getNumElements(); |
| 1510 | if (Count == 0) |
| 1511 | // If number of elements are not known then this is an unbounded array. |
| 1512 | // Use Count == -1 to express such arrays. |
| 1513 | Count = -1; |
| 1514 | |
| 1515 | llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count); |
| 1516 | llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); |
| 1517 | |
| 1518 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1519 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 1520 | |
| 1521 | return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); |
| 1522 | } |
| 1523 | |
| 1524 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
| 1525 | llvm::DIFile Unit) { |
| 1526 | uint64_t Size; |
| 1527 | uint64_t Align; |
| 1528 | |
| 1529 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
| 1530 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { |
| 1531 | Size = 0; |
| 1532 | Align = |
| 1533 | CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); |
| 1534 | } else if (Ty->isIncompleteArrayType()) { |
| 1535 | Size = 0; |
| 1536 | if (Ty->getElementType()->isIncompleteType()) |
| 1537 | Align = 0; |
| 1538 | else |
| 1539 | Align = CGM.getContext().getTypeAlign(Ty->getElementType()); |
| 1540 | } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) { |
| 1541 | Size = 0; |
| 1542 | Align = 0; |
| 1543 | } else { |
| 1544 | // Size and align of the whole array, not the element type. |
| 1545 | Size = CGM.getContext().getTypeSize(Ty); |
| 1546 | Align = CGM.getContext().getTypeAlign(Ty); |
| 1547 | } |
| 1548 | |
| 1549 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 1550 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 1551 | // obvious/recursive way? |
| 1552 | SmallVector<llvm::Value *, 8> Subscripts; |
| 1553 | QualType EltTy(Ty, 0); |
| 1554 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
| 1555 | // If the number of elements is known, then count is that number. Otherwise, |
| 1556 | // it's -1. This allows us to represent a subrange with an array of 0 |
| 1557 | // elements, like this: |
| 1558 | // |
| 1559 | // struct foo { |
| 1560 | // int x[0]; |
| 1561 | // }; |
| 1562 | int64_t Count = -1; // Count == -1 is an unbounded array. |
| 1563 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) |
| 1564 | Count = CAT->getSize().getZExtValue(); |
| 1565 | |
| 1566 | // FIXME: Verify this is right for VLAs. |
| 1567 | Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count)); |
| 1568 | EltTy = Ty->getElementType(); |
| 1569 | } |
| 1570 | |
| 1571 | llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); |
| 1572 | |
| 1573 | llvm::DIType DbgTy = |
| 1574 | DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), |
| 1575 | SubscriptArray); |
| 1576 | return DbgTy; |
| 1577 | } |
| 1578 | |
| 1579 | llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
| 1580 | llvm::DIFile Unit) { |
| 1581 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, |
| 1582 | Ty, Ty->getPointeeType(), Unit); |
| 1583 | } |
| 1584 | |
| 1585 | llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty, |
| 1586 | llvm::DIFile Unit) { |
| 1587 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, |
| 1588 | Ty, Ty->getPointeeType(), Unit); |
| 1589 | } |
| 1590 | |
| 1591 | llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, |
| 1592 | llvm::DIFile U) { |
David Blaikie | 0028ee3 | 2013-01-07 05:55:03 +0000 | [diff] [blame] | 1593 | return DBuilder.createMemberPointerType(CreatePointeeType(Ty->getPointeeType(), U), |
| 1594 | getOrCreateType(QualType(Ty->getClass(), 0), U)); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, |
| 1598 | llvm::DIFile U) { |
| 1599 | // Ignore the atomic wrapping |
| 1600 | // FIXME: What is the correct representation? |
| 1601 | return getOrCreateType(Ty->getValueType(), U); |
| 1602 | } |
| 1603 | |
| 1604 | /// CreateEnumType - get enumeration type. |
| 1605 | llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) { |
| 1606 | uint64_t Size = 0; |
| 1607 | uint64_t Align = 0; |
| 1608 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 1609 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
| 1610 | Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl()); |
| 1611 | } |
| 1612 | |
| 1613 | // If this is just a forward declaration, construct an appropriately |
| 1614 | // marked node and just return it. |
| 1615 | if (!ED->getDefinition()) { |
| 1616 | llvm::DIDescriptor EDContext; |
| 1617 | EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext())); |
| 1618 | llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); |
| 1619 | unsigned Line = getLineNumber(ED->getLocation()); |
| 1620 | StringRef EDName = ED->getName(); |
| 1621 | return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type, |
| 1622 | EDName, EDContext, DefUnit, Line, 0, |
| 1623 | Size, Align); |
| 1624 | } |
| 1625 | |
| 1626 | // Create DIEnumerator elements for each enumerator. |
| 1627 | SmallVector<llvm::Value *, 16> Enumerators; |
| 1628 | ED = ED->getDefinition(); |
| 1629 | for (EnumDecl::enumerator_iterator |
| 1630 | Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); |
| 1631 | Enum != EnumEnd; ++Enum) { |
| 1632 | Enumerators.push_back( |
| 1633 | DBuilder.createEnumerator(Enum->getName(), |
| 1634 | Enum->getInitVal().getZExtValue())); |
| 1635 | } |
| 1636 | |
| 1637 | // Return a CompositeType for the enum itself. |
| 1638 | llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators); |
| 1639 | |
| 1640 | llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); |
| 1641 | unsigned Line = getLineNumber(ED->getLocation()); |
| 1642 | llvm::DIDescriptor EnumContext = |
| 1643 | getContextDescriptor(cast<Decl>(ED->getDeclContext())); |
| 1644 | llvm::DIType ClassTy = ED->isScopedUsingClassTag() ? |
| 1645 | getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType(); |
| 1646 | llvm::DIType DbgTy = |
| 1647 | DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line, |
| 1648 | Size, Align, EltArray, |
| 1649 | ClassTy); |
| 1650 | return DbgTy; |
| 1651 | } |
| 1652 | |
| 1653 | static QualType UnwrapTypeForDebugInfo(QualType T) { |
| 1654 | do { |
| 1655 | QualType LastT = T; |
| 1656 | switch (T->getTypeClass()) { |
| 1657 | default: |
| 1658 | return T; |
| 1659 | case Type::TemplateSpecialization: |
| 1660 | T = cast<TemplateSpecializationType>(T)->desugar(); |
| 1661 | break; |
| 1662 | case Type::TypeOfExpr: |
| 1663 | T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); |
| 1664 | break; |
| 1665 | case Type::TypeOf: |
| 1666 | T = cast<TypeOfType>(T)->getUnderlyingType(); |
| 1667 | break; |
| 1668 | case Type::Decltype: |
| 1669 | T = cast<DecltypeType>(T)->getUnderlyingType(); |
| 1670 | break; |
| 1671 | case Type::UnaryTransform: |
| 1672 | T = cast<UnaryTransformType>(T)->getUnderlyingType(); |
| 1673 | break; |
| 1674 | case Type::Attributed: |
| 1675 | T = cast<AttributedType>(T)->getEquivalentType(); |
| 1676 | break; |
| 1677 | case Type::Elaborated: |
| 1678 | T = cast<ElaboratedType>(T)->getNamedType(); |
| 1679 | break; |
| 1680 | case Type::Paren: |
| 1681 | T = cast<ParenType>(T)->getInnerType(); |
| 1682 | break; |
| 1683 | case Type::SubstTemplateTypeParm: { |
| 1684 | // We need to keep the qualifiers handy since getReplacementType() |
| 1685 | // will strip them away. |
| 1686 | unsigned Quals = T.getLocalFastQualifiers(); |
| 1687 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
| 1688 | T.addFastQualifiers(Quals); |
| 1689 | } |
| 1690 | break; |
| 1691 | case Type::Auto: |
| 1692 | T = cast<AutoType>(T)->getDeducedType(); |
| 1693 | break; |
| 1694 | } |
| 1695 | |
| 1696 | assert(T != LastT && "Type unwrapping failed to unwrap!"); |
| 1697 | if (T == LastT) |
| 1698 | return T; |
| 1699 | } while (true); |
| 1700 | } |
| 1701 | |
| 1702 | /// getType - Get the type from the cache or return null type if it doesn't exist. |
| 1703 | llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) { |
| 1704 | |
| 1705 | // Unwrap the type as needed for debug information. |
| 1706 | Ty = UnwrapTypeForDebugInfo(Ty); |
| 1707 | |
| 1708 | // Check for existing entry. |
| 1709 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
| 1710 | TypeCache.find(Ty.getAsOpaquePtr()); |
| 1711 | if (it != TypeCache.end()) { |
| 1712 | // Verify that the debug info still exists. |
| 1713 | if (llvm::Value *V = it->second) |
| 1714 | return llvm::DIType(cast<llvm::MDNode>(V)); |
| 1715 | } |
| 1716 | |
| 1717 | return llvm::DIType(); |
| 1718 | } |
| 1719 | |
| 1720 | /// getCompletedTypeOrNull - Get the type from the cache or return null if it |
| 1721 | /// doesn't exist. |
| 1722 | llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) { |
| 1723 | |
| 1724 | // Unwrap the type as needed for debug information. |
| 1725 | Ty = UnwrapTypeForDebugInfo(Ty); |
| 1726 | |
| 1727 | // Check for existing entry. |
| 1728 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
| 1729 | CompletedTypeCache.find(Ty.getAsOpaquePtr()); |
| 1730 | if (it != CompletedTypeCache.end()) { |
| 1731 | // Verify that the debug info still exists. |
| 1732 | if (llvm::Value *V = it->second) |
| 1733 | return llvm::DIType(cast<llvm::MDNode>(V)); |
| 1734 | } |
| 1735 | |
| 1736 | return llvm::DIType(); |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | /// getOrCreateType - Get the type from the cache or create a new |
| 1741 | /// one if necessary. |
| 1742 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) { |
| 1743 | if (Ty.isNull()) |
| 1744 | return llvm::DIType(); |
| 1745 | |
| 1746 | // Unwrap the type as needed for debug information. |
| 1747 | Ty = UnwrapTypeForDebugInfo(Ty); |
| 1748 | |
| 1749 | llvm::DIType T = getCompletedTypeOrNull(Ty); |
| 1750 | |
| 1751 | if (T.Verify()) |
| 1752 | return T; |
| 1753 | |
| 1754 | // Otherwise create the type. |
| 1755 | llvm::DIType Res = CreateTypeNode(Ty, Unit); |
| 1756 | |
| 1757 | llvm::DIType TC = getTypeOrNull(Ty); |
| 1758 | if (TC.Verify() && TC.isForwardDecl()) |
| 1759 | ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), |
| 1760 | static_cast<llvm::Value*>(TC))); |
| 1761 | |
| 1762 | // And update the type cache. |
| 1763 | TypeCache[Ty.getAsOpaquePtr()] = Res; |
| 1764 | |
| 1765 | if (!Res.isForwardDecl()) |
| 1766 | CompletedTypeCache[Ty.getAsOpaquePtr()] = Res; |
| 1767 | |
| 1768 | return Res; |
| 1769 | } |
| 1770 | |
| 1771 | /// CreateTypeNode - Create a new debug type node. |
| 1772 | llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) { |
| 1773 | // Handle qualifiers, which recursively handles what they refer to. |
| 1774 | if (Ty.hasLocalQualifiers()) |
| 1775 | return CreateQualifiedType(Ty, Unit); |
| 1776 | |
| 1777 | const char *Diag = 0; |
| 1778 | |
| 1779 | // Work out details of type. |
| 1780 | switch (Ty->getTypeClass()) { |
| 1781 | #define TYPE(Class, Base) |
| 1782 | #define ABSTRACT_TYPE(Class, Base) |
| 1783 | #define NON_CANONICAL_TYPE(Class, Base) |
| 1784 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1785 | #include "clang/AST/TypeNodes.def" |
| 1786 | llvm_unreachable("Dependent types cannot show up in debug information"); |
| 1787 | |
| 1788 | case Type::ExtVector: |
| 1789 | case Type::Vector: |
| 1790 | return CreateType(cast<VectorType>(Ty), Unit); |
| 1791 | case Type::ObjCObjectPointer: |
| 1792 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
| 1793 | case Type::ObjCObject: |
| 1794 | return CreateType(cast<ObjCObjectType>(Ty), Unit); |
| 1795 | case Type::ObjCInterface: |
| 1796 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
| 1797 | case Type::Builtin: |
| 1798 | return CreateType(cast<BuiltinType>(Ty)); |
| 1799 | case Type::Complex: |
| 1800 | return CreateType(cast<ComplexType>(Ty)); |
| 1801 | case Type::Pointer: |
| 1802 | return CreateType(cast<PointerType>(Ty), Unit); |
| 1803 | case Type::BlockPointer: |
| 1804 | return CreateType(cast<BlockPointerType>(Ty), Unit); |
| 1805 | case Type::Typedef: |
| 1806 | return CreateType(cast<TypedefType>(Ty), Unit); |
| 1807 | case Type::Record: |
| 1808 | return CreateType(cast<RecordType>(Ty)); |
| 1809 | case Type::Enum: |
| 1810 | return CreateEnumType(cast<EnumType>(Ty)->getDecl()); |
| 1811 | case Type::FunctionProto: |
| 1812 | case Type::FunctionNoProto: |
| 1813 | return CreateType(cast<FunctionType>(Ty), Unit); |
| 1814 | case Type::ConstantArray: |
| 1815 | case Type::VariableArray: |
| 1816 | case Type::IncompleteArray: |
| 1817 | return CreateType(cast<ArrayType>(Ty), Unit); |
| 1818 | |
| 1819 | case Type::LValueReference: |
| 1820 | return CreateType(cast<LValueReferenceType>(Ty), Unit); |
| 1821 | case Type::RValueReference: |
| 1822 | return CreateType(cast<RValueReferenceType>(Ty), Unit); |
| 1823 | |
| 1824 | case Type::MemberPointer: |
| 1825 | return CreateType(cast<MemberPointerType>(Ty), Unit); |
| 1826 | |
| 1827 | case Type::Atomic: |
| 1828 | return CreateType(cast<AtomicType>(Ty), Unit); |
| 1829 | |
| 1830 | case Type::Attributed: |
| 1831 | case Type::TemplateSpecialization: |
| 1832 | case Type::Elaborated: |
| 1833 | case Type::Paren: |
| 1834 | case Type::SubstTemplateTypeParm: |
| 1835 | case Type::TypeOfExpr: |
| 1836 | case Type::TypeOf: |
| 1837 | case Type::Decltype: |
| 1838 | case Type::UnaryTransform: |
| 1839 | case Type::Auto: |
| 1840 | llvm_unreachable("type should have been unwrapped!"); |
| 1841 | } |
| 1842 | |
| 1843 | assert(Diag && "Fall through without a diagnostic?"); |
| 1844 | unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
| 1845 | "debug information for %0 is not yet supported"); |
| 1846 | CGM.getDiags().Report(DiagID) |
| 1847 | << Diag; |
| 1848 | return llvm::DIType(); |
| 1849 | } |
| 1850 | |
| 1851 | /// getOrCreateLimitedType - Get the type from the cache or create a new |
| 1852 | /// limited type if necessary. |
| 1853 | llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty, |
| 1854 | llvm::DIFile Unit) { |
| 1855 | if (Ty.isNull()) |
| 1856 | return llvm::DIType(); |
| 1857 | |
| 1858 | // Unwrap the type as needed for debug information. |
| 1859 | Ty = UnwrapTypeForDebugInfo(Ty); |
| 1860 | |
| 1861 | llvm::DIType T = getTypeOrNull(Ty); |
| 1862 | |
| 1863 | // We may have cached a forward decl when we could have created |
| 1864 | // a non-forward decl. Go ahead and create a non-forward decl |
| 1865 | // now. |
| 1866 | if (T.Verify() && !T.isForwardDecl()) return T; |
| 1867 | |
| 1868 | // Otherwise create the type. |
| 1869 | llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit); |
| 1870 | |
| 1871 | if (T.Verify() && T.isForwardDecl()) |
| 1872 | ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), |
| 1873 | static_cast<llvm::Value*>(T))); |
| 1874 | |
| 1875 | // And update the type cache. |
| 1876 | TypeCache[Ty.getAsOpaquePtr()] = Res; |
| 1877 | return Res; |
| 1878 | } |
| 1879 | |
| 1880 | // TODO: Currently used for context chains when limiting debug info. |
| 1881 | llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) { |
| 1882 | RecordDecl *RD = Ty->getDecl(); |
| 1883 | |
| 1884 | // Get overall information about the record type for the debug info. |
| 1885 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 1886 | unsigned Line = getLineNumber(RD->getLocation()); |
| 1887 | StringRef RDName = getClassName(RD); |
| 1888 | |
| 1889 | llvm::DIDescriptor RDContext; |
| 1890 | if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::LimitedDebugInfo) |
| 1891 | RDContext = createContextChain(cast<Decl>(RD->getDeclContext())); |
| 1892 | else |
| 1893 | RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext())); |
| 1894 | |
| 1895 | // If this is just a forward declaration, construct an appropriately |
| 1896 | // marked node and just return it. |
| 1897 | if (!RD->getDefinition()) |
| 1898 | return createRecordFwdDecl(RD, RDContext); |
| 1899 | |
| 1900 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1901 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 1902 | const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
| 1903 | llvm::TrackingVH<llvm::MDNode> RealDecl; |
| 1904 | |
| 1905 | if (RD->isUnion()) |
| 1906 | RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, |
| 1907 | Size, Align, 0, llvm::DIArray()); |
| 1908 | else if (RD->isClass()) { |
| 1909 | // FIXME: This could be a struct type giving a default visibility different |
| 1910 | // than C++ class type, but needs llvm metadata changes first. |
| 1911 | RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line, |
| 1912 | Size, Align, 0, 0, llvm::DIType(), |
| 1913 | llvm::DIArray(), llvm::DIType(), |
| 1914 | llvm::DIArray()); |
| 1915 | } else |
| 1916 | RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line, |
| 1917 | Size, Align, 0, llvm::DIArray()); |
| 1918 | |
| 1919 | RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl); |
| 1920 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl); |
| 1921 | |
| 1922 | if (CXXDecl) { |
| 1923 | // A class's primary base or the class itself contains the vtable. |
| 1924 | llvm::MDNode *ContainingType = NULL; |
| 1925 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 1926 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { |
| 1927 | // Seek non virtual primary base root. |
| 1928 | while (1) { |
| 1929 | const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); |
| 1930 | const CXXRecordDecl *PBT = BRL.getPrimaryBase(); |
| 1931 | if (PBT && !BRL.isPrimaryBaseVirtual()) |
| 1932 | PBase = PBT; |
| 1933 | else |
| 1934 | break; |
| 1935 | } |
| 1936 | ContainingType = |
| 1937 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit); |
| 1938 | } |
| 1939 | else if (CXXDecl->isDynamicClass()) |
| 1940 | ContainingType = RealDecl; |
| 1941 | |
| 1942 | RealDecl->replaceOperandWith(12, ContainingType); |
| 1943 | } |
| 1944 | return llvm::DIType(RealDecl); |
| 1945 | } |
| 1946 | |
| 1947 | /// CreateLimitedTypeNode - Create a new debug type node, but only forward |
| 1948 | /// declare composite types that haven't been processed yet. |
| 1949 | llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) { |
| 1950 | |
| 1951 | // Work out details of type. |
| 1952 | switch (Ty->getTypeClass()) { |
| 1953 | #define TYPE(Class, Base) |
| 1954 | #define ABSTRACT_TYPE(Class, Base) |
| 1955 | #define NON_CANONICAL_TYPE(Class, Base) |
| 1956 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1957 | #include "clang/AST/TypeNodes.def" |
| 1958 | llvm_unreachable("Dependent types cannot show up in debug information"); |
| 1959 | |
| 1960 | case Type::Record: |
| 1961 | return CreateLimitedType(cast<RecordType>(Ty)); |
| 1962 | default: |
| 1963 | return CreateTypeNode(Ty, Unit); |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | /// CreateMemberType - Create new member and increase Offset by FType's size. |
| 1968 | llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, |
| 1969 | StringRef Name, |
| 1970 | uint64_t *Offset) { |
| 1971 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 1972 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); |
| 1973 | unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); |
| 1974 | llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, |
| 1975 | FieldSize, FieldAlign, |
| 1976 | *Offset, 0, FieldTy); |
| 1977 | *Offset += FieldSize; |
| 1978 | return Ty; |
| 1979 | } |
| 1980 | |
| 1981 | /// getFunctionDeclaration - Return debug info descriptor to describe method |
| 1982 | /// declaration for the given method definition. |
| 1983 | llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) { |
| 1984 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1985 | if (!FD) return llvm::DISubprogram(); |
| 1986 | |
| 1987 | // Setup context. |
| 1988 | getContextDescriptor(cast<Decl>(D->getDeclContext())); |
| 1989 | |
| 1990 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 1991 | MI = SPCache.find(FD->getCanonicalDecl()); |
| 1992 | if (MI != SPCache.end()) { |
| 1993 | llvm::Value *V = MI->second; |
| 1994 | llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V)); |
| 1995 | if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition()) |
| 1996 | return SP; |
| 1997 | } |
| 1998 | |
| 1999 | for (FunctionDecl::redecl_iterator I = FD->redecls_begin(), |
| 2000 | E = FD->redecls_end(); I != E; ++I) { |
| 2001 | const FunctionDecl *NextFD = *I; |
| 2002 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 2003 | MI = SPCache.find(NextFD->getCanonicalDecl()); |
| 2004 | if (MI != SPCache.end()) { |
| 2005 | llvm::Value *V = MI->second; |
| 2006 | llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V)); |
| 2007 | if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition()) |
| 2008 | return SP; |
| 2009 | } |
| 2010 | } |
| 2011 | return llvm::DISubprogram(); |
| 2012 | } |
| 2013 | |
| 2014 | // getOrCreateFunctionType - Construct DIType. If it is a c++ method, include |
| 2015 | // implicit parameter "this". |
| 2016 | llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl *D, |
| 2017 | QualType FnType, |
| 2018 | llvm::DIFile F) { |
| 2019 | |
| 2020 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 2021 | return getOrCreateMethodType(Method, F); |
| 2022 | if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) { |
| 2023 | // Add "self" and "_cmd" |
| 2024 | SmallVector<llvm::Value *, 16> Elts; |
| 2025 | |
| 2026 | // First element is always return type. For 'void' functions it is NULL. |
| 2027 | Elts.push_back(getOrCreateType(OMethod->getResultType(), F)); |
| 2028 | // "self" pointer is always first argument. |
| 2029 | llvm::DIType SelfTy = getOrCreateType(OMethod->getSelfDecl()->getType(), F); |
| 2030 | Elts.push_back(DBuilder.createObjectPointerType(SelfTy)); |
| 2031 | // "_cmd" pointer is always second argument. |
| 2032 | llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F); |
| 2033 | Elts.push_back(DBuilder.createArtificialType(CmdTy)); |
| 2034 | // Get rest of the arguments. |
| 2035 | for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(), |
| 2036 | PE = OMethod->param_end(); PI != PE; ++PI) |
| 2037 | Elts.push_back(getOrCreateType((*PI)->getType(), F)); |
| 2038 | |
| 2039 | llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); |
| 2040 | return DBuilder.createSubroutineType(F, EltTypeArray); |
| 2041 | } |
| 2042 | return getOrCreateType(FnType, F); |
| 2043 | } |
| 2044 | |
| 2045 | /// EmitFunctionStart - Constructs the debug code for entering a function. |
| 2046 | void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, |
| 2047 | llvm::Function *Fn, |
| 2048 | CGBuilderTy &Builder) { |
| 2049 | |
| 2050 | StringRef Name; |
| 2051 | StringRef LinkageName; |
| 2052 | |
| 2053 | FnBeginRegionCount.push_back(LexicalBlockStack.size()); |
| 2054 | |
| 2055 | const Decl *D = GD.getDecl(); |
| 2056 | // Function may lack declaration in source code if it is created by Clang |
| 2057 | // CodeGen (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk). |
| 2058 | bool HasDecl = (D != 0); |
| 2059 | // Use the location of the declaration. |
| 2060 | SourceLocation Loc; |
| 2061 | if (HasDecl) |
| 2062 | Loc = D->getLocation(); |
| 2063 | |
| 2064 | unsigned Flags = 0; |
| 2065 | llvm::DIFile Unit = getOrCreateFile(Loc); |
| 2066 | llvm::DIDescriptor FDContext(Unit); |
| 2067 | llvm::DIArray TParamsArray; |
| 2068 | if (!HasDecl) { |
| 2069 | // Use llvm function name. |
| 2070 | Name = Fn->getName(); |
| 2071 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 2072 | // If there is a DISubprogram for this function available then use it. |
| 2073 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 2074 | FI = SPCache.find(FD->getCanonicalDecl()); |
| 2075 | if (FI != SPCache.end()) { |
| 2076 | llvm::Value *V = FI->second; |
| 2077 | llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V)); |
| 2078 | if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { |
| 2079 | llvm::MDNode *SPN = SP; |
| 2080 | LexicalBlockStack.push_back(SPN); |
| 2081 | RegionMap[D] = llvm::WeakVH(SP); |
| 2082 | return; |
| 2083 | } |
| 2084 | } |
| 2085 | Name = getFunctionName(FD); |
| 2086 | // Use mangled name as linkage name for c/c++ functions. |
| 2087 | if (FD->hasPrototype()) { |
| 2088 | LinkageName = CGM.getMangledName(GD); |
| 2089 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
| 2090 | } |
| 2091 | if (LinkageName == Name || |
| 2092 | CGM.getCodeGenOpts().getDebugInfo() <= CodeGenOptions::DebugLineTablesOnly) |
| 2093 | LinkageName = StringRef(); |
| 2094 | |
| 2095 | if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) { |
| 2096 | if (const NamespaceDecl *NSDecl = |
| 2097 | dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) |
| 2098 | FDContext = getOrCreateNameSpace(NSDecl); |
| 2099 | else if (const RecordDecl *RDecl = |
| 2100 | dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) |
| 2101 | FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext())); |
| 2102 | |
| 2103 | // Collect template parameters. |
| 2104 | TParamsArray = CollectFunctionTemplateParams(FD, Unit); |
| 2105 | } |
| 2106 | } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
| 2107 | Name = getObjCMethodName(OMD); |
| 2108 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
| 2109 | } else { |
| 2110 | // Use llvm function name. |
| 2111 | Name = Fn->getName(); |
| 2112 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
| 2113 | } |
| 2114 | if (!Name.empty() && Name[0] == '\01') |
| 2115 | Name = Name.substr(1); |
| 2116 | |
| 2117 | unsigned LineNo = getLineNumber(Loc); |
| 2118 | if (!HasDecl || D->isImplicit()) |
| 2119 | Flags |= llvm::DIDescriptor::FlagArtificial; |
| 2120 | |
| 2121 | llvm::DIType DIFnType; |
| 2122 | llvm::DISubprogram SPDecl; |
| 2123 | if (HasDecl && |
| 2124 | CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) { |
| 2125 | DIFnType = getOrCreateFunctionType(D, FnType, Unit); |
| 2126 | SPDecl = getFunctionDeclaration(D); |
| 2127 | } else { |
| 2128 | // Create fake but valid subroutine type. Otherwise |
| 2129 | // llvm::DISubprogram::Verify() would return false, and |
| 2130 | // subprogram DIE will miss DW_AT_decl_file and |
| 2131 | // DW_AT_decl_line fields. |
| 2132 | SmallVector<llvm::Value*, 16> Elts; |
| 2133 | llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); |
| 2134 | DIFnType = DBuilder.createSubroutineType(Unit, EltTypeArray); |
| 2135 | } |
| 2136 | llvm::DISubprogram SP; |
| 2137 | SP = DBuilder.createFunction(FDContext, Name, LinkageName, Unit, |
| 2138 | LineNo, DIFnType, |
| 2139 | Fn->hasInternalLinkage(), true/*definition*/, |
| 2140 | getLineNumber(CurLoc), Flags, |
| 2141 | CGM.getLangOpts().Optimize, |
| 2142 | Fn, TParamsArray, SPDecl); |
| 2143 | |
| 2144 | // Push function on region stack. |
| 2145 | llvm::MDNode *SPN = SP; |
| 2146 | LexicalBlockStack.push_back(SPN); |
| 2147 | if (HasDecl) |
| 2148 | RegionMap[D] = llvm::WeakVH(SP); |
| 2149 | } |
| 2150 | |
| 2151 | /// EmitLocation - Emit metadata to indicate a change in line/column |
| 2152 | /// information in the source file. |
| 2153 | void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) { |
| 2154 | |
| 2155 | // Update our current location |
| 2156 | setLocation(Loc); |
| 2157 | |
| 2158 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
| 2159 | |
| 2160 | // Don't bother if things are the same as last time. |
| 2161 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 2162 | if (CurLoc == PrevLoc || |
| 2163 | SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc)) |
| 2164 | // New Builder may not be in sync with CGDebugInfo. |
| 2165 | if (!Builder.getCurrentDebugLocation().isUnknown()) |
| 2166 | return; |
| 2167 | |
| 2168 | // Update last state. |
| 2169 | PrevLoc = CurLoc; |
| 2170 | |
| 2171 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
| 2172 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc), |
| 2173 | getColumnNumber(CurLoc), |
| 2174 | Scope)); |
| 2175 | } |
| 2176 | |
| 2177 | /// CreateLexicalBlock - Creates a new lexical block node and pushes it on |
| 2178 | /// the stack. |
| 2179 | void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { |
| 2180 | llvm::DIDescriptor D = |
| 2181 | DBuilder.createLexicalBlock(LexicalBlockStack.empty() ? |
| 2182 | llvm::DIDescriptor() : |
| 2183 | llvm::DIDescriptor(LexicalBlockStack.back()), |
| 2184 | getOrCreateFile(CurLoc), |
| 2185 | getLineNumber(CurLoc), |
| 2186 | getColumnNumber(CurLoc)); |
| 2187 | llvm::MDNode *DN = D; |
| 2188 | LexicalBlockStack.push_back(DN); |
| 2189 | } |
| 2190 | |
| 2191 | /// EmitLexicalBlockStart - Constructs the debug code for entering a declarative |
| 2192 | /// region - beginning of a DW_TAG_lexical_block. |
| 2193 | void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) { |
| 2194 | // Set our current location. |
| 2195 | setLocation(Loc); |
| 2196 | |
| 2197 | // Create a new lexical block and push it on the stack. |
| 2198 | CreateLexicalBlock(Loc); |
| 2199 | |
| 2200 | // Emit a line table change for the current location inside the new scope. |
| 2201 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc), |
| 2202 | getColumnNumber(Loc), |
| 2203 | LexicalBlockStack.back())); |
| 2204 | } |
| 2205 | |
| 2206 | /// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative |
| 2207 | /// region - end of a DW_TAG_lexical_block. |
| 2208 | void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) { |
| 2209 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 2210 | |
| 2211 | // Provide an entry in the line table for the end of the block. |
| 2212 | EmitLocation(Builder, Loc); |
| 2213 | |
| 2214 | LexicalBlockStack.pop_back(); |
| 2215 | } |
| 2216 | |
| 2217 | /// EmitFunctionEnd - Constructs the debug code for exiting a function. |
| 2218 | void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { |
| 2219 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 2220 | unsigned RCount = FnBeginRegionCount.back(); |
| 2221 | assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch"); |
| 2222 | |
| 2223 | // Pop all regions for this function. |
| 2224 | while (LexicalBlockStack.size() != RCount) |
| 2225 | EmitLexicalBlockEnd(Builder, CurLoc); |
| 2226 | FnBeginRegionCount.pop_back(); |
| 2227 | } |
| 2228 | |
| 2229 | // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. |
| 2230 | // See BuildByRefType. |
| 2231 | llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD, |
| 2232 | uint64_t *XOffset) { |
| 2233 | |
| 2234 | SmallVector<llvm::Value *, 5> EltTys; |
| 2235 | QualType FType; |
| 2236 | uint64_t FieldSize, FieldOffset; |
| 2237 | unsigned FieldAlign; |
| 2238 | |
| 2239 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
| 2240 | QualType Type = VD->getType(); |
| 2241 | |
| 2242 | FieldOffset = 0; |
| 2243 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 2244 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 2245 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); |
| 2246 | FType = CGM.getContext().IntTy; |
| 2247 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 2248 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); |
| 2249 | |
| 2250 | bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD); |
| 2251 | if (HasCopyAndDispose) { |
| 2252 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 2253 | EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", |
| 2254 | &FieldOffset)); |
| 2255 | EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", |
| 2256 | &FieldOffset)); |
| 2257 | } |
| 2258 | bool HasByrefExtendedLayout; |
| 2259 | Qualifiers::ObjCLifetime Lifetime; |
| 2260 | if (CGM.getContext().getByrefLifetime(Type, |
| 2261 | Lifetime, HasByrefExtendedLayout) |
| 2262 | && HasByrefExtendedLayout) |
| 2263 | EltTys.push_back(CreateMemberType(Unit, FType, |
| 2264 | "__byref_variable_layout", |
| 2265 | &FieldOffset)); |
| 2266 | |
| 2267 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 2268 | if (Align > CGM.getContext().toCharUnitsFromBits( |
| 2269 | CGM.getContext().getTargetInfo().getPointerAlign(0))) { |
| 2270 | CharUnits FieldOffsetInBytes |
| 2271 | = CGM.getContext().toCharUnitsFromBits(FieldOffset); |
| 2272 | CharUnits AlignedOffsetInBytes |
| 2273 | = FieldOffsetInBytes.RoundUpToAlignment(Align); |
| 2274 | CharUnits NumPaddingBytes |
| 2275 | = AlignedOffsetInBytes - FieldOffsetInBytes; |
| 2276 | |
| 2277 | if (NumPaddingBytes.isPositive()) { |
| 2278 | llvm::APInt pad(32, NumPaddingBytes.getQuantity()); |
| 2279 | FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, |
| 2280 | pad, ArrayType::Normal, 0); |
| 2281 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); |
| 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | FType = Type; |
| 2286 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 2287 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 2288 | FieldAlign = CGM.getContext().toBits(Align); |
| 2289 | |
| 2290 | *XOffset = FieldOffset; |
| 2291 | FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, |
| 2292 | 0, FieldSize, FieldAlign, |
| 2293 | FieldOffset, 0, FieldTy); |
| 2294 | EltTys.push_back(FieldTy); |
| 2295 | FieldOffset += FieldSize; |
| 2296 | |
| 2297 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
| 2298 | |
| 2299 | unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct; |
| 2300 | |
| 2301 | return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, |
| 2302 | Elements); |
| 2303 | } |
| 2304 | |
| 2305 | /// EmitDeclare - Emit local variable declaration debug info. |
| 2306 | void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, |
| 2307 | llvm::Value *Storage, |
| 2308 | unsigned ArgNo, CGBuilderTy &Builder) { |
| 2309 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2310 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 2311 | |
| 2312 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
| 2313 | llvm::DIType Ty; |
| 2314 | uint64_t XOffset = 0; |
| 2315 | if (VD->hasAttr<BlocksAttr>()) |
| 2316 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 2317 | else |
| 2318 | Ty = getOrCreateType(VD->getType(), Unit); |
| 2319 | |
| 2320 | // If there is no debug info for this type then do not emit debug info |
| 2321 | // for this variable. |
| 2322 | if (!Ty) |
| 2323 | return; |
| 2324 | |
| 2325 | if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) { |
| 2326 | // If Storage is an aggregate returned as 'sret' then let debugger know |
| 2327 | // about this. |
| 2328 | if (Arg->hasStructRetAttr()) |
| 2329 | Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty); |
| 2330 | else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) { |
| 2331 | // If an aggregate variable has non trivial destructor or non trivial copy |
| 2332 | // constructor than it is pass indirectly. Let debug info know about this |
| 2333 | // by using reference of the aggregate type as a argument type. |
| 2334 | if (Record->hasNonTrivialCopyConstructor() || |
| 2335 | !Record->hasTrivialDestructor()) |
| 2336 | Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty); |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | // Get location information. |
| 2341 | unsigned Line = getLineNumber(VD->getLocation()); |
| 2342 | unsigned Column = getColumnNumber(VD->getLocation()); |
| 2343 | unsigned Flags = 0; |
| 2344 | if (VD->isImplicit()) |
| 2345 | Flags |= llvm::DIDescriptor::FlagArtificial; |
| 2346 | // If this is the first argument and it is implicit then |
| 2347 | // give it an object pointer flag. |
| 2348 | // FIXME: There has to be a better way to do this, but for static |
| 2349 | // functions there won't be an implicit param at arg1 and |
| 2350 | // otherwise it is 'self' or 'this'. |
| 2351 | if (isa<ImplicitParamDecl>(VD) && ArgNo == 1) |
| 2352 | Flags |= llvm::DIDescriptor::FlagObjectPointer; |
| 2353 | |
| 2354 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
| 2355 | |
| 2356 | StringRef Name = VD->getName(); |
| 2357 | if (!Name.empty()) { |
| 2358 | if (VD->hasAttr<BlocksAttr>()) { |
| 2359 | CharUnits offset = CharUnits::fromQuantity(32); |
| 2360 | SmallVector<llvm::Value *, 9> addr; |
| 2361 | llvm::Type *Int64Ty = CGM.Int64Ty; |
| 2362 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
| 2363 | // offset of __forwarding field |
| 2364 | offset = CGM.getContext().toCharUnitsFromBits( |
| 2365 | CGM.getContext().getTargetInfo().getPointerWidth(0)); |
| 2366 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 2367 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); |
| 2368 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
| 2369 | // offset of x field |
| 2370 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
| 2371 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 2372 | |
| 2373 | // Create the descriptor for the variable. |
| 2374 | llvm::DIVariable D = |
| 2375 | DBuilder.createComplexVariable(Tag, |
| 2376 | llvm::DIDescriptor(Scope), |
| 2377 | VD->getName(), Unit, Line, Ty, |
| 2378 | addr, ArgNo); |
| 2379 | |
| 2380 | // Insert an llvm.dbg.declare into the current block. |
| 2381 | llvm::Instruction *Call = |
| 2382 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); |
| 2383 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
| 2384 | return; |
| 2385 | } else if (isa<VariableArrayType>(VD->getType())) { |
| 2386 | // These are "complex" variables in that they need an op_deref. |
| 2387 | // Create the descriptor for the variable. |
| 2388 | llvm::Value *Addr = llvm::ConstantInt::get(CGM.Int64Ty, |
| 2389 | llvm::DIBuilder::OpDeref); |
| 2390 | llvm::DIVariable D = |
| 2391 | DBuilder.createComplexVariable(Tag, |
| 2392 | llvm::DIDescriptor(Scope), |
| 2393 | Name, Unit, Line, Ty, |
| 2394 | Addr, ArgNo); |
| 2395 | |
| 2396 | // Insert an llvm.dbg.declare into the current block. |
| 2397 | llvm::Instruction *Call = |
| 2398 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); |
| 2399 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
| 2400 | return; |
| 2401 | } |
David Blaikie | 436653b | 2013-01-05 05:58:35 +0000 | [diff] [blame] | 2402 | } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) { |
| 2403 | // If VD is an anonymous union then Storage represents value for |
| 2404 | // all union fields. |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2405 | const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); |
David Blaikie | d8180cf | 2013-01-05 20:03:07 +0000 | [diff] [blame] | 2406 | if (RD->isUnion() && RD->isAnonymousStructOrUnion()) { |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2407 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 2408 | E = RD->field_end(); |
| 2409 | I != E; ++I) { |
| 2410 | FieldDecl *Field = *I; |
| 2411 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
| 2412 | StringRef FieldName = Field->getName(); |
| 2413 | |
| 2414 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 2415 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
| 2416 | continue; |
| 2417 | |
| 2418 | // Use VarDecl's Tag, Scope and Line number. |
| 2419 | llvm::DIVariable D = |
| 2420 | DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), |
| 2421 | FieldName, Unit, Line, FieldTy, |
| 2422 | CGM.getLangOpts().Optimize, Flags, |
| 2423 | ArgNo); |
| 2424 | |
| 2425 | // Insert an llvm.dbg.declare into the current block. |
| 2426 | llvm::Instruction *Call = |
| 2427 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); |
| 2428 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
| 2429 | } |
David Blaikie | d8180cf | 2013-01-05 20:03:07 +0000 | [diff] [blame] | 2430 | return; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2431 | } |
| 2432 | } |
David Blaikie | 436653b | 2013-01-05 05:58:35 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Create the descriptor for the variable. |
| 2435 | llvm::DIVariable D = |
| 2436 | DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), |
| 2437 | Name, Unit, Line, Ty, |
| 2438 | CGM.getLangOpts().Optimize, Flags, ArgNo); |
| 2439 | |
| 2440 | // Insert an llvm.dbg.declare into the current block. |
| 2441 | llvm::Instruction *Call = |
| 2442 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); |
| 2443 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, |
| 2447 | llvm::Value *Storage, |
| 2448 | CGBuilderTy &Builder) { |
| 2449 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2450 | EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder); |
| 2451 | } |
| 2452 | |
| 2453 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD, |
| 2454 | llvm::Value *Storage, |
| 2455 | CGBuilderTy &Builder, |
| 2456 | const CGBlockInfo &blockInfo) { |
| 2457 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2458 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 2459 | |
| 2460 | if (Builder.GetInsertBlock() == 0) |
| 2461 | return; |
| 2462 | |
| 2463 | bool isByRef = VD->hasAttr<BlocksAttr>(); |
| 2464 | |
| 2465 | uint64_t XOffset = 0; |
| 2466 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
| 2467 | llvm::DIType Ty; |
| 2468 | if (isByRef) |
| 2469 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 2470 | else |
| 2471 | Ty = getOrCreateType(VD->getType(), Unit); |
| 2472 | |
| 2473 | // Self is passed along as an implicit non-arg variable in a |
| 2474 | // block. Mark it as the object pointer. |
| 2475 | if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self") |
| 2476 | Ty = DBuilder.createObjectPointerType(Ty); |
| 2477 | |
| 2478 | // Get location information. |
| 2479 | unsigned Line = getLineNumber(VD->getLocation()); |
| 2480 | unsigned Column = getColumnNumber(VD->getLocation()); |
| 2481 | |
| 2482 | const llvm::DataLayout &target = CGM.getDataLayout(); |
| 2483 | |
| 2484 | CharUnits offset = CharUnits::fromQuantity( |
| 2485 | target.getStructLayout(blockInfo.StructureType) |
| 2486 | ->getElementOffset(blockInfo.getCapture(VD).getIndex())); |
| 2487 | |
| 2488 | SmallVector<llvm::Value *, 9> addr; |
| 2489 | llvm::Type *Int64Ty = CGM.Int64Ty; |
| 2490 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
| 2491 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 2492 | if (isByRef) { |
| 2493 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); |
| 2494 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
| 2495 | // offset of __forwarding field |
| 2496 | offset = CGM.getContext() |
| 2497 | .toCharUnitsFromBits(target.getPointerSizeInBits(0)); |
| 2498 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 2499 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); |
| 2500 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
| 2501 | // offset of x field |
| 2502 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
| 2503 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 2504 | } |
| 2505 | |
| 2506 | // Create the descriptor for the variable. |
| 2507 | llvm::DIVariable D = |
| 2508 | DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable, |
| 2509 | llvm::DIDescriptor(LexicalBlockStack.back()), |
| 2510 | VD->getName(), Unit, Line, Ty, addr); |
| 2511 | // Insert an llvm.dbg.declare into the current block. |
| 2512 | llvm::Instruction *Call = |
| 2513 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint()); |
| 2514 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, |
| 2515 | LexicalBlockStack.back())); |
| 2516 | } |
| 2517 | |
| 2518 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 2519 | /// variable declaration. |
| 2520 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, |
| 2521 | unsigned ArgNo, |
| 2522 | CGBuilderTy &Builder) { |
| 2523 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2524 | EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder); |
| 2525 | } |
| 2526 | |
| 2527 | namespace { |
| 2528 | struct BlockLayoutChunk { |
| 2529 | uint64_t OffsetInBits; |
| 2530 | const BlockDecl::Capture *Capture; |
| 2531 | }; |
| 2532 | bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) { |
| 2533 | return l.OffsetInBits < r.OffsetInBits; |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, |
| 2538 | llvm::Value *addr, |
| 2539 | CGBuilderTy &Builder) { |
| 2540 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2541 | ASTContext &C = CGM.getContext(); |
| 2542 | const BlockDecl *blockDecl = block.getBlockDecl(); |
| 2543 | |
| 2544 | // Collect some general information about the block's location. |
| 2545 | SourceLocation loc = blockDecl->getCaretLocation(); |
| 2546 | llvm::DIFile tunit = getOrCreateFile(loc); |
| 2547 | unsigned line = getLineNumber(loc); |
| 2548 | unsigned column = getColumnNumber(loc); |
| 2549 | |
| 2550 | // Build the debug-info type for the block literal. |
| 2551 | getContextDescriptor(cast<Decl>(blockDecl->getDeclContext())); |
| 2552 | |
| 2553 | const llvm::StructLayout *blockLayout = |
| 2554 | CGM.getDataLayout().getStructLayout(block.StructureType); |
| 2555 | |
| 2556 | SmallVector<llvm::Value*, 16> fields; |
| 2557 | fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public, |
| 2558 | blockLayout->getElementOffsetInBits(0), |
| 2559 | tunit, tunit)); |
| 2560 | fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public, |
| 2561 | blockLayout->getElementOffsetInBits(1), |
| 2562 | tunit, tunit)); |
| 2563 | fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public, |
| 2564 | blockLayout->getElementOffsetInBits(2), |
| 2565 | tunit, tunit)); |
| 2566 | fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public, |
| 2567 | blockLayout->getElementOffsetInBits(3), |
| 2568 | tunit, tunit)); |
| 2569 | fields.push_back(createFieldType("__descriptor", |
| 2570 | C.getPointerType(block.NeedsCopyDispose ? |
| 2571 | C.getBlockDescriptorExtendedType() : |
| 2572 | C.getBlockDescriptorType()), |
| 2573 | 0, loc, AS_public, |
| 2574 | blockLayout->getElementOffsetInBits(4), |
| 2575 | tunit, tunit)); |
| 2576 | |
| 2577 | // We want to sort the captures by offset, not because DWARF |
| 2578 | // requires this, but because we're paranoid about debuggers. |
| 2579 | SmallVector<BlockLayoutChunk, 8> chunks; |
| 2580 | |
| 2581 | // 'this' capture. |
| 2582 | if (blockDecl->capturesCXXThis()) { |
| 2583 | BlockLayoutChunk chunk; |
| 2584 | chunk.OffsetInBits = |
| 2585 | blockLayout->getElementOffsetInBits(block.CXXThisIndex); |
| 2586 | chunk.Capture = 0; |
| 2587 | chunks.push_back(chunk); |
| 2588 | } |
| 2589 | |
| 2590 | // Variable captures. |
| 2591 | for (BlockDecl::capture_const_iterator |
| 2592 | i = blockDecl->capture_begin(), e = blockDecl->capture_end(); |
| 2593 | i != e; ++i) { |
| 2594 | const BlockDecl::Capture &capture = *i; |
| 2595 | const VarDecl *variable = capture.getVariable(); |
| 2596 | const CGBlockInfo::Capture &captureInfo = block.getCapture(variable); |
| 2597 | |
| 2598 | // Ignore constant captures. |
| 2599 | if (captureInfo.isConstant()) |
| 2600 | continue; |
| 2601 | |
| 2602 | BlockLayoutChunk chunk; |
| 2603 | chunk.OffsetInBits = |
| 2604 | blockLayout->getElementOffsetInBits(captureInfo.getIndex()); |
| 2605 | chunk.Capture = &capture; |
| 2606 | chunks.push_back(chunk); |
| 2607 | } |
| 2608 | |
| 2609 | // Sort by offset. |
| 2610 | llvm::array_pod_sort(chunks.begin(), chunks.end()); |
| 2611 | |
| 2612 | for (SmallVectorImpl<BlockLayoutChunk>::iterator |
| 2613 | i = chunks.begin(), e = chunks.end(); i != e; ++i) { |
| 2614 | uint64_t offsetInBits = i->OffsetInBits; |
| 2615 | const BlockDecl::Capture *capture = i->Capture; |
| 2616 | |
| 2617 | // If we have a null capture, this must be the C++ 'this' capture. |
| 2618 | if (!capture) { |
| 2619 | const CXXMethodDecl *method = |
| 2620 | cast<CXXMethodDecl>(blockDecl->getNonClosureContext()); |
| 2621 | QualType type = method->getThisType(C); |
| 2622 | |
| 2623 | fields.push_back(createFieldType("this", type, 0, loc, AS_public, |
| 2624 | offsetInBits, tunit, tunit)); |
| 2625 | continue; |
| 2626 | } |
| 2627 | |
| 2628 | const VarDecl *variable = capture->getVariable(); |
| 2629 | StringRef name = variable->getName(); |
| 2630 | |
| 2631 | llvm::DIType fieldType; |
| 2632 | if (capture->isByRef()) { |
| 2633 | std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy); |
| 2634 | |
| 2635 | // FIXME: this creates a second copy of this type! |
| 2636 | uint64_t xoffset; |
| 2637 | fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset); |
| 2638 | fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first); |
| 2639 | fieldType = DBuilder.createMemberType(tunit, name, tunit, line, |
| 2640 | ptrInfo.first, ptrInfo.second, |
| 2641 | offsetInBits, 0, fieldType); |
| 2642 | } else { |
| 2643 | fieldType = createFieldType(name, variable->getType(), 0, |
| 2644 | loc, AS_public, offsetInBits, tunit, tunit); |
| 2645 | } |
| 2646 | fields.push_back(fieldType); |
| 2647 | } |
| 2648 | |
| 2649 | SmallString<36> typeName; |
| 2650 | llvm::raw_svector_ostream(typeName) |
| 2651 | << "__block_literal_" << CGM.getUniqueBlockCount(); |
| 2652 | |
| 2653 | llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields); |
| 2654 | |
| 2655 | llvm::DIType type = |
| 2656 | DBuilder.createStructType(tunit, typeName.str(), tunit, line, |
| 2657 | CGM.getContext().toBits(block.BlockSize), |
| 2658 | CGM.getContext().toBits(block.BlockAlign), |
| 2659 | 0, fieldsArray); |
| 2660 | type = DBuilder.createPointerType(type, CGM.PointerWidthInBits); |
| 2661 | |
| 2662 | // Get overall information about the block. |
| 2663 | unsigned flags = llvm::DIDescriptor::FlagArtificial; |
| 2664 | llvm::MDNode *scope = LexicalBlockStack.back(); |
| 2665 | StringRef name = ".block_descriptor"; |
| 2666 | |
| 2667 | // Create the descriptor for the parameter. |
| 2668 | llvm::DIVariable debugVar = |
| 2669 | DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable, |
| 2670 | llvm::DIDescriptor(scope), |
| 2671 | name, tunit, line, type, |
| 2672 | CGM.getLangOpts().Optimize, flags, |
| 2673 | cast<llvm::Argument>(addr)->getArgNo() + 1); |
| 2674 | |
| 2675 | // Insert an llvm.dbg.value into the current block. |
| 2676 | llvm::Instruction *declare = |
| 2677 | DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar, |
| 2678 | Builder.GetInsertBlock()); |
| 2679 | declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope)); |
| 2680 | } |
| 2681 | |
| 2682 | /// EmitGlobalVariable - Emit information about a global variable. |
| 2683 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
| 2684 | const VarDecl *D) { |
| 2685 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2686 | // Create global variable debug descriptor. |
| 2687 | llvm::DIFile Unit = getOrCreateFile(D->getLocation()); |
| 2688 | unsigned LineNo = getLineNumber(D->getLocation()); |
| 2689 | |
| 2690 | setLocation(D->getLocation()); |
| 2691 | |
| 2692 | QualType T = D->getType(); |
| 2693 | if (T->isIncompleteArrayType()) { |
| 2694 | |
| 2695 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 2696 | llvm::APInt ConstVal(32, 1); |
| 2697 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
| 2698 | |
| 2699 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
| 2700 | ArrayType::Normal, 0); |
| 2701 | } |
| 2702 | StringRef DeclName = D->getName(); |
| 2703 | StringRef LinkageName; |
| 2704 | if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()) |
| 2705 | && !isa<ObjCMethodDecl>(D->getDeclContext())) |
| 2706 | LinkageName = Var->getName(); |
| 2707 | if (LinkageName == DeclName) |
| 2708 | LinkageName = StringRef(); |
| 2709 | llvm::DIDescriptor DContext = |
| 2710 | getContextDescriptor(dyn_cast<Decl>(D->getDeclContext())); |
| 2711 | DBuilder.createStaticVariable(DContext, DeclName, LinkageName, |
| 2712 | Unit, LineNo, getOrCreateType(T, Unit), |
| 2713 | Var->hasInternalLinkage(), Var); |
| 2714 | } |
| 2715 | |
| 2716 | /// EmitGlobalVariable - Emit information about an objective-c interface. |
| 2717 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
| 2718 | ObjCInterfaceDecl *ID) { |
| 2719 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2720 | // Create global variable debug descriptor. |
| 2721 | llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); |
| 2722 | unsigned LineNo = getLineNumber(ID->getLocation()); |
| 2723 | |
| 2724 | StringRef Name = ID->getName(); |
| 2725 | |
| 2726 | QualType T = CGM.getContext().getObjCInterfaceType(ID); |
| 2727 | if (T->isIncompleteArrayType()) { |
| 2728 | |
| 2729 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 2730 | llvm::APInt ConstVal(32, 1); |
| 2731 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
| 2732 | |
| 2733 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
| 2734 | ArrayType::Normal, 0); |
| 2735 | } |
| 2736 | |
| 2737 | DBuilder.createGlobalVariable(Name, Unit, LineNo, |
| 2738 | getOrCreateType(T, Unit), |
| 2739 | Var->hasInternalLinkage(), Var); |
| 2740 | } |
| 2741 | |
| 2742 | /// EmitGlobalVariable - Emit global variable's debug info. |
| 2743 | void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, |
| 2744 | llvm::Constant *Init) { |
| 2745 | assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); |
| 2746 | // Create the descriptor for the variable. |
| 2747 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
| 2748 | StringRef Name = VD->getName(); |
| 2749 | llvm::DIType Ty = getOrCreateType(VD->getType(), Unit); |
| 2750 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) { |
| 2751 | const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext()); |
| 2752 | assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?"); |
| 2753 | Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit); |
| 2754 | } |
| 2755 | // Do not use DIGlobalVariable for enums. |
| 2756 | if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type) |
| 2757 | return; |
| 2758 | DBuilder.createStaticVariable(Unit, Name, Name, Unit, |
| 2759 | getLineNumber(VD->getLocation()), |
| 2760 | Ty, true, Init); |
| 2761 | } |
| 2762 | |
| 2763 | /// getOrCreateNamesSpace - Return namespace descriptor for the given |
| 2764 | /// namespace decl. |
| 2765 | llvm::DINameSpace |
| 2766 | CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) { |
| 2767 | llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = |
| 2768 | NameSpaceCache.find(NSDecl); |
| 2769 | if (I != NameSpaceCache.end()) |
| 2770 | return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); |
| 2771 | |
| 2772 | unsigned LineNo = getLineNumber(NSDecl->getLocation()); |
| 2773 | llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation()); |
| 2774 | llvm::DIDescriptor Context = |
| 2775 | getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext())); |
| 2776 | llvm::DINameSpace NS = |
| 2777 | DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo); |
| 2778 | NameSpaceCache[NSDecl] = llvm::WeakVH(NS); |
| 2779 | return NS; |
| 2780 | } |
| 2781 | |
| 2782 | void CGDebugInfo::finalize() { |
| 2783 | for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI |
| 2784 | = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) { |
| 2785 | llvm::DIType Ty, RepTy; |
| 2786 | // Verify that the debug info still exists. |
| 2787 | if (llvm::Value *V = VI->second) |
| 2788 | Ty = llvm::DIType(cast<llvm::MDNode>(V)); |
| 2789 | |
| 2790 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
| 2791 | TypeCache.find(VI->first); |
| 2792 | if (it != TypeCache.end()) { |
| 2793 | // Verify that the debug info still exists. |
| 2794 | if (llvm::Value *V = it->second) |
| 2795 | RepTy = llvm::DIType(cast<llvm::MDNode>(V)); |
| 2796 | } |
| 2797 | |
| 2798 | if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) { |
| 2799 | Ty.replaceAllUsesWith(RepTy); |
| 2800 | } |
| 2801 | } |
| 2802 | DBuilder.finalize(); |
| 2803 | } |