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