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