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