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