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