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" |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
| 24 | #include "clang/Basic/FileManager.h" |
Mike Stump | 5a86217 | 2009-09-15 21:48:34 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Version.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
| 28 | #include "llvm/DerivedTypes.h" |
| 29 | #include "llvm/Instructions.h" |
| 30 | #include "llvm/Intrinsics.h" |
| 31 | #include "llvm/Module.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
| 33 | #include "llvm/ADT/SmallVector.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Dwarf.h" |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 35 | #include "llvm/System/Path.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | using namespace clang::CodeGen; |
| 39 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 40 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 41 | : CGM(CGM), DebugFactory(CGM.getModule()), |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 42 | BlockLiteralGenericSet(false) { |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 43 | CreateCompileUnit(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 46 | CGDebugInfo::~CGDebugInfo() { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 47 | assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 50 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 51 | if (Loc.isValid()) |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 52 | CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Devang Patel | 3358305 | 2010-01-28 23:15:27 +0000 | [diff] [blame] | 55 | /// getContextDescriptor - Get context info for the decl. |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 56 | llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context, |
Devang Patel | 3358305 | 2010-01-28 23:15:27 +0000 | [diff] [blame] | 57 | llvm::DIDescriptor &CompileUnit) { |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 58 | if (!Context) |
| 59 | return CompileUnit; |
| 60 | |
| 61 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator |
| 62 | I = RegionMap.find(Context); |
| 63 | if (I != RegionMap.end()) |
| 64 | return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second)); |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 65 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 66 | // Check namespace. |
| 67 | if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
| 68 | return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit)); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 69 | |
| 70 | if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) { |
| 71 | if (!RDecl->isDependentType()) { |
| 72 | llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), |
| 73 | llvm::DIFile(CompileUnit)); |
| 74 | return llvm::DIDescriptor(Ty); |
| 75 | } |
| 76 | } |
Devang Patel | 979ec2e | 2009-10-06 00:35:31 +0000 | [diff] [blame] | 77 | return CompileUnit; |
| 78 | } |
| 79 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 80 | /// getFunctionName - Get function name for the given FunctionDecl. If the |
| 81 | /// name is constructred on demand (e.g. C++ destructor) then the name |
| 82 | /// is stored on the side. |
| 83 | llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
| 84 | assert (FD && "Invalid FunctionDecl!"); |
| 85 | IdentifierInfo *FII = FD->getIdentifier(); |
| 86 | if (FII) |
| 87 | return FII->getName(); |
| 88 | |
| 89 | // Otherwise construct human readable name for debug info. |
| 90 | std::string NS = FD->getNameAsString(); |
| 91 | |
| 92 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 93 | char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); |
Benjamin Kramer | 1b627dc | 2010-01-23 18:16:07 +0000 | [diff] [blame] | 94 | memcpy(StrPtr, NS.data(), NS.length()); |
| 95 | return llvm::StringRef(StrPtr, NS.length()); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 98 | /// getClassName - Get class name including template argument list. |
| 99 | llvm::StringRef |
| 100 | CGDebugInfo::getClassName(RecordDecl *RD) { |
| 101 | ClassTemplateSpecializationDecl *Spec |
| 102 | = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 103 | if (!Spec) |
| 104 | return RD->getName(); |
| 105 | |
| 106 | const TemplateArgument *Args; |
| 107 | unsigned NumArgs; |
| 108 | std::string Buffer; |
| 109 | if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { |
| 110 | const TemplateSpecializationType *TST = |
| 111 | cast<TemplateSpecializationType>(TAW->getType()); |
| 112 | Args = TST->getArgs(); |
| 113 | NumArgs = TST->getNumArgs(); |
| 114 | } else { |
| 115 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 116 | Args = TemplateArgs.getFlatArgumentList(); |
| 117 | NumArgs = TemplateArgs.flat_size(); |
| 118 | } |
| 119 | Buffer = RD->getIdentifier()->getNameStart(); |
| 120 | PrintingPolicy Policy(CGM.getLangOptions()); |
| 121 | Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, |
| 122 | NumArgs, |
| 123 | Policy); |
| 124 | |
| 125 | // Copy this name on the side and use its reference. |
| 126 | char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length()); |
| 127 | memcpy(StrPtr, Buffer.data(), Buffer.length()); |
| 128 | return llvm::StringRef(StrPtr, Buffer.length()); |
| 129 | |
| 130 | } |
| 131 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 132 | /// getOrCreateFile - Get the file debug info descriptor for the input location. |
| 133 | llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 134 | if (!Loc.isValid()) |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 135 | // If Location is not valid then use main input file. |
| 136 | return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(), |
| 137 | TheCU); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 138 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 139 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 140 | |
| 141 | // Cache the results. |
| 142 | const char *fname = PLoc.getFilename(); |
| 143 | llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = |
| 144 | DIFileCache.find(fname); |
| 145 | |
| 146 | if (it != DIFileCache.end()) { |
| 147 | // Verify that the information still exists. |
| 148 | if (&*it->second) |
| 149 | return llvm::DIFile(cast<llvm::MDNode>(it->second)); |
| 150 | } |
| 151 | |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 152 | llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(), |
| 153 | getCurrentDirname(), TheCU); |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 154 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 155 | DIFileCache[fname] = F; |
Ted Kremenek | 9c25039 | 2010-03-30 00:27:51 +0000 | [diff] [blame] | 156 | return F; |
| 157 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 158 | } |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 159 | |
| 160 | /// getLineNumber - Get line number for the location. If location is invalid |
| 161 | /// then use current location. |
| 162 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
| 163 | assert (CurLoc.isValid() && "Invalid current location!"); |
| 164 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 165 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| 166 | return PLoc.getLine(); |
| 167 | } |
| 168 | |
| 169 | /// getColumnNumber - Get column number for the location. If location is |
| 170 | /// invalid then use current location. |
| 171 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) { |
| 172 | assert (CurLoc.isValid() && "Invalid current location!"); |
| 173 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 174 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| 175 | return PLoc.getColumn(); |
| 176 | } |
| 177 | |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 178 | llvm::StringRef CGDebugInfo::getCurrentDirname() { |
| 179 | if (!CWDName.empty()) |
| 180 | return CWDName; |
| 181 | char *CompDirnamePtr = NULL; |
| 182 | llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory(); |
| 183 | CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size()); |
| 184 | memcpy(CompDirnamePtr, CWD.c_str(), CWD.size()); |
| 185 | return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size()); |
| 186 | } |
| 187 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 188 | /// CreateCompileUnit - Create new compile unit. |
| 189 | void CGDebugInfo::CreateCompileUnit() { |
| 190 | |
| 191 | // Get absolute path name. |
Douglas Gregor | ac91b4c | 2010-03-18 23:46:43 +0000 | [diff] [blame] | 192 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 193 | std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| 194 | if (MainFileName.empty()) |
Devang Patel | 22fe585 | 2010-03-12 21:04:27 +0000 | [diff] [blame] | 195 | MainFileName = "<unknown>"; |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | f6728fc | 2010-03-22 21:28:29 +0000 | [diff] [blame] | 197 | // The main file name provided via the "-main-file-name" option contains just |
| 198 | // the file name itself with no path information. This file name may have had |
| 199 | // a relative path, so we look into the actual file entry for the main |
| 200 | // file to determine the real absolute path for the file. |
Devang Patel | 6e6bc39 | 2010-07-23 23:04:28 +0000 | [diff] [blame] | 201 | std::string MainFileDir; |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 202 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 203 | MainFileDir = MainFile->getDir()->getName(); |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 204 | if (MainFileDir != ".") |
| 205 | MainFileName = MainFileDir + "/" + MainFileName; |
| 206 | } |
Douglas Gregor | f7ad500 | 2010-03-19 14:49:09 +0000 | [diff] [blame] | 207 | |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 208 | // Save filename string. |
| 209 | char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length()); |
| 210 | memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length()); |
| 211 | llvm::StringRef Filename(FilenamePtr, MainFileName.length()); |
| 212 | |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 213 | unsigned LangTag; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 214 | const LangOptions &LO = CGM.getLangOptions(); |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 215 | if (LO.CPlusPlus) { |
| 216 | if (LO.ObjC1) |
| 217 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 218 | else |
| 219 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 220 | } else if (LO.ObjC1) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 221 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 222 | } else if (LO.C99) { |
Devang Patel | 8d9aefc | 2009-03-24 20:35:51 +0000 | [diff] [blame] | 223 | LangTag = llvm::dwarf::DW_LANG_C99; |
Chris Lattner | 515455a | 2009-03-25 03:28:08 +0000 | [diff] [blame] | 224 | } else { |
| 225 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 226 | } |
Devang Patel | 446c619 | 2009-04-17 21:06:59 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | 19f1983 | 2010-08-24 17:41:09 +0000 | [diff] [blame] | 228 | std::string Producer = getClangFullVersion(); |
Chris Lattner | 4c2577a | 2009-05-02 01:00:04 +0000 | [diff] [blame] | 229 | |
| 230 | // Figure out which version of the ObjC runtime we have. |
| 231 | unsigned RuntimeVers = 0; |
| 232 | if (LO.ObjC1) |
| 233 | RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 235 | // Create new compile unit. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 236 | TheCU = DebugFactory.CreateCompileUnit( |
Devang Patel | 5811500 | 2010-07-27 20:49:59 +0000 | [diff] [blame] | 237 | LangTag, Filename, getCurrentDirname(), |
Devang Patel | ac4d13c | 2010-07-27 15:17:16 +0000 | [diff] [blame] | 238 | Producer, true, |
Daniel Dunbar | f2d8b9f | 2009-12-18 02:43:17 +0000 | [diff] [blame] | 239 | LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 242 | /// CreateType - Get the Basic type from the cache or create a new |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 243 | /// one if necessary. |
| 244 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 245 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 246 | unsigned Encoding = 0; |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 247 | const char *BTName = NULL; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 248 | switch (BT->getKind()) { |
| 249 | default: |
| 250 | case BuiltinType::Void: |
| 251 | return llvm::DIType(); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 252 | case BuiltinType::ObjCClass: |
Devang Patel | eaf5ee9 | 2010-07-21 22:41:25 +0000 | [diff] [blame] | 253 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 254 | Unit, "objc_class", Unit, 0, 0, 0, 0, |
Devang Patel | eaf5ee9 | 2010-07-21 22:41:25 +0000 | [diff] [blame] | 255 | llvm::DIType::FlagFwdDecl, |
| 256 | llvm::DIType(), llvm::DIArray()); |
Devang Patel | c8972c6 | 2010-07-28 01:33:15 +0000 | [diff] [blame] | 257 | case BuiltinType::ObjCId: { |
| 258 | // typedef struct objc_class *Class; |
| 259 | // typedef struct objc_object { |
| 260 | // Class isa; |
| 261 | // } *id; |
| 262 | |
| 263 | llvm::DIType OCTy = |
| 264 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, |
| 265 | Unit, "objc_class", Unit, 0, 0, 0, 0, |
| 266 | llvm::DIType::FlagFwdDecl, |
| 267 | llvm::DIType(), llvm::DIArray()); |
| 268 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 269 | |
| 270 | llvm::DIType ISATy = |
| 271 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
| 272 | Unit, "", Unit, |
| 273 | 0, Size, 0, 0, 0, OCTy); |
| 274 | |
| 275 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 276 | |
| 277 | llvm::DIType FieldTy = |
| 278 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 279 | "isa", Unit, |
| 280 | 0,Size, 0, 0, 0, ISATy); |
| 281 | EltTys.push_back(FieldTy); |
| 282 | llvm::DIArray Elements = |
| 283 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
| 284 | |
| 285 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, |
| 286 | Unit, "objc_object", Unit, 0, 0, 0, 0, |
| 287 | 0, |
| 288 | llvm::DIType(), Elements); |
| 289 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 290 | case BuiltinType::UChar: |
| 291 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 292 | case BuiltinType::Char_S: |
| 293 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
| 294 | case BuiltinType::UShort: |
| 295 | case BuiltinType::UInt: |
| 296 | case BuiltinType::ULong: |
| 297 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 298 | case BuiltinType::Short: |
| 299 | case BuiltinType::Int: |
| 300 | case BuiltinType::Long: |
| 301 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 302 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
| 303 | case BuiltinType::Float: |
Devang Patel | 7c173cb | 2009-10-12 22:28:31 +0000 | [diff] [blame] | 304 | case BuiltinType::LongDouble: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 305 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | } |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 307 | |
| 308 | switch (BT->getKind()) { |
| 309 | case BuiltinType::Long: BTName = "long int"; break; |
| 310 | case BuiltinType::LongLong: BTName = "long long int"; break; |
| 311 | case BuiltinType::ULong: BTName = "long unsigned int"; break; |
| 312 | case BuiltinType::ULongLong: BTName = "long long unsigned int"; break; |
| 313 | default: |
| 314 | BTName = BT->getName(CGM.getContext().getLangOptions()); |
| 315 | break; |
| 316 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 317 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 318 | uint64_t Size = CGM.getContext().getTypeSize(BT); |
| 319 | uint64_t Align = CGM.getContext().getTypeAlign(BT); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 320 | uint64_t Offset = 0; |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 321 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 322 | llvm::DIType DbgTy = |
Devang Patel | 05127ca | 2010-07-28 23:23:29 +0000 | [diff] [blame] | 323 | DebugFactory.CreateBasicType(Unit, BTName, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 324 | Unit, 0, Size, Align, |
| 325 | Offset, /*flags*/ 0, Encoding); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 326 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 327 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 328 | |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 329 | llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 330 | llvm::DIFile Unit) { |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 331 | // Bit size, align and offset of the type. |
| 332 | unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; |
| 333 | if (Ty->isComplexIntegerType()) |
| 334 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 336 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 337 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 338 | uint64_t Offset = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 340 | llvm::DIType DbgTy = |
| 341 | DebugFactory.CreateBasicType(Unit, "complex", |
| 342 | Unit, 0, Size, Align, |
| 343 | Offset, /*flags*/ 0, Encoding); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 344 | return DbgTy; |
Chris Lattner | b700377 | 2009-04-23 06:13:01 +0000 | [diff] [blame] | 345 | } |
| 346 | |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 347 | /// CreateCVRType - Get the qualified type from the cache or create |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 348 | /// a new one if necessary. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 349 | llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 350 | QualifierCollector Qc; |
| 351 | const Type *T = Qc.strip(Ty); |
| 352 | |
| 353 | // Ignore these qualifiers for now. |
| 354 | Qc.removeObjCGCAttr(); |
| 355 | Qc.removeAddressSpace(); |
| 356 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 357 | // We will create one Derived type for one qualifier and recurse to handle any |
| 358 | // additional ones. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 359 | unsigned Tag; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 360 | if (Qc.hasConst()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 361 | Tag = llvm::dwarf::DW_TAG_const_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 362 | Qc.removeConst(); |
| 363 | } else if (Qc.hasVolatile()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 364 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 365 | Qc.removeVolatile(); |
| 366 | } else if (Qc.hasRestrict()) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 367 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 368 | Qc.removeRestrict(); |
| 369 | } else { |
| 370 | assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| 371 | return getOrCreateType(QualType(T, 0), Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 374 | llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit); |
| 375 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame] | 376 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 377 | // CVR derived types. |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 378 | llvm::DIType DbgTy = |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 379 | DebugFactory.CreateDerivedType(Tag, Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 380 | 0, 0, 0, 0, 0, FromTy); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 381 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 384 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 385 | llvm::DIFile Unit) { |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 386 | llvm::DIType DbgTy = |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 387 | CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 388 | Ty->getPointeeType(), Unit); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 389 | return DbgTy; |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 392 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 393 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 394 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 395 | Ty->getPointeeType(), Unit); |
| 396 | } |
| 397 | |
| 398 | llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, |
| 399 | const Type *Ty, |
| 400 | QualType PointeeTy, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 401 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 402 | llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 404 | // Bit size, align and offset of the type. |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 405 | |
| 406 | // Size is always the size of a pointer. We can't use getTypeSize here |
| 407 | // because that does not return the correct value for references. |
| 408 | uint64_t Size = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 409 | CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace()); |
| 410 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 412 | return |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 413 | DebugFactory.CreateDerivedType(Tag, Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 414 | 0, Size, Align, 0, 0, EltTy); |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 415 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 418 | llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 419 | llvm::DIFile Unit) { |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 420 | if (BlockLiteralGenericSet) |
| 421 | return BlockLiteralGeneric; |
| 422 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 423 | unsigned Tag = llvm::dwarf::DW_TAG_structure_type; |
| 424 | |
| 425 | llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; |
| 426 | |
| 427 | llvm::DIType FieldTy; |
| 428 | |
| 429 | QualType FType; |
| 430 | uint64_t FieldSize, FieldOffset; |
| 431 | unsigned FieldAlign; |
| 432 | |
| 433 | llvm::DIArray Elements; |
| 434 | llvm::DIType EltTy, DescTy; |
| 435 | |
| 436 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 437 | FType = CGM.getContext().UnsignedLongTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 438 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| 439 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 440 | |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 441 | Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 442 | EltTys.clear(); |
| 443 | |
Mike Stump | 3d363c5 | 2009-10-02 02:30:50 +0000 | [diff] [blame] | 444 | unsigned Flags = llvm::DIType::FlagAppleBlock; |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 445 | unsigned LineNo = getLineNumber(CurLoc); |
Mike Stump | 3d363c5 | 2009-10-02 02:30:50 +0000 | [diff] [blame] | 446 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 447 | EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor", |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 448 | Unit, LineNo, FieldOffset, 0, 0, |
| 449 | Flags, llvm::DIType(), Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 451 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 452 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 453 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 455 | DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 456 | Unit, "", Unit, |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 457 | LineNo, Size, Align, 0, 0, EltTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 458 | |
| 459 | FieldOffset = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 460 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 461 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 462 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 463 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 464 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
Benjamin Kramer | d3651cc | 2010-04-24 20:26:20 +0000 | [diff] [blame] | 465 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 466 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 467 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 468 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 469 | FieldTy = DescTy; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 470 | FieldSize = CGM.getContext().getTypeSize(Ty); |
| 471 | FieldAlign = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 472 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 473 | "__descriptor", Unit, |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 474 | LineNo, FieldSize, FieldAlign, |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 475 | FieldOffset, 0, FieldTy); |
| 476 | EltTys.push_back(FieldTy); |
| 477 | |
| 478 | FieldOffset += FieldSize; |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 479 | Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 480 | |
| 481 | EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic", |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 482 | Unit, LineNo, FieldOffset, 0, 0, |
| 483 | Flags, llvm::DIType(), Elements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 485 | BlockLiteralGenericSet = true; |
| 486 | BlockLiteralGeneric |
| 487 | = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 488 | "", Unit, |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 489 | LineNo, Size, Align, 0, 0, EltTy); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 490 | return BlockLiteralGeneric; |
| 491 | } |
| 492 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 493 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 494 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 495 | // Typedefs are derived from some other type. If we have a typedef of a |
| 496 | // typedef, make sure to emit the whole chain. |
| 497 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 499 | // We don't set size information, but do specify where the typedef was |
| 500 | // declared. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 501 | unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 502 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 503 | llvm::DIDescriptor TyContext |
| 504 | = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()), |
| 505 | Unit); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 506 | llvm::DIType DbgTy = |
Devang Patel | d528905 | 2010-01-29 22:29:31 +0000 | [diff] [blame] | 507 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 508 | TyContext, |
Devang Patel | d528905 | 2010-01-29 22:29:31 +0000 | [diff] [blame] | 509 | Ty->getDecl()->getName(), Unit, |
| 510 | Line, 0, 0, 0, 0, Src); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 511 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 514 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 515 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 516 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 517 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 518 | // Add the result type at least. |
| 519 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 521 | // Set up remainder of arguments if there is a prototype. |
| 522 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 523 | if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 524 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 525 | EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); |
| 526 | } else { |
| 527 | // FIXME: Handle () case in C. llvm-gcc doesn't do it either. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 530 | llvm::DIArray EltTypeArray = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 531 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 533 | llvm::DIType DbgTy = |
| 534 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 535 | Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 536 | 0, 0, 0, 0, 0, |
| 537 | llvm::DIType(), EltTypeArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 538 | return DbgTy; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 541 | /// CollectRecordFields - A helper function to collect debug info for |
| 542 | /// record fields. This is used while creating debug info entry for a Record. |
| 543 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 544 | CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 545 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { |
| 546 | unsigned FieldNo = 0; |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 547 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 548 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 549 | E = RD->field_end(); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 550 | I != E; ++I, ++FieldNo) { |
| 551 | FieldDecl *Field = *I; |
| 552 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 553 | llvm::StringRef FieldName = Field->getName(); |
| 554 | |
Devang Patel | 4835fdd | 2010-02-12 01:31:06 +0000 | [diff] [blame] | 555 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 556 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 557 | continue; |
| 558 | |
| 559 | // Get the location for the field. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 560 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 561 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 562 | QualType FType = Field->getType(); |
| 563 | uint64_t FieldSize = 0; |
| 564 | unsigned FieldAlign = 0; |
| 565 | if (!FType->isIncompleteArrayType()) { |
| 566 | |
| 567 | // Bit size, align and offset of the type. |
| 568 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 569 | Expr *BitWidth = Field->getBitWidth(); |
| 570 | if (BitWidth) |
| 571 | FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 572 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
| 573 | } |
| 574 | |
| 575 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 576 | |
Devang Patel | 71f4ff6 | 2010-04-21 23:12:37 +0000 | [diff] [blame] | 577 | unsigned Flags = 0; |
| 578 | AccessSpecifier Access = I->getAccess(); |
| 579 | if (Access == clang::AS_private) |
| 580 | Flags |= llvm::DIType::FlagPrivate; |
| 581 | else if (Access == clang::AS_protected) |
| 582 | Flags |= llvm::DIType::FlagProtected; |
| 583 | |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 584 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 585 | // struct. FIXME: This is an absolutely insane way to capture this |
| 586 | // information. When we gut debug info, this should be fixed. |
| 587 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 588 | FieldName, FieldDefUnit, |
| 589 | FieldLine, FieldSize, FieldAlign, |
Devang Patel | 71f4ff6 | 2010-04-21 23:12:37 +0000 | [diff] [blame] | 590 | FieldOffset, Flags, FieldTy); |
Devang Patel | 428deb5 | 2010-01-19 00:00:59 +0000 | [diff] [blame] | 591 | EltTys.push_back(FieldTy); |
| 592 | } |
| 593 | } |
| 594 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 595 | /// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This |
| 596 | /// function type is not updated to include implicit "this" pointer. Use this |
| 597 | /// routine to get a method type which includes "this" pointer. |
| 598 | llvm::DIType |
| 599 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 600 | llvm::DIFile Unit) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 601 | llvm::DIType FnTy |
| 602 | = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(), |
| 603 | 0), |
| 604 | Unit); |
Devang Patel | d774d1e | 2010-01-28 21:43:50 +0000 | [diff] [blame] | 605 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 606 | unsigned BFlags=0; |
| 607 | AccessSpecifier Access = Method->getAccess(); |
| 608 | if (Access == clang::AS_private) |
| 609 | BFlags |= llvm::DIType::FlagPrivate; |
| 610 | else if (Access == clang::AS_protected) |
| 611 | BFlags |= llvm::DIType::FlagProtected; |
Devang Patel | d774d1e | 2010-01-28 21:43:50 +0000 | [diff] [blame] | 612 | |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 613 | // Add "this" pointer. |
| 614 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 615 | llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray(); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 616 | assert (Args.getNumElements() && "Invalid number of arguments!"); |
| 617 | |
| 618 | llvm::SmallVector<llvm::DIDescriptor, 16> Elts; |
| 619 | |
| 620 | // First element is always return type. For 'void' functions it is NULL. |
| 621 | Elts.push_back(Args.getElement(0)); |
| 622 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 623 | if (!Method->isStatic()) |
| 624 | { |
| 625 | // "this" pointer is always first argument. |
| 626 | ASTContext &Context = CGM.getContext(); |
| 627 | QualType ThisPtr = |
| 628 | Context.getPointerType(Context.getTagDeclType(Method->getParent())); |
| 629 | llvm::DIType ThisPtrType = |
| 630 | DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit)); |
Devang Patel | 769640e | 2010-07-13 00:24:30 +0000 | [diff] [blame] | 631 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 632 | TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; |
| 633 | Elts.push_back(ThisPtrType); |
| 634 | } |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 635 | |
| 636 | // Copy rest of the arguments. |
| 637 | for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) |
| 638 | Elts.push_back(Args.getElement(i)); |
| 639 | |
| 640 | llvm::DIArray EltTypeArray = |
| 641 | DebugFactory.GetOrCreateArray(Elts.data(), Elts.size()); |
| 642 | |
| 643 | return |
| 644 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 645 | Unit, "", Unit, |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 646 | 0, 0, 0, 0, 0, |
| 647 | llvm::DIType(), EltTypeArray); |
| 648 | } |
| 649 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 650 | /// CreateCXXMemberFunction - A helper function to create a DISubprogram for |
| 651 | /// a single member function GlobalDecl. |
| 652 | llvm::DISubprogram |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 653 | CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 654 | llvm::DIFile Unit, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 655 | llvm::DIType RecordTy) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 656 | bool IsCtorOrDtor = |
| 657 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
| 658 | |
| 659 | llvm::StringRef MethodName = getFunctionName(Method); |
Devang Patel | a6da192 | 2010-01-28 00:28:01 +0000 | [diff] [blame] | 660 | llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 661 | |
| 662 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| 663 | // make sense to give a single ctor/dtor a linkage name. |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 664 | llvm::StringRef MethodLinkageName; |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 665 | if (!IsCtorOrDtor) |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 666 | MethodLinkageName = CGM.getMangledName(Method); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 667 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 668 | // Get the location for the method. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 669 | llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| 670 | unsigned MethodLine = getLineNumber(Method->getLocation()); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 671 | |
| 672 | // Collect virtual method info. |
| 673 | llvm::DIType ContainingType; |
| 674 | unsigned Virtuality = 0; |
| 675 | unsigned VIndex = 0; |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 676 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 677 | if (Method->isVirtual()) { |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 678 | if (Method->isPure()) |
| 679 | Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| 680 | else |
| 681 | Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
| 682 | |
| 683 | // It doesn't make sense to give a virtual destructor a vtable index, |
| 684 | // since a single destructor has two entries in the vtable. |
| 685 | if (!isa<CXXDestructorDecl>(Method)) |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 686 | VIndex = CGM.getVTables().getMethodVTableIndex(Method); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 687 | ContainingType = RecordTy; |
| 688 | } |
| 689 | |
| 690 | llvm::DISubprogram SP = |
| 691 | DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName, |
| 692 | MethodLinkageName, |
| 693 | MethodDefUnit, MethodLine, |
| 694 | MethodTy, /*isLocalToUnit=*/false, |
Devang Patel | a5c5bab | 2010-07-12 22:54:41 +0000 | [diff] [blame] | 695 | /* isDefintion=*/ false, |
Devang Patel | 4089491 | 2010-07-15 22:57:00 +0000 | [diff] [blame] | 696 | Virtuality, VIndex, ContainingType, |
Devang Patel | 15a3d7d | 2010-07-15 23:09:46 +0000 | [diff] [blame] | 697 | Method->isImplicit(), |
| 698 | CGM.getLangOptions().Optimize); |
Anders Carlsson | 4433f1c | 2010-01-26 05:19:50 +0000 | [diff] [blame] | 699 | |
| 700 | // Don't cache ctors or dtors since we have to emit multiple functions for |
| 701 | // a single ctor or dtor. |
| 702 | if (!IsCtorOrDtor && Method->isThisDeclarationADefinition()) |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 703 | SPCache[Method] = llvm::WeakVH(SP); |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 704 | |
| 705 | return SP; |
| 706 | } |
| 707 | |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 708 | /// CollectCXXMemberFunctions - A helper function to collect debug info for |
| 709 | /// C++ member functions.This is used while creating debug info entry for |
| 710 | /// a Record. |
| 711 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 712 | CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 713 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 714 | llvm::DIType RecordTy) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 715 | for(CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 716 | E = RD->method_end(); I != E; ++I) { |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 717 | const CXXMethodDecl *Method = *I; |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 718 | |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 719 | if (Method->isImplicit() && !Method->isUsed()) |
Anders Carlsson | bea9b23 | 2010-01-26 04:40:11 +0000 | [diff] [blame] | 720 | continue; |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 721 | |
Anders Carlsson | d6f9a0d | 2010-01-26 04:49:33 +0000 | [diff] [blame] | 722 | EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 726 | /// CollectCXXFriends - A helper function to collect debug info for |
| 727 | /// C++ base classes. This is used while creating debug info entry for |
| 728 | /// a Record. |
| 729 | void CGDebugInfo:: |
| 730 | CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit, |
| 731 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, |
| 732 | llvm::DIType RecordTy) { |
| 733 | |
| 734 | for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(), |
| 735 | BE = RD->friend_end(); BI != BE; ++BI) { |
| 736 | |
| 737 | TypeSourceInfo *TInfo = (*BI)->getFriendType(); |
| 738 | if(TInfo) |
| 739 | { |
| 740 | llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit); |
| 741 | |
| 742 | llvm::DIType DTy = |
| 743 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend, |
| 744 | RecordTy, llvm::StringRef(), |
| 745 | Unit, 0, 0, 0, |
| 746 | 0, 0, Ty); |
| 747 | |
| 748 | EltTys.push_back(DTy); |
| 749 | } |
| 750 | |
| 751 | } |
| 752 | } |
| 753 | |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 754 | /// CollectCXXBases - A helper function to collect debug info for |
| 755 | /// C++ base classes. This is used while creating debug info entry for |
| 756 | /// a Record. |
| 757 | void CGDebugInfo:: |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 758 | CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 759 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 760 | llvm::DIType RecordTy) { |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 761 | |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 762 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 763 | for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), |
| 764 | BE = RD->bases_end(); BI != BE; ++BI) { |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 765 | unsigned BFlags = 0; |
| 766 | uint64_t BaseOffset; |
| 767 | |
| 768 | const CXXRecordDecl *Base = |
| 769 | cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); |
| 770 | |
| 771 | if (BI->isVirtual()) { |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 772 | // virtual base offset offset is -ve. The code generator emits dwarf |
Devang Patel | d5322da | 2010-02-09 19:09:28 +0000 | [diff] [blame] | 773 | // expression where it expects +ve number. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 774 | BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base); |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 775 | BFlags = llvm::DIType::FlagVirtual; |
| 776 | } else |
| 777 | BaseOffset = RL.getBaseClassOffset(Base); |
| 778 | |
| 779 | AccessSpecifier Access = BI->getAccessSpecifier(); |
| 780 | if (Access == clang::AS_private) |
| 781 | BFlags |= llvm::DIType::FlagPrivate; |
| 782 | else if (Access == clang::AS_protected) |
| 783 | BFlags |= llvm::DIType::FlagProtected; |
| 784 | |
| 785 | llvm::DIType DTy = |
| 786 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, |
| 787 | RecordTy, llvm::StringRef(), |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 788 | Unit, 0, 0, 0, |
Devang Patel | ca7daed | 2010-01-28 21:54:15 +0000 | [diff] [blame] | 789 | BaseOffset, BFlags, |
| 790 | getOrCreateType(BI->getType(), |
| 791 | Unit)); |
| 792 | EltTys.push_back(DTy); |
| 793 | } |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 796 | /// getOrCreateVTablePtrType - Return debug info descriptor for vtable. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 797 | llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { |
Devang Patel | 0804e6e | 2010-03-08 20:53:17 +0000 | [diff] [blame] | 798 | if (VTablePtrType.isValid()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 799 | return VTablePtrType; |
| 800 | |
| 801 | ASTContext &Context = CGM.getContext(); |
| 802 | |
| 803 | /* Function type */ |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 804 | llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit); |
| 805 | llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 806 | llvm::DIType SubTy = |
| 807 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 808 | Unit, "", Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 809 | 0, 0, 0, 0, 0, llvm::DIType(), SElements); |
| 810 | |
| 811 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
| 812 | llvm::DIType vtbl_ptr_type |
| 813 | = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 814 | Unit, "__vtbl_ptr_type", Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 815 | 0, Size, 0, 0, 0, SubTy); |
| 816 | |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 817 | VTablePtrType = |
| 818 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, |
| 819 | Unit, "", Unit, |
| 820 | 0, Size, 0, 0, 0, vtbl_ptr_type); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 821 | return VTablePtrType; |
| 822 | } |
| 823 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 824 | /// getVTableName - Get vtable name for the given Class. |
| 825 | llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 826 | // Otherwise construct gdb compatible name name. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 827 | std::string Name = "_vptr$" + RD->getNameAsString(); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 828 | |
| 829 | // Copy this name on the side and use its reference. |
Devang Patel | 89f05f8 | 2010-01-28 18:21:00 +0000 | [diff] [blame] | 830 | char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 831 | memcpy(StrPtr, Name.data(), Name.length()); |
| 832 | return llvm::StringRef(StrPtr, Name.length()); |
| 833 | } |
| 834 | |
| 835 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 836 | /// CollectVTableInfo - If the C++ class has vtable info then insert appropriate |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 837 | /// debug info entry in EltTys vector. |
| 838 | void CGDebugInfo:: |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 839 | CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 840 | llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 841 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 842 | |
| 843 | // If there is a primary base then it will hold vtable info. |
| 844 | if (RL.getPrimaryBase()) |
| 845 | return; |
| 846 | |
| 847 | // If this class is not dynamic then there is not any vtable info to collect. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 848 | if (!RD->isDynamicClass()) |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 849 | return; |
| 850 | |
| 851 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 852 | llvm::DIType VPTR |
| 853 | = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 854 | getVTableName(RD), Unit, |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 855 | 0, Size, 0, 0, 0, |
| 856 | getOrCreateVTablePtrType(Unit)); |
| 857 | EltTys.push_back(VPTR); |
| 858 | } |
| 859 | |
Devang Patel | 65e99f2 | 2009-02-25 01:36:11 +0000 | [diff] [blame] | 860 | /// CreateType - get structure or union type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 861 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 862 | llvm::DIFile Unit) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 863 | RecordDecl *RD = Ty->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 865 | unsigned Tag; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 866 | if (RD->isStruct()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 867 | Tag = llvm::dwarf::DW_TAG_structure_type; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 868 | else if (RD->isUnion()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 869 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 870 | else { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 871 | assert(RD->isClass() && "Unknown RecordType!"); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 872 | Tag = llvm::dwarf::DW_TAG_class_type; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 875 | // Get overall information about the record type for the debug info. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 876 | llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); |
| 877 | unsigned Line = getLineNumber(RD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 879 | // Records and classes and unions can all be recursive. To handle them, we |
| 880 | // first generate a debug descriptor for the struct as a forward declaration. |
| 881 | // Then (if it is a definition) we go through and get debug info for all of |
| 882 | // its members. Finally, we create a descriptor for the complete type (which |
| 883 | // may refer to the forward decl if the struct is recursive) and replace all |
| 884 | // uses of the forward declaration with the final definition. |
Devang Patel | 0b89799 | 2010-07-08 19:56:29 +0000 | [diff] [blame] | 885 | llvm::DIDescriptor FDContext = |
| 886 | getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); |
| 887 | |
| 888 | // If this is just a forward declaration, construct an appropriately |
| 889 | // marked node and just return it. |
| 890 | if (!RD->getDefinition()) { |
| 891 | llvm::DICompositeType FwdDecl = |
| 892 | DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(), |
| 893 | DefUnit, Line, 0, 0, 0, |
| 894 | llvm::DIType::FlagFwdDecl, |
| 895 | llvm::DIType(), llvm::DIArray()); |
| 896 | |
| 897 | return FwdDecl; |
| 898 | } |
Devang Patel | d0f251b | 2010-01-20 23:56:40 +0000 | [diff] [blame] | 899 | |
Dan Gohman | 8696837 | 2010-08-20 22:39:57 +0000 | [diff] [blame] | 900 | llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 902 | llvm::MDNode *MN = FwdDecl; |
| 903 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 904 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 905 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 906 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 907 | // Push the struct on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 908 | RegionStack.push_back(FwdDeclNode); |
| 909 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 910 | |
| 911 | // Convert all the elements. |
| 912 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 913 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 914 | const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 915 | if (CXXDecl) { |
| 916 | CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 917 | CollectVTableInfo(CXXDecl, Unit, EltTys); |
Devang Patel | 3064afe | 2010-01-28 21:41:35 +0000 | [diff] [blame] | 918 | } |
Devang Patel | dabc3e9 | 2010-08-12 00:02:44 +0000 | [diff] [blame] | 919 | |
| 920 | // Collect static variables with initializers. |
| 921 | for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end(); |
| 922 | I != E; ++I) |
| 923 | if (const VarDecl *V = dyn_cast<VarDecl>(*I)) { |
| 924 | if (const Expr *Init = V->getInit()) { |
| 925 | Expr::EvalResult Result; |
| 926 | if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) { |
| 927 | llvm::ConstantInt *CI |
| 928 | = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt()); |
| 929 | |
| 930 | // Create the descriptor for static variable. |
| 931 | llvm::DIFile VUnit = getOrCreateFile(V->getLocation()); |
| 932 | llvm::StringRef VName = V->getName(); |
| 933 | llvm::DIType VTy = getOrCreateType(V->getType(), VUnit); |
| 934 | // Do not use DIGlobalVariable for enums. |
| 935 | if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) { |
| 936 | DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit, |
| 937 | getLineNumber(V->getLocation()), |
| 938 | VTy, true, true, CI); |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 944 | CollectRecordFields(RD, Unit, EltTys); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 945 | llvm::MDNode *ContainingType = NULL; |
Devang Patel | 4ce3f20 | 2010-01-28 18:11:52 +0000 | [diff] [blame] | 946 | if (CXXDecl) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 947 | CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 948 | CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 949 | |
| 950 | // A class's primary base or the class itself contains the vtable. |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 951 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 952 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) |
| 953 | ContainingType = |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 954 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit); |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 955 | else if (CXXDecl->isDynamicClass()) |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 956 | ContainingType = FwdDecl; |
Devang Patel | a245c5b | 2010-01-25 23:32:18 +0000 | [diff] [blame] | 957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 959 | llvm::DIArray Elements = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 960 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 961 | |
| 962 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 963 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 964 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 966 | RegionStack.pop_back(); |
| 967 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 968 | RegionMap.find(Ty->getDecl()); |
| 969 | if (RI != RegionMap.end()) |
| 970 | RegionMap.erase(RI); |
| 971 | |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 972 | llvm::DIDescriptor RDContext = |
| 973 | getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 974 | |
| 975 | llvm::StringRef RDName = RD->getName(); |
| 976 | // If this is a class, include the template arguments also. |
| 977 | if (Tag == llvm::dwarf::DW_TAG_class_type) |
| 978 | RDName = getClassName(RD); |
| 979 | |
Devang Patel | 0ce73f6 | 2009-07-22 18:57:00 +0000 | [diff] [blame] | 980 | llvm::DICompositeType RealDecl = |
Devang Patel | 411894b | 2010-02-01 22:40:08 +0000 | [diff] [blame] | 981 | DebugFactory.CreateCompositeType(Tag, RDContext, |
Devang Patel | 700a1cb | 2010-07-20 20:24:18 +0000 | [diff] [blame] | 982 | RDName, |
Devang Patel | ab71ff5 | 2009-11-12 00:51:46 +0000 | [diff] [blame] | 983 | DefUnit, Line, Size, Align, 0, 0, |
Devang Patel | 0ac8f31 | 2010-01-28 00:54:21 +0000 | [diff] [blame] | 984 | llvm::DIType(), Elements, |
| 985 | 0, ContainingType); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 986 | |
| 987 | // Now that we have a real decl for the struct, replace anything using the |
| 988 | // old decl with the new one. This will recursively update the debug info. |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 989 | llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 990 | RegionMap[RD] = llvm::WeakVH(RealDecl); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 991 | return RealDecl; |
| 992 | } |
| 993 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 994 | /// CreateType - get objective-c object type. |
| 995 | llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty, |
| 996 | llvm::DIFile Unit) { |
| 997 | // Ignore protocols. |
| 998 | return getOrCreateType(Ty->getBaseType(), Unit); |
| 999 | } |
| 1000 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1001 | /// CreateType - get objective-c interface type. |
| 1002 | llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1003 | llvm::DIFile Unit) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1004 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1005 | unsigned Tag = llvm::dwarf::DW_TAG_structure_type; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1006 | |
| 1007 | // Get overall information about the record type for the debug info. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1008 | llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1009 | unsigned Line = getLineNumber(ID->getLocation()); |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1010 | unsigned RuntimeLang = TheCU.getLanguage(); |
Chris Lattner | ac7c814 | 2009-05-02 01:13:16 +0000 | [diff] [blame] | 1011 | |
Dan Gohman | 45f7c78 | 2010-08-23 21:15:56 +0000 | [diff] [blame] | 1012 | // If this is just a forward declaration, return a special forward-declaration |
| 1013 | // debug type. |
| 1014 | if (ID->isForwardDecl()) { |
| 1015 | llvm::DICompositeType FwdDecl = |
| 1016 | DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), |
| 1017 | DefUnit, Line, 0, 0, 0, 0, |
| 1018 | llvm::DIType(), llvm::DIArray(), |
| 1019 | RuntimeLang); |
| 1020 | return FwdDecl; |
| 1021 | } |
| 1022 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1023 | // To handle recursive interface, we |
| 1024 | // first generate a debug descriptor for the struct as a forward declaration. |
| 1025 | // Then (if it is a definition) we go through and get debug info for all of |
| 1026 | // its members. Finally, we create a descriptor for the complete type (which |
| 1027 | // may refer to the forward decl if the struct is recursive) and replace all |
| 1028 | // uses of the forward declaration with the final definition. |
Dan Gohman | 8696837 | 2010-08-20 22:39:57 +0000 | [diff] [blame] | 1029 | llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1031 | llvm::MDNode *MN = FwdDecl; |
| 1032 | llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1033 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 1034 | // it. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1035 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 1036 | // Push the struct on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1037 | RegionStack.push_back(FwdDeclNode); |
| 1038 | RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1039 | |
| 1040 | // Convert all the elements. |
| 1041 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 1042 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1043 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 1044 | if (SClass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | llvm::DIType SClassTy = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1046 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | llvm::DIType InhTag = |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 1048 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1049 | Unit, "", Unit, 0, 0, 0, |
Devang Patel | fbe899f | 2009-03-10 21:30:26 +0000 | [diff] [blame] | 1050 | 0 /* offset */, 0, SClassTy); |
| 1051 | EltTys.push_back(InhTag); |
| 1052 | } |
| 1053 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1054 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1055 | |
| 1056 | unsigned FieldNo = 0; |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1057 | for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(), |
| 1058 | E = ID->ivar_end(); I != E; ++I, ++FieldNo) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1059 | ObjCIvarDecl *Field = *I; |
| 1060 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
| 1061 | |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 1062 | llvm::StringRef FieldName = Field->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1063 | |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 1064 | // Ignore unnamed fields. |
Devang Patel | 7362162 | 2009-11-25 17:37:31 +0000 | [diff] [blame] | 1065 | if (FieldName.empty()) |
Devang Patel | de13502 | 2009-04-27 22:40:36 +0000 | [diff] [blame] | 1066 | continue; |
| 1067 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1068 | // Get the location for the field. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1069 | llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| 1070 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1071 | QualType FType = Field->getType(); |
| 1072 | uint64_t FieldSize = 0; |
| 1073 | unsigned FieldAlign = 0; |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1074 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1075 | if (!FType->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1077 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1078 | FieldSize = CGM.getContext().getTypeSize(FType); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1079 | Expr *BitWidth = Field->getBitWidth(); |
| 1080 | if (BitWidth) |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1081 | FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 1082 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1083 | FieldAlign = CGM.getContext().getTypeAlign(FType); |
Devang Patel | 99c20eb | 2009-03-20 18:24:39 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 1087 | |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1088 | unsigned Flags = 0; |
| 1089 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
| 1090 | Flags = llvm::DIType::FlagProtected; |
| 1091 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
| 1092 | Flags = llvm::DIType::FlagPrivate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1094 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 1095 | // struct. FIXME: This is an absolutely insane way to capture this |
| 1096 | // information. When we gut debug info, this should be fixed. |
| 1097 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 1098 | FieldName, FieldDefUnit, |
| 1099 | FieldLine, FieldSize, FieldAlign, |
Devang Patel | c20482b | 2009-03-19 00:23:53 +0000 | [diff] [blame] | 1100 | FieldOffset, Flags, FieldTy); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1101 | EltTys.push_back(FieldTy); |
| 1102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1104 | llvm::DIArray Elements = |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1105 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1106 | |
Devang Patel | e4c1ea0 | 2010-03-11 20:01:48 +0000 | [diff] [blame] | 1107 | RegionStack.pop_back(); |
| 1108 | llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = |
| 1109 | RegionMap.find(Ty->getDecl()); |
| 1110 | if (RI != RegionMap.end()) |
| 1111 | RegionMap.erase(RI); |
| 1112 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1113 | // Bit size, align and offset of the type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1114 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1115 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Devang Patel | 6c1fddf | 2009-07-27 18:42:03 +0000 | [diff] [blame] | 1117 | llvm::DICompositeType RealDecl = |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1118 | DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit, |
Devang Patel | ab71ff5 | 2009-11-12 00:51:46 +0000 | [diff] [blame] | 1119 | Line, Size, Align, 0, 0, llvm::DIType(), |
| 1120 | Elements, RuntimeLang); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Now that we have a real decl for the struct, replace anything using the |
| 1123 | // old decl with the new one. This will recursively update the debug info. |
Dan Gohman | 4cac5b4 | 2010-08-20 22:02:57 +0000 | [diff] [blame] | 1124 | llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1125 | RegionMap[ID] = llvm::WeakVH(RealDecl); |
Devang Patel | fe09eab | 2009-07-13 17:03:14 +0000 | [diff] [blame] | 1126 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1127 | return RealDecl; |
| 1128 | } |
| 1129 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1130 | llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1131 | llvm::DIFile Unit) { |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1132 | return CreateEnumType(Ty->getDecl(), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1137 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1138 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
| 1139 | return CreateType(RT, Unit); |
| 1140 | else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) |
| 1141 | return CreateType(ET, Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1143 | return llvm::DIType(); |
| 1144 | } |
| 1145 | |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1146 | llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1147 | llvm::DIFile Unit) { |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1148 | llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
| 1149 | uint64_t NumElems = Ty->getNumElements(); |
| 1150 | if (NumElems > 0) |
| 1151 | --NumElems; |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1152 | |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 1153 | llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems); |
| 1154 | llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1155 | |
| 1156 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 1157 | uint64_t Align = CGM.getContext().getTypeAlign(Ty); |
| 1158 | |
| 1159 | return |
| 1160 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1161 | Unit, "", Unit, |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1162 | 0, Size, Align, 0, 0, |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1163 | ElementTy, SubscriptArray); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1166 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1167 | llvm::DIFile Unit) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1168 | uint64_t Size; |
| 1169 | uint64_t Align; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
| 1171 | |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1172 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1173 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1174 | Size = 0; |
| 1175 | Align = |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1176 | CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); |
Nuno Lopes | 010d514 | 2009-01-28 00:35:17 +0000 | [diff] [blame] | 1177 | } else if (Ty->isIncompleteArrayType()) { |
| 1178 | Size = 0; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1179 | Align = CGM.getContext().getTypeAlign(Ty->getElementType()); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1180 | } else { |
| 1181 | // Size and align of the whole array, not the element type. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1182 | Size = CGM.getContext().getTypeSize(Ty); |
| 1183 | Align = CGM.getContext().getTypeAlign(Ty); |
Anders Carlsson | 835c909 | 2009-01-05 01:23:29 +0000 | [diff] [blame] | 1184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1186 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 1187 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 1188 | // obvious/recursive way? |
| 1189 | llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; |
| 1190 | QualType EltTy(Ty, 0); |
| 1191 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 1192 | uint64_t Upper = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) |
Devang Patel | 5a6bfe3 | 2009-08-14 20:57:45 +0000 | [diff] [blame] | 1194 | if (CAT->getSize().getZExtValue()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | Upper = CAT->getSize().getZExtValue() - 1; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1196 | // FIXME: Verify this is right for VLAs. |
| 1197 | Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); |
| 1198 | EltTy = Ty->getElementType(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 1199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1201 | llvm::DIArray SubscriptArray = |
Daniel Dunbar | ca308df | 2009-05-26 19:40:20 +0000 | [diff] [blame] | 1202 | DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1203 | |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1204 | llvm::DIType DbgTy = |
| 1205 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1206 | Unit, "", Unit, |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1207 | 0, Size, Align, 0, 0, |
| 1208 | getOrCreateType(EltTy, Unit), |
| 1209 | SubscriptArray); |
Devang Patel | ca80a5f | 2009-10-20 19:55:01 +0000 | [diff] [blame] | 1210 | return DbgTy; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1213 | llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1214 | llvm::DIFile Unit) { |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1215 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, |
| 1216 | Ty, Ty->getPointeeType(), Unit); |
| 1217 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1218 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1219 | llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1220 | llvm::DIFile U) { |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1221 | QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); |
| 1222 | llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); |
| 1223 | |
| 1224 | if (!Ty->getPointeeType()->isFunctionType()) { |
| 1225 | // We have a data member pointer type. |
| 1226 | return PointerDiffDITy; |
| 1227 | } |
| 1228 | |
| 1229 | // We have a member function pointer type. Treat it as a struct with two |
| 1230 | // ptrdiff_t members. |
| 1231 | std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); |
| 1232 | |
| 1233 | uint64_t FieldOffset = 0; |
| 1234 | llvm::DIDescriptor ElementTypes[2]; |
| 1235 | |
| 1236 | // FIXME: This should probably be a function type instead. |
| 1237 | ElementTypes[0] = |
| 1238 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1239 | "ptr", U, 0, |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1240 | Info.first, Info.second, FieldOffset, 0, |
| 1241 | PointerDiffDITy); |
| 1242 | FieldOffset += Info.first; |
| 1243 | |
| 1244 | ElementTypes[1] = |
| 1245 | DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1246 | "ptr", U, 0, |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1247 | Info.first, Info.second, FieldOffset, 0, |
| 1248 | PointerDiffDITy); |
| 1249 | |
| 1250 | llvm::DIArray Elements = |
| 1251 | DebugFactory.GetOrCreateArray(&ElementTypes[0], |
| 1252 | llvm::array_lengthof(ElementTypes)); |
| 1253 | |
| 1254 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, |
| 1255 | U, llvm::StringRef("test"), |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1256 | U, 0, FieldOffset, |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1257 | 0, 0, 0, llvm::DIType(), Elements); |
| 1258 | } |
| 1259 | |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1260 | /// CreateEnumType - get enumeration type. |
| 1261 | llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){ |
| 1262 | llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; |
| 1263 | |
| 1264 | // Create DIEnumerator elements for each enumerator. |
| 1265 | for (EnumDecl::enumerator_iterator |
| 1266 | Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); |
| 1267 | Enum != EnumEnd; ++Enum) { |
| 1268 | Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(), |
| 1269 | Enum->getInitVal().getZExtValue())); |
| 1270 | } |
| 1271 | |
| 1272 | // Return a CompositeType for the enum itself. |
| 1273 | llvm::DIArray EltArray = |
| 1274 | DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size()); |
| 1275 | |
| 1276 | llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); |
| 1277 | unsigned Line = getLineNumber(ED->getLocation()); |
| 1278 | uint64_t Size = 0; |
Devang Patel | ffc52e7 | 2010-08-24 18:14:06 +0000 | [diff] [blame] | 1279 | uint64_t Align = 0; |
| 1280 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 1281 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
| 1282 | Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl()); |
| 1283 | } |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1284 | llvm::DIType DbgTy = |
| 1285 | DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, |
| 1286 | Unit, ED->getName(), DefUnit, Line, |
Devang Patel | ffc52e7 | 2010-08-24 18:14:06 +0000 | [diff] [blame] | 1287 | Size, Align, 0, 0, |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1288 | llvm::DIType(), EltArray); |
| 1289 | return DbgTy; |
| 1290 | } |
| 1291 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1292 | static QualType UnwrapTypeForDebugInfo(QualType T) { |
| 1293 | do { |
| 1294 | QualType LastT = T; |
| 1295 | switch (T->getTypeClass()) { |
| 1296 | default: |
| 1297 | return T; |
| 1298 | case Type::TemplateSpecialization: |
| 1299 | T = cast<TemplateSpecializationType>(T)->desugar(); |
| 1300 | break; |
| 1301 | case Type::TypeOfExpr: { |
| 1302 | TypeOfExprType *Ty = cast<TypeOfExprType>(T); |
| 1303 | T = Ty->getUnderlyingExpr()->getType(); |
| 1304 | break; |
| 1305 | } |
| 1306 | case Type::TypeOf: |
| 1307 | T = cast<TypeOfType>(T)->getUnderlyingType(); |
| 1308 | break; |
| 1309 | case Type::Decltype: |
| 1310 | T = cast<DecltypeType>(T)->getUnderlyingType(); |
| 1311 | break; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1312 | case Type::Elaborated: |
| 1313 | T = cast<ElaboratedType>(T)->getNamedType(); |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1314 | break; |
| 1315 | case Type::SubstTemplateTypeParm: |
| 1316 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
| 1317 | break; |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | assert(T != LastT && "Type unwrapping failed to unwrap!"); |
| 1321 | if (T == LastT) |
| 1322 | return T; |
| 1323 | } while (true); |
| 1324 | |
| 1325 | return T; |
Anders Carlsson | 5b6117a | 2009-11-14 21:08:12 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1328 | /// getOrCreateType - Get the type from the cache or create a new |
| 1329 | /// one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1330 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1331 | llvm::DIFile Unit) { |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1332 | if (Ty.isNull()) |
| 1333 | return llvm::DIType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1335 | // Unwrap the type as needed for debug information. |
| 1336 | Ty = UnwrapTypeForDebugInfo(Ty); |
Anders Carlsson | 5b6117a | 2009-11-14 21:08:12 +0000 | [diff] [blame] | 1337 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1338 | // Check for existing entry. |
Ted Kremenek | 590838b | 2010-03-29 18:29:57 +0000 | [diff] [blame] | 1339 | llvm::DenseMap<void *, llvm::WeakVH>::iterator it = |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1340 | TypeCache.find(Ty.getAsOpaquePtr()); |
Daniel Dunbar | 65f13c3 | 2009-09-19 20:17:48 +0000 | [diff] [blame] | 1341 | if (it != TypeCache.end()) { |
| 1342 | // Verify that the debug info still exists. |
| 1343 | if (&*it->second) |
| 1344 | return llvm::DIType(cast<llvm::MDNode>(it->second)); |
| 1345 | } |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1346 | |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1347 | // Otherwise create the type. |
| 1348 | llvm::DIType Res = CreateTypeNode(Ty, Unit); |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1349 | |
| 1350 | // And update the type cache. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1351 | TypeCache[Ty.getAsOpaquePtr()] = Res; |
Daniel Dunbar | 23e81ba | 2009-09-19 19:27:24 +0000 | [diff] [blame] | 1352 | return Res; |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Anders Carlsson | 0dd57c6 | 2009-11-14 20:52:05 +0000 | [diff] [blame] | 1355 | /// CreateTypeNode - Create a new debug type node. |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1356 | llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1357 | llvm::DIFile Unit) { |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1358 | // Handle qualifiers, which recursively handles what they refer to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1359 | if (Ty.hasLocalQualifiers()) |
John McCall | a180529 | 2009-09-25 01:40:47 +0000 | [diff] [blame] | 1360 | return CreateQualifiedType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1361 | |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1362 | const char *Diag = 0; |
| 1363 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1364 | // Work out details of type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1365 | switch (Ty->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1366 | #define TYPE(Class, Base) |
| 1367 | #define ABSTRACT_TYPE(Class, Base) |
| 1368 | #define NON_CANONICAL_TYPE(Class, Base) |
| 1369 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1370 | #include "clang/AST/TypeNodes.def" |
| 1371 | assert(false && "Dependent types cannot show up in debug information"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1372 | |
Anders Carlsson | bfe6995 | 2009-11-06 18:24:04 +0000 | [diff] [blame] | 1373 | // FIXME: Handle these. |
| 1374 | case Type::ExtVector: |
Anders Carlsson | bfe6995 | 2009-11-06 18:24:04 +0000 | [diff] [blame] | 1375 | return llvm::DIType(); |
Devang Patel | 70c23cd | 2010-02-23 22:59:39 +0000 | [diff] [blame] | 1376 | |
| 1377 | case Type::Vector: |
| 1378 | return CreateType(cast<VectorType>(Ty), Unit); |
Daniel Dunbar | 9df4bb3 | 2009-07-14 01:20:56 +0000 | [diff] [blame] | 1379 | case Type::ObjCObjectPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1380 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1381 | case Type::ObjCObject: |
| 1382 | return CreateType(cast<ObjCObjectType>(Ty), Unit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | case Type::ObjCInterface: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1384 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
| 1385 | case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit); |
| 1386 | case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit); |
| 1387 | case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); |
Mike Stump | 9bc093c | 2009-05-14 02:03:51 +0000 | [diff] [blame] | 1388 | case Type::BlockPointer: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1389 | return CreateType(cast<BlockPointerType>(Ty), Unit); |
| 1390 | case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1391 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1392 | case Type::Enum: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1393 | return CreateType(cast<TagType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1394 | case Type::FunctionProto: |
| 1395 | case Type::FunctionNoProto: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1396 | return CreateType(cast<FunctionType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1397 | case Type::ConstantArray: |
| 1398 | case Type::VariableArray: |
| 1399 | case Type::IncompleteArray: |
Daniel Dunbar | 03faac3 | 2009-09-19 19:27:14 +0000 | [diff] [blame] | 1400 | return CreateType(cast<ArrayType>(Ty), Unit); |
Anders Carlsson | a031b35 | 2009-11-06 19:19:55 +0000 | [diff] [blame] | 1401 | |
| 1402 | case Type::LValueReference: |
| 1403 | return CreateType(cast<LValueReferenceType>(Ty), Unit); |
| 1404 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1405 | case Type::MemberPointer: |
| 1406 | return CreateType(cast<MemberPointerType>(Ty), Unit); |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1407 | |
| 1408 | case Type::TemplateSpecialization: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1409 | case Type::Elaborated: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1410 | case Type::SubstTemplateTypeParm: |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1411 | case Type::TypeOfExpr: |
| 1412 | case Type::TypeOf: |
Douglas Gregor | 840943d | 2009-12-21 20:18:30 +0000 | [diff] [blame] | 1413 | case Type::Decltype: |
| 1414 | llvm_unreachable("type should have been unwrapped!"); |
| 1415 | return llvm::DIType(); |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1416 | |
| 1417 | case Type::RValueReference: |
| 1418 | // FIXME: Implement! |
| 1419 | Diag = "rvalue references"; |
| 1420 | break; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1421 | } |
Douglas Gregor | 2101a82 | 2009-12-21 19:57:21 +0000 | [diff] [blame] | 1422 | |
| 1423 | assert(Diag && "Fall through without a diagnostic?"); |
| 1424 | unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error, |
| 1425 | "debug information for %0 is not yet supported"); |
| 1426 | CGM.getDiags().Report(FullSourceLoc(), DiagID) |
| 1427 | << Diag; |
| 1428 | return llvm::DIType(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1431 | /// CreateMemberType - Create new member and increase Offset by FType's size. |
| 1432 | llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, |
| 1433 | llvm::StringRef Name, |
| 1434 | uint64_t *Offset) { |
| 1435 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
| 1436 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); |
| 1437 | unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); |
| 1438 | llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, |
| 1439 | Unit, Name, Unit, 0, |
| 1440 | FieldSize, FieldAlign, |
| 1441 | *Offset, 0, FieldTy); |
| 1442 | *Offset += FieldSize; |
| 1443 | return Ty; |
| 1444 | } |
| 1445 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1446 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 1447 | /// "llvm.dbg.func.start.". |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1448 | void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1449 | llvm::Function *Fn, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1450 | CGBuilderTy &Builder) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1452 | llvm::StringRef Name; |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 1453 | llvm::StringRef LinkageName; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1454 | |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1455 | FnBeginRegionCount.push_back(RegionStack.size()); |
| 1456 | |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1457 | const Decl *D = GD.getDecl(); |
| 1458 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1459 | // If there is a DISubprogram for this function available then use it. |
| 1460 | llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator |
| 1461 | FI = SPCache.find(FD); |
| 1462 | if (FI != SPCache.end()) { |
Devang Patel | 0804e6e | 2010-03-08 20:53:17 +0000 | [diff] [blame] | 1463 | llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1464 | if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { |
| 1465 | llvm::MDNode *SPN = SP; |
| 1466 | RegionStack.push_back(SPN); |
| 1467 | RegionMap[D] = llvm::WeakVH(SP); |
Devang Patel | 4125fd2 | 2010-01-19 01:54:44 +0000 | [diff] [blame] | 1468 | return; |
| 1469 | } |
| 1470 | } |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1471 | Name = getFunctionName(FD); |
| 1472 | // Use mangled name as linkage name for c/c++ functions. |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 1473 | LinkageName = CGM.getMangledName(GD); |
David Chisnall | 70b9b44 | 2010-09-02 17:16:32 +0000 | [diff] [blame^] | 1474 | } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
| 1475 | llvm::SmallString<256> MethodName; |
| 1476 | llvm::raw_svector_ostream OS(MethodName); |
| 1477 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 1478 | const DeclContext *DC = OMD->getDeclContext(); |
| 1479 | if (const ObjCImplementationDecl *OID = dyn_cast<const ObjCImplementationDecl>(DC)) { |
| 1480 | OS << OID->getName(); |
| 1481 | } else if (const ObjCCategoryImplDecl *OCD = dyn_cast<const ObjCCategoryImplDecl>(DC)){ |
| 1482 | OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' << |
| 1483 | OCD->getIdentifier()->getNameStart() << ')'; |
| 1484 | } |
| 1485 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 1486 | Name = MethodName; |
| 1487 | LinkageName = Name; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1488 | } else { |
| 1489 | // Use llvm function name as linkage name. |
| 1490 | Name = Fn->getName(); |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 1491 | LinkageName = Name; |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 1492 | } |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1493 | if (!Name.empty() && Name[0] == '\01') |
| 1494 | Name = Name.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
Devang Patel | 970c618 | 2010-04-24 00:49:16 +0000 | [diff] [blame] | 1496 | // It is expected that CurLoc is set before using EmitFunctionStart. |
| 1497 | // Usually, CurLoc points to the left bracket location of compound |
| 1498 | // statement representing function body. |
| 1499 | llvm::DIFile Unit = getOrCreateFile(CurLoc); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1500 | unsigned LineNo = getLineNumber(CurLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1502 | llvm::DISubprogram SP = |
Devang Patel | 970c618 | 2010-04-24 00:49:16 +0000 | [diff] [blame] | 1503 | DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo, |
Mike Stump | 91cc815 | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 1504 | getOrCreateType(FnType, Unit), |
Devang Patel | 15a3d7d | 2010-07-15 23:09:46 +0000 | [diff] [blame] | 1505 | Fn->hasInternalLinkage(), true/*definition*/, |
| 1506 | 0, 0, llvm::DIType(), |
| 1507 | D->isImplicit(), |
| 1508 | CGM.getLangOptions().Optimize, Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1510 | // Push function on region stack. |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1511 | llvm::MDNode *SPN = SP; |
| 1512 | RegionStack.push_back(SPN); |
| 1513 | RegionMap[D] = llvm::WeakVH(SP); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1514 | |
| 1515 | // Clear stack used to keep track of #line directives. |
| 1516 | LineDirectiveFiles.clear(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1520 | void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1521 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1523 | // Don't bother if things are the same as last time. |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1524 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | if (CurLoc == PrevLoc |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1526 | || (SM.getInstantiationLineNumber(CurLoc) == |
| 1527 | SM.getInstantiationLineNumber(PrevLoc) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1528 | && SM.isFromSameFile(CurLoc, PrevLoc))) |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 1529 | // New Builder may not be in sync with CGDebugInfo. |
| 1530 | if (!Builder.getCurrentDebugLocation().isUnknown()) |
| 1531 | return; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1532 | |
| 1533 | // Update last state. |
| 1534 | PrevLoc = CurLoc; |
| 1535 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1536 | llvm::MDNode *Scope = RegionStack.back(); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1537 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc), |
| 1538 | getColumnNumber(CurLoc), |
Chris Lattner | e541d01 | 2010-04-02 20:21:43 +0000 | [diff] [blame] | 1539 | Scope)); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1542 | /// UpdateLineDirectiveRegion - Update region stack only if #line directive |
| 1543 | /// has introduced scope change. |
| 1544 | void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) { |
| 1545 | if (CurLoc.isInvalid() || CurLoc.isMacroID() || |
| 1546 | PrevLoc.isInvalid() || PrevLoc.isMacroID()) |
| 1547 | return; |
| 1548 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 1549 | PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); |
| 1550 | PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc); |
| 1551 | |
| 1552 | if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename())) |
| 1553 | return; |
| 1554 | |
| 1555 | // If #line directive stack is empty then we are entering a new scope. |
| 1556 | if (LineDirectiveFiles.empty()) { |
| 1557 | EmitRegionStart(Builder); |
| 1558 | LineDirectiveFiles.push_back(PCLoc.getFilename()); |
| 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | assert (RegionStack.size() >= LineDirectiveFiles.size() |
| 1563 | && "error handling #line regions!"); |
| 1564 | |
| 1565 | bool SeenThisFile = false; |
| 1566 | for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(), |
| 1567 | E = LineDirectiveFiles.end(); I != E; ++I) |
| 1568 | if (!strcmp(PPLoc.getFilename(), *I)) { |
| 1569 | SeenThisFile = true; |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | // If #line for this file is seen earlier then pop out #line regions. |
| 1574 | if (SeenThisFile) { |
| 1575 | while (!LineDirectiveFiles.empty()) { |
| 1576 | const char *LastFile = LineDirectiveFiles.back(); |
| 1577 | RegionStack.pop_back(); |
| 1578 | LineDirectiveFiles.pop_back(); |
| 1579 | if (!strcmp(PPLoc.getFilename(), LastFile)) |
| 1580 | break; |
| 1581 | } |
| 1582 | return; |
| 1583 | } |
| 1584 | |
| 1585 | // .. otherwise insert new #line region. |
| 1586 | EmitRegionStart(Builder); |
| 1587 | LineDirectiveFiles.push_back(PCLoc.getFilename()); |
| 1588 | |
| 1589 | return; |
| 1590 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1591 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 1592 | /// region - "llvm.dbg.region.start.". |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1593 | void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) { |
Devang Patel | 8fae060 | 2009-11-13 19:10:24 +0000 | [diff] [blame] | 1594 | llvm::DIDescriptor D = |
| 1595 | DebugFactory.CreateLexicalBlock(RegionStack.empty() ? |
| 1596 | llvm::DIDescriptor() : |
Devang Patel | d19429f | 2010-02-16 21:41:20 +0000 | [diff] [blame] | 1597 | llvm::DIDescriptor(RegionStack.back()), |
Stuart Hastings | 257d1d3 | 2010-07-19 23:56:31 +0000 | [diff] [blame] | 1598 | getOrCreateFile(CurLoc), |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1599 | getLineNumber(CurLoc), |
| 1600 | getColumnNumber(CurLoc)); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1601 | llvm::MDNode *DN = D; |
| 1602 | RegionStack.push_back(DN); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 1606 | /// region - "llvm.dbg.region.end." |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1607 | void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1608 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1609 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1610 | // Provide an region stop point. |
Devang Patel | 4d939e6 | 2010-07-20 22:20:10 +0000 | [diff] [blame] | 1611 | EmitStopPoint(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 1613 | RegionStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 1616 | /// EmitFunctionEnd - Constructs the debug code for exiting a function. |
| 1617 | void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { |
| 1618 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1619 | unsigned RCount = FnBeginRegionCount.back(); |
| 1620 | assert(RCount <= RegionStack.size() && "Region stack mismatch"); |
| 1621 | |
| 1622 | // Pop all regions for this function. |
| 1623 | while (RegionStack.size() != RCount) |
| 1624 | EmitRegionEnd(Builder); |
| 1625 | FnBeginRegionCount.pop_back(); |
| 1626 | } |
| 1627 | |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1628 | // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. |
| 1629 | // See BuildByRefType. |
| 1630 | llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, |
| 1631 | uint64_t *XOffset) { |
| 1632 | |
| 1633 | llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; |
| 1634 | |
| 1635 | QualType FType; |
| 1636 | uint64_t FieldSize, FieldOffset; |
| 1637 | unsigned FieldAlign; |
| 1638 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1639 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1640 | QualType Type = VD->getType(); |
| 1641 | |
| 1642 | FieldOffset = 0; |
| 1643 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1644 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 1645 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1646 | FType = CGM.getContext().IntTy; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1647 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 1648 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); |
| 1649 | |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1650 | bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type); |
| 1651 | if (HasCopyAndDispose) { |
| 1652 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1653 | EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", |
| 1654 | &FieldOffset)); |
| 1655 | EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", |
| 1656 | &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 1660 | if (Align > CharUnits::fromQuantity( |
| 1661 | CGM.getContext().Target.getPointerAlign(0) / 8)) { |
| 1662 | unsigned AlignedOffsetInBytes |
| 1663 | = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity()); |
| 1664 | unsigned NumPaddingBytes |
| 1665 | = AlignedOffsetInBytes - FieldOffset/8; |
| 1666 | |
| 1667 | if (NumPaddingBytes > 0) { |
| 1668 | llvm::APInt pad(32, NumPaddingBytes); |
| 1669 | FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, |
| 1670 | pad, ArrayType::Normal, 0); |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1671 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | FType = Type; |
Benjamin Kramer | 48c70f6 | 2010-04-24 20:19:58 +0000 | [diff] [blame] | 1676 | llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1677 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 1678 | FieldAlign = Align.getQuantity()*8; |
| 1679 | |
| 1680 | *XOffset = FieldOffset; |
| 1681 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1682 | VD->getName(), Unit, |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1683 | 0, FieldSize, FieldAlign, |
| 1684 | FieldOffset, 0, FieldTy); |
| 1685 | EltTys.push_back(FieldTy); |
| 1686 | FieldOffset += FieldSize; |
| 1687 | |
| 1688 | llvm::DIArray Elements = |
| 1689 | DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); |
| 1690 | |
| 1691 | unsigned Flags = llvm::DIType::FlagBlockByrefStruct; |
| 1692 | |
| 1693 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, |
Devang Patel | d58562e | 2010-03-09 22:49:11 +0000 | [diff] [blame] | 1694 | Unit, "", Unit, |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1695 | 0, FieldOffset, 0, 0, Flags, |
| 1696 | llvm::DIType(), Elements); |
| 1697 | |
| 1698 | } |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1699 | /// EmitDeclare - Emit local variable declaration debug info. |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1700 | void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1701 | llvm::Value *Storage, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 1702 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1703 | |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1704 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1705 | llvm::DIType Ty; |
| 1706 | uint64_t XOffset = 0; |
| 1707 | if (VD->hasAttr<BlocksAttr>()) |
| 1708 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 1709 | else |
| 1710 | Ty = getOrCreateType(VD->getType(), Unit); |
Chris Lattner | 650cea9 | 2009-05-05 04:57:08 +0000 | [diff] [blame] | 1711 | |
Devang Patel | f4e54a2 | 2010-05-07 23:05:55 +0000 | [diff] [blame] | 1712 | // If there is not any debug info for type then do not emit debug info |
| 1713 | // for this variable. |
| 1714 | if (!Ty) |
| 1715 | return; |
| 1716 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1717 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1718 | unsigned Line = getLineNumber(VD->getLocation()); |
| 1719 | unsigned Column = getColumnNumber(VD->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1721 | // Create the descriptor for the variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | llvm::DIVariable D = |
Devang Patel | 8fae060 | 2009-11-13 19:10:24 +0000 | [diff] [blame] | 1723 | DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()), |
Devang Patel | 239cec6 | 2010-02-01 21:39:52 +0000 | [diff] [blame] | 1724 | VD->getName(), |
Devang Patel | 44eeeba | 2010-06-05 01:14:40 +0000 | [diff] [blame] | 1725 | Unit, Line, Ty, CGM.getLangOptions().Optimize); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1726 | // Insert an llvm.dbg.declare into the current block. |
Devang Patel | ebf16e8 | 2009-11-11 19:10:19 +0000 | [diff] [blame] | 1727 | llvm::Instruction *Call = |
Devang Patel | a020380 | 2009-11-10 23:07:24 +0000 | [diff] [blame] | 1728 | DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Devang Patel | 23908b8 | 2009-11-12 18:21:39 +0000 | [diff] [blame] | 1729 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1730 | llvm::MDNode *Scope = RegionStack.back(); |
Chris Lattner | e541d01 | 2010-04-02 20:21:43 +0000 | [diff] [blame] | 1731 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1734 | /// EmitDeclare - Emit local variable declaration debug info. |
| 1735 | void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, |
| 1736 | llvm::Value *Storage, CGBuilderTy &Builder, |
| 1737 | CodeGenFunction *CGF) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1738 | const ValueDecl *VD = BDRE->getDecl(); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1739 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 1740 | |
Devang Patel | 2b594b9 | 2010-04-26 23:28:46 +0000 | [diff] [blame] | 1741 | if (Builder.GetInsertBlock() == 0) |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1742 | return; |
| 1743 | |
| 1744 | uint64_t XOffset = 0; |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1745 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
Devang Patel | 809b9bb | 2010-02-10 18:49:08 +0000 | [diff] [blame] | 1746 | llvm::DIType Ty; |
| 1747 | if (VD->hasAttr<BlocksAttr>()) |
| 1748 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
| 1749 | else |
| 1750 | Ty = getOrCreateType(VD->getType(), Unit); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1751 | |
| 1752 | // Get location information. |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1753 | unsigned Line = getLineNumber(VD->getLocation()); |
| 1754 | unsigned Column = getColumnNumber(VD->getLocation()); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1755 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1756 | CharUnits offset = CGF->BlockDecls[VD]; |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1757 | llvm::SmallVector<llvm::Value *, 9> addr; |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1758 | const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); |
| 1759 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1760 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
| 1761 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1762 | if (BDRE->isByRef()) { |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1763 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1764 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1765 | // offset of __forwarding field |
| 1766 | offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1767 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
| 1768 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); |
| 1769 | addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1770 | // offset of x field |
| 1771 | offset = CharUnits::fromQuantity(XOffset/8); |
Chris Lattner | 14b1a36 | 2010-01-25 03:29:35 +0000 | [diff] [blame] | 1772 | addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | // Create the descriptor for the variable. |
| 1776 | llvm::DIVariable D = |
Chris Lattner | 165714e | 2010-01-25 03:34:56 +0000 | [diff] [blame] | 1777 | DebugFactory.CreateComplexVariable(Tag, |
| 1778 | llvm::DIDescriptor(RegionStack.back()), |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1779 | VD->getName(), Unit, Line, Ty, |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1780 | addr); |
| 1781 | // Insert an llvm.dbg.declare into the current block. |
Devang Patel | ebf16e8 | 2009-11-11 19:10:19 +0000 | [diff] [blame] | 1782 | llvm::Instruction *Call = |
Chris Lattner | 165714e | 2010-01-25 03:34:56 +0000 | [diff] [blame] | 1783 | DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Chris Lattner | d5b8902 | 2009-12-28 21:44:41 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1785 | llvm::MDNode *Scope = RegionStack.back(); |
Devang Patel | f8e10a5 | 2010-05-10 23:48:38 +0000 | [diff] [blame] | 1786 | Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1789 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1790 | llvm::Value *Storage, |
| 1791 | CGBuilderTy &Builder) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1792 | EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1795 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( |
| 1796 | const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder, |
| 1797 | CodeGenFunction *CGF) { |
| 1798 | EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF); |
| 1799 | } |
| 1800 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1801 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 1802 | /// variable declaration. |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1803 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1804 | CGBuilderTy &Builder) { |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1805 | EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | |
| 1809 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1810 | /// EmitGlobalVariable - Emit information about a global variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1812 | const VarDecl *D) { |
| 1813 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1814 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1815 | llvm::DIFile Unit = getOrCreateFile(D->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1816 | unsigned LineNo = getLineNumber(D->getLocation()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1817 | |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1818 | QualType T = D->getType(); |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1819 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1821 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 1822 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1824 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1825 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1827 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame] | 1828 | ArrayType::Normal, 0); |
| 1829 | } |
Devang Patel | 5d822f0 | 2010-04-29 17:48:37 +0000 | [diff] [blame] | 1830 | llvm::StringRef DeclName = D->getName(); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 1831 | llvm::StringRef LinkageName; |
Devang Patel | 0fd3d1f | 2010-05-14 16:55:25 +0000 | [diff] [blame] | 1832 | if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())) |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 1833 | LinkageName = Var->getName(); |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1834 | llvm::DIDescriptor DContext = |
| 1835 | getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit); |
Devang Patel | 8b90a78 | 2010-05-13 23:52:37 +0000 | [diff] [blame] | 1836 | DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName, |
| 1837 | Unit, LineNo, getOrCreateType(T, Unit), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 1838 | Var->hasInternalLinkage(), |
| 1839 | true/*definition*/, Var); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1842 | /// EmitGlobalVariable - Emit information about an objective-c interface. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1844 | ObjCInterfaceDecl *ID) { |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1845 | // Create global variable debug descriptor. |
Devang Patel | 1780055 | 2010-03-09 00:44:50 +0000 | [diff] [blame] | 1846 | llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1847 | unsigned LineNo = getLineNumber(ID->getLocation()); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1848 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1849 | llvm::StringRef Name = ID->getName(); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1850 | |
Devang Patel | d6c5a26 | 2010-02-01 21:52:22 +0000 | [diff] [blame] | 1851 | QualType T = CGM.getContext().getObjCInterfaceType(ID); |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1852 | if (T->isIncompleteArrayType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1854 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 1855 | llvm::APSInt ConstVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1857 | ConstVal = 1; |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1858 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
Anders Carlsson | 20f12a2 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 1860 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1861 | ArrayType::Normal, 0); |
| 1862 | } |
| 1863 | |
Devang Patel | f6a39b7 | 2009-10-20 18:26:30 +0000 | [diff] [blame] | 1864 | DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo, |
Devang Patel | 9ca36b6 | 2009-02-26 21:10:26 +0000 | [diff] [blame] | 1865 | getOrCreateType(T, Unit), |
| 1866 | Var->hasInternalLinkage(), |
| 1867 | true/*definition*/, Var); |
| 1868 | } |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1869 | |
Devang Patel | 25c2c8f | 2010-08-10 17:53:33 +0000 | [diff] [blame] | 1870 | /// EmitGlobalVariable - Emit global variable's debug info. |
| 1871 | void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, |
| 1872 | llvm::ConstantInt *Init, |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 1873 | CGBuilderTy &Builder) { |
| 1874 | // Create the descriptor for the variable. |
| 1875 | llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); |
| 1876 | llvm::StringRef Name = VD->getName(); |
Devang Patel | 0317ab0 | 2010-08-10 18:27:15 +0000 | [diff] [blame] | 1877 | llvm::DIType Ty = getOrCreateType(VD->getType(), Unit); |
Devang Patel | 6237cea | 2010-08-23 22:07:25 +0000 | [diff] [blame] | 1878 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) { |
| 1879 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) |
| 1880 | Ty = CreateEnumType(ED, Unit); |
| 1881 | } |
Devang Patel | 0317ab0 | 2010-08-10 18:27:15 +0000 | [diff] [blame] | 1882 | // Do not use DIGlobalVariable for enums. |
| 1883 | if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type) |
| 1884 | return; |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 1885 | DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, |
| 1886 | getLineNumber(VD->getLocation()), |
Devang Patel | 4c4acc0 | 2010-08-10 20:16:57 +0000 | [diff] [blame] | 1887 | Ty, true, true, Init); |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1890 | /// getOrCreateNamesSpace - Return namespace descriptor for the given |
| 1891 | /// namespace decl. |
| 1892 | llvm::DINameSpace |
| 1893 | CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl, |
| 1894 | llvm::DIDescriptor Unit) { |
| 1895 | llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = |
| 1896 | NameSpaceCache.find(NSDecl); |
| 1897 | if (I != NameSpaceCache.end()) |
| 1898 | return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); |
| 1899 | |
Devang Patel | 8ab870d | 2010-05-12 23:46:38 +0000 | [diff] [blame] | 1900 | unsigned LineNo = getLineNumber(NSDecl->getLocation()); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1901 | |
| 1902 | llvm::DIDescriptor Context = |
Devang Patel | eb6d79b | 2010-02-01 21:34:11 +0000 | [diff] [blame] | 1903 | getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1904 | llvm::DINameSpace NS = |
| 1905 | DebugFactory.CreateNameSpace(Context, NSDecl->getName(), |
Devang Patel | 2ed8f00 | 2010-08-27 17:47:47 +0000 | [diff] [blame] | 1906 | llvm::DIFile(Unit), LineNo); |
Devang Patel | ab69979 | 2010-05-07 18:12:35 +0000 | [diff] [blame] | 1907 | NameSpaceCache[NSDecl] = llvm::WeakVH(NS); |
Devang Patel | abb485f | 2010-02-01 19:16:32 +0000 | [diff] [blame] | 1908 | return NS; |
| 1909 | } |