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" |
| 15 | #include "CodeGenModule.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/FileManager.h" |
Devang Patel | 61b5f3e | 2009-07-14 02:47:58 +0000 | [diff] [blame^] | 22 | #include "clang/Basic/TargetInfo.h" |
Devang Patel | 0773903 | 2009-03-27 23:16:32 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CompileOptions.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/DerivedTypes.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/Intrinsics.h" |
| 28 | #include "llvm/Module.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
| 30 | #include "llvm/ADT/SmallVector.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Dwarf.h" |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 32 | #include "llvm/System/Path.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace clang::CodeGen; |
| 36 | |
Devang Patel | 61b5f3e | 2009-07-14 02:47:58 +0000 | [diff] [blame^] | 37 | CGDebugInfo::CGDebugInfo(CodeGenModule *m, TargetInfo *t) |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 38 | : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()), |
| 39 | BlockLiteralGenericSet(false) { |
Devang Patel | 61b5f3e | 2009-07-14 02:47:58 +0000 | [diff] [blame^] | 40 | LLVMMangler = new llvm::Mangler(m->getModule(), t->getUserLabelPrefix(), "."); |
| 41 | // add chars used in ObjC method names so method names aren't mangled |
| 42 | LLVMMangler->markCharAcceptable('['); |
| 43 | LLVMMangler->markCharAcceptable(']'); |
| 44 | LLVMMangler->markCharAcceptable('('); |
| 45 | LLVMMangler->markCharAcceptable(')'); |
| 46 | LLVMMangler->markCharAcceptable('-'); |
| 47 | LLVMMangler->markCharAcceptable('+'); |
| 48 | LLVMMangler->markCharAcceptable(' '); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 51 | CGDebugInfo::~CGDebugInfo() { |
Devang Patel | 61b5f3e | 2009-07-14 02:47:58 +0000 | [diff] [blame^] | 52 | delete LLVMMangler; |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 53 | assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 56 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 57 | if (Loc.isValid()) |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 58 | CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 61 | /// getOrCreateCompileUnit - Get the compile unit from the cache or create a new |
Daniel Dunbar | 25f51dd | 2008-10-24 08:38:36 +0000 | [diff] [blame] | 62 | /// one if necessary. This returns null for invalid source locations. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 63 | llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) { |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 64 | // Get source file information. |
| 65 | const char *FileName = "<unknown>"; |
Devang Patel | 7782022 | 2009-02-24 23:16:03 +0000 | [diff] [blame] | 66 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | adb1a6f | 2009-04-19 06:50:29 +0000 | [diff] [blame] | 67 | unsigned FID = 0; |
Daniel Dunbar | 831570c | 2009-01-22 00:09:25 +0000 | [diff] [blame] | 68 | if (Loc.isValid()) { |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 69 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 70 | FileName = PLoc.getFilename(); |
| 71 | FID = PLoc.getIncludeLoc().getRawEncoding(); |
Daniel Dunbar | 831570c | 2009-01-22 00:09:25 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 74 | // See if this compile unit has been used before. |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 75 | llvm::DICompileUnit &Unit = CompileUnitCache[FID]; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 76 | if (!Unit.isNull()) return Unit; |
Daniel Dunbar | c9abc04 | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 77 | |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 78 | // Get absolute path name. |
| 79 | llvm::sys::Path AbsFileName(FileName); |
| 80 | if (!AbsFileName.isAbsolute()) { |
| 81 | llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory(); |
| 82 | tmp.appendComponent(FileName); |
| 83 | AbsFileName = tmp; |
| 84 | } |
| 85 | |
Devang Patel | 72240d7 | 2009-06-26 18:32:22 +0000 | [diff] [blame] | 86 | // See if thie compile unit is representing main source file. Each source |
| 87 | // file has corresponding compile unit. There is only one main source |
| 88 | // file at a time. |
| 89 | bool isMain = false; |
| 90 | const LangOptions &LO = M->getLangOptions(); |
| 91 | const char *MainFileName = LO.getMainFileName(); |
| 92 | if (isMainCompileUnitCreated == false) { |
| 93 | if (MainFileName) { |
| 94 | if (!strcmp(AbsFileName.getLast().c_str(), MainFileName)) |
| 95 | isMain = true; |
| 96 | } else { |
| 97 | if (Loc.isValid() && SM.isFromMainFile(Loc)) |
| 98 | isMain = true; |
| 99 | } |
| 100 | if (isMain) |
| 101 | isMainCompileUnitCreated = true; |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 102 | } |
Daniel Dunbar | c9abc04 | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 104 | unsigned LangTag; |
| 105 | if (LO.CPlusPlus) { |
| 106 | if (LO.ObjC1) |
| 107 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 108 | else |
| 109 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 110 | } else if (LO.ObjC1) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 111 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 112 | } else if (LO.C99) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 113 | LangTag = llvm::dwarf::DW_LANG_C99; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 114 | } else { |
| 115 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 116 | } |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 117 | |
Chris Lattner | b95ee58 | 2009-05-02 01:04:13 +0000 | [diff] [blame] | 118 | std::string Producer = "clang 1.0";// FIXME: clang version. |
| 119 | bool isOptimized = LO.Optimize; |
Chris Lattner | 4c2577a | 2009-05-02 01:00:04 +0000 | [diff] [blame] | 120 | const char *Flags = ""; // FIXME: Encode command line options. |
| 121 | |
| 122 | // Figure out which version of the ObjC runtime we have. |
| 123 | unsigned RuntimeVers = 0; |
| 124 | if (LO.ObjC1) |
| 125 | RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; |
| 126 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 127 | // Create new compile unit. |
Devang Patel | 72240d7 | 2009-06-26 18:32:22 +0000 | [diff] [blame] | 128 | return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(), |
| 129 | AbsFileName.getDirname(), |
| 130 | Producer, isMain, isOptimized, |
| 131 | Flags, RuntimeVers); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 134 | /// CreateType - Get the Basic type from the cache or create a new |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 135 | /// one if necessary. |
| 136 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 137 | llvm::DICompileUnit Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 138 | unsigned Encoding = 0; |
| 139 | switch (BT->getKind()) { |
| 140 | default: |
| 141 | case BuiltinType::Void: |
| 142 | return llvm::DIType(); |
| 143 | case BuiltinType::UChar: |
| 144 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 145 | case BuiltinType::Char_S: |
| 146 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
| 147 | case BuiltinType::UShort: |
| 148 | case BuiltinType::UInt: |
| 149 | case BuiltinType::ULong: |
| 150 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 151 | case BuiltinType::Short: |
| 152 | case BuiltinType::Int: |
| 153 | case BuiltinType::Long: |
| 154 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 155 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
| 156 | case BuiltinType::Float: |
| 157 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
| 158 | } |
| 159 | // Bit size, align and offset of the type. |
| 160 | uint64_t Size = M->getContext().getTypeSize(BT); |
| 161 | uint64_t Align = M->getContext().getTypeAlign(BT); |
| 162 | uint64_t Offset = 0; |
| 163 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 164 | return DebugFactory.CreateBasicType(Unit, |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 165 | BT->getName(M->getContext().getLangOptions()), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 166 | Unit, 0, Size, Align, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 167 | Offset, /*flags*/ 0, Encoding); |
| 168 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 169 | |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 170 | llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty, |
| 171 | llvm::DICompileUnit Unit) { |
| 172 | // Bit size, align and offset of the type. |
| 173 | unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; |
| 174 | if (Ty->isComplexIntegerType()) |
| 175 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
| 176 | |
| 177 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 178 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 179 | uint64_t Offset = 0; |
| 180 | |
| 181 | return DebugFactory.CreateBasicType(Unit, "complex", |
| 182 | Unit, 0, Size, Align, |
| 183 | Offset, /*flags*/ 0, Encoding); |
| 184 | } |
| 185 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 186 | /// getOrCreateCVRType - Get the CVR qualified type from the cache or create |
| 187 | /// a new one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 188 | llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) { |
| 189 | // We will create one Derived type for one qualifier and recurse to handle any |
| 190 | // additional ones. |
| 191 | llvm::DIType FromTy; |
| 192 | unsigned Tag; |
| 193 | if (Ty.isConstQualified()) { |
| 194 | Tag = llvm::dwarf::DW_TAG_const_type; |
| 195 | Ty.removeConst(); |
| 196 | FromTy = getOrCreateType(Ty, Unit); |
| 197 | } else if (Ty.isVolatileQualified()) { |
| 198 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
| 199 | Ty.removeVolatile(); |
| 200 | FromTy = getOrCreateType(Ty, Unit); |
| 201 | } else { |
| 202 | assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info"); |
| 203 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
| 204 | Ty.removeRestrict(); |
| 205 | FromTy = getOrCreateType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 207 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame] | 208 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 209 | // CVR derived types. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 210 | return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), |
| 211 | 0, 0, 0, 0, 0, FromTy); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 214 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
| 215 | llvm::DICompileUnit Unit) { |
| 216 | llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit); |
| 217 | |
| 218 | // Bit size, align and offset of the type. |
| 219 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 220 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 221 | |
| 222 | return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, |
| 223 | "", llvm::DICompileUnit(), |
| 224 | 0, Size, Align, 0, 0, EltTy); |
| 225 | } |
| 226 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 227 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
| 228 | llvm::DICompileUnit Unit) { |
| 229 | llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 230 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 231 | // Bit size, align and offset of the type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 232 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 233 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 235 | return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, |
| 236 | "", llvm::DICompileUnit(), |
| 237 | 0, Size, Align, 0, 0, EltTy); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 240 | llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, |
| 241 | llvm::DICompileUnit Unit) { |
| 242 | if (BlockLiteralGenericSet) |
| 243 | return BlockLiteralGeneric; |
| 244 | |
| 245 | llvm::DICompileUnit DefUnit; |
| 246 | unsigned Tag = llvm::dwarf::DW_TAG_structure_type; |
| 247 | |
| 248 | llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; |
| 249 | |
| 250 | llvm::DIType FieldTy; |
| 251 | |
| 252 | QualType FType; |
| 253 | uint64_t FieldSize, FieldOffset; |
| 254 | unsigned FieldAlign; |
| 255 | |
| 256 | llvm::DIArray Elements; |
| 257 | llvm::DIType EltTy, DescTy; |
| 258 | |
| 259 | FieldOffset = 0; |
| 260 | FType = M->getContext().UnsignedLongTy; |
| 261 | FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 262 | FieldSize = M->getContext().getTypeSize(FType); |
| 263 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 264 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 265 | "reserved", DefUnit, |
| 266 | 0, FieldSize, FieldAlign, |
| 267 | FieldOffset, 0, FieldTy); |
| 268 | EltTys.push_back(FieldTy); |
| 269 | |
| 270 | FieldOffset += FieldSize; |
| 271 | FType = M->getContext().UnsignedLongTy; |
| 272 | FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 273 | FieldSize = M->getContext().getTypeSize(FType); |
| 274 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 275 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 276 | "Size", DefUnit, |
| 277 | 0, FieldSize, FieldAlign, |
| 278 | FieldOffset, 0, FieldTy); |
| 279 | EltTys.push_back(FieldTy); |
| 280 | |
| 281 | FieldOffset += FieldSize; |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 282 | Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 283 | EltTys.clear(); |
| 284 | |
| 285 | EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor", |
| 286 | DefUnit, 0, FieldOffset, 0, 0, 0, |
| 287 | llvm::DIType(), Elements); |
| 288 | |
| 289 | // Bit size, align and offset of the type. |
| 290 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 291 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 292 | |
| 293 | DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
| 294 | Unit, "", llvm::DICompileUnit(), |
| 295 | 0, Size, Align, 0, 0, EltTy); |
| 296 | |
| 297 | FieldOffset = 0; |
| 298 | FType = M->getContext().getPointerType(M->getContext().VoidTy); |
| 299 | FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 300 | FieldSize = M->getContext().getTypeSize(FType); |
| 301 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 302 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 303 | "__isa", DefUnit, |
| 304 | 0, FieldSize, FieldAlign, |
| 305 | FieldOffset, 0, FieldTy); |
| 306 | EltTys.push_back(FieldTy); |
| 307 | |
| 308 | FieldOffset += FieldSize; |
| 309 | FType = M->getContext().IntTy; |
| 310 | FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 311 | FieldSize = M->getContext().getTypeSize(FType); |
| 312 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 313 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 314 | "__flags", DefUnit, |
| 315 | 0, FieldSize, FieldAlign, |
| 316 | FieldOffset, 0, FieldTy); |
| 317 | EltTys.push_back(FieldTy); |
| 318 | |
| 319 | FieldOffset += FieldSize; |
| 320 | FType = M->getContext().IntTy; |
| 321 | FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 322 | FieldSize = M->getContext().getTypeSize(FType); |
| 323 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 324 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 325 | "__reserved", DefUnit, |
| 326 | 0, FieldSize, FieldAlign, |
| 327 | FieldOffset, 0, FieldTy); |
| 328 | EltTys.push_back(FieldTy); |
| 329 | |
| 330 | FieldOffset += FieldSize; |
| 331 | FType = M->getContext().getPointerType(M->getContext().VoidTy); |
| 332 | FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 333 | FieldSize = M->getContext().getTypeSize(FType); |
| 334 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 335 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 336 | "__FuncPtr", DefUnit, |
| 337 | 0, FieldSize, FieldAlign, |
| 338 | FieldOffset, 0, FieldTy); |
| 339 | EltTys.push_back(FieldTy); |
| 340 | |
| 341 | FieldOffset += FieldSize; |
| 342 | FType = M->getContext().getPointerType(M->getContext().VoidTy); |
| 343 | FieldTy = DescTy; |
| 344 | FieldSize = M->getContext().getTypeSize(Ty); |
| 345 | FieldAlign = M->getContext().getTypeAlign(Ty); |
| 346 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 347 | "__descriptor", DefUnit, |
| 348 | 0, FieldSize, FieldAlign, |
| 349 | FieldOffset, 0, FieldTy); |
| 350 | EltTys.push_back(FieldTy); |
| 351 | |
| 352 | FieldOffset += FieldSize; |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 353 | Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 354 | |
| 355 | EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic", |
| 356 | DefUnit, 0, FieldOffset, 0, 0, 0, |
| 357 | llvm::DIType(), Elements); |
| 358 | |
| 359 | BlockLiteralGenericSet = true; |
| 360 | BlockLiteralGeneric |
| 361 | = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, |
| 362 | "", llvm::DICompileUnit(), |
| 363 | 0, Size, Align, 0, 0, EltTy); |
| 364 | return BlockLiteralGeneric; |
| 365 | } |
| 366 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 367 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, |
| 368 | llvm::DICompileUnit Unit) { |
| 369 | // Typedefs are derived from some other type. If we have a typedef of a |
| 370 | // typedef, make sure to emit the whole chain. |
| 371 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
| 372 | |
| 373 | // We don't set size information, but do specify where the typedef was |
| 374 | // declared. |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 375 | std::string TyName = Ty->getDecl()->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 376 | SourceLocation DefLoc = Ty->getDecl()->getLocation(); |
| 377 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 378 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 379 | SourceManager &SM = M->getContext().getSourceManager(); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 380 | PresumedLoc PLoc = SM.getPresumedLoc(DefLoc); |
| 381 | unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 383 | return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit, |
| 384 | TyName, DefUnit, Line, 0, 0, 0, 0, Src); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 387 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
| 388 | llvm::DICompileUnit Unit) { |
| 389 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 391 | // Add the result type at least. |
| 392 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
| 393 | |
| 394 | // Set up remainder of arguments if there is a prototype. |
| 395 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 396 | if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 397 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 398 | EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); |
| 399 | } else { |
| 400 | // FIXME: Handle () case in C. llvm-gcc doesn't do it either. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 403 | llvm::DIArray EltTypeArray = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 404 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 405 | |
| 406 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
| 407 | Unit, "", llvm::DICompileUnit(), |
| 408 | 0, 0, 0, 0, 0, |
| 409 | llvm::DIType(), EltTypeArray); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 412 | /// CreateType - get structure or union type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 413 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, |
| 414 | llvm::DICompileUnit Unit) { |
Douglas Gregor | a4c46df | 2008-12-11 17:59:21 +0000 | [diff] [blame] | 415 | RecordDecl *Decl = Ty->getDecl(); |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 417 | unsigned Tag; |
| 418 | if (Decl->isStruct()) |
| 419 | Tag = llvm::dwarf::DW_TAG_structure_type; |
| 420 | else if (Decl->isUnion()) |
| 421 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 422 | else { |
| 423 | assert(Decl->isClass() && "Unknown RecordType!"); |
| 424 | Tag = llvm::dwarf::DW_TAG_class_type; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 427 | SourceManager &SM = M->getContext().getSourceManager(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 429 | // Get overall information about the record type for the debug info. |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 430 | std::string Name = Decl->getNameAsString(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 431 | |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 432 | PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); |
Chris Lattner | d37d9b5 | 2009-05-05 05:16:17 +0000 | [diff] [blame] | 433 | llvm::DICompileUnit DefUnit; |
| 434 | unsigned Line = 0; |
| 435 | if (!PLoc.isInvalid()) { |
| 436 | DefUnit = getOrCreateCompileUnit(Decl->getLocation()); |
| 437 | Line = PLoc.getLine(); |
| 438 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 439 | |
| 440 | // Records and classes and unions can all be recursive. To handle them, we |
| 441 | // first generate a debug descriptor for the struct as a forward declaration. |
| 442 | // Then (if it is a definition) we go through and get debug info for all of |
| 443 | // its members. Finally, we create a descriptor for the complete type (which |
| 444 | // may refer to the forward decl if the struct is recursive) and replace all |
| 445 | // uses of the forward declaration with the final definition. |
| 446 | llvm::DIType FwdDecl = |
| 447 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, |
| 448 | llvm::DIType(), llvm::DIArray()); |
| 449 | |
| 450 | // If this is just a forward declaration, return it. |
| 451 | if (!Decl->getDefinition(M->getContext())) |
| 452 | return FwdDecl; |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 454 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 455 | // it. |
| 456 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
| 457 | |
| 458 | // Convert all the elements. |
| 459 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 460 | |
| 461 | const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl); |
| 462 | |
| 463 | unsigned FieldNo = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 464 | for (RecordDecl::field_iterator I = Decl->field_begin(), |
| 465 | E = Decl->field_end(); |
Douglas Gregor | a4c46df | 2008-12-11 17:59:21 +0000 | [diff] [blame] | 466 | I != E; ++I, ++FieldNo) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 467 | FieldDecl *Field = *I; |
| 468 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 469 | |
| 470 | std::string FieldName = Field->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 471 | |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 472 | // Ignore unnamed fields. |
| 473 | if (FieldName.empty()) |
| 474 | continue; |
| 475 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 476 | // Get the location for the field. |
| 477 | SourceLocation FieldDefLoc = Field->getLocation(); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 478 | PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); |
Chris Lattner | d37d9b5 | 2009-05-05 05:16:17 +0000 | [diff] [blame] | 479 | llvm::DICompileUnit FieldDefUnit; |
| 480 | unsigned FieldLine = 0; |
| 481 | |
| 482 | if (!PLoc.isInvalid()) { |
| 483 | FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); |
| 484 | FieldLine = PLoc.getLine(); |
| 485 | } |
Devang Patel | ec9b5d5 | 2009-03-16 23:47:53 +0000 | [diff] [blame] | 486 | |
| 487 | QualType FType = Field->getType(); |
| 488 | uint64_t FieldSize = 0; |
| 489 | unsigned FieldAlign = 0; |
| 490 | if (!FType->isIncompleteArrayType()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 491 | |
Devang Patel | ec9b5d5 | 2009-03-16 23:47:53 +0000 | [diff] [blame] | 492 | // Bit size, align and offset of the type. |
| 493 | FieldSize = M->getContext().getTypeSize(FType); |
| 494 | Expr *BitWidth = Field->getBitWidth(); |
| 495 | if (BitWidth) |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 496 | FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue(); |
Devang Patel | ec9b5d5 | 2009-03-16 23:47:53 +0000 | [diff] [blame] | 497 | |
| 498 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 499 | } |
| 500 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 501 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 502 | |
| 503 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 504 | // struct. FIXME: This is an absolutely insane way to capture this |
| 505 | // information. When we gut debug info, this should be fixed. |
| 506 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 507 | FieldName, FieldDefUnit, |
| 508 | FieldLine, FieldSize, FieldAlign, |
| 509 | FieldOffset, 0, FieldTy); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 510 | EltTys.push_back(FieldTy); |
| 511 | } |
| 512 | |
| 513 | llvm::DIArray Elements = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 514 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 515 | |
| 516 | // Bit size, align and offset of the type. |
| 517 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 518 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 519 | |
| 520 | llvm::DIType RealDecl = |
| 521 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, |
| 522 | Align, 0, 0, llvm::DIType(), Elements); |
| 523 | |
| 524 | // Now that we have a real decl for the struct, replace anything using the |
| 525 | // old decl with the new one. This will recursively update the debug info. |
| 526 | FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); |
| 527 | FwdDecl.getGV()->eraseFromParent(); |
Devang Patel | fe09eab | 2009-07-13 17:03:14 +0000 | [diff] [blame] | 528 | |
| 529 | // Update TypeCache. |
| 530 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 531 | return RealDecl; |
| 532 | } |
| 533 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 534 | /// CreateType - get objective-c interface type. |
| 535 | llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
| 536 | llvm::DICompileUnit Unit) { |
| 537 | ObjCInterfaceDecl *Decl = Ty->getDecl(); |
| 538 | |
| 539 | unsigned Tag = llvm::dwarf::DW_TAG_structure_type; |
| 540 | SourceManager &SM = M->getContext().getSourceManager(); |
| 541 | |
| 542 | // Get overall information about the record type for the debug info. |
| 543 | std::string Name = Decl->getNameAsString(); |
| 544 | |
| 545 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation()); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 546 | PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); |
| 547 | unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); |
| 548 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 549 | |
Daniel Dunbar | d86d336 | 2009-05-18 20:51:58 +0000 | [diff] [blame] | 550 | unsigned RuntimeLang = DefUnit.getLanguage(); |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 551 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 552 | // To handle recursive interface, we |
| 553 | // first generate a debug descriptor for the struct as a forward declaration. |
| 554 | // Then (if it is a definition) we go through and get debug info for all of |
| 555 | // its members. Finally, we create a descriptor for the complete type (which |
| 556 | // may refer to the forward decl if the struct is recursive) and replace all |
| 557 | // uses of the forward declaration with the final definition. |
| 558 | llvm::DIType FwdDecl = |
| 559 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 560 | llvm::DIType(), llvm::DIArray(), |
| 561 | RuntimeLang); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 562 | |
| 563 | // If this is just a forward declaration, return it. |
| 564 | if (Decl->isForwardDecl()) |
| 565 | return FwdDecl; |
| 566 | |
| 567 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 568 | // it. |
| 569 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
| 570 | |
| 571 | // Convert all the elements. |
| 572 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 573 | |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 574 | ObjCInterfaceDecl *SClass = Decl->getSuperClass(); |
| 575 | if (SClass) { |
| 576 | llvm::DIType SClassTy = |
| 577 | getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit); |
| 578 | llvm::DIType InhTag = |
| 579 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, |
Chris Lattner | 9e55b8a | 2009-05-05 05:05:36 +0000 | [diff] [blame] | 580 | Unit, "", llvm::DICompileUnit(), 0, 0, 0, |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 581 | 0 /* offset */, 0, SClassTy); |
| 582 | EltTys.push_back(InhTag); |
| 583 | } |
| 584 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 585 | const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl); |
| 586 | |
| 587 | unsigned FieldNo = 0; |
| 588 | for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(), |
| 589 | E = Decl->ivar_end(); I != E; ++I, ++FieldNo) { |
| 590 | ObjCIvarDecl *Field = *I; |
| 591 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
| 592 | |
| 593 | std::string FieldName = Field->getNameAsString(); |
| 594 | |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 595 | // Ignore unnamed fields. |
| 596 | if (FieldName.empty()) |
| 597 | continue; |
| 598 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 599 | // Get the location for the field. |
| 600 | SourceLocation FieldDefLoc = Field->getLocation(); |
| 601 | llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 602 | PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); |
| 603 | unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine(); |
| 604 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 605 | |
| 606 | QualType FType = Field->getType(); |
| 607 | uint64_t FieldSize = 0; |
| 608 | unsigned FieldAlign = 0; |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 609 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 610 | if (!FType->isIncompleteArrayType()) { |
| 611 | |
| 612 | // Bit size, align and offset of the type. |
| 613 | FieldSize = M->getContext().getTypeSize(FType); |
| 614 | Expr *BitWidth = Field->getBitWidth(); |
| 615 | if (BitWidth) |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 616 | FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue(); |
| 617 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 618 | FieldAlign = M->getContext().getTypeAlign(FType); |
| 619 | } |
| 620 | |
| 621 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 622 | |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 623 | unsigned Flags = 0; |
| 624 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
| 625 | Flags = llvm::DIType::FlagProtected; |
| 626 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
| 627 | Flags = llvm::DIType::FlagPrivate; |
| 628 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 629 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 630 | // struct. FIXME: This is an absolutely insane way to capture this |
| 631 | // information. When we gut debug info, this should be fixed. |
| 632 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 633 | FieldName, FieldDefUnit, |
| 634 | FieldLine, FieldSize, FieldAlign, |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 635 | FieldOffset, Flags, FieldTy); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 636 | EltTys.push_back(FieldTy); |
| 637 | } |
| 638 | |
| 639 | llvm::DIArray Elements = |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 640 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 641 | |
| 642 | // Bit size, align and offset of the type. |
| 643 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 644 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 645 | |
| 646 | llvm::DIType RealDecl = |
| 647 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 648 | Align, 0, 0, llvm::DIType(), Elements, |
| 649 | RuntimeLang); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 650 | |
| 651 | // Now that we have a real decl for the struct, replace anything using the |
| 652 | // old decl with the new one. This will recursively update the debug info. |
| 653 | FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); |
| 654 | FwdDecl.getGV()->eraseFromParent(); |
Devang Patel | fe09eab | 2009-07-13 17:03:14 +0000 | [diff] [blame] | 655 | |
| 656 | // Update TypeCache. |
| 657 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 658 | return RealDecl; |
| 659 | } |
| 660 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 661 | llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, |
| 662 | llvm::DICompileUnit Unit) { |
| 663 | EnumDecl *Decl = Ty->getDecl(); |
| 664 | |
| 665 | llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; |
| 666 | |
| 667 | // Create DIEnumerator elements for each enumerator. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 668 | for (EnumDecl::enumerator_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 669 | Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 670 | Enum != EnumEnd; ++Enum) { |
| 671 | Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(), |
| 672 | Enum->getInitVal().getZExtValue())); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // Return a CompositeType for the enum itself. |
| 676 | llvm::DIArray EltArray = |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 677 | DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 678 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 679 | std::string EnumName = Decl->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 680 | SourceLocation DefLoc = Decl->getLocation(); |
| 681 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); |
| 682 | SourceManager &SM = M->getContext().getSourceManager(); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 683 | PresumedLoc PLoc = SM.getPresumedLoc(DefLoc); |
| 684 | unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); |
| 685 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 686 | |
| 687 | // Size and align of the type. |
Eli Friedman | 3189e4b | 2009-05-04 04:39:55 +0000 | [diff] [blame] | 688 | uint64_t Size = 0; |
| 689 | unsigned Align = 0; |
| 690 | if (!Ty->isIncompleteType()) { |
| 691 | Size = M->getContext().getTypeSize(Ty); |
| 692 | Align = M->getContext().getTypeAlign(Ty); |
| 693 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 694 | |
| 695 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, |
| 696 | Unit, EnumName, DefUnit, Line, |
| 697 | Size, Align, 0, 0, |
| 698 | llvm::DIType(), EltArray); |
| 699 | } |
| 700 | |
| 701 | llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, |
| 702 | llvm::DICompileUnit Unit) { |
| 703 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
| 704 | return CreateType(RT, Unit); |
| 705 | else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) |
| 706 | return CreateType(ET, Unit); |
| 707 | |
| 708 | return llvm::DIType(); |
| 709 | } |
| 710 | |
| 711 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
| 712 | llvm::DICompileUnit Unit) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 713 | uint64_t Size; |
| 714 | uint64_t Align; |
| 715 | |
| 716 | |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 717 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 718 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 719 | Size = 0; |
| 720 | Align = |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 721 | M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT)); |
| 722 | } else if (Ty->isIncompleteArrayType()) { |
| 723 | Size = 0; |
| 724 | Align = M->getContext().getTypeAlign(Ty->getElementType()); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 725 | } else { |
| 726 | // Size and align of the whole array, not the element type. |
| 727 | Size = M->getContext().getTypeSize(Ty); |
| 728 | Align = M->getContext().getTypeAlign(Ty); |
| 729 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 730 | |
| 731 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 732 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 733 | // obvious/recursive way? |
| 734 | llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; |
| 735 | QualType EltTy(Ty, 0); |
| 736 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 737 | uint64_t Upper = 0; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 738 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) |
| 739 | Upper = CAT->getSize().getZExtValue() - 1; |
| 740 | // FIXME: Verify this is right for VLAs. |
| 741 | Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); |
| 742 | EltTy = Ty->getElementType(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 743 | } |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 744 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 745 | llvm::DIArray SubscriptArray = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 746 | DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 747 | |
| 748 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, |
| 749 | Unit, "", llvm::DICompileUnit(), |
| 750 | 0, Size, Align, 0, 0, |
| 751 | getOrCreateType(EltTy, Unit), |
| 752 | SubscriptArray); |
| 753 | } |
| 754 | |
| 755 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 756 | /// getOrCreateType - Get the type from the cache or create a new |
| 757 | /// one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 758 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, |
| 759 | llvm::DICompileUnit Unit) { |
| 760 | if (Ty.isNull()) |
| 761 | return llvm::DIType(); |
| 762 | |
Devang Patel | 0ae3d60 | 2009-07-13 16:15:54 +0000 | [diff] [blame] | 763 | // Check TypeCache first. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 764 | llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()]; |
| 765 | if (!Slot.isNull()) return Slot; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 767 | // Handle CVR qualifiers, which recursively handles what they refer to. |
| 768 | if (Ty.getCVRQualifiers()) |
| 769 | return Slot = CreateCVRType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 770 | |
| 771 | // Work out details of type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 772 | switch (Ty->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 773 | #define TYPE(Class, Base) |
| 774 | #define ABSTRACT_TYPE(Class, Base) |
| 775 | #define NON_CANONICAL_TYPE(Class, Base) |
| 776 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 777 | #include "clang/AST/TypeNodes.def" |
| 778 | assert(false && "Dependent types cannot show up in debug information"); |
| 779 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 780 | case Type::LValueReference: |
| 781 | case Type::RValueReference: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 782 | case Type::Vector: |
| 783 | case Type::ExtVector: |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 784 | case Type::ExtQual: |
Eli Friedman | 00524e3 | 2009-02-27 23:15:07 +0000 | [diff] [blame] | 785 | case Type::FixedWidthInt: |
Eli Friedman | 00524e3 | 2009-02-27 23:15:07 +0000 | [diff] [blame] | 786 | case Type::MemberPointer: |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 787 | case Type::TemplateSpecialization: |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 788 | case Type::QualifiedName: |
Eli Friedman | 00524e3 | 2009-02-27 23:15:07 +0000 | [diff] [blame] | 789 | // Unsupported types |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 790 | return llvm::DIType(); |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 791 | case Type::ObjCObjectPointer: |
| 792 | return Slot = CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
Chris Lattner | aa2b579 | 2009-04-22 06:58:56 +0000 | [diff] [blame] | 793 | case Type::ObjCQualifiedInterface: // Drop protocols from interface. |
Devang Patel | e798706 | 2009-03-02 17:58:28 +0000 | [diff] [blame] | 794 | case Type::ObjCInterface: |
Chris Lattner | aa2b579 | 2009-04-22 06:58:56 +0000 | [diff] [blame] | 795 | return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
| 796 | case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit); |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 797 | case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit); |
Chris Lattner | aa2b579 | 2009-04-22 06:58:56 +0000 | [diff] [blame] | 798 | case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 799 | case Type::BlockPointer: |
| 800 | return Slot = CreateType(cast<BlockPointerType>(Ty), Unit); |
Chris Lattner | aa2b579 | 2009-04-22 06:58:56 +0000 | [diff] [blame] | 801 | case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 802 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 803 | case Type::Enum: |
Chris Lattner | aa2b579 | 2009-04-22 06:58:56 +0000 | [diff] [blame] | 804 | return Slot = CreateType(cast<TagType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 805 | case Type::FunctionProto: |
| 806 | case Type::FunctionNoProto: |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 807 | return Slot = CreateType(cast<FunctionType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 808 | |
| 809 | case Type::ConstantArray: |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 810 | case Type::ConstantArrayWithExpr: |
| 811 | case Type::ConstantArrayWithoutExpr: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 812 | case Type::VariableArray: |
| 813 | case Type::IncompleteArray: |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 814 | return Slot = CreateType(cast<ArrayType>(Ty), Unit); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 815 | case Type::TypeOfExpr: |
| 816 | return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr() |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 817 | ->getType(), Unit); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 818 | case Type::TypeOf: |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 819 | return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), |
| 820 | Unit); |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 821 | case Type::Decltype: |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 822 | return Slot = getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), |
| 823 | Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 824 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 825 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 826 | return Slot; |
| 827 | } |
| 828 | |
| 829 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 830 | /// "llvm.dbg.func.start.". |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 831 | void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 832 | llvm::Function *Fn, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 833 | CGBuilderTy &Builder) { |
Daniel Dunbar | a289393 | 2009-05-13 23:08:57 +0000 | [diff] [blame] | 834 | // Skip the asm prefix if it exists. |
Daniel Dunbar | bbd53af | 2009-05-14 01:45:24 +0000 | [diff] [blame] | 835 | // |
| 836 | // FIXME: This should probably be the unmangled name? |
Daniel Dunbar | a289393 | 2009-05-13 23:08:57 +0000 | [diff] [blame] | 837 | if (Name[0] == '\01') |
| 838 | ++Name; |
| 839 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 840 | // FIXME: Why is this using CurLoc??? |
| 841 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 842 | SourceManager &SM = M->getContext().getSourceManager(); |
Devang Patel | 0f78fea | 2009-04-08 19:47:04 +0000 | [diff] [blame] | 843 | unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 844 | |
| 845 | llvm::DISubprogram SP = |
Devang Patel | 61b5f3e | 2009-07-14 02:47:58 +0000 | [diff] [blame^] | 846 | DebugFactory.CreateSubprogram(Unit, Name, Name, LLVMMangler->getValueName(Fn), |
| 847 | Unit, LineNo, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 848 | getOrCreateType(ReturnType, Unit), |
| 849 | Fn->hasInternalLinkage(), true/*definition*/); |
| 850 | |
| 851 | DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock()); |
| 852 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 853 | // Push function on region stack. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 854 | RegionStack.push_back(SP); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 858 | void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 859 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
| 860 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 861 | // Don't bother if things are the same as last time. |
| 862 | SourceManager &SM = M->getContext().getSourceManager(); |
| 863 | if (CurLoc == PrevLoc |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 864 | || (SM.getInstantiationLineNumber(CurLoc) == |
| 865 | SM.getInstantiationLineNumber(PrevLoc) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 866 | && SM.isFromSameFile(CurLoc, PrevLoc))) |
| 867 | return; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 868 | |
| 869 | // Update last state. |
| 870 | PrevLoc = CurLoc; |
| 871 | |
| 872 | // Get the appropriate compile unit. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 873 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); |
Devang Patel | 0f78fea | 2009-04-08 19:47:04 +0000 | [diff] [blame] | 874 | PresumedLoc PLoc = SM.getPresumedLoc(CurLoc); |
| 875 | DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 876 | Builder.GetInsertBlock()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 880 | /// region - "llvm.dbg.region.start.". |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 881 | void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { |
| 882 | llvm::DIDescriptor D; |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 883 | if (!RegionStack.empty()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 884 | D = RegionStack.back(); |
| 885 | D = DebugFactory.CreateBlock(D); |
| 886 | RegionStack.push_back(D); |
| 887 | DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 891 | /// region - "llvm.dbg.region.end." |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 892 | void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 893 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 894 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 895 | // Provide an region stop point. |
| 896 | EmitStopPoint(Fn, Builder); |
| 897 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 898 | DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 899 | RegionStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 902 | /// EmitDeclare - Emit local variable declaration debug info. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 903 | void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag, |
| 904 | llvm::Value *Storage, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 905 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 906 | |
Devang Patel | 0773903 | 2009-03-27 23:16:32 +0000 | [diff] [blame] | 907 | // Do not emit variable debug information while generating optimized code. |
| 908 | // The llvm optimizer and code generator are not yet ready to support |
| 909 | // optimized code debugging. |
| 910 | const CompileOptions &CO = M->getCompileOpts(); |
| 911 | if (CO.OptimizationLevel) |
| 912 | return; |
| 913 | |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 914 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); |
| 915 | llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit); |
| 916 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 917 | // Get location information. |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 918 | SourceManager &SM = M->getContext().getSourceManager(); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 919 | PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 920 | unsigned Line = 0; |
| 921 | if (!PLoc.isInvalid()) |
| 922 | Line = PLoc.getLine(); |
| 923 | else |
| 924 | Unit = llvm::DICompileUnit(); |
| 925 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 926 | |
| 927 | // Create the descriptor for the variable. |
| 928 | llvm::DIVariable D = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 929 | DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(), |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 930 | Unit, Line, Ty); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 931 | // Insert an llvm.dbg.declare into the current block. |
| 932 | DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 935 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl, |
| 936 | llvm::Value *Storage, |
| 937 | CGBuilderTy &Builder) { |
| 938 | EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); |
| 939 | } |
| 940 | |
| 941 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 942 | /// variable declaration. |
| 943 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, |
| 944 | CGBuilderTy &Builder) { |
| 945 | EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); |
| 946 | } |
| 947 | |
| 948 | |
| 949 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 950 | /// EmitGlobalVariable - Emit information about a global variable. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 951 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
| 952 | const VarDecl *Decl) { |
Devang Patel | 0773903 | 2009-03-27 23:16:32 +0000 | [diff] [blame] | 953 | |
| 954 | // Do not emit variable debug information while generating optimized code. |
| 955 | // The llvm optimizer and code generator are not yet ready to support |
| 956 | // optimized code debugging. |
| 957 | const CompileOptions &CO = M->getCompileOpts(); |
| 958 | if (CO.OptimizationLevel) |
| 959 | return; |
| 960 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 961 | // Create global variable debug descriptor. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 962 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 963 | SourceManager &SM = M->getContext().getSourceManager(); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 964 | PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); |
| 965 | unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 966 | |
| 967 | std::string Name = Decl->getNameAsString(); |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 968 | |
| 969 | QualType T = Decl->getType(); |
| 970 | if (T->isIncompleteArrayType()) { |
| 971 | |
| 972 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 973 | llvm::APSInt ConstVal(32); |
| 974 | |
| 975 | ConstVal = 1; |
| 976 | QualType ET = M->getContext().getAsArrayType(T)->getElementType(); |
| 977 | |
| 978 | T = M->getContext().getConstantArrayType(ET, ConstVal, |
| 979 | ArrayType::Normal, 0); |
| 980 | } |
| 981 | |
Devang Patel | 61b5f3e | 2009-07-14 02:47:58 +0000 | [diff] [blame^] | 982 | DebugFactory.CreateGlobalVariable(Unit, Name, Name, |
| 983 | LLVMMangler->getValueName(Var), |
| 984 | Unit, LineNo, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 985 | getOrCreateType(T, Unit), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 986 | Var->hasInternalLinkage(), |
| 987 | true/*definition*/, Var); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 990 | /// EmitGlobalVariable - Emit information about an objective-c interface. |
| 991 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
| 992 | ObjCInterfaceDecl *Decl) { |
| 993 | // Create global variable debug descriptor. |
| 994 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); |
| 995 | SourceManager &SM = M->getContext().getSourceManager(); |
Devang Patel | 4f6fa23 | 2009-04-17 21:35:15 +0000 | [diff] [blame] | 996 | PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); |
| 997 | unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 998 | |
| 999 | std::string Name = Decl->getNameAsString(); |
| 1000 | |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1001 | QualType T = M->getContext().getObjCInterfaceType(Decl); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1002 | if (T->isIncompleteArrayType()) { |
| 1003 | |
| 1004 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 1005 | llvm::APSInt ConstVal(32); |
| 1006 | |
| 1007 | ConstVal = 1; |
| 1008 | QualType ET = M->getContext().getAsArrayType(T)->getElementType(); |
| 1009 | |
| 1010 | T = M->getContext().getConstantArrayType(ET, ConstVal, |
| 1011 | ArrayType::Normal, 0); |
| 1012 | } |
| 1013 | |
Devang Patel | 61b5f3e | 2009-07-14 02:47:58 +0000 | [diff] [blame^] | 1014 | DebugFactory.CreateGlobalVariable(Unit, Name, Name, |
| 1015 | LLVMMangler->getValueName(Var), |
| 1016 | Unit, LineNo, |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1017 | getOrCreateType(T, Unit), |
| 1018 | Var->hasInternalLinkage(), |
| 1019 | true/*definition*/, Var); |
| 1020 | } |
| 1021 | |