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