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