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