CGDebugInfo.cpp revision 0691f93ac23978e2b28b031e5c4a0b3c6dc94798
1b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// 2b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// 3b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// The LLVM Compiler Infrastructure 4b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// 5b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// This file is distributed under the University of Illinois Open Source 6b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// License. See LICENSE.TXT for details. 7b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// 8b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//===----------------------------------------------------------------------===// 9b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// 10b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// This coordinates the debug information generation while generating code. 11b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// 12b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//===----------------------------------------------------------------------===// 13b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 14b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "CGDebugInfo.h" 15b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "CodeGenFunction.h" 1621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "CodeGenModule.h" 1721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "CGBlocks.h" 1821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/AST/ASTContext.h" 1921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/AST/DeclFriend.h" 2021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/AST/DeclObjC.h" 2121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/AST/DeclTemplate.h" 2221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/AST/Expr.h" 2321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/AST/RecordLayout.h" 2421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/Basic/SourceManager.h" 2521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/Basic/FileManager.h" 2621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/Basic/Version.h" 2721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "clang/Frontend/CodeGenOptions.h" 2821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Constants.h" 2921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/DerivedTypes.h" 3021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Instructions.h" 3121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Intrinsics.h" 3221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Module.h" 3321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/ADT/StringExtras.h" 3421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/ADT/SmallVector.h" 3521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Support/Dwarf.h" 3621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Support/Path.h" 3721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Target/TargetData.h" 3821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch#include "llvm/Target/TargetMachine.h" 3921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochusing namespace clang; 4021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochusing namespace clang::CodeGen; 4121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 4221efce637eb329c94f1323b6a2334a1c977e1a9dBen MurdochCGDebugInfo::CGDebugInfo(CodeGenModule &CGM) 4321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch : CGM(CGM), DBuilder(CGM.getModule()), 4421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch BlockLiteralGenericSet(false) { 4521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch CreateCompileUnit(); 4621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch} 4721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 4821efce637eb329c94f1323b6a2334a1c977e1a9dBen MurdochCGDebugInfo::~CGDebugInfo() { 4921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); 5021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch} 5121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 5221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochvoid CGDebugInfo::setLocation(SourceLocation Loc) { 5321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (Loc.isValid()) 5421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc); 5521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch} 5621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 5721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch/// getContextDescriptor - Get context info for the decl. 5821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochllvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) { 5921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (!Context) 6021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return TheCU; 6121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 6221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator 6321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch I = RegionMap.find(Context); 6421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (I != RegionMap.end()) 6521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second)); 6621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 6721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Check namespace. 6821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) 6921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl)); 7021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 7121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) { 7221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (!RDecl->isDependentType()) { 7321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), 7421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch getOrCreateMainFile()); 7521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return llvm::DIDescriptor(Ty); 7621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } 7721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } 7821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return TheCU; 7921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch} 8021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 8121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch/// getFunctionName - Get function name for the given FunctionDecl. If the 8221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch/// name is constructred on demand (e.g. C++ destructor) then the name 8321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch/// is stored on the side. 8421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochllvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { 8521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch assert (FD && "Invalid FunctionDecl!"); 8621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch IdentifierInfo *FII = FD->getIdentifier(); 8721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (FII) 8821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return FII->getName(); 8921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 9021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Otherwise construct human readable name for debug info. 9121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch std::string NS = FD->getNameAsString(); 9221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 9321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Copy this name on the side and use its reference. 9421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); 9521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch memcpy(StrPtr, NS.data(), NS.length()); 9621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return llvm::StringRef(StrPtr, NS.length()); 9721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch} 9821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 9921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochllvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { 100537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch llvm::SmallString<256> MethodName; 101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::raw_svector_ostream OS(MethodName); 102b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; 103b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const DeclContext *DC = OMD->getDeclContext(); 104014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (const ObjCImplementationDecl *OID = 105014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch dyn_cast<const ObjCImplementationDecl>(DC)) { 106b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch OS << OID->getName(); 107b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } else if (const ObjCInterfaceDecl *OID = 108b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch dyn_cast<const ObjCInterfaceDecl>(DC)) { 109b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch OS << OID->getName(); 110342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch } else if (const ObjCCategoryImplDecl *OCD = 111014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch dyn_cast<const ObjCCategoryImplDecl>(DC)){ 112014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' << 113014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch OCD->getIdentifier()->getNameStart() << ')'; 114b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 115b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch OS << ' ' << OMD->getSelector().getAsString() << ']'; 116b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 117b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell()); 118014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch memcpy(StrPtr, MethodName.begin(), OS.tell()); 11921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return llvm::StringRef(StrPtr, OS.tell()); 120014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 121b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 122b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// getClassName - Get class name including template argument list. 123b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochllvm::StringRef 124014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochCGDebugInfo::getClassName(RecordDecl *RD) { 125b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch ClassTemplateSpecializationDecl *Spec 126b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch = dyn_cast<ClassTemplateSpecializationDecl>(RD); 127b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (!Spec) 128014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return RD->getName(); 129b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 130b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const TemplateArgument *Args; 131b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch unsigned NumArgs; 132b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch std::string Buffer; 133342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 134014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch const TemplateSpecializationType *TST = 135b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch cast<TemplateSpecializationType>(TAW->getType()); 136b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Args = TST->getArgs(); 137b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch NumArgs = TST->getNumArgs(); 138b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } else { 139b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 140b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Args = TemplateArgs.data(); 141b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch NumArgs = TemplateArgs.size(); 142014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 143014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Buffer = RD->getIdentifier()->getNameStart(); 144014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch PrintingPolicy Policy(CGM.getLangOptions()); 145342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, 146b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch NumArgs, 147b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Policy); 148b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 149b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Copy this name on the side and use its reference. 150b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length()); 151b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch memcpy(StrPtr, Buffer.data(), Buffer.length()); 152b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return llvm::StringRef(StrPtr, Buffer.length()); 153b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 154b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 155b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// getOrCreateFile - Get the file debug info descriptor for the input location. 156b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochllvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { 157b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (!Loc.isValid()) 158b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // If Location is not valid then use main input file. 159b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory()); 160b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 161b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch SourceManager &SM = CGM.getContext().getSourceManager(); 162b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch PresumedLoc PLoc = SM.getPresumedLoc(Loc); 163b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 164b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (PLoc.isInvalid()) 165b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // If the location is not valid then use main input file. 166b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory()); 167014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 168b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Cache the results. 169b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const char *fname = PLoc.getFilename(); 170b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = 171b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch DIFileCache.find(fname); 172014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 173b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (it != DIFileCache.end()) { 174b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Verify that the information still exists. 175b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (&*it->second) 176b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return llvm::DIFile(cast<llvm::MDNode>(it->second)); 177b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 178014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 179014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile F = DBuilder.CreateFile(PLoc.getFilename(), getCurrentDirname()); 180b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 181b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch DIFileCache[fname] = F; 182014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return F; 183014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 184b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 185b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 186014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// getOrCreateMainFile - Get the file info for main compile unit. 187014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIFile CGDebugInfo::getOrCreateMainFile() { 188014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory()); 189b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 190b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 191b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// getLineNumber - Get line number for the location. If location is invalid 192b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// then use current location. 193b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochunsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { 194b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch assert (CurLoc.isValid() && "Invalid current location!"); 195b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch SourceManager &SM = CGM.getContext().getSourceManager(); 196342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); 197014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return PLoc.isValid()? PLoc.getLine() : 0; 198b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 199b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 200958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier/// getColumnNumber - Get column number for the location. If location is 201b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// invalid then use current location. 202342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochunsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) { 203342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch assert (CurLoc.isValid() && "Invalid current location!"); 204014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch SourceManager &SM = CGM.getContext().getSourceManager(); 205b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); 206b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return PLoc.isValid()? PLoc.getColumn() : 0; 207b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 208342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 209014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::StringRef CGDebugInfo::getCurrentDirname() { 210014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (!CWDName.empty()) 211014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return CWDName; 212014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch char *CompDirnamePtr = NULL; 213014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory(); 214014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size()); 215014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch memcpy(CompDirnamePtr, CWD.c_str(), CWD.size()); 216014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size()); 217014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 218014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 219014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// CreateCompileUnit - Create new compile unit. 2201b268ca467c924004286c97bac133db489cf43d0Ben Murdochvoid CGDebugInfo::CreateCompileUnit() { 221014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 222342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch // Get absolute path name. 223342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch SourceManager &SM = CGM.getContext().getSourceManager(); 224014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch std::string MainFileName = CGM.getCodeGenOpts().MainFileName; 225014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (MainFileName.empty()) 226014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch MainFileName = "<unknown>"; 227014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 22821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // The main file name provided via the "-main-file-name" option contains just 22921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // the file name itself with no path information. This file name may have had 23021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // a relative path, so we look into the actual file entry for the main 23121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // file to determine the real absolute path for the file. 23221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch std::string MainFileDir; 23321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 234b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch MainFileDir = MainFile->getDir()->getName(); 235b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (MainFileDir != ".") 236b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch MainFileName = MainFileDir + "/" + MainFileName; 237b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 2381b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 2391b268ca467c924004286c97bac133db489cf43d0Ben Murdoch // Save filename string. 2401b268ca467c924004286c97bac133db489cf43d0Ben Murdoch char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length()); 2411b268ca467c924004286c97bac133db489cf43d0Ben Murdoch memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length()); 2421b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::StringRef Filename(FilenamePtr, MainFileName.length()); 2431b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 244b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch unsigned LangTag; 24521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch const LangOptions &LO = CGM.getLangOptions(); 24621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (LO.CPlusPlus) { 24721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (LO.ObjC1) 24821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; 24921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch else 25021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch LangTag = llvm::dwarf::DW_LANG_C_plus_plus; 25121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } else if (LO.ObjC1) { 25221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch LangTag = llvm::dwarf::DW_LANG_ObjC; 25321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } else if (LO.C99) { 25421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch LangTag = llvm::dwarf::DW_LANG_C99; 25521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } else { 256014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch LangTag = llvm::dwarf::DW_LANG_C89; 2571b268ca467c924004286c97bac133db489cf43d0Ben Murdoch } 258014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 259014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch std::string Producer = getClangFullVersion(); 260014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 261014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Figure out which version of the ObjC runtime we have. 2621b268ca467c924004286c97bac133db489cf43d0Ben Murdoch unsigned RuntimeVers = 0; 263b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (LO.ObjC1) 264014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; 265014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 266014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Create new compile unit. 267342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch DBuilder.CreateCompileUnit( 268014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch LangTag, Filename, getCurrentDirname(), 269014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Producer, 270014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); 271014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // FIXME - Eliminate TheCU. 2721b268ca467c924004286c97bac133db489cf43d0Ben Murdoch TheCU = llvm::DICompileUnit(DBuilder.getCU()); 2731b268ca467c924004286c97bac133db489cf43d0Ben Murdoch} 2741b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 2751b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// CreateType - Get the Basic type from the cache or create a new 2761b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// one if necessary. 2771b268ca467c924004286c97bac133db489cf43d0Ben Murdochllvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) { 2781b268ca467c924004286c97bac133db489cf43d0Ben Murdoch unsigned Encoding = 0; 2791b268ca467c924004286c97bac133db489cf43d0Ben Murdoch const char *BTName = NULL; 2801b268ca467c924004286c97bac133db489cf43d0Ben Murdoch switch (BT->getKind()) { 2811b268ca467c924004286c97bac133db489cf43d0Ben Murdoch default: 2821b268ca467c924004286c97bac133db489cf43d0Ben Murdoch case BuiltinType::Void: 283014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return llvm::DIType(); 284014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch case BuiltinType::ObjCClass: 285014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return DBuilder.CreateStructType(TheCU, "objc_class", 286014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch getOrCreateMainFile(), 0, 0, 0, 287014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIDescriptor::FlagFwdDecl, 28821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIArray()); 28921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::ObjCId: { 29021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // typedef struct objc_class *Class; 29121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // typedef struct objc_object { 29221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Class isa; 29321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // } *id; 29421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 29521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIType OCTy = 29621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch DBuilder.CreateStructType(TheCU, "objc_class", 29721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch getOrCreateMainFile(), 0, 0, 0, 29821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIDescriptor::FlagFwdDecl, 29921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIArray()); 30021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 30121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 30221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIType ISATy = DBuilder.CreatePointerType(OCTy, Size); 30321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 30421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::SmallVector<llvm::Value *, 16> EltTys; 30521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIType FieldTy = 30621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch DBuilder.CreateMemberType("isa", getOrCreateMainFile(), 30721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 0,Size, 0, 0, 0, ISATy); 30821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch EltTys.push_back(FieldTy); 30921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIArray Elements = 31021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); 31121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 31221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return DBuilder.CreateStructType(TheCU, "objc_object", 31321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch getOrCreateMainFile(), 31421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 0, 0, 0, 0, Elements); 31521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } 31621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::ObjCSel: { 31721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return DBuilder.CreateStructType(TheCU, "objc_selector", 31821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch getOrCreateMainFile(), 0, 0, 0, 31921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIDescriptor::FlagFwdDecl, 32021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIArray()); 32121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } 32221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::UChar: 32321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 32421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Char_S: 32521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 32621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::UShort: 32721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::UInt: 32821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::ULong: 32921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 33021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Short: 33121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Int: 33221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Long: 33321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 33421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 33521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Float: 33621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::LongDouble: 33721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 33821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } 33921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 34021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch switch (BT->getKind()) { 34121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::Long: BTName = "long int"; break; 34221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::LongLong: BTName = "long long int"; break; 34321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::ULong: BTName = "long unsigned int"; break; 34421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch case BuiltinType::ULongLong: BTName = "long long unsigned int"; break; 34521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch default: 34621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch BTName = BT->getName(CGM.getContext().getLangOptions()); 34721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch break; 34821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch } 34921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Bit size, align and offset of the type. 35021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch uint64_t Size = CGM.getContext().getTypeSize(BT); 35121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch uint64_t Align = CGM.getContext().getTypeAlign(BT); 35221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIType DbgTy = 35321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch DBuilder.CreateBasicType(BTName, Size, Align, Encoding); 35421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch return DbgTy; 35521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch} 35621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 35721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochllvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) { 35821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Bit size, align and offset of the type. 359014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; 360b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Ty->isComplexIntegerType()) 361b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Encoding = llvm::dwarf::DW_ATE_lo_user; 362b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 363014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch uint64_t Size = CGM.getContext().getTypeSize(Ty); 364014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch uint64_t Align = CGM.getContext().getTypeAlign(Ty); 365b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType DbgTy = 366014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch DBuilder.CreateBasicType("complex", Size, Align, Encoding); 367b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 368b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return DbgTy; 369014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 370b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 371b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CreateCVRType - Get the qualified type from the cache or create 37221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch/// a new one if necessary. 37321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochllvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { 37421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch QualifierCollector Qc; 37521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch const Type *T = Qc.strip(Ty); 37621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 37721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Ignore these qualifiers for now. 37821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch Qc.removeObjCGCAttr(); 37921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch Qc.removeAddressSpace(); 38021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 38121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // We will create one Derived type for one qualifier and recurse to handle any 38221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // additional ones. 383b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch unsigned Tag; 384b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Qc.hasConst()) { 385b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Tag = llvm::dwarf::DW_TAG_const_type; 386b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Qc.removeConst(); 387b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } else if (Qc.hasVolatile()) { 388b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Tag = llvm::dwarf::DW_TAG_volatile_type; 389b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Qc.removeVolatile(); 390b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } else if (Qc.hasRestrict()) { 391b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Tag = llvm::dwarf::DW_TAG_restrict_type; 392b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Qc.removeRestrict(); 393b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } else { 394b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch assert(Qc.empty() && "Unknown type qualifier for debug info"); 395b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return getOrCreateType(QualType(T, 0), Unit); 396b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 397b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 398b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); 399b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 400958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier // No need to fill in the Name, Line, Size, Alignment, Offset in case of 401958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier // CVR derived types. 402014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType DbgTy = DBuilder.CreateQualifiedType(Tag, FromTy); 403014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 404958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier return DbgTy; 405958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier} 406958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier 407958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernierllvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, 408958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier llvm::DIFile Unit) { 409958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier llvm::DIType DbgTy = 410958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 411958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier Ty->getPointeeType(), Unit); 412958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier return DbgTy; 413014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 414958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier 415958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernierllvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 416958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier llvm::DIFile Unit) { 417958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 418014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Ty->getPointeeType(), Unit); 419b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 420014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 421014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// CreatePointeeType - Create PointTee type. If Pointee is a record 422014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// then emit record's fwd if debug info size reduction is enabled. 423014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy, 424014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile Unit) { 425014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (!CGM.getCodeGenOpts().LimitDebugInfo) 426014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return getOrCreateType(PointeeTy, Unit); 427014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 428014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) { 429014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch RecordDecl *RD = RTy->getDecl(); 430014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 431014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch unsigned Line = getLineNumber(RD->getLocation()); 432014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIDescriptor FDContext = 433014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext())); 434014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 435014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (RD->isStruct()) 436b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return DBuilder.CreateStructType(FDContext, RD->getName(), DefUnit, 437b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Line, 0, 0, llvm::DIType::FlagFwdDecl, 438014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIArray()); 439014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch else if (RD->isUnion()) 440b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return DBuilder.CreateUnionType(FDContext, RD->getName(), DefUnit, 441b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Line, 0, 0, llvm::DIType::FlagFwdDecl, 442b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIArray()); 443014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch else { 444b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch assert(RD->isClass() && "Unknown RecordType!"); 445014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return DBuilder.CreateClassType(FDContext, RD->getName(), DefUnit, 446014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Line, 0, 0, 0, llvm::DIType::FlagFwdDecl, 447b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType(), llvm::DIArray()); 448014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 449b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 450b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return getOrCreateType(PointeeTy, Unit); 451b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 452b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 453b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 454014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, 455b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const Type *Ty, 456014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch QualType PointeeTy, 457014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile Unit) { 458014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 459014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (Tag == llvm::dwarf::DW_TAG_reference_type) 460014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return DBuilder.CreateReferenceType(CreatePointeeType(PointeeTy, Unit)); 461014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 462014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Bit size, align and offset of the type. 463014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Size is always the size of a pointer. We can't use getTypeSize here 464014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // because that does not return the correct value for references. 465014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch uint64_t Size = 466014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace()); 467014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch uint64_t Align = CGM.getContext().getTypeAlign(Ty); 468014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 469014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return 470014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch DBuilder.CreatePointerType(CreatePointeeType(PointeeTy, Unit), Size, Align); 471014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 472014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 473014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, 474014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile Unit) { 475b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (BlockLiteralGenericSet) 476b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return BlockLiteralGeneric; 477b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 478b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::SmallVector<llvm::Value *, 8> EltTys; 479b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType FieldTy; 480b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch QualType FType; 481b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch uint64_t FieldSize, FieldOffset; 482b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch unsigned FieldAlign; 483b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIArray Elements; 484b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType EltTy, DescTy; 485b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 48621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch FieldOffset = 0; 48721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch FType = CGM.getContext().UnsignedLongTy; 48821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); 48921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); 49021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 49121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); 49221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch EltTys.clear(); 49321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 49421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch unsigned Flags = llvm::DIDescriptor::FlagAppleBlock; 49521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch unsigned LineNo = getLineNumber(CurLoc); 49621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 497b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch EltTy = DBuilder.CreateStructType(Unit, "__block_descriptor", 498b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Unit, LineNo, FieldOffset, 0, 499b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags, Elements); 500b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 501b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Bit size, align and offset of the type. 502b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch uint64_t Size = CGM.getContext().getTypeSize(Ty); 503b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 504b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch DescTy = DBuilder.CreatePointerType(EltTy, Size); 505b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 506b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch FieldOffset = 0; 507b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 508b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 509342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FType = CGM.getContext().IntTy; 510342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 511342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); 512342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 513342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); 514342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 515342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 516342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FieldTy = DescTy; 517342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FieldSize = CGM.getContext().getTypeSize(Ty); 518342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FieldAlign = CGM.getContext().getTypeAlign(Ty); 519342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FieldTy = DBuilder.CreateMemberType("__descriptor", Unit, 520342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch LineNo, FieldSize, FieldAlign, 521342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FieldOffset, 0, FieldTy); 522342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch EltTys.push_back(FieldTy); 523342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 524342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch FieldOffset += FieldSize; 525342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); 526342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 527342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch EltTy = DBuilder.CreateStructType(Unit, "__block_literal_generic", 528342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Unit, LineNo, FieldOffset, 0, 529342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Flags, Elements); 530342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 5311b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BlockLiteralGenericSet = true; 532b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch BlockLiteralGeneric = DBuilder.CreatePointerType(EltTy, Size); 5331b268ca467c924004286c97bac133db489cf43d0Ben Murdoch return BlockLiteralGeneric; 534b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 5351b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 536b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochllvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, 5371b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::DIFile Unit) { 538b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Typedefs are derived from some other type. If we have a typedef of a 539b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // typedef, make sure to emit the whole chain. 540537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 541537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch if (!Src.Verify()) 542537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch return llvm::DIType(); 543537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch // We don't set size information, but do specify where the typedef was 54421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // declared. 545537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); 546b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType DbgTy = DBuilder.CreateTypedef(Src, Ty->getDecl()->getName(), 547014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Unit, Line); 548014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return DbgTy; 549014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 550014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 551014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 552014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile Unit) { 553014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::SmallVector<llvm::Value *, 16> EltTys; 554014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 555014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Add the result type at least. 556014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 557014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 558014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Set up remainder of arguments if there is a prototype. 559014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 560b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (isa<FunctionNoProtoType>(Ty)) 561b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch EltTys.push_back(DBuilder.CreateUnspecifiedParameter()); 562014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { 563014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 564b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); 565b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 566b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 567b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIArray EltTypeArray = 568b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); 569b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 570b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType DbgTy = DBuilder.CreateSubroutineType(Unit, EltTypeArray); 571b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return DbgTy; 572b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 573b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 574b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CollectRecordFields - A helper function to collect debug info for 575014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// record fields. This is used while creating debug info entry for a Record. 576014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid CGDebugInfo:: 577014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochCollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit, 578014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::SmallVectorImpl<llvm::Value *> &EltTys) { 579014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch unsigned FieldNo = 0; 580014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 581014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch for (RecordDecl::field_iterator I = RD->field_begin(), 582014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch E = RD->field_end(); 583014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch I != E; ++I, ++FieldNo) { 584014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch FieldDecl *Field = *I; 585014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 586014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::StringRef FieldName = Field->getName(); 587014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 588014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Ignore unnamed fields. Do not ignore unnamed records. 589014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (FieldName.empty() && !isa<RecordType>(Field->getType())) 590014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch continue; 591014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 592014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Get the location for the field. 593014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); 594014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch unsigned FieldLine = getLineNumber(Field->getLocation()); 595014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch QualType FType = Field->getType(); 596014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch uint64_t FieldSize = 0; 597014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch unsigned FieldAlign = 0; 598014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (!FType->isIncompleteArrayType()) { 599014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 600014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Bit size, align and offset of the type. 601014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch FieldSize = CGM.getContext().getTypeSize(FType); 602014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Expr *BitWidth = Field->getBitWidth(); 603014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (BitWidth) 604014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); 605014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch FieldAlign = CGM.getContext().getTypeAlign(FType); 606014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 607014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 608014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 609014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 610014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch unsigned Flags = 0; 611014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch AccessSpecifier Access = I->getAccess(); 612b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Access == clang::AS_private) 613b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags |= llvm::DIDescriptor::FlagPrivate; 614b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch else if (Access == clang::AS_protected) 615b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags |= llvm::DIDescriptor::FlagProtected; 616b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 617b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit, 618b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch FieldLine, FieldSize, FieldAlign, 619b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch FieldOffset, Flags, FieldTy); 620b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch EltTys.push_back(FieldTy); 621b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 622b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 623014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 624014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This 625014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// function type is not updated to include implicit "this" pointer. Use this 626014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// routine to get a method type which includes "this" pointer. 627014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIType 628014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochCGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, 629014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIFile Unit) { 630014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType FnTy 631014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(), 632014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 0), 633b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Unit); 634b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 635b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Add "this" pointer. 636b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 637b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray(); 638b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch assert (Args.getNumElements() && "Invalid number of arguments!"); 639b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 640b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::SmallVector<llvm::Value *, 16> Elts; 641b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 642b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // First element is always return type. For 'void' functions it is NULL. 643b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Elts.push_back(Args.getElement(0)); 644b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 645b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (!Method->isStatic()) 646b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch { 647b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // "this" pointer is always first argument. 648014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch ASTContext &Context = CGM.getContext(); 649014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch QualType ThisPtr = 650014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Context.getPointerType(Context.getTagDeclType(Method->getParent())); 651014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType ThisPtrType = 652014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch DBuilder.CreateArtificialType(getOrCreateType(ThisPtr, Unit)); 653014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 654014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; 655014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch Elts.push_back(ThisPtrType); 656014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 657014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 658b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Copy rest of the arguments. 659b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) 660b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Elts.push_back(Args.getElement(i)); 661b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 662b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIArray EltTypeArray = 663b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch DBuilder.GetOrCreateArray(Elts.data(), Elts.size()); 664958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier 665958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier return DBuilder.CreateSubroutineType(Unit, EltTypeArray); 666958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier} 667958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier 668958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier/// isFunctionLocalClass - Return true if CXXRecordDecl is defined 6691b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// inside a function. 6701b268ca467c924004286c97bac133db489cf43d0Ben Murdochstatic bool isFunctionLocalClass(const CXXRecordDecl *RD) { 6711b268ca467c924004286c97bac133db489cf43d0Ben Murdoch if (const CXXRecordDecl *NRD = 6721b268ca467c924004286c97bac133db489cf43d0Ben Murdoch dyn_cast<CXXRecordDecl>(RD->getDeclContext())) 6731b268ca467c924004286c97bac133db489cf43d0Ben Murdoch return isFunctionLocalClass(NRD); 6741b268ca467c924004286c97bac133db489cf43d0Ben Murdoch else if (isa<FunctionDecl>(RD->getDeclContext())) 6751b268ca467c924004286c97bac133db489cf43d0Ben Murdoch return true; 676958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier return false; 677537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch 678014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 679537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// CreateCXXMemberFunction - A helper function to create a DISubprogram for 680537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// a single member function GlobalDecl. 681537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdochllvm::DISubprogram 682537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben MurdochCGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, 683537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch llvm::DIFile Unit, 684537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch llvm::DIType RecordTy) { 68521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch bool IsCtorOrDtor = 686537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); 687537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch 688014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::StringRef MethodName = getFunctionName(Method); 689014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); 69021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 691b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Since a single ctor/dtor corresponds to multiple functions, it doesn't 692b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // make sense to give a single ctor/dtor a linkage name. 69321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::StringRef MethodLinkageName; 69421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) 69521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch MethodLinkageName = CGM.getMangledName(Method); 69621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 69721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Get the location for the method. 69821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation()); 69921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch unsigned MethodLine = getLineNumber(Method->getLocation()); 70021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 70121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // Collect virtual method info. 70221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch llvm::DIType ContainingType; 703b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch unsigned Virtuality = 0; 704b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch unsigned VIndex = 0; 70521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 706b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Method->isVirtual()) { 707014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (Method->isPure()) 70821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; 70921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch else 71021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; 71121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 71221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // It doesn't make sense to give a virtual destructor a vtable index, 71321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch // since a single destructor has two entries in the vtable. 71421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch if (!isa<CXXDestructorDecl>(Method)) 715014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch VIndex = CGM.getVTables().getMethodVTableIndex(Method); 716014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch ContainingType = RecordTy; 717014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 718014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 719014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch unsigned Flags = 0; 720014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (Method->isImplicit()) 721b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags |= llvm::DIDescriptor::FlagArtificial; 722b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch AccessSpecifier Access = Method->getAccess(); 723b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Access == clang::AS_private) 724b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags |= llvm::DIDescriptor::FlagPrivate; 725b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch else if (Access == clang::AS_protected) 726b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags |= llvm::DIDescriptor::FlagProtected; 727b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { 728b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (CXXC->isExplicit()) 729b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags |= llvm::DIDescriptor::FlagExplicit; 730b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } else if (const CXXConversionDecl *CXXC = 731b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch dyn_cast<CXXConversionDecl>(Method)) { 732b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (CXXC->isExplicit()) 733b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags |= llvm::DIDescriptor::FlagExplicit; 734b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 735b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Method->hasPrototype()) 736537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch Flags |= llvm::DIDescriptor::FlagPrototyped; 737537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch 738537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch llvm::DISubprogram SP = 739537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch DBuilder.CreateMethod(RecordTy , MethodName, MethodLinkageName, 740b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch MethodDefUnit, MethodLine, 741b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch MethodTy, /*isLocalToUnit=*/false, 742b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch /* isDefinition=*/ false, 743b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Virtuality, VIndex, ContainingType, 744b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Flags, CGM.getLangOptions().Optimize); 745b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 746b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Don't cache ctors or dtors since we have to emit multiple functions for 747014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // a single ctor or dtor. 748014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (!IsCtorOrDtor && Method->isThisDeclarationADefinition()) 749014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch SPCache[Method] = llvm::WeakVH(SP); 750014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 751014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return SP; 752014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 753014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 754014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// CollectCXXMemberFunctions - A helper function to collect debug info for 755014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// C++ member functions.This is used while creating debug info entry for 756014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// a Record. 757b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochvoid CGDebugInfo:: 758b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochCollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, 759b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::SmallVectorImpl<llvm::Value *> &EltTys, 760b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType RecordTy) { 761b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch for(CXXRecordDecl::method_iterator I = RD->method_begin(), 762b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch E = RD->method_end(); I != E; ++I) { 763b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const CXXMethodDecl *Method = *I; 764b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 765b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Method->isImplicit() && !Method->isUsed()) 766b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch continue; 767b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 768b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); 769b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 770b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch} 771b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 772b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CollectCXXFriends - A helper function to collect debug info for 773b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// C++ base classes. This is used while creating debug info entry for 774b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// a Record. 775b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochvoid CGDebugInfo:: 776b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochCollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit, 777014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::SmallVectorImpl<llvm::Value *> &EltTys, 778014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType RecordTy) { 779b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 780b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(), 7811b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BE = RD->friend_end(); BI != BE; ++BI) { 7821b268ca467c924004286c97bac133db489cf43d0Ben Murdoch if (TypeSourceInfo *TInfo = (*BI)->getFriendType()) 7831b268ca467c924004286c97bac133db489cf43d0Ben Murdoch EltTys.push_back(DBuilder.CreateFriend(RecordTy, 7841b268ca467c924004286c97bac133db489cf43d0Ben Murdoch getOrCreateType(TInfo->getType(), 7851b268ca467c924004286c97bac133db489cf43d0Ben Murdoch Unit))); 7861b268ca467c924004286c97bac133db489cf43d0Ben Murdoch } 7871b268ca467c924004286c97bac133db489cf43d0Ben Murdoch} 7881b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 7891b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// CollectCXXBases - A helper function to collect debug info for 7901b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// C++ base classes. This is used while creating debug info entry for 7911b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// a Record. 7921b268ca467c924004286c97bac133db489cf43d0Ben Murdochvoid CGDebugInfo:: 7931b268ca467c924004286c97bac133db489cf43d0Ben MurdochCollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, 7941b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::SmallVectorImpl<llvm::Value *> &EltTys, 795b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType RecordTy) { 7961b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 7971b268ca467c924004286c97bac133db489cf43d0Ben Murdoch const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 7981b268ca467c924004286c97bac133db489cf43d0Ben Murdoch for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), 7991b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BE = RD->bases_end(); BI != BE; ++BI) { 8001b268ca467c924004286c97bac133db489cf43d0Ben Murdoch unsigned BFlags = 0; 8011b268ca467c924004286c97bac133db489cf43d0Ben Murdoch uint64_t BaseOffset; 8021b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 8031b268ca467c924004286c97bac133db489cf43d0Ben Murdoch const CXXRecordDecl *Base = 8041b268ca467c924004286c97bac133db489cf43d0Ben Murdoch cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); 805b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 8061b268ca467c924004286c97bac133db489cf43d0Ben Murdoch if (BI->isVirtual()) { 8071b268ca467c924004286c97bac133db489cf43d0Ben Murdoch // virtual base offset offset is -ve. The code generator emits dwarf 808014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // expression where it expects +ve number. 809014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base); 8101b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BFlags = llvm::DIDescriptor::FlagVirtual; 8111b268ca467c924004286c97bac133db489cf43d0Ben Murdoch } else 8121b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BaseOffset = RL.getBaseClassOffsetInBits(Base); 8131b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 8141b268ca467c924004286c97bac133db489cf43d0Ben Murdoch AccessSpecifier Access = BI->getAccessSpecifier(); 815014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (Access == clang::AS_private) 8161b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BFlags |= llvm::DIDescriptor::FlagPrivate; 817014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch else if (Access == clang::AS_protected) 8181b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BFlags |= llvm::DIDescriptor::FlagProtected; 8191b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 8201b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::DIType DTy = 8211b268ca467c924004286c97bac133db489cf43d0Ben Murdoch DBuilder.CreateInheritance(RecordTy, 8221b268ca467c924004286c97bac133db489cf43d0Ben Murdoch getOrCreateType(BI->getType(), Unit), 8231b268ca467c924004286c97bac133db489cf43d0Ben Murdoch BaseOffset, BFlags); 8241b268ca467c924004286c97bac133db489cf43d0Ben Murdoch EltTys.push_back(DTy); 8251b268ca467c924004286c97bac133db489cf43d0Ben Murdoch } 8261b268ca467c924004286c97bac133db489cf43d0Ben Murdoch} 8271b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 8281b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// getOrCreateVTablePtrType - Return debug info descriptor for vtable. 8291b268ca467c924004286c97bac133db489cf43d0Ben Murdochllvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { 8301b268ca467c924004286c97bac133db489cf43d0Ben Murdoch if (VTablePtrType.isValid()) 8311b268ca467c924004286c97bac133db489cf43d0Ben Murdoch return VTablePtrType; 8321b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 8331b268ca467c924004286c97bac133db489cf43d0Ben Murdoch ASTContext &Context = CGM.getContext(); 8341b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 8351b268ca467c924004286c97bac133db489cf43d0Ben Murdoch /* Function type */ 8361b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::Value *STy = getOrCreateType(Context.IntTy, Unit); 8371b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::DIArray SElements = DBuilder.GetOrCreateArray(&STy, 1); 8381b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::DIType SubTy = DBuilder.CreateSubroutineType(Unit, SElements); 8391b268ca467c924004286c97bac133db489cf43d0Ben Murdoch unsigned Size = Context.getTypeSize(Context.VoidPtrTy); 8401b268ca467c924004286c97bac133db489cf43d0Ben Murdoch llvm::DIType vtbl_ptr_type = DBuilder.CreatePointerType(SubTy, Size, 0, 8411b268ca467c924004286c97bac133db489cf43d0Ben Murdoch "__vtbl_ptr_type"); 8421b268ca467c924004286c97bac133db489cf43d0Ben Murdoch VTablePtrType = DBuilder.CreatePointerType(vtbl_ptr_type, Size); 8431b268ca467c924004286c97bac133db489cf43d0Ben Murdoch return VTablePtrType; 8441b268ca467c924004286c97bac133db489cf43d0Ben Murdoch} 8451b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 8461b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// getVTableName - Get vtable name for the given Class. 8471b268ca467c924004286c97bac133db489cf43d0Ben Murdochllvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { 8481b268ca467c924004286c97bac133db489cf43d0Ben Murdoch // Otherwise construct gdb compatible name name. 8491b268ca467c924004286c97bac133db489cf43d0Ben Murdoch std::string Name = "_vptr$" + RD->getNameAsString(); 850b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 851b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // Copy this name on the side and use its reference. 852b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); 853b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch memcpy(StrPtr, Name.data(), Name.length()); 854b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return llvm::StringRef(StrPtr, Name.length()); 855537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch} 856b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 857537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch 858537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate 859537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// debug info entry in EltTys vector. 860b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochvoid CGDebugInfo:: 861b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochCollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, 862b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::SmallVectorImpl<llvm::Value *> &EltTys) { 863b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 864b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 865b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // If there is a primary base then it will hold vtable info. 866b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (RL.getPrimaryBase()) 867b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return; 868b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 869b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch // If this class is not dynamic then there is not any vtable info to collect. 870b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (!RD->isDynamicClass()) 871b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch return; 872b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 873b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 874b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::DIType VPTR 875b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch = DBuilder.CreateMemberType(getVTableName(RD), Unit, 87621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch 0, Size, 0, 0, 0, 8771b268ca467c924004286c97bac133db489cf43d0Ben Murdoch getOrCreateVTablePtrType(Unit)); 87821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch EltTys.push_back(VPTR); 8791b268ca467c924004286c97bac133db489cf43d0Ben Murdoch} 8801b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 881014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// getOrCreateRecordType - Emit record type's standalone debug info. 882014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy, 883014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch SourceLocation Loc) { 884014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc)); 885014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch DBuilder.RetainType(T); 886014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return T; 887014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch} 888014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 889014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// CreateType - get structure or union type. 890014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochllvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) { 8911b268ca467c924004286c97bac133db489cf43d0Ben Murdoch RecordDecl *RD = Ty->getDecl(); 892342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch llvm::DIFile Unit = getOrCreateFile(RD->getLocation()); 893342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 894342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch // Get overall information about the record type for the debug info. 895342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 896342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch unsigned Line = getLineNumber(RD->getLocation()); 897537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch 898537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch // Records and classes and unions can all be recursive. To handle them, we 899537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch // first generate a debug descriptor for the struct as a forward declaration. 900342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch // Then (if it is a definition) we go through and get debug info for all of 901014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // its members. Finally, we create a descriptor for the complete type (which 902014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // may refer to the forward decl if the struct is recursive) and replace all 903014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // uses of the forward declaration with the final definition. 904014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIDescriptor FDContext = 905014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext())); 906014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 907014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // If this is just a forward declaration, construct an appropriately 908014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // marked node and just return it. 909014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch if (!RD->getDefinition()) { 910014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType FwdDecl = 911014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch DBuilder.CreateStructType(FDContext, RD->getName(), 912014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch DefUnit, Line, 0, 0, 913014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIDescriptor::FlagFwdDecl, 914014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIArray()); 915014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 916014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return FwdDecl; 917014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 918014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 919014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit); 920014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 921014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch llvm::MDNode *MN = FwdDecl; 922537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; 923537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch // Otherwise, insert it into the TypeCache so that recursive uses will find 924537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch // it. 925537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 926537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch // Push the struct on region stack. 9271b268ca467c924004286c97bac133db489cf43d0Ben Murdoch RegionStack.push_back(FwdDeclNode); 928b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); 9291b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 9301b268ca467c924004286c97bac133db489cf43d0Ben Murdoch // Convert all the elements. 931b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::SmallVector<llvm::Value *, 16> EltTys; 932b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 933b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 934b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (CXXDecl) { 935b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); 936b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch CollectVTableInfo(CXXDecl, Unit, EltTys); 937b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch } 938014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 939014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch // Collect static variables with initializers. 940b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end(); 941b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch I != E; ++I) 942b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (const VarDecl *V = dyn_cast<VarDecl>(*I)) { 943b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (const Expr *Init = V->getInit()) { 944b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch Expr::EvalResult Result; 945b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) { 946b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch llvm::ConstantInt *CI 947b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt()); 948b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch 949 // Create the descriptor for static variable. 950 llvm::DIFile VUnit = getOrCreateFile(V->getLocation()); 951 llvm::StringRef VName = V->getName(); 952 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit); 953 // Do not use DIGlobalVariable for enums. 954 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) { 955 DBuilder.CreateStaticVariable(FwdDecl, VName, VName, VUnit, 956 getLineNumber(V->getLocation()), 957 VTy, true, CI); 958 } 959 } 960 } 961 } 962 963 CollectRecordFields(RD, Unit, EltTys); 964 llvm::SmallVector<llvm::Value *, 16> TemplateParams; 965 if (CXXDecl) { 966 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); 967 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl); 968 if (ClassTemplateSpecializationDecl *TSpecial 969 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 970 const TemplateArgumentList &TAL = TSpecial->getTemplateArgs(); 971 for (unsigned i = 0, e = TAL.size(); i != e; ++i) { 972 const TemplateArgument &TA = TAL[i]; 973 if (TA.getKind() == TemplateArgument::Type) { 974 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit); 975 llvm::DITemplateTypeParameter TTP = 976 DBuilder.CreateTemplateTypeParameter(TheCU, TTy.getName(), TTy); 977 TemplateParams.push_back(TTP); 978 } else if (TA.getKind() == TemplateArgument::Integral) { 979 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit); 980 // FIXME: Get parameter name, instead of parameter type name. 981 llvm::DITemplateValueParameter TVP = 982 DBuilder.CreateTemplateValueParameter(TheCU, TTy.getName(), TTy, 983 TA.getAsIntegral()->getZExtValue()); 984 TemplateParams.push_back(TVP); 985 } 986 } 987 } 988 } 989 990 RegionStack.pop_back(); 991 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = 992 RegionMap.find(Ty->getDecl()); 993 if (RI != RegionMap.end()) 994 RegionMap.erase(RI); 995 996 llvm::DIDescriptor RDContext = 997 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext())); 998 llvm::StringRef RDName = RD->getName(); 999 uint64_t Size = CGM.getContext().getTypeSize(Ty); 1000 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 1001 llvm::DIArray Elements = 1002 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); 1003 llvm::MDNode *RealDecl = NULL; 1004 1005 if (RD->isStruct()) 1006 RealDecl = DBuilder.CreateStructType(RDContext, RDName, DefUnit, Line, 1007 Size, Align, 0, Elements); 1008 else if (RD->isUnion()) 1009 RealDecl = DBuilder.CreateUnionType(RDContext, RDName, DefUnit, Line, 1010 Size, Align, 0, Elements); 1011 else { 1012 assert(RD->isClass() && "Unknown RecordType!"); 1013 RDName = getClassName(RD); 1014 // A class's primary base or the class itself contains the vtable. 1015 llvm::MDNode *ContainingType = NULL; 1016 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 1017 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { 1018 // Seek non virtual primary base root. 1019 while (1) { 1020 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); 1021 const CXXRecordDecl *PBT = BRL.getPrimaryBase(); 1022 if (PBT && !BRL.isPrimaryBaseVirtual()) 1023 PBase = PBT; 1024 else 1025 break; 1026 } 1027 ContainingType = 1028 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit); 1029 } 1030 else if (CXXDecl->isDynamicClass()) 1031 ContainingType = FwdDecl; 1032 llvm::DIArray TParamsArray = 1033 DBuilder.GetOrCreateArray(TemplateParams.data(), TemplateParams.size()); 1034 RealDecl = DBuilder.CreateClassType(RDContext, RDName, DefUnit, Line, 1035 Size, Align, 0, 0, llvm::DIType(), 1036 Elements, ContainingType, 1037 TParamsArray); 1038 } 1039 1040 // Now that we have a real decl for the struct, replace anything using the 1041 // old decl with the new one. This will recursively update the debug info. 1042 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); 1043 RegionMap[RD] = llvm::WeakVH(RealDecl); 1044 return llvm::DIType(RealDecl); 1045} 1046 1047/// CreateType - get objective-c object type. 1048llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty, 1049 llvm::DIFile Unit) { 1050 // Ignore protocols. 1051 return getOrCreateType(Ty->getBaseType(), Unit); 1052} 1053 1054/// CreateType - get objective-c interface type. 1055llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 1056 llvm::DIFile Unit) { 1057 ObjCInterfaceDecl *ID = Ty->getDecl(); 1058 if (!ID) 1059 return llvm::DIType(); 1060 1061 // Get overall information about the record type for the debug info. 1062 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); 1063 unsigned Line = getLineNumber(ID->getLocation()); 1064 unsigned RuntimeLang = TheCU.getLanguage(); 1065 1066 // If this is just a forward declaration, return a special forward-declaration 1067 // debug type. 1068 if (ID->isForwardDecl()) { 1069 llvm::DIType FwdDecl = 1070 DBuilder.CreateStructType(Unit, ID->getName(), 1071 DefUnit, Line, 0, 0, 0, 1072 llvm::DIArray(), RuntimeLang); 1073 return FwdDecl; 1074 } 1075 1076 // To handle recursive interface, we 1077 // first generate a debug descriptor for the struct as a forward declaration. 1078 // Then (if it is a definition) we go through and get debug info for all of 1079 // its members. Finally, we create a descriptor for the complete type (which 1080 // may refer to the forward decl if the struct is recursive) and replace all 1081 // uses of the forward declaration with the final definition. 1082 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit); 1083 1084 llvm::MDNode *MN = FwdDecl; 1085 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN; 1086 // Otherwise, insert it into the TypeCache so that recursive uses will find 1087 // it. 1088 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 1089 // Push the struct on region stack. 1090 RegionStack.push_back(FwdDeclNode); 1091 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); 1092 1093 // Convert all the elements. 1094 llvm::SmallVector<llvm::Value *, 16> EltTys; 1095 1096 ObjCInterfaceDecl *SClass = ID->getSuperClass(); 1097 if (SClass) { 1098 llvm::DIType SClassTy = 1099 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); 1100 if (!SClassTy.isValid()) 1101 return llvm::DIType(); 1102 1103 llvm::DIType InhTag = 1104 DBuilder.CreateInheritance(FwdDecl, SClassTy, 0, 0); 1105 EltTys.push_back(InhTag); 1106 } 1107 1108 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); 1109 1110 unsigned FieldNo = 0; 1111 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; 1112 Field = Field->getNextIvar(), ++FieldNo) { 1113 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 1114 if (!FieldTy.isValid()) 1115 return llvm::DIType(); 1116 1117 llvm::StringRef FieldName = Field->getName(); 1118 1119 // Ignore unnamed fields. 1120 if (FieldName.empty()) 1121 continue; 1122 1123 // Get the location for the field. 1124 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); 1125 unsigned FieldLine = getLineNumber(Field->getLocation()); 1126 QualType FType = Field->getType(); 1127 uint64_t FieldSize = 0; 1128 unsigned FieldAlign = 0; 1129 1130 if (!FType->isIncompleteArrayType()) { 1131 1132 // Bit size, align and offset of the type. 1133 FieldSize = CGM.getContext().getTypeSize(FType); 1134 Expr *BitWidth = Field->getBitWidth(); 1135 if (BitWidth) 1136 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); 1137 1138 FieldAlign = CGM.getContext().getTypeAlign(FType); 1139 } 1140 1141 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 1142 1143 unsigned Flags = 0; 1144 if (Field->getAccessControl() == ObjCIvarDecl::Protected) 1145 Flags = llvm::DIDescriptor::FlagProtected; 1146 else if (Field->getAccessControl() == ObjCIvarDecl::Private) 1147 Flags = llvm::DIDescriptor::FlagPrivate; 1148 1149 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit, 1150 FieldLine, FieldSize, FieldAlign, 1151 FieldOffset, Flags, FieldTy); 1152 EltTys.push_back(FieldTy); 1153 } 1154 1155 llvm::DIArray Elements = 1156 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); 1157 1158 RegionStack.pop_back(); 1159 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = 1160 RegionMap.find(Ty->getDecl()); 1161 if (RI != RegionMap.end()) 1162 RegionMap.erase(RI); 1163 1164 // Bit size, align and offset of the type. 1165 uint64_t Size = CGM.getContext().getTypeSize(Ty); 1166 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 1167 1168 llvm::DIType RealDecl = 1169 DBuilder.CreateStructType(Unit, ID->getName(), DefUnit, 1170 Line, Size, Align, 0, 1171 Elements, RuntimeLang); 1172 1173 // Now that we have a real decl for the struct, replace anything using the 1174 // old decl with the new one. This will recursively update the debug info. 1175 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl); 1176 RegionMap[ID] = llvm::WeakVH(RealDecl); 1177 1178 return RealDecl; 1179} 1180 1181llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) { 1182 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 1183 return CreateType(RT); 1184 else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) 1185 return CreateEnumType(ET->getDecl()); 1186 1187 return llvm::DIType(); 1188} 1189 1190llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, 1191 llvm::DIFile Unit) { 1192 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); 1193 uint64_t NumElems = Ty->getNumElements(); 1194 if (NumElems > 0) 1195 --NumElems; 1196 1197 llvm::Value *Subscript = DBuilder.GetOrCreateSubrange(0, NumElems); 1198 llvm::DIArray SubscriptArray = DBuilder.GetOrCreateArray(&Subscript, 1); 1199 1200 uint64_t Size = CGM.getContext().getTypeSize(Ty); 1201 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 1202 1203 return 1204 DBuilder.CreateVectorType(Size, Align, ElementTy, SubscriptArray); 1205} 1206 1207llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 1208 llvm::DIFile Unit) { 1209 uint64_t Size; 1210 uint64_t Align; 1211 1212 1213 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 1214 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 1215 Size = 0; 1216 Align = 1217 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); 1218 } else if (Ty->isIncompleteArrayType()) { 1219 Size = 0; 1220 Align = CGM.getContext().getTypeAlign(Ty->getElementType()); 1221 } else { 1222 // Size and align of the whole array, not the element type. 1223 Size = CGM.getContext().getTypeSize(Ty); 1224 Align = CGM.getContext().getTypeAlign(Ty); 1225 } 1226 1227 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 1228 // interior arrays, do we care? Why aren't nested arrays represented the 1229 // obvious/recursive way? 1230 llvm::SmallVector<llvm::Value *, 8> Subscripts; 1231 QualType EltTy(Ty, 0); 1232 if (Ty->isIncompleteArrayType()) 1233 EltTy = Ty->getElementType(); 1234 else { 1235 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 1236 uint64_t Upper = 0; 1237 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 1238 if (CAT->getSize().getZExtValue()) 1239 Upper = CAT->getSize().getZExtValue() - 1; 1240 // FIXME: Verify this is right for VLAs. 1241 Subscripts.push_back(DBuilder.GetOrCreateSubrange(0, Upper)); 1242 EltTy = Ty->getElementType(); 1243 } 1244 } 1245 1246 llvm::DIArray SubscriptArray = 1247 DBuilder.GetOrCreateArray(Subscripts.data(), Subscripts.size()); 1248 1249 llvm::DIType DbgTy = 1250 DBuilder.CreateArrayType(Size, Align, getOrCreateType(EltTy, Unit), 1251 SubscriptArray); 1252 return DbgTy; 1253} 1254 1255llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, 1256 llvm::DIFile Unit) { 1257 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, 1258 Ty, Ty->getPointeeType(), Unit); 1259} 1260 1261llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty, 1262 llvm::DIFile Unit) { 1263 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, 1264 Ty, Ty->getPointeeType(), Unit); 1265} 1266 1267llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, 1268 llvm::DIFile U) { 1269 QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); 1270 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); 1271 1272 if (!Ty->getPointeeType()->isFunctionType()) { 1273 // We have a data member pointer type. 1274 return PointerDiffDITy; 1275 } 1276 1277 // We have a member function pointer type. Treat it as a struct with two 1278 // ptrdiff_t members. 1279 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); 1280 1281 uint64_t FieldOffset = 0; 1282 llvm::Value *ElementTypes[2]; 1283 1284 // FIXME: This should probably be a function type instead. 1285 ElementTypes[0] = 1286 DBuilder.CreateMemberType("ptr", U, 0, 1287 Info.first, Info.second, FieldOffset, 0, 1288 PointerDiffDITy); 1289 FieldOffset += Info.first; 1290 1291 ElementTypes[1] = 1292 DBuilder.CreateMemberType("ptr", U, 0, 1293 Info.first, Info.second, FieldOffset, 0, 1294 PointerDiffDITy); 1295 1296 llvm::DIArray Elements = 1297 DBuilder.GetOrCreateArray(&ElementTypes[0], 1298 llvm::array_lengthof(ElementTypes)); 1299 1300 return DBuilder.CreateStructType(U, llvm::StringRef("test"), 1301 U, 0, FieldOffset, 1302 0, 0, Elements); 1303} 1304 1305/// CreateEnumType - get enumeration type. 1306llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) { 1307 llvm::DIFile Unit = getOrCreateFile(ED->getLocation()); 1308 llvm::SmallVector<llvm::Value *, 16> Enumerators; 1309 1310 // Create DIEnumerator elements for each enumerator. 1311 for (EnumDecl::enumerator_iterator 1312 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); 1313 Enum != EnumEnd; ++Enum) { 1314 Enumerators.push_back( 1315 DBuilder.CreateEnumerator(Enum->getName(), 1316 Enum->getInitVal().getZExtValue())); 1317 } 1318 1319 // Return a CompositeType for the enum itself. 1320 llvm::DIArray EltArray = 1321 DBuilder.GetOrCreateArray(Enumerators.data(), Enumerators.size()); 1322 1323 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); 1324 unsigned Line = getLineNumber(ED->getLocation()); 1325 uint64_t Size = 0; 1326 uint64_t Align = 0; 1327 if (!ED->getTypeForDecl()->isIncompleteType()) { 1328 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); 1329 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl()); 1330 } 1331 llvm::DIDescriptor EnumContext = 1332 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext())); 1333 llvm::DIType DbgTy = 1334 DBuilder.CreateEnumerationType(EnumContext, ED->getName(), DefUnit, Line, 1335 Size, Align, EltArray); 1336 return DbgTy; 1337} 1338 1339static QualType UnwrapTypeForDebugInfo(QualType T) { 1340 do { 1341 QualType LastT = T; 1342 switch (T->getTypeClass()) { 1343 default: 1344 return T; 1345 case Type::TemplateSpecialization: 1346 T = cast<TemplateSpecializationType>(T)->desugar(); 1347 break; 1348 case Type::TypeOfExpr: 1349 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); 1350 break; 1351 case Type::TypeOf: 1352 T = cast<TypeOfType>(T)->getUnderlyingType(); 1353 break; 1354 case Type::Decltype: 1355 T = cast<DecltypeType>(T)->getUnderlyingType(); 1356 break; 1357 case Type::Attributed: 1358 T = cast<AttributedType>(T)->getEquivalentType(); 1359 case Type::Elaborated: 1360 T = cast<ElaboratedType>(T)->getNamedType(); 1361 break; 1362 case Type::Paren: 1363 T = cast<ParenType>(T)->getInnerType(); 1364 break; 1365 case Type::SubstTemplateTypeParm: 1366 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); 1367 break; 1368 } 1369 1370 assert(T != LastT && "Type unwrapping failed to unwrap!"); 1371 if (T == LastT) 1372 return T; 1373 } while (true); 1374 1375 return T; 1376} 1377 1378/// getOrCreateType - Get the type from the cache or create a new 1379/// one if necessary. 1380llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, 1381 llvm::DIFile Unit) { 1382 if (Ty.isNull()) 1383 return llvm::DIType(); 1384 1385 // Unwrap the type as needed for debug information. 1386 Ty = UnwrapTypeForDebugInfo(Ty); 1387 1388 // Check for existing entry. 1389 llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 1390 TypeCache.find(Ty.getAsOpaquePtr()); 1391 if (it != TypeCache.end()) { 1392 // Verify that the debug info still exists. 1393 if (&*it->second) 1394 return llvm::DIType(cast<llvm::MDNode>(it->second)); 1395 } 1396 1397 // Otherwise create the type. 1398 llvm::DIType Res = CreateTypeNode(Ty, Unit); 1399 1400 // And update the type cache. 1401 TypeCache[Ty.getAsOpaquePtr()] = Res; 1402 return Res; 1403} 1404 1405/// CreateTypeNode - Create a new debug type node. 1406llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, 1407 llvm::DIFile Unit) { 1408 // Handle qualifiers, which recursively handles what they refer to. 1409 if (Ty.hasLocalQualifiers()) 1410 return CreateQualifiedType(Ty, Unit); 1411 1412 const char *Diag = 0; 1413 1414 // Work out details of type. 1415 switch (Ty->getTypeClass()) { 1416#define TYPE(Class, Base) 1417#define ABSTRACT_TYPE(Class, Base) 1418#define NON_CANONICAL_TYPE(Class, Base) 1419#define DEPENDENT_TYPE(Class, Base) case Type::Class: 1420#include "clang/AST/TypeNodes.def" 1421 assert(false && "Dependent types cannot show up in debug information"); 1422 1423 // FIXME: Handle these. 1424 case Type::ExtVector: 1425 return llvm::DIType(); 1426 1427 case Type::Vector: 1428 return CreateType(cast<VectorType>(Ty), Unit); 1429 case Type::ObjCObjectPointer: 1430 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); 1431 case Type::ObjCObject: 1432 return CreateType(cast<ObjCObjectType>(Ty), Unit); 1433 case Type::ObjCInterface: 1434 return CreateType(cast<ObjCInterfaceType>(Ty), Unit); 1435 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty)); 1436 case Type::Complex: return CreateType(cast<ComplexType>(Ty)); 1437 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); 1438 case Type::BlockPointer: 1439 return CreateType(cast<BlockPointerType>(Ty), Unit); 1440 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); 1441 case Type::Record: 1442 case Type::Enum: 1443 return CreateType(cast<TagType>(Ty)); 1444 case Type::FunctionProto: 1445 case Type::FunctionNoProto: 1446 return CreateType(cast<FunctionType>(Ty), Unit); 1447 case Type::ConstantArray: 1448 case Type::VariableArray: 1449 case Type::IncompleteArray: 1450 return CreateType(cast<ArrayType>(Ty), Unit); 1451 1452 case Type::LValueReference: 1453 return CreateType(cast<LValueReferenceType>(Ty), Unit); 1454 case Type::RValueReference: 1455 return CreateType(cast<RValueReferenceType>(Ty), Unit); 1456 1457 case Type::MemberPointer: 1458 return CreateType(cast<MemberPointerType>(Ty), Unit); 1459 1460 case Type::Attributed: 1461 case Type::TemplateSpecialization: 1462 case Type::Elaborated: 1463 case Type::Paren: 1464 case Type::SubstTemplateTypeParm: 1465 case Type::TypeOfExpr: 1466 case Type::TypeOf: 1467 case Type::Decltype: 1468 llvm_unreachable("type should have been unwrapped!"); 1469 return llvm::DIType(); 1470 } 1471 1472 assert(Diag && "Fall through without a diagnostic?"); 1473 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error, 1474 "debug information for %0 is not yet supported"); 1475 CGM.getDiags().Report(DiagID) 1476 << Diag; 1477 return llvm::DIType(); 1478} 1479 1480/// CreateMemberType - Create new member and increase Offset by FType's size. 1481llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, 1482 llvm::StringRef Name, 1483 uint64_t *Offset) { 1484 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1485 uint64_t FieldSize = CGM.getContext().getTypeSize(FType); 1486 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); 1487 llvm::DIType Ty = DBuilder.CreateMemberType(Name, Unit, 0, 1488 FieldSize, FieldAlign, 1489 *Offset, 0, FieldTy); 1490 *Offset += FieldSize; 1491 return Ty; 1492} 1493 1494/// EmitFunctionStart - Constructs the debug code for entering a function - 1495/// "llvm.dbg.func.start.". 1496void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, 1497 llvm::Function *Fn, 1498 CGBuilderTy &Builder) { 1499 1500 llvm::StringRef Name; 1501 llvm::StringRef LinkageName; 1502 1503 FnBeginRegionCount.push_back(RegionStack.size()); 1504 1505 const Decl *D = GD.getDecl(); 1506 unsigned Flags = 0; 1507 llvm::DIFile Unit = getOrCreateFile(CurLoc); 1508 llvm::DIDescriptor FDContext(Unit); 1509 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1510 // If there is a DISubprogram for this function available then use it. 1511 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 1512 FI = SPCache.find(FD); 1513 if (FI != SPCache.end()) { 1514 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second)); 1515 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { 1516 llvm::MDNode *SPN = SP; 1517 RegionStack.push_back(SPN); 1518 RegionMap[D] = llvm::WeakVH(SP); 1519 return; 1520 } 1521 } 1522 Name = getFunctionName(FD); 1523 // Use mangled name as linkage name for c/c++ functions. 1524 LinkageName = CGM.getMangledName(GD); 1525 if (LinkageName == Name) 1526 LinkageName = llvm::StringRef(); 1527 if (FD->hasPrototype()) 1528 Flags |= llvm::DIDescriptor::FlagPrototyped; 1529 if (const NamespaceDecl *NSDecl = 1530 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) 1531 FDContext = getOrCreateNameSpace(NSDecl); 1532 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { 1533 Name = getObjCMethodName(OMD); 1534 Flags |= llvm::DIDescriptor::FlagPrototyped; 1535 } else { 1536 // Use llvm function name. 1537 Name = Fn->getName(); 1538 Flags |= llvm::DIDescriptor::FlagPrototyped; 1539 } 1540 if (!Name.empty() && Name[0] == '\01') 1541 Name = Name.substr(1); 1542 1543 // It is expected that CurLoc is set before using EmitFunctionStart. 1544 // Usually, CurLoc points to the left bracket location of compound 1545 // statement representing function body. 1546 unsigned LineNo = getLineNumber(CurLoc); 1547 if (D->isImplicit()) 1548 Flags |= llvm::DIDescriptor::FlagArtificial; 1549 llvm::DISubprogram SP = 1550 DBuilder.CreateFunction(FDContext, Name, LinkageName, Unit, 1551 LineNo, getOrCreateType(FnType, Unit), 1552 Fn->hasInternalLinkage(), true/*definition*/, 1553 Flags, CGM.getLangOptions().Optimize, Fn); 1554 1555 // Push function on region stack. 1556 llvm::MDNode *SPN = SP; 1557 RegionStack.push_back(SPN); 1558 RegionMap[D] = llvm::WeakVH(SP); 1559 1560 // Clear stack used to keep track of #line directives. 1561 LineDirectiveFiles.clear(); 1562} 1563 1564 1565void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) { 1566 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 1567 1568 // Don't bother if things are the same as last time. 1569 SourceManager &SM = CGM.getContext().getSourceManager(); 1570 if (CurLoc == PrevLoc 1571 || (SM.getInstantiationLineNumber(CurLoc) == 1572 SM.getInstantiationLineNumber(PrevLoc) 1573 && SM.isFromSameFile(CurLoc, PrevLoc))) 1574 // New Builder may not be in sync with CGDebugInfo. 1575 if (!Builder.getCurrentDebugLocation().isUnknown()) 1576 return; 1577 1578 // Update last state. 1579 PrevLoc = CurLoc; 1580 1581 llvm::MDNode *Scope = RegionStack.back(); 1582 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc), 1583 getColumnNumber(CurLoc), 1584 Scope)); 1585} 1586 1587/// UpdateLineDirectiveRegion - Update region stack only if #line directive 1588/// has introduced scope change. 1589void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) { 1590 if (CurLoc.isInvalid() || CurLoc.isMacroID() || 1591 PrevLoc.isInvalid() || PrevLoc.isMacroID()) 1592 return; 1593 SourceManager &SM = CGM.getContext().getSourceManager(); 1594 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); 1595 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc); 1596 1597 if (PCLoc.isInvalid() || PPLoc.isInvalid() || 1598 !strcmp(PPLoc.getFilename(), PCLoc.getFilename())) 1599 return; 1600 1601 // If #line directive stack is empty then we are entering a new scope. 1602 if (LineDirectiveFiles.empty()) { 1603 EmitRegionStart(Builder); 1604 LineDirectiveFiles.push_back(PCLoc.getFilename()); 1605 return; 1606 } 1607 1608 assert (RegionStack.size() >= LineDirectiveFiles.size() 1609 && "error handling #line regions!"); 1610 1611 bool SeenThisFile = false; 1612 // Chek if current file is already seen earlier. 1613 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(), 1614 E = LineDirectiveFiles.end(); I != E; ++I) 1615 if (!strcmp(PCLoc.getFilename(), *I)) { 1616 SeenThisFile = true; 1617 break; 1618 } 1619 1620 // If #line for this file is seen earlier then pop out #line regions. 1621 if (SeenThisFile) { 1622 while (!LineDirectiveFiles.empty()) { 1623 const char *LastFile = LineDirectiveFiles.back(); 1624 RegionStack.pop_back(); 1625 LineDirectiveFiles.pop_back(); 1626 if (!strcmp(PPLoc.getFilename(), LastFile)) 1627 break; 1628 } 1629 return; 1630 } 1631 1632 // .. otherwise insert new #line region. 1633 EmitRegionStart(Builder); 1634 LineDirectiveFiles.push_back(PCLoc.getFilename()); 1635 1636 return; 1637} 1638/// EmitRegionStart- Constructs the debug code for entering a declarative 1639/// region - "llvm.dbg.region.start.". 1640void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) { 1641 llvm::DIDescriptor D = 1642 DBuilder.CreateLexicalBlock(RegionStack.empty() ? 1643 llvm::DIDescriptor() : 1644 llvm::DIDescriptor(RegionStack.back()), 1645 getOrCreateFile(CurLoc), 1646 getLineNumber(CurLoc), 1647 getColumnNumber(CurLoc)); 1648 llvm::MDNode *DN = D; 1649 RegionStack.push_back(DN); 1650} 1651 1652/// EmitRegionEnd - Constructs the debug code for exiting a declarative 1653/// region - "llvm.dbg.region.end." 1654void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) { 1655 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1656 1657 // Provide an region stop point. 1658 EmitStopPoint(Builder); 1659 1660 RegionStack.pop_back(); 1661} 1662 1663/// EmitFunctionEnd - Constructs the debug code for exiting a function. 1664void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { 1665 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1666 unsigned RCount = FnBeginRegionCount.back(); 1667 assert(RCount <= RegionStack.size() && "Region stack mismatch"); 1668 1669 // Pop all regions for this function. 1670 while (RegionStack.size() != RCount) 1671 EmitRegionEnd(Builder); 1672 FnBeginRegionCount.pop_back(); 1673} 1674 1675// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 1676// See BuildByRefType. 1677llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, 1678 uint64_t *XOffset) { 1679 1680 llvm::SmallVector<llvm::Value *, 5> EltTys; 1681 QualType FType; 1682 uint64_t FieldSize, FieldOffset; 1683 unsigned FieldAlign; 1684 1685 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1686 QualType Type = VD->getType(); 1687 1688 FieldOffset = 0; 1689 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1690 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 1691 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); 1692 FType = CGM.getContext().IntTy; 1693 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 1694 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); 1695 1696 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type); 1697 if (HasCopyAndDispose) { 1698 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1699 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", 1700 &FieldOffset)); 1701 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", 1702 &FieldOffset)); 1703 } 1704 1705 CharUnits Align = CGM.getContext().getDeclAlign(VD); 1706 if (Align > CharUnits::fromQuantity( 1707 CGM.getContext().Target.getPointerAlign(0) / 8)) { 1708 unsigned AlignedOffsetInBytes 1709 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity()); 1710 unsigned NumPaddingBytes 1711 = AlignedOffsetInBytes - FieldOffset/8; 1712 1713 if (NumPaddingBytes > 0) { 1714 llvm::APInt pad(32, NumPaddingBytes); 1715 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, 1716 pad, ArrayType::Normal, 0); 1717 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); 1718 } 1719 } 1720 1721 FType = Type; 1722 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1723 FieldSize = CGM.getContext().getTypeSize(FType); 1724 FieldAlign = Align.getQuantity()*8; 1725 1726 *XOffset = FieldOffset; 1727 FieldTy = DBuilder.CreateMemberType(VD->getName(), Unit, 1728 0, FieldSize, FieldAlign, 1729 FieldOffset, 0, FieldTy); 1730 EltTys.push_back(FieldTy); 1731 FieldOffset += FieldSize; 1732 1733 llvm::DIArray Elements = 1734 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size()); 1735 1736 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct; 1737 1738 return DBuilder.CreateStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, 1739 Elements); 1740} 1741 1742/// EmitDeclare - Emit local variable declaration debug info. 1743void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, 1744 llvm::Value *Storage, CGBuilderTy &Builder, 1745 bool IndirectArgument) { 1746 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1747 1748 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1749 llvm::DIType Ty; 1750 uint64_t XOffset = 0; 1751 if (VD->hasAttr<BlocksAttr>()) 1752 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1753 else 1754 Ty = getOrCreateType(VD->getType(), Unit); 1755 1756 // If there is not any debug info for type then do not emit debug info 1757 // for this variable. 1758 if (!Ty) 1759 return; 1760 1761 // If an aggregate variable has non trivial destructor or non trivial copy 1762 // constructor than it is pass indirectly. Let debug info know about this 1763 // by using reference of the aggregate type as a argument type. 1764 if (IndirectArgument && VD->getType()->isRecordType()) 1765 Ty = DBuilder.CreateReferenceType(Ty); 1766 1767 // If Storage is an aggregate returned as 'sret' then let debugger know 1768 // about this. 1769 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) 1770 if (Arg->hasStructRetAttr()) 1771 Ty = DBuilder.CreateReferenceType(Ty); 1772 1773 // Get location information. 1774 unsigned Line = getLineNumber(VD->getLocation()); 1775 unsigned Column = getColumnNumber(VD->getLocation()); 1776 unsigned Flags = 0; 1777 if (VD->isImplicit()) 1778 Flags |= llvm::DIDescriptor::FlagArtificial; 1779 llvm::MDNode *Scope = RegionStack.back(); 1780 1781 llvm::StringRef Name = VD->getName(); 1782 if (!Name.empty()) { 1783 if (VD->hasAttr<BlocksAttr>()) { 1784 CharUnits offset = CharUnits::fromQuantity(32); 1785 llvm::SmallVector<llvm::Value *, 9> addr; 1786 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); 1787 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1788 // offset of __forwarding field 1789 offset = 1790 CharUnits::fromQuantity(CGM.getContext().Target.getPointerWidth(0)/8); 1791 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1792 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1793 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1794 // offset of x field 1795 offset = CharUnits::fromQuantity(XOffset/8); 1796 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1797 1798 // Create the descriptor for the variable. 1799 llvm::DIVariable D = 1800 DBuilder.CreateComplexVariable(Tag, 1801 llvm::DIDescriptor(RegionStack.back()), 1802 VD->getName(), Unit, Line, Ty, 1803 addr.data(), addr.size()); 1804 1805 // Insert an llvm.dbg.declare into the current block. 1806 llvm::Instruction *Call = 1807 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1808 1809 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 1810 return; 1811 } 1812 // Create the descriptor for the variable. 1813 llvm::DIVariable D = 1814 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope), 1815 Name, Unit, Line, Ty, 1816 CGM.getLangOptions().Optimize, Flags); 1817 1818 // Insert an llvm.dbg.declare into the current block. 1819 llvm::Instruction *Call = 1820 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1821 1822 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 1823 return; 1824 } 1825 1826 // If VD is an anonymous union then Storage represents value for 1827 // all union fields. 1828 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) 1829 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl())) 1830 if (RD->isUnion()) { 1831 for (RecordDecl::field_iterator I = RD->field_begin(), 1832 E = RD->field_end(); 1833 I != E; ++I) { 1834 FieldDecl *Field = *I; 1835 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 1836 llvm::StringRef FieldName = Field->getName(); 1837 1838 // Ignore unnamed fields. Do not ignore unnamed records. 1839 if (FieldName.empty() && !isa<RecordType>(Field->getType())) 1840 continue; 1841 1842 // Use VarDecl's Tag, Scope and Line number. 1843 llvm::DIVariable D = 1844 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope), 1845 FieldName, Unit, Line, FieldTy, 1846 CGM.getLangOptions().Optimize, Flags); 1847 1848 // Insert an llvm.dbg.declare into the current block. 1849 llvm::Instruction *Call = 1850 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1851 1852 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 1853 } 1854 } 1855} 1856 1857/// EmitDeclare - Emit local variable declaration debug info. 1858void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, 1859 llvm::Value *Storage, CGBuilderTy &Builder, 1860 const CGBlockInfo &blockInfo) { 1861 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1862 1863 if (Builder.GetInsertBlock() == 0) 1864 return; 1865 1866 bool isByRef = VD->hasAttr<BlocksAttr>(); 1867 1868 uint64_t XOffset = 0; 1869 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1870 llvm::DIType Ty; 1871 if (isByRef) 1872 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1873 else 1874 Ty = getOrCreateType(VD->getType(), Unit); 1875 1876 // Get location information. 1877 unsigned Line = getLineNumber(VD->getLocation()); 1878 unsigned Column = getColumnNumber(VD->getLocation()); 1879 1880 const llvm::TargetData &target = CGM.getTargetData(); 1881 1882 CharUnits offset = CharUnits::fromQuantity( 1883 target.getStructLayout(blockInfo.StructureType) 1884 ->getElementOffset(blockInfo.getCapture(VD).getIndex())); 1885 1886 llvm::SmallVector<llvm::Value *, 9> addr; 1887 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); 1888 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1889 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1890 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1891 if (isByRef) { 1892 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1893 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1894 // offset of __forwarding field 1895 offset = CharUnits::fromQuantity(target.getPointerSize()/8); 1896 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1897 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1898 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1899 // offset of x field 1900 offset = CharUnits::fromQuantity(XOffset/8); 1901 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1902 } 1903 1904 // Create the descriptor for the variable. 1905 llvm::DIVariable D = 1906 DBuilder.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()), 1907 VD->getName(), Unit, Line, Ty, 1908 addr.data(), addr.size()); 1909 // Insert an llvm.dbg.declare into the current block. 1910 llvm::Instruction *Call = 1911 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1912 1913 llvm::MDNode *Scope = RegionStack.back(); 1914 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 1915} 1916 1917void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, 1918 llvm::Value *Storage, 1919 CGBuilderTy &Builder) { 1920 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); 1921} 1922 1923void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( 1924 const VarDecl *variable, llvm::Value *Storage, CGBuilderTy &Builder, 1925 const CGBlockInfo &blockInfo) { 1926 EmitDeclare(variable, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, 1927 blockInfo); 1928} 1929 1930/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 1931/// variable declaration. 1932void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, 1933 CGBuilderTy &Builder, 1934 bool IndirectArgument) { 1935 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder, 1936 IndirectArgument); 1937} 1938 1939 1940 1941/// EmitGlobalVariable - Emit information about a global variable. 1942void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1943 const VarDecl *D) { 1944 1945 // Create global variable debug descriptor. 1946 llvm::DIFile Unit = getOrCreateFile(D->getLocation()); 1947 unsigned LineNo = getLineNumber(D->getLocation()); 1948 1949 QualType T = D->getType(); 1950 if (T->isIncompleteArrayType()) { 1951 1952 // CodeGen turns int[] into int[1] so we'll do the same here. 1953 llvm::APSInt ConstVal(32); 1954 1955 ConstVal = 1; 1956 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1957 1958 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1959 ArrayType::Normal, 0); 1960 } 1961 llvm::StringRef DeclName = D->getName(); 1962 llvm::StringRef LinkageName; 1963 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()) 1964 && !isa<ObjCMethodDecl>(D->getDeclContext())) 1965 LinkageName = Var->getName(); 1966 if (LinkageName == DeclName) 1967 LinkageName = llvm::StringRef(); 1968 llvm::DIDescriptor DContext = 1969 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext())); 1970 DBuilder.CreateStaticVariable(DContext, DeclName, LinkageName, 1971 Unit, LineNo, getOrCreateType(T, Unit), 1972 Var->hasInternalLinkage(), Var); 1973} 1974 1975/// EmitGlobalVariable - Emit information about an objective-c interface. 1976void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1977 ObjCInterfaceDecl *ID) { 1978 // Create global variable debug descriptor. 1979 llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); 1980 unsigned LineNo = getLineNumber(ID->getLocation()); 1981 1982 llvm::StringRef Name = ID->getName(); 1983 1984 QualType T = CGM.getContext().getObjCInterfaceType(ID); 1985 if (T->isIncompleteArrayType()) { 1986 1987 // CodeGen turns int[] into int[1] so we'll do the same here. 1988 llvm::APSInt ConstVal(32); 1989 1990 ConstVal = 1; 1991 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1992 1993 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1994 ArrayType::Normal, 0); 1995 } 1996 1997 DBuilder.CreateGlobalVariable(Name, Unit, LineNo, 1998 getOrCreateType(T, Unit), 1999 Var->hasInternalLinkage(), Var); 2000} 2001 2002/// EmitGlobalVariable - Emit global variable's debug info. 2003void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, 2004 llvm::Constant *Init) { 2005 // Create the descriptor for the variable. 2006 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 2007 llvm::StringRef Name = VD->getName(); 2008 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit); 2009 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) { 2010 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) 2011 Ty = CreateEnumType(ED); 2012 } 2013 // Do not use DIGlobalVariable for enums. 2014 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type) 2015 return; 2016 DBuilder.CreateStaticVariable(Unit, Name, Name, Unit, 2017 getLineNumber(VD->getLocation()), 2018 Ty, true, Init); 2019} 2020 2021/// getOrCreateNamesSpace - Return namespace descriptor for the given 2022/// namespace decl. 2023llvm::DINameSpace 2024CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) { 2025 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = 2026 NameSpaceCache.find(NSDecl); 2027 if (I != NameSpaceCache.end()) 2028 return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); 2029 2030 unsigned LineNo = getLineNumber(NSDecl->getLocation()); 2031 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation()); 2032 llvm::DIDescriptor Context = 2033 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext())); 2034 llvm::DINameSpace NS = 2035 DBuilder.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo); 2036 NameSpaceCache[NSDecl] = llvm::WeakVH(NS); 2037 return NS; 2038} 2039