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" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
| 24 | #include "clang/Basic/FileManager.h" |
Mike Stump | 5a86217 | 2009-09-15 21:48:34 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Version.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
| 28 | #include "llvm/DerivedTypes.h" |
| 29 | #include "llvm/Instructions.h" |
| 30 | #include "llvm/Intrinsics.h" |
| 31 | #include "llvm/Module.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
| 33 | #include "llvm/ADT/SmallVector.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Dwarf.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Path.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | using namespace clang::CodeGen; |
| 39 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 40 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 41 | : CGM(CGM), DBuilder(CGM.getModule()), |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 42 | BlockLiteralGenericSet(false) { |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 43 | CreateCompileUnit(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 46 | CGDebugInfo::~CGDebugInfo() { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 47 | assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 50 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 51 | if (Loc.isValid()) |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 52 | CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Devang Patel | 3358305 | 2010-01-28 23:15:27 +0000 | [diff] [blame] | 55 | /// getContextDescriptor - Get context info for the decl. |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 56 | llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) { |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 57 | if (!Context) |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 58 | return TheCU; |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 59 | |
| 60 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator |
| 61 | I = RegionMap.find(Context); |
| 62 | if (I != RegionMap.end()) |
Gabor Greif | 38c9b17 | 2010-09-18 13:00:17 +0000 | [diff] [blame] | 63 | return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second)); |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 64 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 65 | // Check namespace. |
| 66 | if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 67 | return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl)); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 68 | |
| 69 | if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) { |
| 70 | if (!RDecl->isDependentType()) { |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 71 | llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 72 | getOrCreateMainFile()); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 73 | return llvm::DIDescriptor(Ty); |
| 74 | } |
| 75 | } |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 76 | return TheCU; |
Devang Patel | 979ec2e | 2009-10-06 00:35:31 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 79 | /// getFunctionName - Get function name for the given FunctionDecl. If the |
| 80 | /// name is constructred on demand (e.g. C++ destructor) then the name |
| 81 | /// is stored on the side. |
| 82 | llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
| 83 | assert (FD && "Invalid FunctionDecl!"); |
| 84 | IdentifierInfo *FII = FD->getIdentifier(); |
| 85 | if (FII) |
| 86 | return FII->getName(); |
| 87 | |
| 88 | // Otherwise construct human readable name for debug info. |
| 89 | std::string NS = FD->getNameAsString(); |
| 90 | |
| 91 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 92 | char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); |
Benjamin Kramer | 1b627dc | 2010-01-23 18:16:07 +0000 | [diff] [blame] | 93 | memcpy(StrPtr, NS.data(), NS.length()); |
| 94 | return llvm::StringRef(StrPtr, NS.length()); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 95 | } |
| 96 | |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 97 | llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { |
| 98 | llvm::SmallString<256> MethodName; |
| 99 | llvm::raw_svector_ostream OS(MethodName); |
| 100 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 101 | const DeclContext *DC = OMD->getDeclContext(); |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 102 | if (const ObjCImplementationDecl *OID = |
| 103 | dyn_cast<const ObjCImplementationDecl>(DC)) { |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 104 | OS << OID->getName(); |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 105 | } else if (const ObjCInterfaceDecl *OID = |
| 106 | dyn_cast<const ObjCInterfaceDecl>(DC)) { |
Fariborz Jahanian | 1a4c937 | 2010-10-18 17:51:06 +0000 | [diff] [blame] | 107 | OS << OID->getName(); |
Devang Patel | a2e5769 | 2010-10-28 17:27:32 +0000 | [diff] [blame] | 108 | } else if (const ObjCCategoryImplDecl *OCD = |
| 109 | dyn_cast<const ObjCCategoryImplDecl>(DC)){ |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 110 | OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' << |
| 111 | OCD->getIdentifier()->getNameStart() << ')'; |
| 112 | } |
| 113 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 114 | |
| 115 | char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell()); |
| 116 | memcpy(StrPtr, MethodName.begin(), OS.tell()); |
| 117 | return llvm::StringRef(StrPtr, OS.tell()); |
| 118 | } |
| 119 | |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 120 | /// getClassName - Get class name including template argument list. |
| 121 | llvm::StringRef |
| 122 | CGDebugInfo::getClassName(RecordDecl *RD) { |
| 123 | ClassTemplateSpecializationDecl *Spec |
| 124 | = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 125 | if (!Spec) |
| 126 | return RD->getName(); |
| 127 | |
| 128 | const TemplateArgument *Args; |
| 129 | unsigned NumArgs; |
| 130 | std::string Buffer; |
| 131 | if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { |
| 132 | const TemplateSpecializationType *TST = |
| 133 | cast<TemplateSpecializationType>(TAW->getType()); |
| 134 | Args = TST->getArgs(); |
| 135 | NumArgs = TST->getNumArgs(); |
| 136 | } else { |
| 137 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 138 | Args = TemplateArgs.data(); |
| 139 | NumArgs = TemplateArgs.size(); |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 140 | } |
| 141 | Buffer = RD->getIdentifier()->getNameStart(); |
| 142 | PrintingPolicy Policy(CGM.getLangOptions()); |
| 143 | Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, |
| 144 | NumArgs, |
| 145 | Policy); |
| 146 | |
| 147 | // Copy this name on the side and use its reference. |
| 148 | char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length()); |
| 149 | memcpy(StrPtr, Buffer.data(), Buffer.length()); |
| 150 | return llvm::StringRef(StrPtr, Buffer.length()); |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 153 | /// getOrCreateFile - Get the file debug info descriptor for the input location. |
| 154 | llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 155 | if (!Loc.isValid()) |
| 156 | // If Location is not valid then use main input file. |
| 157 | return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory()); |
| 158 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 159 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 160 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 162 | if (PLoc.isInvalid()) |
| 163 | // If the location is not valid then use main input file. |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 164 | return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory()); |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 166 | // Cache the results. |
| 167 | const char *fname = PLoc.getFilename(); |
| 168 | llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = |
| 169 | DIFileCache.find(fname); |
| 170 | |
| 171 | if (it != DIFileCache.end()) { |
| 172 | // Verify that the information still exists. |
| 173 | if (&*it->second) |
| 174 | return llvm::DIFile(cast<llvm::MDNode>(it->second)); |
| 175 | } |
| 176 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 177 | llvm::DIFile F = DBuilder.CreateFile(PLoc.getFilename(), getCurrentDirname()); |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 178 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 179 | DIFileCache[fname] = F; |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 180 | return F; |
| 181 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 182 | } |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 183 | |
Devang Patel | 532105f | 2010-10-28 22:03:20 +0000 | [diff] [blame] | 184 | /// getOrCreateMainFile - Get the file info for main compile unit. |
| 185 | llvm::DIFile CGDebugInfo::getOrCreateMainFile() { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 186 | return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory()); |
Devang Patel | 532105f | 2010-10-28 22:03:20 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 189 | /// getLineNumber - Get line number for the location. If location is invalid |
| 190 | /// then use current location. |
| 191 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
| 192 | assert (CurLoc.isValid() && "Invalid current location!"); |
| 193 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 194 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 195 | return PLoc.isValid()? PLoc.getLine() : 0; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /// getColumnNumber - Get column number for the location. If location is |
| 199 | /// invalid then use current location. |
| 200 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) { |
| 201 | assert (CurLoc.isValid() && "Invalid current location!"); |
| 202 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 203 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 204 | return PLoc.isValid()? PLoc.getColumn() : 0; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 207 | llvm::StringRef CGDebugInfo::getCurrentDirname() { |
| 208 | if (!CWDName.empty()) |
| 209 | return CWDName; |
| 210 | char *CompDirnamePtr = NULL; |
| 211 | llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory(); |
| 212 | CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size()); |
| 213 | memcpy(CompDirnamePtr, CWD.c_str(), CWD.size()); |
| 214 | return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size()); |
| 215 | } |
| 216 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 217 | /// CreateCompileUnit - Create new compile unit. |
| 218 | void CGDebugInfo::CreateCompileUnit() { |
| 219 | |
| 220 | // Get absolute path name. |
Douglas Gregor | ac91b4c | 2010-03-18 23:46:43 +0000 | [diff] [blame] | 221 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 222 | std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| 223 | if (MainFileName.empty()) |
Devang Patel | 22fe585 | 2010-03-12 21:04:27 +0000 | [diff] [blame] | 224 | MainFileName = "<unknown>"; |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | f6728fc | 2010-03-22 21:28:29 +0000 | [diff] [blame] | 226 | // The main file name provided via the "-main-file-name" option contains just |
| 227 | // the file name itself with no path information. This file name may have had |
| 228 | // a relative path, so we look into the actual file entry for the main |
| 229 | // file to determine the real absolute path for the file. |
Devang Patel | 6e6bc39 | 2010-07-23 23:04:28 +0000 | [diff] [blame] | 230 | std::string MainFileDir; |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 231 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 232 | MainFileDir = MainFile->getDir()->getName(); |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 233 | if (MainFileDir != ".") |
| 234 | MainFileName = MainFileDir + "/" + MainFileName; |
| 235 | } |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 236 | |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 237 | // Save filename string. |
| 238 | char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length()); |
| 239 | memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length()); |
| 240 | llvm::StringRef Filename(FilenamePtr, MainFileName.length()); |
| 241 | |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 242 | unsigned LangTag; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 243 | const LangOptions &LO = CGM.getLangOptions(); |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 244 | if (LO.CPlusPlus) { |
| 245 | if (LO.ObjC1) |
| 246 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 247 | else |
| 248 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 249 | } else if (LO.ObjC1) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 250 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 251 | } else if (LO.C99) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 252 | LangTag = llvm::dwarf::DW_LANG_C99; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 253 | } else { |
| 254 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 255 | } |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 256 | |
Daniel Dunbar | 19f1983 | 2010-08-24 17:41:09 +0000 | [diff] [blame] | 257 | std::string Producer = getClangFullVersion(); |
Chris Lattner | 4c2577a | 2009-05-02 01:00:04 +0000 | [diff] [blame] | 258 | |
| 259 | // Figure out which version of the ObjC runtime we have. |
| 260 | unsigned RuntimeVers = 0; |
| 261 | if (LO.ObjC1) |
| 262 | RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 264 | // Create new compile unit. |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 265 | DBuilder.CreateCompileUnit( |
Devang Patel | 5811500 | 2010-07-27 20:49:59 +0000 | [diff] [blame] | 266 | LangTag, Filename, getCurrentDirname(), |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 267 | Producer, |
Daniel Dunbar | f2d8b9f | 2009-12-18 02:43:17 +0000 | [diff] [blame] | 268 | LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 269 | // FIXME - Eliminate TheCU. |
| 270 | TheCU = llvm::DICompileUnit(DBuilder.getCU()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 273 | /// CreateType - Get the Basic type from the cache or create a new |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 274 | /// one if necessary. |
Devang Patel | f1d1d9a | 2010-11-01 16:52:40 +0000 | [diff] [blame] | 275 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 276 | unsigned Encoding = 0; |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 277 | const char *BTName = NULL; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 278 | switch (BT->getKind()) { |
| 279 | default: |
| 280 | case BuiltinType::Void: |
| 281 | return llvm::DIType(); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 282 | case BuiltinType::ObjCClass: |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 283 | return DBuilder.CreateStructType(TheCU, "objc_class", |
| 284 | getOrCreateMainFile(), 0, 0, 0, |
| 285 | llvm::DIDescriptor::FlagFwdDecl, |
| 286 | llvm::DIArray()); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 287 | case BuiltinType::ObjCId: { |
| 288 | // typedef struct objc_class *Class; |
| 289 | // typedef struct objc_object { |
| 290 | // Class isa; |
| 291 | // } *id; |
| 292 | |
| 293 | llvm::DIType OCTy = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 294 | DBuilder.CreateStructType(TheCU, "objc_class", |
| 295 | getOrCreateMainFile(), 0, 0, 0, |
| 296 | llvm::DIDescriptor::FlagFwdDecl, |
| 297 | llvm::DIArray()); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 298 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 299 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 300 | llvm::DIType ISATy = DBuilder.CreatePointerType(OCTy, Size); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 301 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 302 | llvm::SmallVector<llvm::Value *, 16> EltTys; |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 303 | llvm::DIType FieldTy = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 304 | DBuilder.CreateMemberType("isa", getOrCreateMainFile(), |
| 305 | 0,Size, 0, 0, 0, ISATy); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 306 | EltTys.push_back(FieldTy); |
| 307 | llvm::DIArray Elements = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 308 | DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 309 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 310 | return DBuilder.CreateStructType(TheCU, "objc_object", |
| 311 | getOrCreateMainFile(), |
| 312 | 0, 0, 0, 0, Elements); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 314 | case BuiltinType::UChar: |
| 315 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 316 | case BuiltinType::Char_S: |
| 317 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
| 318 | case BuiltinType::UShort: |
| 319 | case BuiltinType::UInt: |
| 320 | case BuiltinType::ULong: |
| 321 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 322 | case BuiltinType::Short: |
| 323 | case BuiltinType::Int: |
| 324 | case BuiltinType::Long: |
| 325 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 326 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
| 327 | case BuiltinType::Float: |
Devang Patel | 7c173cb | 2009-10-12 22:28:31 +0000 | [diff] [blame] | 328 | case BuiltinType::LongDouble: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 329 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | } |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 331 | |
| 332 | switch (BT->getKind()) { |
| 333 | case BuiltinType::Long: BTName = "long int"; break; |
| 334 | case BuiltinType::LongLong: BTName = "long long int"; break; |
| 335 | case BuiltinType::ULong: BTName = "long unsigned int"; break; |
| 336 | case BuiltinType::ULongLong: BTName = "long long unsigned int"; break; |
| 337 | default: |
| 338 | BTName = BT->getName(CGM.getContext().getLangOptions()); |
| 339 | break; |
| 340 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 341 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 342 | uint64_t Size = CGM.getContext().getTypeSize(BT); |
| 343 | uint64_t Align = CGM.getContext().getTypeAlign(BT); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 344 | llvm::DIType DbgTy = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 345 | DBuilder.CreateBasicType(BTName, Size, Align, Encoding); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 346 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 347 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 348 | |
Devang Patel | 344ff5d | 2010-12-09 00:25:29 +0000 | [diff] [blame] | 349 | llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) { |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 350 | // Bit size, align and offset of the type. |
| 351 | unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; |
| 352 | if (Ty->isComplexIntegerType()) |
| 353 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 355 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 356 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 357 | llvm::DIType DbgTy = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 358 | DBuilder.CreateBasicType("complex", Size, Align, Encoding); |
| 359 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 360 | return DbgTy; |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 361 | } |
| 362 | |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 363 | /// CreateCVRType - Get the qualified type from the cache or create |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 364 | /// a new one if necessary. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 365 | llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 366 | QualifierCollector Qc; |
| 367 | const Type *T = Qc.strip(Ty); |
| 368 | |
| 369 | // Ignore these qualifiers for now. |
| 370 | Qc.removeObjCGCAttr(); |
| 371 | Qc.removeAddressSpace(); |
| 372 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 373 | // We will create one Derived type for one qualifier and recurse to handle any |
| 374 | // additional ones. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 375 | unsigned Tag; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 376 | if (Qc.hasConst()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 377 | Tag = llvm::dwarf::DW_TAG_const_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 378 | Qc.removeConst(); |
| 379 | } else if (Qc.hasVolatile()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 380 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 381 | Qc.removeVolatile(); |
| 382 | } else if (Qc.hasRestrict()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 383 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 384 | Qc.removeRestrict(); |
| 385 | } else { |
| 386 | assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| 387 | return getOrCreateType(QualType(T, 0), Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 388 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 390 | llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 391 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame] | 392 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 393 | // CVR derived types. |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 394 | llvm::DIType DbgTy = DBuilder.CreateQualifiedType(Tag, FromTy); |
| 395 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 396 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 399 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 400 | llvm::DIFile Unit) { |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 401 | llvm::DIType DbgTy = |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 402 | CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 403 | Ty->getPointeeType(), Unit); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 404 | return DbgTy; |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 407 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 408 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 409 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 410 | Ty->getPointeeType(), Unit); |
| 411 | } |
| 412 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 413 | /// CreatePointeeType - Create PointTee type. If Pointee is a record |
| 414 | /// then emit record's fwd if debug info size reduction is enabled. |
| 415 | llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy, |
| 416 | llvm::DIFile Unit) { |
| 417 | if (!CGM.getCodeGenOpts().LimitDebugInfo) |
| 418 | return getOrCreateType(PointeeTy, Unit); |
| 419 | |
| 420 | if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) { |
| 421 | RecordDecl *RD = RTy->getDecl(); |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 422 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 423 | unsigned Line = getLineNumber(RD->getLocation()); |
| 424 | llvm::DIDescriptor FDContext = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 425 | getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext())); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 426 | |
| 427 | if (RD->isStruct()) |
| 428 | return DBuilder.CreateStructType(FDContext, RD->getName(), DefUnit, |
| 429 | Line, 0, 0, llvm::DIType::FlagFwdDecl, |
| 430 | llvm::DIArray()); |
| 431 | else if (RD->isUnion()) |
| 432 | return DBuilder.CreateUnionType(FDContext, RD->getName(), DefUnit, |
| 433 | Line, 0, 0, llvm::DIType::FlagFwdDecl, |
| 434 | llvm::DIArray()); |
| 435 | else { |
| 436 | assert(RD->isClass() && "Unknown RecordType!"); |
| 437 | return DBuilder.CreateClassType(FDContext, RD->getName(), DefUnit, |
| 438 | Line, 0, 0, 0, llvm::DIType::FlagFwdDecl, |
| 439 | llvm::DIType(), llvm::DIArray()); |
| 440 | } |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 441 | } |
| 442 | return getOrCreateType(PointeeTy, Unit); |
| 443 | |
| 444 | } |
| 445 | |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 446 | llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, |
| 447 | const Type *Ty, |
| 448 | QualType PointeeTy, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 449 | llvm::DIFile Unit) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 450 | |
| 451 | if (Tag == llvm::dwarf::DW_TAG_reference_type) |
| 452 | return DBuilder.CreateReferenceType(CreatePointeeType(PointeeTy, Unit)); |
| 453 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 454 | // Bit size, align and offset of the type. |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 455 | // Size is always the size of a pointer. We can't use getTypeSize here |
| 456 | // because that does not return the correct value for references. |
| 457 | uint64_t Size = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 458 | CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace()); |
| 459 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 461 | return |
| 462 | DBuilder.CreatePointerType(CreatePointeeType(PointeeTy, Unit), Size, Align); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 465 | llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 466 | llvm::DIFile Unit) { |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 467 | if (BlockLiteralGenericSet) |
| 468 | return BlockLiteralGeneric; |
| 469 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 470 | llvm::SmallVector<llvm::Value *, 8> EltTys; |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 471 | llvm::DIType FieldTy; |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 472 | QualType FType; |
| 473 | uint64_t FieldSize, FieldOffset; |
| 474 | unsigned FieldAlign; |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 475 | llvm::DIArray Elements; |
| 476 | llvm::DIType EltTy, DescTy; |
| 477 | |
| 478 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 479 | FType = CGM.getContext().UnsignedLongTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 480 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| 481 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 482 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 483 | Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 484 | EltTys.clear(); |
| 485 | |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 486 | unsigned Flags = llvm::DIDescriptor::FlagAppleBlock; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 487 | unsigned LineNo = getLineNumber(CurLoc); |
Mike Stump | 3d363c5 | 2009-10-02 02:30:50 +0000 | [diff] [blame] | 488 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 489 | EltTy = DBuilder.CreateStructType(Unit, "__block_descriptor", |
| 490 | Unit, LineNo, FieldOffset, 0, |
| 491 | Flags, Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 493 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 494 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 496 | DescTy = DBuilder.CreatePointerType(EltTy, Size); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 497 | |
| 498 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 499 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 500 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 501 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 502 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 503 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
Benjamin Kramer | d3651cc | 2010-04-24 20:26:20 +0000 | [diff] [blame] | 504 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 505 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 506 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 507 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 508 | FieldTy = DescTy; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 509 | FieldSize = CGM.getContext().getTypeSize(Ty); |
| 510 | FieldAlign = CGM.getContext().getTypeAlign(Ty); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 511 | FieldTy = DBuilder.CreateMemberType("__descriptor", Unit, |
| 512 | LineNo, FieldSize, FieldAlign, |
| 513 | FieldOffset, 0, FieldTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 514 | EltTys.push_back(FieldTy); |
| 515 | |
| 516 | FieldOffset += FieldSize; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 517 | Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 518 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 519 | EltTy = DBuilder.CreateStructType(Unit, "__block_literal_generic", |
| 520 | Unit, LineNo, FieldOffset, 0, |
| 521 | Flags, Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 523 | BlockLiteralGenericSet = true; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 524 | BlockLiteralGeneric = DBuilder.CreatePointerType(EltTy, Size); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 525 | return BlockLiteralGeneric; |
| 526 | } |
| 527 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 528 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 529 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 530 | // Typedefs are derived from some other type. If we have a typedef of a |
| 531 | // typedef, make sure to emit the whole chain. |
| 532 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 533 | if (!Src.Verify()) |
| 534 | return llvm::DIType(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 535 | // We don't set size information, but do specify where the typedef was |
| 536 | // declared. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 537 | unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 538 | llvm::DIType DbgTy = DBuilder.CreateTypedef(Src, Ty->getDecl()->getName(), |
| 539 | Unit, Line); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 540 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 543 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 544 | llvm::DIFile Unit) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 545 | llvm::SmallVector<llvm::Value *, 16> EltTys; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 547 | // Add the result type at least. |
| 548 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 550 | // Set up remainder of arguments if there is a prototype. |
| 551 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
Devang Patel | af164bb | 2010-10-06 20:51:45 +0000 | [diff] [blame] | 552 | if (isa<FunctionNoProtoType>(Ty)) |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 553 | EltTys.push_back(DBuilder.CreateUnspecifiedParameter()); |
Devang Patel | af164bb | 2010-10-06 20:51:45 +0000 | [diff] [blame] | 554 | else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 555 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 556 | EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 559 | llvm::DIArray EltTypeArray = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 560 | DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 562 | llvm::DIType DbgTy = DBuilder.CreateSubroutineType(Unit, EltTypeArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 563 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 566 | /// CollectRecordFields - A helper function to collect debug info for |
| 567 | /// record fields. This is used while creating debug info entry for a Record. |
| 568 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 569 | CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 570 | llvm::SmallVectorImpl<llvm::Value *> &EltTys) { |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 571 | unsigned FieldNo = 0; |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 572 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 573 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 574 | E = RD->field_end(); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 575 | I != E; ++I, ++FieldNo) { |
| 576 | FieldDecl *Field = *I; |
| 577 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 578 | llvm::StringRef FieldName = Field->getName(); |
| 579 | |
Devang Patel | 4835fdd | 2010-02-12 01:31:06 +0000 | [diff] [blame] | 580 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 581 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 582 | continue; |
| 583 | |
| 584 | // Get the location for the field. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 585 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 586 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 587 | QualType FType = Field->getType(); |
| 588 | uint64_t FieldSize = 0; |
| 589 | unsigned FieldAlign = 0; |
| 590 | if (!FType->isIncompleteArrayType()) { |
| 591 | |
| 592 | // Bit size, align and offset of the type. |
| 593 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 594 | Expr *BitWidth = Field->getBitWidth(); |
| 595 | if (BitWidth) |
| 596 | FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 597 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
| 598 | } |
| 599 | |
| 600 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 601 | |
Devang Patel | 71f4ff6 | 2010-04-21 23:12:37 +0000 | [diff] [blame] | 602 | unsigned Flags = 0; |
| 603 | AccessSpecifier Access = I->getAccess(); |
| 604 | if (Access == clang::AS_private) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 605 | Flags |= llvm::DIDescriptor::FlagPrivate; |
Devang Patel | 71f4ff6 | 2010-04-21 23:12:37 +0000 | [diff] [blame] | 606 | else if (Access == clang::AS_protected) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 607 | Flags |= llvm::DIDescriptor::FlagProtected; |
Devang Patel | 71f4ff6 | 2010-04-21 23:12:37 +0000 | [diff] [blame] | 608 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 609 | FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit, |
| 610 | FieldLine, FieldSize, FieldAlign, |
| 611 | FieldOffset, Flags, FieldTy); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 612 | EltTys.push_back(FieldTy); |
| 613 | } |
| 614 | } |
| 615 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 616 | /// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This |
| 617 | /// function type is not updated to include implicit "this" pointer. Use this |
| 618 | /// routine to get a method type which includes "this" pointer. |
| 619 | llvm::DIType |
| 620 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 621 | llvm::DIFile Unit) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 622 | llvm::DIType FnTy |
| 623 | = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(), |
| 624 | 0), |
| 625 | Unit); |
Devang Patel | d774d1e | 2010-01-28 21:43:50 +0000 | [diff] [blame] | 626 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 627 | // Add "this" pointer. |
| 628 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 629 | llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray(); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 630 | assert (Args.getNumElements() && "Invalid number of arguments!"); |
| 631 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 632 | llvm::SmallVector<llvm::Value *, 16> Elts; |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 633 | |
| 634 | // First element is always return type. For 'void' functions it is NULL. |
| 635 | Elts.push_back(Args.getElement(0)); |
| 636 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 637 | if (!Method->isStatic()) |
| 638 | { |
| 639 | // "this" pointer is always first argument. |
| 640 | ASTContext &Context = CGM.getContext(); |
| 641 | QualType ThisPtr = |
| 642 | Context.getPointerType(Context.getTagDeclType(Method->getParent())); |
| 643 | llvm::DIType ThisPtrType = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 644 | DBuilder.CreateArtificialType(getOrCreateType(ThisPtr, Unit)); |
Devang Patel | 769640e | 2010-07-13 00:24:30 +0000 | [diff] [blame] | 645 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 646 | TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; |
| 647 | Elts.push_back(ThisPtrType); |
| 648 | } |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 649 | |
| 650 | // Copy rest of the arguments. |
| 651 | for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) |
| 652 | Elts.push_back(Args.getElement(i)); |
| 653 | |
| 654 | llvm::DIArray EltTypeArray = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 655 | DBuilder.GetOrCreateArray(Elts.data(), Elts.size()); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 656 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 657 | return DBuilder.CreateSubroutineType(Unit, EltTypeArray); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 660 | /// isFunctionLocalClass - Return true if CXXRecordDecl is defined |
| 661 | /// inside a function. |
| 662 | static bool isFunctionLocalClass(const CXXRecordDecl *RD) { |
| 663 | if (const CXXRecordDecl *NRD = |
| 664 | dyn_cast<CXXRecordDecl>(RD->getDeclContext())) |
| 665 | return isFunctionLocalClass(NRD); |
| 666 | else if (isa<FunctionDecl>(RD->getDeclContext())) |
| 667 | return true; |
| 668 | return false; |
| 669 | |
| 670 | } |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 671 | /// CreateCXXMemberFunction - A helper function to create a DISubprogram for |
| 672 | /// a single member function GlobalDecl. |
| 673 | llvm::DISubprogram |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 674 | CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 675 | llvm::DIFile Unit, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 676 | llvm::DIType RecordTy) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 677 | bool IsCtorOrDtor = |
| 678 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
| 679 | |
| 680 | llvm::StringRef MethodName = getFunctionName(Method); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 681 | llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 682 | |
| 683 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| 684 | // make sense to give a single ctor/dtor a linkage name. |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 685 | llvm::StringRef MethodLinkageName; |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 686 | if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 687 | MethodLinkageName = CGM.getMangledName(Method); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 688 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 689 | // Get the location for the method. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 690 | llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| 691 | unsigned MethodLine = getLineNumber(Method->getLocation()); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 692 | |
| 693 | // Collect virtual method info. |
| 694 | llvm::DIType ContainingType; |
| 695 | unsigned Virtuality = 0; |
| 696 | unsigned VIndex = 0; |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 697 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 698 | if (Method->isVirtual()) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 699 | if (Method->isPure()) |
| 700 | Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| 701 | else |
| 702 | Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
| 703 | |
| 704 | // It doesn't make sense to give a virtual destructor a vtable index, |
| 705 | // since a single destructor has two entries in the vtable. |
| 706 | if (!isa<CXXDestructorDecl>(Method)) |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 707 | VIndex = CGM.getVTables().getMethodVTableIndex(Method); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 708 | ContainingType = RecordTy; |
| 709 | } |
| 710 | |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 711 | unsigned Flags = 0; |
| 712 | if (Method->isImplicit()) |
| 713 | Flags |= llvm::DIDescriptor::FlagArtificial; |
Devang Patel | 10a7a6a | 2010-09-29 21:46:16 +0000 | [diff] [blame] | 714 | AccessSpecifier Access = Method->getAccess(); |
| 715 | if (Access == clang::AS_private) |
| 716 | Flags |= llvm::DIDescriptor::FlagPrivate; |
| 717 | else if (Access == clang::AS_protected) |
| 718 | Flags |= llvm::DIDescriptor::FlagProtected; |
Devang Patel | d78a019 | 2010-10-01 23:32:17 +0000 | [diff] [blame] | 719 | if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { |
| 720 | if (CXXC->isExplicit()) |
| 721 | Flags |= llvm::DIDescriptor::FlagExplicit; |
| 722 | } else if (const CXXConversionDecl *CXXC = |
| 723 | dyn_cast<CXXConversionDecl>(Method)) { |
| 724 | if (CXXC->isExplicit()) |
| 725 | Flags |= llvm::DIDescriptor::FlagExplicit; |
| 726 | } |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 727 | if (Method->hasPrototype()) |
| 728 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | d78a019 | 2010-10-01 23:32:17 +0000 | [diff] [blame] | 729 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 730 | llvm::DISubprogram SP = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 731 | DBuilder.CreateMethod(RecordTy , MethodName, MethodLinkageName, |
| 732 | MethodDefUnit, MethodLine, |
| 733 | MethodTy, /*isLocalToUnit=*/false, |
| 734 | /* isDefinition=*/ false, |
| 735 | Virtuality, VIndex, ContainingType, |
| 736 | Flags, CGM.getLangOptions().Optimize); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 737 | |
| 738 | // Don't cache ctors or dtors since we have to emit multiple functions for |
| 739 | // a single ctor or dtor. |
| 740 | if (!IsCtorOrDtor && Method->isThisDeclarationADefinition()) |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 741 | SPCache[Method] = llvm::WeakVH(SP); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 742 | |
| 743 | return SP; |
| 744 | } |
| 745 | |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 746 | /// CollectCXXMemberFunctions - A helper function to collect debug info for |
| 747 | /// C++ member functions.This is used while creating debug info entry for |
| 748 | /// a Record. |
| 749 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 750 | CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 751 | llvm::SmallVectorImpl<llvm::Value *> &EltTys, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 752 | llvm::DIType RecordTy) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 753 | for(CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 754 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 755 | const CXXMethodDecl *Method = *I; |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 756 | |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 757 | if (Method->isImplicit() && !Method->isUsed()) |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 758 | continue; |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 759 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 760 | EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 764 | /// CollectCXXFriends - A helper function to collect debug info for |
| 765 | /// C++ base classes. This is used while creating debug info entry for |
| 766 | /// a Record. |
| 767 | void CGDebugInfo:: |
| 768 | CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 769 | llvm::SmallVectorImpl<llvm::Value *> &EltTys, |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 770 | llvm::DIType RecordTy) { |
| 771 | |
| 772 | for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(), |
| 773 | BE = RD->friend_end(); BI != BE; ++BI) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 774 | if (TypeSourceInfo *TInfo = (*BI)->getFriendType()) |
| 775 | EltTys.push_back(DBuilder.CreateFriend(RecordTy, |
| 776 | getOrCreateType(TInfo->getType(), |
| 777 | Unit))); |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 781 | /// CollectCXXBases - A helper function to collect debug info for |
| 782 | /// C++ base classes. This is used while creating debug info entry for |
| 783 | /// a Record. |
| 784 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 785 | CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 786 | llvm::SmallVectorImpl<llvm::Value *> &EltTys, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 787 | llvm::DIType RecordTy) { |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 788 | |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 789 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 790 | for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), |
| 791 | BE = RD->bases_end(); BI != BE; ++BI) { |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 792 | unsigned BFlags = 0; |
| 793 | uint64_t BaseOffset; |
| 794 | |
| 795 | const CXXRecordDecl *Base = |
| 796 | cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); |
| 797 | |
| 798 | if (BI->isVirtual()) { |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 799 | // virtual base offset offset is -ve. The code generator emits dwarf |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 800 | // expression where it expects +ve number. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 801 | BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base); |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 802 | BFlags = llvm::DIDescriptor::FlagVirtual; |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 803 | } else |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 804 | BaseOffset = RL.getBaseClassOffsetInBits(Base); |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 805 | |
| 806 | AccessSpecifier Access = BI->getAccessSpecifier(); |
| 807 | if (Access == clang::AS_private) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 808 | BFlags |= llvm::DIDescriptor::FlagPrivate; |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 809 | else if (Access == clang::AS_protected) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 810 | BFlags |= llvm::DIDescriptor::FlagProtected; |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 811 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 812 | llvm::DIType DTy = |
| 813 | DBuilder.CreateInheritance(RecordTy, |
| 814 | getOrCreateType(BI->getType(), Unit), |
| 815 | BaseOffset, BFlags); |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 816 | EltTys.push_back(DTy); |
| 817 | } |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 820 | /// getOrCreateVTablePtrType - Return debug info descriptor for vtable. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 821 | llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { |
Devang Patel | 0804e6e | 2010-03-08 20:53:17 +0000 | [diff] [blame] | 822 | if (VTablePtrType.isValid()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 823 | return VTablePtrType; |
| 824 | |
| 825 | ASTContext &Context = CGM.getContext(); |
| 826 | |
| 827 | /* Function type */ |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 828 | llvm::Value *STy = getOrCreateType(Context.IntTy, Unit); |
| 829 | llvm::DIArray SElements = DBuilder.GetOrCreateArray(&STy, 1); |
| 830 | llvm::DIType SubTy = DBuilder.CreateSubroutineType(Unit, SElements); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 831 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 832 | llvm::DIType vtbl_ptr_type = DBuilder.CreatePointerType(SubTy, Size, 0, |
| 833 | "__vtbl_ptr_type"); |
| 834 | VTablePtrType = DBuilder.CreatePointerType(vtbl_ptr_type, Size); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 835 | return VTablePtrType; |
| 836 | } |
| 837 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 838 | /// getVTableName - Get vtable name for the given Class. |
| 839 | llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 840 | // Otherwise construct gdb compatible name name. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 841 | std::string Name = "_vptr$" + RD->getNameAsString(); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 842 | |
| 843 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 844 | char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 845 | memcpy(StrPtr, Name.data(), Name.length()); |
| 846 | return llvm::StringRef(StrPtr, Name.length()); |
| 847 | } |
| 848 | |
| 849 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 850 | /// CollectVTableInfo - If the C++ class has vtable info then insert appropriate |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 851 | /// debug info entry in EltTys vector. |
| 852 | void CGDebugInfo:: |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 853 | CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 854 | llvm::SmallVectorImpl<llvm::Value *> &EltTys) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 855 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 856 | |
| 857 | // If there is a primary base then it will hold vtable info. |
| 858 | if (RL.getPrimaryBase()) |
| 859 | return; |
| 860 | |
| 861 | // 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] | 862 | if (!RD->isDynamicClass()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 863 | return; |
| 864 | |
| 865 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 866 | llvm::DIType VPTR |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 867 | = DBuilder.CreateMemberType(getVTableName(RD), Unit, |
| 868 | 0, Size, 0, 0, 0, |
| 869 | getOrCreateVTablePtrType(Unit)); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 870 | EltTys.push_back(VPTR); |
| 871 | } |
| 872 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 873 | /// getOrCreateRecordType - Emit record type's standalone debug info. |
| 874 | llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy, |
| 875 | SourceLocation Loc) { |
| 876 | llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc)); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 877 | DBuilder.RetainType(T); |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 878 | return T; |
| 879 | } |
| 880 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 881 | /// CreateType - get structure or union type. |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 882 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 883 | RecordDecl *RD = Ty->getDecl(); |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 884 | llvm::DIFile Unit = getOrCreateFile(RD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 886 | // Get overall information about the record type for the debug info. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 887 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 888 | unsigned Line = getLineNumber(RD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 890 | // Records and classes and unions can all be recursive. To handle them, we |
| 891 | // first generate a debug descriptor for the struct as a forward declaration. |
| 892 | // Then (if it is a definition) we go through and get debug info for all of |
| 893 | // its members. Finally, we create a descriptor for the complete type (which |
| 894 | // may refer to the forward decl if the struct is recursive) and replace all |
| 895 | // uses of the forward declaration with the final definition. |
Devang Patel | 0b89799 | 2010-07-08 19:56:29 +0000 | [diff] [blame] | 896 | llvm::DIDescriptor FDContext = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 897 | getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext())); |
Devang Patel | 0b89799 | 2010-07-08 19:56:29 +0000 | [diff] [blame] | 898 | |
| 899 | // If this is just a forward declaration, construct an appropriately |
| 900 | // marked node and just return it. |
| 901 | if (!RD->getDefinition()) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 902 | llvm::DIType FwdDecl = |
| 903 | DBuilder.CreateStructType(FDContext, RD->getName(), |
| 904 | DefUnit, Line, 0, 0, |
| 905 | llvm::DIDescriptor::FlagFwdDecl, |
| 906 | llvm::DIArray()); |
Devang Patel | 0b89799 | 2010-07-08 19:56:29 +0000 | [diff] [blame] | 907 | |
| 908 | return FwdDecl; |
| 909 | } |
Devang Patel | d0f251b | 2010-01-20 23:56:40 +0000 | [diff] [blame] | 910 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 911 | llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 913 | llvm::MDNode *MN = FwdDecl; |
| 914 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 915 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 916 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 917 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 918 | // Push the struct on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 919 | RegionStack.push_back(FwdDeclNode); |
| 920 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 921 | |
| 922 | // Convert all the elements. |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 923 | llvm::SmallVector<llvm::Value *, 16> EltTys; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 924 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 925 | const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 926 | if (CXXDecl) { |
| 927 | CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 928 | CollectVTableInfo(CXXDecl, Unit, EltTys); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 929 | } |
Devang Patel | dabc3e9 | 2010-08-12 00:02:44 +0000 | [diff] [blame] | 930 | |
| 931 | // Collect static variables with initializers. |
| 932 | for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end(); |
| 933 | I != E; ++I) |
| 934 | if (const VarDecl *V = dyn_cast<VarDecl>(*I)) { |
| 935 | if (const Expr *Init = V->getInit()) { |
| 936 | Expr::EvalResult Result; |
| 937 | if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) { |
| 938 | llvm::ConstantInt *CI |
| 939 | = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt()); |
| 940 | |
| 941 | // Create the descriptor for static variable. |
| 942 | llvm::DIFile VUnit = getOrCreateFile(V->getLocation()); |
| 943 | llvm::StringRef VName = V->getName(); |
| 944 | llvm::DIType VTy = getOrCreateType(V->getType(), VUnit); |
| 945 | // Do not use DIGlobalVariable for enums. |
| 946 | if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 947 | DBuilder.CreateStaticVariable(FwdDecl, VName, VName, VUnit, |
| 948 | getLineNumber(V->getLocation()), |
| 949 | VTy, true, CI); |
Devang Patel | dabc3e9 | 2010-08-12 00:02:44 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 955 | CollectRecordFields(RD, Unit, EltTys); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 956 | if (CXXDecl) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 957 | CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 958 | CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 959 | } |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 960 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 961 | RegionStack.pop_back(); |
| 962 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 963 | RegionMap.find(Ty->getDecl()); |
| 964 | if (RI != RegionMap.end()) |
| 965 | RegionMap.erase(RI); |
| 966 | |
| 967 | llvm::DIDescriptor RDContext = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 968 | getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext())); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 969 | llvm::StringRef RDName = RD->getName(); |
| 970 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 971 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 972 | llvm::DIArray Elements = |
| 973 | DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); |
| 974 | llvm::MDNode *RealDecl = NULL; |
| 975 | |
| 976 | if (RD->isStruct()) |
| 977 | RealDecl = DBuilder.CreateStructType(RDContext, RDName, DefUnit, Line, |
| 978 | Size, Align, 0, Elements); |
| 979 | else if (RD->isUnion()) |
| 980 | RealDecl = DBuilder.CreateUnionType(RDContext, RDName, DefUnit, Line, |
| 981 | Size, Align, 0, Elements); |
| 982 | else { |
| 983 | assert(RD->isClass() && "Unknown RecordType!"); |
| 984 | RDName = getClassName(RD); |
| 985 | // A class's primary base or the class itself contains the vtable. |
| 986 | llvm::MDNode *ContainingType = NULL; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 987 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 5bc794f | 2010-10-14 22:59:23 +0000 | [diff] [blame] | 988 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { |
| 989 | // Seek non virtual primary base root. |
| 990 | while (1) { |
| 991 | const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); |
| 992 | const CXXRecordDecl *PBT = BRL.getPrimaryBase(); |
Anders Carlsson | c9e814b | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 993 | if (PBT && !BRL.isPrimaryBaseVirtual()) |
Devang Patel | 5bc794f | 2010-10-14 22:59:23 +0000 | [diff] [blame] | 994 | PBase = PBT; |
| 995 | else |
| 996 | break; |
| 997 | } |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 998 | ContainingType = |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 999 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit); |
Devang Patel | 5bc794f | 2010-10-14 22:59:23 +0000 | [diff] [blame] | 1000 | } |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 1001 | else if (CXXDecl->isDynamicClass()) |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1002 | ContainingType = FwdDecl; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1003 | RealDecl = DBuilder.CreateClassType(RDContext, RDName, DefUnit, Line, |
| 1004 | Size, Align, 0, 0, llvm::DIType(), |
| 1005 | Elements, ContainingType); |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 1006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1008 | // Now that we have a real decl for the struct, replace anything using the |
| 1009 | // 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] | 1010 | llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1011 | RegionMap[RD] = llvm::WeakVH(RealDecl); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1012 | return llvm::DIType(RealDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1015 | /// CreateType - get objective-c object type. |
| 1016 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty, |
| 1017 | llvm::DIFile Unit) { |
| 1018 | // Ignore protocols. |
| 1019 | return getOrCreateType(Ty->getBaseType(), Unit); |
| 1020 | } |
| 1021 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1022 | /// CreateType - get objective-c interface type. |
| 1023 | llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1024 | llvm::DIFile Unit) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1025 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
Douglas Gregor | a6a2897 | 2010-11-30 06:38:09 +0000 | [diff] [blame] | 1026 | if (!ID) |
| 1027 | return llvm::DIType(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1028 | |
| 1029 | // Get overall information about the record type for the debug info. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1030 | llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1031 | unsigned Line = getLineNumber(ID->getLocation()); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1032 | unsigned RuntimeLang = TheCU.getLanguage(); |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 1033 | |
Dan Gohman | 45f7c78 | 2010-08-23 21:15:56 +0000 | [diff] [blame] | 1034 | // If this is just a forward declaration, return a special forward-declaration |
| 1035 | // debug type. |
| 1036 | if (ID->isForwardDecl()) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1037 | llvm::DIType FwdDecl = |
| 1038 | DBuilder.CreateStructType(Unit, ID->getName(), |
| 1039 | DefUnit, Line, 0, 0, 0, |
| 1040 | llvm::DIArray(), RuntimeLang); |
Dan Gohman | 45f7c78 | 2010-08-23 21:15:56 +0000 | [diff] [blame] | 1041 | return FwdDecl; |
| 1042 | } |
| 1043 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1044 | // To handle recursive interface, 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 | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1050 | llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1052 | llvm::MDNode *MN = FwdDecl; |
| 1053 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1054 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 1055 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1056 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 1057 | // Push the struct on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1058 | RegionStack.push_back(FwdDeclNode); |
| 1059 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1060 | |
| 1061 | // Convert all the elements. |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1062 | llvm::SmallVector<llvm::Value *, 16> EltTys; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1063 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1064 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 1065 | if (SClass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | llvm::DIType SClassTy = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1067 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
Douglas Gregor | a6a2897 | 2010-11-30 06:38:09 +0000 | [diff] [blame] | 1068 | if (!SClassTy.isValid()) |
| 1069 | return llvm::DIType(); |
| 1070 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | llvm::DIType InhTag = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1072 | DBuilder.CreateInheritance(FwdDecl, SClassTy, 0, 0); |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 1073 | EltTys.push_back(InhTag); |
| 1074 | } |
| 1075 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1076 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1077 | |
| 1078 | unsigned FieldNo = 0; |
Fariborz Jahanian | 9747739 | 2010-10-01 00:01:53 +0000 | [diff] [blame] | 1079 | for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; |
Fariborz Jahanian | fe8fdba | 2010-10-11 23:55:47 +0000 | [diff] [blame] | 1080 | Field = Field->getNextIvar(), ++FieldNo) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1081 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Douglas Gregor | a6a2897 | 2010-11-30 06:38:09 +0000 | [diff] [blame] | 1082 | if (!FieldTy.isValid()) |
| 1083 | return llvm::DIType(); |
| 1084 | |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 1085 | llvm::StringRef FieldName = Field->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1086 | |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 1087 | // Ignore unnamed fields. |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 1088 | if (FieldName.empty()) |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 1089 | continue; |
| 1090 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1091 | // Get the location for the field. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1092 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 1093 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1094 | QualType FType = Field->getType(); |
| 1095 | uint64_t FieldSize = 0; |
| 1096 | unsigned FieldAlign = 0; |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1097 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1098 | if (!FType->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1100 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1101 | FieldSize = CGM.getContext().getTypeSize(FType); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1102 | Expr *BitWidth = Field->getBitWidth(); |
| 1103 | if (BitWidth) |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1104 | FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 1105 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1106 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 1110 | |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1111 | unsigned Flags = 0; |
| 1112 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1113 | Flags = llvm::DIDescriptor::FlagProtected; |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1114 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1115 | Flags = llvm::DIDescriptor::FlagPrivate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1117 | FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit, |
| 1118 | FieldLine, FieldSize, FieldAlign, |
| 1119 | FieldOffset, Flags, FieldTy); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1120 | EltTys.push_back(FieldTy); |
| 1121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1123 | llvm::DIArray Elements = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1124 | DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1125 | |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 1126 | RegionStack.pop_back(); |
| 1127 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 1128 | RegionMap.find(Ty->getDecl()); |
| 1129 | if (RI != RegionMap.end()) |
| 1130 | RegionMap.erase(RI); |
| 1131 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1132 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1133 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1134 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1136 | llvm::DIType RealDecl = |
| 1137 | DBuilder.CreateStructType(Unit, ID->getName(), DefUnit, |
| 1138 | Line, Size, Align, 0, |
| 1139 | Elements, RuntimeLang); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Now that we have a real decl for the struct, replace anything using the |
| 1142 | // 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] | 1143 | llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1144 | RegionMap[ID] = llvm::WeakVH(RealDecl); |
Devang Patel | fe09eab | 2009-07-13 17:03:14 +0000 | [diff] [blame] | 1145 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1146 | return RealDecl; |
| 1147 | } |
| 1148 | |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1149 | llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1150 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1151 | return CreateType(RT); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1152 | else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1153 | return CreateEnumType(ET->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1155 | return llvm::DIType(); |
| 1156 | } |
| 1157 | |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1158 | llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1159 | llvm::DIFile Unit) { |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1160 | llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
| 1161 | uint64_t NumElems = Ty->getNumElements(); |
| 1162 | if (NumElems > 0) |
| 1163 | --NumElems; |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1164 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1165 | llvm::Value *Subscript = DBuilder.GetOrCreateSubrange(0, NumElems); |
| 1166 | llvm::DIArray SubscriptArray = DBuilder.GetOrCreateArray(&Subscript, 1); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1167 | |
| 1168 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1169 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 1170 | |
| 1171 | return |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1172 | DBuilder.CreateVectorType(Size, Align, ElementTy, SubscriptArray); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1175 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1176 | llvm::DIFile Unit) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1177 | uint64_t Size; |
| 1178 | uint64_t Align; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
| 1180 | |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1181 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1182 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1183 | Size = 0; |
| 1184 | Align = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1185 | CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1186 | } else if (Ty->isIncompleteArrayType()) { |
| 1187 | Size = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1188 | Align = CGM.getContext().getTypeAlign(Ty->getElementType()); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1189 | } else { |
| 1190 | // Size and align of the whole array, not the element type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1191 | Size = CGM.getContext().getTypeSize(Ty); |
| 1192 | Align = CGM.getContext().getTypeAlign(Ty); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1193 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1195 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 1196 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 1197 | // obvious/recursive way? |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1198 | llvm::SmallVector<llvm::Value *, 8> Subscripts; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1199 | QualType EltTy(Ty, 0); |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1200 | if (Ty->isIncompleteArrayType()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1201 | EltTy = Ty->getElementType(); |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1202 | else { |
| 1203 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
| 1204 | uint64_t Upper = 0; |
| 1205 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) |
| 1206 | if (CAT->getSize().getZExtValue()) |
| 1207 | Upper = CAT->getSize().getZExtValue() - 1; |
| 1208 | // FIXME: Verify this is right for VLAs. |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1209 | Subscripts.push_back(DBuilder.GetOrCreateSubrange(0, Upper)); |
Devang Patel | cdf523c | 2010-10-06 18:30:00 +0000 | [diff] [blame] | 1210 | EltTy = Ty->getElementType(); |
| 1211 | } |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 1212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1214 | llvm::DIArray SubscriptArray = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1215 | DBuilder.GetOrCreateArray(Subscripts.data(), Subscripts.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1216 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1217 | llvm::DIType DbgTy = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1218 | DBuilder.CreateArrayType(Size, Align, getOrCreateType(EltTy, Unit), |
| 1219 | SubscriptArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1220 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1223 | llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1224 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1225 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, |
| 1226 | Ty, Ty->getPointeeType(), Unit); |
| 1227 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1228 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1229 | llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1230 | llvm::DIFile U) { |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1231 | QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); |
| 1232 | llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); |
| 1233 | |
| 1234 | if (!Ty->getPointeeType()->isFunctionType()) { |
| 1235 | // We have a data member pointer type. |
| 1236 | return PointerDiffDITy; |
| 1237 | } |
| 1238 | |
| 1239 | // We have a member function pointer type. Treat it as a struct with two |
| 1240 | // ptrdiff_t members. |
| 1241 | std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); |
| 1242 | |
| 1243 | uint64_t FieldOffset = 0; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1244 | llvm::Value *ElementTypes[2]; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1245 | |
| 1246 | // FIXME: This should probably be a function type instead. |
| 1247 | ElementTypes[0] = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1248 | DBuilder.CreateMemberType("ptr", U, 0, |
| 1249 | Info.first, Info.second, FieldOffset, 0, |
| 1250 | PointerDiffDITy); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1251 | FieldOffset += Info.first; |
| 1252 | |
| 1253 | ElementTypes[1] = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1254 | DBuilder.CreateMemberType("ptr", U, 0, |
| 1255 | Info.first, Info.second, FieldOffset, 0, |
| 1256 | PointerDiffDITy); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1257 | |
| 1258 | llvm::DIArray Elements = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1259 | DBuilder.GetOrCreateArray(&ElementTypes[0], |
| 1260 | llvm::array_lengthof(ElementTypes)); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1261 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1262 | return DBuilder.CreateStructType(U, llvm::StringRef("test"), |
| 1263 | U, 0, FieldOffset, |
| 1264 | 0, 0, Elements); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1267 | /// CreateEnumType - get enumeration type. |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1268 | llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) { |
| 1269 | llvm::DIFile Unit = getOrCreateFile(ED->getLocation()); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1270 | llvm::SmallVector<llvm::Value *, 16> Enumerators; |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Create DIEnumerator elements for each enumerator. |
| 1273 | for (EnumDecl::enumerator_iterator |
| 1274 | Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); |
| 1275 | Enum != EnumEnd; ++Enum) { |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1276 | Enumerators.push_back( |
| 1277 | DBuilder.CreateEnumerator(Enum->getName(), |
| 1278 | Enum->getInitVal().getZExtValue())); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | // Return a CompositeType for the enum itself. |
| 1282 | llvm::DIArray EltArray = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1283 | DBuilder.GetOrCreateArray(Enumerators.data(), Enumerators.size()); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1284 | |
| 1285 | llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); |
| 1286 | unsigned Line = getLineNumber(ED->getLocation()); |
| 1287 | uint64_t Size = 0; |
Devang Patel | ffc52e7 | 2010-08-24 18:14:06 +0000 | [diff] [blame] | 1288 | uint64_t Align = 0; |
| 1289 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 1290 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
| 1291 | Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl()); |
| 1292 | } |
Devang Patel | 4bc4887 | 2010-10-27 23:23:58 +0000 | [diff] [blame] | 1293 | llvm::DIDescriptor EnumContext = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 1294 | getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext())); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1295 | llvm::DIType DbgTy = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1296 | DBuilder.CreateEnumerationType(EnumContext, ED->getName(), DefUnit, Line, |
| 1297 | Size, Align, EltArray); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1298 | return DbgTy; |
| 1299 | } |
| 1300 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1301 | static QualType UnwrapTypeForDebugInfo(QualType T) { |
| 1302 | do { |
| 1303 | QualType LastT = T; |
| 1304 | switch (T->getTypeClass()) { |
| 1305 | default: |
| 1306 | return T; |
| 1307 | case Type::TemplateSpecialization: |
| 1308 | T = cast<TemplateSpecializationType>(T)->desugar(); |
| 1309 | break; |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1310 | case Type::TypeOfExpr: |
| 1311 | T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1312 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1313 | case Type::TypeOf: |
| 1314 | T = cast<TypeOfType>(T)->getUnderlyingType(); |
| 1315 | break; |
| 1316 | case Type::Decltype: |
| 1317 | T = cast<DecltypeType>(T)->getUnderlyingType(); |
| 1318 | break; |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1319 | case Type::Attributed: |
| 1320 | T = cast<AttributedType>(T)->getEquivalentType(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1321 | case Type::Elaborated: |
| 1322 | T = cast<ElaboratedType>(T)->getNamedType(); |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1323 | break; |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1324 | case Type::Paren: |
| 1325 | T = cast<ParenType>(T)->getInnerType(); |
| 1326 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1327 | case Type::SubstTemplateTypeParm: |
| 1328 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
| 1329 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | assert(T != LastT && "Type unwrapping failed to unwrap!"); |
| 1333 | if (T == LastT) |
| 1334 | return T; |
| 1335 | } while (true); |
| 1336 | |
| 1337 | return T; |
Anders Carlsson | 5b6117a | 2009-11-14 21:08:12 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1340 | /// getOrCreateType - Get the type from the cache or create a new |
| 1341 | /// one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1342 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1343 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1344 | if (Ty.isNull()) |
| 1345 | return llvm::DIType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1347 | // Unwrap the type as needed for debug information. |
| 1348 | Ty = UnwrapTypeForDebugInfo(Ty); |
Anders Carlsson | 5b6117a | 2009-11-14 21:08:12 +0000 | [diff] [blame] | 1349 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1350 | // Check for existing entry. |
Ted Kremenek | 590838b | 2010-03-29 18:29:57 +0000 | [diff] [blame] | 1351 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1352 | TypeCache.find(Ty.getAsOpaquePtr()); |
Daniel Dunbar | 65f13c3 | 2009-09-19 20:17:48 +0000 | [diff] [blame] | 1353 | if (it != TypeCache.end()) { |
| 1354 | // Verify that the debug info still exists. |
| 1355 | if (&*it->second) |
| 1356 | return llvm::DIType(cast<llvm::MDNode>(it->second)); |
| 1357 | } |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1358 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1359 | // Otherwise create the type. |
| 1360 | llvm::DIType Res = CreateTypeNode(Ty, Unit); |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1361 | |
| 1362 | // And update the type cache. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1363 | TypeCache[Ty.getAsOpaquePtr()] = Res; |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1364 | return Res; |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1367 | /// CreateTypeNode - Create a new debug type node. |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1368 | llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1369 | llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1370 | // Handle qualifiers, which recursively handles what they refer to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1371 | if (Ty.hasLocalQualifiers()) |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1372 | return CreateQualifiedType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1373 | |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1374 | const char *Diag = 0; |
| 1375 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1376 | // Work out details of type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1377 | switch (Ty->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1378 | #define TYPE(Class, Base) |
| 1379 | #define ABSTRACT_TYPE(Class, Base) |
| 1380 | #define NON_CANONICAL_TYPE(Class, Base) |
| 1381 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1382 | #include "clang/AST/TypeNodes.def" |
| 1383 | assert(false && "Dependent types cannot show up in debug information"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1384 | |
Anders Carlsson | bfe6995 | 2009-11-06 18:24:04 +0000 | [diff] [blame] | 1385 | // FIXME: Handle these. |
| 1386 | case Type::ExtVector: |
Anders Carlsson | bfe6995 | 2009-11-06 18:24:04 +0000 | [diff] [blame] | 1387 | return llvm::DIType(); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1388 | |
| 1389 | case Type::Vector: |
| 1390 | return CreateType(cast<VectorType>(Ty), Unit); |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 1391 | case Type::ObjCObjectPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1392 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1393 | case Type::ObjCObject: |
| 1394 | return CreateType(cast<ObjCObjectType>(Ty), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | case Type::ObjCInterface: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1396 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
Devang Patel | f1d1d9a | 2010-11-01 16:52:40 +0000 | [diff] [blame] | 1397 | case Type::Builtin: return CreateType(cast<BuiltinType>(Ty)); |
Devang Patel | 344ff5d | 2010-12-09 00:25:29 +0000 | [diff] [blame] | 1398 | case Type::Complex: return CreateType(cast<ComplexType>(Ty)); |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1399 | case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 1400 | case Type::BlockPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1401 | return CreateType(cast<BlockPointerType>(Ty), Unit); |
| 1402 | case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1403 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1404 | case Type::Enum: |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1405 | return CreateType(cast<TagType>(Ty)); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1406 | case Type::FunctionProto: |
| 1407 | case Type::FunctionNoProto: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1408 | return CreateType(cast<FunctionType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1409 | case Type::ConstantArray: |
| 1410 | case Type::VariableArray: |
| 1411 | case Type::IncompleteArray: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1412 | return CreateType(cast<ArrayType>(Ty), Unit); |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1413 | |
| 1414 | case Type::LValueReference: |
| 1415 | return CreateType(cast<LValueReferenceType>(Ty), Unit); |
| 1416 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1417 | case Type::MemberPointer: |
| 1418 | return CreateType(cast<MemberPointerType>(Ty), Unit); |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1419 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1420 | case Type::Attributed: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1421 | case Type::TemplateSpecialization: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1422 | case Type::Elaborated: |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1423 | case Type::Paren: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1424 | case Type::SubstTemplateTypeParm: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1425 | case Type::TypeOfExpr: |
| 1426 | case Type::TypeOf: |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1427 | case Type::Decltype: |
| 1428 | llvm_unreachable("type should have been unwrapped!"); |
| 1429 | return llvm::DIType(); |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1430 | |
| 1431 | case Type::RValueReference: |
| 1432 | // FIXME: Implement! |
| 1433 | Diag = "rvalue references"; |
| 1434 | break; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1435 | } |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1436 | |
| 1437 | assert(Diag && "Fall through without a diagnostic?"); |
| 1438 | unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error, |
| 1439 | "debug information for %0 is not yet supported"); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1440 | CGM.getDiags().Report(DiagID) |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1441 | << Diag; |
| 1442 | return llvm::DIType(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1445 | /// CreateMemberType - Create new member and increase Offset by FType's size. |
| 1446 | llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, |
| 1447 | llvm::StringRef Name, |
| 1448 | uint64_t *Offset) { |
| 1449 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 1450 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); |
| 1451 | unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1452 | llvm::DIType Ty = DBuilder.CreateMemberType(Name, Unit, 0, |
| 1453 | FieldSize, FieldAlign, |
| 1454 | *Offset, 0, FieldTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1455 | *Offset += FieldSize; |
| 1456 | return Ty; |
| 1457 | } |
| 1458 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1459 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 1460 | /// "llvm.dbg.func.start.". |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1461 | void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1462 | llvm::Function *Fn, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1463 | CGBuilderTy &Builder) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1465 | llvm::StringRef Name; |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 1466 | llvm::StringRef LinkageName; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1467 | |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1468 | FnBeginRegionCount.push_back(RegionStack.size()); |
| 1469 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1470 | const Decl *D = GD.getDecl(); |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1471 | unsigned Flags = 0; |
Devang Patel | 0692f83 | 2010-10-11 21:58:41 +0000 | [diff] [blame] | 1472 | llvm::DIFile Unit = getOrCreateFile(CurLoc); |
| 1473 | llvm::DIDescriptor FDContext(Unit); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1474 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1475 | // If there is a DISubprogram for this function available then use it. |
| 1476 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 1477 | FI = SPCache.find(FD); |
| 1478 | if (FI != SPCache.end()) { |
Gabor Greif | 38c9b17 | 2010-09-18 13:00:17 +0000 | [diff] [blame] | 1479 | llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1480 | if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { |
| 1481 | llvm::MDNode *SPN = SP; |
| 1482 | RegionStack.push_back(SPN); |
| 1483 | RegionMap[D] = llvm::WeakVH(SP); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1484 | return; |
| 1485 | } |
| 1486 | } |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1487 | Name = getFunctionName(FD); |
| 1488 | // Use mangled name as linkage name for c/c++ functions. |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 1489 | LinkageName = CGM.getMangledName(GD); |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 1490 | if (LinkageName == Name) |
| 1491 | LinkageName = llvm::StringRef(); |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1492 | if (FD->hasPrototype()) |
| 1493 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | 0692f83 | 2010-10-11 21:58:41 +0000 | [diff] [blame] | 1494 | if (const NamespaceDecl *NSDecl = |
| 1495 | dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 1496 | FDContext = getOrCreateNameSpace(NSDecl); |
David Chisnall | 70b9b44 | 2010-09-02 17:16:32 +0000 | [diff] [blame] | 1497 | } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
David Chisnall | 52044a2 | 2010-09-02 18:01:51 +0000 | [diff] [blame] | 1498 | Name = getObjCMethodName(OMD); |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1499 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1500 | } else { |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 1501 | // Use llvm function name. |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1502 | Name = Fn->getName(); |
Devang Patel | 3951e71 | 2010-10-07 22:03:49 +0000 | [diff] [blame] | 1503 | Flags |= llvm::DIDescriptor::FlagPrototyped; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1504 | } |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1505 | if (!Name.empty() && Name[0] == '\01') |
| 1506 | Name = Name.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Devang Patel | 970c618 | 2010-04-24 00:49:16 +0000 | [diff] [blame] | 1508 | // It is expected that CurLoc is set before using EmitFunctionStart. |
| 1509 | // Usually, CurLoc points to the left bracket location of compound |
| 1510 | // statement representing function body. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1511 | unsigned LineNo = getLineNumber(CurLoc); |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1512 | if (D->isImplicit()) |
| 1513 | Flags |= llvm::DIDescriptor::FlagArtificial; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1514 | llvm::DISubprogram SP = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1515 | DBuilder.CreateFunction(FDContext, Name, LinkageName, Unit, |
| 1516 | LineNo, getOrCreateType(FnType, Unit), |
| 1517 | Fn->hasInternalLinkage(), true/*definition*/, |
| 1518 | Flags, CGM.getLangOptions().Optimize, Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1520 | // Push function on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1521 | llvm::MDNode *SPN = SP; |
| 1522 | RegionStack.push_back(SPN); |
| 1523 | RegionMap[D] = llvm::WeakVH(SP); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1524 | |
| 1525 | // Clear stack used to keep track of #line directives. |
| 1526 | LineDirectiveFiles.clear(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1530 | void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1531 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1533 | // Don't bother if things are the same as last time. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1534 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | if (CurLoc == PrevLoc |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1536 | || (SM.getInstantiationLineNumber(CurLoc) == |
| 1537 | SM.getInstantiationLineNumber(PrevLoc) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1538 | && SM.isFromSameFile(CurLoc, PrevLoc))) |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 1539 | // New Builder may not be in sync with CGDebugInfo. |
| 1540 | if (!Builder.getCurrentDebugLocation().isUnknown()) |
| 1541 | return; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Update last state. |
| 1544 | PrevLoc = CurLoc; |
| 1545 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1546 | llvm::MDNode *Scope = RegionStack.back(); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1547 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc), |
| 1548 | getColumnNumber(CurLoc), |
Chris Lattner | e541d01 | 2010-04-02 20:21:43 +0000 | [diff] [blame] | 1549 | Scope)); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1552 | /// UpdateLineDirectiveRegion - Update region stack only if #line directive |
| 1553 | /// has introduced scope change. |
| 1554 | void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) { |
| 1555 | if (CurLoc.isInvalid() || CurLoc.isMacroID() || |
| 1556 | PrevLoc.isInvalid() || PrevLoc.isMacroID()) |
| 1557 | return; |
| 1558 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 1559 | PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); |
| 1560 | PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc); |
| 1561 | |
Douglas Gregor | 8c457a8 | 2010-11-11 20:45:16 +0000 | [diff] [blame] | 1562 | if (PCLoc.isInvalid() || PPLoc.isInvalid() || |
| 1563 | !strcmp(PPLoc.getFilename(), PCLoc.getFilename())) |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1564 | return; |
| 1565 | |
| 1566 | // If #line directive stack is empty then we are entering a new scope. |
| 1567 | if (LineDirectiveFiles.empty()) { |
| 1568 | EmitRegionStart(Builder); |
| 1569 | LineDirectiveFiles.push_back(PCLoc.getFilename()); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | assert (RegionStack.size() >= LineDirectiveFiles.size() |
| 1574 | && "error handling #line regions!"); |
| 1575 | |
| 1576 | bool SeenThisFile = false; |
Devang Patel | 424a5c6 | 2010-09-15 20:50:40 +0000 | [diff] [blame] | 1577 | // Chek if current file is already seen earlier. |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1578 | for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(), |
| 1579 | E = LineDirectiveFiles.end(); I != E; ++I) |
Devang Patel | 424a5c6 | 2010-09-15 20:50:40 +0000 | [diff] [blame] | 1580 | if (!strcmp(PCLoc.getFilename(), *I)) { |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1581 | SeenThisFile = true; |
| 1582 | break; |
| 1583 | } |
| 1584 | |
| 1585 | // If #line for this file is seen earlier then pop out #line regions. |
| 1586 | if (SeenThisFile) { |
| 1587 | while (!LineDirectiveFiles.empty()) { |
| 1588 | const char *LastFile = LineDirectiveFiles.back(); |
| 1589 | RegionStack.pop_back(); |
| 1590 | LineDirectiveFiles.pop_back(); |
| 1591 | if (!strcmp(PPLoc.getFilename(), LastFile)) |
| 1592 | break; |
| 1593 | } |
| 1594 | return; |
| 1595 | } |
| 1596 | |
| 1597 | // .. otherwise insert new #line region. |
| 1598 | EmitRegionStart(Builder); |
| 1599 | LineDirectiveFiles.push_back(PCLoc.getFilename()); |
| 1600 | |
| 1601 | return; |
| 1602 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1603 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 1604 | /// region - "llvm.dbg.region.start.". |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1605 | void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) { |
Devang Patel | 8fae060 | 2009-11-13 19:10:24 +0000 | [diff] [blame] | 1606 | llvm::DIDescriptor D = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1607 | DBuilder.CreateLexicalBlock(RegionStack.empty() ? |
| 1608 | llvm::DIDescriptor() : |
| 1609 | llvm::DIDescriptor(RegionStack.back()), |
| 1610 | getOrCreateFile(CurLoc), |
| 1611 | getLineNumber(CurLoc), |
| 1612 | getColumnNumber(CurLoc)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1613 | llvm::MDNode *DN = D; |
| 1614 | RegionStack.push_back(DN); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 1618 | /// region - "llvm.dbg.region.end." |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1619 | void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1620 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1621 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1622 | // Provide an region stop point. |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1623 | EmitStopPoint(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1625 | RegionStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1628 | /// EmitFunctionEnd - Constructs the debug code for exiting a function. |
| 1629 | void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { |
| 1630 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1631 | unsigned RCount = FnBeginRegionCount.back(); |
| 1632 | assert(RCount <= RegionStack.size() && "Region stack mismatch"); |
| 1633 | |
| 1634 | // Pop all regions for this function. |
| 1635 | while (RegionStack.size() != RCount) |
| 1636 | EmitRegionEnd(Builder); |
| 1637 | FnBeginRegionCount.pop_back(); |
| 1638 | } |
| 1639 | |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1640 | // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. |
| 1641 | // See BuildByRefType. |
| 1642 | llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, |
| 1643 | uint64_t *XOffset) { |
| 1644 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1645 | llvm::SmallVector<llvm::Value *, 5> EltTys; |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1646 | QualType FType; |
| 1647 | uint64_t FieldSize, FieldOffset; |
| 1648 | unsigned FieldAlign; |
| 1649 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1650 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1651 | QualType Type = VD->getType(); |
| 1652 | |
| 1653 | FieldOffset = 0; |
| 1654 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1655 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 1656 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1657 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1658 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 1659 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); |
| 1660 | |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1661 | bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type); |
| 1662 | if (HasCopyAndDispose) { |
| 1663 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1664 | EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", |
| 1665 | &FieldOffset)); |
| 1666 | EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", |
| 1667 | &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 1671 | if (Align > CharUnits::fromQuantity( |
| 1672 | CGM.getContext().Target.getPointerAlign(0) / 8)) { |
| 1673 | unsigned AlignedOffsetInBytes |
| 1674 | = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity()); |
| 1675 | unsigned NumPaddingBytes |
| 1676 | = AlignedOffsetInBytes - FieldOffset/8; |
| 1677 | |
| 1678 | if (NumPaddingBytes > 0) { |
| 1679 | llvm::APInt pad(32, NumPaddingBytes); |
| 1680 | FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, |
| 1681 | pad, ArrayType::Normal, 0); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1682 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | FType = Type; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1687 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1688 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 1689 | FieldAlign = Align.getQuantity()*8; |
| 1690 | |
| 1691 | *XOffset = FieldOffset; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1692 | FieldTy = DBuilder.CreateMemberType(VD->getName(), Unit, |
| 1693 | 0, FieldSize, FieldAlign, |
| 1694 | FieldOffset, 0, FieldTy); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1695 | EltTys.push_back(FieldTy); |
| 1696 | FieldOffset += FieldSize; |
| 1697 | |
| 1698 | llvm::DIArray Elements = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1699 | DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1700 | |
Devang Patel | e247248 | 2010-09-29 21:05:52 +0000 | [diff] [blame] | 1701 | unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct; |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1702 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1703 | return DBuilder.CreateStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, |
| 1704 | Elements); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1705 | } |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1706 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1707 | /// EmitDeclare - Emit local variable declaration debug info. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1708 | void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1709 | llvm::Value *Storage, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1710 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1711 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1712 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1713 | llvm::DIType Ty; |
| 1714 | uint64_t XOffset = 0; |
| 1715 | if (VD->hasAttr<BlocksAttr>()) |
| 1716 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 1717 | else |
| 1718 | Ty = getOrCreateType(VD->getType(), Unit); |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 1719 | |
Devang Patel | f4e54a2 | 2010-05-07 23:05:55 +0000 | [diff] [blame] | 1720 | // If there is not any debug info for type then do not emit debug info |
| 1721 | // for this variable. |
| 1722 | if (!Ty) |
| 1723 | return; |
| 1724 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1725 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1726 | unsigned Line = getLineNumber(VD->getLocation()); |
| 1727 | unsigned Column = getColumnNumber(VD->getLocation()); |
Devang Patel | aca745b | 2010-09-29 23:09:21 +0000 | [diff] [blame] | 1728 | unsigned Flags = 0; |
| 1729 | if (VD->isImplicit()) |
| 1730 | Flags |= llvm::DIDescriptor::FlagArtificial; |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1731 | llvm::MDNode *Scope = RegionStack.back(); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 1732 | |
| 1733 | llvm::StringRef Name = VD->getName(); |
| 1734 | if (!Name.empty()) { |
Devang Patel | b1fd0eb | 2011-01-11 00:30:27 +0000 | [diff] [blame] | 1735 | if (VD->hasAttr<BlocksAttr>()) { |
| 1736 | CharUnits offset = CharUnits::fromQuantity(32); |
| 1737 | llvm::SmallVector<llvm::Value *, 9> addr; |
| 1738 | const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); |
| 1739 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
| 1740 | // offset of __forwarding field |
| 1741 | offset = |
| 1742 | CharUnits::fromQuantity(CGM.getContext().Target.getPointerWidth(0)/8); |
| 1743 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 1744 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1745 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
| 1746 | // offset of x field |
| 1747 | offset = CharUnits::fromQuantity(XOffset/8); |
| 1748 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 1749 | |
| 1750 | // Create the descriptor for the variable. |
| 1751 | llvm::DIVariable D = |
| 1752 | DBuilder.CreateComplexVariable(Tag, |
| 1753 | llvm::DIDescriptor(RegionStack.back()), |
| 1754 | VD->getName(), Unit, Line, Ty, |
| 1755 | addr.data(), addr.size()); |
| 1756 | |
| 1757 | // Insert an llvm.dbg.declare into the current block. |
| 1758 | llvm::Instruction *Call = |
| 1759 | DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
| 1760 | |
| 1761 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
| 1762 | return; |
| 1763 | } |
| 1764 | // Create the descriptor for the variable. |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 1765 | llvm::DIVariable D = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1766 | DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope), |
| 1767 | Name, Unit, Line, Ty, |
| 1768 | CGM.getLangOptions().Optimize, Flags); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 1769 | |
| 1770 | // Insert an llvm.dbg.declare into the current block. |
| 1771 | llvm::Instruction *Call = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1772 | DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 1773 | |
| 1774 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Devang Patel | f4dd962 | 2010-10-29 16:21:19 +0000 | [diff] [blame] | 1775 | return; |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | // If VD is an anonymous union then Storage represents value for |
| 1779 | // all union fields. |
| 1780 | if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) |
| 1781 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl())) |
| 1782 | if (RD->isUnion()) { |
| 1783 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 1784 | E = RD->field_end(); |
| 1785 | I != E; ++I) { |
| 1786 | FieldDecl *Field = *I; |
| 1787 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
| 1788 | llvm::StringRef FieldName = Field->getName(); |
| 1789 | |
| 1790 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 1791 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
| 1792 | continue; |
| 1793 | |
| 1794 | // Use VarDecl's Tag, Scope and Line number. |
| 1795 | llvm::DIVariable D = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1796 | DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope), |
| 1797 | FieldName, Unit, Line, FieldTy, |
| 1798 | CGM.getLangOptions().Optimize, Flags); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 1799 | |
| 1800 | // Insert an llvm.dbg.declare into the current block. |
| 1801 | llvm::Instruction *Call = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1802 | DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Devang Patel | cebbedd | 2010-10-12 23:24:54 +0000 | [diff] [blame] | 1803 | |
| 1804 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
| 1805 | } |
| 1806 | } |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1809 | /// EmitDeclare - Emit local variable declaration debug info. |
| 1810 | void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, |
| 1811 | llvm::Value *Storage, CGBuilderTy &Builder, |
| 1812 | CodeGenFunction *CGF) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1813 | const ValueDecl *VD = BDRE->getDecl(); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1814 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1815 | |
Devang Patel | 2b594b9 | 2010-04-26 23:28:46 +0000 | [diff] [blame] | 1816 | if (Builder.GetInsertBlock() == 0) |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1817 | return; |
| 1818 | |
| 1819 | uint64_t XOffset = 0; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1820 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1821 | llvm::DIType Ty; |
| 1822 | if (VD->hasAttr<BlocksAttr>()) |
| 1823 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 1824 | else |
| 1825 | Ty = getOrCreateType(VD->getType(), Unit); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1826 | |
| 1827 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1828 | unsigned Line = getLineNumber(VD->getLocation()); |
| 1829 | unsigned Column = getColumnNumber(VD->getLocation()); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1830 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1831 | CharUnits offset = CGF->BlockDecls[VD]; |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1832 | llvm::SmallVector<llvm::Value *, 9> addr; |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1833 | const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); |
| 1834 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1835 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
| 1836 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1837 | if (BDRE->isByRef()) { |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1838 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1839 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1840 | // offset of __forwarding field |
| 1841 | offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1842 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 1843 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1844 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1845 | // offset of x field |
| 1846 | offset = CharUnits::fromQuantity(XOffset/8); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1847 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | // Create the descriptor for the variable. |
| 1851 | llvm::DIVariable D = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1852 | DBuilder.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()), |
| 1853 | VD->getName(), Unit, Line, Ty, |
| 1854 | addr.data(), addr.size()); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1855 | // Insert an llvm.dbg.declare into the current block. |
Devang Patel | ebf16e8 | 2009-11-11 19:10:19 +0000 | [diff] [blame] | 1856 | llvm::Instruction *Call = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1857 | DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Chris Lattner | d5b8902 | 2009-12-28 21:44:41 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1859 | llvm::MDNode *Scope = RegionStack.back(); |
Devang Patel | f8e10a5 | 2010-05-10 23:48:38 +0000 | [diff] [blame] | 1860 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1863 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1864 | llvm::Value *Storage, |
| 1865 | CGBuilderTy &Builder) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1866 | EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1869 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( |
| 1870 | const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder, |
| 1871 | CodeGenFunction *CGF) { |
| 1872 | EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF); |
| 1873 | } |
| 1874 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1875 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 1876 | /// variable declaration. |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1877 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1878 | CGBuilderTy &Builder) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1879 | EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | |
| 1883 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1884 | /// EmitGlobalVariable - Emit information about a global variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1886 | const VarDecl *D) { |
| 1887 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1888 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1889 | llvm::DIFile Unit = getOrCreateFile(D->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1890 | unsigned LineNo = getLineNumber(D->getLocation()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1891 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1892 | QualType T = D->getType(); |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1893 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1895 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 1896 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1898 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1899 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1901 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1902 | ArrayType::Normal, 0); |
| 1903 | } |
Devang Patel | 5d822f0 | 2010-04-29 17:48:37 +0000 | [diff] [blame] | 1904 | llvm::StringRef DeclName = D->getName(); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 1905 | llvm::StringRef LinkageName; |
Devang Patel | 0fd3d1f | 2010-05-14 16:55:25 +0000 | [diff] [blame] | 1906 | if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())) |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 1907 | LinkageName = Var->getName(); |
Devang Patel | 58faf20 | 2010-10-22 17:11:50 +0000 | [diff] [blame] | 1908 | if (LinkageName == DeclName) |
| 1909 | LinkageName = llvm::StringRef(); |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1910 | llvm::DIDescriptor DContext = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 1911 | getContextDescriptor(dyn_cast<Decl>(D->getDeclContext())); |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1912 | DBuilder.CreateStaticVariable(DContext, DeclName, LinkageName, |
| 1913 | Unit, LineNo, getOrCreateType(T, Unit), |
| 1914 | Var->hasInternalLinkage(), Var); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1917 | /// EmitGlobalVariable - Emit information about an objective-c interface. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1919 | ObjCInterfaceDecl *ID) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1920 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1921 | llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1922 | unsigned LineNo = getLineNumber(ID->getLocation()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1923 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1924 | llvm::StringRef Name = ID->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1925 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1926 | QualType T = CGM.getContext().getObjCInterfaceType(ID); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1927 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1929 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 1930 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1932 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1933 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1935 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1936 | ArrayType::Normal, 0); |
| 1937 | } |
| 1938 | |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1939 | DBuilder.CreateGlobalVariable(Name, Unit, LineNo, |
| 1940 | getOrCreateType(T, Unit), |
| 1941 | Var->hasInternalLinkage(), Var); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1942 | } |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1943 | |
Devang Patel | 25c2c8f | 2010-08-10 17:53:33 +0000 | [diff] [blame] | 1944 | /// EmitGlobalVariable - Emit global variable's debug info. |
| 1945 | void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 1946 | llvm::Constant *Init) { |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 1947 | // Create the descriptor for the variable. |
| 1948 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
| 1949 | llvm::StringRef Name = VD->getName(); |
Devang Patel | 0317ab0 | 2010-08-10 18:27:15 +0000 | [diff] [blame] | 1950 | llvm::DIType Ty = getOrCreateType(VD->getType(), Unit); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1951 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) { |
| 1952 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) |
Devang Patel | 31f7d02 | 2011-01-17 22:23:07 +0000 | [diff] [blame] | 1953 | Ty = CreateEnumType(ED); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1954 | } |
Devang Patel | 0317ab0 | 2010-08-10 18:27:15 +0000 | [diff] [blame] | 1955 | // Do not use DIGlobalVariable for enums. |
| 1956 | if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type) |
| 1957 | return; |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1958 | DBuilder.CreateStaticVariable(Unit, Name, Name, Unit, |
| 1959 | getLineNumber(VD->getLocation()), |
| 1960 | Ty, true, Init); |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1963 | /// getOrCreateNamesSpace - Return namespace descriptor for the given |
| 1964 | /// namespace decl. |
| 1965 | llvm::DINameSpace |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 1966 | CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) { |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1967 | llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = |
| 1968 | NameSpaceCache.find(NSDecl); |
| 1969 | if (I != NameSpaceCache.end()) |
| 1970 | return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); |
| 1971 | |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1972 | unsigned LineNo = getLineNumber(NSDecl->getLocation()); |
Devang Patel | 8c37668 | 2010-10-28 19:12:46 +0000 | [diff] [blame] | 1973 | llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation()); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1974 | llvm::DIDescriptor Context = |
Devang Patel | 170cef3 | 2010-12-09 00:33:05 +0000 | [diff] [blame] | 1975 | getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext())); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1976 | llvm::DINameSpace NS = |
Devang Patel | 823d8e9 | 2010-12-08 22:42:58 +0000 | [diff] [blame] | 1977 | DBuilder.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1978 | NameSpaceCache[NSDecl] = llvm::WeakVH(NS); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1979 | return NS; |
| 1980 | } |