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