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