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