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