1193326Sed//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This coordinates the debug information generation while generating code. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#include "CGDebugInfo.h" 15249423Sdim#include "CGBlocks.h" 16263508Sdim#include "CGCXXABI.h" 17249423Sdim#include "CGObjCRuntime.h" 18198092Srdivacky#include "CodeGenFunction.h" 19193326Sed#include "CodeGenModule.h" 20193326Sed#include "clang/AST/ASTContext.h" 21212904Sdim#include "clang/AST/DeclFriend.h" 22193326Sed#include "clang/AST/DeclObjC.h" 23212904Sdim#include "clang/AST/DeclTemplate.h" 24193326Sed#include "clang/AST/Expr.h" 25193326Sed#include "clang/AST/RecordLayout.h" 26249423Sdim#include "clang/Basic/FileManager.h" 27193326Sed#include "clang/Basic/SourceManager.h" 28198092Srdivacky#include "clang/Basic/Version.h" 29210299Sed#include "clang/Frontend/CodeGenOptions.h" 30249423Sdim#include "llvm/ADT/SmallVector.h" 31193326Sed#include "llvm/ADT/StringExtras.h" 32249423Sdim#include "llvm/IR/Constants.h" 33249423Sdim#include "llvm/IR/DataLayout.h" 34249423Sdim#include "llvm/IR/DerivedTypes.h" 35249423Sdim#include "llvm/IR/Instructions.h" 36249423Sdim#include "llvm/IR/Intrinsics.h" 37249423Sdim#include "llvm/IR/Module.h" 38193326Sed#include "llvm/Support/Dwarf.h" 39226633Sdim#include "llvm/Support/FileSystem.h" 40269000Semaste#include "llvm/Support/Path.h" 41193326Sedusing namespace clang; 42193326Sedusing namespace clang::CodeGen; 43193326Sed 44200583SrdivackyCGDebugInfo::CGDebugInfo(CodeGenModule &CGM) 45263508Sdim : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), 46263508Sdim DBuilder(CGM.getModule()) { 47204962Srdivacky CreateCompileUnit(); 48193326Sed} 49193326Sed 50193326SedCGDebugInfo::~CGDebugInfo() { 51226633Sdim assert(LexicalBlockStack.empty() && 52226633Sdim "Region stack mismatch, stack not empty!"); 53193326Sed} 54193326Sed 55263508Sdim 56263508SdimNoLocation::NoLocation(CodeGenFunction &CGF, CGBuilderTy &B) 57263508Sdim : DI(CGF.getDebugInfo()), Builder(B) { 58263508Sdim if (DI) { 59263508Sdim SavedLoc = DI->getLocation(); 60263508Sdim DI->CurLoc = SourceLocation(); 61263508Sdim Builder.SetCurrentDebugLocation(llvm::DebugLoc()); 62263508Sdim } 63263508Sdim} 64263508Sdim 65263508SdimNoLocation::~NoLocation() { 66263508Sdim if (DI) { 67263508Sdim assert(Builder.getCurrentDebugLocation().isUnknown()); 68263508Sdim DI->CurLoc = SavedLoc; 69263508Sdim } 70263508Sdim} 71263508Sdim 72263508SdimArtificialLocation::ArtificialLocation(CodeGenFunction &CGF, CGBuilderTy &B) 73263508Sdim : DI(CGF.getDebugInfo()), Builder(B) { 74263508Sdim if (DI) { 75263508Sdim SavedLoc = DI->getLocation(); 76263508Sdim DI->CurLoc = SourceLocation(); 77263508Sdim Builder.SetCurrentDebugLocation(llvm::DebugLoc()); 78263508Sdim } 79263508Sdim} 80263508Sdim 81263508Sdimvoid ArtificialLocation::Emit() { 82263508Sdim if (DI) { 83263508Sdim // Sync the Builder. 84263508Sdim DI->EmitLocation(Builder, SavedLoc); 85263508Sdim DI->CurLoc = SourceLocation(); 86263508Sdim // Construct a location that has a valid scope, but no line info. 87263508Sdim assert(!DI->LexicalBlockStack.empty()); 88263508Sdim llvm::DIDescriptor Scope(DI->LexicalBlockStack.back()); 89263508Sdim Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope)); 90263508Sdim } 91263508Sdim} 92263508Sdim 93263508SdimArtificialLocation::~ArtificialLocation() { 94263508Sdim if (DI) { 95263508Sdim assert(Builder.getCurrentDebugLocation().getLine() == 0); 96263508Sdim DI->CurLoc = SavedLoc; 97263508Sdim } 98263508Sdim} 99263508Sdim 100193326Sedvoid CGDebugInfo::setLocation(SourceLocation Loc) { 101226633Sdim // If the new location isn't valid return. 102263508Sdim if (Loc.isInvalid()) return; 103226633Sdim 104226633Sdim CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); 105226633Sdim 106226633Sdim // If we've changed files in the middle of a lexical scope go ahead 107226633Sdim // and create a new lexical scope with file node if it's different 108226633Sdim // from the one in the scope. 109226633Sdim if (LexicalBlockStack.empty()) return; 110226633Sdim 111226633Sdim SourceManager &SM = CGM.getContext().getSourceManager(); 112226633Sdim PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); 113226633Sdim PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc); 114226633Sdim 115226633Sdim if (PCLoc.isInvalid() || PPLoc.isInvalid() || 116226633Sdim !strcmp(PPLoc.getFilename(), PCLoc.getFilename())) 117226633Sdim return; 118226633Sdim 119226633Sdim llvm::MDNode *LB = LexicalBlockStack.back(); 120226633Sdim llvm::DIScope Scope = llvm::DIScope(LB); 121226633Sdim if (Scope.isLexicalBlockFile()) { 122226633Sdim llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB); 123226633Sdim llvm::DIDescriptor D 124226633Sdim = DBuilder.createLexicalBlockFile(LBF.getScope(), 125234353Sdim getOrCreateFile(CurLoc)); 126226633Sdim llvm::MDNode *N = D; 127226633Sdim LexicalBlockStack.pop_back(); 128226633Sdim LexicalBlockStack.push_back(N); 129249423Sdim } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) { 130226633Sdim llvm::DIDescriptor D 131226633Sdim = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)); 132226633Sdim llvm::MDNode *N = D; 133226633Sdim LexicalBlockStack.pop_back(); 134226633Sdim LexicalBlockStack.push_back(N); 135226633Sdim } 136193326Sed} 137193326Sed 138203955Srdivacky/// getContextDescriptor - Get context info for the decl. 139251662Sdimllvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) { 140203955Srdivacky if (!Context) 141218893Sdim return TheCU; 142203955Srdivacky 143203955Srdivacky llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator 144203955Srdivacky I = RegionMap.find(Context); 145239462Sdim if (I != RegionMap.end()) { 146239462Sdim llvm::Value *V = I->second; 147251662Sdim return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V)); 148239462Sdim } 149203955Srdivacky 150203955Srdivacky // Check namespace. 151203955Srdivacky if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) 152251662Sdim return getOrCreateNameSpace(NSDecl); 153208600Srdivacky 154251662Sdim if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) 155251662Sdim if (!RDecl->isDependentType()) 156251662Sdim return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), 157218893Sdim getOrCreateMainFile()); 158218893Sdim return TheCU; 159198092Srdivacky} 160198092Srdivacky 161202379Srdivacky/// getFunctionName - Get function name for the given FunctionDecl. If the 162263508Sdim/// name is constructed on demand (e.g. C++ destructor) then the name 163202379Srdivacky/// is stored on the side. 164226633SdimStringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { 165202379Srdivacky assert (FD && "Invalid FunctionDecl!"); 166202379Srdivacky IdentifierInfo *FII = FD->getIdentifier(); 167234353Sdim FunctionTemplateSpecializationInfo *Info 168234353Sdim = FD->getTemplateSpecializationInfo(); 169234353Sdim if (!Info && FII) 170202379Srdivacky return FII->getName(); 171202379Srdivacky 172202379Srdivacky // Otherwise construct human readable name for debug info. 173249423Sdim SmallString<128> NS; 174249423Sdim llvm::raw_svector_ostream OS(NS); 175249423Sdim FD->printName(OS); 176202379Srdivacky 177234353Sdim // Add any template specialization args. 178234353Sdim if (Info) { 179234353Sdim const TemplateArgumentList *TArgs = Info->TemplateArguments; 180234353Sdim const TemplateArgument *Args = TArgs->data(); 181234353Sdim unsigned NumArgs = TArgs->size(); 182234353Sdim PrintingPolicy Policy(CGM.getLangOpts()); 183249423Sdim TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs, 184249423Sdim Policy); 185234353Sdim } 186234353Sdim 187202379Srdivacky // Copy this name on the side and use its reference. 188263508Sdim return internString(OS.str()); 189202379Srdivacky} 190202379Srdivacky 191226633SdimStringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { 192234353Sdim SmallString<256> MethodName; 193212904Sdim llvm::raw_svector_ostream OS(MethodName); 194212904Sdim OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; 195212904Sdim const DeclContext *DC = OMD->getDeclContext(); 196263508Sdim if (const ObjCImplementationDecl *OID = 197218893Sdim dyn_cast<const ObjCImplementationDecl>(DC)) { 198212904Sdim OS << OID->getName(); 199263508Sdim } else if (const ObjCInterfaceDecl *OID = 200218893Sdim dyn_cast<const ObjCInterfaceDecl>(DC)) { 201218893Sdim OS << OID->getName(); 202263508Sdim } else if (const ObjCCategoryImplDecl *OCD = 203218893Sdim dyn_cast<const ObjCCategoryImplDecl>(DC)){ 204243830Sdim OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' << 205212904Sdim OCD->getIdentifier()->getNameStart() << ')'; 206263508Sdim } else if (isa<ObjCProtocolDecl>(DC)) { 207263508Sdim // We can extract the type of the class from the self pointer. 208263508Sdim if (ImplicitParamDecl* SelfDecl = OMD->getSelfDecl()) { 209263508Sdim QualType ClassTy = 210263508Sdim cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType(); 211263508Sdim ClassTy.print(OS, PrintingPolicy(LangOptions())); 212263508Sdim } 213212904Sdim } 214212904Sdim OS << ' ' << OMD->getSelector().getAsString() << ']'; 215212904Sdim 216263508Sdim return internString(OS.str()); 217212904Sdim} 218212904Sdim 219221345Sdim/// getSelectorName - Return selector name. This is used for debugging 220221345Sdim/// info. 221226633SdimStringRef CGDebugInfo::getSelectorName(Selector S) { 222263508Sdim return internString(S.getAsString()); 223221345Sdim} 224221345Sdim 225212904Sdim/// getClassName - Get class name including template argument list. 226263508SdimStringRef 227234353SdimCGDebugInfo::getClassName(const RecordDecl *RD) { 228234353Sdim const ClassTemplateSpecializationDecl *Spec 229212904Sdim = dyn_cast<ClassTemplateSpecializationDecl>(RD); 230212904Sdim if (!Spec) 231212904Sdim return RD->getName(); 232212904Sdim 233212904Sdim const TemplateArgument *Args; 234212904Sdim unsigned NumArgs; 235212904Sdim if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 236212904Sdim const TemplateSpecializationType *TST = 237212904Sdim cast<TemplateSpecializationType>(TAW->getType()); 238212904Sdim Args = TST->getArgs(); 239212904Sdim NumArgs = TST->getNumArgs(); 240212904Sdim } else { 241212904Sdim const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 242218893Sdim Args = TemplateArgs.data(); 243218893Sdim NumArgs = TemplateArgs.size(); 244212904Sdim } 245234982Sdim StringRef Name = RD->getIdentifier()->getName(); 246234353Sdim PrintingPolicy Policy(CGM.getLangOpts()); 247249423Sdim SmallString<128> TemplateArgList; 248249423Sdim { 249249423Sdim llvm::raw_svector_ostream OS(TemplateArgList); 250249423Sdim TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs, 251249423Sdim Policy); 252249423Sdim } 253212904Sdim 254212904Sdim // Copy this name on the side and use its reference. 255263508Sdim return internString(Name, TemplateArgList); 256212904Sdim} 257212904Sdim 258204962Srdivacky/// getOrCreateFile - Get the file debug info descriptor for the input location. 259204962Srdivackyllvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { 260206084Srdivacky if (!Loc.isValid()) 261204962Srdivacky // If Location is not valid then use main input file. 262219077Sdim return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); 263218893Sdim 264200583Srdivacky SourceManager &SM = CGM.getContext().getSourceManager(); 265204962Srdivacky PresumedLoc PLoc = SM.getPresumedLoc(Loc); 266206084Srdivacky 267226633Sdim if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty()) 268218893Sdim // If the location is not valid then use main input file. 269219077Sdim return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); 270218893Sdim 271206084Srdivacky // Cache the results. 272206084Srdivacky const char *fname = PLoc.getFilename(); 273206084Srdivacky llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = 274206084Srdivacky DIFileCache.find(fname); 275206084Srdivacky 276206084Srdivacky if (it != DIFileCache.end()) { 277206084Srdivacky // Verify that the information still exists. 278239462Sdim if (llvm::Value *V = it->second) 279239462Sdim return llvm::DIFile(cast<llvm::MDNode>(V)); 280206084Srdivacky } 281206084Srdivacky 282219077Sdim llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname()); 283203955Srdivacky 284208600Srdivacky DIFileCache[fname] = F; 285206084Srdivacky return F; 286204962Srdivacky} 287208600Srdivacky 288218893Sdim/// getOrCreateMainFile - Get the file info for main compile unit. 289218893Sdimllvm::DIFile CGDebugInfo::getOrCreateMainFile() { 290219077Sdim return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); 291218893Sdim} 292218893Sdim 293208600Srdivacky/// getLineNumber - Get line number for the location. If location is invalid 294208600Srdivacky/// then use current location. 295208600Srdivackyunsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { 296234353Sdim if (Loc.isInvalid() && CurLoc.isInvalid()) 297234353Sdim return 0; 298208600Srdivacky SourceManager &SM = CGM.getContext().getSourceManager(); 299208600Srdivacky PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); 300218893Sdim return PLoc.isValid()? PLoc.getLine() : 0; 301208600Srdivacky} 302208600Srdivacky 303243830Sdim/// getColumnNumber - Get column number for the location. 304249423Sdimunsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { 305243830Sdim // We may not want column information at all. 306249423Sdim if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo) 307243830Sdim return 0; 308243830Sdim 309243830Sdim // If the location is invalid then use the current column. 310234353Sdim if (Loc.isInvalid() && CurLoc.isInvalid()) 311234353Sdim return 0; 312208600Srdivacky SourceManager &SM = CGM.getContext().getSourceManager(); 313208600Srdivacky PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); 314218893Sdim return PLoc.isValid()? PLoc.getColumn() : 0; 315208600Srdivacky} 316208600Srdivacky 317226633SdimStringRef CGDebugInfo::getCurrentDirname() { 318234353Sdim if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) 319234353Sdim return CGM.getCodeGenOpts().DebugCompilationDir; 320234353Sdim 321212904Sdim if (!CWDName.empty()) 322212904Sdim return CWDName; 323234353Sdim SmallString<256> CWD; 324226633Sdim llvm::sys::fs::current_path(CWD); 325263508Sdim return CWDName = internString(CWD); 326212904Sdim} 327212904Sdim 328204962Srdivacky/// CreateCompileUnit - Create new compile unit. 329204962Srdivackyvoid CGDebugInfo::CreateCompileUnit() { 330198092Srdivacky 331193326Sed // Get absolute path name. 332205408Srdivacky SourceManager &SM = CGM.getContext().getSourceManager(); 333205219Srdivacky std::string MainFileName = CGM.getCodeGenOpts().MainFileName; 334205219Srdivacky if (MainFileName.empty()) 335205219Srdivacky MainFileName = "<unknown>"; 336205408Srdivacky 337206084Srdivacky // The main file name provided via the "-main-file-name" option contains just 338206084Srdivacky // the file name itself with no path information. This file name may have had 339206084Srdivacky // a relative path, so we look into the actual file entry for the main 340206084Srdivacky // file to determine the real absolute path for the file. 341205408Srdivacky std::string MainFileDir; 342212904Sdim if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 343205408Srdivacky MainFileDir = MainFile->getDir()->getName(); 344263508Sdim if (MainFileDir != ".") { 345269000Semaste llvm::SmallString<1024> MainFileDirSS(MainFileDir); 346269000Semaste llvm::sys::path::append(MainFileDirSS, MainFileName); 347269000Semaste MainFileName = MainFileDirSS.str(); 348263508Sdim } 349212904Sdim } 350205408Srdivacky 351212904Sdim // Save filename string. 352263508Sdim StringRef Filename = internString(MainFileName); 353249423Sdim 354249423Sdim // Save split dwarf file string. 355249423Sdim std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile; 356263508Sdim StringRef SplitDwarfFilename = internString(SplitDwarfFile); 357263508Sdim 358204962Srdivacky unsigned LangTag; 359234353Sdim const LangOptions &LO = CGM.getLangOpts(); 360193326Sed if (LO.CPlusPlus) { 361193326Sed if (LO.ObjC1) 362193326Sed LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; 363193326Sed else 364193326Sed LangTag = llvm::dwarf::DW_LANG_C_plus_plus; 365193326Sed } else if (LO.ObjC1) { 366193326Sed LangTag = llvm::dwarf::DW_LANG_ObjC; 367193326Sed } else if (LO.C99) { 368193326Sed LangTag = llvm::dwarf::DW_LANG_C99; 369193326Sed } else { 370193326Sed LangTag = llvm::dwarf::DW_LANG_C89; 371193326Sed } 372193326Sed 373212904Sdim std::string Producer = getClangFullVersion(); 374193326Sed 375193326Sed // Figure out which version of the ObjC runtime we have. 376193326Sed unsigned RuntimeVers = 0; 377193326Sed if (LO.ObjC1) 378239462Sdim RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1; 379198092Srdivacky 380193326Sed // Create new compile unit. 381218893Sdim // FIXME - Eliminate TheCU. 382263508Sdim TheCU = DBuilder.createCompileUnit(LangTag, Filename, getCurrentDirname(), 383263508Sdim Producer, LO.Optimize, 384263508Sdim CGM.getCodeGenOpts().DwarfDebugFlags, 385263508Sdim RuntimeVers, SplitDwarfFilename); 386193326Sed} 387193326Sed 388193326Sed/// CreateType - Get the Basic type from the cache or create a new 389193326Sed/// one if necessary. 390218893Sdimllvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) { 391193326Sed unsigned Encoding = 0; 392239462Sdim StringRef BTName; 393193326Sed switch (BT->getKind()) { 394234353Sdim#define BUILTIN_TYPE(Id, SingletonId) 395234353Sdim#define PLACEHOLDER_TYPE(Id, SingletonId) \ 396234353Sdim case BuiltinType::Id: 397234353Sdim#include "clang/AST/BuiltinTypes.def" 398226633Sdim case BuiltinType::Dependent: 399234353Sdim llvm_unreachable("Unexpected builtin type"); 400226633Sdim case BuiltinType::NullPtr: 401263508Sdim return DBuilder.createNullPtrType(); 402193326Sed case BuiltinType::Void: 403193326Sed return llvm::DIType(); 404212904Sdim case BuiltinType::ObjCClass: 405263508Sdim if (ClassTy) 406243830Sdim return ClassTy; 407243830Sdim ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 408243830Sdim "objc_class", TheCU, 409243830Sdim getOrCreateMainFile(), 0); 410243830Sdim return ClassTy; 411212904Sdim case BuiltinType::ObjCId: { 412212904Sdim // typedef struct objc_class *Class; 413212904Sdim // typedef struct objc_object { 414212904Sdim // Class isa; 415212904Sdim // } *id; 416212904Sdim 417263508Sdim if (ObjTy) 418243830Sdim return ObjTy; 419243830Sdim 420263508Sdim if (!ClassTy) 421243830Sdim ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 422243830Sdim "objc_class", TheCU, 423243830Sdim getOrCreateMainFile(), 0); 424243830Sdim 425212904Sdim unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 426263508Sdim 427243830Sdim llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size); 428212904Sdim 429249423Sdim ObjTy = 430249423Sdim DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(), 431249423Sdim 0, 0, 0, 0, llvm::DIType(), llvm::DIArray()); 432243830Sdim 433249423Sdim ObjTy.setTypeArray(DBuilder.getOrCreateArray(&*DBuilder.createMemberType( 434249423Sdim ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy))); 435243830Sdim return ObjTy; 436212904Sdim } 437218893Sdim case BuiltinType::ObjCSel: { 438263508Sdim if (SelTy) 439243830Sdim return SelTy; 440243830Sdim SelTy = 441234353Sdim DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 442239462Sdim "objc_selector", TheCU, getOrCreateMainFile(), 443234353Sdim 0); 444243830Sdim return SelTy; 445218893Sdim } 446249423Sdim 447249423Sdim case BuiltinType::OCLImage1d: 448249423Sdim return getOrCreateStructPtrType("opencl_image1d_t", 449249423Sdim OCLImage1dDITy); 450249423Sdim case BuiltinType::OCLImage1dArray: 451263508Sdim return getOrCreateStructPtrType("opencl_image1d_array_t", 452249423Sdim OCLImage1dArrayDITy); 453249423Sdim case BuiltinType::OCLImage1dBuffer: 454249423Sdim return getOrCreateStructPtrType("opencl_image1d_buffer_t", 455249423Sdim OCLImage1dBufferDITy); 456249423Sdim case BuiltinType::OCLImage2d: 457249423Sdim return getOrCreateStructPtrType("opencl_image2d_t", 458249423Sdim OCLImage2dDITy); 459249423Sdim case BuiltinType::OCLImage2dArray: 460249423Sdim return getOrCreateStructPtrType("opencl_image2d_array_t", 461249423Sdim OCLImage2dArrayDITy); 462249423Sdim case BuiltinType::OCLImage3d: 463249423Sdim return getOrCreateStructPtrType("opencl_image3d_t", 464249423Sdim OCLImage3dDITy); 465249423Sdim case BuiltinType::OCLSampler: 466249423Sdim return DBuilder.createBasicType("opencl_sampler_t", 467249423Sdim CGM.getContext().getTypeSize(BT), 468249423Sdim CGM.getContext().getTypeAlign(BT), 469249423Sdim llvm::dwarf::DW_ATE_unsigned); 470249423Sdim case BuiltinType::OCLEvent: 471249423Sdim return getOrCreateStructPtrType("opencl_event_t", 472249423Sdim OCLEventDITy); 473249423Sdim 474193326Sed case BuiltinType::UChar: 475193326Sed case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 476193326Sed case BuiltinType::Char_S: 477193326Sed case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 478226633Sdim case BuiltinType::Char16: 479226633Sdim case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break; 480193326Sed case BuiltinType::UShort: 481193326Sed case BuiltinType::UInt: 482223017Sdim case BuiltinType::UInt128: 483193326Sed case BuiltinType::ULong: 484226633Sdim case BuiltinType::WChar_U: 485193326Sed case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 486193326Sed case BuiltinType::Short: 487193326Sed case BuiltinType::Int: 488223017Sdim case BuiltinType::Int128: 489193326Sed case BuiltinType::Long: 490226633Sdim case BuiltinType::WChar_S: 491193326Sed case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 492193326Sed case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 493226633Sdim case BuiltinType::Half: 494193326Sed case BuiltinType::Float: 495198092Srdivacky case BuiltinType::LongDouble: 496193326Sed case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 497198092Srdivacky } 498212904Sdim 499212904Sdim switch (BT->getKind()) { 500212904Sdim case BuiltinType::Long: BTName = "long int"; break; 501212904Sdim case BuiltinType::LongLong: BTName = "long long int"; break; 502212904Sdim case BuiltinType::ULong: BTName = "long unsigned int"; break; 503212904Sdim case BuiltinType::ULongLong: BTName = "long long unsigned int"; break; 504212904Sdim default: 505243830Sdim BTName = BT->getName(CGM.getLangOpts()); 506212904Sdim break; 507212904Sdim } 508193326Sed // Bit size, align and offset of the type. 509200583Srdivacky uint64_t Size = CGM.getContext().getTypeSize(BT); 510200583Srdivacky uint64_t Align = CGM.getContext().getTypeAlign(BT); 511263508Sdim llvm::DIType DbgTy = 512219077Sdim DBuilder.createBasicType(BTName, Size, Align, Encoding); 513198398Srdivacky return DbgTy; 514193326Sed} 515193326Sed 516218893Sdimllvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) { 517193326Sed // Bit size, align and offset of the type. 518193326Sed unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; 519193326Sed if (Ty->isComplexIntegerType()) 520193326Sed Encoding = llvm::dwarf::DW_ATE_lo_user; 521198092Srdivacky 522200583Srdivacky uint64_t Size = CGM.getContext().getTypeSize(Ty); 523200583Srdivacky uint64_t Align = CGM.getContext().getTypeAlign(Ty); 524263508Sdim llvm::DIType DbgTy = 525219077Sdim DBuilder.createBasicType("complex", Size, Align, Encoding); 526198092Srdivacky 527198398Srdivacky return DbgTy; 528193326Sed} 529193326Sed 530198092Srdivacky/// CreateCVRType - Get the qualified type from the cache or create 531193326Sed/// a new one if necessary. 532204962Srdivackyllvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { 533198092Srdivacky QualifierCollector Qc; 534198092Srdivacky const Type *T = Qc.strip(Ty); 535198092Srdivacky 536198092Srdivacky // Ignore these qualifiers for now. 537198092Srdivacky Qc.removeObjCGCAttr(); 538198092Srdivacky Qc.removeAddressSpace(); 539224145Sdim Qc.removeObjCLifetime(); 540198092Srdivacky 541193326Sed // We will create one Derived type for one qualifier and recurse to handle any 542193326Sed // additional ones. 543193326Sed unsigned Tag; 544198092Srdivacky if (Qc.hasConst()) { 545193326Sed Tag = llvm::dwarf::DW_TAG_const_type; 546198092Srdivacky Qc.removeConst(); 547198092Srdivacky } else if (Qc.hasVolatile()) { 548193326Sed Tag = llvm::dwarf::DW_TAG_volatile_type; 549198092Srdivacky Qc.removeVolatile(); 550198092Srdivacky } else if (Qc.hasRestrict()) { 551198092Srdivacky Tag = llvm::dwarf::DW_TAG_restrict_type; 552198092Srdivacky Qc.removeRestrict(); 553193326Sed } else { 554198092Srdivacky assert(Qc.empty() && "Unknown type qualifier for debug info"); 555198092Srdivacky return getOrCreateType(QualType(T, 0), Unit); 556193326Sed } 557198092Srdivacky 558218893Sdim llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); 559198092Srdivacky 560193326Sed // No need to fill in the Name, Line, Size, Alignment, Offset in case of 561193326Sed // CVR derived types. 562219077Sdim llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy); 563263508Sdim 564198398Srdivacky return DbgTy; 565193326Sed} 566193326Sed 567198092Srdivackyllvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, 568204962Srdivacky llvm::DIFile Unit) { 569249423Sdim 570249423Sdim // The frontend treats 'id' as a typedef to an ObjCObjectType, 571249423Sdim // whereas 'id<protocol>' is treated as an ObjCPointerType. For the 572249423Sdim // debug info, we want to emit 'id' in both cases. 573249423Sdim if (Ty->isObjCQualifiedIdType()) 574249423Sdim return getOrCreateType(CGM.getContext().getObjCIdType(), Unit); 575249423Sdim 576198398Srdivacky llvm::DIType DbgTy = 577263508Sdim CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 578199482Srdivacky Ty->getPointeeType(), Unit); 579198398Srdivacky return DbgTy; 580198092Srdivacky} 581198092Srdivacky 582193326Sedllvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 583204962Srdivacky llvm::DIFile Unit) { 584263508Sdim return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 585199482Srdivacky Ty->getPointeeType(), Unit); 586199482Srdivacky} 587198092Srdivacky 588263508Sdim/// In C++ mode, types have linkage, so we can rely on the ODR and 589263508Sdim/// on their mangled names, if they're external. 590263508Sdimstatic SmallString<256> 591263508SdimgetUniqueTagTypeName(const TagType *Ty, CodeGenModule &CGM, 592263508Sdim llvm::DICompileUnit TheCU) { 593263508Sdim SmallString<256> FullName; 594263508Sdim // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++. 595263508Sdim // For now, only apply ODR with C++. 596263508Sdim const TagDecl *TD = Ty->getDecl(); 597263508Sdim if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus || 598263508Sdim !TD->isExternallyVisible()) 599263508Sdim return FullName; 600263508Sdim // Microsoft Mangler does not have support for mangleCXXRTTIName yet. 601263508Sdim if (CGM.getTarget().getCXXABI().isMicrosoft()) 602263508Sdim return FullName; 603263508Sdim 604263508Sdim // TODO: This is using the RTTI name. Is there a better way to get 605263508Sdim // a unique string for a type? 606263508Sdim llvm::raw_svector_ostream Out(FullName); 607263508Sdim CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out); 608263508Sdim Out.flush(); 609263508Sdim return FullName; 610263508Sdim} 611263508Sdim 612234353Sdim// Creates a forward declaration for a RecordDecl in the given context. 613263508Sdimllvm::DICompositeType 614263508SdimCGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty, 615263508Sdim llvm::DIDescriptor Ctx) { 616263508Sdim const RecordDecl *RD = Ty->getDecl(); 617263508Sdim if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD))) 618263508Sdim return llvm::DICompositeType(T); 619234353Sdim llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 620234353Sdim unsigned Line = getLineNumber(RD->getLocation()); 621243830Sdim StringRef RDName = getClassName(RD); 622234353Sdim 623234353Sdim unsigned Tag = 0; 624243830Sdim if (RD->isStruct() || RD->isInterface()) 625234353Sdim Tag = llvm::dwarf::DW_TAG_structure_type; 626234353Sdim else if (RD->isUnion()) 627234353Sdim Tag = llvm::dwarf::DW_TAG_union_type; 628243830Sdim else { 629243830Sdim assert(RD->isClass()); 630243830Sdim Tag = llvm::dwarf::DW_TAG_class_type; 631243830Sdim } 632234353Sdim 633234353Sdim // Create the type. 634263508Sdim SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); 635263508Sdim return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, 636263508Sdim FullName); 637234353Sdim} 638234353Sdim 639199482Srdivackyllvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, 640263508Sdim const Type *Ty, 641199482Srdivacky QualType PointeeTy, 642204962Srdivacky llvm::DIFile Unit) { 643239462Sdim if (Tag == llvm::dwarf::DW_TAG_reference_type || 644239462Sdim Tag == llvm::dwarf::DW_TAG_rvalue_reference_type) 645263508Sdim return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit)); 646249423Sdim 647193326Sed // Bit size, align and offset of the type. 648199482Srdivacky // Size is always the size of a pointer. We can't use getTypeSize here 649199482Srdivacky // because that does not return the correct value for references. 650221345Sdim unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); 651251662Sdim uint64_t Size = CGM.getTarget().getPointerWidth(AS); 652200583Srdivacky uint64_t Align = CGM.getContext().getTypeAlign(Ty); 653198092Srdivacky 654263508Sdim return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size, 655263508Sdim Align); 656193326Sed} 657193326Sed 658263508Sdimllvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name, 659263508Sdim llvm::DIType &Cache) { 660263508Sdim if (Cache) 661249423Sdim return Cache; 662263508Sdim Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name, 663263508Sdim TheCU, getOrCreateMainFile(), 0); 664263508Sdim unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 665263508Sdim Cache = DBuilder.createPointerType(Cache, Size); 666263508Sdim return Cache; 667249423Sdim} 668249423Sdim 669193326Sedllvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, 670204962Srdivacky llvm::DIFile Unit) { 671263508Sdim if (BlockLiteralGeneric) 672193326Sed return BlockLiteralGeneric; 673193326Sed 674226633Sdim SmallVector<llvm::Value *, 8> EltTys; 675193326Sed llvm::DIType FieldTy; 676193326Sed QualType FType; 677193326Sed uint64_t FieldSize, FieldOffset; 678193326Sed unsigned FieldAlign; 679193326Sed llvm::DIArray Elements; 680193326Sed llvm::DIType EltTy, DescTy; 681193326Sed 682193326Sed FieldOffset = 0; 683200583Srdivacky FType = CGM.getContext().UnsignedLongTy; 684207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); 685207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); 686193326Sed 687221345Sdim Elements = DBuilder.getOrCreateArray(EltTys); 688193326Sed EltTys.clear(); 689193326Sed 690218893Sdim unsigned Flags = llvm::DIDescriptor::FlagAppleBlock; 691208600Srdivacky unsigned LineNo = getLineNumber(CurLoc); 692198092Srdivacky 693219077Sdim EltTy = DBuilder.createStructType(Unit, "__block_descriptor", 694218893Sdim Unit, LineNo, FieldOffset, 0, 695249423Sdim Flags, llvm::DIType(), Elements); 696198092Srdivacky 697193326Sed // Bit size, align and offset of the type. 698200583Srdivacky uint64_t Size = CGM.getContext().getTypeSize(Ty); 699198092Srdivacky 700219077Sdim DescTy = DBuilder.createPointerType(EltTy, Size); 701193326Sed 702193326Sed FieldOffset = 0; 703200583Srdivacky FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 704207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 705200583Srdivacky FType = CGM.getContext().IntTy; 706207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 707207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); 708200583Srdivacky FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 709207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); 710193326Sed 711200583Srdivacky FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 712193326Sed FieldTy = DescTy; 713200583Srdivacky FieldSize = CGM.getContext().getTypeSize(Ty); 714200583Srdivacky FieldAlign = CGM.getContext().getTypeAlign(Ty); 715224145Sdim FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit, 716218893Sdim LineNo, FieldSize, FieldAlign, 717218893Sdim FieldOffset, 0, FieldTy); 718193326Sed EltTys.push_back(FieldTy); 719193326Sed 720193326Sed FieldOffset += FieldSize; 721221345Sdim Elements = DBuilder.getOrCreateArray(EltTys); 722193326Sed 723219077Sdim EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", 724218893Sdim Unit, LineNo, FieldOffset, 0, 725249423Sdim Flags, llvm::DIType(), Elements); 726198092Srdivacky 727219077Sdim BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size); 728193326Sed return BlockLiteralGeneric; 729193326Sed} 730193326Sed 731234353Sdimllvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) { 732193326Sed // Typedefs are derived from some other type. If we have a typedef of a 733193326Sed // typedef, make sure to emit the whole chain. 734193326Sed llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 735263508Sdim if (!Src) 736218893Sdim return llvm::DIType(); 737193326Sed // We don't set size information, but do specify where the typedef was 738193326Sed // declared. 739208600Srdivacky unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); 740223017Sdim const TypedefNameDecl *TyDecl = Ty->getDecl(); 741263508Sdim 742234353Sdim llvm::DIDescriptor TypedefContext = 743223017Sdim getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())); 744263508Sdim 745234353Sdim return 746234353Sdim DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext); 747193326Sed} 748193326Sed 749193326Sedllvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 750204962Srdivacky llvm::DIFile Unit) { 751226633Sdim SmallVector<llvm::Value *, 16> EltTys; 752193326Sed 753193326Sed // Add the result type at least. 754193326Sed EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 755198092Srdivacky 756193326Sed // Set up remainder of arguments if there is a prototype. 757193326Sed // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 758218893Sdim if (isa<FunctionNoProtoType>(Ty)) 759219077Sdim EltTys.push_back(DBuilder.createUnspecifiedParameter()); 760239462Sdim else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) { 761239462Sdim for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i) 762239462Sdim EltTys.push_back(getOrCreateType(FPT->getArgType(i), Unit)); 763269000Semaste if (FPT->isVariadic()) 764269000Semaste EltTys.push_back(DBuilder.createUnspecifiedParameter()); 765193326Sed } 766193326Sed 767221345Sdim llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys); 768239462Sdim return DBuilder.createSubroutineType(Unit, EltTypeArray); 769193326Sed} 770193326Sed 771234353Sdim 772226633Sdimllvm::DIType CGDebugInfo::createFieldType(StringRef name, 773219077Sdim QualType type, 774226633Sdim uint64_t sizeInBitsOverride, 775219077Sdim SourceLocation loc, 776219077Sdim AccessSpecifier AS, 777219077Sdim uint64_t offsetInBits, 778224145Sdim llvm::DIFile tunit, 779263508Sdim llvm::DIScope scope) { 780219077Sdim llvm::DIType debugType = getOrCreateType(type, tunit); 781219077Sdim 782219077Sdim // Get the location for the field. 783219077Sdim llvm::DIFile file = getOrCreateFile(loc); 784219077Sdim unsigned line = getLineNumber(loc); 785219077Sdim 786219077Sdim uint64_t sizeInBits = 0; 787219077Sdim unsigned alignInBits = 0; 788219077Sdim if (!type->isIncompleteArrayType()) { 789219077Sdim llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type); 790219077Sdim 791226633Sdim if (sizeInBitsOverride) 792226633Sdim sizeInBits = sizeInBitsOverride; 793219077Sdim } 794219077Sdim 795219077Sdim unsigned flags = 0; 796219077Sdim if (AS == clang::AS_private) 797219077Sdim flags |= llvm::DIDescriptor::FlagPrivate; 798219077Sdim else if (AS == clang::AS_protected) 799219077Sdim flags |= llvm::DIDescriptor::FlagProtected; 800219077Sdim 801224145Sdim return DBuilder.createMemberType(scope, name, file, line, sizeInBits, 802224145Sdim alignInBits, offsetInBits, flags, debugType); 803219077Sdim} 804219077Sdim 805249423Sdim/// CollectRecordLambdaFields - Helper for CollectRecordFields. 806249423Sdimvoid CGDebugInfo:: 807249423SdimCollectRecordLambdaFields(const CXXRecordDecl *CXXDecl, 808249423Sdim SmallVectorImpl<llvm::Value *> &elements, 809249423Sdim llvm::DIType RecordTy) { 810249423Sdim // For C++11 Lambdas a Field will be the same as a Capture, but the Capture 811249423Sdim // has the name and the location of the variable so we should iterate over 812249423Sdim // both concurrently. 813249423Sdim const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl); 814249423Sdim RecordDecl::field_iterator Field = CXXDecl->field_begin(); 815249423Sdim unsigned fieldno = 0; 816249423Sdim for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(), 817249423Sdim E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) { 818249423Sdim const LambdaExpr::Capture C = *I; 819249423Sdim if (C.capturesVariable()) { 820249423Sdim VarDecl *V = C.getCapturedVar(); 821249423Sdim llvm::DIFile VUnit = getOrCreateFile(C.getLocation()); 822249423Sdim StringRef VName = V->getName(); 823249423Sdim uint64_t SizeInBitsOverride = 0; 824249423Sdim if (Field->isBitField()) { 825249423Sdim SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext()); 826249423Sdim assert(SizeInBitsOverride && "found named 0-width bitfield"); 827249423Sdim } 828249423Sdim llvm::DIType fieldType 829249423Sdim = createFieldType(VName, Field->getType(), SizeInBitsOverride, 830249423Sdim C.getLocation(), Field->getAccess(), 831249423Sdim layout.getFieldOffset(fieldno), VUnit, RecordTy); 832249423Sdim elements.push_back(fieldType); 833249423Sdim } else { 834249423Sdim // TODO: Need to handle 'this' in some way by probably renaming the 835249423Sdim // this of the lambda class and having a field member of 'this' or 836249423Sdim // by using AT_object_pointer for the function and having that be 837249423Sdim // used as 'this' for semantic references. 838249423Sdim assert(C.capturesThis() && "Field that isn't captured and isn't this?"); 839249423Sdim FieldDecl *f = *Field; 840249423Sdim llvm::DIFile VUnit = getOrCreateFile(f->getLocation()); 841249423Sdim QualType type = f->getType(); 842249423Sdim llvm::DIType fieldType 843249423Sdim = createFieldType("this", type, 0, f->getLocation(), f->getAccess(), 844249423Sdim layout.getFieldOffset(fieldno), VUnit, RecordTy); 845249423Sdim 846249423Sdim elements.push_back(fieldType); 847249423Sdim } 848249423Sdim } 849249423Sdim} 850249423Sdim 851263508Sdim/// Helper for CollectRecordFields. 852263508Sdimllvm::DIDerivedType 853263508SdimCGDebugInfo::CreateRecordStaticField(const VarDecl *Var, 854263508Sdim llvm::DIType RecordTy) { 855249423Sdim // Create the descriptor for the static variable, with or without 856249423Sdim // constant initializers. 857249423Sdim llvm::DIFile VUnit = getOrCreateFile(Var->getLocation()); 858249423Sdim llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit); 859249423Sdim 860249423Sdim unsigned LineNumber = getLineNumber(Var->getLocation()); 861249423Sdim StringRef VName = Var->getName(); 862249423Sdim llvm::Constant *C = NULL; 863249423Sdim if (Var->getInit()) { 864249423Sdim const APValue *Value = Var->evaluateValue(); 865249423Sdim if (Value) { 866249423Sdim if (Value->isInt()) 867249423Sdim C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt()); 868249423Sdim if (Value->isFloat()) 869249423Sdim C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat()); 870249423Sdim } 871249423Sdim } 872249423Sdim 873249423Sdim unsigned Flags = 0; 874249423Sdim AccessSpecifier Access = Var->getAccess(); 875249423Sdim if (Access == clang::AS_private) 876249423Sdim Flags |= llvm::DIDescriptor::FlagPrivate; 877249423Sdim else if (Access == clang::AS_protected) 878249423Sdim Flags |= llvm::DIDescriptor::FlagProtected; 879249423Sdim 880263508Sdim llvm::DIDerivedType GV = DBuilder.createStaticMemberType( 881263508Sdim RecordTy, VName, VUnit, LineNumber, VTy, Flags, C); 882249423Sdim StaticDataMemberCache[Var->getCanonicalDecl()] = llvm::WeakVH(GV); 883263508Sdim return GV; 884249423Sdim} 885249423Sdim 886249423Sdim/// CollectRecordNormalField - Helper for CollectRecordFields. 887249423Sdimvoid CGDebugInfo:: 888249423SdimCollectRecordNormalField(const FieldDecl *field, uint64_t OffsetInBits, 889249423Sdim llvm::DIFile tunit, 890249423Sdim SmallVectorImpl<llvm::Value *> &elements, 891249423Sdim llvm::DIType RecordTy) { 892249423Sdim StringRef name = field->getName(); 893249423Sdim QualType type = field->getType(); 894249423Sdim 895249423Sdim // Ignore unnamed fields unless they're anonymous structs/unions. 896249423Sdim if (name.empty() && !type->isRecordType()) 897249423Sdim return; 898249423Sdim 899249423Sdim uint64_t SizeInBitsOverride = 0; 900249423Sdim if (field->isBitField()) { 901249423Sdim SizeInBitsOverride = field->getBitWidthValue(CGM.getContext()); 902249423Sdim assert(SizeInBitsOverride && "found named 0-width bitfield"); 903249423Sdim } 904249423Sdim 905249423Sdim llvm::DIType fieldType 906249423Sdim = createFieldType(name, type, SizeInBitsOverride, 907249423Sdim field->getLocation(), field->getAccess(), 908249423Sdim OffsetInBits, tunit, RecordTy); 909249423Sdim 910249423Sdim elements.push_back(fieldType); 911249423Sdim} 912249423Sdim 913202879Srdivacky/// CollectRecordFields - A helper function to collect debug info for 914202879Srdivacky/// record fields. This is used while creating debug info entry for a Record. 915263508Sdimvoid CGDebugInfo::CollectRecordFields(const RecordDecl *record, 916263508Sdim llvm::DIFile tunit, 917263508Sdim SmallVectorImpl<llvm::Value *> &elements, 918263508Sdim llvm::DICompositeType RecordTy) { 919234353Sdim const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record); 920234353Sdim 921249423Sdim if (CXXDecl && CXXDecl->isLambda()) 922249423Sdim CollectRecordLambdaFields(CXXDecl, elements, RecordTy); 923249423Sdim else { 924249423Sdim const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); 925243830Sdim 926249423Sdim // Field number for non-static fields. 927249423Sdim unsigned fieldNo = 0; 928249423Sdim 929249423Sdim // Static and non-static members should appear in the same order as 930249423Sdim // the corresponding declarations in the source program. 931249423Sdim for (RecordDecl::decl_iterator I = record->decls_begin(), 932249423Sdim E = record->decls_end(); I != E; ++I) 933263508Sdim if (const VarDecl *V = dyn_cast<VarDecl>(*I)) { 934263508Sdim // Reuse the existing static member declaration if one exists 935263508Sdim llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator MI = 936263508Sdim StaticDataMemberCache.find(V->getCanonicalDecl()); 937263508Sdim if (MI != StaticDataMemberCache.end()) { 938263508Sdim assert(MI->second && 939263508Sdim "Static data member declaration should still exist"); 940263508Sdim elements.push_back( 941263508Sdim llvm::DIDerivedType(cast<llvm::MDNode>(MI->second))); 942263508Sdim } else 943263508Sdim elements.push_back(CreateRecordStaticField(V, RecordTy)); 944263508Sdim } else if (FieldDecl *field = dyn_cast<FieldDecl>(*I)) { 945249423Sdim CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), 946249423Sdim tunit, elements, RecordTy); 947219077Sdim 948249423Sdim // Bump field number for next field. 949249423Sdim ++fieldNo; 950234353Sdim } 951193326Sed } 952202879Srdivacky} 953198092Srdivacky 954203955Srdivacky/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This 955203955Srdivacky/// function type is not updated to include implicit "this" pointer. Use this 956203955Srdivacky/// routine to get a method type which includes "this" pointer. 957263508Sdimllvm::DICompositeType 958203955SrdivackyCGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, 959204962Srdivacky llvm::DIFile Unit) { 960249423Sdim const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>(); 961249423Sdim if (Method->isStatic()) 962263508Sdim return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit)); 963249423Sdim return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()), 964249423Sdim Func, Unit); 965249423Sdim} 966234353Sdim 967263508Sdimllvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType( 968249423Sdim QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) { 969203955Srdivacky // Add "this" pointer. 970249423Sdim llvm::DIArray Args = llvm::DICompositeType( 971249423Sdim getOrCreateType(QualType(Func, 0), Unit)).getTypeArray(); 972203955Srdivacky assert (Args.getNumElements() && "Invalid number of arguments!"); 973203955Srdivacky 974226633Sdim SmallVector<llvm::Value *, 16> Elts; 975203955Srdivacky 976203955Srdivacky // First element is always return type. For 'void' functions it is NULL. 977203955Srdivacky Elts.push_back(Args.getElement(0)); 978203955Srdivacky 979249423Sdim // "this" pointer is always first argument. 980249423Sdim const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl(); 981249423Sdim if (isa<ClassTemplateSpecializationDecl>(RD)) { 982249423Sdim // Create pointer type directly in this case. 983249423Sdim const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); 984249423Sdim QualType PointeeTy = ThisPtrTy->getPointeeType(); 985249423Sdim unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); 986251662Sdim uint64_t Size = CGM.getTarget().getPointerWidth(AS); 987249423Sdim uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy); 988249423Sdim llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit); 989263508Sdim llvm::DIType ThisPtrType = 990263508Sdim DBuilder.createPointerType(PointeeType, Size, Align); 991249423Sdim TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; 992249423Sdim // TODO: This and the artificial type below are misleading, the 993249423Sdim // types aren't artificial the argument is, but the current 994249423Sdim // metadata doesn't represent that. 995249423Sdim ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); 996249423Sdim Elts.push_back(ThisPtrType); 997249423Sdim } else { 998249423Sdim llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit); 999249423Sdim TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; 1000249423Sdim ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); 1001249423Sdim Elts.push_back(ThisPtrType); 1002226633Sdim } 1003210299Sed 1004203955Srdivacky // Copy rest of the arguments. 1005203955Srdivacky for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) 1006203955Srdivacky Elts.push_back(Args.getElement(i)); 1007203955Srdivacky 1008221345Sdim llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); 1009203955Srdivacky 1010219077Sdim return DBuilder.createSubroutineType(Unit, EltTypeArray); 1011203955Srdivacky} 1012203955Srdivacky 1013263508Sdim/// isFunctionLocalClass - Return true if CXXRecordDecl is defined 1014218893Sdim/// inside a function. 1015218893Sdimstatic bool isFunctionLocalClass(const CXXRecordDecl *RD) { 1016234353Sdim if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext())) 1017218893Sdim return isFunctionLocalClass(NRD); 1018234353Sdim if (isa<FunctionDecl>(RD->getDeclContext())) 1019218893Sdim return true; 1020218893Sdim return false; 1021218893Sdim} 1022234353Sdim 1023203955Srdivacky/// CreateCXXMemberFunction - A helper function to create a DISubprogram for 1024203955Srdivacky/// a single member function GlobalDecl. 1025203955Srdivackyllvm::DISubprogram 1026203955SrdivackyCGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, 1027204962Srdivacky llvm::DIFile Unit, 1028212904Sdim llvm::DIType RecordTy) { 1029263508Sdim bool IsCtorOrDtor = 1030203955Srdivacky isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); 1031263508Sdim 1032226633Sdim StringRef MethodName = getFunctionName(Method); 1033263508Sdim llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit); 1034239462Sdim 1035203955Srdivacky // Since a single ctor/dtor corresponds to multiple functions, it doesn't 1036203955Srdivacky // make sense to give a single ctor/dtor a linkage name. 1037226633Sdim StringRef MethodLinkageName; 1038218893Sdim if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) 1039210299Sed MethodLinkageName = CGM.getMangledName(Method); 1040203955Srdivacky 1041203955Srdivacky // Get the location for the method. 1042263508Sdim llvm::DIFile MethodDefUnit; 1043263508Sdim unsigned MethodLine = 0; 1044263508Sdim if (!Method->isImplicit()) { 1045263508Sdim MethodDefUnit = getOrCreateFile(Method->getLocation()); 1046263508Sdim MethodLine = getLineNumber(Method->getLocation()); 1047263508Sdim } 1048203955Srdivacky 1049203955Srdivacky // Collect virtual method info. 1050203955Srdivacky llvm::DIType ContainingType; 1051263508Sdim unsigned Virtuality = 0; 1052203955Srdivacky unsigned VIndex = 0; 1053263508Sdim 1054203955Srdivacky if (Method->isVirtual()) { 1055203955Srdivacky if (Method->isPure()) 1056203955Srdivacky Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; 1057203955Srdivacky else 1058203955Srdivacky Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; 1059263508Sdim 1060203955Srdivacky // It doesn't make sense to give a virtual destructor a vtable index, 1061203955Srdivacky // since a single destructor has two entries in the vtable. 1062263508Sdim // FIXME: Add proper support for debug info for virtual calls in 1063263508Sdim // the Microsoft ABI, where we may use multiple vptrs to make a vftable 1064263508Sdim // lookup if we have multiple or virtual inheritance. 1065263508Sdim if (!isa<CXXDestructorDecl>(Method) && 1066263508Sdim !CGM.getTarget().getCXXABI().isMicrosoft()) 1067263508Sdim VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method); 1068203955Srdivacky ContainingType = RecordTy; 1069203955Srdivacky } 1070203955Srdivacky 1071218893Sdim unsigned Flags = 0; 1072218893Sdim if (Method->isImplicit()) 1073218893Sdim Flags |= llvm::DIDescriptor::FlagArtificial; 1074218893Sdim AccessSpecifier Access = Method->getAccess(); 1075218893Sdim if (Access == clang::AS_private) 1076218893Sdim Flags |= llvm::DIDescriptor::FlagPrivate; 1077218893Sdim else if (Access == clang::AS_protected) 1078218893Sdim Flags |= llvm::DIDescriptor::FlagProtected; 1079218893Sdim if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { 1080218893Sdim if (CXXC->isExplicit()) 1081218893Sdim Flags |= llvm::DIDescriptor::FlagExplicit; 1082263508Sdim } else if (const CXXConversionDecl *CXXC = 1083218893Sdim dyn_cast<CXXConversionDecl>(Method)) { 1084218893Sdim if (CXXC->isExplicit()) 1085218893Sdim Flags |= llvm::DIDescriptor::FlagExplicit; 1086218893Sdim } 1087218893Sdim if (Method->hasPrototype()) 1088218893Sdim Flags |= llvm::DIDescriptor::FlagPrototyped; 1089234353Sdim 1090234353Sdim llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit); 1091203955Srdivacky llvm::DISubprogram SP = 1092263508Sdim DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName, 1093218893Sdim MethodDefUnit, MethodLine, 1094263508Sdim MethodTy, /*isLocalToUnit=*/false, 1095218893Sdim /* isDefinition=*/ false, 1096218893Sdim Virtuality, VIndex, ContainingType, 1097234353Sdim Flags, CGM.getLangOpts().Optimize, NULL, 1098234353Sdim TParamsArray); 1099263508Sdim 1100234353Sdim SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP); 1101203955Srdivacky 1102203955Srdivacky return SP; 1103203955Srdivacky} 1104203955Srdivacky 1105202879Srdivacky/// CollectCXXMemberFunctions - A helper function to collect debug info for 1106263508Sdim/// C++ member functions. This is used while creating debug info entry for 1107202879Srdivacky/// a Record. 1108202879Srdivackyvoid CGDebugInfo:: 1109204962SrdivackyCollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, 1110226633Sdim SmallVectorImpl<llvm::Value *> &EltTys, 1111212904Sdim llvm::DIType RecordTy) { 1112234353Sdim 1113234353Sdim // Since we want more than just the individual member decls if we 1114234353Sdim // have templated functions iterate over every declaration to gather 1115234353Sdim // the functions. 1116234353Sdim for(DeclContext::decl_iterator I = RD->decls_begin(), 1117234353Sdim E = RD->decls_end(); I != E; ++I) { 1118263508Sdim if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*I)) { 1119263508Sdim // Reuse the existing member function declaration if it exists. 1120263508Sdim // It may be associated with the declaration of the type & should be 1121263508Sdim // reused as we're building the definition. 1122263508Sdim // 1123263508Sdim // This situation can arise in the vtable-based debug info reduction where 1124263508Sdim // implicit members are emitted in a non-vtable TU. 1125263508Sdim llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator MI = 1126263508Sdim SPCache.find(Method->getCanonicalDecl()); 1127263508Sdim if (MI == SPCache.end()) { 1128263508Sdim // If the member is implicit, lazily create it when we see the 1129263508Sdim // definition, not before. (an ODR-used implicit default ctor that's 1130263508Sdim // never actually code generated should not produce debug info) 1131263508Sdim if (!Method->isImplicit()) 1132263508Sdim EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); 1133263508Sdim } else 1134263508Sdim EltTys.push_back(MI->second); 1135263508Sdim } else if (const FunctionTemplateDecl *FTD = 1136263508Sdim dyn_cast<FunctionTemplateDecl>(*I)) { 1137263508Sdim // Add any template specializations that have already been seen. Like 1138263508Sdim // implicit member functions, these may have been added to a declaration 1139263508Sdim // in the case of vtable-based debug info reduction. 1140234353Sdim for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(), 1141263508Sdim SE = FTD->spec_end(); 1142263508Sdim SI != SE; ++SI) { 1143263508Sdim llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator MI = 1144263508Sdim SPCache.find(cast<CXXMethodDecl>(*SI)->getCanonicalDecl()); 1145263508Sdim if (MI != SPCache.end()) 1146263508Sdim EltTys.push_back(MI->second); 1147263508Sdim } 1148263508Sdim } 1149203955Srdivacky } 1150212904Sdim} 1151212904Sdim 1152203955Srdivacky/// CollectCXXBases - A helper function to collect debug info for 1153263508Sdim/// C++ base classes. This is used while creating debug info entry for 1154203955Srdivacky/// a Record. 1155203955Srdivackyvoid CGDebugInfo:: 1156204962SrdivackyCollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, 1157226633Sdim SmallVectorImpl<llvm::Value *> &EltTys, 1158212904Sdim llvm::DIType RecordTy) { 1159202879Srdivacky 1160203955Srdivacky const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 1161203955Srdivacky for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), 1162203955Srdivacky BE = RD->bases_end(); BI != BE; ++BI) { 1163203955Srdivacky unsigned BFlags = 0; 1164203955Srdivacky uint64_t BaseOffset; 1165263508Sdim 1166203955Srdivacky const CXXRecordDecl *Base = 1167203955Srdivacky cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); 1168263508Sdim 1169203955Srdivacky if (BI->isVirtual()) { 1170205219Srdivacky // virtual base offset offset is -ve. The code generator emits dwarf 1171203955Srdivacky // expression where it expects +ve number. 1172263508Sdim BaseOffset = 1173263508Sdim 0 - CGM.getItaniumVTableContext() 1174226633Sdim .getVirtualBaseOffsetOffset(RD, Base).getQuantity(); 1175218893Sdim BFlags = llvm::DIDescriptor::FlagVirtual; 1176203955Srdivacky } else 1177239462Sdim BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base)); 1178221345Sdim // FIXME: Inconsistent units for BaseOffset. It is in bytes when 1179221345Sdim // BI->isVirtual() and bits when not. 1180263508Sdim 1181203955Srdivacky AccessSpecifier Access = BI->getAccessSpecifier(); 1182203955Srdivacky if (Access == clang::AS_private) 1183218893Sdim BFlags |= llvm::DIDescriptor::FlagPrivate; 1184203955Srdivacky else if (Access == clang::AS_protected) 1185218893Sdim BFlags |= llvm::DIDescriptor::FlagProtected; 1186263508Sdim 1187263508Sdim llvm::DIType DTy = 1188263508Sdim DBuilder.createInheritance(RecordTy, 1189218893Sdim getOrCreateType(BI->getType(), Unit), 1190218893Sdim BaseOffset, BFlags); 1191203955Srdivacky EltTys.push_back(DTy); 1192202879Srdivacky } 1193203955Srdivacky} 1194202879Srdivacky 1195221345Sdim/// CollectTemplateParams - A helper function to collect template parameters. 1196221345Sdimllvm::DIArray CGDebugInfo:: 1197221345SdimCollectTemplateParams(const TemplateParameterList *TPList, 1198263508Sdim ArrayRef<TemplateArgument> TAList, 1199221345Sdim llvm::DIFile Unit) { 1200263508Sdim SmallVector<llvm::Value *, 16> TemplateParams; 1201221345Sdim for (unsigned i = 0, e = TAList.size(); i != e; ++i) { 1202221345Sdim const TemplateArgument &TA = TAList[i]; 1203263508Sdim StringRef Name; 1204263508Sdim if (TPList) 1205263508Sdim Name = TPList->getParam(i)->getName(); 1206263508Sdim switch (TA.getKind()) { 1207263508Sdim case TemplateArgument::Type: { 1208221345Sdim llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit); 1209221345Sdim llvm::DITemplateTypeParameter TTP = 1210263508Sdim DBuilder.createTemplateTypeParameter(TheCU, Name, TTy); 1211221345Sdim TemplateParams.push_back(TTP); 1212263508Sdim } break; 1213263508Sdim case TemplateArgument::Integral: { 1214221345Sdim llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit); 1215221345Sdim llvm::DITemplateValueParameter TVP = 1216263508Sdim DBuilder.createTemplateValueParameter( 1217263508Sdim TheCU, Name, TTy, 1218263508Sdim llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())); 1219263508Sdim TemplateParams.push_back(TVP); 1220263508Sdim } break; 1221263508Sdim case TemplateArgument::Declaration: { 1222263508Sdim const ValueDecl *D = TA.getAsDecl(); 1223263508Sdim bool InstanceMember = D->isCXXInstanceMember(); 1224263508Sdim QualType T = InstanceMember 1225263508Sdim ? CGM.getContext().getMemberPointerType( 1226263508Sdim D->getType(), cast<RecordDecl>(D->getDeclContext()) 1227263508Sdim ->getTypeForDecl()) 1228263508Sdim : CGM.getContext().getPointerType(D->getType()); 1229263508Sdim llvm::DIType TTy = getOrCreateType(T, Unit); 1230263508Sdim llvm::Value *V = 0; 1231263508Sdim // Variable pointer template parameters have a value that is the address 1232263508Sdim // of the variable. 1233263508Sdim if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 1234263508Sdim V = CGM.GetAddrOfGlobalVar(VD); 1235263508Sdim // Member function pointers have special support for building them, though 1236263508Sdim // this is currently unsupported in LLVM CodeGen. 1237263508Sdim if (InstanceMember) { 1238263508Sdim if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(D)) 1239263508Sdim V = CGM.getCXXABI().EmitMemberPointer(method); 1240263508Sdim } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 1241263508Sdim V = CGM.GetAddrOfFunction(FD); 1242263508Sdim // Member data pointers have special handling too to compute the fixed 1243263508Sdim // offset within the object. 1244271729Semaste if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) { 1245263508Sdim // These five lines (& possibly the above member function pointer 1246263508Sdim // handling) might be able to be refactored to use similar code in 1247263508Sdim // CodeGenModule::getMemberPointerConstant 1248263508Sdim uint64_t fieldOffset = CGM.getContext().getFieldOffset(D); 1249263508Sdim CharUnits chars = 1250263508Sdim CGM.getContext().toCharUnitsFromBits((int64_t) fieldOffset); 1251263508Sdim V = CGM.getCXXABI().EmitMemberDataPointer( 1252263508Sdim cast<MemberPointerType>(T.getTypePtr()), chars); 1253263508Sdim } 1254263508Sdim llvm::DITemplateValueParameter TVP = 1255263508Sdim DBuilder.createTemplateValueParameter(TheCU, Name, TTy, 1256263508Sdim V->stripPointerCasts()); 1257263508Sdim TemplateParams.push_back(TVP); 1258263508Sdim } break; 1259263508Sdim case TemplateArgument::NullPtr: { 1260263508Sdim QualType T = TA.getNullPtrType(); 1261263508Sdim llvm::DIType TTy = getOrCreateType(T, Unit); 1262263508Sdim llvm::Value *V = 0; 1263263508Sdim // Special case member data pointer null values since they're actually -1 1264263508Sdim // instead of zero. 1265263508Sdim if (const MemberPointerType *MPT = 1266263508Sdim dyn_cast<MemberPointerType>(T.getTypePtr())) 1267263508Sdim // But treat member function pointers as simple zero integers because 1268263508Sdim // it's easier than having a special case in LLVM's CodeGen. If LLVM 1269263508Sdim // CodeGen grows handling for values of non-null member function 1270263508Sdim // pointers then perhaps we could remove this special case and rely on 1271263508Sdim // EmitNullMemberPointer for member function pointers. 1272263508Sdim if (MPT->isMemberDataPointer()) 1273263508Sdim V = CGM.getCXXABI().EmitNullMemberPointer(MPT); 1274263508Sdim if (!V) 1275263508Sdim V = llvm::ConstantInt::get(CGM.Int8Ty, 0); 1276263508Sdim llvm::DITemplateValueParameter TVP = 1277263508Sdim DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V); 1278263508Sdim TemplateParams.push_back(TVP); 1279263508Sdim } break; 1280263508Sdim case TemplateArgument::Template: { 1281263508Sdim llvm::DITemplateValueParameter TVP = 1282263508Sdim DBuilder.createTemplateTemplateParameter( 1283263508Sdim TheCU, Name, llvm::DIType(), 1284263508Sdim TA.getAsTemplate().getAsTemplateDecl() 1285263508Sdim ->getQualifiedNameAsString()); 1286263508Sdim TemplateParams.push_back(TVP); 1287263508Sdim } break; 1288263508Sdim case TemplateArgument::Pack: { 1289263508Sdim llvm::DITemplateValueParameter TVP = 1290263508Sdim DBuilder.createTemplateParameterPack( 1291263508Sdim TheCU, Name, llvm::DIType(), 1292263508Sdim CollectTemplateParams(NULL, TA.getPackAsArray(), Unit)); 1293263508Sdim TemplateParams.push_back(TVP); 1294263508Sdim } break; 1295263508Sdim case TemplateArgument::Expression: { 1296263508Sdim const Expr *E = TA.getAsExpr(); 1297263508Sdim QualType T = E->getType(); 1298263508Sdim llvm::Value *V = CGM.EmitConstantExpr(E, T); 1299263508Sdim assert(V && "Expression in template argument isn't constant"); 1300263508Sdim llvm::DIType TTy = getOrCreateType(T, Unit); 1301263508Sdim llvm::DITemplateValueParameter TVP = 1302263508Sdim DBuilder.createTemplateValueParameter(TheCU, Name, TTy, 1303263508Sdim V->stripPointerCasts()); 1304263508Sdim TemplateParams.push_back(TVP); 1305263508Sdim } break; 1306263508Sdim // And the following should never occur: 1307263508Sdim case TemplateArgument::TemplateExpansion: 1308263508Sdim case TemplateArgument::Null: 1309263508Sdim llvm_unreachable( 1310263508Sdim "These argument types shouldn't exist in concrete types"); 1311221345Sdim } 1312221345Sdim } 1313221345Sdim return DBuilder.getOrCreateArray(TemplateParams); 1314221345Sdim} 1315221345Sdim 1316221345Sdim/// CollectFunctionTemplateParams - A helper function to collect debug 1317221345Sdim/// info for function template parameters. 1318221345Sdimllvm::DIArray CGDebugInfo:: 1319221345SdimCollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) { 1320226633Sdim if (FD->getTemplatedKind() == 1321226633Sdim FunctionDecl::TK_FunctionTemplateSpecialization) { 1322221345Sdim const TemplateParameterList *TList = 1323226633Sdim FD->getTemplateSpecializationInfo()->getTemplate() 1324226633Sdim ->getTemplateParameters(); 1325263508Sdim return CollectTemplateParams( 1326263508Sdim TList, FD->getTemplateSpecializationArgs()->asArray(), Unit); 1327221345Sdim } 1328221345Sdim return llvm::DIArray(); 1329221345Sdim} 1330221345Sdim 1331221345Sdim/// CollectCXXTemplateParams - A helper function to collect debug info for 1332221345Sdim/// template parameters. 1333221345Sdimllvm::DIArray CGDebugInfo:: 1334221345SdimCollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial, 1335221345Sdim llvm::DIFile Unit) { 1336221345Sdim llvm::PointerUnion<ClassTemplateDecl *, 1337221345Sdim ClassTemplatePartialSpecializationDecl *> 1338221345Sdim PU = TSpecial->getSpecializedTemplateOrPartial(); 1339263508Sdim 1340221345Sdim TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ? 1341221345Sdim PU.get<ClassTemplateDecl *>()->getTemplateParameters() : 1342221345Sdim PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters(); 1343221345Sdim const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs(); 1344263508Sdim return CollectTemplateParams(TPList, TAList.asArray(), Unit); 1345221345Sdim} 1346221345Sdim 1347203955Srdivacky/// getOrCreateVTablePtrType - Return debug info descriptor for vtable. 1348204962Srdivackyllvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { 1349204962Srdivacky if (VTablePtrType.isValid()) 1350203955Srdivacky return VTablePtrType; 1351203955Srdivacky 1352203955Srdivacky ASTContext &Context = CGM.getContext(); 1353203955Srdivacky 1354203955Srdivacky /* Function type */ 1355218893Sdim llvm::Value *STy = getOrCreateType(Context.IntTy, Unit); 1356221345Sdim llvm::DIArray SElements = DBuilder.getOrCreateArray(STy); 1357219077Sdim llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements); 1358203955Srdivacky unsigned Size = Context.getTypeSize(Context.VoidPtrTy); 1359219077Sdim llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0, 1360218893Sdim "__vtbl_ptr_type"); 1361219077Sdim VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); 1362203955Srdivacky return VTablePtrType; 1363203955Srdivacky} 1364203955Srdivacky 1365207619Srdivacky/// getVTableName - Get vtable name for the given Class. 1366226633SdimStringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { 1367263508Sdim // Copy the gdb compatible name on the side and use its reference. 1368263508Sdim return internString("_vptr$", RD->getNameAsString()); 1369203955Srdivacky} 1370203955Srdivacky 1371203955Srdivacky 1372207619Srdivacky/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate 1373203955Srdivacky/// debug info entry in EltTys vector. 1374203955Srdivackyvoid CGDebugInfo:: 1375207619SrdivackyCollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, 1376226633Sdim SmallVectorImpl<llvm::Value *> &EltTys) { 1377203955Srdivacky const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 1378203955Srdivacky 1379203955Srdivacky // If there is a primary base then it will hold vtable info. 1380203955Srdivacky if (RL.getPrimaryBase()) 1381203955Srdivacky return; 1382203955Srdivacky 1383203955Srdivacky // If this class is not dynamic then there is not any vtable info to collect. 1384203955Srdivacky if (!RD->isDynamicClass()) 1385203955Srdivacky return; 1386203955Srdivacky 1387203955Srdivacky unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 1388203955Srdivacky llvm::DIType VPTR 1389224145Sdim = DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 1390263508Sdim 0, Size, 0, 0, 1391263508Sdim llvm::DIDescriptor::FlagArtificial, 1392218893Sdim getOrCreateVTablePtrType(Unit)); 1393203955Srdivacky EltTys.push_back(VPTR); 1394203955Srdivacky} 1395203955Srdivacky 1396263508Sdim/// getOrCreateRecordType - Emit record type's standalone debug info. 1397263508Sdimllvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy, 1398218893Sdim SourceLocation Loc) { 1399263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 1400234353Sdim llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc)); 1401234353Sdim return T; 1402234353Sdim} 1403234353Sdim 1404234353Sdim/// getOrCreateInterfaceType - Emit an objective c interface type standalone 1405234353Sdim/// debug info. 1406234353Sdimllvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D, 1407249423Sdim SourceLocation Loc) { 1408263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 1409234353Sdim llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc)); 1410249423Sdim RetainedTypes.push_back(D.getAsOpaquePtr()); 1411218893Sdim return T; 1412218893Sdim} 1413218893Sdim 1414263508Sdimvoid CGDebugInfo::completeType(const RecordDecl *RD) { 1415263508Sdim if (DebugKind > CodeGenOptions::LimitedDebugInfo || 1416263508Sdim !CGM.getLangOpts().CPlusPlus) 1417263508Sdim completeRequiredType(RD); 1418263508Sdim} 1419263508Sdim 1420263508Sdimvoid CGDebugInfo::completeRequiredType(const RecordDecl *RD) { 1421263508Sdim if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) 1422263508Sdim if (CXXDecl->isDynamicClass()) 1423263508Sdim return; 1424263508Sdim 1425263508Sdim QualType Ty = CGM.getContext().getRecordType(RD); 1426263508Sdim llvm::DIType T = getTypeOrNull(Ty); 1427263508Sdim if (T && T.isForwardDecl()) 1428263508Sdim completeClassData(RD); 1429263508Sdim} 1430263508Sdim 1431263508Sdimvoid CGDebugInfo::completeClassData(const RecordDecl *RD) { 1432263508Sdim if (DebugKind <= CodeGenOptions::DebugLineTablesOnly) 1433263508Sdim return; 1434263508Sdim QualType Ty = CGM.getContext().getRecordType(RD); 1435263508Sdim void* TyPtr = Ty.getAsOpaquePtr(); 1436263508Sdim if (CompletedTypeCache.count(TyPtr)) 1437263508Sdim return; 1438263508Sdim llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>()); 1439263508Sdim assert(!Res.isForwardDecl()); 1440263508Sdim CompletedTypeCache[TyPtr] = Res; 1441263508Sdim TypeCache[TyPtr] = Res; 1442263508Sdim} 1443263508Sdim 1444202879Srdivacky/// CreateType - get structure or union type. 1445218893Sdimllvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) { 1446203955Srdivacky RecordDecl *RD = Ty->getDecl(); 1447263508Sdim const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 1448263508Sdim // Always emit declarations for types that aren't required to be complete when 1449263508Sdim // in limit-debug-info mode. If the type is later found to be required to be 1450263508Sdim // complete this declaration will be upgraded to a definition by 1451263508Sdim // `completeRequiredType`. 1452263508Sdim // If the type is dynamic, only emit the definition in TUs that require class 1453263508Sdim // data. This is handled by `completeClassData`. 1454263508Sdim llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0))); 1455263508Sdim // If we've already emitted the type, just use that, even if it's only a 1456263508Sdim // declaration. The completeType, completeRequiredType, and completeClassData 1457263508Sdim // callbacks will handle promoting the declaration to a definition. 1458263508Sdim if (T || 1459269011Semaste // Under -flimit-debug-info: 1460263508Sdim (DebugKind <= CodeGenOptions::LimitedDebugInfo && 1461269011Semaste // Emit only a forward declaration unless the type is required. 1462269011Semaste ((!RD->isCompleteDefinitionRequired() && CGM.getLangOpts().CPlusPlus) || 1463269011Semaste // If the class is dynamic, only emit a declaration. A definition will be 1464269011Semaste // emitted whenever the vtable is emitted. 1465269011Semaste (CXXDecl && CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())))) { 1466263508Sdim llvm::DIDescriptor FDContext = 1467263508Sdim getContextDescriptor(cast<Decl>(RD->getDeclContext())); 1468263508Sdim if (!T) 1469263508Sdim T = getOrCreateRecordFwdDecl(Ty, FDContext); 1470263508Sdim return T; 1471263508Sdim } 1472202879Srdivacky 1473263508Sdim return CreateTypeDefinition(Ty); 1474263508Sdim} 1475263508Sdim 1476263508Sdimllvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) { 1477263508Sdim RecordDecl *RD = Ty->getDecl(); 1478263508Sdim 1479202879Srdivacky // Get overall information about the record type for the debug info. 1480208600Srdivacky llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 1481202879Srdivacky 1482202879Srdivacky // Records and classes and unions can all be recursive. To handle them, we 1483202879Srdivacky // first generate a debug descriptor for the struct as a forward declaration. 1484202879Srdivacky // Then (if it is a definition) we go through and get debug info for all of 1485202879Srdivacky // its members. Finally, we create a descriptor for the complete type (which 1486202879Srdivacky // may refer to the forward decl if the struct is recursive) and replace all 1487202879Srdivacky // uses of the forward declaration with the final definition. 1488202879Srdivacky 1489263508Sdim llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit)); 1490263508Sdim assert(FwdDecl.isCompositeType() && 1491263508Sdim "The debug type of a RecordType should be a llvm::DICompositeType"); 1492210299Sed 1493234353Sdim if (FwdDecl.isForwardDecl()) 1494234353Sdim return FwdDecl; 1495210299Sed 1496263508Sdim if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) 1497263508Sdim CollectContainingType(CXXDecl, FwdDecl); 1498263508Sdim 1499205219Srdivacky // Push the struct on region stack. 1500249423Sdim LexicalBlockStack.push_back(&*FwdDecl); 1501208600Srdivacky RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); 1502202879Srdivacky 1503249423Sdim // Add this to the completed-type cache while we're completing it recursively. 1504234353Sdim CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 1505234353Sdim 1506202879Srdivacky // Convert all the elements. 1507226633Sdim SmallVector<llvm::Value *, 16> EltTys; 1508263508Sdim // what about nested types? 1509202879Srdivacky 1510234353Sdim // Note: The split of CXXDecl information here is intentional, the 1511234353Sdim // gdb tests will depend on a certain ordering at printout. The debug 1512234353Sdim // information offsets are still correct if we merge them all together 1513234353Sdim // though. 1514203955Srdivacky const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 1515203955Srdivacky if (CXXDecl) { 1516234353Sdim CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl); 1517234353Sdim CollectVTableInfo(CXXDecl, DefUnit, EltTys); 1518203955Srdivacky } 1519212904Sdim 1520249423Sdim // Collect data fields (including static variables and any initializers). 1521234353Sdim CollectRecordFields(RD, DefUnit, EltTys, FwdDecl); 1522263508Sdim if (CXXDecl) 1523234353Sdim CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl); 1524203955Srdivacky 1525226633Sdim LexicalBlockStack.pop_back(); 1526234353Sdim RegionMap.erase(Ty->getDecl()); 1527205219Srdivacky 1528221345Sdim llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); 1529263508Sdim FwdDecl.setTypeArray(Elements); 1530221345Sdim 1531249423Sdim RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); 1532249423Sdim return FwdDecl; 1533193326Sed} 1534193326Sed 1535208600Srdivacky/// CreateType - get objective-c object type. 1536208600Srdivackyllvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty, 1537208600Srdivacky llvm::DIFile Unit) { 1538208600Srdivacky // Ignore protocols. 1539208600Srdivacky return getOrCreateType(Ty->getBaseType(), Unit); 1540208600Srdivacky} 1541208600Srdivacky 1542263508Sdim 1543263508Sdim/// \return true if Getter has the default name for the property PD. 1544263508Sdimstatic bool hasDefaultGetterName(const ObjCPropertyDecl *PD, 1545263508Sdim const ObjCMethodDecl *Getter) { 1546263508Sdim assert(PD); 1547263508Sdim if (!Getter) 1548263508Sdim return true; 1549263508Sdim 1550263508Sdim assert(Getter->getDeclName().isObjCZeroArgSelector()); 1551263508Sdim return PD->getName() == 1552263508Sdim Getter->getDeclName().getObjCSelector().getNameForSlot(0); 1553263508Sdim} 1554263508Sdim 1555263508Sdim/// \return true if Setter has the default name for the property PD. 1556263508Sdimstatic bool hasDefaultSetterName(const ObjCPropertyDecl *PD, 1557263508Sdim const ObjCMethodDecl *Setter) { 1558263508Sdim assert(PD); 1559263508Sdim if (!Setter) 1560263508Sdim return true; 1561263508Sdim 1562263508Sdim assert(Setter->getDeclName().isObjCOneArgSelector()); 1563263508Sdim return SelectorTable::constructSetterName(PD->getName()) == 1564263508Sdim Setter->getDeclName().getObjCSelector().getNameForSlot(0); 1565263508Sdim} 1566263508Sdim 1567193326Sed/// CreateType - get objective-c interface type. 1568193326Sedllvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 1569204962Srdivacky llvm::DIFile Unit) { 1570203955Srdivacky ObjCInterfaceDecl *ID = Ty->getDecl(); 1571218893Sdim if (!ID) 1572218893Sdim return llvm::DIType(); 1573193326Sed 1574193326Sed // Get overall information about the record type for the debug info. 1575204962Srdivacky llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); 1576208600Srdivacky unsigned Line = getLineNumber(ID->getLocation()); 1577204962Srdivacky unsigned RuntimeLang = TheCU.getLanguage(); 1578193326Sed 1579226633Sdim // If this is just a forward declaration return a special forward-declaration 1580226633Sdim // debug type since we won't be able to lay out the entire type. 1581234353Sdim ObjCInterfaceDecl *Def = ID->getDefinition(); 1582234353Sdim if (!Def) { 1583218893Sdim llvm::DIType FwdDecl = 1584234353Sdim DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 1585249423Sdim ID->getName(), TheCU, DefUnit, Line, 1586249423Sdim RuntimeLang); 1587212904Sdim return FwdDecl; 1588212904Sdim } 1589212904Sdim 1590234353Sdim ID = Def; 1591198092Srdivacky 1592234353Sdim // Bit size, align and offset of the type. 1593234353Sdim uint64_t Size = CGM.getContext().getTypeSize(Ty); 1594234353Sdim uint64_t Align = CGM.getContext().getTypeAlign(Ty); 1595234353Sdim 1596234353Sdim unsigned Flags = 0; 1597234353Sdim if (ID->getImplementation()) 1598234353Sdim Flags |= llvm::DIDescriptor::FlagObjcClassComplete; 1599234353Sdim 1600249423Sdim llvm::DICompositeType RealDecl = 1601234353Sdim DBuilder.createStructType(Unit, ID->getName(), DefUnit, 1602234353Sdim Line, Size, Align, Flags, 1603249423Sdim llvm::DIType(), llvm::DIArray(), RuntimeLang); 1604234353Sdim 1605234353Sdim // Otherwise, insert it into the CompletedTypeCache so that recursive uses 1606234353Sdim // will find it and we're emitting the complete type. 1607249423Sdim QualType QualTy = QualType(Ty, 0); 1608249423Sdim CompletedTypeCache[QualTy.getAsOpaquePtr()] = RealDecl; 1609263508Sdim 1610205219Srdivacky // Push the struct on region stack. 1611249423Sdim LexicalBlockStack.push_back(static_cast<llvm::MDNode*>(RealDecl)); 1612234353Sdim RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl); 1613193326Sed 1614193326Sed // Convert all the elements. 1615226633Sdim SmallVector<llvm::Value *, 16> EltTys; 1616193326Sed 1617203955Srdivacky ObjCInterfaceDecl *SClass = ID->getSuperClass(); 1618193326Sed if (SClass) { 1619198092Srdivacky llvm::DIType SClassTy = 1620200583Srdivacky getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); 1621218893Sdim if (!SClassTy.isValid()) 1622218893Sdim return llvm::DIType(); 1623263508Sdim 1624198092Srdivacky llvm::DIType InhTag = 1625234353Sdim DBuilder.createInheritance(RealDecl, SClassTy, 0, 0); 1626193326Sed EltTys.push_back(InhTag); 1627193326Sed } 1628193326Sed 1629263508Sdim // Create entries for all of the properties. 1630234353Sdim for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(), 1631234353Sdim E = ID->prop_end(); I != E; ++I) { 1632234353Sdim const ObjCPropertyDecl *PD = *I; 1633234353Sdim SourceLocation Loc = PD->getLocation(); 1634234353Sdim llvm::DIFile PUnit = getOrCreateFile(Loc); 1635234353Sdim unsigned PLine = getLineNumber(Loc); 1636234353Sdim ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); 1637234353Sdim ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); 1638234353Sdim llvm::MDNode *PropertyNode = 1639234353Sdim DBuilder.createObjCProperty(PD->getName(), 1640249423Sdim PUnit, PLine, 1641263508Sdim hasDefaultGetterName(PD, Getter) ? "" : 1642234353Sdim getSelectorName(PD->getGetterName()), 1643263508Sdim hasDefaultSetterName(PD, Setter) ? "" : 1644234353Sdim getSelectorName(PD->getSetterName()), 1645234353Sdim PD->getPropertyAttributes(), 1646249423Sdim getOrCreateType(PD->getType(), PUnit)); 1647234353Sdim EltTys.push_back(PropertyNode); 1648234353Sdim } 1649234353Sdim 1650203955Srdivacky const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); 1651193326Sed unsigned FieldNo = 0; 1652218893Sdim for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; 1653218893Sdim Field = Field->getNextIvar(), ++FieldNo) { 1654193326Sed llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 1655218893Sdim if (!FieldTy.isValid()) 1656218893Sdim return llvm::DIType(); 1657263508Sdim 1658226633Sdim StringRef FieldName = Field->getName(); 1659193326Sed 1660193326Sed // Ignore unnamed fields. 1661199990Srdivacky if (FieldName.empty()) 1662193326Sed continue; 1663193326Sed 1664193326Sed // Get the location for the field. 1665208600Srdivacky llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); 1666208600Srdivacky unsigned FieldLine = getLineNumber(Field->getLocation()); 1667193326Sed QualType FType = Field->getType(); 1668193326Sed uint64_t FieldSize = 0; 1669193326Sed unsigned FieldAlign = 0; 1670193326Sed 1671193326Sed if (!FType->isIncompleteArrayType()) { 1672198092Srdivacky 1673193326Sed // Bit size, align and offset of the type. 1674226633Sdim FieldSize = Field->isBitField() 1675263508Sdim ? Field->getBitWidthValue(CGM.getContext()) 1676263508Sdim : CGM.getContext().getTypeSize(FType); 1677226633Sdim FieldAlign = CGM.getContext().getTypeAlign(FType); 1678193326Sed } 1679193326Sed 1680243830Sdim uint64_t FieldOffset; 1681243830Sdim if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { 1682243830Sdim // We don't know the runtime offset of an ivar if we're using the 1683243830Sdim // non-fragile ABI. For bitfields, use the bit offset into the first 1684243830Sdim // byte of storage of the bitfield. For other fields, use zero. 1685243830Sdim if (Field->isBitField()) { 1686243830Sdim FieldOffset = CGM.getObjCRuntime().ComputeBitfieldBitOffset( 1687243830Sdim CGM, ID, Field); 1688243830Sdim FieldOffset %= CGM.getContext().getCharWidth(); 1689243830Sdim } else { 1690243830Sdim FieldOffset = 0; 1691243830Sdim } 1692243830Sdim } else { 1693243830Sdim FieldOffset = RL.getFieldOffset(FieldNo); 1694243830Sdim } 1695198092Srdivacky 1696193326Sed unsigned Flags = 0; 1697193326Sed if (Field->getAccessControl() == ObjCIvarDecl::Protected) 1698218893Sdim Flags = llvm::DIDescriptor::FlagProtected; 1699193326Sed else if (Field->getAccessControl() == ObjCIvarDecl::Private) 1700218893Sdim Flags = llvm::DIDescriptor::FlagPrivate; 1701198092Srdivacky 1702234353Sdim llvm::MDNode *PropertyNode = NULL; 1703234353Sdim if (ObjCImplementationDecl *ImpD = ID->getImplementation()) { 1704263508Sdim if (ObjCPropertyImplDecl *PImpD = 1705234353Sdim ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { 1706234353Sdim if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { 1707249423Sdim SourceLocation Loc = PD->getLocation(); 1708249423Sdim llvm::DIFile PUnit = getOrCreateFile(Loc); 1709249423Sdim unsigned PLine = getLineNumber(Loc); 1710234353Sdim ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); 1711234353Sdim ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); 1712234353Sdim PropertyNode = 1713234353Sdim DBuilder.createObjCProperty(PD->getName(), 1714234353Sdim PUnit, PLine, 1715263508Sdim hasDefaultGetterName(PD, Getter) ? "" : 1716234353Sdim getSelectorName(PD->getGetterName()), 1717263508Sdim hasDefaultSetterName(PD, Setter) ? "" : 1718234353Sdim getSelectorName(PD->getSetterName()), 1719234353Sdim PD->getPropertyAttributes(), 1720234353Sdim getOrCreateType(PD->getType(), PUnit)); 1721234353Sdim } 1722234353Sdim } 1723234353Sdim } 1724221345Sdim FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, 1725221345Sdim FieldLine, FieldSize, FieldAlign, 1726221345Sdim FieldOffset, Flags, FieldTy, 1727234353Sdim PropertyNode); 1728193326Sed EltTys.push_back(FieldTy); 1729193326Sed } 1730198092Srdivacky 1731221345Sdim llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); 1732249423Sdim RealDecl.setTypeArray(Elements); 1733249423Sdim 1734249423Sdim // If the implementation is not yet set, we do not want to mark it 1735249423Sdim // as complete. An implementation may declare additional 1736249423Sdim // private ivars that we would miss otherwise. 1737249423Sdim if (ID->getImplementation() == 0) 1738249423Sdim CompletedTypeCache.erase(QualTy.getAsOpaquePtr()); 1739263508Sdim 1740226633Sdim LexicalBlockStack.pop_back(); 1741249423Sdim return RealDecl; 1742193326Sed} 1743193326Sed 1744234353Sdimllvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) { 1745204643Srdivacky llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); 1746249423Sdim int64_t Count = Ty->getNumElements(); 1747249423Sdim if (Count == 0) 1748221345Sdim // If number of elements are not known then this is an unbounded array. 1749249423Sdim // Use Count == -1 to express such arrays. 1750249423Sdim Count = -1; 1751204643Srdivacky 1752249423Sdim llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count); 1753221345Sdim llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); 1754204643Srdivacky 1755204643Srdivacky uint64_t Size = CGM.getContext().getTypeSize(Ty); 1756204643Srdivacky uint64_t Align = CGM.getContext().getTypeAlign(Ty); 1757204643Srdivacky 1758249423Sdim return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); 1759204643Srdivacky} 1760204643Srdivacky 1761193326Sedllvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 1762204962Srdivacky llvm::DIFile Unit) { 1763193326Sed uint64_t Size; 1764193326Sed uint64_t Align; 1765198092Srdivacky 1766193326Sed // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 1767193326Sed if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 1768193326Sed Size = 0; 1769193326Sed Align = 1770200583Srdivacky CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); 1771193326Sed } else if (Ty->isIncompleteArrayType()) { 1772193326Sed Size = 0; 1773239462Sdim if (Ty->getElementType()->isIncompleteType()) 1774239462Sdim Align = 0; 1775239462Sdim else 1776239462Sdim Align = CGM.getContext().getTypeAlign(Ty->getElementType()); 1777263508Sdim } else if (Ty->isIncompleteType()) { 1778221345Sdim Size = 0; 1779221345Sdim Align = 0; 1780193326Sed } else { 1781193326Sed // Size and align of the whole array, not the element type. 1782200583Srdivacky Size = CGM.getContext().getTypeSize(Ty); 1783200583Srdivacky Align = CGM.getContext().getTypeAlign(Ty); 1784193326Sed } 1785198092Srdivacky 1786193326Sed // Add the dimensions of the array. FIXME: This loses CV qualifiers from 1787193326Sed // interior arrays, do we care? Why aren't nested arrays represented the 1788193326Sed // obvious/recursive way? 1789226633Sdim SmallVector<llvm::Value *, 8> Subscripts; 1790193326Sed QualType EltTy(Ty, 0); 1791236149Sdim while ((Ty = dyn_cast<ArrayType>(EltTy))) { 1792249423Sdim // If the number of elements is known, then count is that number. Otherwise, 1793249423Sdim // it's -1. This allows us to represent a subrange with an array of 0 1794249423Sdim // elements, like this: 1795249423Sdim // 1796249423Sdim // struct foo { 1797249423Sdim // int x[0]; 1798249423Sdim // }; 1799249423Sdim int64_t Count = -1; // Count == -1 is an unbounded array. 1800249423Sdim if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 1801249423Sdim Count = CAT->getSize().getZExtValue(); 1802263508Sdim 1803236149Sdim // FIXME: Verify this is right for VLAs. 1804249423Sdim Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count)); 1805193326Sed EltTy = Ty->getElementType(); 1806193326Sed } 1807198092Srdivacky 1808221345Sdim llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); 1809193326Sed 1810263508Sdim llvm::DIType DbgTy = 1811219077Sdim DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), 1812218893Sdim SubscriptArray); 1813198398Srdivacky return DbgTy; 1814193326Sed} 1815193326Sed 1816263508Sdimllvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, 1817204962Srdivacky llvm::DIFile Unit) { 1818263508Sdim return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, 1819199482Srdivacky Ty, Ty->getPointeeType(), Unit); 1820199482Srdivacky} 1821193326Sed 1822263508Sdimllvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty, 1823218893Sdim llvm::DIFile Unit) { 1824263508Sdim return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, 1825218893Sdim Ty, Ty->getPointeeType(), Unit); 1826218893Sdim} 1827218893Sdim 1828263508Sdimllvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, 1829204962Srdivacky llvm::DIFile U) { 1830249423Sdim llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U); 1831249423Sdim if (!Ty->getPointeeType()->isFunctionType()) 1832249423Sdim return DBuilder.createMemberPointerType( 1833263508Sdim getOrCreateType(Ty->getPointeeType(), U), ClassType); 1834249423Sdim return DBuilder.createMemberPointerType(getOrCreateInstanceMethodType( 1835249423Sdim CGM.getContext().getPointerType( 1836249423Sdim QualType(Ty->getClass(), Ty->getPointeeType().getCVRQualifiers())), 1837249423Sdim Ty->getPointeeType()->getAs<FunctionProtoType>(), U), 1838249423Sdim ClassType); 1839200583Srdivacky} 1840200583Srdivacky 1841263508Sdimllvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, 1842226633Sdim llvm::DIFile U) { 1843226633Sdim // Ignore the atomic wrapping 1844226633Sdim // FIXME: What is the correct representation? 1845226633Sdim return getOrCreateType(Ty->getValueType(), U); 1846226633Sdim} 1847226633Sdim 1848212904Sdim/// CreateEnumType - get enumeration type. 1849263508Sdimllvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) { 1850263508Sdim const EnumDecl *ED = Ty->getDecl(); 1851243830Sdim uint64_t Size = 0; 1852243830Sdim uint64_t Align = 0; 1853243830Sdim if (!ED->getTypeForDecl()->isIncompleteType()) { 1854243830Sdim Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); 1855243830Sdim Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl()); 1856243830Sdim } 1857212904Sdim 1858263508Sdim SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); 1859263508Sdim 1860243830Sdim // If this is just a forward declaration, construct an appropriately 1861243830Sdim // marked node and just return it. 1862243830Sdim if (!ED->getDefinition()) { 1863243830Sdim llvm::DIDescriptor EDContext; 1864243830Sdim EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext())); 1865243830Sdim llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); 1866243830Sdim unsigned Line = getLineNumber(ED->getLocation()); 1867243830Sdim StringRef EDName = ED->getName(); 1868243830Sdim return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type, 1869243830Sdim EDName, EDContext, DefUnit, Line, 0, 1870263508Sdim Size, Align, FullName); 1871243830Sdim } 1872243830Sdim 1873212904Sdim // Create DIEnumerator elements for each enumerator. 1874243830Sdim SmallVector<llvm::Value *, 16> Enumerators; 1875243830Sdim ED = ED->getDefinition(); 1876212904Sdim for (EnumDecl::enumerator_iterator 1877212904Sdim Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); 1878212904Sdim Enum != EnumEnd; ++Enum) { 1879218893Sdim Enumerators.push_back( 1880219077Sdim DBuilder.createEnumerator(Enum->getName(), 1881263508Sdim Enum->getInitVal().getSExtValue())); 1882212904Sdim } 1883212904Sdim 1884212904Sdim // Return a CompositeType for the enum itself. 1885221345Sdim llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators); 1886212904Sdim 1887212904Sdim llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); 1888212904Sdim unsigned Line = getLineNumber(ED->getLocation()); 1889263508Sdim llvm::DIDescriptor EnumContext = 1890219077Sdim getContextDescriptor(cast<Decl>(ED->getDeclContext())); 1891251662Sdim llvm::DIType ClassTy = ED->isFixed() ? 1892239462Sdim getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType(); 1893263508Sdim llvm::DIType DbgTy = 1894219077Sdim DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line, 1895239462Sdim Size, Align, EltArray, 1896263508Sdim ClassTy, FullName); 1897212904Sdim return DbgTy; 1898212904Sdim} 1899212904Sdim 1900249423Sdimstatic QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { 1901249423Sdim Qualifiers Quals; 1902201361Srdivacky do { 1903263508Sdim Qualifiers InnerQuals = T.getLocalQualifiers(); 1904263508Sdim // Qualifiers::operator+() doesn't like it if you add a Qualifier 1905263508Sdim // that is already there. 1906263508Sdim Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals); 1907263508Sdim Quals += InnerQuals; 1908201361Srdivacky QualType LastT = T; 1909201361Srdivacky switch (T->getTypeClass()) { 1910201361Srdivacky default: 1911249423Sdim return C.getQualifiedType(T.getTypePtr(), Quals); 1912201361Srdivacky case Type::TemplateSpecialization: 1913201361Srdivacky T = cast<TemplateSpecializationType>(T)->desugar(); 1914201361Srdivacky break; 1915218893Sdim case Type::TypeOfExpr: 1916218893Sdim T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); 1917201361Srdivacky break; 1918201361Srdivacky case Type::TypeOf: 1919201361Srdivacky T = cast<TypeOfType>(T)->getUnderlyingType(); 1920201361Srdivacky break; 1921201361Srdivacky case Type::Decltype: 1922201361Srdivacky T = cast<DecltypeType>(T)->getUnderlyingType(); 1923201361Srdivacky break; 1924223017Sdim case Type::UnaryTransform: 1925223017Sdim T = cast<UnaryTransformType>(T)->getUnderlyingType(); 1926223017Sdim break; 1927218893Sdim case Type::Attributed: 1928218893Sdim T = cast<AttributedType>(T)->getEquivalentType(); 1929221345Sdim break; 1930208600Srdivacky case Type::Elaborated: 1931208600Srdivacky T = cast<ElaboratedType>(T)->getNamedType(); 1932201361Srdivacky break; 1933218893Sdim case Type::Paren: 1934218893Sdim T = cast<ParenType>(T)->getInnerType(); 1935218893Sdim break; 1936249423Sdim case Type::SubstTemplateTypeParm: 1937201361Srdivacky T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); 1938201361Srdivacky break; 1939221345Sdim case Type::Auto: 1940263508Sdim QualType DT = cast<AutoType>(T)->getDeducedType(); 1941263508Sdim if (DT.isNull()) 1942263508Sdim return T; 1943263508Sdim T = DT; 1944221345Sdim break; 1945201361Srdivacky } 1946263508Sdim 1947201361Srdivacky assert(T != LastT && "Type unwrapping failed to unwrap!"); 1948249423Sdim (void)LastT; 1949201361Srdivacky } while (true); 1950199482Srdivacky} 1951199482Srdivacky 1952263508Sdim/// getType - Get the type from the cache or return null type if it doesn't 1953263508Sdim/// exist. 1954234353Sdimllvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) { 1955193326Sed 1956201361Srdivacky // Unwrap the type as needed for debug information. 1957249423Sdim Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); 1958263508Sdim 1959198092Srdivacky // Check for existing entry. 1960249423Sdim if (Ty->getTypeClass() == Type::ObjCInterface) { 1961249423Sdim llvm::Value *V = getCachedInterfaceTypeOrNull(Ty); 1962249423Sdim if (V) 1963249423Sdim return llvm::DIType(cast<llvm::MDNode>(V)); 1964249423Sdim else return llvm::DIType(); 1965249423Sdim } 1966249423Sdim 1967206084Srdivacky llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 1968198092Srdivacky TypeCache.find(Ty.getAsOpaquePtr()); 1969198092Srdivacky if (it != TypeCache.end()) { 1970198092Srdivacky // Verify that the debug info still exists. 1971239462Sdim if (llvm::Value *V = it->second) 1972239462Sdim return llvm::DIType(cast<llvm::MDNode>(V)); 1973198092Srdivacky } 1974193326Sed 1975234353Sdim return llvm::DIType(); 1976234353Sdim} 1977234353Sdim 1978234353Sdim/// getCompletedTypeOrNull - Get the type from the cache or return null if it 1979234353Sdim/// doesn't exist. 1980234353Sdimllvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) { 1981234353Sdim 1982234353Sdim // Unwrap the type as needed for debug information. 1983249423Sdim Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); 1984234353Sdim 1985234353Sdim // Check for existing entry. 1986249423Sdim llvm::Value *V = 0; 1987234353Sdim llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 1988234353Sdim CompletedTypeCache.find(Ty.getAsOpaquePtr()); 1989249423Sdim if (it != CompletedTypeCache.end()) 1990249423Sdim V = it->second; 1991249423Sdim else { 1992249423Sdim V = getCachedInterfaceTypeOrNull(Ty); 1993234353Sdim } 1994234353Sdim 1995249423Sdim // Verify that any cached debug info still exists. 1996263508Sdim return llvm::DIType(cast_or_null<llvm::MDNode>(V)); 1997234353Sdim} 1998234353Sdim 1999249423Sdim/// getCachedInterfaceTypeOrNull - Get the type from the interface 2000249423Sdim/// cache, unless it needs to regenerated. Otherwise return null. 2001249423Sdimllvm::Value *CGDebugInfo::getCachedInterfaceTypeOrNull(QualType Ty) { 2002249423Sdim // Is there a cached interface that hasn't changed? 2003249423Sdim llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > > 2004249423Sdim ::iterator it1 = ObjCInterfaceCache.find(Ty.getAsOpaquePtr()); 2005234353Sdim 2006249423Sdim if (it1 != ObjCInterfaceCache.end()) 2007249423Sdim if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty)) 2008249423Sdim if (Checksum(Decl) == it1->second.second) 2009249423Sdim // Return cached forward declaration. 2010249423Sdim return it1->second.first; 2011249423Sdim 2012249423Sdim return 0; 2013249423Sdim} 2014249423Sdim 2015234353Sdim/// getOrCreateType - Get the type from the cache or create a new 2016234353Sdim/// one if necessary. 2017234353Sdimllvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) { 2018234353Sdim if (Ty.isNull()) 2019234353Sdim return llvm::DIType(); 2020234353Sdim 2021234353Sdim // Unwrap the type as needed for debug information. 2022249423Sdim Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); 2023239462Sdim 2024263508Sdim if (llvm::DIType T = getCompletedTypeOrNull(Ty)) 2025239462Sdim return T; 2026234353Sdim 2027198092Srdivacky // Otherwise create the type. 2028198092Srdivacky llvm::DIType Res = CreateTypeNode(Ty, Unit); 2029249423Sdim void* TyPtr = Ty.getAsOpaquePtr(); 2030199482Srdivacky 2031249423Sdim // And update the type cache. 2032249423Sdim TypeCache[TyPtr] = Res; 2033249423Sdim 2034263508Sdim // FIXME: this getTypeOrNull call seems silly when we just inserted the type 2035263508Sdim // into the cache - but getTypeOrNull has a special case for cached interface 2036263508Sdim // types. We should probably just pull that out as a special case for the 2037263508Sdim // "else" block below & skip the otherwise needless lookup. 2038234353Sdim llvm::DIType TC = getTypeOrNull(Ty); 2039263508Sdim if (TC && TC.isForwardDecl()) 2040249423Sdim ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC))); 2041249423Sdim else if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty)) { 2042249423Sdim // Interface types may have elements added to them by a 2043249423Sdim // subsequent implementation or extension, so we keep them in 2044249423Sdim // the ObjCInterfaceCache together with a checksum. Instead of 2045263508Sdim // the (possibly) incomplete interface type, we return a forward 2046249423Sdim // declaration that gets RAUW'd in CGDebugInfo::finalize(). 2047263508Sdim std::pair<llvm::WeakVH, unsigned> &V = ObjCInterfaceCache[TyPtr]; 2048263508Sdim if (V.first) 2049263508Sdim return llvm::DIType(cast<llvm::MDNode>(V.first)); 2050263508Sdim TC = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 2051263508Sdim Decl->getName(), TheCU, Unit, 2052263508Sdim getLineNumber(Decl->getLocation()), 2053263508Sdim TheCU.getLanguage()); 2054249423Sdim // Store the forward declaration in the cache. 2055263508Sdim V.first = TC; 2056263508Sdim V.second = Checksum(Decl); 2057234353Sdim 2058249423Sdim // Register the type for replacement in finalize(). 2059249423Sdim ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC))); 2060263508Sdim 2061249423Sdim return TC; 2062249423Sdim } 2063249423Sdim 2064234353Sdim if (!Res.isForwardDecl()) 2065249423Sdim CompletedTypeCache[TyPtr] = Res; 2066239462Sdim 2067198092Srdivacky return Res; 2068198092Srdivacky} 2069198092Srdivacky 2070263508Sdim/// Currently the checksum of an interface includes the number of 2071263508Sdim/// ivars and property accessors. 2072263508Sdimunsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) { 2073263508Sdim // The assumption is that the number of ivars can only increase 2074263508Sdim // monotonically, so it is safe to just use their current number as 2075263508Sdim // a checksum. 2076263508Sdim unsigned Sum = 0; 2077263508Sdim for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin(); 2078263508Sdim Ivar != 0; Ivar = Ivar->getNextIvar()) 2079263508Sdim ++Sum; 2080263508Sdim 2081263508Sdim return Sum; 2082249423Sdim} 2083249423Sdim 2084249423SdimObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) { 2085249423Sdim switch (Ty->getTypeClass()) { 2086249423Sdim case Type::ObjCObjectPointer: 2087263508Sdim return getObjCInterfaceDecl(cast<ObjCObjectPointerType>(Ty) 2088263508Sdim ->getPointeeType()); 2089249423Sdim case Type::ObjCInterface: 2090249423Sdim return cast<ObjCInterfaceType>(Ty)->getDecl(); 2091249423Sdim default: 2092249423Sdim return 0; 2093249423Sdim } 2094249423Sdim} 2095249423Sdim 2096199482Srdivacky/// CreateTypeNode - Create a new debug type node. 2097234353Sdimllvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) { 2098198092Srdivacky // Handle qualifiers, which recursively handles what they refer to. 2099199482Srdivacky if (Ty.hasLocalQualifiers()) 2100198092Srdivacky return CreateQualifiedType(Ty, Unit); 2101198092Srdivacky 2102201361Srdivacky const char *Diag = 0; 2103263508Sdim 2104193326Sed // Work out details of type. 2105193326Sed switch (Ty->getTypeClass()) { 2106193326Sed#define TYPE(Class, Base) 2107193326Sed#define ABSTRACT_TYPE(Class, Base) 2108193326Sed#define NON_CANONICAL_TYPE(Class, Base) 2109193326Sed#define DEPENDENT_TYPE(Class, Base) case Type::Class: 2110193326Sed#include "clang/AST/TypeNodes.def" 2111226633Sdim llvm_unreachable("Dependent types cannot show up in debug information"); 2112198092Srdivacky 2113199482Srdivacky case Type::ExtVector: 2114193326Sed case Type::Vector: 2115204643Srdivacky return CreateType(cast<VectorType>(Ty), Unit); 2116198092Srdivacky case Type::ObjCObjectPointer: 2117198092Srdivacky return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); 2118208600Srdivacky case Type::ObjCObject: 2119208600Srdivacky return CreateType(cast<ObjCObjectType>(Ty), Unit); 2120198092Srdivacky case Type::ObjCInterface: 2121198092Srdivacky return CreateType(cast<ObjCInterfaceType>(Ty), Unit); 2122234353Sdim case Type::Builtin: 2123234353Sdim return CreateType(cast<BuiltinType>(Ty)); 2124234353Sdim case Type::Complex: 2125234353Sdim return CreateType(cast<ComplexType>(Ty)); 2126234353Sdim case Type::Pointer: 2127234353Sdim return CreateType(cast<PointerType>(Ty), Unit); 2128263508Sdim case Type::Decayed: 2129263508Sdim // Decayed types are just pointers in LLVM and DWARF. 2130263508Sdim return CreateType( 2131263508Sdim cast<PointerType>(cast<DecayedType>(Ty)->getDecayedType()), Unit); 2132193326Sed case Type::BlockPointer: 2133198092Srdivacky return CreateType(cast<BlockPointerType>(Ty), Unit); 2134234353Sdim case Type::Typedef: 2135234353Sdim return CreateType(cast<TypedefType>(Ty), Unit); 2136193326Sed case Type::Record: 2137234353Sdim return CreateType(cast<RecordType>(Ty)); 2138193326Sed case Type::Enum: 2139263508Sdim return CreateEnumType(cast<EnumType>(Ty)); 2140193326Sed case Type::FunctionProto: 2141193326Sed case Type::FunctionNoProto: 2142198092Srdivacky return CreateType(cast<FunctionType>(Ty), Unit); 2143193326Sed case Type::ConstantArray: 2144193326Sed case Type::VariableArray: 2145193326Sed case Type::IncompleteArray: 2146198092Srdivacky return CreateType(cast<ArrayType>(Ty), Unit); 2147199482Srdivacky 2148199482Srdivacky case Type::LValueReference: 2149199482Srdivacky return CreateType(cast<LValueReferenceType>(Ty), Unit); 2150218893Sdim case Type::RValueReference: 2151218893Sdim return CreateType(cast<RValueReferenceType>(Ty), Unit); 2152199482Srdivacky 2153200583Srdivacky case Type::MemberPointer: 2154200583Srdivacky return CreateType(cast<MemberPointerType>(Ty), Unit); 2155201361Srdivacky 2156226633Sdim case Type::Atomic: 2157226633Sdim return CreateType(cast<AtomicType>(Ty), Unit); 2158226633Sdim 2159218893Sdim case Type::Attributed: 2160201361Srdivacky case Type::TemplateSpecialization: 2161201361Srdivacky case Type::Elaborated: 2162218893Sdim case Type::Paren: 2163201361Srdivacky case Type::SubstTemplateTypeParm: 2164201361Srdivacky case Type::TypeOfExpr: 2165201361Srdivacky case Type::TypeOf: 2166201361Srdivacky case Type::Decltype: 2167223017Sdim case Type::UnaryTransform: 2168263508Sdim case Type::PackExpansion: 2169263508Sdim llvm_unreachable("type should have been unwrapped!"); 2170218893Sdim case Type::Auto: 2171263508Sdim Diag = "auto"; 2172263508Sdim break; 2173193326Sed } 2174263508Sdim 2175201361Srdivacky assert(Diag && "Fall through without a diagnostic?"); 2176226633Sdim unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error, 2177201361Srdivacky "debug information for %0 is not yet supported"); 2178218893Sdim CGM.getDiags().Report(DiagID) 2179201361Srdivacky << Diag; 2180201361Srdivacky return llvm::DIType(); 2181193326Sed} 2182193326Sed 2183234353Sdim/// getOrCreateLimitedType - Get the type from the cache or create a new 2184234353Sdim/// limited type if necessary. 2185263508Sdimllvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty, 2186249423Sdim llvm::DIFile Unit) { 2187263508Sdim QualType QTy(Ty, 0); 2188234353Sdim 2189263508Sdim llvm::DICompositeType T(getTypeOrNull(QTy)); 2190234353Sdim 2191234353Sdim // We may have cached a forward decl when we could have created 2192234353Sdim // a non-forward decl. Go ahead and create a non-forward decl 2193234353Sdim // now. 2194263508Sdim if (T && !T.isForwardDecl()) return T; 2195234353Sdim 2196234353Sdim // Otherwise create the type. 2197263508Sdim llvm::DICompositeType Res = CreateLimitedType(Ty); 2198234353Sdim 2199263508Sdim // Propagate members from the declaration to the definition 2200263508Sdim // CreateType(const RecordType*) will overwrite this with the members in the 2201263508Sdim // correct order if the full type is needed. 2202263508Sdim Res.setTypeArray(T.getTypeArray()); 2203234353Sdim 2204263508Sdim if (T && T.isForwardDecl()) 2205263508Sdim ReplaceMap.push_back( 2206263508Sdim std::make_pair(QTy.getAsOpaquePtr(), static_cast<llvm::Value *>(T))); 2207263508Sdim 2208234353Sdim // And update the type cache. 2209263508Sdim TypeCache[QTy.getAsOpaquePtr()] = Res; 2210234353Sdim return Res; 2211234353Sdim} 2212234353Sdim 2213234353Sdim// TODO: Currently used for context chains when limiting debug info. 2214263508Sdimllvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) { 2215234353Sdim RecordDecl *RD = Ty->getDecl(); 2216263508Sdim 2217234353Sdim // Get overall information about the record type for the debug info. 2218234353Sdim llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 2219234353Sdim unsigned Line = getLineNumber(RD->getLocation()); 2220243830Sdim StringRef RDName = getClassName(RD); 2221234353Sdim 2222263508Sdim llvm::DIDescriptor RDContext = 2223263508Sdim getContextDescriptor(cast<Decl>(RD->getDeclContext())); 2224234353Sdim 2225263508Sdim // If we ended up creating the type during the context chain construction, 2226263508Sdim // just return that. 2227263508Sdim // FIXME: this could be dealt with better if the type was recorded as 2228263508Sdim // completed before we started this (see the CompletedTypeCache usage in 2229263508Sdim // CGDebugInfo::CreateTypeDefinition(const RecordType*) - that would need to 2230263508Sdim // be pushed to before context creation, but after it was known to be 2231263508Sdim // destined for completion (might still have an issue if this caller only 2232263508Sdim // required a declaration but the context construction ended up creating a 2233263508Sdim // definition) 2234263508Sdim llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD))); 2235263508Sdim if (T && (!T.isForwardDecl() || !RD->getDefinition())) 2236263508Sdim return T; 2237263508Sdim 2238271414Semaste // If this is just a forward or incomplete declaration, construct an 2239271414Semaste // appropriately marked node and just return it. 2240271414Semaste const RecordDecl *D = RD->getDefinition(); 2241271414Semaste if (!D || !D->isCompleteDefinition()) 2242263508Sdim return getOrCreateRecordFwdDecl(Ty, RDContext); 2243234353Sdim 2244234353Sdim uint64_t Size = CGM.getContext().getTypeSize(Ty); 2245234353Sdim uint64_t Align = CGM.getContext().getTypeAlign(Ty); 2246249423Sdim llvm::DICompositeType RealDecl; 2247263508Sdim 2248263508Sdim SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); 2249263508Sdim 2250234353Sdim if (RD->isUnion()) 2251234353Sdim RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, 2252263508Sdim Size, Align, 0, llvm::DIArray(), 0, 2253263508Sdim FullName); 2254243830Sdim else if (RD->isClass()) { 2255234353Sdim // FIXME: This could be a struct type giving a default visibility different 2256234353Sdim // than C++ class type, but needs llvm metadata changes first. 2257234353Sdim RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line, 2258249423Sdim Size, Align, 0, 0, llvm::DIType(), 2259249423Sdim llvm::DIArray(), llvm::DIType(), 2260263508Sdim llvm::DIArray(), FullName); 2261234353Sdim } else 2262234353Sdim RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line, 2263263508Sdim Size, Align, 0, llvm::DIType(), 2264263508Sdim llvm::DIArray(), 0, llvm::DIType(), 2265263508Sdim FullName); 2266234353Sdim 2267234353Sdim RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl); 2268249423Sdim TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl; 2269234353Sdim 2270263508Sdim if (const ClassTemplateSpecializationDecl *TSpecial = 2271263508Sdim dyn_cast<ClassTemplateSpecializationDecl>(RD)) 2272263508Sdim RealDecl.setTypeArray(llvm::DIArray(), 2273263508Sdim CollectCXXTemplateParams(TSpecial, DefUnit)); 2274263508Sdim return RealDecl; 2275234353Sdim} 2276234353Sdim 2277263508Sdimvoid CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD, 2278263508Sdim llvm::DICompositeType RealDecl) { 2279263508Sdim // A class's primary base or the class itself contains the vtable. 2280263508Sdim llvm::DICompositeType ContainingType; 2281263508Sdim const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 2282263508Sdim if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { 2283263508Sdim // Seek non virtual primary base root. 2284263508Sdim while (1) { 2285263508Sdim const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); 2286263508Sdim const CXXRecordDecl *PBT = BRL.getPrimaryBase(); 2287263508Sdim if (PBT && !BRL.isPrimaryBaseVirtual()) 2288263508Sdim PBase = PBT; 2289263508Sdim else 2290263508Sdim break; 2291263508Sdim } 2292263508Sdim ContainingType = llvm::DICompositeType( 2293263508Sdim getOrCreateType(QualType(PBase->getTypeForDecl(), 0), 2294263508Sdim getOrCreateFile(RD->getLocation()))); 2295263508Sdim } else if (RD->isDynamicClass()) 2296263508Sdim ContainingType = RealDecl; 2297234353Sdim 2298263508Sdim RealDecl.setContainingType(ContainingType); 2299234353Sdim} 2300234353Sdim 2301207619Srdivacky/// CreateMemberType - Create new member and increase Offset by FType's size. 2302207619Srdivackyllvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, 2303226633Sdim StringRef Name, 2304207619Srdivacky uint64_t *Offset) { 2305207619Srdivacky llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 2306207619Srdivacky uint64_t FieldSize = CGM.getContext().getTypeSize(FType); 2307207619Srdivacky unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); 2308224145Sdim llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, 2309218893Sdim FieldSize, FieldAlign, 2310218893Sdim *Offset, 0, FieldTy); 2311207619Srdivacky *Offset += FieldSize; 2312207619Srdivacky return Ty; 2313207619Srdivacky} 2314207619Srdivacky 2315263508Sdimllvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) { 2316263508Sdim // We only need a declaration (not a definition) of the type - so use whatever 2317263508Sdim // we would otherwise do to get a type for a pointee. (forward declarations in 2318263508Sdim // limited debug info, full definitions (if the type definition is available) 2319263508Sdim // in unlimited debug info) 2320263508Sdim if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 2321263508Sdim return getOrCreateType(CGM.getContext().getTypeDeclType(TD), 2322263508Sdim getOrCreateFile(TD->getLocation())); 2323263508Sdim // Otherwise fall back to a fairly rudimentary cache of existing declarations. 2324263508Sdim // This doesn't handle providing declarations (for functions or variables) for 2325263508Sdim // entities without definitions in this TU, nor when the definition proceeds 2326263508Sdim // the call to this function. 2327263508Sdim // FIXME: This should be split out into more specific maps with support for 2328263508Sdim // emitting forward declarations and merging definitions with declarations, 2329263508Sdim // the same way as we do for types. 2330263508Sdim llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I = 2331263508Sdim DeclCache.find(D->getCanonicalDecl()); 2332263508Sdim if (I == DeclCache.end()) 2333263508Sdim return llvm::DIDescriptor(); 2334263508Sdim llvm::Value *V = I->second; 2335263508Sdim return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V)); 2336263508Sdim} 2337263508Sdim 2338221345Sdim/// getFunctionDeclaration - Return debug info descriptor to describe method 2339221345Sdim/// declaration for the given method definition. 2340221345Sdimllvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) { 2341263508Sdim if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly) 2342263508Sdim return llvm::DISubprogram(); 2343263508Sdim 2344221345Sdim const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 2345221345Sdim if (!FD) return llvm::DISubprogram(); 2346221345Sdim 2347221345Sdim // Setup context. 2348263508Sdim llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext())); 2349221345Sdim 2350221345Sdim llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 2351234353Sdim MI = SPCache.find(FD->getCanonicalDecl()); 2352263508Sdim if (MI == SPCache.end()) { 2353263508Sdim if (const CXXMethodDecl *MD = 2354263508Sdim dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) { 2355263508Sdim llvm::DICompositeType T(S); 2356263508Sdim llvm::DISubprogram SP = 2357263508Sdim CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T); 2358263508Sdim T.addMember(SP); 2359263508Sdim return SP; 2360263508Sdim } 2361263508Sdim } 2362221345Sdim if (MI != SPCache.end()) { 2363239462Sdim llvm::Value *V = MI->second; 2364239462Sdim llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V)); 2365263508Sdim if (SP.isSubprogram() && !SP.isDefinition()) 2366221345Sdim return SP; 2367221345Sdim } 2368221345Sdim 2369221345Sdim for (FunctionDecl::redecl_iterator I = FD->redecls_begin(), 2370221345Sdim E = FD->redecls_end(); I != E; ++I) { 2371221345Sdim const FunctionDecl *NextFD = *I; 2372221345Sdim llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 2373234353Sdim MI = SPCache.find(NextFD->getCanonicalDecl()); 2374221345Sdim if (MI != SPCache.end()) { 2375239462Sdim llvm::Value *V = MI->second; 2376239462Sdim llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V)); 2377263508Sdim if (SP.isSubprogram() && !SP.isDefinition()) 2378221345Sdim return SP; 2379221345Sdim } 2380221345Sdim } 2381221345Sdim return llvm::DISubprogram(); 2382221345Sdim} 2383221345Sdim 2384223017Sdim// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include 2385223017Sdim// implicit parameter "this". 2386263508Sdimllvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D, 2387263508Sdim QualType FnType, 2388263508Sdim llvm::DIFile F) { 2389263508Sdim if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly) 2390263508Sdim // Create fake but valid subroutine type. Otherwise 2391263508Sdim // llvm::DISubprogram::Verify() would return false, and 2392263508Sdim // subprogram DIE will miss DW_AT_decl_file and 2393263508Sdim // DW_AT_decl_line fields. 2394263508Sdim return DBuilder.createSubroutineType(F, DBuilder.getOrCreateArray(None)); 2395239462Sdim 2396223017Sdim if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) 2397223017Sdim return getOrCreateMethodType(Method, F); 2398234353Sdim if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) { 2399223017Sdim // Add "self" and "_cmd" 2400226633Sdim SmallVector<llvm::Value *, 16> Elts; 2401223017Sdim 2402223017Sdim // First element is always return type. For 'void' functions it is NULL. 2403263508Sdim QualType ResultTy = OMethod->getResultType(); 2404263508Sdim 2405263508Sdim // Replace the instancetype keyword with the actual type. 2406263508Sdim if (ResultTy == CGM.getContext().getObjCInstanceType()) 2407263508Sdim ResultTy = CGM.getContext().getPointerType( 2408263508Sdim QualType(OMethod->getClassInterface()->getTypeForDecl(), 0)); 2409263508Sdim 2410263508Sdim Elts.push_back(getOrCreateType(ResultTy, F)); 2411223017Sdim // "self" pointer is always first argument. 2412249423Sdim QualType SelfDeclTy = OMethod->getSelfDecl()->getType(); 2413249423Sdim llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F); 2414249423Sdim Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy)); 2415243830Sdim // "_cmd" pointer is always second argument. 2416243830Sdim llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F); 2417243830Sdim Elts.push_back(DBuilder.createArtificialType(CmdTy)); 2418223017Sdim // Get rest of the arguments. 2419263508Sdim for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(), 2420223017Sdim PE = OMethod->param_end(); PI != PE; ++PI) 2421223017Sdim Elts.push_back(getOrCreateType((*PI)->getType(), F)); 2422223017Sdim 2423223017Sdim llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); 2424223017Sdim return DBuilder.createSubroutineType(F, EltTypeArray); 2425223017Sdim } 2426269000Semaste 2427269000Semaste // Variadic function. 2428269000Semaste if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 2429269000Semaste if (FD->isVariadic()) { 2430269000Semaste SmallVector<llvm::Value *, 16> EltTys; 2431269000Semaste EltTys.push_back(getOrCreateType(FD->getResultType(), F)); 2432269000Semaste if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType)) 2433269000Semaste for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i) 2434269000Semaste EltTys.push_back(getOrCreateType(FPT->getArgType(i), F)); 2435269000Semaste EltTys.push_back(DBuilder.createUnspecifiedParameter()); 2436269000Semaste llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys); 2437269000Semaste return DBuilder.createSubroutineType(F, EltTypeArray); 2438269000Semaste } 2439269000Semaste 2440263508Sdim return llvm::DICompositeType(getOrCreateType(FnType, F)); 2441223017Sdim} 2442223017Sdim 2443234353Sdim/// EmitFunctionStart - Constructs the debug code for entering a function. 2444202379Srdivackyvoid CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, 2445193326Sed llvm::Function *Fn, 2446193326Sed CGBuilderTy &Builder) { 2447198092Srdivacky 2448226633Sdim StringRef Name; 2449226633Sdim StringRef LinkageName; 2450198092Srdivacky 2451226633Sdim FnBeginRegionCount.push_back(LexicalBlockStack.size()); 2452212904Sdim 2453202379Srdivacky const Decl *D = GD.getDecl(); 2454243830Sdim // Function may lack declaration in source code if it is created by Clang 2455243830Sdim // CodeGen (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk). 2456243830Sdim bool HasDecl = (D != 0); 2457234353Sdim // Use the location of the declaration. 2458243830Sdim SourceLocation Loc; 2459243830Sdim if (HasDecl) 2460243830Sdim Loc = D->getLocation(); 2461243830Sdim 2462218893Sdim unsigned Flags = 0; 2463234353Sdim llvm::DIFile Unit = getOrCreateFile(Loc); 2464218893Sdim llvm::DIDescriptor FDContext(Unit); 2465221345Sdim llvm::DIArray TParamsArray; 2466243830Sdim if (!HasDecl) { 2467243830Sdim // Use llvm function name. 2468263508Sdim LinkageName = Fn->getName(); 2469243830Sdim } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2470234353Sdim // If there is a DISubprogram for this function available then use it. 2471202879Srdivacky llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 2472234353Sdim FI = SPCache.find(FD->getCanonicalDecl()); 2473202879Srdivacky if (FI != SPCache.end()) { 2474239462Sdim llvm::Value *V = FI->second; 2475239462Sdim llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V)); 2476208600Srdivacky if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { 2477208600Srdivacky llvm::MDNode *SPN = SP; 2478226633Sdim LexicalBlockStack.push_back(SPN); 2479208600Srdivacky RegionMap[D] = llvm::WeakVH(SP); 2480202879Srdivacky return; 2481202879Srdivacky } 2482202879Srdivacky } 2483202379Srdivacky Name = getFunctionName(FD); 2484249423Sdim // Use mangled name as linkage name for C/C++ functions. 2485234353Sdim if (FD->hasPrototype()) { 2486223017Sdim LinkageName = CGM.getMangledName(GD); 2487234353Sdim Flags |= llvm::DIDescriptor::FlagPrototyped; 2488234353Sdim } 2489249423Sdim // No need to replicate the linkage name if it isn't different from the 2490249423Sdim // subprogram name, no need to have it at all unless coverage is enabled or 2491249423Sdim // debug is set to more than just line tables. 2492239462Sdim if (LinkageName == Name || 2493249423Sdim (!CGM.getCodeGenOpts().EmitGcovArcs && 2494249423Sdim !CGM.getCodeGenOpts().EmitGcovNotes && 2495263508Sdim DebugKind <= CodeGenOptions::DebugLineTablesOnly)) 2496226633Sdim LinkageName = StringRef(); 2497234353Sdim 2498263508Sdim if (DebugKind >= CodeGenOptions::LimitedDebugInfo) { 2499239462Sdim if (const NamespaceDecl *NSDecl = 2500263508Sdim dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) 2501239462Sdim FDContext = getOrCreateNameSpace(NSDecl); 2502239462Sdim else if (const RecordDecl *RDecl = 2503263508Sdim dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) 2504263508Sdim FDContext = getContextDescriptor(cast<Decl>(RDecl)); 2505221345Sdim 2506239462Sdim // Collect template parameters. 2507239462Sdim TParamsArray = CollectFunctionTemplateParams(FD, Unit); 2508239462Sdim } 2509212904Sdim } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { 2510212904Sdim Name = getObjCMethodName(OMD); 2511218893Sdim Flags |= llvm::DIDescriptor::FlagPrototyped; 2512202379Srdivacky } else { 2513218893Sdim // Use llvm function name. 2514202379Srdivacky Name = Fn->getName(); 2515218893Sdim Flags |= llvm::DIDescriptor::FlagPrototyped; 2516202379Srdivacky } 2517207619Srdivacky if (!Name.empty() && Name[0] == '\01') 2518207619Srdivacky Name = Name.substr(1); 2519202379Srdivacky 2520234353Sdim unsigned LineNo = getLineNumber(Loc); 2521243830Sdim if (!HasDecl || D->isImplicit()) 2522218893Sdim Flags |= llvm::DIDescriptor::FlagArtificial; 2523234353Sdim 2524263508Sdim llvm::DISubprogram SP = 2525263508Sdim DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo, 2526263508Sdim getOrCreateFunctionType(D, FnType, Unit), 2527263508Sdim Fn->hasInternalLinkage(), true /*definition*/, 2528263508Sdim getLineNumber(CurLoc), Flags, 2529263508Sdim CGM.getLangOpts().Optimize, Fn, TParamsArray, 2530263508Sdim getFunctionDeclaration(D)); 2531263508Sdim if (HasDecl) 2532263508Sdim DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(SP))); 2533198092Srdivacky 2534193326Sed // Push function on region stack. 2535208600Srdivacky llvm::MDNode *SPN = SP; 2536226633Sdim LexicalBlockStack.push_back(SPN); 2537243830Sdim if (HasDecl) 2538243830Sdim RegionMap[D] = llvm::WeakVH(SP); 2539193326Sed} 2540193326Sed 2541226633Sdim/// EmitLocation - Emit metadata to indicate a change in line/column 2542263508Sdim/// information in the source file. If the location is invalid, the 2543263508Sdim/// previous location will be reused. 2544249423Sdimvoid CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc, 2545249423Sdim bool ForceColumnInfo) { 2546226633Sdim // Update our current location 2547226633Sdim setLocation(Loc); 2548193326Sed 2549193326Sed if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 2550198092Srdivacky 2551193326Sed // Don't bother if things are the same as last time. 2552200583Srdivacky SourceManager &SM = CGM.getContext().getSourceManager(); 2553226633Sdim if (CurLoc == PrevLoc || 2554226633Sdim SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc)) 2555206275Srdivacky // New Builder may not be in sync with CGDebugInfo. 2556249423Sdim if (!Builder.getCurrentDebugLocation().isUnknown() && 2557249423Sdim Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) == 2558249423Sdim LexicalBlockStack.back()) 2559206275Srdivacky return; 2560263508Sdim 2561193326Sed // Update last state. 2562193326Sed PrevLoc = CurLoc; 2563193326Sed 2564226633Sdim llvm::MDNode *Scope = LexicalBlockStack.back(); 2565249423Sdim Builder.SetCurrentDebugLocation(llvm::DebugLoc::get 2566249423Sdim (getLineNumber(CurLoc), 2567249423Sdim getColumnNumber(CurLoc, ForceColumnInfo), 2568249423Sdim Scope)); 2569193326Sed} 2570193326Sed 2571226633Sdim/// CreateLexicalBlock - Creates a new lexical block node and pushes it on 2572226633Sdim/// the stack. 2573226633Sdimvoid CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { 2574226633Sdim llvm::DIDescriptor D = 2575226633Sdim DBuilder.createLexicalBlock(LexicalBlockStack.empty() ? 2576234353Sdim llvm::DIDescriptor() : 2577234353Sdim llvm::DIDescriptor(LexicalBlockStack.back()), 2578234353Sdim getOrCreateFile(CurLoc), 2579234353Sdim getLineNumber(CurLoc), 2580234353Sdim getColumnNumber(CurLoc)); 2581226633Sdim llvm::MDNode *DN = D; 2582226633Sdim LexicalBlockStack.push_back(DN); 2583226633Sdim} 2584212904Sdim 2585226633Sdim/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative 2586226633Sdim/// region - beginning of a DW_TAG_lexical_block. 2587263508Sdimvoid CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, 2588263508Sdim SourceLocation Loc) { 2589226633Sdim // Set our current location. 2590226633Sdim setLocation(Loc); 2591212904Sdim 2592226633Sdim // Create a new lexical block and push it on the stack. 2593226633Sdim CreateLexicalBlock(Loc); 2594212904Sdim 2595226633Sdim // Emit a line table change for the current location inside the new scope. 2596226633Sdim Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc), 2597234353Sdim getColumnNumber(Loc), 2598234353Sdim LexicalBlockStack.back())); 2599212904Sdim} 2600193326Sed 2601226633Sdim/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative 2602226633Sdim/// region - end of a DW_TAG_lexical_block. 2603263508Sdimvoid CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, 2604263508Sdim SourceLocation Loc) { 2605226633Sdim assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 2606193326Sed 2607226633Sdim // Provide an entry in the line table for the end of the block. 2608226633Sdim EmitLocation(Builder, Loc); 2609198092Srdivacky 2610226633Sdim LexicalBlockStack.pop_back(); 2611193326Sed} 2612193326Sed 2613212904Sdim/// EmitFunctionEnd - Constructs the debug code for exiting a function. 2614212904Sdimvoid CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { 2615226633Sdim assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 2616212904Sdim unsigned RCount = FnBeginRegionCount.back(); 2617226633Sdim assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch"); 2618212904Sdim 2619212904Sdim // Pop all regions for this function. 2620226633Sdim while (LexicalBlockStack.size() != RCount) 2621226633Sdim EmitLexicalBlockEnd(Builder, CurLoc); 2622212904Sdim FnBeginRegionCount.pop_back(); 2623212904Sdim} 2624212904Sdim 2625263508Sdim// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 2626203955Srdivacky// See BuildByRefType. 2627249423Sdimllvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD, 2628203955Srdivacky uint64_t *XOffset) { 2629193326Sed 2630226633Sdim SmallVector<llvm::Value *, 5> EltTys; 2631203955Srdivacky QualType FType; 2632203955Srdivacky uint64_t FieldSize, FieldOffset; 2633203955Srdivacky unsigned FieldAlign; 2634263508Sdim 2635204962Srdivacky llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 2636263508Sdim QualType Type = VD->getType(); 2637193326Sed 2638203955Srdivacky FieldOffset = 0; 2639203955Srdivacky FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 2640207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 2641207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); 2642203955Srdivacky FType = CGM.getContext().IntTy; 2643207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 2644207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); 2645207619Srdivacky 2646249423Sdim bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD); 2647203955Srdivacky if (HasCopyAndDispose) { 2648200583Srdivacky FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 2649207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", 2650207619Srdivacky &FieldOffset)); 2651207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", 2652207619Srdivacky &FieldOffset)); 2653203955Srdivacky } 2654249423Sdim bool HasByrefExtendedLayout; 2655249423Sdim Qualifiers::ObjCLifetime Lifetime; 2656249423Sdim if (CGM.getContext().getByrefLifetime(Type, 2657249423Sdim Lifetime, HasByrefExtendedLayout) 2658263508Sdim && HasByrefExtendedLayout) { 2659263508Sdim FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 2660249423Sdim EltTys.push_back(CreateMemberType(Unit, FType, 2661249423Sdim "__byref_variable_layout", 2662249423Sdim &FieldOffset)); 2663263508Sdim } 2664263508Sdim 2665203955Srdivacky CharUnits Align = CGM.getContext().getDeclAlign(VD); 2666221345Sdim if (Align > CGM.getContext().toCharUnitsFromBits( 2667251662Sdim CGM.getTarget().getPointerAlign(0))) { 2668263508Sdim CharUnits FieldOffsetInBytes 2669221345Sdim = CGM.getContext().toCharUnitsFromBits(FieldOffset); 2670221345Sdim CharUnits AlignedOffsetInBytes 2671221345Sdim = FieldOffsetInBytes.RoundUpToAlignment(Align); 2672221345Sdim CharUnits NumPaddingBytes 2673221345Sdim = AlignedOffsetInBytes - FieldOffsetInBytes; 2674263508Sdim 2675221345Sdim if (NumPaddingBytes.isPositive()) { 2676221345Sdim llvm::APInt pad(32, NumPaddingBytes.getQuantity()); 2677203955Srdivacky FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, 2678203955Srdivacky pad, ArrayType::Normal, 0); 2679207619Srdivacky EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); 2680198092Srdivacky } 2681203955Srdivacky } 2682263508Sdim 2683203955Srdivacky FType = Type; 2684207619Srdivacky llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 2685203955Srdivacky FieldSize = CGM.getContext().getTypeSize(FType); 2686221345Sdim FieldAlign = CGM.getContext().toBits(Align); 2687198092Srdivacky 2688263508Sdim *XOffset = FieldOffset; 2689224145Sdim FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 2690218893Sdim 0, FieldSize, FieldAlign, 2691218893Sdim FieldOffset, 0, FieldTy); 2692203955Srdivacky EltTys.push_back(FieldTy); 2693203955Srdivacky FieldOffset += FieldSize; 2694263508Sdim 2695221345Sdim llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); 2696263508Sdim 2697218893Sdim unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct; 2698263508Sdim 2699219077Sdim return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, 2700249423Sdim llvm::DIType(), Elements); 2701203955Srdivacky} 2702218893Sdim 2703203955Srdivacky/// EmitDeclare - Emit local variable declaration debug info. 2704203955Srdivackyvoid CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, 2705263508Sdim llvm::Value *Storage, 2706221345Sdim unsigned ArgNo, CGBuilderTy &Builder) { 2707263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 2708226633Sdim assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 2709198092Srdivacky 2710263508Sdim bool Unwritten = 2711263508Sdim VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) && 2712263508Sdim cast<Decl>(VD->getDeclContext())->isImplicit()); 2713263508Sdim llvm::DIFile Unit; 2714263508Sdim if (!Unwritten) 2715263508Sdim Unit = getOrCreateFile(VD->getLocation()); 2716203955Srdivacky llvm::DIType Ty; 2717203955Srdivacky uint64_t XOffset = 0; 2718203955Srdivacky if (VD->hasAttr<BlocksAttr>()) 2719203955Srdivacky Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 2720263508Sdim else 2721203955Srdivacky Ty = getOrCreateType(VD->getType(), Unit); 2722203955Srdivacky 2723243830Sdim // If there is no debug info for this type then do not emit debug info 2724208600Srdivacky // for this variable. 2725208600Srdivacky if (!Ty) 2726208600Srdivacky return; 2727208600Srdivacky 2728263508Sdim // Get location information. 2729263508Sdim unsigned Line = 0; 2730263508Sdim unsigned Column = 0; 2731263508Sdim if (!Unwritten) { 2732263508Sdim Line = getLineNumber(VD->getLocation()); 2733263508Sdim Column = getColumnNumber(VD->getLocation()); 2734218893Sdim } 2735218893Sdim unsigned Flags = 0; 2736218893Sdim if (VD->isImplicit()) 2737218893Sdim Flags |= llvm::DIDescriptor::FlagArtificial; 2738243830Sdim // If this is the first argument and it is implicit then 2739243830Sdim // give it an object pointer flag. 2740243830Sdim // FIXME: There has to be a better way to do this, but for static 2741243830Sdim // functions there won't be an implicit param at arg1 and 2742243830Sdim // otherwise it is 'self' or 'this'. 2743243830Sdim if (isa<ImplicitParamDecl>(VD) && ArgNo == 1) 2744243830Sdim Flags |= llvm::DIDescriptor::FlagObjectPointer; 2745263508Sdim if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) 2746263508Sdim if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() && 2747263508Sdim !VD->getType()->isPointerType()) 2748263508Sdim Flags |= llvm::DIDescriptor::FlagIndirectVariable; 2749243830Sdim 2750226633Sdim llvm::MDNode *Scope = LexicalBlockStack.back(); 2751243830Sdim 2752226633Sdim StringRef Name = VD->getName(); 2753218893Sdim if (!Name.empty()) { 2754218893Sdim if (VD->hasAttr<BlocksAttr>()) { 2755218893Sdim CharUnits offset = CharUnits::fromQuantity(32); 2756226633Sdim SmallVector<llvm::Value *, 9> addr; 2757234353Sdim llvm::Type *Int64Ty = CGM.Int64Ty; 2758218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 2759218893Sdim // offset of __forwarding field 2760221345Sdim offset = CGM.getContext().toCharUnitsFromBits( 2761251662Sdim CGM.getTarget().getPointerWidth(0)); 2762218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 2763218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); 2764218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 2765218893Sdim // offset of x field 2766221345Sdim offset = CGM.getContext().toCharUnitsFromBits(XOffset); 2767218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 2768193326Sed 2769218893Sdim // Create the descriptor for the variable. 2770218893Sdim llvm::DIVariable D = 2771263508Sdim DBuilder.createComplexVariable(Tag, 2772226633Sdim llvm::DIDescriptor(Scope), 2773218893Sdim VD->getName(), Unit, Line, Ty, 2774221345Sdim addr, ArgNo); 2775239462Sdim 2776239462Sdim // Insert an llvm.dbg.declare into the current block. 2777239462Sdim llvm::Instruction *Call = 2778239462Sdim DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); 2779239462Sdim Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 2780239462Sdim return; 2781263508Sdim } else if (isa<VariableArrayType>(VD->getType())) 2782263508Sdim Flags |= llvm::DIDescriptor::FlagIndirectVariable; 2783249423Sdim } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) { 2784249423Sdim // If VD is an anonymous union then Storage represents value for 2785249423Sdim // all union fields. 2786219077Sdim const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); 2787249423Sdim if (RD->isUnion() && RD->isAnonymousStructOrUnion()) { 2788219077Sdim for (RecordDecl::field_iterator I = RD->field_begin(), 2789219077Sdim E = RD->field_end(); 2790219077Sdim I != E; ++I) { 2791219077Sdim FieldDecl *Field = *I; 2792219077Sdim llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 2793226633Sdim StringRef FieldName = Field->getName(); 2794263508Sdim 2795219077Sdim // Ignore unnamed fields. Do not ignore unnamed records. 2796219077Sdim if (FieldName.empty() && !isa<RecordType>(Field->getType())) 2797219077Sdim continue; 2798263508Sdim 2799219077Sdim // Use VarDecl's Tag, Scope and Line number. 2800219077Sdim llvm::DIVariable D = 2801219077Sdim DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), 2802263508Sdim FieldName, Unit, Line, FieldTy, 2803234353Sdim CGM.getLangOpts().Optimize, Flags, 2804221345Sdim ArgNo); 2805263508Sdim 2806219077Sdim // Insert an llvm.dbg.declare into the current block. 2807219077Sdim llvm::Instruction *Call = 2808219077Sdim DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); 2809219077Sdim Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 2810218893Sdim } 2811249423Sdim return; 2812219077Sdim } 2813219077Sdim } 2814249423Sdim 2815249423Sdim // Create the descriptor for the variable. 2816249423Sdim llvm::DIVariable D = 2817249423Sdim DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), 2818249423Sdim Name, Unit, Line, Ty, 2819249423Sdim CGM.getLangOpts().Optimize, Flags, ArgNo); 2820249423Sdim 2821249423Sdim // Insert an llvm.dbg.declare into the current block. 2822249423Sdim llvm::Instruction *Call = 2823249423Sdim DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); 2824249423Sdim Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 2825193326Sed} 2826193326Sed 2827221345Sdimvoid CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, 2828221345Sdim llvm::Value *Storage, 2829221345Sdim CGBuilderTy &Builder) { 2830263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 2831221345Sdim EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder); 2832221345Sdim} 2833221345Sdim 2834249423Sdim/// Look up the completed type for a self pointer in the TypeCache and 2835249423Sdim/// create a copy of it with the ObjectPointer and Artificial flags 2836249423Sdim/// set. If the type is not cached, a new one is created. This should 2837249423Sdim/// never happen though, since creating a type for the implicit self 2838249423Sdim/// argument implies that we already parsed the interface definition 2839249423Sdim/// and the ivar declarations in the implementation. 2840263508Sdimllvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy, 2841263508Sdim llvm::DIType Ty) { 2842249423Sdim llvm::DIType CachedTy = getTypeOrNull(QualTy); 2843263508Sdim if (CachedTy) Ty = CachedTy; 2844249423Sdim else DEBUG(llvm::dbgs() << "No cached type for self."); 2845249423Sdim return DBuilder.createObjectPointerType(Ty); 2846249423Sdim} 2847249423Sdim 2848243830Sdimvoid CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD, 2849243830Sdim llvm::Value *Storage, 2850243830Sdim CGBuilderTy &Builder, 2851243830Sdim const CGBlockInfo &blockInfo) { 2852263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 2853226633Sdim assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 2854263508Sdim 2855207619Srdivacky if (Builder.GetInsertBlock() == 0) 2856198092Srdivacky return; 2857263508Sdim 2858218893Sdim bool isByRef = VD->hasAttr<BlocksAttr>(); 2859263508Sdim 2860198092Srdivacky uint64_t XOffset = 0; 2861204962Srdivacky llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 2862203955Srdivacky llvm::DIType Ty; 2863218893Sdim if (isByRef) 2864203955Srdivacky Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 2865263508Sdim else 2866203955Srdivacky Ty = getOrCreateType(VD->getType(), Unit); 2867198092Srdivacky 2868243830Sdim // Self is passed along as an implicit non-arg variable in a 2869243830Sdim // block. Mark it as the object pointer. 2870243830Sdim if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self") 2871249423Sdim Ty = CreateSelfType(VD->getType(), Ty); 2872243830Sdim 2873198092Srdivacky // Get location information. 2874208600Srdivacky unsigned Line = getLineNumber(VD->getLocation()); 2875208600Srdivacky unsigned Column = getColumnNumber(VD->getLocation()); 2876198092Srdivacky 2877243830Sdim const llvm::DataLayout &target = CGM.getDataLayout(); 2878218893Sdim 2879218893Sdim CharUnits offset = CharUnits::fromQuantity( 2880218893Sdim target.getStructLayout(blockInfo.StructureType) 2881218893Sdim ->getElementOffset(blockInfo.getCapture(VD).getIndex())); 2882218893Sdim 2883226633Sdim SmallVector<llvm::Value *, 9> addr; 2884234353Sdim llvm::Type *Int64Ty = CGM.Int64Ty; 2885249423Sdim if (isa<llvm::AllocaInst>(Storage)) 2886249423Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); 2887218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 2888203955Srdivacky addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 2889218893Sdim if (isByRef) { 2890218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); 2891218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 2892202379Srdivacky // offset of __forwarding field 2893226633Sdim offset = CGM.getContext() 2894243830Sdim .toCharUnitsFromBits(target.getPointerSizeInBits(0)); 2895203955Srdivacky addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 2896218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); 2897218893Sdim addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 2898202379Srdivacky // offset of x field 2899221345Sdim offset = CGM.getContext().toCharUnitsFromBits(XOffset); 2900203955Srdivacky addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 2901198092Srdivacky } 2902198092Srdivacky 2903198092Srdivacky // Create the descriptor for the variable. 2904198092Srdivacky llvm::DIVariable D = 2905263508Sdim DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable, 2906226633Sdim llvm::DIDescriptor(LexicalBlockStack.back()), 2907221345Sdim VD->getName(), Unit, Line, Ty, addr); 2908249423Sdim 2909198092Srdivacky // Insert an llvm.dbg.declare into the current block. 2910226633Sdim llvm::Instruction *Call = 2911221345Sdim DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint()); 2912226633Sdim Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, 2913226633Sdim LexicalBlockStack.back())); 2914198092Srdivacky} 2915198092Srdivacky 2916193326Sed/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 2917193326Sed/// variable declaration. 2918203955Srdivackyvoid CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, 2919221345Sdim unsigned ArgNo, 2920193326Sed CGBuilderTy &Builder) { 2921263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 2922221345Sdim EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder); 2923193326Sed} 2924193326Sed 2925219077Sdimnamespace { 2926219077Sdim struct BlockLayoutChunk { 2927219077Sdim uint64_t OffsetInBits; 2928219077Sdim const BlockDecl::Capture *Capture; 2929219077Sdim }; 2930219077Sdim bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) { 2931219077Sdim return l.OffsetInBits < r.OffsetInBits; 2932219077Sdim } 2933219077Sdim} 2934193326Sed 2935219077Sdimvoid CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, 2936249423Sdim llvm::Value *Arg, 2937249423Sdim llvm::Value *LocalAddr, 2938219077Sdim CGBuilderTy &Builder) { 2939263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 2940219077Sdim ASTContext &C = CGM.getContext(); 2941219077Sdim const BlockDecl *blockDecl = block.getBlockDecl(); 2942193326Sed 2943219077Sdim // Collect some general information about the block's location. 2944219077Sdim SourceLocation loc = blockDecl->getCaretLocation(); 2945219077Sdim llvm::DIFile tunit = getOrCreateFile(loc); 2946219077Sdim unsigned line = getLineNumber(loc); 2947219077Sdim unsigned column = getColumnNumber(loc); 2948263508Sdim 2949219077Sdim // Build the debug-info type for the block literal. 2950221345Sdim getContextDescriptor(cast<Decl>(blockDecl->getDeclContext())); 2951219077Sdim 2952219077Sdim const llvm::StructLayout *blockLayout = 2953243830Sdim CGM.getDataLayout().getStructLayout(block.StructureType); 2954219077Sdim 2955226633Sdim SmallVector<llvm::Value*, 16> fields; 2956219077Sdim fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public, 2957219077Sdim blockLayout->getElementOffsetInBits(0), 2958224145Sdim tunit, tunit)); 2959219077Sdim fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public, 2960219077Sdim blockLayout->getElementOffsetInBits(1), 2961224145Sdim tunit, tunit)); 2962219077Sdim fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public, 2963219077Sdim blockLayout->getElementOffsetInBits(2), 2964224145Sdim tunit, tunit)); 2965219077Sdim fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public, 2966219077Sdim blockLayout->getElementOffsetInBits(3), 2967224145Sdim tunit, tunit)); 2968219077Sdim fields.push_back(createFieldType("__descriptor", 2969219077Sdim C.getPointerType(block.NeedsCopyDispose ? 2970219077Sdim C.getBlockDescriptorExtendedType() : 2971219077Sdim C.getBlockDescriptorType()), 2972219077Sdim 0, loc, AS_public, 2973219077Sdim blockLayout->getElementOffsetInBits(4), 2974224145Sdim tunit, tunit)); 2975219077Sdim 2976219077Sdim // We want to sort the captures by offset, not because DWARF 2977219077Sdim // requires this, but because we're paranoid about debuggers. 2978226633Sdim SmallVector<BlockLayoutChunk, 8> chunks; 2979219077Sdim 2980219077Sdim // 'this' capture. 2981219077Sdim if (blockDecl->capturesCXXThis()) { 2982219077Sdim BlockLayoutChunk chunk; 2983219077Sdim chunk.OffsetInBits = 2984219077Sdim blockLayout->getElementOffsetInBits(block.CXXThisIndex); 2985219077Sdim chunk.Capture = 0; 2986219077Sdim chunks.push_back(chunk); 2987219077Sdim } 2988219077Sdim 2989219077Sdim // Variable captures. 2990219077Sdim for (BlockDecl::capture_const_iterator 2991219077Sdim i = blockDecl->capture_begin(), e = blockDecl->capture_end(); 2992219077Sdim i != e; ++i) { 2993219077Sdim const BlockDecl::Capture &capture = *i; 2994219077Sdim const VarDecl *variable = capture.getVariable(); 2995219077Sdim const CGBlockInfo::Capture &captureInfo = block.getCapture(variable); 2996219077Sdim 2997219077Sdim // Ignore constant captures. 2998219077Sdim if (captureInfo.isConstant()) 2999219077Sdim continue; 3000219077Sdim 3001219077Sdim BlockLayoutChunk chunk; 3002219077Sdim chunk.OffsetInBits = 3003219077Sdim blockLayout->getElementOffsetInBits(captureInfo.getIndex()); 3004219077Sdim chunk.Capture = &capture; 3005219077Sdim chunks.push_back(chunk); 3006219077Sdim } 3007219077Sdim 3008219077Sdim // Sort by offset. 3009219077Sdim llvm::array_pod_sort(chunks.begin(), chunks.end()); 3010219077Sdim 3011226633Sdim for (SmallVectorImpl<BlockLayoutChunk>::iterator 3012219077Sdim i = chunks.begin(), e = chunks.end(); i != e; ++i) { 3013219077Sdim uint64_t offsetInBits = i->OffsetInBits; 3014219077Sdim const BlockDecl::Capture *capture = i->Capture; 3015219077Sdim 3016219077Sdim // If we have a null capture, this must be the C++ 'this' capture. 3017219077Sdim if (!capture) { 3018219077Sdim const CXXMethodDecl *method = 3019219077Sdim cast<CXXMethodDecl>(blockDecl->getNonClosureContext()); 3020219077Sdim QualType type = method->getThisType(C); 3021219077Sdim 3022219077Sdim fields.push_back(createFieldType("this", type, 0, loc, AS_public, 3023224145Sdim offsetInBits, tunit, tunit)); 3024219077Sdim continue; 3025219077Sdim } 3026219077Sdim 3027219077Sdim const VarDecl *variable = capture->getVariable(); 3028226633Sdim StringRef name = variable->getName(); 3029221345Sdim 3030221345Sdim llvm::DIType fieldType; 3031221345Sdim if (capture->isByRef()) { 3032221345Sdim std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy); 3033221345Sdim 3034221345Sdim // FIXME: this creates a second copy of this type! 3035221345Sdim uint64_t xoffset; 3036221345Sdim fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset); 3037221345Sdim fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first); 3038224145Sdim fieldType = DBuilder.createMemberType(tunit, name, tunit, line, 3039221345Sdim ptrInfo.first, ptrInfo.second, 3040221345Sdim offsetInBits, 0, fieldType); 3041221345Sdim } else { 3042221345Sdim fieldType = createFieldType(name, variable->getType(), 0, 3043224145Sdim loc, AS_public, offsetInBits, tunit, tunit); 3044221345Sdim } 3045221345Sdim fields.push_back(fieldType); 3046219077Sdim } 3047219077Sdim 3048234353Sdim SmallString<36> typeName; 3049219077Sdim llvm::raw_svector_ostream(typeName) 3050219077Sdim << "__block_literal_" << CGM.getUniqueBlockCount(); 3051219077Sdim 3052221345Sdim llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields); 3053219077Sdim 3054219077Sdim llvm::DIType type = 3055219077Sdim DBuilder.createStructType(tunit, typeName.str(), tunit, line, 3056219077Sdim CGM.getContext().toBits(block.BlockSize), 3057219077Sdim CGM.getContext().toBits(block.BlockAlign), 3058249423Sdim 0, llvm::DIType(), fieldsArray); 3059219077Sdim type = DBuilder.createPointerType(type, CGM.PointerWidthInBits); 3060219077Sdim 3061219077Sdim // Get overall information about the block. 3062219077Sdim unsigned flags = llvm::DIDescriptor::FlagArtificial; 3063226633Sdim llvm::MDNode *scope = LexicalBlockStack.back(); 3064219077Sdim 3065219077Sdim // Create the descriptor for the parameter. 3066219077Sdim llvm::DIVariable debugVar = 3067219077Sdim DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable, 3068263508Sdim llvm::DIDescriptor(scope), 3069249423Sdim Arg->getName(), tunit, line, type, 3070234353Sdim CGM.getLangOpts().Optimize, flags, 3071249423Sdim cast<llvm::Argument>(Arg)->getArgNo() + 1); 3072249423Sdim 3073249423Sdim if (LocalAddr) { 3074249423Sdim // Insert an llvm.dbg.value into the current block. 3075249423Sdim llvm::Instruction *DbgVal = 3076249423Sdim DBuilder.insertDbgValueIntrinsic(LocalAddr, 0, debugVar, 3077249423Sdim Builder.GetInsertBlock()); 3078249423Sdim DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope)); 3079249423Sdim } 3080249423Sdim 3081249423Sdim // Insert an llvm.dbg.declare into the current block. 3082249423Sdim llvm::Instruction *DbgDecl = 3083249423Sdim DBuilder.insertDeclare(Arg, debugVar, Builder.GetInsertBlock()); 3084249423Sdim DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope)); 3085219077Sdim} 3086219077Sdim 3087263508Sdim/// If D is an out-of-class definition of a static data member of a class, find 3088263508Sdim/// its corresponding in-class declaration. 3089263508Sdimllvm::DIDerivedType 3090263508SdimCGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) { 3091263508Sdim if (!D->isStaticDataMember()) 3092263508Sdim return llvm::DIDerivedType(); 3093263508Sdim llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator MI = 3094263508Sdim StaticDataMemberCache.find(D->getCanonicalDecl()); 3095263508Sdim if (MI != StaticDataMemberCache.end()) { 3096263508Sdim assert(MI->second && "Static data member declaration should still exist"); 3097263508Sdim return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)); 3098249423Sdim } 3099263508Sdim 3100263508Sdim // If the member wasn't found in the cache, lazily construct and add it to the 3101263508Sdim // type (used when a limited form of the type is emitted). 3102263508Sdim llvm::DICompositeType Ctxt( 3103263508Sdim getContextDescriptor(cast<Decl>(D->getDeclContext()))); 3104263508Sdim llvm::DIDerivedType T = CreateRecordStaticField(D, Ctxt); 3105263508Sdim Ctxt.addMember(T); 3106263508Sdim return T; 3107249423Sdim} 3108249423Sdim 3109193326Sed/// EmitGlobalVariable - Emit information about a global variable. 3110198092Srdivackyvoid CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 3111203955Srdivacky const VarDecl *D) { 3112263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 3113193326Sed // Create global variable debug descriptor. 3114204962Srdivacky llvm::DIFile Unit = getOrCreateFile(D->getLocation()); 3115208600Srdivacky unsigned LineNo = getLineNumber(D->getLocation()); 3116193326Sed 3117226633Sdim setLocation(D->getLocation()); 3118226633Sdim 3119203955Srdivacky QualType T = D->getType(); 3120193326Sed if (T->isIncompleteArrayType()) { 3121198092Srdivacky 3122193326Sed // CodeGen turns int[] into int[1] so we'll do the same here. 3123239462Sdim llvm::APInt ConstVal(32, 1); 3124200583Srdivacky QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 3125198092Srdivacky 3126200583Srdivacky T = CGM.getContext().getConstantArrayType(ET, ConstVal, 3127234353Sdim ArrayType::Normal, 0); 3128193326Sed } 3129226633Sdim StringRef DeclName = D->getName(); 3130226633Sdim StringRef LinkageName; 3131218893Sdim if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()) 3132218893Sdim && !isa<ObjCMethodDecl>(D->getDeclContext())) 3133208600Srdivacky LinkageName = Var->getName(); 3134218893Sdim if (LinkageName == DeclName) 3135226633Sdim LinkageName = StringRef(); 3136263508Sdim llvm::DIDescriptor DContext = 3137218893Sdim getContextDescriptor(dyn_cast<Decl>(D->getDeclContext())); 3138263508Sdim llvm::DIGlobalVariable GV = DBuilder.createStaticVariable( 3139263508Sdim DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit), 3140263508Sdim Var->hasInternalLinkage(), Var, 3141263508Sdim getOrCreateStaticDataMemberDeclarationOrNull(D)); 3142263508Sdim DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV))); 3143193326Sed} 3144193326Sed 3145193326Sed/// EmitGlobalVariable - Emit information about an objective-c interface. 3146198092Srdivackyvoid CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 3147203955Srdivacky ObjCInterfaceDecl *ID) { 3148263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 3149193326Sed // Create global variable debug descriptor. 3150204962Srdivacky llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); 3151208600Srdivacky unsigned LineNo = getLineNumber(ID->getLocation()); 3152193326Sed 3153226633Sdim StringRef Name = ID->getName(); 3154193326Sed 3155203955Srdivacky QualType T = CGM.getContext().getObjCInterfaceType(ID); 3156193326Sed if (T->isIncompleteArrayType()) { 3157198092Srdivacky 3158193326Sed // CodeGen turns int[] into int[1] so we'll do the same here. 3159239462Sdim llvm::APInt ConstVal(32, 1); 3160200583Srdivacky QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 3161198092Srdivacky 3162200583Srdivacky T = CGM.getContext().getConstantArrayType(ET, ConstVal, 3163193326Sed ArrayType::Normal, 0); 3164193326Sed } 3165193326Sed 3166219077Sdim DBuilder.createGlobalVariable(Name, Unit, LineNo, 3167218893Sdim getOrCreateType(T, Unit), 3168218893Sdim Var->hasInternalLinkage(), Var); 3169193326Sed} 3170203955Srdivacky 3171212904Sdim/// EmitGlobalVariable - Emit global variable's debug info. 3172263508Sdimvoid CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, 3173218893Sdim llvm::Constant *Init) { 3174263508Sdim assert(DebugKind >= CodeGenOptions::LimitedDebugInfo); 3175212904Sdim // Create the descriptor for the variable. 3176212904Sdim llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 3177226633Sdim StringRef Name = VD->getName(); 3178212904Sdim llvm::DIType Ty = getOrCreateType(VD->getType(), Unit); 3179212904Sdim if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) { 3180239462Sdim const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext()); 3181239462Sdim assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?"); 3182239462Sdim Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit); 3183212904Sdim } 3184212904Sdim // Do not use DIGlobalVariable for enums. 3185212904Sdim if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type) 3186212904Sdim return; 3187263508Sdim llvm::DIGlobalVariable GV = DBuilder.createStaticVariable( 3188263508Sdim Unit, Name, Name, Unit, getLineNumber(VD->getLocation()), Ty, true, Init, 3189263508Sdim getOrCreateStaticDataMemberDeclarationOrNull(cast<VarDecl>(VD))); 3190263508Sdim DeclCache.insert(std::make_pair(VD->getCanonicalDecl(), llvm::WeakVH(GV))); 3191212904Sdim} 3192212904Sdim 3193263508Sdimllvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) { 3194263508Sdim if (!LexicalBlockStack.empty()) 3195263508Sdim return llvm::DIScope(LexicalBlockStack.back()); 3196263508Sdim return getContextDescriptor(D); 3197263508Sdim} 3198263508Sdim 3199251662Sdimvoid CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) { 3200263508Sdim if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo) 3201263508Sdim return; 3202251662Sdim DBuilder.createImportedModule( 3203263508Sdim getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())), 3204263508Sdim getOrCreateNameSpace(UD.getNominatedNamespace()), 3205251662Sdim getLineNumber(UD.getLocation())); 3206251662Sdim} 3207251662Sdim 3208263508Sdimvoid CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) { 3209263508Sdim if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo) 3210263508Sdim return; 3211263508Sdim assert(UD.shadow_size() && 3212263508Sdim "We shouldn't be codegening an invalid UsingDecl containing no decls"); 3213263508Sdim // Emitting one decl is sufficient - debuggers can detect that this is an 3214263508Sdim // overloaded name & provide lookup for all the overloads. 3215263508Sdim const UsingShadowDecl &USD = **UD.shadow_begin(); 3216263508Sdim if (llvm::DIDescriptor Target = 3217263508Sdim getDeclarationOrDefinition(USD.getUnderlyingDecl())) 3218263508Sdim DBuilder.createImportedDeclaration( 3219263508Sdim getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target, 3220263508Sdim getLineNumber(USD.getLocation())); 3221263508Sdim} 3222263508Sdim 3223263508Sdimllvm::DIImportedEntity 3224263508SdimCGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) { 3225263508Sdim if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo) 3226263508Sdim return llvm::DIImportedEntity(0); 3227263508Sdim llvm::WeakVH &VH = NamespaceAliasCache[&NA]; 3228263508Sdim if (VH) 3229263508Sdim return llvm::DIImportedEntity(cast<llvm::MDNode>(VH)); 3230263508Sdim llvm::DIImportedEntity R(0); 3231263508Sdim if (const NamespaceAliasDecl *Underlying = 3232263508Sdim dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace())) 3233263508Sdim // This could cache & dedup here rather than relying on metadata deduping. 3234263508Sdim R = DBuilder.createImportedModule( 3235263508Sdim getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), 3236263508Sdim EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()), 3237263508Sdim NA.getName()); 3238263508Sdim else 3239263508Sdim R = DBuilder.createImportedModule( 3240263508Sdim getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), 3241263508Sdim getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())), 3242263508Sdim getLineNumber(NA.getLocation()), NA.getName()); 3243263508Sdim VH = R; 3244263508Sdim return R; 3245263508Sdim} 3246263508Sdim 3247203955Srdivacky/// getOrCreateNamesSpace - Return namespace descriptor for the given 3248203955Srdivacky/// namespace decl. 3249263508Sdimllvm::DINameSpace 3250218893SdimCGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) { 3251263508Sdim NSDecl = NSDecl->getCanonicalDecl(); 3252263508Sdim llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = 3253203955Srdivacky NameSpaceCache.find(NSDecl); 3254203955Srdivacky if (I != NameSpaceCache.end()) 3255203955Srdivacky return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); 3256263508Sdim 3257208600Srdivacky unsigned LineNo = getLineNumber(NSDecl->getLocation()); 3258218893Sdim llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation()); 3259263508Sdim llvm::DIDescriptor Context = 3260218893Sdim getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext())); 3261203955Srdivacky llvm::DINameSpace NS = 3262219077Sdim DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo); 3263208600Srdivacky NameSpaceCache[NSDecl] = llvm::WeakVH(NS); 3264203955Srdivacky return NS; 3265203955Srdivacky} 3266221345Sdim 3267249423Sdimvoid CGDebugInfo::finalize() { 3268234353Sdim for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI 3269234353Sdim = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) { 3270234353Sdim llvm::DIType Ty, RepTy; 3271234353Sdim // Verify that the debug info still exists. 3272239462Sdim if (llvm::Value *V = VI->second) 3273239462Sdim Ty = llvm::DIType(cast<llvm::MDNode>(V)); 3274263508Sdim 3275234353Sdim llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 3276234353Sdim TypeCache.find(VI->first); 3277234353Sdim if (it != TypeCache.end()) { 3278234353Sdim // Verify that the debug info still exists. 3279239462Sdim if (llvm::Value *V = it->second) 3280239462Sdim RepTy = llvm::DIType(cast<llvm::MDNode>(V)); 3281234353Sdim } 3282249423Sdim 3283263508Sdim if (Ty && Ty.isForwardDecl() && RepTy) 3284234353Sdim Ty.replaceAllUsesWith(RepTy); 3285234353Sdim } 3286249423Sdim 3287249423Sdim // We keep our own list of retained types, because we need to look 3288249423Sdim // up the final type in the type cache. 3289249423Sdim for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(), 3290249423Sdim RE = RetainedTypes.end(); RI != RE; ++RI) 3291249423Sdim DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI]))); 3292249423Sdim 3293234353Sdim DBuilder.finalize(); 3294221345Sdim} 3295