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