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