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