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