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