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