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