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