Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +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" |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 17 | #include "CGBlocks.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclFriend.h" |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 22 | #include "clang/AST/Expr.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
| 25 | #include "clang/Basic/FileManager.h" |
Mike Stump | 5a86217 | 2009-09-15 21:48:34 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Version.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/CodeGenOptions.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 28 | #include "llvm/Constants.h" |
| 29 | #include "llvm/DerivedTypes.h" |
| 30 | #include "llvm/Instructions.h" |
| 31 | #include "llvm/Intrinsics.h" |
| 32 | #include "llvm/Module.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
| 34 | #include "llvm/ADT/SmallVector.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Dwarf.h" |
Benjamin Kramer | bcbca75 | 2011-10-14 18:45:11 +0000 | [diff] [blame] | 36 | #include "llvm/Support/FileSystem.h" |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetData.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | using namespace clang::CodeGen; |
| 41 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 42 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 43 | : CGM(CGM), DBuilder(CGM.getModule()), |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 44 | BlockLiteralGenericSet(false) { |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 45 | CreateCompileUnit(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 48 | CGDebugInfo::~CGDebugInfo() { |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 49 | assert(LexicalBlockStack.empty() && |
| 50 | "Region stack mismatch, stack not empty!"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 53 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
Eric Christopher | 944542e | 2011-10-11 23:00:45 +0000 | [diff] [blame] | 54 | // If the new location isn't valid return. |
| 55 | if (!Loc.isValid()) return; |
| 56 | |
| 57 | CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 58 | |
| 59 | // If we've changed files in the middle of a lexical scope go ahead |
| 60 | // and create a new lexical scope with file node if it's different |
| 61 | // from the one in the scope. |
| 62 | if (LexicalBlockStack.empty()) return; |
| 63 | |
| 64 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 65 | PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); |
| 66 | PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc); |
| 67 | |
| 68 | if (PCLoc.isInvalid() || PPLoc.isInvalid() || |
| 69 | !strcmp(PPLoc.getFilename(), PCLoc.getFilename())) |
| 70 | return; |
| 71 | |
| 72 | llvm::MDNode *LB = LexicalBlockStack.back(); |
| 73 | llvm::DIScope Scope = llvm::DIScope(LB); |
| 74 | if (Scope.isLexicalBlockFile()) { |
| 75 | llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB); |
| 76 | llvm::DIDescriptor D |
| 77 | = DBuilder.createLexicalBlockFile(LBF.getScope(), |
| 78 | getOrCreateFile(CurLoc)); |
| 79 | llvm::MDNode *N = D; |
| 80 | LexicalBlockStack.pop_back(); |
| 81 | LexicalBlockStack.push_back(N); |
| 82 | } else if (Scope.isLexicalBlock()) { |
| 83 | llvm::DIDescriptor D |
| 84 | = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)); |
| 85 | llvm::MDNode *N = D; |
| 86 | LexicalBlockStack.pop_back(); |
| 87 | LexicalBlockStack.push_back(N); |
| 88 | } |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Devang Patel | 3358305 | 2010-01-28 23:15:27 +0000 | [diff] [blame] | 91 | /// getContextDescriptor - Get context info for the decl. |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 92 | llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) { |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 93 | if (!Context) |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 94 | return TheCU; |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 95 | |
| 96 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator |
| 97 | I = RegionMap.find(Context); |
| 98 | if (I != RegionMap.end()) |
Gabor Greif | 38c9b17 | 2010-09-18 13:00:17 +0000 | [diff] [blame] | 99 | return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second)); |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 100 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 101 | // Check namespace. |
| 102 | if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 103 | return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl)); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 104 | |
| 105 | if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) { |
| 106 | if (!RDecl->isDependentType()) { |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 107 | llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 108 | getOrCreateMainFile()); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 109 | return llvm::DIDescriptor(Ty); |
| 110 | } |
| 111 | } |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 112 | return TheCU; |
Devang Patel | 979ec2e | 2009-10-06 00:35:31 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 115 | /// getFunctionName - Get function name for the given FunctionDecl. If the |
| 116 | /// name is constructred on demand (e.g. C++ destructor) then the name |
| 117 | /// is stored on the side. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 118 | StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 119 | assert (FD && "Invalid FunctionDecl!"); |
| 120 | IdentifierInfo *FII = FD->getIdentifier(); |
| 121 | if (FII) |
| 122 | return FII->getName(); |
| 123 | |
| 124 | // Otherwise construct human readable name for debug info. |
| 125 | std::string NS = FD->getNameAsString(); |
| 126 | |
| 127 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 128 | char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); |
Benjamin Kramer | 1b627dc | 2010-01-23 18:16:07 +0000 | [diff] [blame] | 129 | memcpy(StrPtr, NS.data(), NS.length()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 130 | return StringRef(StrPtr, NS.length()); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 134 | llvm::SmallString<256> MethodName; |
| 135 | llvm::raw_svector_ostream OS(MethodName); |
| 136 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 137 | const DeclContext *DC = OMD->getDeclContext(); |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 138 | if (const ObjCImplementationDecl *OID = |
| 139 | dyn_cast<const ObjCImplementationDecl>(DC)) { |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 140 | OS << OID->getName(); |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 141 | } else if (const ObjCInterfaceDecl *OID = |
| 142 | dyn_cast<const ObjCInterfaceDecl>(DC)) { |
Fariborz Jahanian | 1a4c937 | 2010-10-18 17:51:06 +0000 | [diff] [blame] | 143 | OS << OID->getName(); |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 144 | } else if (const ObjCCategoryImplDecl *OCD = |
| 145 | dyn_cast<const ObjCCategoryImplDecl>(DC)){ |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 146 | OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' << |
| 147 | OCD->getIdentifier()->getNameStart() << ')'; |
| 148 | } |
| 149 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 150 | |
| 151 | char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell()); |
| 152 | memcpy(StrPtr, MethodName.begin(), OS.tell()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 153 | return StringRef(StrPtr, OS.tell()); |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Devang Patel | 1f15c19 | 2011-04-18 17:30:25 +0000 | [diff] [blame] | 156 | /// getSelectorName - Return selector name. This is used for debugging |
Devang Patel | 90c1eed | 2011-04-16 00:37:51 +0000 | [diff] [blame] | 157 | /// info. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 158 | StringRef CGDebugInfo::getSelectorName(Selector S) { |
Benjamin Kramer | 2b5cfbc | 2011-10-14 18:45:16 +0000 | [diff] [blame] | 159 | const std::string &SName = S.getAsString(); |
| 160 | char *StrPtr = DebugInfoNames.Allocate<char>(SName.size()); |
| 161 | memcpy(StrPtr, SName.data(), SName.size()); |
| 162 | return StringRef(StrPtr, SName.size()); |
Devang Patel | 90c1eed | 2011-04-16 00:37:51 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 165 | /// getClassName - Get class name including template argument list. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 166 | StringRef |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 167 | CGDebugInfo::getClassName(RecordDecl *RD) { |
| 168 | ClassTemplateSpecializationDecl *Spec |
| 169 | = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 170 | if (!Spec) |
| 171 | return RD->getName(); |
| 172 | |
| 173 | const TemplateArgument *Args; |
| 174 | unsigned NumArgs; |
| 175 | std::string Buffer; |
| 176 | if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { |
| 177 | const TemplateSpecializationType *TST = |
| 178 | cast<TemplateSpecializationType>(TAW->getType()); |
| 179 | Args = TST->getArgs(); |
| 180 | NumArgs = TST->getNumArgs(); |
| 181 | } else { |
| 182 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 183 | Args = TemplateArgs.data(); |
| 184 | NumArgs = TemplateArgs.size(); |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 185 | } |
| 186 | Buffer = RD->getIdentifier()->getNameStart(); |
| 187 | PrintingPolicy Policy(CGM.getLangOptions()); |
| 188 | Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, |
| 189 | NumArgs, |
| 190 | Policy); |
| 191 | |
| 192 | // Copy this name on the side and use its reference. |
| 193 | char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length()); |
| 194 | memcpy(StrPtr, Buffer.data(), Buffer.length()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 195 | return StringRef(StrPtr, Buffer.length()); |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 198 | /// getOrCreateFile - Get the file debug info descriptor for the input location. |
| 199 | llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 200 | if (!Loc.isValid()) |
| 201 | // If Location is not valid then use main input file. |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 202 | return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 203 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 204 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 205 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 207 | if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty()) |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 208 | // If the location is not valid then use main input file. |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 209 | return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 211 | // Cache the results. |
| 212 | const char *fname = PLoc.getFilename(); |
| 213 | llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = |
| 214 | DIFileCache.find(fname); |
| 215 | |
| 216 | if (it != DIFileCache.end()) { |
| 217 | // Verify that the information still exists. |
| 218 | if (&*it->second) |
| 219 | return llvm::DIFile(cast<llvm::MDNode>(it->second)); |
| 220 | } |
| 221 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 222 | llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname()); |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 223 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 224 | DIFileCache[fname] = F; |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 225 | return F; |
| 226 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 227 | } |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 228 | |
Devang Patel | 532105f | 2010-10-28 22:03:20 +0000 | [diff] [blame] | 229 | /// getOrCreateMainFile - Get the file info for main compile unit. |
| 230 | llvm::DIFile CGDebugInfo::getOrCreateMainFile() { |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 231 | return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); |
Devang Patel | 532105f | 2010-10-28 22:03:20 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 234 | /// getLineNumber - Get line number for the location. If location is invalid |
| 235 | /// then use current location. |
| 236 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
Benjamin Kramer | c95ff94 | 2011-10-14 18:45:06 +0000 | [diff] [blame] | 237 | assert((Loc.isValid() || CurLoc.isValid()) && "Invalid current location!"); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 238 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 239 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 240 | return PLoc.isValid()? PLoc.getLine() : 0; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /// getColumnNumber - Get column number for the location. If location is |
| 244 | /// invalid then use current location. |
| 245 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) { |
Benjamin Kramer | c95ff94 | 2011-10-14 18:45:06 +0000 | [diff] [blame] | 246 | assert((Loc.isValid() || CurLoc.isValid()) && "Invalid current location!"); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 247 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 248 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 249 | return PLoc.isValid()? PLoc.getColumn() : 0; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 252 | StringRef CGDebugInfo::getCurrentDirname() { |
Nick Lewycky | 7c4fd91 | 2011-10-21 02:32:14 +0000 | [diff] [blame] | 253 | if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) |
| 254 | return CGM.getCodeGenOpts().DebugCompilationDir; |
| 255 | |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 256 | if (!CWDName.empty()) |
| 257 | return CWDName; |
Benjamin Kramer | bcbca75 | 2011-10-14 18:45:11 +0000 | [diff] [blame] | 258 | llvm::SmallString<256> CWD; |
| 259 | llvm::sys::fs::current_path(CWD); |
| 260 | char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size()); |
| 261 | memcpy(CompDirnamePtr, CWD.data(), CWD.size()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 262 | return CWDName = StringRef(CompDirnamePtr, CWD.size()); |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 265 | /// CreateCompileUnit - Create new compile unit. |
| 266 | void CGDebugInfo::CreateCompileUnit() { |
| 267 | |
| 268 | // Get absolute path name. |
Douglas Gregor | ac91b4c | 2010-03-18 23:46:43 +0000 | [diff] [blame] | 269 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 270 | std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| 271 | if (MainFileName.empty()) |
Devang Patel | 22fe585 | 2010-03-12 21:04:27 +0000 | [diff] [blame] | 272 | MainFileName = "<unknown>"; |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 273 | |
Douglas Gregor | f6728fc | 2010-03-22 21:28:29 +0000 | [diff] [blame] | 274 | // The main file name provided via the "-main-file-name" option contains just |
| 275 | // the file name itself with no path information. This file name may have had |
| 276 | // a relative path, so we look into the actual file entry for the main |
| 277 | // file to determine the real absolute path for the file. |
Devang Patel | 6e6bc39 | 2010-07-23 23:04:28 +0000 | [diff] [blame] | 278 | std::string MainFileDir; |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 279 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 280 | MainFileDir = MainFile->getDir()->getName(); |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 281 | if (MainFileDir != ".") |
| 282 | MainFileName = MainFileDir + "/" + MainFileName; |
| 283 | } |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 284 | |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 285 | // Save filename string. |
| 286 | char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length()); |
| 287 | memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 288 | StringRef Filename(FilenamePtr, MainFileName.length()); |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 290 | unsigned LangTag; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 291 | const LangOptions &LO = CGM.getLangOptions(); |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 292 | if (LO.CPlusPlus) { |
| 293 | if (LO.ObjC1) |
| 294 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 295 | else |
| 296 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 297 | } else if (LO.ObjC1) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 298 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 299 | } else if (LO.C99) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 300 | LangTag = llvm::dwarf::DW_LANG_C99; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 301 | } else { |
| 302 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 303 | } |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 304 | |
Daniel Dunbar | 19f1983 | 2010-08-24 17:41:09 +0000 | [diff] [blame] | 305 | std::string Producer = getClangFullVersion(); |
Chris Lattner | 4c2577a | 2009-05-02 01:00:04 +0000 | [diff] [blame] | 306 | |
| 307 | // Figure out which version of the ObjC runtime we have. |
| 308 | unsigned RuntimeVers = 0; |
| 309 | if (LO.ObjC1) |
| 310 | RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 312 | // Create new compile unit. |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 313 | DBuilder.createCompileUnit( |
Devang Patel | 5811500 | 2010-07-27 20:49:59 +0000 | [diff] [blame] | 314 | LangTag, Filename, getCurrentDirname(), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 315 | Producer, |
Daniel Dunbar | f2d8b9f | 2009-12-18 02:43:17 +0000 | [diff] [blame] | 316 | LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 317 | // FIXME - Eliminate TheCU. |
| 318 | TheCU = llvm::DICompileUnit(DBuilder.getCU()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 321 | /// CreateType - Get the Basic type from the cache or create a new |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 322 | /// one if necessary. |
Devang Patel | f1d1d9a | 2010-11-01 16:52:40 +0000 | [diff] [blame] | 323 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 324 | unsigned Encoding = 0; |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 325 | const char *BTName = NULL; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 326 | switch (BT->getKind()) { |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 327 | #define BUILTIN_TYPE(Id, SingletonId) |
| 328 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 329 | case BuiltinType::Id: |
| 330 | #include "clang/AST/BuiltinTypes.def" |
Devang Patel | e7566cf | 2011-09-12 18:50:21 +0000 | [diff] [blame] | 331 | case BuiltinType::Dependent: |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 332 | llvm_unreachable("Unexpected builtin type"); |
Devang Patel | e7566cf | 2011-09-12 18:50:21 +0000 | [diff] [blame] | 333 | case BuiltinType::NullPtr: |
Devang Patel | f60dca3 | 2011-09-14 23:14:14 +0000 | [diff] [blame] | 334 | return DBuilder. |
| 335 | createNullPtrType(BT->getName(CGM.getContext().getLangOptions())); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 336 | case BuiltinType::Void: |
| 337 | return llvm::DIType(); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 338 | case BuiltinType::ObjCClass: |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 339 | return DBuilder.createStructType(TheCU, "objc_class", |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 340 | getOrCreateMainFile(), 0, 0, 0, |
| 341 | llvm::DIDescriptor::FlagFwdDecl, |
| 342 | llvm::DIArray()); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 343 | case BuiltinType::ObjCId: { |
| 344 | // typedef struct objc_class *Class; |
| 345 | // typedef struct objc_object { |
| 346 | // Class isa; |
| 347 | // } *id; |
| 348 | |
| 349 | llvm::DIType OCTy = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 350 | DBuilder.createStructType(TheCU, "objc_class", |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 351 | getOrCreateMainFile(), 0, 0, 0, |
| 352 | llvm::DIDescriptor::FlagFwdDecl, |
| 353 | llvm::DIArray()); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 354 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 355 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 356 | llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 358 | SmallVector<llvm::Value *, 16> EltTys; |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 359 | llvm::DIType FieldTy = |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 360 | DBuilder.createMemberType(getOrCreateMainFile(), "isa", |
| 361 | getOrCreateMainFile(), 0, Size, |
| 362 | 0, 0, 0, ISATy); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 363 | EltTys.push_back(FieldTy); |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 364 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 365 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 366 | return DBuilder.createStructType(TheCU, "objc_object", |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 367 | getOrCreateMainFile(), |
| 368 | 0, 0, 0, 0, Elements); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 369 | } |
Devang Patel | 6e108ce | 2011-02-09 03:15:05 +0000 | [diff] [blame] | 370 | case BuiltinType::ObjCSel: { |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 371 | return DBuilder.createStructType(TheCU, "objc_selector", |
Devang Patel | 6e108ce | 2011-02-09 03:15:05 +0000 | [diff] [blame] | 372 | getOrCreateMainFile(), 0, 0, 0, |
| 373 | llvm::DIDescriptor::FlagFwdDecl, |
| 374 | llvm::DIArray()); |
| 375 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 376 | case BuiltinType::UChar: |
| 377 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 378 | case BuiltinType::Char_S: |
| 379 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
Devang Patel | e8ee3f2 | 2011-09-12 17:11:58 +0000 | [diff] [blame] | 380 | case BuiltinType::Char16: |
| 381 | case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 382 | case BuiltinType::UShort: |
| 383 | case BuiltinType::UInt: |
Devang Patel | 31c79b4 | 2011-05-05 17:06:30 +0000 | [diff] [blame] | 384 | case BuiltinType::UInt128: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 385 | case BuiltinType::ULong: |
Devang Patel | 68f76b1 | 2011-09-10 00:44:49 +0000 | [diff] [blame] | 386 | case BuiltinType::WChar_U: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 387 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 388 | case BuiltinType::Short: |
| 389 | case BuiltinType::Int: |
Devang Patel | 31c79b4 | 2011-05-05 17:06:30 +0000 | [diff] [blame] | 390 | case BuiltinType::Int128: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 391 | case BuiltinType::Long: |
Devang Patel | 68f76b1 | 2011-09-10 00:44:49 +0000 | [diff] [blame] | 392 | case BuiltinType::WChar_S: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 393 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 394 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 395 | case BuiltinType::Half: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 396 | case BuiltinType::Float: |
Devang Patel | 7c173cb | 2009-10-12 22:28:31 +0000 | [diff] [blame] | 397 | case BuiltinType::LongDouble: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 398 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | } |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 400 | |
| 401 | switch (BT->getKind()) { |
| 402 | case BuiltinType::Long: BTName = "long int"; break; |
| 403 | case BuiltinType::LongLong: BTName = "long long int"; break; |
| 404 | case BuiltinType::ULong: BTName = "long unsigned int"; break; |
| 405 | case BuiltinType::ULongLong: BTName = "long long unsigned int"; break; |
| 406 | default: |
| 407 | BTName = BT->getName(CGM.getContext().getLangOptions()); |
| 408 | break; |
| 409 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 410 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 411 | uint64_t Size = CGM.getContext().getTypeSize(BT); |
| 412 | uint64_t Align = CGM.getContext().getTypeAlign(BT); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 413 | llvm::DIType DbgTy = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 414 | DBuilder.createBasicType(BTName, Size, Align, Encoding); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 415 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 416 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 417 | |
Devang Patel | 344ff5d | 2010-12-09 00:25:29 +0000 | [diff] [blame] | 418 | llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) { |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 419 | // Bit size, align and offset of the type. |
| 420 | unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; |
| 421 | if (Ty->isComplexIntegerType()) |
| 422 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 424 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 425 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 426 | llvm::DIType DbgTy = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 427 | DBuilder.createBasicType("complex", Size, Align, Encoding); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 428 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 429 | return DbgTy; |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 430 | } |
| 431 | |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 432 | /// CreateCVRType - Get the qualified type from the cache or create |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 433 | /// a new one if necessary. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 434 | llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 435 | QualifierCollector Qc; |
| 436 | const Type *T = Qc.strip(Ty); |
| 437 | |
| 438 | // Ignore these qualifiers for now. |
| 439 | Qc.removeObjCGCAttr(); |
| 440 | Qc.removeAddressSpace(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 441 | Qc.removeObjCLifetime(); |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 443 | // We will create one Derived type for one qualifier and recurse to handle any |
| 444 | // additional ones. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 445 | unsigned Tag; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 446 | if (Qc.hasConst()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 447 | Tag = llvm::dwarf::DW_TAG_const_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 448 | Qc.removeConst(); |
| 449 | } else if (Qc.hasVolatile()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 450 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 451 | Qc.removeVolatile(); |
| 452 | } else if (Qc.hasRestrict()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 453 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 454 | Qc.removeRestrict(); |
| 455 | } else { |
| 456 | assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| 457 | return getOrCreateType(QualType(T, 0), Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 458 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 460 | llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 461 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame] | 462 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 463 | // CVR derived types. |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 464 | llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 465 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 466 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 469 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 470 | llvm::DIFile Unit) { |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 471 | llvm::DIType DbgTy = |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 472 | CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 473 | Ty->getPointeeType(), Unit); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 474 | return DbgTy; |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 477 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 478 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 479 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 480 | Ty->getPointeeType(), Unit); |
| 481 | } |
| 482 | |
Eric Christopher | edc9592 | 2011-09-13 23:45:09 +0000 | [diff] [blame] | 483 | /// CreatePointeeType - Create Pointee type. If Pointee is a record |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 484 | /// then emit record's fwd if debug info size reduction is enabled. |
| 485 | llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy, |
| 486 | llvm::DIFile Unit) { |
| 487 | if (!CGM.getCodeGenOpts().LimitDebugInfo) |
| 488 | return getOrCreateType(PointeeTy, Unit); |
Devang Patel | 4142251 | 2011-10-24 23:15:17 +0000 | [diff] [blame] | 489 | |
| 490 | // Limit debug info for the pointee type. |
| 491 | |
| 492 | // Handle qualifiers. |
| 493 | if (PointeeTy.hasLocalQualifiers()) |
| 494 | return CreateQualifiedType(PointeeTy, Unit); |
| 495 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 496 | if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) { |
| 497 | RecordDecl *RD = RTy->getDecl(); |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 498 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 499 | unsigned Line = getLineNumber(RD->getLocation()); |
| 500 | llvm::DIDescriptor FDContext = |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 501 | getContextDescriptor(cast<Decl>(RD->getDeclContext())); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 502 | |
| 503 | if (RD->isStruct()) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 504 | return DBuilder.createStructType(FDContext, RD->getName(), DefUnit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 505 | Line, 0, 0, llvm::DIType::FlagFwdDecl, |
| 506 | llvm::DIArray()); |
| 507 | else if (RD->isUnion()) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 508 | return DBuilder.createUnionType(FDContext, RD->getName(), DefUnit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 509 | Line, 0, 0, llvm::DIType::FlagFwdDecl, |
| 510 | llvm::DIArray()); |
| 511 | else { |
| 512 | assert(RD->isClass() && "Unknown RecordType!"); |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 513 | return DBuilder.createClassType(FDContext, RD->getName(), DefUnit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 514 | Line, 0, 0, 0, llvm::DIType::FlagFwdDecl, |
| 515 | llvm::DIType(), llvm::DIArray()); |
| 516 | } |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 517 | } |
| 518 | return getOrCreateType(PointeeTy, Unit); |
| 519 | |
| 520 | } |
| 521 | |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 522 | llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, |
| 523 | const Type *Ty, |
| 524 | QualType PointeeTy, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 525 | llvm::DIFile Unit) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 526 | |
| 527 | if (Tag == llvm::dwarf::DW_TAG_reference_type) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 528 | return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit)); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 529 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 530 | // Bit size, align and offset of the type. |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 531 | // Size is always the size of a pointer. We can't use getTypeSize here |
| 532 | // because that does not return the correct value for references. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 533 | unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 534 | uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 535 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 537 | return |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 538 | DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit), Size, Align); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 541 | llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 542 | llvm::DIFile Unit) { |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 543 | if (BlockLiteralGenericSet) |
| 544 | return BlockLiteralGeneric; |
| 545 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 546 | SmallVector<llvm::Value *, 8> EltTys; |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 547 | llvm::DIType FieldTy; |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 548 | QualType FType; |
| 549 | uint64_t FieldSize, FieldOffset; |
| 550 | unsigned FieldAlign; |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 551 | llvm::DIArray Elements; |
| 552 | llvm::DIType EltTy, DescTy; |
| 553 | |
| 554 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 555 | FType = CGM.getContext().UnsignedLongTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 556 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| 557 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 558 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 559 | Elements = DBuilder.getOrCreateArray(EltTys); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 560 | EltTys.clear(); |
| 561 | |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 562 | unsigned Flags = llvm::DIDescriptor::FlagAppleBlock; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 563 | unsigned LineNo = getLineNumber(CurLoc); |
Mike Stump | 3d363c5 | 2009-10-02 02:30:50 +0000 | [diff] [blame] | 564 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 565 | EltTy = DBuilder.createStructType(Unit, "__block_descriptor", |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 566 | Unit, LineNo, FieldOffset, 0, |
| 567 | Flags, Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 569 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 570 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 572 | DescTy = DBuilder.createPointerType(EltTy, Size); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 573 | |
| 574 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 575 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 576 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 577 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 578 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 579 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
Benjamin Kramer | d3651cc | 2010-04-24 20:26:20 +0000 | [diff] [blame] | 580 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 581 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 582 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 583 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 584 | FieldTy = DescTy; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 585 | FieldSize = CGM.getContext().getTypeSize(Ty); |
| 586 | FieldAlign = CGM.getContext().getTypeAlign(Ty); |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 587 | FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 588 | LineNo, FieldSize, FieldAlign, |
| 589 | FieldOffset, 0, FieldTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 590 | EltTys.push_back(FieldTy); |
| 591 | |
| 592 | FieldOffset += FieldSize; |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 593 | Elements = DBuilder.getOrCreateArray(EltTys); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 594 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 595 | EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 596 | Unit, LineNo, FieldOffset, 0, |
| 597 | Flags, Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 599 | BlockLiteralGenericSet = true; |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 600 | BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 601 | return BlockLiteralGeneric; |
| 602 | } |
| 603 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 604 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 605 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 606 | // Typedefs are derived from some other type. If we have a typedef of a |
| 607 | // typedef, make sure to emit the whole chain. |
| 608 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 609 | if (!Src.Verify()) |
| 610 | return llvm::DIType(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 611 | // We don't set size information, but do specify where the typedef was |
| 612 | // declared. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 613 | unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); |
Devang Patel | c490312 | 2011-06-03 17:23:47 +0000 | [diff] [blame] | 614 | const TypedefNameDecl *TyDecl = Ty->getDecl(); |
| 615 | llvm::DIDescriptor TydefContext = |
| 616 | getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())); |
| 617 | |
| 618 | return |
| 619 | DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TydefContext); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 622 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 623 | llvm::DIFile Unit) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 624 | SmallVector<llvm::Value *, 16> EltTys; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 626 | // Add the result type at least. |
| 627 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 629 | // Set up remainder of arguments if there is a prototype. |
| 630 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
Devang Patel | af164bb | 2010-10-06 20:51:45 +0000 | [diff] [blame] | 631 | if (isa<FunctionNoProtoType>(Ty)) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 632 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
Devang Patel | af164bb | 2010-10-06 20:51:45 +0000 | [diff] [blame] | 633 | else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 634 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 635 | EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 638 | llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 640 | llvm::DIType DbgTy = DBuilder.createSubroutineType(Unit, EltTypeArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 641 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 644 | llvm::DIType CGDebugInfo::createFieldType(StringRef name, |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 645 | QualType type, |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 646 | uint64_t sizeInBitsOverride, |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 647 | SourceLocation loc, |
| 648 | AccessSpecifier AS, |
| 649 | uint64_t offsetInBits, |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 650 | llvm::DIFile tunit, |
| 651 | llvm::DIDescriptor scope) { |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 652 | llvm::DIType debugType = getOrCreateType(type, tunit); |
| 653 | |
| 654 | // Get the location for the field. |
| 655 | llvm::DIFile file = getOrCreateFile(loc); |
| 656 | unsigned line = getLineNumber(loc); |
| 657 | |
| 658 | uint64_t sizeInBits = 0; |
| 659 | unsigned alignInBits = 0; |
| 660 | if (!type->isIncompleteArrayType()) { |
| 661 | llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type); |
| 662 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 663 | if (sizeInBitsOverride) |
| 664 | sizeInBits = sizeInBitsOverride; |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | unsigned flags = 0; |
| 668 | if (AS == clang::AS_private) |
| 669 | flags |= llvm::DIDescriptor::FlagPrivate; |
| 670 | else if (AS == clang::AS_protected) |
| 671 | flags |= llvm::DIDescriptor::FlagProtected; |
| 672 | |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 673 | return DBuilder.createMemberType(scope, name, file, line, sizeInBits, |
| 674 | alignInBits, offsetInBits, flags, debugType); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 677 | /// CollectRecordFields - A helper function to collect debug info for |
| 678 | /// record fields. This is used while creating debug info entry for a Record. |
| 679 | void CGDebugInfo:: |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 680 | CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 681 | SmallVectorImpl<llvm::Value *> &elements, |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 682 | llvm::DIType RecordTy) { |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 683 | unsigned fieldNo = 0; |
Fariborz Jahanian | fbc3cc6 | 2011-04-28 23:43:23 +0000 | [diff] [blame] | 684 | const FieldDecl *LastFD = 0; |
| 685 | bool IsMsStruct = record->hasAttr<MsStructAttr>(); |
| 686 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 687 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); |
| 688 | for (RecordDecl::field_iterator I = record->field_begin(), |
| 689 | E = record->field_end(); |
| 690 | I != E; ++I, ++fieldNo) { |
| 691 | FieldDecl *field = *I; |
Fariborz Jahanian | fbc3cc6 | 2011-04-28 23:43:23 +0000 | [diff] [blame] | 692 | if (IsMsStruct) { |
| 693 | // Zero-length bitfields following non-bitfield members are ignored |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 694 | if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) { |
Fariborz Jahanian | fbc3cc6 | 2011-04-28 23:43:23 +0000 | [diff] [blame] | 695 | --fieldNo; |
| 696 | continue; |
| 697 | } |
| 698 | LastFD = field; |
| 699 | } |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 701 | StringRef name = field->getName(); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 702 | QualType type = field->getType(); |
| 703 | |
| 704 | // Ignore unnamed fields unless they're anonymous structs/unions. |
Fariborz Jahanian | fbc3cc6 | 2011-04-28 23:43:23 +0000 | [diff] [blame] | 705 | if (name.empty() && !type->isRecordType()) { |
| 706 | LastFD = field; |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 707 | continue; |
Fariborz Jahanian | fbc3cc6 | 2011-04-28 23:43:23 +0000 | [diff] [blame] | 708 | } |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 709 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 710 | uint64_t SizeInBitsOverride = 0; |
| 711 | if (field->isBitField()) { |
| 712 | SizeInBitsOverride = field->getBitWidthValue(CGM.getContext()); |
| 713 | assert(SizeInBitsOverride && "found named 0-width bitfield"); |
| 714 | } |
| 715 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 716 | llvm::DIType fieldType |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 717 | = createFieldType(name, type, SizeInBitsOverride, |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 718 | field->getLocation(), field->getAccess(), |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 719 | layout.getFieldOffset(fieldNo), tunit, RecordTy); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 720 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 721 | elements.push_back(fieldType); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 725 | /// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This |
| 726 | /// function type is not updated to include implicit "this" pointer. Use this |
| 727 | /// routine to get a method type which includes "this" pointer. |
| 728 | llvm::DIType |
| 729 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 730 | llvm::DIFile Unit) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 731 | llvm::DIType FnTy |
| 732 | = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(), |
| 733 | 0), |
| 734 | Unit); |
Devang Patel | d774d1e | 2010-01-28 21:43:50 +0000 | [diff] [blame] | 735 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 736 | // Add "this" pointer. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 737 | llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray(); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 738 | assert (Args.getNumElements() && "Invalid number of arguments!"); |
| 739 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 740 | SmallVector<llvm::Value *, 16> Elts; |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 741 | |
| 742 | // First element is always return type. For 'void' functions it is NULL. |
| 743 | Elts.push_back(Args.getElement(0)); |
| 744 | |
Eric Christopher | 2121cda | 2011-09-14 01:10:50 +0000 | [diff] [blame] | 745 | if (!Method->isStatic()) { |
| 746 | // "this" pointer is always first argument. |
| 747 | QualType ThisPtr = Method->getThisType(CGM.getContext()); |
Devang Patel | ef8857d | 2011-10-28 21:12:13 +0000 | [diff] [blame] | 748 | |
| 749 | const CXXRecordDecl *RD = Method->getParent(); |
| 750 | if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| 751 | // Create pointer type directly in this case. |
| 752 | const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); |
| 753 | QualType PointeeTy = ThisPtrTy->getPointeeType(); |
| 754 | unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
| 755 | uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS); |
| 756 | uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy); |
| 757 | llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit); |
| 758 | llvm::DIType ThisPtrType = |
| 759 | DBuilder.createArtificialType |
| 760 | (DBuilder.createPointerType(PointeeType, Size, Align)); |
| 761 | TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; |
| 762 | Elts.push_back(ThisPtrType); |
| 763 | } else { |
| 764 | llvm::DIType ThisPtrType = |
| 765 | DBuilder.createArtificialType(getOrCreateType(ThisPtr, Unit)); |
| 766 | TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; |
| 767 | Elts.push_back(ThisPtrType); |
| 768 | } |
Eric Christopher | 2121cda | 2011-09-14 01:10:50 +0000 | [diff] [blame] | 769 | } |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 770 | |
| 771 | // Copy rest of the arguments. |
| 772 | for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) |
| 773 | Elts.push_back(Args.getElement(i)); |
| 774 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 775 | llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 776 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 777 | return DBuilder.createSubroutineType(Unit, EltTypeArray); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 780 | /// isFunctionLocalClass - Return true if CXXRecordDecl is defined |
| 781 | /// inside a function. |
| 782 | static bool isFunctionLocalClass(const CXXRecordDecl *RD) { |
| 783 | if (const CXXRecordDecl *NRD = |
| 784 | dyn_cast<CXXRecordDecl>(RD->getDeclContext())) |
| 785 | return isFunctionLocalClass(NRD); |
| 786 | else if (isa<FunctionDecl>(RD->getDeclContext())) |
| 787 | return true; |
| 788 | return false; |
| 789 | |
| 790 | } |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 791 | /// CreateCXXMemberFunction - A helper function to create a DISubprogram for |
| 792 | /// a single member function GlobalDecl. |
| 793 | llvm::DISubprogram |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 794 | CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 795 | llvm::DIFile Unit, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 796 | llvm::DIType RecordTy) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 797 | bool IsCtorOrDtor = |
| 798 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
| 799 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 800 | StringRef MethodName = getFunctionName(Method); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 801 | llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 802 | |
| 803 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| 804 | // make sense to give a single ctor/dtor a linkage name. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 805 | StringRef MethodLinkageName; |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 806 | if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 807 | MethodLinkageName = CGM.getMangledName(Method); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 808 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 809 | // Get the location for the method. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 810 | llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| 811 | unsigned MethodLine = getLineNumber(Method->getLocation()); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 812 | |
| 813 | // Collect virtual method info. |
| 814 | llvm::DIType ContainingType; |
| 815 | unsigned Virtuality = 0; |
| 816 | unsigned VIndex = 0; |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 817 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 818 | if (Method->isVirtual()) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 819 | if (Method->isPure()) |
| 820 | Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| 821 | else |
| 822 | Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
| 823 | |
| 824 | // It doesn't make sense to give a virtual destructor a vtable index, |
| 825 | // since a single destructor has two entries in the vtable. |
| 826 | if (!isa<CXXDestructorDecl>(Method)) |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 827 | VIndex = CGM.getVTableContext().getMethodVTableIndex(Method); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 828 | ContainingType = RecordTy; |
| 829 | } |
| 830 | |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 831 | unsigned Flags = 0; |
| 832 | if (Method->isImplicit()) |
| 833 | Flags |= llvm::DIDescriptor::FlagArtificial; |
Devang Patel | 10a7a6a | 2010-09-29 21:46:16 +0000 | [diff] [blame] | 834 | AccessSpecifier Access = Method->getAccess(); |
| 835 | if (Access == clang::AS_private) |
| 836 | Flags |= llvm::DIDescriptor::FlagPrivate; |
| 837 | else if (Access == clang::AS_protected) |
| 838 | Flags |= llvm::DIDescriptor::FlagProtected; |
Devang Patel | d78a019 | 2010-10-01 23:32:17 +0000 | [diff] [blame] | 839 | if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { |
| 840 | if (CXXC->isExplicit()) |
| 841 | Flags |= llvm::DIDescriptor::FlagExplicit; |
| 842 | } else if (const CXXConversionDecl *CXXC = |
| 843 | dyn_cast<CXXConversionDecl>(Method)) { |
| 844 | if (CXXC->isExplicit()) |
| 845 | Flags |= llvm::DIDescriptor::FlagExplicit; |
| 846 | } |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 847 | if (Method->hasPrototype()) |
| 848 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | d78a019 | 2010-10-01 23:32:17 +0000 | [diff] [blame] | 849 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 850 | llvm::DISubprogram SP = |
Nick Lewycky | 7803ec8 | 2011-09-01 21:49:51 +0000 | [diff] [blame] | 851 | DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 852 | MethodDefUnit, MethodLine, |
| 853 | MethodTy, /*isLocalToUnit=*/false, |
| 854 | /* isDefinition=*/ false, |
| 855 | Virtuality, VIndex, ContainingType, |
| 856 | Flags, CGM.getLangOptions().Optimize); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 857 | |
Devang Patel | 22a5cdf | 2011-04-29 23:42:32 +0000 | [diff] [blame] | 858 | SPCache[Method] = llvm::WeakVH(SP); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 859 | |
| 860 | return SP; |
| 861 | } |
| 862 | |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 863 | /// CollectCXXMemberFunctions - A helper function to collect debug info for |
| 864 | /// C++ member functions.This is used while creating debug info entry for |
| 865 | /// a Record. |
| 866 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 867 | CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 868 | SmallVectorImpl<llvm::Value *> &EltTys, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 869 | llvm::DIType RecordTy) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 870 | for(CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 871 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 872 | const CXXMethodDecl *Method = *I; |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 873 | |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 874 | if (Method->isImplicit() && !Method->isUsed()) |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 875 | continue; |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 876 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 877 | EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 881 | /// CollectCXXFriends - A helper function to collect debug info for |
| 882 | /// C++ base classes. This is used while creating debug info entry for |
| 883 | /// a Record. |
| 884 | void CGDebugInfo:: |
| 885 | CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 886 | SmallVectorImpl<llvm::Value *> &EltTys, |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 887 | llvm::DIType RecordTy) { |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 888 | for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(), |
| 889 | BE = RD->friend_end(); BI != BE; ++BI) { |
Nick Lewycky | 7803ec8 | 2011-09-01 21:49:51 +0000 | [diff] [blame] | 890 | if ((*BI)->isUnsupportedFriend()) |
| 891 | continue; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 892 | if (TypeSourceInfo *TInfo = (*BI)->getFriendType()) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 893 | EltTys.push_back(DBuilder.createFriend(RecordTy, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 894 | getOrCreateType(TInfo->getType(), |
| 895 | Unit))); |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 899 | /// CollectCXXBases - A helper function to collect debug info for |
| 900 | /// C++ base classes. This is used while creating debug info entry for |
| 901 | /// a Record. |
| 902 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 903 | CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 904 | SmallVectorImpl<llvm::Value *> &EltTys, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 905 | llvm::DIType RecordTy) { |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 906 | |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 907 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 908 | for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), |
| 909 | BE = RD->bases_end(); BI != BE; ++BI) { |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 910 | unsigned BFlags = 0; |
Devang Patel | 62c117d | 2011-04-04 20:36:06 +0000 | [diff] [blame] | 911 | uint64_t BaseOffset; |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 912 | |
| 913 | const CXXRecordDecl *Base = |
| 914 | cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); |
| 915 | |
| 916 | if (BI->isVirtual()) { |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 917 | // virtual base offset offset is -ve. The code generator emits dwarf |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 918 | // expression where it expects +ve number. |
Ken Dyck | 14c65ca | 2011-04-07 12:37:09 +0000 | [diff] [blame] | 919 | BaseOffset = |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 920 | 0 - CGM.getVTableContext() |
| 921 | .getVirtualBaseOffsetOffset(RD, Base).getQuantity(); |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 922 | BFlags = llvm::DIDescriptor::FlagVirtual; |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 923 | } else |
Devang Patel | 62c117d | 2011-04-04 20:36:06 +0000 | [diff] [blame] | 924 | BaseOffset = RL.getBaseClassOffsetInBits(Base); |
Ken Dyck | 14c65ca | 2011-04-07 12:37:09 +0000 | [diff] [blame] | 925 | // FIXME: Inconsistent units for BaseOffset. It is in bytes when |
| 926 | // BI->isVirtual() and bits when not. |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 927 | |
| 928 | AccessSpecifier Access = BI->getAccessSpecifier(); |
| 929 | if (Access == clang::AS_private) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 930 | BFlags |= llvm::DIDescriptor::FlagPrivate; |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 931 | else if (Access == clang::AS_protected) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 932 | BFlags |= llvm::DIDescriptor::FlagProtected; |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 933 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 934 | llvm::DIType DTy = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 935 | DBuilder.createInheritance(RecordTy, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 936 | getOrCreateType(BI->getType(), Unit), |
Devang Patel | 62c117d | 2011-04-04 20:36:06 +0000 | [diff] [blame] | 937 | BaseOffset, BFlags); |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 938 | EltTys.push_back(DTy); |
| 939 | } |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 942 | /// CollectTemplateParams - A helper function to collect template parameters. |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 943 | llvm::DIArray CGDebugInfo:: |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 944 | CollectTemplateParams(const TemplateParameterList *TPList, |
| 945 | const TemplateArgumentList &TAList, |
| 946 | llvm::DIFile Unit) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 947 | SmallVector<llvm::Value *, 16> TemplateParams; |
Devang Patel | c5ce297 | 2011-04-05 20:15:06 +0000 | [diff] [blame] | 948 | for (unsigned i = 0, e = TAList.size(); i != e; ++i) { |
| 949 | const TemplateArgument &TA = TAList[i]; |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 950 | const NamedDecl *ND = TPList->getParam(i); |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 951 | if (TA.getKind() == TemplateArgument::Type) { |
| 952 | llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit); |
| 953 | llvm::DITemplateTypeParameter TTP = |
Devang Patel | c5ce297 | 2011-04-05 20:15:06 +0000 | [diff] [blame] | 954 | DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy); |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 955 | TemplateParams.push_back(TTP); |
| 956 | } else if (TA.getKind() == TemplateArgument::Integral) { |
| 957 | llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit); |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 958 | llvm::DITemplateValueParameter TVP = |
Devang Patel | c5ce297 | 2011-04-05 20:15:06 +0000 | [diff] [blame] | 959 | DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy, |
| 960 | TA.getAsIntegral()->getZExtValue()); |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 961 | TemplateParams.push_back(TVP); |
| 962 | } |
| 963 | } |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 964 | return DBuilder.getOrCreateArray(TemplateParams); |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 967 | /// CollectFunctionTemplateParams - A helper function to collect debug |
| 968 | /// info for function template parameters. |
| 969 | llvm::DIArray CGDebugInfo:: |
| 970 | CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) { |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 971 | if (FD->getTemplatedKind() == |
| 972 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 973 | const TemplateParameterList *TList = |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 974 | FD->getTemplateSpecializationInfo()->getTemplate() |
| 975 | ->getTemplateParameters(); |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 976 | return |
| 977 | CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit); |
| 978 | } |
| 979 | return llvm::DIArray(); |
| 980 | } |
| 981 | |
| 982 | /// CollectCXXTemplateParams - A helper function to collect debug info for |
| 983 | /// template parameters. |
| 984 | llvm::DIArray CGDebugInfo:: |
| 985 | CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial, |
| 986 | llvm::DIFile Unit) { |
| 987 | llvm::PointerUnion<ClassTemplateDecl *, |
| 988 | ClassTemplatePartialSpecializationDecl *> |
| 989 | PU = TSpecial->getSpecializedTemplateOrPartial(); |
| 990 | |
| 991 | TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ? |
| 992 | PU.get<ClassTemplateDecl *>()->getTemplateParameters() : |
| 993 | PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters(); |
| 994 | const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs(); |
| 995 | return CollectTemplateParams(TPList, TAList, Unit); |
| 996 | } |
| 997 | |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 998 | /// getOrCreateVTablePtrType - Return debug info descriptor for vtable. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 999 | llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { |
Devang Patel | 0804e6e | 2010-03-08 20:53:17 +0000 | [diff] [blame] | 1000 | if (VTablePtrType.isValid()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1001 | return VTablePtrType; |
| 1002 | |
| 1003 | ASTContext &Context = CGM.getContext(); |
| 1004 | |
| 1005 | /* Function type */ |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1006 | llvm::Value *STy = getOrCreateType(Context.IntTy, Unit); |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1007 | llvm::DIArray SElements = DBuilder.getOrCreateArray(STy); |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1008 | llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1009 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1010 | llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1011 | "__vtbl_ptr_type"); |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1012 | VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1013 | return VTablePtrType; |
| 1014 | } |
| 1015 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1016 | /// getVTableName - Get vtable name for the given Class. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1017 | StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1018 | // Otherwise construct gdb compatible name name. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1019 | std::string Name = "_vptr$" + RD->getNameAsString(); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1020 | |
| 1021 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 1022 | char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1023 | memcpy(StrPtr, Name.data(), Name.length()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1024 | return StringRef(StrPtr, Name.length()); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1028 | /// CollectVTableInfo - If the C++ class has vtable info then insert appropriate |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1029 | /// debug info entry in EltTys vector. |
| 1030 | void CGDebugInfo:: |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1031 | CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1032 | SmallVectorImpl<llvm::Value *> &EltTys) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1033 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1034 | |
| 1035 | // If there is a primary base then it will hold vtable info. |
| 1036 | if (RL.getPrimaryBase()) |
| 1037 | return; |
| 1038 | |
| 1039 | // If this class is not dynamic then there is not any vtable info to collect. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1040 | if (!RD->isDynamicClass()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | |
| 1043 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 1044 | llvm::DIType VPTR |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 1045 | = DBuilder.createMemberType(Unit, getVTableName(RD), Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1046 | 0, Size, 0, 0, 0, |
| 1047 | getOrCreateVTablePtrType(Unit)); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1048 | EltTys.push_back(VPTR); |
| 1049 | } |
| 1050 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 1051 | /// getOrCreateRecordType - Emit record type's standalone debug info. |
| 1052 | llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy, |
| 1053 | SourceLocation Loc) { |
| 1054 | llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc)); |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1055 | DBuilder.retainType(T); |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 1056 | return T; |
| 1057 | } |
| 1058 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 1059 | /// CreateType - get structure or union type. |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1060 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1061 | RecordDecl *RD = Ty->getDecl(); |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1062 | llvm::DIFile Unit = getOrCreateFile(RD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1064 | // Get overall information about the record type for the debug info. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1065 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 1066 | unsigned Line = getLineNumber(RD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1068 | // Records and classes and unions can all be recursive. To handle them, we |
| 1069 | // first generate a debug descriptor for the struct as a forward declaration. |
| 1070 | // Then (if it is a definition) we go through and get debug info for all of |
| 1071 | // its members. Finally, we create a descriptor for the complete type (which |
| 1072 | // may refer to the forward decl if the struct is recursive) and replace all |
| 1073 | // uses of the forward declaration with the final definition. |
Devang Patel | 0b89799 | 2010-07-08 19:56:29 +0000 | [diff] [blame] | 1074 | llvm::DIDescriptor FDContext = |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 1075 | getContextDescriptor(cast<Decl>(RD->getDeclContext())); |
Devang Patel | 0b89799 | 2010-07-08 19:56:29 +0000 | [diff] [blame] | 1076 | |
| 1077 | // If this is just a forward declaration, construct an appropriately |
| 1078 | // marked node and just return it. |
| 1079 | if (!RD->getDefinition()) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1080 | llvm::DIType FwdDecl = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1081 | DBuilder.createStructType(FDContext, RD->getName(), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1082 | DefUnit, Line, 0, 0, |
| 1083 | llvm::DIDescriptor::FlagFwdDecl, |
| 1084 | llvm::DIArray()); |
Devang Patel | 0b89799 | 2010-07-08 19:56:29 +0000 | [diff] [blame] | 1085 | |
| 1086 | return FwdDecl; |
| 1087 | } |
Devang Patel | d0f251b | 2010-01-20 23:56:40 +0000 | [diff] [blame] | 1088 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1089 | llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1091 | llvm::MDNode *MN = FwdDecl; |
| 1092 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1093 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 1094 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1095 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 1096 | // Push the struct on region stack. |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1097 | LexicalBlockStack.push_back(FwdDeclNode); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1098 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1099 | |
| 1100 | // Convert all the elements. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1101 | SmallVector<llvm::Value *, 16> EltTys; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1102 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1103 | const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 1104 | if (CXXDecl) { |
| 1105 | CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1106 | CollectVTableInfo(CXXDecl, Unit, EltTys); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 1107 | } |
Devang Patel | dabc3e9 | 2010-08-12 00:02:44 +0000 | [diff] [blame] | 1108 | |
| 1109 | // Collect static variables with initializers. |
| 1110 | for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end(); |
| 1111 | I != E; ++I) |
| 1112 | if (const VarDecl *V = dyn_cast<VarDecl>(*I)) { |
| 1113 | if (const Expr *Init = V->getInit()) { |
| 1114 | Expr::EvalResult Result; |
| 1115 | if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) { |
| 1116 | llvm::ConstantInt *CI |
| 1117 | = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt()); |
| 1118 | |
| 1119 | // Create the descriptor for static variable. |
| 1120 | llvm::DIFile VUnit = getOrCreateFile(V->getLocation()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1121 | StringRef VName = V->getName(); |
Devang Patel | dabc3e9 | 2010-08-12 00:02:44 +0000 | [diff] [blame] | 1122 | llvm::DIType VTy = getOrCreateType(V->getType(), VUnit); |
| 1123 | // Do not use DIGlobalVariable for enums. |
| 1124 | if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) { |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1125 | DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1126 | getLineNumber(V->getLocation()), |
| 1127 | VTy, true, CI); |
Devang Patel | dabc3e9 | 2010-08-12 00:02:44 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 1133 | CollectRecordFields(RD, Unit, EltTys, FwdDecl); |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 1134 | llvm::DIArray TParamsArray; |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 1135 | if (CXXDecl) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1136 | CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 1137 | CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl); |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 1138 | if (const ClassTemplateSpecializationDecl *TSpecial |
| 1139 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
| 1140 | TParamsArray = CollectCXXTemplateParams(TSpecial, Unit); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1141 | } |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 1142 | |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1143 | LexicalBlockStack.pop_back(); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1144 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 1145 | RegionMap.find(Ty->getDecl()); |
| 1146 | if (RI != RegionMap.end()) |
| 1147 | RegionMap.erase(RI); |
| 1148 | |
| 1149 | llvm::DIDescriptor RDContext = |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 1150 | getContextDescriptor(cast<Decl>(RD->getDeclContext())); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1151 | StringRef RDName = RD->getName(); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1152 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1153 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1154 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1155 | llvm::MDNode *RealDecl = NULL; |
| 1156 | |
Devang Patel | 5c5b587 | 2011-02-28 22:32:45 +0000 | [diff] [blame] | 1157 | if (RD->isUnion()) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1158 | RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, |
Devang Patel | 5c5b587 | 2011-02-28 22:32:45 +0000 | [diff] [blame] | 1159 | Size, Align, 0, Elements); |
| 1160 | else if (CXXDecl) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1161 | RDName = getClassName(RD); |
| 1162 | // A class's primary base or the class itself contains the vtable. |
| 1163 | llvm::MDNode *ContainingType = NULL; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1164 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 5bc794f | 2010-10-14 22:59:23 +0000 | [diff] [blame] | 1165 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { |
| 1166 | // Seek non virtual primary base root. |
| 1167 | while (1) { |
| 1168 | const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); |
| 1169 | const CXXRecordDecl *PBT = BRL.getPrimaryBase(); |
Anders Carlsson | c9e814b | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 1170 | if (PBT && !BRL.isPrimaryBaseVirtual()) |
Devang Patel | 5bc794f | 2010-10-14 22:59:23 +0000 | [diff] [blame] | 1171 | PBase = PBT; |
| 1172 | else |
| 1173 | break; |
| 1174 | } |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 1175 | ContainingType = |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1176 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit); |
Devang Patel | 5bc794f | 2010-10-14 22:59:23 +0000 | [diff] [blame] | 1177 | } |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 1178 | else if (CXXDecl->isDynamicClass()) |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1179 | ContainingType = FwdDecl; |
Devang Patel | 9c1714b | 2011-04-05 17:30:54 +0000 | [diff] [blame] | 1180 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1181 | RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1182 | Size, Align, 0, 0, llvm::DIType(), |
Devang Patel | fa275df | 2011-02-02 21:38:49 +0000 | [diff] [blame] | 1183 | Elements, ContainingType, |
| 1184 | TParamsArray); |
Devang Patel | 5c5b587 | 2011-02-28 22:32:45 +0000 | [diff] [blame] | 1185 | } else |
| 1186 | RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line, |
| 1187 | Size, Align, 0, Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1189 | // Now that we have a real decl for the struct, replace anything using the |
| 1190 | // old decl with the new one. This will recursively update the debug info. |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 1191 | llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1192 | RegionMap[RD] = llvm::WeakVH(RealDecl); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1193 | return llvm::DIType(RealDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1196 | /// CreateType - get objective-c object type. |
| 1197 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty, |
| 1198 | llvm::DIFile Unit) { |
| 1199 | // Ignore protocols. |
| 1200 | return getOrCreateType(Ty->getBaseType(), Unit); |
| 1201 | } |
| 1202 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1203 | /// CreateType - get objective-c interface type. |
| 1204 | llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1205 | llvm::DIFile Unit) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1206 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
Douglas Gregor | a6a2897 | 2010-11-30 06:38:09 +0000 | [diff] [blame] | 1207 | if (!ID) |
| 1208 | return llvm::DIType(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1209 | |
| 1210 | // Get overall information about the record type for the debug info. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1211 | llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1212 | unsigned Line = getLineNumber(ID->getLocation()); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1213 | unsigned RuntimeLang = TheCU.getLanguage(); |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 1214 | |
Eric Christopher | d1ab1a2 | 2011-10-06 00:31:18 +0000 | [diff] [blame] | 1215 | // If this is just a forward declaration return a special forward-declaration |
| 1216 | // debug type since we won't be able to lay out the entire type. |
Dan Gohman | 45f7c78 | 2010-08-23 21:15:56 +0000 | [diff] [blame] | 1217 | if (ID->isForwardDecl()) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1218 | llvm::DIType FwdDecl = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1219 | DBuilder.createStructType(Unit, ID->getName(), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1220 | DefUnit, Line, 0, 0, 0, |
| 1221 | llvm::DIArray(), RuntimeLang); |
Dan Gohman | 45f7c78 | 2010-08-23 21:15:56 +0000 | [diff] [blame] | 1222 | return FwdDecl; |
| 1223 | } |
| 1224 | |
Eric Christopher | 1cd1d74 | 2011-10-06 00:30:52 +0000 | [diff] [blame] | 1225 | // To handle a recursive interface, we first generate a debug descriptor |
| 1226 | // for the struct as a forward declaration. Then (if it is a definition) |
| 1227 | // we go through and get debug info for all of its members. Finally, we |
| 1228 | // create a descriptor for the complete type (which may refer to the |
| 1229 | // forward decl if the struct is recursive) and replace all uses of the |
| 1230 | // forward declaration with the final definition. |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1231 | llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1233 | llvm::MDNode *MN = FwdDecl; |
| 1234 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1235 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 1236 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1237 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 1238 | // Push the struct on region stack. |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1239 | LexicalBlockStack.push_back(FwdDeclNode); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1240 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Convert all the elements. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1243 | SmallVector<llvm::Value *, 16> EltTys; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1244 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1245 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 1246 | if (SClass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | llvm::DIType SClassTy = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1248 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
Douglas Gregor | a6a2897 | 2010-11-30 06:38:09 +0000 | [diff] [blame] | 1249 | if (!SClassTy.isValid()) |
| 1250 | return llvm::DIType(); |
| 1251 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | llvm::DIType InhTag = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1253 | DBuilder.createInheritance(FwdDecl, SClassTy, 0, 0); |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 1254 | EltTys.push_back(InhTag); |
| 1255 | } |
| 1256 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1257 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
Devang Patel | 8c6f9c4 | 2011-09-19 18:54:16 +0000 | [diff] [blame] | 1258 | ObjCImplementationDecl *ImpD = ID->getImplementation(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1259 | unsigned FieldNo = 0; |
Fariborz Jahanian | 9747739 | 2010-10-01 00:01:53 +0000 | [diff] [blame] | 1260 | for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; |
Fariborz Jahanian | fe8fdba | 2010-10-11 23:55:47 +0000 | [diff] [blame] | 1261 | Field = Field->getNextIvar(), ++FieldNo) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1262 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Douglas Gregor | a6a2897 | 2010-11-30 06:38:09 +0000 | [diff] [blame] | 1263 | if (!FieldTy.isValid()) |
| 1264 | return llvm::DIType(); |
| 1265 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1266 | StringRef FieldName = Field->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1267 | |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 1268 | // Ignore unnamed fields. |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 1269 | if (FieldName.empty()) |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 1270 | continue; |
| 1271 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1272 | // Get the location for the field. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1273 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 1274 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1275 | QualType FType = Field->getType(); |
| 1276 | uint64_t FieldSize = 0; |
| 1277 | unsigned FieldAlign = 0; |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1278 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1279 | if (!FType->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1281 | // Bit size, align and offset of the type. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1282 | FieldSize = Field->isBitField() |
| 1283 | ? Field->getBitWidthValue(CGM.getContext()) |
| 1284 | : CGM.getContext().getTypeSize(FType); |
| 1285 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Eric Christopher | d1ab1a2 | 2011-10-06 00:31:18 +0000 | [diff] [blame] | 1288 | // We can't know the offset of our ivar in the structure if we're using |
| 1289 | // the non-fragile abi and the debugger should ignore the value anyways. |
| 1290 | // Call it the FieldNo+1 due to how debuggers use the information, |
| 1291 | // e.g. negating the value when it needs a lookup in the dynamic table. |
| 1292 | uint64_t FieldOffset = CGM.getLangOptions().ObjCNonFragileABI ? FieldNo+1 |
| 1293 | : RL.getFieldOffset(FieldNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1295 | unsigned Flags = 0; |
| 1296 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1297 | Flags = llvm::DIDescriptor::FlagProtected; |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1298 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1299 | Flags = llvm::DIDescriptor::FlagPrivate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1301 | StringRef PropertyName; |
| 1302 | StringRef PropertyGetter; |
| 1303 | StringRef PropertySetter; |
Eli Friedman | 10292cc | 2011-04-17 06:40:15 +0000 | [diff] [blame] | 1304 | unsigned PropertyAttributes = 0; |
Devang Patel | 8c6f9c4 | 2011-09-19 18:54:16 +0000 | [diff] [blame] | 1305 | ObjCPropertyDecl *PD = NULL; |
| 1306 | if (ImpD) |
| 1307 | if (ObjCPropertyImplDecl *PImpD = |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 1308 | ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) |
| 1309 | PD = PImpD->getPropertyDecl(); |
Devang Patel | 8c6f9c4 | 2011-09-19 18:54:16 +0000 | [diff] [blame] | 1310 | if (PD) { |
Devang Patel | fa936d8 | 2011-04-16 00:12:55 +0000 | [diff] [blame] | 1311 | PropertyName = PD->getName(); |
Devang Patel | 90c1eed | 2011-04-16 00:37:51 +0000 | [diff] [blame] | 1312 | PropertyGetter = getSelectorName(PD->getGetterName()); |
| 1313 | PropertySetter = getSelectorName(PD->getSetterName()); |
Devang Patel | fa936d8 | 2011-04-16 00:12:55 +0000 | [diff] [blame] | 1314 | PropertyAttributes = PD->getPropertyAttributes(); |
Devang Patel | 8c6f9c4 | 2011-09-19 18:54:16 +0000 | [diff] [blame] | 1315 | } |
Devang Patel | fa936d8 | 2011-04-16 00:12:55 +0000 | [diff] [blame] | 1316 | FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, |
| 1317 | FieldLine, FieldSize, FieldAlign, |
| 1318 | FieldOffset, Flags, FieldTy, |
| 1319 | PropertyName, PropertyGetter, |
| 1320 | PropertySetter, PropertyAttributes); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1321 | EltTys.push_back(FieldTy); |
| 1322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1324 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1325 | |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1326 | LexicalBlockStack.pop_back(); |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 1327 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 1328 | RegionMap.find(Ty->getDecl()); |
| 1329 | if (RI != RegionMap.end()) |
| 1330 | RegionMap.erase(RI); |
| 1331 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1332 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1333 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1334 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Devang Patel | 707b1e9 | 2011-05-12 19:07:41 +0000 | [diff] [blame] | 1336 | unsigned Flags = 0; |
Devang Patel | f568b64 | 2011-05-12 21:14:54 +0000 | [diff] [blame] | 1337 | if (ID->getImplementation()) |
Devang Patel | aad1609 | 2011-05-12 21:29:57 +0000 | [diff] [blame] | 1338 | Flags |= llvm::DIDescriptor::FlagObjcClassComplete; |
Devang Patel | 707b1e9 | 2011-05-12 19:07:41 +0000 | [diff] [blame] | 1339 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1340 | llvm::DIType RealDecl = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1341 | DBuilder.createStructType(Unit, ID->getName(), DefUnit, |
Devang Patel | 707b1e9 | 2011-05-12 19:07:41 +0000 | [diff] [blame] | 1342 | Line, Size, Align, Flags, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1343 | Elements, RuntimeLang); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1344 | |
| 1345 | // Now that we have a real decl for the struct, replace anything using the |
| 1346 | // old decl with the new one. This will recursively update the debug info. |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 1347 | llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1348 | RegionMap[ID] = llvm::WeakVH(RealDecl); |
Devang Patel | fe09eab | 2009-07-13 17:03:14 +0000 | [diff] [blame] | 1349 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1350 | return RealDecl; |
| 1351 | } |
| 1352 | |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1353 | llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1354 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1355 | return CreateType(RT); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1356 | else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1357 | return CreateEnumType(ET->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1359 | return llvm::DIType(); |
| 1360 | } |
| 1361 | |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1362 | llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1363 | llvm::DIFile Unit) { |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1364 | llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
Devang Patel | 6cf37dd | 2011-04-08 21:56:52 +0000 | [diff] [blame] | 1365 | int64_t NumElems = Ty->getNumElements(); |
| 1366 | int64_t LowerBound = 0; |
| 1367 | if (NumElems == 0) |
| 1368 | // If number of elements are not known then this is an unbounded array. |
| 1369 | // Use Low = 1, Hi = 0 to express such arrays. |
| 1370 | LowerBound = 1; |
| 1371 | else |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1372 | --NumElems; |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1373 | |
Devang Patel | 6cf37dd | 2011-04-08 21:56:52 +0000 | [diff] [blame] | 1374 | llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems); |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1375 | llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1376 | |
| 1377 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1378 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 1379 | |
| 1380 | return |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1381 | DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1384 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1385 | llvm::DIFile Unit) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1386 | uint64_t Size; |
| 1387 | uint64_t Align; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
| 1389 | |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1390 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1391 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1392 | Size = 0; |
| 1393 | Align = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1394 | CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1395 | } else if (Ty->isIncompleteArrayType()) { |
| 1396 | Size = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1397 | Align = CGM.getContext().getTypeAlign(Ty->getElementType()); |
Devang Patel | ba690a4 | 2011-04-04 23:18:38 +0000 | [diff] [blame] | 1398 | } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) { |
Devang Patel | ae503df | 2011-04-01 19:02:33 +0000 | [diff] [blame] | 1399 | Size = 0; |
| 1400 | Align = 0; |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1401 | } else { |
| 1402 | // Size and align of the whole array, not the element type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1403 | Size = CGM.getContext().getTypeSize(Ty); |
| 1404 | Align = CGM.getContext().getTypeAlign(Ty); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1407 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 1408 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 1409 | // obvious/recursive way? |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1410 | SmallVector<llvm::Value *, 8> Subscripts; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1411 | QualType EltTy(Ty, 0); |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1412 | if (Ty->isIncompleteArrayType()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1413 | EltTy = Ty->getElementType(); |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1414 | else { |
| 1415 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
Devang Patel | 6cf37dd | 2011-04-08 21:56:52 +0000 | [diff] [blame] | 1416 | int64_t UpperBound = 0; |
| 1417 | int64_t LowerBound = 0; |
Nick Lewycky | 3894c07 | 2011-04-09 00:25:15 +0000 | [diff] [blame] | 1418 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) { |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1419 | if (CAT->getSize().getZExtValue()) |
Devang Patel | 6cf37dd | 2011-04-08 21:56:52 +0000 | [diff] [blame] | 1420 | UpperBound = CAT->getSize().getZExtValue() - 1; |
Nick Lewycky | 3894c07 | 2011-04-09 00:25:15 +0000 | [diff] [blame] | 1421 | } else |
Devang Patel | 6cf37dd | 2011-04-08 21:56:52 +0000 | [diff] [blame] | 1422 | // This is an unbounded array. Use Low = 1, Hi = 0 to express such |
| 1423 | // arrays. |
| 1424 | LowerBound = 1; |
| 1425 | |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1426 | // FIXME: Verify this is right for VLAs. |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 1427 | Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound, |
| 1428 | UpperBound)); |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1429 | EltTy = Ty->getElementType(); |
| 1430 | } |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 1431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1433 | llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1434 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1435 | llvm::DIType DbgTy = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1436 | DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1437 | SubscriptArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1438 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1441 | llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1442 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1443 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, |
| 1444 | Ty, Ty->getPointeeType(), Unit); |
| 1445 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1446 | |
Douglas Gregor | 36b8ee6 | 2011-01-22 01:58:15 +0000 | [diff] [blame] | 1447 | llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty, |
| 1448 | llvm::DIFile Unit) { |
| 1449 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, |
| 1450 | Ty, Ty->getPointeeType(), Unit); |
| 1451 | } |
| 1452 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1453 | llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1454 | llvm::DIFile U) { |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1455 | QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); |
| 1456 | llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); |
| 1457 | |
| 1458 | if (!Ty->getPointeeType()->isFunctionType()) { |
| 1459 | // We have a data member pointer type. |
| 1460 | return PointerDiffDITy; |
| 1461 | } |
| 1462 | |
| 1463 | // We have a member function pointer type. Treat it as a struct with two |
| 1464 | // ptrdiff_t members. |
| 1465 | std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); |
| 1466 | |
| 1467 | uint64_t FieldOffset = 0; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1468 | llvm::Value *ElementTypes[2]; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1469 | |
| 1470 | // FIXME: This should probably be a function type instead. |
| 1471 | ElementTypes[0] = |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 1472 | DBuilder.createMemberType(U, "ptr", U, 0, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1473 | Info.first, Info.second, FieldOffset, 0, |
| 1474 | PointerDiffDITy); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1475 | FieldOffset += Info.first; |
| 1476 | |
| 1477 | ElementTypes[1] = |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 1478 | DBuilder.createMemberType(U, "ptr", U, 0, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1479 | Info.first, Info.second, FieldOffset, 0, |
| 1480 | PointerDiffDITy); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1481 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1482 | llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1483 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1484 | return DBuilder.createStructType(U, StringRef("test"), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1485 | U, 0, FieldOffset, |
| 1486 | 0, 0, Elements); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1489 | llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, |
| 1490 | llvm::DIFile U) { |
| 1491 | // Ignore the atomic wrapping |
| 1492 | // FIXME: What is the correct representation? |
| 1493 | return getOrCreateType(Ty->getValueType(), U); |
| 1494 | } |
| 1495 | |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1496 | /// CreateEnumType - get enumeration type. |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1497 | llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) { |
| 1498 | llvm::DIFile Unit = getOrCreateFile(ED->getLocation()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1499 | SmallVector<llvm::Value *, 16> Enumerators; |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1500 | |
| 1501 | // Create DIEnumerator elements for each enumerator. |
| 1502 | for (EnumDecl::enumerator_iterator |
| 1503 | Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); |
| 1504 | Enum != EnumEnd; ++Enum) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1505 | Enumerators.push_back( |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1506 | DBuilder.createEnumerator(Enum->getName(), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1507 | Enum->getInitVal().getZExtValue())); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | // Return a CompositeType for the enum itself. |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1511 | llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1512 | |
| 1513 | llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); |
| 1514 | unsigned Line = getLineNumber(ED->getLocation()); |
| 1515 | uint64_t Size = 0; |
Devang Patel | ffc52e7 | 2010-08-24 18:14:06 +0000 | [diff] [blame] | 1516 | uint64_t Align = 0; |
| 1517 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 1518 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
| 1519 | Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl()); |
| 1520 | } |
Devang Patel | 4bc4887 | 2010-10-27 23:23:58 +0000 | [diff] [blame] | 1521 | llvm::DIDescriptor EnumContext = |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 1522 | getContextDescriptor(cast<Decl>(ED->getDeclContext())); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1523 | llvm::DIType DbgTy = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1524 | DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1525 | Size, Align, EltArray); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1526 | return DbgTy; |
| 1527 | } |
| 1528 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1529 | static QualType UnwrapTypeForDebugInfo(QualType T) { |
| 1530 | do { |
| 1531 | QualType LastT = T; |
| 1532 | switch (T->getTypeClass()) { |
| 1533 | default: |
| 1534 | return T; |
| 1535 | case Type::TemplateSpecialization: |
| 1536 | T = cast<TemplateSpecializationType>(T)->desugar(); |
| 1537 | break; |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1538 | case Type::TypeOfExpr: |
| 1539 | T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1540 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1541 | case Type::TypeOf: |
| 1542 | T = cast<TypeOfType>(T)->getUnderlyingType(); |
| 1543 | break; |
| 1544 | case Type::Decltype: |
| 1545 | T = cast<DecltypeType>(T)->getUnderlyingType(); |
| 1546 | break; |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1547 | case Type::UnaryTransform: |
| 1548 | T = cast<UnaryTransformType>(T)->getUnderlyingType(); |
| 1549 | break; |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1550 | case Type::Attributed: |
| 1551 | T = cast<AttributedType>(T)->getEquivalentType(); |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 1552 | break; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1553 | case Type::Elaborated: |
| 1554 | T = cast<ElaboratedType>(T)->getNamedType(); |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1555 | break; |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1556 | case Type::Paren: |
| 1557 | T = cast<ParenType>(T)->getInnerType(); |
| 1558 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1559 | case Type::SubstTemplateTypeParm: |
| 1560 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
| 1561 | break; |
Anders Carlsson | ebc3279 | 2011-03-06 16:43:04 +0000 | [diff] [blame] | 1562 | case Type::Auto: |
| 1563 | T = cast<AutoType>(T)->getDeducedType(); |
| 1564 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | assert(T != LastT && "Type unwrapping failed to unwrap!"); |
| 1568 | if (T == LastT) |
| 1569 | return T; |
| 1570 | } while (true); |
| 1571 | |
| 1572 | return T; |
Anders Carlsson | 5b6117a | 2009-11-14 21:08:12 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1575 | /// getOrCreateType - Get the type from the cache or create a new |
| 1576 | /// one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1577 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1578 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1579 | if (Ty.isNull()) |
| 1580 | return llvm::DIType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1582 | // Unwrap the type as needed for debug information. |
| 1583 | Ty = UnwrapTypeForDebugInfo(Ty); |
Devang Patel | e80d567 | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 1584 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1585 | // Check for existing entry. |
Ted Kremenek | 590838b | 2010-03-29 18:29:57 +0000 | [diff] [blame] | 1586 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1587 | TypeCache.find(Ty.getAsOpaquePtr()); |
Daniel Dunbar | 65f13c3 | 2009-09-19 20:17:48 +0000 | [diff] [blame] | 1588 | if (it != TypeCache.end()) { |
| 1589 | // Verify that the debug info still exists. |
| 1590 | if (&*it->second) |
| 1591 | return llvm::DIType(cast<llvm::MDNode>(it->second)); |
| 1592 | } |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1593 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1594 | // Otherwise create the type. |
| 1595 | llvm::DIType Res = CreateTypeNode(Ty, Unit); |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1596 | |
| 1597 | // And update the type cache. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1598 | TypeCache[Ty.getAsOpaquePtr()] = Res; |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1599 | return Res; |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1602 | /// CreateTypeNode - Create a new debug type node. |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1603 | llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1604 | llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1605 | // Handle qualifiers, which recursively handles what they refer to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1606 | if (Ty.hasLocalQualifiers()) |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1607 | return CreateQualifiedType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1608 | |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1609 | const char *Diag = 0; |
| 1610 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1611 | // Work out details of type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1612 | switch (Ty->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1613 | #define TYPE(Class, Base) |
| 1614 | #define ABSTRACT_TYPE(Class, Base) |
| 1615 | #define NON_CANONICAL_TYPE(Class, Base) |
| 1616 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1617 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1618 | llvm_unreachable("Dependent types cannot show up in debug information"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1619 | |
Anders Carlsson | bfe6995 | 2009-11-06 18:24:04 +0000 | [diff] [blame] | 1620 | case Type::ExtVector: |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1621 | case Type::Vector: |
| 1622 | return CreateType(cast<VectorType>(Ty), Unit); |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 1623 | case Type::ObjCObjectPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1624 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1625 | case Type::ObjCObject: |
| 1626 | return CreateType(cast<ObjCObjectType>(Ty), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | case Type::ObjCInterface: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1628 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
Devang Patel | f1d1d9a | 2010-11-01 16:52:40 +0000 | [diff] [blame] | 1629 | case Type::Builtin: return CreateType(cast<BuiltinType>(Ty)); |
Devang Patel | 344ff5d | 2010-12-09 00:25:29 +0000 | [diff] [blame] | 1630 | case Type::Complex: return CreateType(cast<ComplexType>(Ty)); |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1631 | case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 1632 | case Type::BlockPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1633 | return CreateType(cast<BlockPointerType>(Ty), Unit); |
| 1634 | case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1635 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1636 | case Type::Enum: |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1637 | return CreateType(cast<TagType>(Ty)); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1638 | case Type::FunctionProto: |
| 1639 | case Type::FunctionNoProto: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1640 | return CreateType(cast<FunctionType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1641 | case Type::ConstantArray: |
| 1642 | case Type::VariableArray: |
| 1643 | case Type::IncompleteArray: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1644 | return CreateType(cast<ArrayType>(Ty), Unit); |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1645 | |
| 1646 | case Type::LValueReference: |
| 1647 | return CreateType(cast<LValueReferenceType>(Ty), Unit); |
Douglas Gregor | 36b8ee6 | 2011-01-22 01:58:15 +0000 | [diff] [blame] | 1648 | case Type::RValueReference: |
| 1649 | return CreateType(cast<RValueReferenceType>(Ty), Unit); |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1650 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1651 | case Type::MemberPointer: |
| 1652 | return CreateType(cast<MemberPointerType>(Ty), Unit); |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1653 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1654 | case Type::Atomic: |
| 1655 | return CreateType(cast<AtomicType>(Ty), Unit); |
| 1656 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1657 | case Type::Attributed: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1658 | case Type::TemplateSpecialization: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1659 | case Type::Elaborated: |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1660 | case Type::Paren: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1661 | case Type::SubstTemplateTypeParm: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1662 | case Type::TypeOfExpr: |
| 1663 | case Type::TypeOf: |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1664 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1665 | case Type::UnaryTransform: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1666 | case Type::Auto: |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1667 | llvm_unreachable("type should have been unwrapped!"); |
Douglas Gregor | 36b8ee6 | 2011-01-22 01:58:15 +0000 | [diff] [blame] | 1668 | return llvm::DIType(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1669 | } |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1670 | |
| 1671 | assert(Diag && "Fall through without a diagnostic?"); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1672 | unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1673 | "debug information for %0 is not yet supported"); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1674 | CGM.getDiags().Report(DiagID) |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1675 | << Diag; |
| 1676 | return llvm::DIType(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1679 | /// CreateMemberType - Create new member and increase Offset by FType's size. |
| 1680 | llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1681 | StringRef Name, |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1682 | uint64_t *Offset) { |
| 1683 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 1684 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); |
| 1685 | unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 1686 | llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1687 | FieldSize, FieldAlign, |
| 1688 | *Offset, 0, FieldTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1689 | *Offset += FieldSize; |
| 1690 | return Ty; |
| 1691 | } |
| 1692 | |
Devang Patel | 120bf32 | 2011-04-23 00:08:01 +0000 | [diff] [blame] | 1693 | /// getFunctionDeclaration - Return debug info descriptor to describe method |
| 1694 | /// declaration for the given method definition. |
| 1695 | llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) { |
| 1696 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1697 | if (!FD) return llvm::DISubprogram(); |
| 1698 | |
| 1699 | // Setup context. |
| 1700 | getContextDescriptor(cast<Decl>(D->getDeclContext())); |
| 1701 | |
Devang Patel | 22a5cdf | 2011-04-29 23:42:32 +0000 | [diff] [blame] | 1702 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 1703 | MI = SPCache.find(FD); |
| 1704 | if (MI != SPCache.end()) { |
| 1705 | llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second)); |
| 1706 | if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition()) |
| 1707 | return SP; |
| 1708 | } |
| 1709 | |
Devang Patel | 120bf32 | 2011-04-23 00:08:01 +0000 | [diff] [blame] | 1710 | for (FunctionDecl::redecl_iterator I = FD->redecls_begin(), |
| 1711 | E = FD->redecls_end(); I != E; ++I) { |
| 1712 | const FunctionDecl *NextFD = *I; |
| 1713 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 1714 | MI = SPCache.find(NextFD); |
| 1715 | if (MI != SPCache.end()) { |
| 1716 | llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second)); |
| 1717 | if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition()) |
| 1718 | return SP; |
| 1719 | } |
| 1720 | } |
| 1721 | return llvm::DISubprogram(); |
| 1722 | } |
| 1723 | |
Devang Patel | 1c29652 | 2011-05-31 20:46:46 +0000 | [diff] [blame] | 1724 | // getOrCreateFunctionType - Construct DIType. If it is a c++ method, include |
| 1725 | // implicit parameter "this". |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 1726 | llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl * D, |
| 1727 | QualType FnType, |
Devang Patel | 1c29652 | 2011-05-31 20:46:46 +0000 | [diff] [blame] | 1728 | llvm::DIFile F) { |
| 1729 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 1730 | return getOrCreateMethodType(Method, F); |
Devang Patel | c478f21 | 2011-05-31 21:18:50 +0000 | [diff] [blame] | 1731 | else if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) { |
Devang Patel | c478f21 | 2011-05-31 21:18:50 +0000 | [diff] [blame] | 1732 | // Add "self" and "_cmd" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1733 | SmallVector<llvm::Value *, 16> Elts; |
Devang Patel | c478f21 | 2011-05-31 21:18:50 +0000 | [diff] [blame] | 1734 | |
| 1735 | // First element is always return type. For 'void' functions it is NULL. |
Devang Patel | d127bcb | 2011-05-31 22:21:11 +0000 | [diff] [blame] | 1736 | Elts.push_back(getOrCreateType(OMethod->getResultType(), F)); |
Devang Patel | c478f21 | 2011-05-31 21:18:50 +0000 | [diff] [blame] | 1737 | // "self" pointer is always first argument. |
| 1738 | Elts.push_back(getOrCreateType(OMethod->getSelfDecl()->getType(), F)); |
| 1739 | // "cmd" pointer is always second argument. |
| 1740 | Elts.push_back(getOrCreateType(OMethod->getCmdDecl()->getType(), F)); |
Devang Patel | d127bcb | 2011-05-31 22:21:11 +0000 | [diff] [blame] | 1741 | // Get rest of the arguments. |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 1742 | for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(), |
Devang Patel | d127bcb | 2011-05-31 22:21:11 +0000 | [diff] [blame] | 1743 | PE = OMethod->param_end(); PI != PE; ++PI) |
| 1744 | Elts.push_back(getOrCreateType((*PI)->getType(), F)); |
| 1745 | |
Devang Patel | c478f21 | 2011-05-31 21:18:50 +0000 | [diff] [blame] | 1746 | llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); |
| 1747 | return DBuilder.createSubroutineType(F, EltTypeArray); |
| 1748 | } |
Devang Patel | 1c29652 | 2011-05-31 20:46:46 +0000 | [diff] [blame] | 1749 | return getOrCreateType(FnType, F); |
| 1750 | } |
| 1751 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1752 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 1753 | /// "llvm.dbg.func.start.". |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1754 | void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1755 | llvm::Function *Fn, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1756 | CGBuilderTy &Builder) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1758 | StringRef Name; |
| 1759 | StringRef LinkageName; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1760 | |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1761 | FnBeginRegionCount.push_back(LexicalBlockStack.size()); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1762 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1763 | const Decl *D = GD.getDecl(); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1764 | |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1765 | unsigned Flags = 0; |
Devang Patel | 0692f83 | 2010-10-11 21:58:41 +0000 | [diff] [blame] | 1766 | llvm::DIFile Unit = getOrCreateFile(CurLoc); |
| 1767 | llvm::DIDescriptor FDContext(Unit); |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 1768 | llvm::DIArray TParamsArray; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1769 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1770 | // If there is a DISubprogram for this function available then use it. |
| 1771 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 1772 | FI = SPCache.find(FD); |
| 1773 | if (FI != SPCache.end()) { |
Gabor Greif | 38c9b17 | 2010-09-18 13:00:17 +0000 | [diff] [blame] | 1774 | llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1775 | if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { |
| 1776 | llvm::MDNode *SPN = SP; |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1777 | LexicalBlockStack.push_back(SPN); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1778 | RegionMap[D] = llvm::WeakVH(SP); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1779 | return; |
| 1780 | } |
| 1781 | } |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1782 | Name = getFunctionName(FD); |
| 1783 | // Use mangled name as linkage name for c/c++ functions. |
Devang Patel | a87a2b2 | 2011-05-02 22:49:30 +0000 | [diff] [blame] | 1784 | if (!Fn->hasInternalLinkage()) |
Devang Patel | 2df74c0 | 2011-05-02 22:37:48 +0000 | [diff] [blame] | 1785 | LinkageName = CGM.getMangledName(GD); |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 1786 | if (LinkageName == Name) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1787 | LinkageName = StringRef(); |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1788 | if (FD->hasPrototype()) |
| 1789 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | 0692f83 | 2010-10-11 21:58:41 +0000 | [diff] [blame] | 1790 | if (const NamespaceDecl *NSDecl = |
| 1791 | dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 1792 | FDContext = getOrCreateNameSpace(NSDecl); |
Devang Patel | bc6a191 | 2011-05-17 00:20:09 +0000 | [diff] [blame] | 1793 | else if (const RecordDecl *RDecl = |
| 1794 | dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) |
| 1795 | FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext())); |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 1796 | |
| 1797 | // Collect template parameters. |
| 1798 | TParamsArray = CollectFunctionTemplateParams(FD, Unit); |
David Chisnall | 70b9b44 | 2010-09-02 17:16:32 +0000 | [diff] [blame] | 1799 | } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 1800 | Name = getObjCMethodName(OMD); |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1801 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1802 | } else { |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 1803 | // Use llvm function name. |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1804 | Name = Fn->getName(); |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1805 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1806 | } |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1807 | if (!Name.empty() && Name[0] == '\01') |
| 1808 | Name = Name.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
Devang Patel | 970c618 | 2010-04-24 00:49:16 +0000 | [diff] [blame] | 1810 | // It is expected that CurLoc is set before using EmitFunctionStart. |
| 1811 | // Usually, CurLoc points to the left bracket location of compound |
| 1812 | // statement representing function body. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1813 | unsigned LineNo = getLineNumber(CurLoc); |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1814 | if (D->isImplicit()) |
| 1815 | Flags |= llvm::DIDescriptor::FlagArtificial; |
Devang Patel | 120bf32 | 2011-04-23 00:08:01 +0000 | [diff] [blame] | 1816 | llvm::DISubprogram SPDecl = getFunctionDeclaration(D); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1817 | llvm::DISubprogram SP = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1818 | DBuilder.createFunction(FDContext, Name, LinkageName, Unit, |
Devang Patel | 1c29652 | 2011-05-31 20:46:46 +0000 | [diff] [blame] | 1819 | LineNo, getOrCreateFunctionType(D, FnType, Unit), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1820 | Fn->hasInternalLinkage(), true/*definition*/, |
Devang Patel | 5ecb1df | 2011-04-05 22:54:11 +0000 | [diff] [blame] | 1821 | Flags, CGM.getLangOptions().Optimize, Fn, |
Devang Patel | 120bf32 | 2011-04-23 00:08:01 +0000 | [diff] [blame] | 1822 | TParamsArray, SPDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1824 | // Push function on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1825 | llvm::MDNode *SPN = SP; |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1826 | LexicalBlockStack.push_back(SPN); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1827 | RegionMap[D] = llvm::WeakVH(SP); |
Eric Christopher | 69a1b74 | 2011-09-29 00:00:37 +0000 | [diff] [blame] | 1828 | } |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1829 | |
Eric Christopher | 5321bc4 | 2011-09-29 00:00:41 +0000 | [diff] [blame] | 1830 | /// EmitLocation - Emit metadata to indicate a change in line/column |
| 1831 | /// information in the source file. |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1832 | void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) { |
| 1833 | |
| 1834 | // Update our current location |
| 1835 | setLocation(Loc); |
| 1836 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1837 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1839 | // Don't bother if things are the same as last time. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1840 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1841 | if (CurLoc == PrevLoc || |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 1842 | SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc)) |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 1843 | // New Builder may not be in sync with CGDebugInfo. |
| 1844 | if (!Builder.getCurrentDebugLocation().isUnknown()) |
| 1845 | return; |
Eric Christopher | 414ee4b | 2011-09-29 00:00:35 +0000 | [diff] [blame] | 1846 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1847 | // Update last state. |
| 1848 | PrevLoc = CurLoc; |
| 1849 | |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1850 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1851 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc), |
| 1852 | getColumnNumber(CurLoc), |
Chris Lattner | e541d01 | 2010-04-02 20:21:43 +0000 | [diff] [blame] | 1853 | Scope)); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1856 | /// CreateLexicalBlock - Creates a new lexical block node and pushes it on |
| 1857 | /// the stack. |
| 1858 | void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { |
Devang Patel | 8fae060 | 2009-11-13 19:10:24 +0000 | [diff] [blame] | 1859 | llvm::DIDescriptor D = |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1860 | DBuilder.createLexicalBlock(LexicalBlockStack.empty() ? |
| 1861 | llvm::DIDescriptor() : |
| 1862 | llvm::DIDescriptor(LexicalBlockStack.back()), |
| 1863 | getOrCreateFile(CurLoc), |
| 1864 | getLineNumber(CurLoc), |
| 1865 | getColumnNumber(CurLoc)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1866 | llvm::MDNode *DN = D; |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1867 | LexicalBlockStack.push_back(DN); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1870 | /// EmitLexicalBlockStart - Constructs the debug code for entering a declarative |
| 1871 | /// region - beginning of a DW_TAG_lexical_block. |
| 1872 | void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) { |
| 1873 | // Set our current location. |
| 1874 | setLocation(Loc); |
| 1875 | |
| 1876 | // Create a new lexical block and push it on the stack. |
| 1877 | CreateLexicalBlock(Loc); |
| 1878 | |
| 1879 | // Emit a line table change for the current location inside the new scope. |
| 1880 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc), |
| 1881 | getColumnNumber(Loc), |
| 1882 | LexicalBlockStack.back())); |
| 1883 | } |
| 1884 | |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1885 | /// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative |
Eric Christopher | 43202ae | 2011-09-26 15:03:22 +0000 | [diff] [blame] | 1886 | /// region - end of a DW_TAG_lexical_block. |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1887 | void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) { |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1888 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1889 | |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1890 | // Provide an entry in the line table for the end of the block. |
| 1891 | EmitLocation(Builder, Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1893 | LexicalBlockStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1896 | /// EmitFunctionEnd - Constructs the debug code for exiting a function. |
| 1897 | void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1898 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1899 | unsigned RCount = FnBeginRegionCount.back(); |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1900 | assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch"); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1901 | |
| 1902 | // Pop all regions for this function. |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1903 | while (LexicalBlockStack.size() != RCount) |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1904 | EmitLexicalBlockEnd(Builder, CurLoc); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1905 | FnBeginRegionCount.pop_back(); |
| 1906 | } |
| 1907 | |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1908 | // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. |
| 1909 | // See BuildByRefType. |
| 1910 | llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, |
| 1911 | uint64_t *XOffset) { |
| 1912 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1913 | SmallVector<llvm::Value *, 5> EltTys; |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1914 | QualType FType; |
| 1915 | uint64_t FieldSize, FieldOffset; |
| 1916 | unsigned FieldAlign; |
| 1917 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1918 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1919 | QualType Type = VD->getType(); |
| 1920 | |
| 1921 | FieldOffset = 0; |
| 1922 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1923 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 1924 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1925 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1926 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 1927 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); |
| 1928 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1929 | bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1930 | if (HasCopyAndDispose) { |
| 1931 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1932 | EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", |
| 1933 | &FieldOffset)); |
| 1934 | EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", |
| 1935 | &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
Ken Dyck | 573be63 | 2011-04-22 17:34:18 +0000 | [diff] [blame] | 1939 | if (Align > CGM.getContext().toCharUnitsFromBits( |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1940 | CGM.getContext().getTargetInfo().getPointerAlign(0))) { |
Ken Dyck | 573be63 | 2011-04-22 17:34:18 +0000 | [diff] [blame] | 1941 | CharUnits FieldOffsetInBytes |
| 1942 | = CGM.getContext().toCharUnitsFromBits(FieldOffset); |
| 1943 | CharUnits AlignedOffsetInBytes |
| 1944 | = FieldOffsetInBytes.RoundUpToAlignment(Align); |
| 1945 | CharUnits NumPaddingBytes |
| 1946 | = AlignedOffsetInBytes - FieldOffsetInBytes; |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1947 | |
Ken Dyck | 573be63 | 2011-04-22 17:34:18 +0000 | [diff] [blame] | 1948 | if (NumPaddingBytes.isPositive()) { |
| 1949 | llvm::APInt pad(32, NumPaddingBytes.getQuantity()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1950 | FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, |
| 1951 | pad, ArrayType::Normal, 0); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1952 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | FType = Type; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1957 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1958 | FieldSize = CGM.getContext().getTypeSize(FType); |
Ken Dyck | 573be63 | 2011-04-22 17:34:18 +0000 | [diff] [blame] | 1959 | FieldAlign = CGM.getContext().toBits(Align); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1960 | |
| 1961 | *XOffset = FieldOffset; |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 1962 | FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1963 | 0, FieldSize, FieldAlign, |
| 1964 | FieldOffset, 0, FieldTy); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1965 | EltTys.push_back(FieldTy); |
| 1966 | FieldOffset += FieldSize; |
| 1967 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 1968 | llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1969 | |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1970 | unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct; |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1971 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1972 | return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1973 | Elements); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1974 | } |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1975 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1976 | /// EmitDeclare - Emit local variable declaration debug info. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1977 | void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1978 | llvm::Value *Storage, |
| 1979 | unsigned ArgNo, CGBuilderTy &Builder) { |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 1980 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1981 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1982 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1983 | llvm::DIType Ty; |
| 1984 | uint64_t XOffset = 0; |
| 1985 | if (VD->hasAttr<BlocksAttr>()) |
| 1986 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 1987 | else |
| 1988 | Ty = getOrCreateType(VD->getType(), Unit); |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 1989 | |
Devang Patel | f4e54a2 | 2010-05-07 23:05:55 +0000 | [diff] [blame] | 1990 | // If there is not any debug info for type then do not emit debug info |
| 1991 | // for this variable. |
| 1992 | if (!Ty) |
| 1993 | return; |
| 1994 | |
Devang Patel | 3475380 | 2011-02-16 01:11:51 +0000 | [diff] [blame] | 1995 | if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) { |
| 1996 | // If Storage is an aggregate returned as 'sret' then let debugger know |
| 1997 | // about this. |
Devang Patel | 0691f93 | 2011-02-10 00:40:52 +0000 | [diff] [blame] | 1998 | if (Arg->hasStructRetAttr()) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 1999 | Ty = DBuilder.createReferenceType(Ty); |
Devang Patel | 3475380 | 2011-02-16 01:11:51 +0000 | [diff] [blame] | 2000 | else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) { |
| 2001 | // If an aggregate variable has non trivial destructor or non trivial copy |
| 2002 | // constructor than it is pass indirectly. Let debug info know about this |
| 2003 | // by using reference of the aggregate type as a argument type. |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 2004 | if (!Record->hasTrivialCopyConstructor() || |
| 2005 | !Record->hasTrivialDestructor()) |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2006 | Ty = DBuilder.createReferenceType(Ty); |
Devang Patel | 3475380 | 2011-02-16 01:11:51 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
Devang Patel | 0691f93 | 2011-02-10 00:40:52 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 2010 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 2011 | unsigned Line = getLineNumber(VD->getLocation()); |
| 2012 | unsigned Column = getColumnNumber(VD->getLocation()); |
Devang Patel | aca745b | 2010-09-29 23:09:21 +0000 | [diff] [blame] | 2013 | unsigned Flags = 0; |
| 2014 | if (VD->isImplicit()) |
| 2015 | Flags |= llvm::DIDescriptor::FlagArtificial; |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 2016 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2018 | StringRef Name = VD->getName(); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2019 | if (!Name.empty()) { |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2020 | if (VD->hasAttr<BlocksAttr>()) { |
| 2021 | CharUnits offset = CharUnits::fromQuantity(32); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2022 | SmallVector<llvm::Value *, 9> addr; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2023 | llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); |
Devang Patel | 4a4e2ef | 2011-02-18 23:29:22 +0000 | [diff] [blame] | 2024 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2025 | // offset of __forwarding field |
Ken Dyck | 0ebce0e | 2011-04-22 17:41:34 +0000 | [diff] [blame] | 2026 | offset = CGM.getContext().toCharUnitsFromBits( |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2027 | CGM.getContext().getTargetInfo().getPointerWidth(0)); |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2028 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Devang Patel | 4a4e2ef | 2011-02-18 23:29:22 +0000 | [diff] [blame] | 2029 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); |
| 2030 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2031 | // offset of x field |
Ken Dyck | 0ebce0e | 2011-04-22 17:41:34 +0000 | [diff] [blame] | 2032 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2033 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 2034 | |
| 2035 | // Create the descriptor for the variable. |
| 2036 | llvm::DIVariable D = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2037 | DBuilder.createComplexVariable(Tag, |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 2038 | llvm::DIDescriptor(Scope), |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2039 | VD->getName(), Unit, Line, Ty, |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 2040 | addr, ArgNo); |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2041 | |
| 2042 | // Insert an llvm.dbg.declare into the current block. |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2043 | // Insert an llvm.dbg.declare into the current block. |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2044 | llvm::Instruction *Call = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2045 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 2046 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
| 2047 | return; |
| 2048 | } |
| 2049 | // Create the descriptor for the variable. |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2050 | llvm::DIVariable D = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2051 | DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 2052 | Name, Unit, Line, Ty, |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 2053 | CGM.getLangOptions().Optimize, Flags, ArgNo); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2054 | |
| 2055 | // Insert an llvm.dbg.declare into the current block. |
| 2056 | llvm::Instruction *Call = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2057 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2058 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Devang Patel | f4dd962 | 2010-10-29 16:21:19 +0000 | [diff] [blame] | 2059 | return; |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | // If VD is an anonymous union then Storage represents value for |
| 2063 | // all union fields. |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2064 | if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) { |
| 2065 | const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); |
| 2066 | if (RD->isUnion()) { |
| 2067 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 2068 | E = RD->field_end(); |
| 2069 | I != E; ++I) { |
| 2070 | FieldDecl *Field = *I; |
| 2071 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2072 | StringRef FieldName = Field->getName(); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2073 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2074 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 2075 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
| 2076 | continue; |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2077 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2078 | // Use VarDecl's Tag, Scope and Line number. |
| 2079 | llvm::DIVariable D = |
| 2080 | DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), |
| 2081 | FieldName, Unit, Line, FieldTy, |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 2082 | CGM.getLangOptions().Optimize, Flags, |
| 2083 | ArgNo); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2084 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2085 | // Insert an llvm.dbg.declare into the current block. |
| 2086 | llvm::Instruction *Call = |
| 2087 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2088 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 2089 | } |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2090 | } |
| 2091 | } |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Devang Patel | e2d0191 | 2011-04-25 23:43:36 +0000 | [diff] [blame] | 2094 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, |
| 2095 | llvm::Value *Storage, |
| 2096 | CGBuilderTy &Builder) { |
| 2097 | EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder); |
| 2098 | } |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2099 | |
Devang Patel | e2d0191 | 2011-04-25 23:43:36 +0000 | [diff] [blame] | 2100 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( |
| 2101 | const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder, |
| 2102 | const CGBlockInfo &blockInfo) { |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 2103 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Devang Patel | e2d0191 | 2011-04-25 23:43:36 +0000 | [diff] [blame] | 2104 | |
Devang Patel | 2b594b9 | 2010-04-26 23:28:46 +0000 | [diff] [blame] | 2105 | if (Builder.GetInsertBlock() == 0) |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2106 | return; |
Devang Patel | e2d0191 | 2011-04-25 23:43:36 +0000 | [diff] [blame] | 2107 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2108 | bool isByRef = VD->hasAttr<BlocksAttr>(); |
Devang Patel | e2d0191 | 2011-04-25 23:43:36 +0000 | [diff] [blame] | 2109 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2110 | uint64_t XOffset = 0; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 2111 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 2112 | llvm::DIType Ty; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2113 | if (isByRef) |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 2114 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 2115 | else |
| 2116 | Ty = getOrCreateType(VD->getType(), Unit); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2117 | |
| 2118 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 2119 | unsigned Line = getLineNumber(VD->getLocation()); |
| 2120 | unsigned Column = getColumnNumber(VD->getLocation()); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2121 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2122 | const llvm::TargetData &target = CGM.getTargetData(); |
| 2123 | |
| 2124 | CharUnits offset = CharUnits::fromQuantity( |
| 2125 | target.getStructLayout(blockInfo.StructureType) |
| 2126 | ->getElementOffset(blockInfo.getCapture(VD).getIndex())); |
| 2127 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2128 | SmallVector<llvm::Value *, 9> addr; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2129 | llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); |
Devang Patel | 4a4e2ef | 2011-02-18 23:29:22 +0000 | [diff] [blame] | 2130 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 2131 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2132 | if (isByRef) { |
Devang Patel | 4a4e2ef | 2011-02-18 23:29:22 +0000 | [diff] [blame] | 2133 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); |
| 2134 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 2135 | // offset of __forwarding field |
Eric Christopher | ab5278e | 2011-10-11 23:00:51 +0000 | [diff] [blame] | 2136 | offset = CGM.getContext() |
| 2137 | .toCharUnitsFromBits(target.getPointerSizeInBits()); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 2138 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Devang Patel | 4a4e2ef | 2011-02-18 23:29:22 +0000 | [diff] [blame] | 2139 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); |
| 2140 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 2141 | // offset of x field |
Ken Dyck | 0ebce0e | 2011-04-22 17:41:34 +0000 | [diff] [blame] | 2142 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 2143 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | // Create the descriptor for the variable. |
| 2147 | llvm::DIVariable D = |
Devang Patel | e2d0191 | 2011-04-25 23:43:36 +0000 | [diff] [blame] | 2148 | DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable, |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 2149 | llvm::DIDescriptor(LexicalBlockStack.back()), |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 2150 | VD->getName(), Unit, Line, Ty, addr); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2151 | // Insert an llvm.dbg.declare into the current block. |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2152 | llvm::Instruction *Call = |
Devang Patel | 50811d2 | 2011-04-25 23:52:27 +0000 | [diff] [blame] | 2153 | DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint()); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2154 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, |
| 2155 | LexicalBlockStack.back())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 2158 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 2159 | /// variable declaration. |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 2160 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 2161 | unsigned ArgNo, |
Devang Patel | 3475380 | 2011-02-16 01:11:51 +0000 | [diff] [blame] | 2162 | CGBuilderTy &Builder) { |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 2163 | EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2166 | namespace { |
| 2167 | struct BlockLayoutChunk { |
| 2168 | uint64_t OffsetInBits; |
| 2169 | const BlockDecl::Capture *Capture; |
| 2170 | }; |
| 2171 | bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) { |
| 2172 | return l.OffsetInBits < r.OffsetInBits; |
| 2173 | } |
| 2174 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 2175 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2176 | void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, |
| 2177 | llvm::Value *addr, |
| 2178 | CGBuilderTy &Builder) { |
| 2179 | ASTContext &C = CGM.getContext(); |
| 2180 | const BlockDecl *blockDecl = block.getBlockDecl(); |
| 2181 | |
| 2182 | // Collect some general information about the block's location. |
| 2183 | SourceLocation loc = blockDecl->getCaretLocation(); |
| 2184 | llvm::DIFile tunit = getOrCreateFile(loc); |
| 2185 | unsigned line = getLineNumber(loc); |
| 2186 | unsigned column = getColumnNumber(loc); |
| 2187 | |
| 2188 | // Build the debug-info type for the block literal. |
Nick Lewycky | 7d4b159 | 2011-05-02 01:41:48 +0000 | [diff] [blame] | 2189 | getContextDescriptor(cast<Decl>(blockDecl->getDeclContext())); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2190 | |
| 2191 | const llvm::StructLayout *blockLayout = |
| 2192 | CGM.getTargetData().getStructLayout(block.StructureType); |
| 2193 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2194 | SmallVector<llvm::Value*, 16> fields; |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2195 | fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public, |
| 2196 | blockLayout->getElementOffsetInBits(0), |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2197 | tunit, tunit)); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2198 | fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public, |
| 2199 | blockLayout->getElementOffsetInBits(1), |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2200 | tunit, tunit)); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2201 | fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public, |
| 2202 | blockLayout->getElementOffsetInBits(2), |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2203 | tunit, tunit)); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2204 | fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public, |
| 2205 | blockLayout->getElementOffsetInBits(3), |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2206 | tunit, tunit)); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2207 | fields.push_back(createFieldType("__descriptor", |
| 2208 | C.getPointerType(block.NeedsCopyDispose ? |
| 2209 | C.getBlockDescriptorExtendedType() : |
| 2210 | C.getBlockDescriptorType()), |
| 2211 | 0, loc, AS_public, |
| 2212 | blockLayout->getElementOffsetInBits(4), |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2213 | tunit, tunit)); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2214 | |
| 2215 | // We want to sort the captures by offset, not because DWARF |
| 2216 | // requires this, but because we're paranoid about debuggers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2217 | SmallVector<BlockLayoutChunk, 8> chunks; |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2218 | |
| 2219 | // 'this' capture. |
| 2220 | if (blockDecl->capturesCXXThis()) { |
| 2221 | BlockLayoutChunk chunk; |
| 2222 | chunk.OffsetInBits = |
| 2223 | blockLayout->getElementOffsetInBits(block.CXXThisIndex); |
| 2224 | chunk.Capture = 0; |
| 2225 | chunks.push_back(chunk); |
| 2226 | } |
| 2227 | |
| 2228 | // Variable captures. |
| 2229 | for (BlockDecl::capture_const_iterator |
| 2230 | i = blockDecl->capture_begin(), e = blockDecl->capture_end(); |
| 2231 | i != e; ++i) { |
| 2232 | const BlockDecl::Capture &capture = *i; |
| 2233 | const VarDecl *variable = capture.getVariable(); |
| 2234 | const CGBlockInfo::Capture &captureInfo = block.getCapture(variable); |
| 2235 | |
| 2236 | // Ignore constant captures. |
| 2237 | if (captureInfo.isConstant()) |
| 2238 | continue; |
| 2239 | |
| 2240 | BlockLayoutChunk chunk; |
| 2241 | chunk.OffsetInBits = |
| 2242 | blockLayout->getElementOffsetInBits(captureInfo.getIndex()); |
| 2243 | chunk.Capture = &capture; |
| 2244 | chunks.push_back(chunk); |
| 2245 | } |
| 2246 | |
| 2247 | // Sort by offset. |
| 2248 | llvm::array_pod_sort(chunks.begin(), chunks.end()); |
| 2249 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2250 | for (SmallVectorImpl<BlockLayoutChunk>::iterator |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2251 | i = chunks.begin(), e = chunks.end(); i != e; ++i) { |
| 2252 | uint64_t offsetInBits = i->OffsetInBits; |
| 2253 | const BlockDecl::Capture *capture = i->Capture; |
| 2254 | |
| 2255 | // If we have a null capture, this must be the C++ 'this' capture. |
| 2256 | if (!capture) { |
| 2257 | const CXXMethodDecl *method = |
| 2258 | cast<CXXMethodDecl>(blockDecl->getNonClosureContext()); |
| 2259 | QualType type = method->getThisType(C); |
| 2260 | |
| 2261 | fields.push_back(createFieldType("this", type, 0, loc, AS_public, |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2262 | offsetInBits, tunit, tunit)); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2263 | continue; |
| 2264 | } |
| 2265 | |
| 2266 | const VarDecl *variable = capture->getVariable(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2267 | StringRef name = variable->getName(); |
John McCall | d113a6f | 2011-03-02 06:57:14 +0000 | [diff] [blame] | 2268 | |
| 2269 | llvm::DIType fieldType; |
| 2270 | if (capture->isByRef()) { |
| 2271 | std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy); |
| 2272 | |
| 2273 | // FIXME: this creates a second copy of this type! |
| 2274 | uint64_t xoffset; |
| 2275 | fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset); |
| 2276 | fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first); |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2277 | fieldType = DBuilder.createMemberType(tunit, name, tunit, line, |
John McCall | d113a6f | 2011-03-02 06:57:14 +0000 | [diff] [blame] | 2278 | ptrInfo.first, ptrInfo.second, |
| 2279 | offsetInBits, 0, fieldType); |
| 2280 | } else { |
| 2281 | fieldType = createFieldType(name, variable->getType(), 0, |
Devang Patel | 1d323e0 | 2011-06-24 22:00:59 +0000 | [diff] [blame] | 2282 | loc, AS_public, offsetInBits, tunit, tunit); |
John McCall | d113a6f | 2011-03-02 06:57:14 +0000 | [diff] [blame] | 2283 | } |
| 2284 | fields.push_back(fieldType); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | llvm::SmallString<36> typeName; |
| 2288 | llvm::raw_svector_ostream(typeName) |
| 2289 | << "__block_literal_" << CGM.getUniqueBlockCount(); |
| 2290 | |
Jay Foad | c556ef2 | 2011-04-24 10:11:03 +0000 | [diff] [blame] | 2291 | llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2292 | |
| 2293 | llvm::DIType type = |
| 2294 | DBuilder.createStructType(tunit, typeName.str(), tunit, line, |
| 2295 | CGM.getContext().toBits(block.BlockSize), |
| 2296 | CGM.getContext().toBits(block.BlockAlign), |
| 2297 | 0, fieldsArray); |
| 2298 | type = DBuilder.createPointerType(type, CGM.PointerWidthInBits); |
| 2299 | |
| 2300 | // Get overall information about the block. |
| 2301 | unsigned flags = llvm::DIDescriptor::FlagArtificial; |
Eric Christopher | aa2164c | 2011-09-29 00:00:45 +0000 | [diff] [blame] | 2302 | llvm::MDNode *scope = LexicalBlockStack.back(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2303 | StringRef name = ".block_descriptor"; |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2304 | |
| 2305 | // Create the descriptor for the parameter. |
| 2306 | llvm::DIVariable debugVar = |
| 2307 | DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable, |
| 2308 | llvm::DIDescriptor(scope), |
| 2309 | name, tunit, line, type, |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 2310 | CGM.getLangOptions().Optimize, flags, |
| 2311 | cast<llvm::Argument>(addr)->getArgNo() + 1); |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2312 | |
| 2313 | // Insert an llvm.dbg.value into the current block. |
| 2314 | llvm::Instruction *declare = |
| 2315 | DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar, |
| 2316 | Builder.GetInsertBlock()); |
| 2317 | declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope)); |
| 2318 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 2319 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 2320 | /// EmitGlobalVariable - Emit information about a global variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 2322 | const VarDecl *D) { |
| 2323 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 2324 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 2325 | llvm::DIFile Unit = getOrCreateFile(D->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 2326 | unsigned LineNo = getLineNumber(D->getLocation()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2327 | |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2328 | setLocation(D->getLocation()); |
| 2329 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 2330 | QualType T = D->getType(); |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 2331 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 2333 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 2334 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 2336 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 2337 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2338 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 2339 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 2340 | ArrayType::Normal, 0); |
| 2341 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2342 | StringRef DeclName = D->getName(); |
| 2343 | StringRef LinkageName; |
Devang Patel | eb4c45b | 2011-02-09 19:16:38 +0000 | [diff] [blame] | 2344 | if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()) |
| 2345 | && !isa<ObjCMethodDecl>(D->getDeclContext())) |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 2346 | LinkageName = Var->getName(); |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 2347 | if (LinkageName == DeclName) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2348 | LinkageName = StringRef(); |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 2349 | llvm::DIDescriptor DContext = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 2350 | getContextDescriptor(dyn_cast<Decl>(D->getDeclContext())); |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2351 | DBuilder.createStaticVariable(DContext, DeclName, LinkageName, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 2352 | Unit, LineNo, getOrCreateType(T, Unit), |
| 2353 | Var->hasInternalLinkage(), Var); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2356 | /// EmitGlobalVariable - Emit information about an objective-c interface. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 2358 | ObjCInterfaceDecl *ID) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2359 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 2360 | llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 2361 | unsigned LineNo = getLineNumber(ID->getLocation()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2362 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2363 | StringRef Name = ID->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2364 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 2365 | QualType T = CGM.getContext().getObjCInterfaceType(ID); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2366 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2368 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 2369 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2371 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 2372 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 2374 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2375 | ArrayType::Normal, 0); |
| 2376 | } |
| 2377 | |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2378 | DBuilder.createGlobalVariable(Name, Unit, LineNo, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 2379 | getOrCreateType(T, Unit), |
| 2380 | Var->hasInternalLinkage(), Var); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 2381 | } |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 2382 | |
Devang Patel | 25c2c8f | 2010-08-10 17:53:33 +0000 | [diff] [blame] | 2383 | /// EmitGlobalVariable - Emit global variable's debug info. |
| 2384 | void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 2385 | llvm::Constant *Init) { |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 2386 | // Create the descriptor for the variable. |
| 2387 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2388 | StringRef Name = VD->getName(); |
Devang Patel | 0317ab0 | 2010-08-10 18:27:15 +0000 | [diff] [blame] | 2389 | llvm::DIType Ty = getOrCreateType(VD->getType(), Unit); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 2390 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) { |
| 2391 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 2392 | Ty = CreateEnumType(ED); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 2393 | } |
Devang Patel | 0317ab0 | 2010-08-10 18:27:15 +0000 | [diff] [blame] | 2394 | // Do not use DIGlobalVariable for enums. |
| 2395 | if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type) |
| 2396 | return; |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2397 | DBuilder.createStaticVariable(Unit, Name, Name, Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 2398 | getLineNumber(VD->getLocation()), |
| 2399 | Ty, true, Init); |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 2402 | /// getOrCreateNamesSpace - Return namespace descriptor for the given |
| 2403 | /// namespace decl. |
| 2404 | llvm::DINameSpace |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 2405 | CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) { |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 2406 | llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = |
| 2407 | NameSpaceCache.find(NSDecl); |
| 2408 | if (I != NameSpaceCache.end()) |
| 2409 | return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); |
| 2410 | |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 2411 | unsigned LineNo = getLineNumber(NSDecl->getLocation()); |
Devang Patel | 8c37668 | 2010-10-28 19:12:46 +0000 | [diff] [blame] | 2412 | llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation()); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 2413 | llvm::DIDescriptor Context = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 2414 | getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext())); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 2415 | llvm::DINameSpace NS = |
Devang Patel | 16674e8 | 2011-02-22 18:56:36 +0000 | [diff] [blame] | 2416 | DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 2417 | NameSpaceCache[NSDecl] = llvm::WeakVH(NS); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 2418 | return NS; |
| 2419 | } |
Devang Patel | e80d567 | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 2420 | |
| 2421 | /// UpdateCompletedType - Update type cache because the type is now |
| 2422 | /// translated. |
| 2423 | void CGDebugInfo::UpdateCompletedType(const TagDecl *TD) { |
| 2424 | QualType Ty = CGM.getContext().getTagDeclType(TD); |
| 2425 | |
| 2426 | // If the type exist in type cache then remove it from the cache. |
| 2427 | // There is no need to prepare debug info for the completed type |
| 2428 | // right now. It will be generated on demand lazily. |
| 2429 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
| 2430 | TypeCache.find(Ty.getAsOpaquePtr()); |
| 2431 | if (it != TypeCache.end()) |
| 2432 | TypeCache.erase(it); |
| 2433 | } |