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