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