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