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