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