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