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