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