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 | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Basic/FileManager.h" |
Mike Stump | 5a86217 | 2009-09-15 21:48:34 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Version.h" |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 24 | #include "clang/CodeGen/CodeGenOptions.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
| 26 | #include "llvm/DerivedTypes.h" |
| 27 | #include "llvm/Instructions.h" |
| 28 | #include "llvm/Intrinsics.h" |
| 29 | #include "llvm/Module.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Dwarf.h" |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 33 | #include "llvm/System/Path.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | using namespace clang::CodeGen; |
| 37 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 38 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 39 | : CGM(CGM), DebugFactory(CGM.getModule()), |
Devang Patel | 7573f8b | 2010-03-09 21:32:27 +0000 | [diff] [blame] | 40 | FwdDeclCount(0), BlockLiteralGenericSet(false) { |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 41 | CreateCompileUnit(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 44 | CGDebugInfo::~CGDebugInfo() { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 45 | assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 48 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 49 | if (Loc.isValid()) |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 50 | CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Devang Patel | 3358305 | 2010-01-28 23:15:27 +0000 | [diff] [blame] | 53 | /// getContextDescriptor - Get context info for the decl. |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 54 | llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context, |
Devang Patel | 3358305 | 2010-01-28 23:15:27 +0000 | [diff] [blame] | 55 | llvm::DIDescriptor &CompileUnit) { |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 56 | if (!Context) |
| 57 | return CompileUnit; |
| 58 | |
| 59 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator |
| 60 | I = RegionMap.find(Context); |
| 61 | if (I != RegionMap.end()) |
| 62 | return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second)); |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 63 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 64 | // Check namespace. |
| 65 | if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
| 66 | return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit)); |
| 67 | |
Devang Patel | 979ec2e | 2009-10-06 00:35:31 +0000 | [diff] [blame] | 68 | return CompileUnit; |
| 69 | } |
| 70 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 71 | /// getFunctionName - Get function name for the given FunctionDecl. If the |
| 72 | /// name is constructred on demand (e.g. C++ destructor) then the name |
| 73 | /// is stored on the side. |
| 74 | llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
| 75 | assert (FD && "Invalid FunctionDecl!"); |
| 76 | IdentifierInfo *FII = FD->getIdentifier(); |
| 77 | if (FII) |
| 78 | return FII->getName(); |
| 79 | |
| 80 | // Otherwise construct human readable name for debug info. |
| 81 | std::string NS = FD->getNameAsString(); |
| 82 | |
| 83 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 84 | char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); |
Benjamin Kramer | 1b627dc | 2010-01-23 18:16:07 +0000 | [diff] [blame] | 85 | memcpy(StrPtr, NS.data(), NS.length()); |
| 86 | return llvm::StringRef(StrPtr, NS.length()); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 89 | /// getOrCreateFile - Get the file debug info descriptor for the input location. |
| 90 | llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 91 | if (!Loc.isValid()) |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 92 | // If Location is not valid then use main input file. |
| 93 | return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(), |
| 94 | TheCU); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 95 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 96 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 97 | |
| 98 | // Cache the results. |
| 99 | const char *fname = PLoc.getFilename(); |
| 100 | llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = |
| 101 | DIFileCache.find(fname); |
| 102 | |
| 103 | if (it != DIFileCache.end()) { |
| 104 | // Verify that the information still exists. |
| 105 | if (&*it->second) |
| 106 | return llvm::DIFile(cast<llvm::MDNode>(it->second)); |
| 107 | } |
| 108 | |
| 109 | // FIXME: We shouldn't even need to call 'makeAbsolute()' in the cases |
| 110 | // where we can consult the FileEntry. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 111 | llvm::sys::Path AbsFileName(PLoc.getFilename()); |
Benjamin Kramer | 47daf68 | 2009-12-08 11:02:29 +0000 | [diff] [blame] | 112 | AbsFileName.makeAbsolute(); |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 114 | llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(), |
| 115 | AbsFileName.getDirname(), TheCU); |
| 116 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 117 | DIFileCache[fname] = F; |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 118 | return F; |
| 119 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 120 | } |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 121 | |
| 122 | /// getLineNumber - Get line number for the location. If location is invalid |
| 123 | /// then use current location. |
| 124 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
| 125 | assert (CurLoc.isValid() && "Invalid current location!"); |
| 126 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 127 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| 128 | return PLoc.getLine(); |
| 129 | } |
| 130 | |
| 131 | /// getColumnNumber - Get column number for the location. If location is |
| 132 | /// invalid then use current location. |
| 133 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) { |
| 134 | assert (CurLoc.isValid() && "Invalid current location!"); |
| 135 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 136 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| 137 | return PLoc.getColumn(); |
| 138 | } |
| 139 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 140 | /// CreateCompileUnit - Create new compile unit. |
| 141 | void CGDebugInfo::CreateCompileUnit() { |
| 142 | |
| 143 | // Get absolute path name. |
Douglas Gregor | ac91b4c | 2010-03-18 23:46:43 +0000 | [diff] [blame] | 144 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 145 | std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| 146 | if (MainFileName.empty()) |
Devang Patel | 22fe585 | 2010-03-12 21:04:27 +0000 | [diff] [blame] | 147 | MainFileName = "<unknown>"; |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 148 | |
Devang Patel | 22fe585 | 2010-03-12 21:04:27 +0000 | [diff] [blame] | 149 | llvm::sys::Path AbsFileName(MainFileName); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 150 | AbsFileName.makeAbsolute(); |
Daniel Dunbar | c9abc04 | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | f6728fc | 2010-03-22 21:28:29 +0000 | [diff] [blame] | 152 | // The main file name provided via the "-main-file-name" option contains just |
| 153 | // the file name itself with no path information. This file name may have had |
| 154 | // a relative path, so we look into the actual file entry for the main |
| 155 | // file to determine the real absolute path for the file. |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 156 | std::string MainFileDir; |
| 157 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) |
| 158 | MainFileDir = MainFile->getDir()->getName(); |
| 159 | else |
| 160 | MainFileDir = AbsFileName.getDirname(); |
| 161 | |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 162 | unsigned LangTag; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 163 | const LangOptions &LO = CGM.getLangOptions(); |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 164 | if (LO.CPlusPlus) { |
| 165 | if (LO.ObjC1) |
| 166 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 167 | else |
| 168 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 169 | } else if (LO.ObjC1) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 170 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 171 | } else if (LO.C99) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 172 | LangTag = llvm::dwarf::DW_LANG_C99; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 173 | } else { |
| 174 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 175 | } |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 176 | |
Benjamin Kramer | 47daf68 | 2009-12-08 11:02:29 +0000 | [diff] [blame] | 177 | const char *Producer = |
Mike Stump | d8945d6 | 2009-10-09 18:38:12 +0000 | [diff] [blame] | 178 | #ifdef CLANG_VENDOR |
| 179 | CLANG_VENDOR |
| 180 | #endif |
| 181 | "clang " CLANG_VERSION_STRING; |
Chris Lattner | 4c2577a | 2009-05-02 01:00:04 +0000 | [diff] [blame] | 182 | |
| 183 | // Figure out which version of the ObjC runtime we have. |
| 184 | unsigned RuntimeVers = 0; |
| 185 | if (LO.ObjC1) |
| 186 | RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 188 | // Create new compile unit. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 189 | TheCU = DebugFactory.CreateCompileUnit( |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 190 | LangTag, AbsFileName.getLast(), MainFileDir, Producer, true, |
Daniel Dunbar | f2d8b9f | 2009-12-18 02:43:17 +0000 | [diff] [blame] | 191 | LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 194 | /// CreateType - Get the Basic type from the cache or create a new |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 195 | /// one if necessary. |
| 196 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 197 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 198 | unsigned Encoding = 0; |
| 199 | switch (BT->getKind()) { |
| 200 | default: |
| 201 | case BuiltinType::Void: |
| 202 | return llvm::DIType(); |
| 203 | case BuiltinType::UChar: |
| 204 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 205 | case BuiltinType::Char_S: |
| 206 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
| 207 | case BuiltinType::UShort: |
| 208 | case BuiltinType::UInt: |
| 209 | case BuiltinType::ULong: |
| 210 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 211 | case BuiltinType::Short: |
| 212 | case BuiltinType::Int: |
| 213 | case BuiltinType::Long: |
| 214 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 215 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
| 216 | case BuiltinType::Float: |
Devang Patel | 7c173cb | 2009-10-12 22:28:31 +0000 | [diff] [blame] | 217 | case BuiltinType::LongDouble: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 218 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 220 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 221 | uint64_t Size = CGM.getContext().getTypeSize(BT); |
| 222 | uint64_t Align = CGM.getContext().getTypeAlign(BT); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 223 | uint64_t Offset = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 225 | llvm::DIType DbgTy = |
| 226 | DebugFactory.CreateBasicType(Unit, |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 227 | BT->getName(CGM.getContext().getLangOptions()), |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 228 | Unit, 0, Size, Align, |
| 229 | Offset, /*flags*/ 0, Encoding); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 230 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 231 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 232 | |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 233 | llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 234 | llvm::DIFile Unit) { |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 235 | // Bit size, align and offset of the type. |
| 236 | unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; |
| 237 | if (Ty->isComplexIntegerType()) |
| 238 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 240 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 241 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 242 | uint64_t Offset = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 244 | llvm::DIType DbgTy = |
| 245 | DebugFactory.CreateBasicType(Unit, "complex", |
| 246 | Unit, 0, Size, Align, |
| 247 | Offset, /*flags*/ 0, Encoding); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 248 | return DbgTy; |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 249 | } |
| 250 | |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 251 | /// CreateCVRType - Get the qualified type from the cache or create |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 252 | /// a new one if necessary. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 253 | llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 254 | QualifierCollector Qc; |
| 255 | const Type *T = Qc.strip(Ty); |
| 256 | |
| 257 | // Ignore these qualifiers for now. |
| 258 | Qc.removeObjCGCAttr(); |
| 259 | Qc.removeAddressSpace(); |
| 260 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 261 | // We will create one Derived type for one qualifier and recurse to handle any |
| 262 | // additional ones. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 263 | unsigned Tag; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 264 | if (Qc.hasConst()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 265 | Tag = llvm::dwarf::DW_TAG_const_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 266 | Qc.removeConst(); |
| 267 | } else if (Qc.hasVolatile()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 268 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 269 | Qc.removeVolatile(); |
| 270 | } else if (Qc.hasRestrict()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 271 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 272 | Qc.removeRestrict(); |
| 273 | } else { |
| 274 | assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| 275 | return getOrCreateType(QualType(T, 0), Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 278 | llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit); |
| 279 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame] | 280 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 281 | // CVR derived types. |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 282 | llvm::DIType DbgTy = |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 283 | DebugFactory.CreateDerivedType(Tag, Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 284 | 0, 0, 0, 0, 0, FromTy); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 285 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 288 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 289 | llvm::DIFile Unit) { |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 290 | llvm::DIType DbgTy = |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 291 | CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 292 | Ty->getPointeeType(), Unit); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 293 | return DbgTy; |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 296 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 297 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 298 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 299 | Ty->getPointeeType(), Unit); |
| 300 | } |
| 301 | |
| 302 | llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, |
| 303 | const Type *Ty, |
| 304 | QualType PointeeTy, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 305 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 306 | llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 308 | // Bit size, align and offset of the type. |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 309 | |
| 310 | // Size is always the size of a pointer. We can't use getTypeSize here |
| 311 | // because that does not return the correct value for references. |
| 312 | uint64_t Size = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 313 | CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace()); |
| 314 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 316 | return |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 317 | DebugFactory.CreateDerivedType(Tag, Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 318 | 0, Size, Align, 0, 0, EltTy); |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 319 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 322 | llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 323 | llvm::DIFile Unit) { |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 324 | if (BlockLiteralGenericSet) |
| 325 | return BlockLiteralGeneric; |
| 326 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 327 | unsigned Tag = llvm::dwarf::DW_TAG_structure_type; |
| 328 | |
| 329 | llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; |
| 330 | |
| 331 | llvm::DIType FieldTy; |
| 332 | |
| 333 | QualType FType; |
| 334 | uint64_t FieldSize, FieldOffset; |
| 335 | unsigned FieldAlign; |
| 336 | |
| 337 | llvm::DIArray Elements; |
| 338 | llvm::DIType EltTy, DescTy; |
| 339 | |
| 340 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 341 | FType = CGM.getContext().UnsignedLongTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 342 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| 343 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 344 | |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 345 | Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 346 | EltTys.clear(); |
| 347 | |
Mike Stump | 3d363c5 | 2009-10-02 02:30:50 +0000 | [diff] [blame] | 348 | unsigned Flags = llvm::DIType::FlagAppleBlock; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 349 | unsigned LineNo = getLineNumber(CurLoc); |
Mike Stump | 3d363c5 | 2009-10-02 02:30:50 +0000 | [diff] [blame] | 350 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 351 | EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor", |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 352 | Unit, LineNo, FieldOffset, 0, 0, |
| 353 | Flags, llvm::DIType(), Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 355 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 356 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 357 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 359 | DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 360 | Unit, "", Unit, |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 361 | LineNo, Size, Align, 0, 0, EltTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 362 | |
| 363 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 364 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 365 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 366 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 367 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 368 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
Benjamin Kramer | d3651cc | 2010-04-24 20:26:20 +0000 | [diff] [blame] | 369 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 370 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 371 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 372 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 373 | FieldTy = DescTy; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 374 | FieldSize = CGM.getContext().getTypeSize(Ty); |
| 375 | FieldAlign = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 376 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 377 | "__descriptor", Unit, |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 378 | LineNo, FieldSize, FieldAlign, |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 379 | FieldOffset, 0, FieldTy); |
| 380 | EltTys.push_back(FieldTy); |
| 381 | |
| 382 | FieldOffset += FieldSize; |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 383 | Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 384 | |
| 385 | EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic", |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 386 | Unit, LineNo, FieldOffset, 0, 0, |
| 387 | Flags, llvm::DIType(), Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 389 | BlockLiteralGenericSet = true; |
| 390 | BlockLiteralGeneric |
| 391 | = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 392 | "", Unit, |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 393 | LineNo, Size, Align, 0, 0, EltTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 394 | return BlockLiteralGeneric; |
| 395 | } |
| 396 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 397 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 398 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 399 | // Typedefs are derived from some other type. If we have a typedef of a |
| 400 | // typedef, make sure to emit the whole chain. |
| 401 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 403 | // We don't set size information, but do specify where the typedef was |
| 404 | // declared. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 405 | unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 406 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 407 | llvm::DIDescriptor TyContext |
| 408 | = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()), |
| 409 | Unit); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 410 | llvm::DIType DbgTy = |
Devang Patel | d528905 | 2010-01-29 22:29:31 +0000 | [diff] [blame] | 411 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 412 | TyContext, |
Devang Patel | d528905 | 2010-01-29 22:29:31 +0000 | [diff] [blame] | 413 | Ty->getDecl()->getName(), Unit, |
| 414 | Line, 0, 0, 0, 0, Src); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 415 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 418 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 419 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 420 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 422 | // Add the result type at least. |
| 423 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 425 | // Set up remainder of arguments if there is a prototype. |
| 426 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 427 | if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 428 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 429 | EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); |
| 430 | } else { |
| 431 | // FIXME: Handle () case in C. llvm-gcc doesn't do it either. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 434 | llvm::DIArray EltTypeArray = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 435 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 437 | llvm::DIType DbgTy = |
| 438 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 439 | Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 440 | 0, 0, 0, 0, 0, |
| 441 | llvm::DIType(), EltTypeArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 442 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 445 | /// CollectRecordFields - A helper function to collect debug info for |
| 446 | /// record fields. This is used while creating debug info entry for a Record. |
| 447 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 448 | CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 449 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { |
| 450 | unsigned FieldNo = 0; |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 451 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 452 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 453 | E = RD->field_end(); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 454 | I != E; ++I, ++FieldNo) { |
| 455 | FieldDecl *Field = *I; |
| 456 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
| 457 | |
| 458 | llvm::StringRef FieldName = Field->getName(); |
| 459 | |
Devang Patel | 4835fdd | 2010-02-12 01:31:06 +0000 | [diff] [blame] | 460 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 461 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 462 | continue; |
| 463 | |
| 464 | // Get the location for the field. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 465 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 466 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 467 | QualType FType = Field->getType(); |
| 468 | uint64_t FieldSize = 0; |
| 469 | unsigned FieldAlign = 0; |
| 470 | if (!FType->isIncompleteArrayType()) { |
| 471 | |
| 472 | // Bit size, align and offset of the type. |
| 473 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 474 | Expr *BitWidth = Field->getBitWidth(); |
| 475 | if (BitWidth) |
| 476 | FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
| 477 | |
| 478 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
| 479 | } |
| 480 | |
| 481 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 482 | |
Devang Patel | 71f4ff6 | 2010-04-21 23:12:37 +0000 | [diff] [blame] | 483 | unsigned Flags = 0; |
| 484 | AccessSpecifier Access = I->getAccess(); |
| 485 | if (Access == clang::AS_private) |
| 486 | Flags |= llvm::DIType::FlagPrivate; |
| 487 | else if (Access == clang::AS_protected) |
| 488 | Flags |= llvm::DIType::FlagProtected; |
| 489 | |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 490 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 491 | // struct. FIXME: This is an absolutely insane way to capture this |
| 492 | // information. When we gut debug info, this should be fixed. |
| 493 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 494 | FieldName, FieldDefUnit, |
| 495 | FieldLine, FieldSize, FieldAlign, |
Devang Patel | 71f4ff6 | 2010-04-21 23:12:37 +0000 | [diff] [blame] | 496 | FieldOffset, Flags, FieldTy); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 497 | EltTys.push_back(FieldTy); |
| 498 | } |
| 499 | } |
| 500 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 501 | /// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This |
| 502 | /// function type is not updated to include implicit "this" pointer. Use this |
| 503 | /// routine to get a method type which includes "this" pointer. |
| 504 | llvm::DIType |
| 505 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 506 | llvm::DIFile Unit) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 507 | llvm::DIType FnTy |
| 508 | = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(), |
| 509 | 0), |
| 510 | Unit); |
Devang Patel | d774d1e | 2010-01-28 21:43:50 +0000 | [diff] [blame] | 511 | |
| 512 | // Static methods do not need "this" pointer argument. |
| 513 | if (Method->isStatic()) |
| 514 | return FnTy; |
| 515 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 516 | // Add "this" pointer. |
| 517 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 518 | llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray(); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 519 | assert (Args.getNumElements() && "Invalid number of arguments!"); |
| 520 | |
| 521 | llvm::SmallVector<llvm::DIDescriptor, 16> Elts; |
| 522 | |
| 523 | // First element is always return type. For 'void' functions it is NULL. |
| 524 | Elts.push_back(Args.getElement(0)); |
| 525 | |
| 526 | // "this" pointer is always first argument. |
| 527 | ASTContext &Context = CGM.getContext(); |
| 528 | QualType ThisPtr = |
| 529 | Context.getPointerType(Context.getTagDeclType(Method->getParent())); |
Devang Patel | 337472d | 2010-02-09 17:57:50 +0000 | [diff] [blame] | 530 | llvm::DIType ThisPtrType = |
| 531 | DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 532 | TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; |
Devang Patel | 337472d | 2010-02-09 17:57:50 +0000 | [diff] [blame] | 533 | Elts.push_back(ThisPtrType); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 534 | |
| 535 | // Copy rest of the arguments. |
| 536 | for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) |
| 537 | Elts.push_back(Args.getElement(i)); |
| 538 | |
| 539 | llvm::DIArray EltTypeArray = |
| 540 | DebugFactory.GetOrCreateArray(Elts.data(), Elts.size()); |
| 541 | |
| 542 | return |
| 543 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 544 | Unit, "", Unit, |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 545 | 0, 0, 0, 0, 0, |
| 546 | llvm::DIType(), EltTypeArray); |
| 547 | } |
| 548 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 549 | /// CreateCXXMemberFunction - A helper function to create a DISubprogram for |
| 550 | /// a single member function GlobalDecl. |
| 551 | llvm::DISubprogram |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 552 | CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 553 | llvm::DIFile Unit, |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 554 | llvm::DICompositeType &RecordTy) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 555 | bool IsCtorOrDtor = |
| 556 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
| 557 | |
| 558 | llvm::StringRef MethodName = getFunctionName(Method); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 559 | llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 560 | |
| 561 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| 562 | // make sense to give a single ctor/dtor a linkage name. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 563 | MangleBuffer MethodLinkageName; |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 564 | if (!IsCtorOrDtor) |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 565 | CGM.getMangledName(MethodLinkageName, Method); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 566 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 567 | // Get the location for the method. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 568 | llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| 569 | unsigned MethodLine = getLineNumber(Method->getLocation()); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 570 | |
| 571 | // Collect virtual method info. |
| 572 | llvm::DIType ContainingType; |
| 573 | unsigned Virtuality = 0; |
| 574 | unsigned VIndex = 0; |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 575 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 576 | if (Method->isVirtual()) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 577 | if (Method->isPure()) |
| 578 | Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| 579 | else |
| 580 | Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
| 581 | |
| 582 | // It doesn't make sense to give a virtual destructor a vtable index, |
| 583 | // since a single destructor has two entries in the vtable. |
| 584 | if (!isa<CXXDestructorDecl>(Method)) |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 585 | VIndex = CGM.getVTables().getMethodVTableIndex(Method); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 586 | ContainingType = RecordTy; |
| 587 | } |
| 588 | |
| 589 | llvm::DISubprogram SP = |
| 590 | DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName, |
| 591 | MethodLinkageName, |
| 592 | MethodDefUnit, MethodLine, |
| 593 | MethodTy, /*isLocalToUnit=*/false, |
| 594 | Method->isThisDeclarationADefinition(), |
| 595 | Virtuality, VIndex, ContainingType); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 596 | |
| 597 | // Don't cache ctors or dtors since we have to emit multiple functions for |
| 598 | // a single ctor or dtor. |
| 599 | if (!IsCtorOrDtor && Method->isThisDeclarationADefinition()) |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 600 | SPCache[Method] = llvm::WeakVH(SP); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 601 | |
| 602 | return SP; |
| 603 | } |
| 604 | |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 605 | /// CollectCXXMemberFunctions - A helper function to collect debug info for |
| 606 | /// C++ member functions.This is used while creating debug info entry for |
| 607 | /// a Record. |
| 608 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 609 | CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 610 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, |
| 611 | llvm::DICompositeType &RecordTy) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 612 | for(CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 613 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 614 | const CXXMethodDecl *Method = *I; |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 615 | |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 616 | if (Method->isImplicit() && !Method->isUsed()) |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 617 | continue; |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 618 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 619 | EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 623 | /// CollectCXXBases - A helper function to collect debug info for |
| 624 | /// C++ base classes. This is used while creating debug info entry for |
| 625 | /// a Record. |
| 626 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 627 | CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 628 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, |
| 629 | llvm::DICompositeType &RecordTy) { |
| 630 | |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 631 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 632 | for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), |
| 633 | BE = RD->bases_end(); BI != BE; ++BI) { |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 634 | unsigned BFlags = 0; |
| 635 | uint64_t BaseOffset; |
| 636 | |
| 637 | const CXXRecordDecl *Base = |
| 638 | cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); |
| 639 | |
| 640 | if (BI->isVirtual()) { |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 641 | // virtual base offset offset is -ve. The code generator emits dwarf |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 642 | // expression where it expects +ve number. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 643 | BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base); |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 644 | BFlags = llvm::DIType::FlagVirtual; |
| 645 | } else |
| 646 | BaseOffset = RL.getBaseClassOffset(Base); |
| 647 | |
| 648 | AccessSpecifier Access = BI->getAccessSpecifier(); |
| 649 | if (Access == clang::AS_private) |
| 650 | BFlags |= llvm::DIType::FlagPrivate; |
| 651 | else if (Access == clang::AS_protected) |
| 652 | BFlags |= llvm::DIType::FlagProtected; |
| 653 | |
| 654 | llvm::DIType DTy = |
| 655 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, |
| 656 | RecordTy, llvm::StringRef(), |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 657 | Unit, 0, 0, 0, |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 658 | BaseOffset, BFlags, |
| 659 | getOrCreateType(BI->getType(), |
| 660 | Unit)); |
| 661 | EltTys.push_back(DTy); |
| 662 | } |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 665 | /// getOrCreateVTablePtrType - Return debug info descriptor for vtable. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 666 | llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { |
Devang Patel | 0804e6e | 2010-03-08 20:53:17 +0000 | [diff] [blame] | 667 | if (VTablePtrType.isValid()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 668 | return VTablePtrType; |
| 669 | |
| 670 | ASTContext &Context = CGM.getContext(); |
| 671 | |
| 672 | /* Function type */ |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 673 | llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit); |
| 674 | llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 675 | llvm::DIType SubTy = |
| 676 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 677 | Unit, "", Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 678 | 0, 0, 0, 0, 0, llvm::DIType(), SElements); |
| 679 | |
| 680 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
| 681 | llvm::DIType vtbl_ptr_type |
| 682 | = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 683 | Unit, "__vtbl_ptr_type", Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 684 | 0, Size, 0, 0, 0, SubTy); |
| 685 | |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 686 | VTablePtrType = |
| 687 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
| 688 | Unit, "", Unit, |
| 689 | 0, Size, 0, 0, 0, vtbl_ptr_type); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 690 | return VTablePtrType; |
| 691 | } |
| 692 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 693 | /// getVTableName - Get vtable name for the given Class. |
| 694 | llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 695 | // Otherwise construct gdb compatible name name. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 696 | std::string Name = "_vptr$" + RD->getNameAsString(); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 697 | |
| 698 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 699 | char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 700 | memcpy(StrPtr, Name.data(), Name.length()); |
| 701 | return llvm::StringRef(StrPtr, Name.length()); |
| 702 | } |
| 703 | |
| 704 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 705 | /// CollectVTableInfo - If the C++ class has vtable info then insert appropriate |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 706 | /// debug info entry in EltTys vector. |
| 707 | void CGDebugInfo:: |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 708 | CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 709 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 710 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 711 | |
| 712 | // If there is a primary base then it will hold vtable info. |
| 713 | if (RL.getPrimaryBase()) |
| 714 | return; |
| 715 | |
| 716 | // 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] | 717 | if (!RD->isDynamicClass()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 718 | return; |
| 719 | |
| 720 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 721 | llvm::DIType VPTR |
| 722 | = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 723 | getVTableName(RD), Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 724 | 0, Size, 0, 0, 0, |
| 725 | getOrCreateVTablePtrType(Unit)); |
| 726 | EltTys.push_back(VPTR); |
| 727 | } |
| 728 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 729 | /// CreateType - get structure or union type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 730 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 731 | llvm::DIFile Unit) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 732 | RecordDecl *RD = Ty->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 734 | unsigned Tag; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 735 | if (RD->isStruct()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 736 | Tag = llvm::dwarf::DW_TAG_structure_type; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 737 | else if (RD->isUnion()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 738 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 739 | else { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 740 | assert(RD->isClass() && "Unknown RecordType!"); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 741 | Tag = llvm::dwarf::DW_TAG_class_type; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 744 | // Get overall information about the record type for the debug info. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 745 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 746 | unsigned Line = getLineNumber(RD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 748 | // Records and classes and unions can all be recursive. To handle them, we |
| 749 | // first generate a debug descriptor for the struct as a forward declaration. |
| 750 | // Then (if it is a definition) we go through and get debug info for all of |
| 751 | // its members. Finally, we create a descriptor for the complete type (which |
| 752 | // may refer to the forward decl if the struct is recursive) and replace all |
| 753 | // uses of the forward declaration with the final definition. |
Devang Patel | d0f251b | 2010-01-20 23:56:40 +0000 | [diff] [blame] | 754 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 755 | // A RD->getName() is not unique. However, the debug info descriptors |
Devang Patel | ce78c97 | 2010-02-01 22:51:29 +0000 | [diff] [blame] | 756 | // are uniqued so use type name to ensure uniquness. |
Benjamin Kramer | fea3d4d | 2010-03-13 12:06:51 +0000 | [diff] [blame] | 757 | llvm::SmallString<128> FwdDeclName; |
| 758 | llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++; |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 759 | llvm::DIDescriptor FDContext = |
| 760 | getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); |
Devang Patel | 0ce73f6 | 2009-07-22 18:57:00 +0000 | [diff] [blame] | 761 | llvm::DICompositeType FwdDecl = |
Devang Patel | 7573f8b | 2010-03-09 21:32:27 +0000 | [diff] [blame] | 762 | DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName, |
Devang Patel | ab71ff5 | 2009-11-12 00:51:46 +0000 | [diff] [blame] | 763 | DefUnit, Line, 0, 0, 0, 0, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 764 | llvm::DIType(), llvm::DIArray()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 766 | // If this is just a forward declaration, return it. |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 767 | if (!RD->getDefinition()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 768 | return FwdDecl; |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 769 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 770 | llvm::MDNode *MN = FwdDecl; |
| 771 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 772 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 773 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 774 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 775 | // Push the struct on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 776 | RegionStack.push_back(FwdDeclNode); |
| 777 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 778 | |
| 779 | // Convert all the elements. |
| 780 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 781 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 782 | const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 783 | if (CXXDecl) { |
| 784 | CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 785 | CollectVTableInfo(CXXDecl, Unit, EltTys); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 786 | } |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 787 | CollectRecordFields(RD, Unit, EltTys); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 788 | llvm::MDNode *ContainingType = NULL; |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 789 | if (CXXDecl) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 790 | CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 791 | |
| 792 | // A class's primary base or the class itself contains the vtable. |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 793 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 794 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) |
| 795 | ContainingType = |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 796 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 797 | else if (CXXDecl->isDynamicClass()) |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 798 | ContainingType = FwdDecl; |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 801 | llvm::DIArray Elements = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 802 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 803 | |
| 804 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 805 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 806 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 808 | RegionStack.pop_back(); |
| 809 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 810 | RegionMap.find(Ty->getDecl()); |
| 811 | if (RI != RegionMap.end()) |
| 812 | RegionMap.erase(RI); |
| 813 | |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 814 | llvm::DIDescriptor RDContext = |
| 815 | getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); |
Devang Patel | 0ce73f6 | 2009-07-22 18:57:00 +0000 | [diff] [blame] | 816 | llvm::DICompositeType RealDecl = |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 817 | DebugFactory.CreateCompositeType(Tag, RDContext, |
| 818 | RD->getName(), |
Devang Patel | ab71ff5 | 2009-11-12 00:51:46 +0000 | [diff] [blame] | 819 | DefUnit, Line, Size, Align, 0, 0, |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 820 | llvm::DIType(), Elements, |
| 821 | 0, ContainingType); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 822 | |
| 823 | // Now that we have a real decl for the struct, replace anything using the |
| 824 | // old decl with the new one. This will recursively update the debug info. |
Eli Friedman | 14d6365 | 2009-11-16 21:04:30 +0000 | [diff] [blame] | 825 | llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 826 | RegionMap[RD] = llvm::WeakVH(RealDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 827 | return RealDecl; |
| 828 | } |
| 829 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 830 | /// CreateType - get objective-c interface type. |
| 831 | llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 832 | llvm::DIFile Unit) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 833 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 834 | unsigned Tag = llvm::dwarf::DW_TAG_structure_type; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 835 | |
| 836 | // Get overall information about the record type for the debug info. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 837 | llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 838 | unsigned Line = getLineNumber(ID->getLocation()); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 839 | unsigned RuntimeLang = TheCU.getLanguage(); |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 840 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 841 | // To handle recursive interface, we |
| 842 | // first generate a debug descriptor for the struct as a forward declaration. |
| 843 | // Then (if it is a definition) we go through and get debug info for all of |
| 844 | // its members. Finally, we create a descriptor for the complete type (which |
| 845 | // may refer to the forward decl if the struct is recursive) and replace all |
| 846 | // uses of the forward declaration with the final definition. |
Devang Patel | 6c1fddf | 2009-07-27 18:42:03 +0000 | [diff] [blame] | 847 | llvm::DICompositeType FwdDecl = |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 848 | DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), |
Devang Patel | ab71ff5 | 2009-11-12 00:51:46 +0000 | [diff] [blame] | 849 | DefUnit, Line, 0, 0, 0, 0, |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 850 | llvm::DIType(), llvm::DIArray(), |
| 851 | RuntimeLang); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 853 | // If this is just a forward declaration, return it. |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 854 | if (ID->isForwardDecl()) |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 855 | return FwdDecl; |
| 856 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 857 | llvm::MDNode *MN = FwdDecl; |
| 858 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 859 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 860 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 861 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 862 | // Push the struct on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 863 | RegionStack.push_back(FwdDeclNode); |
| 864 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 865 | |
| 866 | // Convert all the elements. |
| 867 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 868 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 869 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 870 | if (SClass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | llvm::DIType SClassTy = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 872 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | llvm::DIType InhTag = |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 874 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 875 | Unit, "", Unit, 0, 0, 0, |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 876 | 0 /* offset */, 0, SClassTy); |
| 877 | EltTys.push_back(InhTag); |
| 878 | } |
| 879 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 880 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 881 | |
| 882 | unsigned FieldNo = 0; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 883 | for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(), |
| 884 | E = ID->ivar_end(); I != E; ++I, ++FieldNo) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 885 | ObjCIvarDecl *Field = *I; |
| 886 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
| 887 | |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 888 | llvm::StringRef FieldName = Field->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 889 | |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 890 | // Ignore unnamed fields. |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 891 | if (FieldName.empty()) |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 892 | continue; |
| 893 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 894 | // Get the location for the field. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 895 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 896 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 897 | QualType FType = Field->getType(); |
| 898 | uint64_t FieldSize = 0; |
| 899 | unsigned FieldAlign = 0; |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 900 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 901 | if (!FType->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 903 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 904 | FieldSize = CGM.getContext().getTypeSize(FType); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 905 | Expr *BitWidth = Field->getBitWidth(); |
| 906 | if (BitWidth) |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 907 | FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 908 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 909 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 913 | |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 914 | unsigned Flags = 0; |
| 915 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
| 916 | Flags = llvm::DIType::FlagProtected; |
| 917 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
| 918 | Flags = llvm::DIType::FlagPrivate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 920 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 921 | // struct. FIXME: This is an absolutely insane way to capture this |
| 922 | // information. When we gut debug info, this should be fixed. |
| 923 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 924 | FieldName, FieldDefUnit, |
| 925 | FieldLine, FieldSize, FieldAlign, |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 926 | FieldOffset, Flags, FieldTy); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 927 | EltTys.push_back(FieldTy); |
| 928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 930 | llvm::DIArray Elements = |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 931 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 932 | |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 933 | RegionStack.pop_back(); |
| 934 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 935 | RegionMap.find(Ty->getDecl()); |
| 936 | if (RI != RegionMap.end()) |
| 937 | RegionMap.erase(RI); |
| 938 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 939 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 940 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 941 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | |
Devang Patel | 6c1fddf | 2009-07-27 18:42:03 +0000 | [diff] [blame] | 943 | llvm::DICompositeType RealDecl = |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 944 | DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit, |
Devang Patel | ab71ff5 | 2009-11-12 00:51:46 +0000 | [diff] [blame] | 945 | Line, Size, Align, 0, 0, llvm::DIType(), |
| 946 | Elements, RuntimeLang); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 947 | |
| 948 | // Now that we have a real decl for the struct, replace anything using the |
| 949 | // old decl with the new one. This will recursively update the debug info. |
Devang Patel | ffffb03 | 2009-11-16 20:09:38 +0000 | [diff] [blame] | 950 | llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 951 | RegionMap[ID] = llvm::WeakVH(RealDecl); |
Devang Patel | fe09eab | 2009-07-13 17:03:14 +0000 | [diff] [blame] | 952 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 953 | return RealDecl; |
| 954 | } |
| 955 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 956 | llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 957 | llvm::DIFile Unit) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 958 | EnumDecl *ED = Ty->getDecl(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 959 | |
| 960 | llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; |
| 961 | |
| 962 | // Create DIEnumerator elements for each enumerator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | for (EnumDecl::enumerator_iterator |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 964 | Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 965 | Enum != EnumEnd; ++Enum) { |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 966 | Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 967 | Enum->getInitVal().getZExtValue())); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 968 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 970 | // Return a CompositeType for the enum itself. |
| 971 | llvm::DIArray EltArray = |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 972 | DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 973 | |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 974 | llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); |
| 975 | unsigned Line = getLineNumber(ED->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 977 | // Size and align of the type. |
Eli Friedman | 3189e4b | 2009-05-04 04:39:55 +0000 | [diff] [blame] | 978 | uint64_t Size = 0; |
| 979 | unsigned Align = 0; |
| 980 | if (!Ty->isIncompleteType()) { |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 981 | Size = CGM.getContext().getTypeSize(Ty); |
| 982 | Align = CGM.getContext().getTypeAlign(Ty); |
Eli Friedman | 3189e4b | 2009-05-04 04:39:55 +0000 | [diff] [blame] | 983 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 985 | llvm::DIType DbgTy = |
| 986 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 987 | Unit, ED->getName(), DefUnit, Line, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 988 | Size, Align, 0, 0, |
| 989 | llvm::DIType(), EltArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 990 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 994 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 995 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
| 996 | return CreateType(RT, Unit); |
| 997 | else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) |
| 998 | return CreateType(ET, Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1000 | return llvm::DIType(); |
| 1001 | } |
| 1002 | |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1003 | llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1004 | llvm::DIFile Unit) { |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1005 | llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
| 1006 | uint64_t NumElems = Ty->getNumElements(); |
| 1007 | if (NumElems > 0) |
| 1008 | --NumElems; |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1009 | |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 1010 | llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems); |
| 1011 | llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1012 | |
| 1013 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1014 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 1015 | |
| 1016 | return |
| 1017 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1018 | Unit, "", Unit, |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1019 | 0, Size, Align, 0, 0, |
| 1020 | ElementTy, SubscriptArray); |
| 1021 | } |
| 1022 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1023 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1024 | llvm::DIFile Unit) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1025 | uint64_t Size; |
| 1026 | uint64_t Align; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
| 1028 | |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1029 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1030 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1031 | Size = 0; |
| 1032 | Align = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1033 | CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1034 | } else if (Ty->isIncompleteArrayType()) { |
| 1035 | Size = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1036 | Align = CGM.getContext().getTypeAlign(Ty->getElementType()); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1037 | } else { |
| 1038 | // Size and align of the whole array, not the element type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1039 | Size = CGM.getContext().getTypeSize(Ty); |
| 1040 | Align = CGM.getContext().getTypeAlign(Ty); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1041 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1043 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 1044 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 1045 | // obvious/recursive way? |
| 1046 | llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; |
| 1047 | QualType EltTy(Ty, 0); |
| 1048 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 1049 | uint64_t Upper = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) |
Devang Patel | 5a6bfe3 | 2009-08-14 20:57:45 +0000 | [diff] [blame] | 1051 | if (CAT->getSize().getZExtValue()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | Upper = CAT->getSize().getZExtValue() - 1; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1053 | // FIXME: Verify this is right for VLAs. |
| 1054 | Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); |
| 1055 | EltTy = Ty->getElementType(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 1056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1058 | llvm::DIArray SubscriptArray = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 1059 | DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1060 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1061 | llvm::DIType DbgTy = |
| 1062 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1063 | Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1064 | 0, Size, Align, 0, 0, |
| 1065 | getOrCreateType(EltTy, Unit), |
| 1066 | SubscriptArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1067 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1070 | llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1071 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1072 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, |
| 1073 | Ty, Ty->getPointeeType(), Unit); |
| 1074 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1075 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1076 | llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1077 | llvm::DIFile U) { |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1078 | QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); |
| 1079 | llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); |
| 1080 | |
| 1081 | if (!Ty->getPointeeType()->isFunctionType()) { |
| 1082 | // We have a data member pointer type. |
| 1083 | return PointerDiffDITy; |
| 1084 | } |
| 1085 | |
| 1086 | // We have a member function pointer type. Treat it as a struct with two |
| 1087 | // ptrdiff_t members. |
| 1088 | std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); |
| 1089 | |
| 1090 | uint64_t FieldOffset = 0; |
| 1091 | llvm::DIDescriptor ElementTypes[2]; |
| 1092 | |
| 1093 | // FIXME: This should probably be a function type instead. |
| 1094 | ElementTypes[0] = |
| 1095 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1096 | "ptr", U, 0, |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1097 | Info.first, Info.second, FieldOffset, 0, |
| 1098 | PointerDiffDITy); |
| 1099 | FieldOffset += Info.first; |
| 1100 | |
| 1101 | ElementTypes[1] = |
| 1102 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1103 | "ptr", U, 0, |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1104 | Info.first, Info.second, FieldOffset, 0, |
| 1105 | PointerDiffDITy); |
| 1106 | |
| 1107 | llvm::DIArray Elements = |
| 1108 | DebugFactory.GetOrCreateArray(&ElementTypes[0], |
| 1109 | llvm::array_lengthof(ElementTypes)); |
| 1110 | |
| 1111 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, |
| 1112 | U, llvm::StringRef("test"), |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1113 | U, 0, FieldOffset, |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1114 | 0, 0, 0, llvm::DIType(), Elements); |
| 1115 | } |
| 1116 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1117 | static QualType UnwrapTypeForDebugInfo(QualType T) { |
| 1118 | do { |
| 1119 | QualType LastT = T; |
| 1120 | switch (T->getTypeClass()) { |
| 1121 | default: |
| 1122 | return T; |
| 1123 | case Type::TemplateSpecialization: |
| 1124 | T = cast<TemplateSpecializationType>(T)->desugar(); |
| 1125 | break; |
| 1126 | case Type::TypeOfExpr: { |
| 1127 | TypeOfExprType *Ty = cast<TypeOfExprType>(T); |
| 1128 | T = Ty->getUnderlyingExpr()->getType(); |
| 1129 | break; |
| 1130 | } |
| 1131 | case Type::TypeOf: |
| 1132 | T = cast<TypeOfType>(T)->getUnderlyingType(); |
| 1133 | break; |
| 1134 | case Type::Decltype: |
| 1135 | T = cast<DecltypeType>(T)->getUnderlyingType(); |
| 1136 | break; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1137 | case Type::Elaborated: |
| 1138 | T = cast<ElaboratedType>(T)->getNamedType(); |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1139 | break; |
| 1140 | case Type::SubstTemplateTypeParm: |
| 1141 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
| 1142 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | assert(T != LastT && "Type unwrapping failed to unwrap!"); |
| 1146 | if (T == LastT) |
| 1147 | return T; |
| 1148 | } while (true); |
| 1149 | |
| 1150 | return T; |
Anders Carlsson | 5b6117a | 2009-11-14 21:08:12 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1153 | /// getOrCreateType - Get the type from the cache or create a new |
| 1154 | /// one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1155 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1156 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1157 | if (Ty.isNull()) |
| 1158 | return llvm::DIType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1160 | // Unwrap the type as needed for debug information. |
| 1161 | Ty = UnwrapTypeForDebugInfo(Ty); |
Anders Carlsson | 5b6117a | 2009-11-14 21:08:12 +0000 | [diff] [blame] | 1162 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1163 | // Check for existing entry. |
Ted Kremenek | 590838b | 2010-03-29 18:29:57 +0000 | [diff] [blame] | 1164 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1165 | TypeCache.find(Ty.getAsOpaquePtr()); |
Daniel Dunbar | 65f13c3 | 2009-09-19 20:17:48 +0000 | [diff] [blame] | 1166 | if (it != TypeCache.end()) { |
| 1167 | // Verify that the debug info still exists. |
| 1168 | if (&*it->second) |
| 1169 | return llvm::DIType(cast<llvm::MDNode>(it->second)); |
| 1170 | } |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1171 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1172 | // Otherwise create the type. |
| 1173 | llvm::DIType Res = CreateTypeNode(Ty, Unit); |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1174 | |
| 1175 | // And update the type cache. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1176 | TypeCache[Ty.getAsOpaquePtr()] = Res; |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1177 | return Res; |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1180 | /// CreateTypeNode - Create a new debug type node. |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1181 | llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1182 | llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1183 | // Handle qualifiers, which recursively handles what they refer to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1184 | if (Ty.hasLocalQualifiers()) |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1185 | return CreateQualifiedType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1187 | const char *Diag = 0; |
| 1188 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1189 | // Work out details of type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1190 | switch (Ty->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1191 | #define TYPE(Class, Base) |
| 1192 | #define ABSTRACT_TYPE(Class, Base) |
| 1193 | #define NON_CANONICAL_TYPE(Class, Base) |
| 1194 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1195 | #include "clang/AST/TypeNodes.def" |
| 1196 | assert(false && "Dependent types cannot show up in debug information"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1197 | |
Anders Carlsson | bfe6995 | 2009-11-06 18:24:04 +0000 | [diff] [blame] | 1198 | // FIXME: Handle these. |
| 1199 | case Type::ExtVector: |
Anders Carlsson | bfe6995 | 2009-11-06 18:24:04 +0000 | [diff] [blame] | 1200 | return llvm::DIType(); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1201 | |
| 1202 | case Type::Vector: |
| 1203 | return CreateType(cast<VectorType>(Ty), Unit); |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 1204 | case Type::ObjCObjectPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1205 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | case Type::ObjCInterface: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1207 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
| 1208 | case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit); |
| 1209 | case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit); |
| 1210 | case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 1211 | case Type::BlockPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1212 | return CreateType(cast<BlockPointerType>(Ty), Unit); |
| 1213 | case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1214 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1215 | case Type::Enum: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1216 | return CreateType(cast<TagType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1217 | case Type::FunctionProto: |
| 1218 | case Type::FunctionNoProto: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1219 | return CreateType(cast<FunctionType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1220 | case Type::ConstantArray: |
| 1221 | case Type::VariableArray: |
| 1222 | case Type::IncompleteArray: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1223 | return CreateType(cast<ArrayType>(Ty), Unit); |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1224 | |
| 1225 | case Type::LValueReference: |
| 1226 | return CreateType(cast<LValueReferenceType>(Ty), Unit); |
| 1227 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1228 | case Type::MemberPointer: |
| 1229 | return CreateType(cast<MemberPointerType>(Ty), Unit); |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1230 | |
| 1231 | case Type::TemplateSpecialization: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1232 | case Type::Elaborated: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1233 | case Type::SubstTemplateTypeParm: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1234 | case Type::TypeOfExpr: |
| 1235 | case Type::TypeOf: |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1236 | case Type::Decltype: |
| 1237 | llvm_unreachable("type should have been unwrapped!"); |
| 1238 | return llvm::DIType(); |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1239 | |
| 1240 | case Type::RValueReference: |
| 1241 | // FIXME: Implement! |
| 1242 | Diag = "rvalue references"; |
| 1243 | break; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1244 | } |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1245 | |
| 1246 | assert(Diag && "Fall through without a diagnostic?"); |
| 1247 | unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error, |
| 1248 | "debug information for %0 is not yet supported"); |
| 1249 | CGM.getDiags().Report(FullSourceLoc(), DiagID) |
| 1250 | << Diag; |
| 1251 | return llvm::DIType(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1254 | /// CreateMemberType - Create new member and increase Offset by FType's size. |
| 1255 | llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, |
| 1256 | llvm::StringRef Name, |
| 1257 | uint64_t *Offset) { |
| 1258 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 1259 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); |
| 1260 | unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); |
| 1261 | llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, |
| 1262 | Unit, Name, Unit, 0, |
| 1263 | FieldSize, FieldAlign, |
| 1264 | *Offset, 0, FieldTy); |
| 1265 | *Offset += FieldSize; |
| 1266 | return Ty; |
| 1267 | } |
| 1268 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1269 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 1270 | /// "llvm.dbg.func.start.". |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1271 | void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1272 | llvm::Function *Fn, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1273 | CGBuilderTy &Builder) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1275 | llvm::StringRef Name; |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1276 | MangleBuffer LinkageName; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1277 | |
| 1278 | const Decl *D = GD.getDecl(); |
| 1279 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1280 | // If there is a DISubprogram for this function available then use it. |
| 1281 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 1282 | FI = SPCache.find(FD); |
| 1283 | if (FI != SPCache.end()) { |
Devang Patel | 0804e6e | 2010-03-08 20:53:17 +0000 | [diff] [blame] | 1284 | llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1285 | if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { |
| 1286 | llvm::MDNode *SPN = SP; |
| 1287 | RegionStack.push_back(SPN); |
| 1288 | RegionMap[D] = llvm::WeakVH(SP); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1289 | return; |
| 1290 | } |
| 1291 | } |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1292 | Name = getFunctionName(FD); |
| 1293 | // Use mangled name as linkage name for c/c++ functions. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1294 | CGM.getMangledName(LinkageName, GD); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1295 | } else { |
| 1296 | // Use llvm function name as linkage name. |
| 1297 | Name = Fn->getName(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1298 | LinkageName.setString(Name); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1299 | } |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1300 | if (!Name.empty() && Name[0] == '\01') |
| 1301 | Name = Name.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
Devang Patel | 970c618 | 2010-04-24 00:49:16 +0000 | [diff] [blame] | 1303 | // It is expected that CurLoc is set before using EmitFunctionStart. |
| 1304 | // Usually, CurLoc points to the left bracket location of compound |
| 1305 | // statement representing function body. |
| 1306 | llvm::DIFile Unit = getOrCreateFile(CurLoc); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1307 | unsigned LineNo = getLineNumber(CurLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1309 | llvm::DISubprogram SP = |
Devang Patel | 970c618 | 2010-04-24 00:49:16 +0000 | [diff] [blame] | 1310 | DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo, |
Mike Stump | 91cc815 | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 1311 | getOrCreateType(FnType, Unit), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1312 | Fn->hasInternalLinkage(), true/*definition*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1314 | // Push function on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1315 | llvm::MDNode *SPN = SP; |
| 1316 | RegionStack.push_back(SPN); |
| 1317 | RegionMap[D] = llvm::WeakVH(SP); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1321 | void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1322 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1324 | // Don't bother if things are the same as last time. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1325 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | if (CurLoc == PrevLoc |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1327 | || (SM.getInstantiationLineNumber(CurLoc) == |
| 1328 | SM.getInstantiationLineNumber(PrevLoc) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1329 | && SM.isFromSameFile(CurLoc, PrevLoc))) |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 1330 | // New Builder may not be in sync with CGDebugInfo. |
| 1331 | if (!Builder.getCurrentDebugLocation().isUnknown()) |
| 1332 | return; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Update last state. |
| 1335 | PrevLoc = CurLoc; |
| 1336 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1337 | llvm::MDNode *Scope = RegionStack.back(); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1338 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc), |
| 1339 | getColumnNumber(CurLoc), |
Chris Lattner | e541d01 | 2010-04-02 20:21:43 +0000 | [diff] [blame] | 1340 | Scope)); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 1344 | /// region - "llvm.dbg.region.start.". |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1345 | void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { |
Devang Patel | 8fae060 | 2009-11-13 19:10:24 +0000 | [diff] [blame] | 1346 | llvm::DIDescriptor D = |
| 1347 | DebugFactory.CreateLexicalBlock(RegionStack.empty() ? |
| 1348 | llvm::DIDescriptor() : |
Devang Patel | d19429f | 2010-02-16 21:41:20 +0000 | [diff] [blame] | 1349 | llvm::DIDescriptor(RegionStack.back()), |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1350 | getLineNumber(CurLoc), |
| 1351 | getColumnNumber(CurLoc)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1352 | llvm::MDNode *DN = D; |
| 1353 | RegionStack.push_back(DN); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 1357 | /// region - "llvm.dbg.region.end." |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1358 | void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1359 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1360 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1361 | // Provide an region stop point. |
| 1362 | EmitStopPoint(Fn, Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1364 | RegionStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1367 | // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. |
| 1368 | // See BuildByRefType. |
| 1369 | llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, |
| 1370 | uint64_t *XOffset) { |
| 1371 | |
| 1372 | llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; |
| 1373 | |
| 1374 | QualType FType; |
| 1375 | uint64_t FieldSize, FieldOffset; |
| 1376 | unsigned FieldAlign; |
| 1377 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1378 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1379 | QualType Type = VD->getType(); |
| 1380 | |
| 1381 | FieldOffset = 0; |
| 1382 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1383 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 1384 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1385 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1386 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 1387 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); |
| 1388 | |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1389 | bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type); |
| 1390 | if (HasCopyAndDispose) { |
| 1391 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1392 | EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", |
| 1393 | &FieldOffset)); |
| 1394 | EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", |
| 1395 | &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 1399 | if (Align > CharUnits::fromQuantity( |
| 1400 | CGM.getContext().Target.getPointerAlign(0) / 8)) { |
| 1401 | unsigned AlignedOffsetInBytes |
| 1402 | = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity()); |
| 1403 | unsigned NumPaddingBytes |
| 1404 | = AlignedOffsetInBytes - FieldOffset/8; |
| 1405 | |
| 1406 | if (NumPaddingBytes > 0) { |
| 1407 | llvm::APInt pad(32, NumPaddingBytes); |
| 1408 | FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, |
| 1409 | pad, ArrayType::Normal, 0); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1410 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | FType = Type; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1415 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1416 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 1417 | FieldAlign = Align.getQuantity()*8; |
| 1418 | |
| 1419 | *XOffset = FieldOffset; |
| 1420 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1421 | VD->getName(), Unit, |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1422 | 0, FieldSize, FieldAlign, |
| 1423 | FieldOffset, 0, FieldTy); |
| 1424 | EltTys.push_back(FieldTy); |
| 1425 | FieldOffset += FieldSize; |
| 1426 | |
| 1427 | llvm::DIArray Elements = |
| 1428 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
| 1429 | |
| 1430 | unsigned Flags = llvm::DIType::FlagBlockByrefStruct; |
| 1431 | |
| 1432 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1433 | Unit, "", Unit, |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1434 | 0, FieldOffset, 0, 0, Flags, |
| 1435 | llvm::DIType(), Elements); |
| 1436 | |
| 1437 | } |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1438 | /// EmitDeclare - Emit local variable declaration debug info. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1439 | void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1440 | llvm::Value *Storage, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1441 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1442 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1443 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1444 | llvm::DIType Ty; |
| 1445 | uint64_t XOffset = 0; |
| 1446 | if (VD->hasAttr<BlocksAttr>()) |
| 1447 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 1448 | else |
| 1449 | Ty = getOrCreateType(VD->getType(), Unit); |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 1450 | |
Devang Patel | f4e54a2 | 2010-05-07 23:05:55 +0000 | [diff] [blame] | 1451 | // If there is not any debug info for type then do not emit debug info |
| 1452 | // for this variable. |
| 1453 | if (!Ty) |
| 1454 | return; |
| 1455 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1456 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1457 | unsigned Line = getLineNumber(VD->getLocation()); |
| 1458 | unsigned Column = getColumnNumber(VD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1460 | // Create the descriptor for the variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | llvm::DIVariable D = |
Devang Patel | 8fae060 | 2009-11-13 19:10:24 +0000 | [diff] [blame] | 1462 | DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()), |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1463 | VD->getName(), |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 1464 | Unit, Line, Ty); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1465 | // Insert an llvm.dbg.declare into the current block. |
Devang Patel | ebf16e8 | 2009-11-11 19:10:19 +0000 | [diff] [blame] | 1466 | llvm::Instruction *Call = |
Devang Patel | a020380 | 2009-11-10 23:07:24 +0000 | [diff] [blame] | 1467 | DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Devang Patel | 23908b8 | 2009-11-12 18:21:39 +0000 | [diff] [blame] | 1468 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1469 | llvm::MDNode *Scope = RegionStack.back(); |
Chris Lattner | e541d01 | 2010-04-02 20:21:43 +0000 | [diff] [blame] | 1470 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1473 | /// EmitDeclare - Emit local variable declaration debug info. |
| 1474 | void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, |
| 1475 | llvm::Value *Storage, CGBuilderTy &Builder, |
| 1476 | CodeGenFunction *CGF) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1477 | const ValueDecl *VD = BDRE->getDecl(); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1478 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1479 | |
Devang Patel | 2b594b9 | 2010-04-26 23:28:46 +0000 | [diff] [blame] | 1480 | if (Builder.GetInsertBlock() == 0) |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1481 | return; |
| 1482 | |
| 1483 | uint64_t XOffset = 0; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1484 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1485 | llvm::DIType Ty; |
| 1486 | if (VD->hasAttr<BlocksAttr>()) |
| 1487 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 1488 | else |
| 1489 | Ty = getOrCreateType(VD->getType(), Unit); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1492 | unsigned Line = getLineNumber(VD->getLocation()); |
| 1493 | unsigned Column = getColumnNumber(VD->getLocation()); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1494 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1495 | CharUnits offset = CGF->BlockDecls[VD]; |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1496 | llvm::SmallVector<llvm::Value *, 9> addr; |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1497 | const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); |
| 1498 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1499 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
| 1500 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1501 | if (BDRE->isByRef()) { |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1502 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1503 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1504 | // offset of __forwarding field |
| 1505 | offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1506 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 1507 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1508 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1509 | // offset of x field |
| 1510 | offset = CharUnits::fromQuantity(XOffset/8); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1511 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | // Create the descriptor for the variable. |
| 1515 | llvm::DIVariable D = |
Chris Lattner | 165714e | 2010-01-25 03:34:56 +0000 | [diff] [blame] | 1516 | DebugFactory.CreateComplexVariable(Tag, |
| 1517 | llvm::DIDescriptor(RegionStack.back()), |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1518 | VD->getName(), Unit, Line, Ty, |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1519 | addr); |
| 1520 | // Insert an llvm.dbg.declare into the current block. |
Devang Patel | ebf16e8 | 2009-11-11 19:10:19 +0000 | [diff] [blame] | 1521 | llvm::Instruction *Call = |
Chris Lattner | 165714e | 2010-01-25 03:34:56 +0000 | [diff] [blame] | 1522 | DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Chris Lattner | d5b8902 | 2009-12-28 21:44:41 +0000 | [diff] [blame] | 1523 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1524 | llvm::MDNode *Scope = RegionStack.back(); |
Devang Patel | f8e10a5 | 2010-05-10 23:48:38 +0000 | [diff] [blame] | 1525 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1528 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1529 | llvm::Value *Storage, |
| 1530 | CGBuilderTy &Builder) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1531 | EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1534 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( |
| 1535 | const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder, |
| 1536 | CodeGenFunction *CGF) { |
| 1537 | EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF); |
| 1538 | } |
| 1539 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1540 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 1541 | /// variable declaration. |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1542 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1543 | CGBuilderTy &Builder) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1544 | EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1549 | /// EmitGlobalVariable - Emit information about a global variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1551 | const VarDecl *D) { |
| 1552 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1553 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1554 | llvm::DIFile Unit = getOrCreateFile(D->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1555 | unsigned LineNo = getLineNumber(D->getLocation()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1556 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1557 | QualType T = D->getType(); |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1558 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1560 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 1561 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1563 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1564 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1566 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1567 | ArrayType::Normal, 0); |
| 1568 | } |
Devang Patel | 5d822f0 | 2010-04-29 17:48:37 +0000 | [diff] [blame] | 1569 | llvm::StringRef DeclName = D->getName(); |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1570 | llvm::DIDescriptor DContext = |
| 1571 | getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit); |
| 1572 | DebugFactory.CreateGlobalVariable(DContext, DeclName, |
Devang Patel | 3358305 | 2010-01-28 23:15:27 +0000 | [diff] [blame] | 1573 | DeclName, llvm::StringRef(), Unit, LineNo, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1574 | getOrCreateType(T, Unit), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1575 | Var->hasInternalLinkage(), |
| 1576 | true/*definition*/, Var); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1579 | /// EmitGlobalVariable - Emit information about an objective-c interface. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1581 | ObjCInterfaceDecl *ID) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1582 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1583 | llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1584 | unsigned LineNo = getLineNumber(ID->getLocation()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1585 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1586 | llvm::StringRef Name = ID->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1587 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1588 | QualType T = CGM.getContext().getObjCInterfaceType(ID); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1589 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1591 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 1592 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1594 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1595 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1597 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1598 | ArrayType::Normal, 0); |
| 1599 | } |
| 1600 | |
Devang Patel | f6a39b7 | 2009-10-20 18:26:30 +0000 | [diff] [blame] | 1601 | DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo, |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1602 | getOrCreateType(T, Unit), |
| 1603 | Var->hasInternalLinkage(), |
| 1604 | true/*definition*/, Var); |
| 1605 | } |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1606 | |
| 1607 | /// getOrCreateNamesSpace - Return namespace descriptor for the given |
| 1608 | /// namespace decl. |
| 1609 | llvm::DINameSpace |
| 1610 | CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl, |
| 1611 | llvm::DIDescriptor Unit) { |
| 1612 | llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = |
| 1613 | NameSpaceCache.find(NSDecl); |
| 1614 | if (I != NameSpaceCache.end()) |
| 1615 | return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); |
| 1616 | |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame^] | 1617 | unsigned LineNo = getLineNumber(NSDecl->getLocation()); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1618 | |
| 1619 | llvm::DIDescriptor Context = |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1620 | getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1621 | llvm::DINameSpace NS = |
| 1622 | DebugFactory.CreateNameSpace(Context, NSDecl->getName(), |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1623 | llvm::DIFile(Unit), LineNo); |
| 1624 | NameSpaceCache[NSDecl] = llvm::WeakVH(NS); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1625 | return NS; |
| 1626 | } |