clang API Documentation
00001 //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This coordinates the debug information generation while generating code. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "CGDebugInfo.h" 00015 #include "CodeGenFunction.h" 00016 #include "CodeGenModule.h" 00017 #include "CGBlocks.h" 00018 #include "clang/AST/ASTContext.h" 00019 #include "clang/AST/DeclFriend.h" 00020 #include "clang/AST/DeclObjC.h" 00021 #include "clang/AST/DeclTemplate.h" 00022 #include "clang/AST/Expr.h" 00023 #include "clang/AST/RecordLayout.h" 00024 #include "clang/Basic/SourceManager.h" 00025 #include "clang/Basic/FileManager.h" 00026 #include "clang/Basic/Version.h" 00027 #include "clang/Frontend/CodeGenOptions.h" 00028 #include "llvm/Constants.h" 00029 #include "llvm/DerivedTypes.h" 00030 #include "llvm/Instructions.h" 00031 #include "llvm/Intrinsics.h" 00032 #include "llvm/Module.h" 00033 #include "llvm/ADT/StringExtras.h" 00034 #include "llvm/ADT/SmallVector.h" 00035 #include "llvm/Support/Dwarf.h" 00036 #include "llvm/Support/FileSystem.h" 00037 #include "llvm/Target/TargetData.h" 00038 using namespace clang; 00039 using namespace clang::CodeGen; 00040 00041 CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) 00042 : CGM(CGM), DBuilder(CGM.getModule()), 00043 BlockLiteralGenericSet(false) { 00044 CreateCompileUnit(); 00045 } 00046 00047 CGDebugInfo::~CGDebugInfo() { 00048 assert(LexicalBlockStack.empty() && 00049 "Region stack mismatch, stack not empty!"); 00050 } 00051 00052 void CGDebugInfo::setLocation(SourceLocation Loc) { 00053 // If the new location isn't valid return. 00054 if (!Loc.isValid()) return; 00055 00056 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); 00057 00058 // If we've changed files in the middle of a lexical scope go ahead 00059 // and create a new lexical scope with file node if it's different 00060 // from the one in the scope. 00061 if (LexicalBlockStack.empty()) return; 00062 00063 SourceManager &SM = CGM.getContext().getSourceManager(); 00064 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); 00065 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc); 00066 00067 if (PCLoc.isInvalid() || PPLoc.isInvalid() || 00068 !strcmp(PPLoc.getFilename(), PCLoc.getFilename())) 00069 return; 00070 00071 llvm::MDNode *LB = LexicalBlockStack.back(); 00072 llvm::DIScope Scope = llvm::DIScope(LB); 00073 if (Scope.isLexicalBlockFile()) { 00074 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB); 00075 llvm::DIDescriptor D 00076 = DBuilder.createLexicalBlockFile(LBF.getScope(), 00077 getOrCreateFile(CurLoc)); 00078 llvm::MDNode *N = D; 00079 LexicalBlockStack.pop_back(); 00080 LexicalBlockStack.push_back(N); 00081 } else if (Scope.isLexicalBlock()) { 00082 llvm::DIDescriptor D 00083 = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)); 00084 llvm::MDNode *N = D; 00085 LexicalBlockStack.pop_back(); 00086 LexicalBlockStack.push_back(N); 00087 } 00088 } 00089 00090 /// getContextDescriptor - Get context info for the decl. 00091 llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) { 00092 if (!Context) 00093 return TheCU; 00094 00095 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator 00096 I = RegionMap.find(Context); 00097 if (I != RegionMap.end()) 00098 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second)); 00099 00100 // Check namespace. 00101 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) 00102 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl)); 00103 00104 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) { 00105 if (!RDecl->isDependentType()) { 00106 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), 00107 getOrCreateMainFile()); 00108 return llvm::DIDescriptor(Ty); 00109 } 00110 } 00111 return TheCU; 00112 } 00113 00114 /// getFunctionName - Get function name for the given FunctionDecl. If the 00115 /// name is constructred on demand (e.g. C++ destructor) then the name 00116 /// is stored on the side. 00117 StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { 00118 assert (FD && "Invalid FunctionDecl!"); 00119 IdentifierInfo *FII = FD->getIdentifier(); 00120 FunctionTemplateSpecializationInfo *Info 00121 = FD->getTemplateSpecializationInfo(); 00122 if (!Info && FII) 00123 return FII->getName(); 00124 00125 // Otherwise construct human readable name for debug info. 00126 std::string NS = FD->getNameAsString(); 00127 00128 // Add any template specialization args. 00129 if (Info) { 00130 const TemplateArgumentList *TArgs = Info->TemplateArguments; 00131 const TemplateArgument *Args = TArgs->data(); 00132 unsigned NumArgs = TArgs->size(); 00133 PrintingPolicy Policy(CGM.getLangOpts()); 00134 NS += TemplateSpecializationType::PrintTemplateArgumentList(Args, 00135 NumArgs, 00136 Policy); 00137 } 00138 00139 // Copy this name on the side and use its reference. 00140 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); 00141 memcpy(StrPtr, NS.data(), NS.length()); 00142 return StringRef(StrPtr, NS.length()); 00143 } 00144 00145 StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { 00146 SmallString<256> MethodName; 00147 llvm::raw_svector_ostream OS(MethodName); 00148 OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; 00149 const DeclContext *DC = OMD->getDeclContext(); 00150 if (const ObjCImplementationDecl *OID = 00151 dyn_cast<const ObjCImplementationDecl>(DC)) { 00152 OS << OID->getName(); 00153 } else if (const ObjCInterfaceDecl *OID = 00154 dyn_cast<const ObjCInterfaceDecl>(DC)) { 00155 OS << OID->getName(); 00156 } else if (const ObjCCategoryImplDecl *OCD = 00157 dyn_cast<const ObjCCategoryImplDecl>(DC)){ 00158 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' << 00159 OCD->getIdentifier()->getNameStart() << ')'; 00160 } 00161 OS << ' ' << OMD->getSelector().getAsString() << ']'; 00162 00163 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell()); 00164 memcpy(StrPtr, MethodName.begin(), OS.tell()); 00165 return StringRef(StrPtr, OS.tell()); 00166 } 00167 00168 /// getSelectorName - Return selector name. This is used for debugging 00169 /// info. 00170 StringRef CGDebugInfo::getSelectorName(Selector S) { 00171 const std::string &SName = S.getAsString(); 00172 char *StrPtr = DebugInfoNames.Allocate<char>(SName.size()); 00173 memcpy(StrPtr, SName.data(), SName.size()); 00174 return StringRef(StrPtr, SName.size()); 00175 } 00176 00177 /// getClassName - Get class name including template argument list. 00178 StringRef 00179 CGDebugInfo::getClassName(const RecordDecl *RD) { 00180 const ClassTemplateSpecializationDecl *Spec 00181 = dyn_cast<ClassTemplateSpecializationDecl>(RD); 00182 if (!Spec) 00183 return RD->getName(); 00184 00185 const TemplateArgument *Args; 00186 unsigned NumArgs; 00187 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 00188 const TemplateSpecializationType *TST = 00189 cast<TemplateSpecializationType>(TAW->getType()); 00190 Args = TST->getArgs(); 00191 NumArgs = TST->getNumArgs(); 00192 } else { 00193 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 00194 Args = TemplateArgs.data(); 00195 NumArgs = TemplateArgs.size(); 00196 } 00197 StringRef Name = RD->getIdentifier()->getName(); 00198 PrintingPolicy Policy(CGM.getLangOpts()); 00199 std::string TemplateArgList = 00200 TemplateSpecializationType::PrintTemplateArgumentList(Args, NumArgs, Policy); 00201 00202 // Copy this name on the side and use its reference. 00203 size_t Length = Name.size() + TemplateArgList.size(); 00204 char *StrPtr = DebugInfoNames.Allocate<char>(Length); 00205 memcpy(StrPtr, Name.data(), Name.size()); 00206 memcpy(StrPtr + Name.size(), TemplateArgList.data(), TemplateArgList.size()); 00207 return StringRef(StrPtr, Length); 00208 } 00209 00210 /// getOrCreateFile - Get the file debug info descriptor for the input location. 00211 llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { 00212 if (!Loc.isValid()) 00213 // If Location is not valid then use main input file. 00214 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); 00215 00216 SourceManager &SM = CGM.getContext().getSourceManager(); 00217 PresumedLoc PLoc = SM.getPresumedLoc(Loc); 00218 00219 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty()) 00220 // If the location is not valid then use main input file. 00221 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); 00222 00223 // Cache the results. 00224 const char *fname = PLoc.getFilename(); 00225 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = 00226 DIFileCache.find(fname); 00227 00228 if (it != DIFileCache.end()) { 00229 // Verify that the information still exists. 00230 if (&*it->second) 00231 return llvm::DIFile(cast<llvm::MDNode>(it->second)); 00232 } 00233 00234 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname()); 00235 00236 DIFileCache[fname] = F; 00237 return F; 00238 } 00239 00240 /// getOrCreateMainFile - Get the file info for main compile unit. 00241 llvm::DIFile CGDebugInfo::getOrCreateMainFile() { 00242 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory()); 00243 } 00244 00245 /// getLineNumber - Get line number for the location. If location is invalid 00246 /// then use current location. 00247 unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { 00248 if (Loc.isInvalid() && CurLoc.isInvalid()) 00249 return 0; 00250 SourceManager &SM = CGM.getContext().getSourceManager(); 00251 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); 00252 return PLoc.isValid()? PLoc.getLine() : 0; 00253 } 00254 00255 /// getColumnNumber - Get column number for the location. If location is 00256 /// invalid then use current location. 00257 unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) { 00258 if (Loc.isInvalid() && CurLoc.isInvalid()) 00259 return 0; 00260 SourceManager &SM = CGM.getContext().getSourceManager(); 00261 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); 00262 return PLoc.isValid()? PLoc.getColumn() : 0; 00263 } 00264 00265 StringRef CGDebugInfo::getCurrentDirname() { 00266 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) 00267 return CGM.getCodeGenOpts().DebugCompilationDir; 00268 00269 if (!CWDName.empty()) 00270 return CWDName; 00271 SmallString<256> CWD; 00272 llvm::sys::fs::current_path(CWD); 00273 char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size()); 00274 memcpy(CompDirnamePtr, CWD.data(), CWD.size()); 00275 return CWDName = StringRef(CompDirnamePtr, CWD.size()); 00276 } 00277 00278 /// CreateCompileUnit - Create new compile unit. 00279 void CGDebugInfo::CreateCompileUnit() { 00280 00281 // Get absolute path name. 00282 SourceManager &SM = CGM.getContext().getSourceManager(); 00283 std::string MainFileName = CGM.getCodeGenOpts().MainFileName; 00284 if (MainFileName.empty()) 00285 MainFileName = "<unknown>"; 00286 00287 // The main file name provided via the "-main-file-name" option contains just 00288 // the file name itself with no path information. This file name may have had 00289 // a relative path, so we look into the actual file entry for the main 00290 // file to determine the real absolute path for the file. 00291 std::string MainFileDir; 00292 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 00293 MainFileDir = MainFile->getDir()->getName(); 00294 if (MainFileDir != ".") 00295 MainFileName = MainFileDir + "/" + MainFileName; 00296 } 00297 00298 // Save filename string. 00299 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length()); 00300 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length()); 00301 StringRef Filename(FilenamePtr, MainFileName.length()); 00302 00303 unsigned LangTag; 00304 const LangOptions &LO = CGM.getLangOpts(); 00305 if (LO.CPlusPlus) { 00306 if (LO.ObjC1) 00307 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; 00308 else 00309 LangTag = llvm::dwarf::DW_LANG_C_plus_plus; 00310 } else if (LO.ObjC1) { 00311 LangTag = llvm::dwarf::DW_LANG_ObjC; 00312 } else if (LO.C99) { 00313 LangTag = llvm::dwarf::DW_LANG_C99; 00314 } else { 00315 LangTag = llvm::dwarf::DW_LANG_C89; 00316 } 00317 00318 std::string Producer = getClangFullVersion(); 00319 00320 // Figure out which version of the ObjC runtime we have. 00321 unsigned RuntimeVers = 0; 00322 if (LO.ObjC1) 00323 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; 00324 00325 // Create new compile unit. 00326 DBuilder.createCompileUnit( 00327 LangTag, Filename, getCurrentDirname(), 00328 Producer, 00329 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); 00330 // FIXME - Eliminate TheCU. 00331 TheCU = llvm::DICompileUnit(DBuilder.getCU()); 00332 } 00333 00334 /// CreateType - Get the Basic type from the cache or create a new 00335 /// one if necessary. 00336 llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) { 00337 unsigned Encoding = 0; 00338 StringRef BTName; 00339 switch (BT->getKind()) { 00340 #define BUILTIN_TYPE(Id, SingletonId) 00341 #define PLACEHOLDER_TYPE(Id, SingletonId) \ 00342 case BuiltinType::Id: 00343 #include "clang/AST/BuiltinTypes.def" 00344 case BuiltinType::Dependent: 00345 llvm_unreachable("Unexpected builtin type"); 00346 case BuiltinType::NullPtr: 00347 return DBuilder. 00348 createNullPtrType(BT->getName(CGM.getContext().getLangOpts())); 00349 case BuiltinType::Void: 00350 return llvm::DIType(); 00351 case BuiltinType::ObjCClass: 00352 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 00353 "objc_class", TheCU, 00354 getOrCreateMainFile(), 0); 00355 case BuiltinType::ObjCId: { 00356 // typedef struct objc_class *Class; 00357 // typedef struct objc_object { 00358 // Class isa; 00359 // } *id; 00360 00361 // TODO: Cache these two types to avoid duplicates. 00362 llvm::DIType OCTy = 00363 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 00364 "objc_class", TheCU, getOrCreateMainFile(), 0); 00365 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 00366 00367 llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size); 00368 00369 SmallVector<llvm::Value *, 16> EltTys; 00370 llvm::DIType FieldTy = 00371 DBuilder.createMemberType(getOrCreateMainFile(), "isa", 00372 getOrCreateMainFile(), 0, Size, 00373 0, 0, 0, ISATy); 00374 EltTys.push_back(FieldTy); 00375 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); 00376 00377 return DBuilder.createStructType(TheCU, "objc_object", 00378 getOrCreateMainFile(), 00379 0, 0, 0, 0, Elements); 00380 } 00381 case BuiltinType::ObjCSel: { 00382 return 00383 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 00384 "objc_selector", TheCU, getOrCreateMainFile(), 00385 0); 00386 } 00387 case BuiltinType::UChar: 00388 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 00389 case BuiltinType::Char_S: 00390 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 00391 case BuiltinType::Char16: 00392 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break; 00393 case BuiltinType::UShort: 00394 case BuiltinType::UInt: 00395 case BuiltinType::UInt128: 00396 case BuiltinType::ULong: 00397 case BuiltinType::WChar_U: 00398 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 00399 case BuiltinType::Short: 00400 case BuiltinType::Int: 00401 case BuiltinType::Int128: 00402 case BuiltinType::Long: 00403 case BuiltinType::WChar_S: 00404 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 00405 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 00406 case BuiltinType::Half: 00407 case BuiltinType::Float: 00408 case BuiltinType::LongDouble: 00409 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 00410 } 00411 00412 switch (BT->getKind()) { 00413 case BuiltinType::Long: BTName = "long int"; break; 00414 case BuiltinType::LongLong: BTName = "long long int"; break; 00415 case BuiltinType::ULong: BTName = "long unsigned int"; break; 00416 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break; 00417 default: 00418 BTName = BT->getName(CGM.getContext().getLangOpts()); 00419 break; 00420 } 00421 // Bit size, align and offset of the type. 00422 uint64_t Size = CGM.getContext().getTypeSize(BT); 00423 uint64_t Align = CGM.getContext().getTypeAlign(BT); 00424 llvm::DIType DbgTy = 00425 DBuilder.createBasicType(BTName, Size, Align, Encoding); 00426 return DbgTy; 00427 } 00428 00429 llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) { 00430 // Bit size, align and offset of the type. 00431 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; 00432 if (Ty->isComplexIntegerType()) 00433 Encoding = llvm::dwarf::DW_ATE_lo_user; 00434 00435 uint64_t Size = CGM.getContext().getTypeSize(Ty); 00436 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 00437 llvm::DIType DbgTy = 00438 DBuilder.createBasicType("complex", Size, Align, Encoding); 00439 00440 return DbgTy; 00441 } 00442 00443 /// CreateCVRType - Get the qualified type from the cache or create 00444 /// a new one if necessary. 00445 llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { 00446 QualifierCollector Qc; 00447 const Type *T = Qc.strip(Ty); 00448 00449 // Ignore these qualifiers for now. 00450 Qc.removeObjCGCAttr(); 00451 Qc.removeAddressSpace(); 00452 Qc.removeObjCLifetime(); 00453 00454 // We will create one Derived type for one qualifier and recurse to handle any 00455 // additional ones. 00456 unsigned Tag; 00457 if (Qc.hasConst()) { 00458 Tag = llvm::dwarf::DW_TAG_const_type; 00459 Qc.removeConst(); 00460 } else if (Qc.hasVolatile()) { 00461 Tag = llvm::dwarf::DW_TAG_volatile_type; 00462 Qc.removeVolatile(); 00463 } else if (Qc.hasRestrict()) { 00464 Tag = llvm::dwarf::DW_TAG_restrict_type; 00465 Qc.removeRestrict(); 00466 } else { 00467 assert(Qc.empty() && "Unknown type qualifier for debug info"); 00468 return getOrCreateType(QualType(T, 0), Unit); 00469 } 00470 00471 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); 00472 00473 // No need to fill in the Name, Line, Size, Alignment, Offset in case of 00474 // CVR derived types. 00475 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy); 00476 00477 return DbgTy; 00478 } 00479 00480 llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, 00481 llvm::DIFile Unit) { 00482 llvm::DIType DbgTy = 00483 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 00484 Ty->getPointeeType(), Unit); 00485 return DbgTy; 00486 } 00487 00488 llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 00489 llvm::DIFile Unit) { 00490 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 00491 Ty->getPointeeType(), Unit); 00492 } 00493 00494 // Creates a forward declaration for a RecordDecl in the given context. 00495 llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD, 00496 llvm::DIDescriptor Ctx) { 00497 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 00498 unsigned Line = getLineNumber(RD->getLocation()); 00499 StringRef RDName = RD->getName(); 00500 00501 // Get the tag. 00502 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 00503 unsigned Tag = 0; 00504 if (CXXDecl) { 00505 RDName = getClassName(RD); 00506 Tag = llvm::dwarf::DW_TAG_class_type; 00507 } 00508 else if (RD->isStruct()) 00509 Tag = llvm::dwarf::DW_TAG_structure_type; 00510 else if (RD->isUnion()) 00511 Tag = llvm::dwarf::DW_TAG_union_type; 00512 else 00513 llvm_unreachable("Unknown RecordDecl type!"); 00514 00515 // Create the type. 00516 return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line); 00517 } 00518 00519 // Walk up the context chain and create forward decls for record decls, 00520 // and normal descriptors for namespaces. 00521 llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) { 00522 if (!Context) 00523 return TheCU; 00524 00525 // See if we already have the parent. 00526 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator 00527 I = RegionMap.find(Context); 00528 if (I != RegionMap.end()) 00529 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second)); 00530 00531 // Check namespace. 00532 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) 00533 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl)); 00534 00535 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) { 00536 if (!RD->isDependentType()) { 00537 llvm::DIType Ty = getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD), 00538 getOrCreateMainFile()); 00539 return llvm::DIDescriptor(Ty); 00540 } 00541 } 00542 return TheCU; 00543 } 00544 00545 /// CreatePointeeType - Create Pointee type. If Pointee is a record 00546 /// then emit record's fwd if debug info size reduction is enabled. 00547 llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy, 00548 llvm::DIFile Unit) { 00549 if (CGM.getCodeGenOpts().DebugInfo != CodeGenOptions::LimitedDebugInfo) 00550 return getOrCreateType(PointeeTy, Unit); 00551 00552 // Limit debug info for the pointee type. 00553 00554 // If we have an existing type, use that, it's still smaller than creating 00555 // a new type. 00556 llvm::DIType Ty = getTypeOrNull(PointeeTy); 00557 if (Ty.Verify()) return Ty; 00558 00559 // Handle qualifiers. 00560 if (PointeeTy.hasLocalQualifiers()) 00561 return CreateQualifiedType(PointeeTy, Unit); 00562 00563 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) { 00564 RecordDecl *RD = RTy->getDecl(); 00565 llvm::DIDescriptor FDContext = 00566 getContextDescriptor(cast<Decl>(RD->getDeclContext())); 00567 llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext); 00568 TypeCache[QualType(RTy, 0).getAsOpaquePtr()] = RetTy; 00569 return RetTy; 00570 } 00571 return getOrCreateType(PointeeTy, Unit); 00572 00573 } 00574 00575 llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, 00576 const Type *Ty, 00577 QualType PointeeTy, 00578 llvm::DIFile Unit) { 00579 if (Tag == llvm::dwarf::DW_TAG_reference_type) 00580 return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit)); 00581 00582 // Bit size, align and offset of the type. 00583 // Size is always the size of a pointer. We can't use getTypeSize here 00584 // because that does not return the correct value for references. 00585 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); 00586 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS); 00587 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 00588 00589 return DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit), 00590 Size, Align); 00591 } 00592 00593 llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, 00594 llvm::DIFile Unit) { 00595 if (BlockLiteralGenericSet) 00596 return BlockLiteralGeneric; 00597 00598 SmallVector<llvm::Value *, 8> EltTys; 00599 llvm::DIType FieldTy; 00600 QualType FType; 00601 uint64_t FieldSize, FieldOffset; 00602 unsigned FieldAlign; 00603 llvm::DIArray Elements; 00604 llvm::DIType EltTy, DescTy; 00605 00606 FieldOffset = 0; 00607 FType = CGM.getContext().UnsignedLongTy; 00608 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); 00609 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); 00610 00611 Elements = DBuilder.getOrCreateArray(EltTys); 00612 EltTys.clear(); 00613 00614 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock; 00615 unsigned LineNo = getLineNumber(CurLoc); 00616 00617 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", 00618 Unit, LineNo, FieldOffset, 0, 00619 Flags, Elements); 00620 00621 // Bit size, align and offset of the type. 00622 uint64_t Size = CGM.getContext().getTypeSize(Ty); 00623 00624 DescTy = DBuilder.createPointerType(EltTy, Size); 00625 00626 FieldOffset = 0; 00627 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 00628 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 00629 FType = CGM.getContext().IntTy; 00630 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 00631 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); 00632 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 00633 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); 00634 00635 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 00636 FieldTy = DescTy; 00637 FieldSize = CGM.getContext().getTypeSize(Ty); 00638 FieldAlign = CGM.getContext().getTypeAlign(Ty); 00639 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit, 00640 LineNo, FieldSize, FieldAlign, 00641 FieldOffset, 0, FieldTy); 00642 EltTys.push_back(FieldTy); 00643 00644 FieldOffset += FieldSize; 00645 Elements = DBuilder.getOrCreateArray(EltTys); 00646 00647 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", 00648 Unit, LineNo, FieldOffset, 0, 00649 Flags, Elements); 00650 00651 BlockLiteralGenericSet = true; 00652 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size); 00653 return BlockLiteralGeneric; 00654 } 00655 00656 llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) { 00657 // Typedefs are derived from some other type. If we have a typedef of a 00658 // typedef, make sure to emit the whole chain. 00659 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 00660 if (!Src.Verify()) 00661 return llvm::DIType(); 00662 // We don't set size information, but do specify where the typedef was 00663 // declared. 00664 unsigned Line = getLineNumber(Ty->getDecl()->getLocation()); 00665 const TypedefNameDecl *TyDecl = Ty->getDecl(); 00666 00667 llvm::DIDescriptor TypedefContext = 00668 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())); 00669 00670 return 00671 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext); 00672 } 00673 00674 llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 00675 llvm::DIFile Unit) { 00676 SmallVector<llvm::Value *, 16> EltTys; 00677 00678 // Add the result type at least. 00679 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 00680 00681 // Set up remainder of arguments if there is a prototype. 00682 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 00683 if (isa<FunctionNoProtoType>(Ty)) 00684 EltTys.push_back(DBuilder.createUnspecifiedParameter()); 00685 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { 00686 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 00687 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); 00688 } 00689 00690 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys); 00691 00692 llvm::DIType DbgTy = DBuilder.createSubroutineType(Unit, EltTypeArray); 00693 return DbgTy; 00694 } 00695 00696 00697 void CGDebugInfo:: 00698 CollectRecordStaticVars(const RecordDecl *RD, llvm::DIType FwdDecl) { 00699 00700 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end(); 00701 I != E; ++I) 00702 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) { 00703 if (V->getInit()) { 00704 const APValue *Value = V->evaluateValue(); 00705 if (Value && Value->isInt()) { 00706 llvm::ConstantInt *CI 00707 = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt()); 00708 00709 // Create the descriptor for static variable. 00710 llvm::DIFile VUnit = getOrCreateFile(V->getLocation()); 00711 StringRef VName = V->getName(); 00712 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit); 00713 // Do not use DIGlobalVariable for enums. 00714 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) { 00715 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit, 00716 getLineNumber(V->getLocation()), 00717 VTy, true, CI); 00718 } 00719 } 00720 } 00721 } 00722 } 00723 00724 llvm::DIType CGDebugInfo::createFieldType(StringRef name, 00725 QualType type, 00726 uint64_t sizeInBitsOverride, 00727 SourceLocation loc, 00728 AccessSpecifier AS, 00729 uint64_t offsetInBits, 00730 llvm::DIFile tunit, 00731 llvm::DIDescriptor scope) { 00732 llvm::DIType debugType = getOrCreateType(type, tunit); 00733 00734 // Get the location for the field. 00735 llvm::DIFile file = getOrCreateFile(loc); 00736 unsigned line = getLineNumber(loc); 00737 00738 uint64_t sizeInBits = 0; 00739 unsigned alignInBits = 0; 00740 if (!type->isIncompleteArrayType()) { 00741 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type); 00742 00743 if (sizeInBitsOverride) 00744 sizeInBits = sizeInBitsOverride; 00745 } 00746 00747 unsigned flags = 0; 00748 if (AS == clang::AS_private) 00749 flags |= llvm::DIDescriptor::FlagPrivate; 00750 else if (AS == clang::AS_protected) 00751 flags |= llvm::DIDescriptor::FlagProtected; 00752 00753 return DBuilder.createMemberType(scope, name, file, line, sizeInBits, 00754 alignInBits, offsetInBits, flags, debugType); 00755 } 00756 00757 /// CollectRecordFields - A helper function to collect debug info for 00758 /// record fields. This is used while creating debug info entry for a Record. 00759 void CGDebugInfo:: 00760 CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit, 00761 SmallVectorImpl<llvm::Value *> &elements, 00762 llvm::DIType RecordTy) { 00763 unsigned fieldNo = 0; 00764 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); 00765 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record); 00766 00767 // For C++11 Lambdas a Fields will be the same as a Capture, but the Capture 00768 // has the name and the location of the variable so we should iterate over 00769 // both concurrently. 00770 if (CXXDecl && CXXDecl->isLambda()) { 00771 RecordDecl::field_iterator Field = CXXDecl->field_begin(); 00772 unsigned fieldno = 0; 00773 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(), 00774 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) { 00775 const LambdaExpr::Capture C = *I; 00776 // TODO: Need to handle 'this' in some way by probably renaming the 00777 // this of the lambda class and having a field member of 'this'. 00778 if (C.capturesVariable()) { 00779 VarDecl *V = C.getCapturedVar(); 00780 llvm::DIFile VUnit = getOrCreateFile(C.getLocation()); 00781 StringRef VName = V->getName(); 00782 uint64_t SizeInBitsOverride = 0; 00783 if (Field->isBitField()) { 00784 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext()); 00785 assert(SizeInBitsOverride && "found named 0-width bitfield"); 00786 } 00787 llvm::DIType fieldType 00788 = createFieldType(VName, Field->getType(), SizeInBitsOverride, C.getLocation(), 00789 Field->getAccess(), layout.getFieldOffset(fieldno), 00790 VUnit, RecordTy); 00791 elements.push_back(fieldType); 00792 } 00793 } 00794 } else { 00795 bool IsMsStruct = record->hasAttr<MsStructAttr>(); 00796 const FieldDecl *LastFD = 0; 00797 for (RecordDecl::field_iterator I = record->field_begin(), 00798 E = record->field_end(); 00799 I != E; ++I, ++fieldNo) { 00800 FieldDecl *field = &*I; 00801 00802 if (IsMsStruct) { 00803 // Zero-length bitfields following non-bitfield members are ignored 00804 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) { 00805 --fieldNo; 00806 continue; 00807 } 00808 LastFD = field; 00809 } 00810 00811 StringRef name = field->getName(); 00812 QualType type = field->getType(); 00813 00814 // Ignore unnamed fields unless they're anonymous structs/unions. 00815 if (name.empty() && !type->isRecordType()) { 00816 LastFD = field; 00817 continue; 00818 } 00819 00820 uint64_t SizeInBitsOverride = 0; 00821 if (field->isBitField()) { 00822 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext()); 00823 assert(SizeInBitsOverride && "found named 0-width bitfield"); 00824 } 00825 00826 llvm::DIType fieldType 00827 = createFieldType(name, type, SizeInBitsOverride, 00828 field->getLocation(), field->getAccess(), 00829 layout.getFieldOffset(fieldNo), tunit, RecordTy); 00830 00831 elements.push_back(fieldType); 00832 } 00833 } 00834 } 00835 00836 /// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This 00837 /// function type is not updated to include implicit "this" pointer. Use this 00838 /// routine to get a method type which includes "this" pointer. 00839 llvm::DIType 00840 CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, 00841 llvm::DIFile Unit) { 00842 llvm::DIType FnTy 00843 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(), 00844 0), 00845 Unit); 00846 00847 // Add "this" pointer. 00848 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray(); 00849 assert (Args.getNumElements() && "Invalid number of arguments!"); 00850 00851 SmallVector<llvm::Value *, 16> Elts; 00852 00853 // First element is always return type. For 'void' functions it is NULL. 00854 Elts.push_back(Args.getElement(0)); 00855 00856 if (!Method->isStatic()) { 00857 // "this" pointer is always first argument. 00858 QualType ThisPtr = Method->getThisType(CGM.getContext()); 00859 00860 const CXXRecordDecl *RD = Method->getParent(); 00861 if (isa<ClassTemplateSpecializationDecl>(RD)) { 00862 // Create pointer type directly in this case. 00863 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); 00864 QualType PointeeTy = ThisPtrTy->getPointeeType(); 00865 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); 00866 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS); 00867 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy); 00868 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit); 00869 llvm::DIType ThisPtrType = DBuilder.createPointerType(PointeeType, Size, Align); 00870 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; 00871 // TODO: This and the artificial type below are misleading, the 00872 // types aren't artificial the argument is, but the current 00873 // metadata doesn't represent that. 00874 ThisPtrType = DBuilder.createArtificialType(ThisPtrType); 00875 Elts.push_back(ThisPtrType); 00876 } else { 00877 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit); 00878 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType; 00879 ThisPtrType = DBuilder.createArtificialType(ThisPtrType); 00880 Elts.push_back(ThisPtrType); 00881 } 00882 } 00883 00884 // Copy rest of the arguments. 00885 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) 00886 Elts.push_back(Args.getElement(i)); 00887 00888 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); 00889 00890 return DBuilder.createSubroutineType(Unit, EltTypeArray); 00891 } 00892 00893 /// isFunctionLocalClass - Return true if CXXRecordDecl is defined 00894 /// inside a function. 00895 static bool isFunctionLocalClass(const CXXRecordDecl *RD) { 00896 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext())) 00897 return isFunctionLocalClass(NRD); 00898 if (isa<FunctionDecl>(RD->getDeclContext())) 00899 return true; 00900 return false; 00901 } 00902 00903 /// CreateCXXMemberFunction - A helper function to create a DISubprogram for 00904 /// a single member function GlobalDecl. 00905 llvm::DISubprogram 00906 CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, 00907 llvm::DIFile Unit, 00908 llvm::DIType RecordTy) { 00909 bool IsCtorOrDtor = 00910 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); 00911 00912 StringRef MethodName = getFunctionName(Method); 00913 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); 00914 00915 // Since a single ctor/dtor corresponds to multiple functions, it doesn't 00916 // make sense to give a single ctor/dtor a linkage name. 00917 StringRef MethodLinkageName; 00918 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) 00919 MethodLinkageName = CGM.getMangledName(Method); 00920 00921 // Get the location for the method. 00922 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation()); 00923 unsigned MethodLine = getLineNumber(Method->getLocation()); 00924 00925 // Collect virtual method info. 00926 llvm::DIType ContainingType; 00927 unsigned Virtuality = 0; 00928 unsigned VIndex = 0; 00929 00930 if (Method->isVirtual()) { 00931 if (Method->isPure()) 00932 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; 00933 else 00934 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; 00935 00936 // It doesn't make sense to give a virtual destructor a vtable index, 00937 // since a single destructor has two entries in the vtable. 00938 if (!isa<CXXDestructorDecl>(Method)) 00939 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method); 00940 ContainingType = RecordTy; 00941 } 00942 00943 unsigned Flags = 0; 00944 if (Method->isImplicit()) 00945 Flags |= llvm::DIDescriptor::FlagArtificial; 00946 AccessSpecifier Access = Method->getAccess(); 00947 if (Access == clang::AS_private) 00948 Flags |= llvm::DIDescriptor::FlagPrivate; 00949 else if (Access == clang::AS_protected) 00950 Flags |= llvm::DIDescriptor::FlagProtected; 00951 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { 00952 if (CXXC->isExplicit()) 00953 Flags |= llvm::DIDescriptor::FlagExplicit; 00954 } else if (const CXXConversionDecl *CXXC = 00955 dyn_cast<CXXConversionDecl>(Method)) { 00956 if (CXXC->isExplicit()) 00957 Flags |= llvm::DIDescriptor::FlagExplicit; 00958 } 00959 if (Method->hasPrototype()) 00960 Flags |= llvm::DIDescriptor::FlagPrototyped; 00961 00962 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit); 00963 llvm::DISubprogram SP = 00964 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName, 00965 MethodDefUnit, MethodLine, 00966 MethodTy, /*isLocalToUnit=*/false, 00967 /* isDefinition=*/ false, 00968 Virtuality, VIndex, ContainingType, 00969 Flags, CGM.getLangOpts().Optimize, NULL, 00970 TParamsArray); 00971 00972 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP); 00973 00974 return SP; 00975 } 00976 00977 /// CollectCXXMemberFunctions - A helper function to collect debug info for 00978 /// C++ member functions. This is used while creating debug info entry for 00979 /// a Record. 00980 void CGDebugInfo:: 00981 CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, 00982 SmallVectorImpl<llvm::Value *> &EltTys, 00983 llvm::DIType RecordTy) { 00984 00985 // Since we want more than just the individual member decls if we 00986 // have templated functions iterate over every declaration to gather 00987 // the functions. 00988 for(DeclContext::decl_iterator I = RD->decls_begin(), 00989 E = RD->decls_end(); I != E; ++I) { 00990 Decl *D = *I; 00991 if (D->isImplicit() && !D->isUsed()) 00992 continue; 00993 00994 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) 00995 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); 00996 else if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) 00997 for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(), 00998 SE = FTD->spec_end(); SI != SE; ++SI) { 00999 FunctionDecl *FD = *SI; 01000 if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(FD)) 01001 EltTys.push_back(CreateCXXMemberFunction(M, Unit, RecordTy)); 01002 } 01003 } 01004 } 01005 01006 /// CollectCXXFriends - A helper function to collect debug info for 01007 /// C++ base classes. This is used while creating debug info entry for 01008 /// a Record. 01009 void CGDebugInfo:: 01010 CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit, 01011 SmallVectorImpl<llvm::Value *> &EltTys, 01012 llvm::DIType RecordTy) { 01013 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(), 01014 BE = RD->friend_end(); BI != BE; ++BI) { 01015 if ((*BI)->isUnsupportedFriend()) 01016 continue; 01017 if (TypeSourceInfo *TInfo = (*BI)->getFriendType()) 01018 EltTys.push_back(DBuilder.createFriend(RecordTy, 01019 getOrCreateType(TInfo->getType(), 01020 Unit))); 01021 } 01022 } 01023 01024 /// CollectCXXBases - A helper function to collect debug info for 01025 /// C++ base classes. This is used while creating debug info entry for 01026 /// a Record. 01027 void CGDebugInfo:: 01028 CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, 01029 SmallVectorImpl<llvm::Value *> &EltTys, 01030 llvm::DIType RecordTy) { 01031 01032 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 01033 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), 01034 BE = RD->bases_end(); BI != BE; ++BI) { 01035 unsigned BFlags = 0; 01036 uint64_t BaseOffset; 01037 01038 const CXXRecordDecl *Base = 01039 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); 01040 01041 if (BI->isVirtual()) { 01042 // virtual base offset offset is -ve. The code generator emits dwarf 01043 // expression where it expects +ve number. 01044 BaseOffset = 01045 0 - CGM.getVTableContext() 01046 .getVirtualBaseOffsetOffset(RD, Base).getQuantity(); 01047 BFlags = llvm::DIDescriptor::FlagVirtual; 01048 } else 01049 BaseOffset = RL.getBaseClassOffsetInBits(Base); 01050 // FIXME: Inconsistent units for BaseOffset. It is in bytes when 01051 // BI->isVirtual() and bits when not. 01052 01053 AccessSpecifier Access = BI->getAccessSpecifier(); 01054 if (Access == clang::AS_private) 01055 BFlags |= llvm::DIDescriptor::FlagPrivate; 01056 else if (Access == clang::AS_protected) 01057 BFlags |= llvm::DIDescriptor::FlagProtected; 01058 01059 llvm::DIType DTy = 01060 DBuilder.createInheritance(RecordTy, 01061 getOrCreateType(BI->getType(), Unit), 01062 BaseOffset, BFlags); 01063 EltTys.push_back(DTy); 01064 } 01065 } 01066 01067 /// CollectTemplateParams - A helper function to collect template parameters. 01068 llvm::DIArray CGDebugInfo:: 01069 CollectTemplateParams(const TemplateParameterList *TPList, 01070 const TemplateArgumentList &TAList, 01071 llvm::DIFile Unit) { 01072 SmallVector<llvm::Value *, 16> TemplateParams; 01073 for (unsigned i = 0, e = TAList.size(); i != e; ++i) { 01074 const TemplateArgument &TA = TAList[i]; 01075 const NamedDecl *ND = TPList->getParam(i); 01076 if (TA.getKind() == TemplateArgument::Type) { 01077 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit); 01078 llvm::DITemplateTypeParameter TTP = 01079 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy); 01080 TemplateParams.push_back(TTP); 01081 } else if (TA.getKind() == TemplateArgument::Integral) { 01082 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit); 01083 llvm::DITemplateValueParameter TVP = 01084 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy, 01085 TA.getAsIntegral()->getZExtValue()); 01086 TemplateParams.push_back(TVP); 01087 } 01088 } 01089 return DBuilder.getOrCreateArray(TemplateParams); 01090 } 01091 01092 /// CollectFunctionTemplateParams - A helper function to collect debug 01093 /// info for function template parameters. 01094 llvm::DIArray CGDebugInfo:: 01095 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) { 01096 if (FD->getTemplatedKind() == 01097 FunctionDecl::TK_FunctionTemplateSpecialization) { 01098 const TemplateParameterList *TList = 01099 FD->getTemplateSpecializationInfo()->getTemplate() 01100 ->getTemplateParameters(); 01101 return 01102 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit); 01103 } 01104 return llvm::DIArray(); 01105 } 01106 01107 /// CollectCXXTemplateParams - A helper function to collect debug info for 01108 /// template parameters. 01109 llvm::DIArray CGDebugInfo:: 01110 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial, 01111 llvm::DIFile Unit) { 01112 llvm::PointerUnion<ClassTemplateDecl *, 01113 ClassTemplatePartialSpecializationDecl *> 01114 PU = TSpecial->getSpecializedTemplateOrPartial(); 01115 01116 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ? 01117 PU.get<ClassTemplateDecl *>()->getTemplateParameters() : 01118 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters(); 01119 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs(); 01120 return CollectTemplateParams(TPList, TAList, Unit); 01121 } 01122 01123 /// getOrCreateVTablePtrType - Return debug info descriptor for vtable. 01124 llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { 01125 if (VTablePtrType.isValid()) 01126 return VTablePtrType; 01127 01128 ASTContext &Context = CGM.getContext(); 01129 01130 /* Function type */ 01131 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit); 01132 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy); 01133 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements); 01134 unsigned Size = Context.getTypeSize(Context.VoidPtrTy); 01135 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0, 01136 "__vtbl_ptr_type"); 01137 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); 01138 return VTablePtrType; 01139 } 01140 01141 /// getVTableName - Get vtable name for the given Class. 01142 StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { 01143 // Construct gdb compatible name name. 01144 std::string Name = "_vptr$" + RD->getNameAsString(); 01145 01146 // Copy this name on the side and use its reference. 01147 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); 01148 memcpy(StrPtr, Name.data(), Name.length()); 01149 return StringRef(StrPtr, Name.length()); 01150 } 01151 01152 01153 /// CollectVTableInfo - If the C++ class has vtable info then insert appropriate 01154 /// debug info entry in EltTys vector. 01155 void CGDebugInfo:: 01156 CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, 01157 SmallVectorImpl<llvm::Value *> &EltTys) { 01158 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 01159 01160 // If there is a primary base then it will hold vtable info. 01161 if (RL.getPrimaryBase()) 01162 return; 01163 01164 // If this class is not dynamic then there is not any vtable info to collect. 01165 if (!RD->isDynamicClass()) 01166 return; 01167 01168 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 01169 llvm::DIType VPTR 01170 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 01171 0, Size, 0, 0, 0, 01172 getOrCreateVTablePtrType(Unit)); 01173 EltTys.push_back(VPTR); 01174 } 01175 01176 /// getOrCreateRecordType - Emit record type's standalone debug info. 01177 llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy, 01178 SourceLocation Loc) { 01179 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 01180 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc)); 01181 return T; 01182 } 01183 01184 /// getOrCreateInterfaceType - Emit an objective c interface type standalone 01185 /// debug info. 01186 llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D, 01187 SourceLocation Loc) { 01188 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 01189 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc)); 01190 DBuilder.retainType(T); 01191 return T; 01192 } 01193 01194 /// CreateType - get structure or union type. 01195 llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) { 01196 RecordDecl *RD = Ty->getDecl(); 01197 01198 // Get overall information about the record type for the debug info. 01199 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 01200 01201 // Records and classes and unions can all be recursive. To handle them, we 01202 // first generate a debug descriptor for the struct as a forward declaration. 01203 // Then (if it is a definition) we go through and get debug info for all of 01204 // its members. Finally, we create a descriptor for the complete type (which 01205 // may refer to the forward decl if the struct is recursive) and replace all 01206 // uses of the forward declaration with the final definition. 01207 01208 llvm::DIType FwdDecl = getOrCreateLimitedType(QualType(Ty, 0), DefUnit); 01209 01210 if (FwdDecl.isForwardDecl()) 01211 return FwdDecl; 01212 01213 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(FwdDecl); 01214 01215 // Push the struct on region stack. 01216 LexicalBlockStack.push_back(FwdDeclNode); 01217 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl); 01218 01219 // Add this to the completed types cache since we're completing it. 01220 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 01221 01222 // Convert all the elements. 01223 SmallVector<llvm::Value *, 16> EltTys; 01224 01225 // Note: The split of CXXDecl information here is intentional, the 01226 // gdb tests will depend on a certain ordering at printout. The debug 01227 // information offsets are still correct if we merge them all together 01228 // though. 01229 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 01230 if (CXXDecl) { 01231 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl); 01232 CollectVTableInfo(CXXDecl, DefUnit, EltTys); 01233 } 01234 01235 // Collect static variables with initializers and other fields. 01236 CollectRecordStaticVars(RD, FwdDecl); 01237 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl); 01238 llvm::DIArray TParamsArray; 01239 if (CXXDecl) { 01240 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl); 01241 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl); 01242 if (const ClassTemplateSpecializationDecl *TSpecial 01243 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) 01244 TParamsArray = CollectCXXTemplateParams(TSpecial, DefUnit); 01245 } 01246 01247 LexicalBlockStack.pop_back(); 01248 RegionMap.erase(Ty->getDecl()); 01249 01250 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); 01251 // FIXME: Magic numbers ahoy! These should be changed when we 01252 // get some enums in llvm/Analysis/DebugInfo.h to refer to 01253 // them. 01254 if (RD->isUnion()) 01255 FwdDeclNode->replaceOperandWith(10, Elements); 01256 else if (CXXDecl) { 01257 FwdDeclNode->replaceOperandWith(10, Elements); 01258 FwdDeclNode->replaceOperandWith(13, TParamsArray); 01259 } else 01260 FwdDeclNode->replaceOperandWith(10, Elements); 01261 01262 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDeclNode); 01263 return llvm::DIType(FwdDeclNode); 01264 } 01265 01266 /// CreateType - get objective-c object type. 01267 llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty, 01268 llvm::DIFile Unit) { 01269 // Ignore protocols. 01270 return getOrCreateType(Ty->getBaseType(), Unit); 01271 } 01272 01273 /// CreateType - get objective-c interface type. 01274 llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 01275 llvm::DIFile Unit) { 01276 ObjCInterfaceDecl *ID = Ty->getDecl(); 01277 if (!ID) 01278 return llvm::DIType(); 01279 01280 // Get overall information about the record type for the debug info. 01281 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); 01282 unsigned Line = getLineNumber(ID->getLocation()); 01283 unsigned RuntimeLang = TheCU.getLanguage(); 01284 01285 // If this is just a forward declaration return a special forward-declaration 01286 // debug type since we won't be able to lay out the entire type. 01287 ObjCInterfaceDecl *Def = ID->getDefinition(); 01288 if (!Def) { 01289 llvm::DIType FwdDecl = 01290 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, 01291 ID->getName(), TheCU, DefUnit, Line, 01292 RuntimeLang); 01293 return FwdDecl; 01294 } 01295 01296 ID = Def; 01297 01298 // Bit size, align and offset of the type. 01299 uint64_t Size = CGM.getContext().getTypeSize(Ty); 01300 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 01301 01302 unsigned Flags = 0; 01303 if (ID->getImplementation()) 01304 Flags |= llvm::DIDescriptor::FlagObjcClassComplete; 01305 01306 llvm::DIType RealDecl = 01307 DBuilder.createStructType(Unit, ID->getName(), DefUnit, 01308 Line, Size, Align, Flags, 01309 llvm::DIArray(), RuntimeLang); 01310 01311 // Otherwise, insert it into the CompletedTypeCache so that recursive uses 01312 // will find it and we're emitting the complete type. 01313 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl; 01314 // Push the struct on region stack. 01315 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(RealDecl); 01316 01317 LexicalBlockStack.push_back(FwdDeclNode); 01318 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl); 01319 01320 // Convert all the elements. 01321 SmallVector<llvm::Value *, 16> EltTys; 01322 01323 ObjCInterfaceDecl *SClass = ID->getSuperClass(); 01324 if (SClass) { 01325 llvm::DIType SClassTy = 01326 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); 01327 if (!SClassTy.isValid()) 01328 return llvm::DIType(); 01329 01330 llvm::DIType InhTag = 01331 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0); 01332 EltTys.push_back(InhTag); 01333 } 01334 01335 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(), 01336 E = ID->prop_end(); I != E; ++I) { 01337 const ObjCPropertyDecl *PD = &*I; 01338 SourceLocation Loc = PD->getLocation(); 01339 llvm::DIFile PUnit = getOrCreateFile(Loc); 01340 unsigned PLine = getLineNumber(Loc); 01341 ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); 01342 ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); 01343 llvm::MDNode *PropertyNode = 01344 DBuilder.createObjCProperty(PD->getName(), 01345 PUnit, PLine, 01346 (Getter && Getter->isImplicit()) ? "" : 01347 getSelectorName(PD->getGetterName()), 01348 (Setter && Setter->isImplicit()) ? "" : 01349 getSelectorName(PD->getSetterName()), 01350 PD->getPropertyAttributes(), 01351 getOrCreateType(PD->getType(), PUnit)); 01352 EltTys.push_back(PropertyNode); 01353 } 01354 01355 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); 01356 unsigned FieldNo = 0; 01357 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; 01358 Field = Field->getNextIvar(), ++FieldNo) { 01359 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 01360 if (!FieldTy.isValid()) 01361 return llvm::DIType(); 01362 01363 StringRef FieldName = Field->getName(); 01364 01365 // Ignore unnamed fields. 01366 if (FieldName.empty()) 01367 continue; 01368 01369 // Get the location for the field. 01370 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation()); 01371 unsigned FieldLine = getLineNumber(Field->getLocation()); 01372 QualType FType = Field->getType(); 01373 uint64_t FieldSize = 0; 01374 unsigned FieldAlign = 0; 01375 01376 if (!FType->isIncompleteArrayType()) { 01377 01378 // Bit size, align and offset of the type. 01379 FieldSize = Field->isBitField() 01380 ? Field->getBitWidthValue(CGM.getContext()) 01381 : CGM.getContext().getTypeSize(FType); 01382 FieldAlign = CGM.getContext().getTypeAlign(FType); 01383 } 01384 01385 // We can't know the offset of our ivar in the structure if we're using 01386 // the non-fragile abi and the debugger should ignore the value anyways. 01387 // Call it the FieldNo+1 due to how debuggers use the information, 01388 // e.g. negating the value when it needs a lookup in the dynamic table. 01389 uint64_t FieldOffset = CGM.getLangOpts().ObjCNonFragileABI ? FieldNo+1 01390 : RL.getFieldOffset(FieldNo); 01391 01392 unsigned Flags = 0; 01393 if (Field->getAccessControl() == ObjCIvarDecl::Protected) 01394 Flags = llvm::DIDescriptor::FlagProtected; 01395 else if (Field->getAccessControl() == ObjCIvarDecl::Private) 01396 Flags = llvm::DIDescriptor::FlagPrivate; 01397 01398 llvm::MDNode *PropertyNode = NULL; 01399 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) { 01400 if (ObjCPropertyImplDecl *PImpD = 01401 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { 01402 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { 01403 SourceLocation Loc = PD->getLocation(); 01404 llvm::DIFile PUnit = getOrCreateFile(Loc); 01405 unsigned PLine = getLineNumber(Loc); 01406 ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); 01407 ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); 01408 PropertyNode = 01409 DBuilder.createObjCProperty(PD->getName(), 01410 PUnit, PLine, 01411 (Getter && Getter->isImplicit()) ? "" : 01412 getSelectorName(PD->getGetterName()), 01413 (Setter && Setter->isImplicit()) ? "" : 01414 getSelectorName(PD->getSetterName()), 01415 PD->getPropertyAttributes(), 01416 getOrCreateType(PD->getType(), PUnit)); 01417 } 01418 } 01419 } 01420 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, 01421 FieldLine, FieldSize, FieldAlign, 01422 FieldOffset, Flags, FieldTy, 01423 PropertyNode); 01424 EltTys.push_back(FieldTy); 01425 } 01426 01427 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); 01428 FwdDeclNode->replaceOperandWith(10, Elements); 01429 01430 LexicalBlockStack.pop_back(); 01431 return llvm::DIType(FwdDeclNode); 01432 } 01433 01434 llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) { 01435 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); 01436 int64_t NumElems = Ty->getNumElements(); 01437 int64_t LowerBound = 0; 01438 if (NumElems == 0) 01439 // If number of elements are not known then this is an unbounded array. 01440 // Use Low = 1, Hi = 0 to express such arrays. 01441 LowerBound = 1; 01442 else 01443 --NumElems; 01444 01445 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems); 01446 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); 01447 01448 uint64_t Size = CGM.getContext().getTypeSize(Ty); 01449 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 01450 01451 return 01452 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); 01453 } 01454 01455 llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 01456 llvm::DIFile Unit) { 01457 uint64_t Size; 01458 uint64_t Align; 01459 01460 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 01461 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 01462 Size = 0; 01463 Align = 01464 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); 01465 } else if (Ty->isIncompleteArrayType()) { 01466 Size = 0; 01467 Align = CGM.getContext().getTypeAlign(Ty->getElementType()); 01468 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) { 01469 Size = 0; 01470 Align = 0; 01471 } else { 01472 // Size and align of the whole array, not the element type. 01473 Size = CGM.getContext().getTypeSize(Ty); 01474 Align = CGM.getContext().getTypeAlign(Ty); 01475 } 01476 01477 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 01478 // interior arrays, do we care? Why aren't nested arrays represented the 01479 // obvious/recursive way? 01480 SmallVector<llvm::Value *, 8> Subscripts; 01481 QualType EltTy(Ty, 0); 01482 if (Ty->isIncompleteArrayType()) 01483 EltTy = Ty->getElementType(); 01484 else { 01485 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 01486 int64_t UpperBound = 0; 01487 int64_t LowerBound = 0; 01488 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) { 01489 if (CAT->getSize().getZExtValue()) 01490 UpperBound = CAT->getSize().getZExtValue() - 1; 01491 } else 01492 // This is an unbounded array. Use Low = 1, Hi = 0 to express such 01493 // arrays. 01494 LowerBound = 1; 01495 01496 // FIXME: Verify this is right for VLAs. 01497 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound, 01498 UpperBound)); 01499 EltTy = Ty->getElementType(); 01500 } 01501 } 01502 01503 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); 01504 01505 llvm::DIType DbgTy = 01506 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), 01507 SubscriptArray); 01508 return DbgTy; 01509 } 01510 01511 llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, 01512 llvm::DIFile Unit) { 01513 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, 01514 Ty, Ty->getPointeeType(), Unit); 01515 } 01516 01517 llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty, 01518 llvm::DIFile Unit) { 01519 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, 01520 Ty, Ty->getPointeeType(), Unit); 01521 } 01522 01523 llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, 01524 llvm::DIFile U) { 01525 QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); 01526 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); 01527 01528 if (!Ty->getPointeeType()->isFunctionType()) { 01529 // We have a data member pointer type. 01530 return PointerDiffDITy; 01531 } 01532 01533 // We have a member function pointer type. Treat it as a struct with two 01534 // ptrdiff_t members. 01535 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); 01536 01537 uint64_t FieldOffset = 0; 01538 llvm::Value *ElementTypes[2]; 01539 01540 // FIXME: This should probably be a function type instead. 01541 ElementTypes[0] = 01542 DBuilder.createMemberType(U, "ptr", U, 0, 01543 Info.first, Info.second, FieldOffset, 0, 01544 PointerDiffDITy); 01545 FieldOffset += Info.first; 01546 01547 ElementTypes[1] = 01548 DBuilder.createMemberType(U, "ptr", U, 0, 01549 Info.first, Info.second, FieldOffset, 0, 01550 PointerDiffDITy); 01551 01552 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes); 01553 01554 return DBuilder.createStructType(U, StringRef("test"), 01555 U, 0, FieldOffset, 01556 0, 0, Elements); 01557 } 01558 01559 llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, 01560 llvm::DIFile U) { 01561 // Ignore the atomic wrapping 01562 // FIXME: What is the correct representation? 01563 return getOrCreateType(Ty->getValueType(), U); 01564 } 01565 01566 /// CreateEnumType - get enumeration type. 01567 llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) { 01568 llvm::DIFile Unit = getOrCreateFile(ED->getLocation()); 01569 SmallVector<llvm::Value *, 16> Enumerators; 01570 01571 // Create DIEnumerator elements for each enumerator. 01572 for (EnumDecl::enumerator_iterator 01573 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); 01574 Enum != EnumEnd; ++Enum) { 01575 Enumerators.push_back( 01576 DBuilder.createEnumerator(Enum->getName(), 01577 Enum->getInitVal().getZExtValue())); 01578 } 01579 01580 // Return a CompositeType for the enum itself. 01581 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators); 01582 01583 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation()); 01584 unsigned Line = getLineNumber(ED->getLocation()); 01585 uint64_t Size = 0; 01586 uint64_t Align = 0; 01587 if (!ED->getTypeForDecl()->isIncompleteType()) { 01588 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); 01589 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl()); 01590 } 01591 llvm::DIDescriptor EnumContext = 01592 getContextDescriptor(cast<Decl>(ED->getDeclContext())); 01593 llvm::DIType DbgTy = 01594 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line, 01595 Size, Align, EltArray); 01596 return DbgTy; 01597 } 01598 01599 static QualType UnwrapTypeForDebugInfo(QualType T) { 01600 do { 01601 QualType LastT = T; 01602 switch (T->getTypeClass()) { 01603 default: 01604 return T; 01605 case Type::TemplateSpecialization: 01606 T = cast<TemplateSpecializationType>(T)->desugar(); 01607 break; 01608 case Type::TypeOfExpr: 01609 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); 01610 break; 01611 case Type::TypeOf: 01612 T = cast<TypeOfType>(T)->getUnderlyingType(); 01613 break; 01614 case Type::Decltype: 01615 T = cast<DecltypeType>(T)->getUnderlyingType(); 01616 break; 01617 case Type::UnaryTransform: 01618 T = cast<UnaryTransformType>(T)->getUnderlyingType(); 01619 break; 01620 case Type::Attributed: 01621 T = cast<AttributedType>(T)->getEquivalentType(); 01622 break; 01623 case Type::Elaborated: 01624 T = cast<ElaboratedType>(T)->getNamedType(); 01625 break; 01626 case Type::Paren: 01627 T = cast<ParenType>(T)->getInnerType(); 01628 break; 01629 case Type::SubstTemplateTypeParm: 01630 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); 01631 break; 01632 case Type::Auto: 01633 T = cast<AutoType>(T)->getDeducedType(); 01634 break; 01635 } 01636 01637 assert(T != LastT && "Type unwrapping failed to unwrap!"); 01638 if (T == LastT) 01639 return T; 01640 } while (true); 01641 } 01642 01643 /// getType - Get the type from the cache or return null type if it doesn't exist. 01644 llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) { 01645 01646 // Unwrap the type as needed for debug information. 01647 Ty = UnwrapTypeForDebugInfo(Ty); 01648 01649 // Check for existing entry. 01650 llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 01651 TypeCache.find(Ty.getAsOpaquePtr()); 01652 if (it != TypeCache.end()) { 01653 // Verify that the debug info still exists. 01654 if (&*it->second) 01655 return llvm::DIType(cast<llvm::MDNode>(it->second)); 01656 } 01657 01658 return llvm::DIType(); 01659 } 01660 01661 /// getCompletedTypeOrNull - Get the type from the cache or return null if it 01662 /// doesn't exist. 01663 llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) { 01664 01665 // Unwrap the type as needed for debug information. 01666 Ty = UnwrapTypeForDebugInfo(Ty); 01667 01668 // Check for existing entry. 01669 llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 01670 CompletedTypeCache.find(Ty.getAsOpaquePtr()); 01671 if (it != CompletedTypeCache.end()) { 01672 // Verify that the debug info still exists. 01673 if (&*it->second) 01674 return llvm::DIType(cast<llvm::MDNode>(it->second)); 01675 } 01676 01677 return llvm::DIType(); 01678 } 01679 01680 01681 /// getOrCreateType - Get the type from the cache or create a new 01682 /// one if necessary. 01683 llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) { 01684 if (Ty.isNull()) 01685 return llvm::DIType(); 01686 01687 // Unwrap the type as needed for debug information. 01688 Ty = UnwrapTypeForDebugInfo(Ty); 01689 01690 llvm::DIType T = getCompletedTypeOrNull(Ty); 01691 01692 if (T.Verify()) return T; 01693 01694 // Otherwise create the type. 01695 llvm::DIType Res = CreateTypeNode(Ty, Unit); 01696 01697 llvm::DIType TC = getTypeOrNull(Ty); 01698 if (TC.Verify() && TC.isForwardDecl()) 01699 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), TC)); 01700 01701 // And update the type cache. 01702 TypeCache[Ty.getAsOpaquePtr()] = Res; 01703 01704 if (!Res.isForwardDecl()) 01705 CompletedTypeCache[Ty.getAsOpaquePtr()] = Res; 01706 return Res; 01707 } 01708 01709 /// CreateTypeNode - Create a new debug type node. 01710 llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) { 01711 // Handle qualifiers, which recursively handles what they refer to. 01712 if (Ty.hasLocalQualifiers()) 01713 return CreateQualifiedType(Ty, Unit); 01714 01715 const char *Diag = 0; 01716 01717 // Work out details of type. 01718 switch (Ty->getTypeClass()) { 01719 #define TYPE(Class, Base) 01720 #define ABSTRACT_TYPE(Class, Base) 01721 #define NON_CANONICAL_TYPE(Class, Base) 01722 #define DEPENDENT_TYPE(Class, Base) case Type::Class: 01723 #include "clang/AST/TypeNodes.def" 01724 llvm_unreachable("Dependent types cannot show up in debug information"); 01725 01726 case Type::ExtVector: 01727 case Type::Vector: 01728 return CreateType(cast<VectorType>(Ty), Unit); 01729 case Type::ObjCObjectPointer: 01730 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); 01731 case Type::ObjCObject: 01732 return CreateType(cast<ObjCObjectType>(Ty), Unit); 01733 case Type::ObjCInterface: 01734 return CreateType(cast<ObjCInterfaceType>(Ty), Unit); 01735 case Type::Builtin: 01736 return CreateType(cast<BuiltinType>(Ty)); 01737 case Type::Complex: 01738 return CreateType(cast<ComplexType>(Ty)); 01739 case Type::Pointer: 01740 return CreateType(cast<PointerType>(Ty), Unit); 01741 case Type::BlockPointer: 01742 return CreateType(cast<BlockPointerType>(Ty), Unit); 01743 case Type::Typedef: 01744 return CreateType(cast<TypedefType>(Ty), Unit); 01745 case Type::Record: 01746 return CreateType(cast<RecordType>(Ty)); 01747 case Type::Enum: 01748 return CreateEnumType(cast<EnumType>(Ty)->getDecl()); 01749 case Type::FunctionProto: 01750 case Type::FunctionNoProto: 01751 return CreateType(cast<FunctionType>(Ty), Unit); 01752 case Type::ConstantArray: 01753 case Type::VariableArray: 01754 case Type::IncompleteArray: 01755 return CreateType(cast<ArrayType>(Ty), Unit); 01756 01757 case Type::LValueReference: 01758 return CreateType(cast<LValueReferenceType>(Ty), Unit); 01759 case Type::RValueReference: 01760 return CreateType(cast<RValueReferenceType>(Ty), Unit); 01761 01762 case Type::MemberPointer: 01763 return CreateType(cast<MemberPointerType>(Ty), Unit); 01764 01765 case Type::Atomic: 01766 return CreateType(cast<AtomicType>(Ty), Unit); 01767 01768 case Type::Attributed: 01769 case Type::TemplateSpecialization: 01770 case Type::Elaborated: 01771 case Type::Paren: 01772 case Type::SubstTemplateTypeParm: 01773 case Type::TypeOfExpr: 01774 case Type::TypeOf: 01775 case Type::Decltype: 01776 case Type::UnaryTransform: 01777 case Type::Auto: 01778 llvm_unreachable("type should have been unwrapped!"); 01779 } 01780 01781 assert(Diag && "Fall through without a diagnostic?"); 01782 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error, 01783 "debug information for %0 is not yet supported"); 01784 CGM.getDiags().Report(DiagID) 01785 << Diag; 01786 return llvm::DIType(); 01787 } 01788 01789 /// getOrCreateLimitedType - Get the type from the cache or create a new 01790 /// limited type if necessary. 01791 llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty, 01792 llvm::DIFile Unit) { 01793 if (Ty.isNull()) 01794 return llvm::DIType(); 01795 01796 // Unwrap the type as needed for debug information. 01797 Ty = UnwrapTypeForDebugInfo(Ty); 01798 01799 llvm::DIType T = getTypeOrNull(Ty); 01800 01801 // We may have cached a forward decl when we could have created 01802 // a non-forward decl. Go ahead and create a non-forward decl 01803 // now. 01804 if (T.Verify() && !T.isForwardDecl()) return T; 01805 01806 // Otherwise create the type. 01807 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit); 01808 01809 if (T.Verify() && T.isForwardDecl()) 01810 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), T)); 01811 01812 // And update the type cache. 01813 TypeCache[Ty.getAsOpaquePtr()] = Res; 01814 return Res; 01815 } 01816 01817 // TODO: Currently used for context chains when limiting debug info. 01818 llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) { 01819 RecordDecl *RD = Ty->getDecl(); 01820 01821 // Get overall information about the record type for the debug info. 01822 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation()); 01823 unsigned Line = getLineNumber(RD->getLocation()); 01824 StringRef RDName = RD->getName(); 01825 01826 llvm::DIDescriptor RDContext; 01827 if (CGM.getCodeGenOpts().DebugInfo == CodeGenOptions::LimitedDebugInfo) 01828 RDContext = createContextChain(cast<Decl>(RD->getDeclContext())); 01829 else 01830 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext())); 01831 01832 // If this is just a forward declaration, construct an appropriately 01833 // marked node and just return it. 01834 if (!RD->getDefinition()) 01835 return createRecordFwdDecl(RD, RDContext); 01836 01837 uint64_t Size = CGM.getContext().getTypeSize(Ty); 01838 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 01839 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 01840 llvm::TrackingVH<llvm::MDNode> RealDecl; 01841 01842 if (RD->isUnion()) 01843 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, 01844 Size, Align, 0, llvm::DIArray()); 01845 else if (CXXDecl) { 01846 RDName = getClassName(RD); 01847 01848 // FIXME: This could be a struct type giving a default visibility different 01849 // than C++ class type, but needs llvm metadata changes first. 01850 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line, 01851 Size, Align, 0, 0, llvm::DIType(), 01852 llvm::DIArray(), llvm::DIType(), 01853 llvm::DIArray()); 01854 } else 01855 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line, 01856 Size, Align, 0, llvm::DIArray()); 01857 01858 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl); 01859 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl); 01860 01861 if (CXXDecl) { 01862 // A class's primary base or the class itself contains the vtable. 01863 llvm::MDNode *ContainingType = NULL; 01864 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 01865 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { 01866 // Seek non virtual primary base root. 01867 while (1) { 01868 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); 01869 const CXXRecordDecl *PBT = BRL.getPrimaryBase(); 01870 if (PBT && !BRL.isPrimaryBaseVirtual()) 01871 PBase = PBT; 01872 else 01873 break; 01874 } 01875 ContainingType = 01876 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit); 01877 } 01878 else if (CXXDecl->isDynamicClass()) 01879 ContainingType = RealDecl; 01880 01881 RealDecl->replaceOperandWith(12, ContainingType); 01882 } 01883 return llvm::DIType(RealDecl); 01884 } 01885 01886 /// CreateLimitedTypeNode - Create a new debug type node, but only forward 01887 /// declare composite types that haven't been processed yet. 01888 llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) { 01889 01890 // Work out details of type. 01891 switch (Ty->getTypeClass()) { 01892 #define TYPE(Class, Base) 01893 #define ABSTRACT_TYPE(Class, Base) 01894 #define NON_CANONICAL_TYPE(Class, Base) 01895 #define DEPENDENT_TYPE(Class, Base) case Type::Class: 01896 #include "clang/AST/TypeNodes.def" 01897 llvm_unreachable("Dependent types cannot show up in debug information"); 01898 01899 case Type::Record: 01900 return CreateLimitedType(cast<RecordType>(Ty)); 01901 default: 01902 return CreateTypeNode(Ty, Unit); 01903 } 01904 } 01905 01906 /// CreateMemberType - Create new member and increase Offset by FType's size. 01907 llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, 01908 StringRef Name, 01909 uint64_t *Offset) { 01910 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 01911 uint64_t FieldSize = CGM.getContext().getTypeSize(FType); 01912 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); 01913 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, 01914 FieldSize, FieldAlign, 01915 *Offset, 0, FieldTy); 01916 *Offset += FieldSize; 01917 return Ty; 01918 } 01919 01920 /// getFunctionDeclaration - Return debug info descriptor to describe method 01921 /// declaration for the given method definition. 01922 llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) { 01923 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 01924 if (!FD) return llvm::DISubprogram(); 01925 01926 // Setup context. 01927 getContextDescriptor(cast<Decl>(D->getDeclContext())); 01928 01929 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 01930 MI = SPCache.find(FD->getCanonicalDecl()); 01931 if (MI != SPCache.end()) { 01932 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second)); 01933 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition()) 01934 return SP; 01935 } 01936 01937 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(), 01938 E = FD->redecls_end(); I != E; ++I) { 01939 const FunctionDecl *NextFD = *I; 01940 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 01941 MI = SPCache.find(NextFD->getCanonicalDecl()); 01942 if (MI != SPCache.end()) { 01943 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second)); 01944 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition()) 01945 return SP; 01946 } 01947 } 01948 return llvm::DISubprogram(); 01949 } 01950 01951 // getOrCreateFunctionType - Construct DIType. If it is a c++ method, include 01952 // implicit parameter "this". 01953 llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl * D, 01954 QualType FnType, 01955 llvm::DIFile F) { 01956 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) 01957 return getOrCreateMethodType(Method, F); 01958 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) { 01959 // Add "self" and "_cmd" 01960 SmallVector<llvm::Value *, 16> Elts; 01961 01962 // First element is always return type. For 'void' functions it is NULL. 01963 Elts.push_back(getOrCreateType(OMethod->getResultType(), F)); 01964 // "self" pointer is always first argument. 01965 Elts.push_back(getOrCreateType(OMethod->getSelfDecl()->getType(), F)); 01966 // "cmd" pointer is always second argument. 01967 Elts.push_back(getOrCreateType(OMethod->getCmdDecl()->getType(), F)); 01968 // Get rest of the arguments. 01969 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(), 01970 PE = OMethod->param_end(); PI != PE; ++PI) 01971 Elts.push_back(getOrCreateType((*PI)->getType(), F)); 01972 01973 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); 01974 return DBuilder.createSubroutineType(F, EltTypeArray); 01975 } 01976 return getOrCreateType(FnType, F); 01977 } 01978 01979 /// EmitFunctionStart - Constructs the debug code for entering a function. 01980 void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, 01981 llvm::Function *Fn, 01982 CGBuilderTy &Builder) { 01983 01984 StringRef Name; 01985 StringRef LinkageName; 01986 01987 FnBeginRegionCount.push_back(LexicalBlockStack.size()); 01988 01989 const Decl *D = GD.getDecl(); 01990 // Use the location of the declaration. 01991 SourceLocation Loc = D->getLocation(); 01992 01993 unsigned Flags = 0; 01994 llvm::DIFile Unit = getOrCreateFile(Loc); 01995 llvm::DIDescriptor FDContext(Unit); 01996 llvm::DIArray TParamsArray; 01997 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 01998 // If there is a DISubprogram for this function available then use it. 01999 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 02000 FI = SPCache.find(FD->getCanonicalDecl()); 02001 if (FI != SPCache.end()) { 02002 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second)); 02003 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) { 02004 llvm::MDNode *SPN = SP; 02005 LexicalBlockStack.push_back(SPN); 02006 RegionMap[D] = llvm::WeakVH(SP); 02007 return; 02008 } 02009 } 02010 Name = getFunctionName(FD); 02011 // Use mangled name as linkage name for c/c++ functions. 02012 if (FD->hasPrototype()) { 02013 LinkageName = CGM.getMangledName(GD); 02014 Flags |= llvm::DIDescriptor::FlagPrototyped; 02015 } 02016 if (LinkageName == Name || 02017 CGM.getCodeGenOpts().DebugInfo <= CodeGenOptions::DebugLineTablesOnly) 02018 LinkageName = StringRef(); 02019 02020 if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo) { 02021 if (const NamespaceDecl *NSDecl = 02022 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) 02023 FDContext = getOrCreateNameSpace(NSDecl); 02024 else if (const RecordDecl *RDecl = 02025 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) 02026 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext())); 02027 02028 // Collect template parameters. 02029 TParamsArray = CollectFunctionTemplateParams(FD, Unit); 02030 } 02031 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { 02032 Name = getObjCMethodName(OMD); 02033 Flags |= llvm::DIDescriptor::FlagPrototyped; 02034 } else { 02035 // Use llvm function name. 02036 Name = Fn->getName(); 02037 Flags |= llvm::DIDescriptor::FlagPrototyped; 02038 } 02039 if (!Name.empty() && Name[0] == '\01') 02040 Name = Name.substr(1); 02041 02042 unsigned LineNo = getLineNumber(Loc); 02043 if (D->isImplicit()) 02044 Flags |= llvm::DIDescriptor::FlagArtificial; 02045 02046 llvm::DIType DIFnType; 02047 llvm::DISubprogram SPDecl; 02048 if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo) { 02049 DIFnType = getOrCreateFunctionType(D, FnType, Unit); 02050 SPDecl = getFunctionDeclaration(D); 02051 } else { 02052 // Create fake but valid subroutine type. Otherwise 02053 // llvm::DISubprogram::Verify() would return false, and 02054 // subprogram DIE will miss DW_AT_decl_file and 02055 // DW_AT_decl_line fields. 02056 SmallVector<llvm::Value*, 16> Elts; 02057 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts); 02058 DIFnType = DBuilder.createSubroutineType(Unit, EltTypeArray); 02059 } 02060 llvm::DISubprogram SP; 02061 SP = DBuilder.createFunction(FDContext, Name, LinkageName, Unit, 02062 LineNo, DIFnType, 02063 Fn->hasInternalLinkage(), true/*definition*/, 02064 getLineNumber(CurLoc), Flags, 02065 CGM.getLangOpts().Optimize, 02066 Fn, TParamsArray, SPDecl); 02067 02068 // Push function on region stack. 02069 llvm::MDNode *SPN = SP; 02070 LexicalBlockStack.push_back(SPN); 02071 RegionMap[D] = llvm::WeakVH(SP); 02072 } 02073 02074 /// EmitLocation - Emit metadata to indicate a change in line/column 02075 /// information in the source file. 02076 void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) { 02077 02078 // Update our current location 02079 setLocation(Loc); 02080 02081 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 02082 02083 // Don't bother if things are the same as last time. 02084 SourceManager &SM = CGM.getContext().getSourceManager(); 02085 if (CurLoc == PrevLoc || 02086 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc)) 02087 // New Builder may not be in sync with CGDebugInfo. 02088 if (!Builder.getCurrentDebugLocation().isUnknown()) 02089 return; 02090 02091 // Update last state. 02092 PrevLoc = CurLoc; 02093 02094 llvm::MDNode *Scope = LexicalBlockStack.back(); 02095 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc), 02096 getColumnNumber(CurLoc), 02097 Scope)); 02098 } 02099 02100 /// CreateLexicalBlock - Creates a new lexical block node and pushes it on 02101 /// the stack. 02102 void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { 02103 llvm::DIDescriptor D = 02104 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ? 02105 llvm::DIDescriptor() : 02106 llvm::DIDescriptor(LexicalBlockStack.back()), 02107 getOrCreateFile(CurLoc), 02108 getLineNumber(CurLoc), 02109 getColumnNumber(CurLoc)); 02110 llvm::MDNode *DN = D; 02111 LexicalBlockStack.push_back(DN); 02112 } 02113 02114 /// EmitLexicalBlockStart - Constructs the debug code for entering a declarative 02115 /// region - beginning of a DW_TAG_lexical_block. 02116 void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) { 02117 // Set our current location. 02118 setLocation(Loc); 02119 02120 // Create a new lexical block and push it on the stack. 02121 CreateLexicalBlock(Loc); 02122 02123 // Emit a line table change for the current location inside the new scope. 02124 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc), 02125 getColumnNumber(Loc), 02126 LexicalBlockStack.back())); 02127 } 02128 02129 /// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative 02130 /// region - end of a DW_TAG_lexical_block. 02131 void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) { 02132 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 02133 02134 // Provide an entry in the line table for the end of the block. 02135 EmitLocation(Builder, Loc); 02136 02137 LexicalBlockStack.pop_back(); 02138 } 02139 02140 /// EmitFunctionEnd - Constructs the debug code for exiting a function. 02141 void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { 02142 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 02143 unsigned RCount = FnBeginRegionCount.back(); 02144 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch"); 02145 02146 // Pop all regions for this function. 02147 while (LexicalBlockStack.size() != RCount) 02148 EmitLexicalBlockEnd(Builder, CurLoc); 02149 FnBeginRegionCount.pop_back(); 02150 } 02151 02152 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 02153 // See BuildByRefType. 02154 llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, 02155 uint64_t *XOffset) { 02156 02157 SmallVector<llvm::Value *, 5> EltTys; 02158 QualType FType; 02159 uint64_t FieldSize, FieldOffset; 02160 unsigned FieldAlign; 02161 02162 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 02163 QualType Type = VD->getType(); 02164 02165 FieldOffset = 0; 02166 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 02167 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 02168 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); 02169 FType = CGM.getContext().IntTy; 02170 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 02171 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); 02172 02173 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type); 02174 if (HasCopyAndDispose) { 02175 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 02176 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", 02177 &FieldOffset)); 02178 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", 02179 &FieldOffset)); 02180 } 02181 02182 CharUnits Align = CGM.getContext().getDeclAlign(VD); 02183 if (Align > CGM.getContext().toCharUnitsFromBits( 02184 CGM.getContext().getTargetInfo().getPointerAlign(0))) { 02185 CharUnits FieldOffsetInBytes 02186 = CGM.getContext().toCharUnitsFromBits(FieldOffset); 02187 CharUnits AlignedOffsetInBytes 02188 = FieldOffsetInBytes.RoundUpToAlignment(Align); 02189 CharUnits NumPaddingBytes 02190 = AlignedOffsetInBytes - FieldOffsetInBytes; 02191 02192 if (NumPaddingBytes.isPositive()) { 02193 llvm::APInt pad(32, NumPaddingBytes.getQuantity()); 02194 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, 02195 pad, ArrayType::Normal, 0); 02196 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); 02197 } 02198 } 02199 02200 FType = Type; 02201 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 02202 FieldSize = CGM.getContext().getTypeSize(FType); 02203 FieldAlign = CGM.getContext().toBits(Align); 02204 02205 *XOffset = FieldOffset; 02206 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 02207 0, FieldSize, FieldAlign, 02208 FieldOffset, 0, FieldTy); 02209 EltTys.push_back(FieldTy); 02210 FieldOffset += FieldSize; 02211 02212 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys); 02213 02214 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct; 02215 02216 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, 02217 Elements); 02218 } 02219 02220 /// EmitDeclare - Emit local variable declaration debug info. 02221 void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, 02222 llvm::Value *Storage, 02223 unsigned ArgNo, CGBuilderTy &Builder) { 02224 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02225 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 02226 02227 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 02228 llvm::DIType Ty; 02229 uint64_t XOffset = 0; 02230 if (VD->hasAttr<BlocksAttr>()) 02231 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 02232 else 02233 Ty = getOrCreateType(VD->getType(), Unit); 02234 02235 // If there is not any debug info for type then do not emit debug info 02236 // for this variable. 02237 if (!Ty) 02238 return; 02239 02240 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) { 02241 // If Storage is an aggregate returned as 'sret' then let debugger know 02242 // about this. 02243 if (Arg->hasStructRetAttr()) 02244 Ty = DBuilder.createReferenceType(Ty); 02245 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) { 02246 // If an aggregate variable has non trivial destructor or non trivial copy 02247 // constructor than it is pass indirectly. Let debug info know about this 02248 // by using reference of the aggregate type as a argument type. 02249 if (!Record->hasTrivialCopyConstructor() || 02250 !Record->hasTrivialDestructor()) 02251 Ty = DBuilder.createReferenceType(Ty); 02252 } 02253 } 02254 02255 // Get location information. 02256 unsigned Line = getLineNumber(VD->getLocation()); 02257 unsigned Column = getColumnNumber(VD->getLocation()); 02258 unsigned Flags = 0; 02259 if (VD->isImplicit()) 02260 Flags |= llvm::DIDescriptor::FlagArtificial; 02261 llvm::MDNode *Scope = LexicalBlockStack.back(); 02262 02263 StringRef Name = VD->getName(); 02264 if (!Name.empty()) { 02265 if (VD->hasAttr<BlocksAttr>()) { 02266 CharUnits offset = CharUnits::fromQuantity(32); 02267 SmallVector<llvm::Value *, 9> addr; 02268 llvm::Type *Int64Ty = CGM.Int64Ty; 02269 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 02270 // offset of __forwarding field 02271 offset = CGM.getContext().toCharUnitsFromBits( 02272 CGM.getContext().getTargetInfo().getPointerWidth(0)); 02273 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 02274 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); 02275 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 02276 // offset of x field 02277 offset = CGM.getContext().toCharUnitsFromBits(XOffset); 02278 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 02279 02280 // Create the descriptor for the variable. 02281 llvm::DIVariable D = 02282 DBuilder.createComplexVariable(Tag, 02283 llvm::DIDescriptor(Scope), 02284 VD->getName(), Unit, Line, Ty, 02285 addr, ArgNo); 02286 02287 // Insert an llvm.dbg.declare into the current block. 02288 llvm::Instruction *Call = 02289 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); 02290 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 02291 return; 02292 } else if (isa<VariableArrayType>(VD->getType())) { 02293 // These are "complex" variables in that they need an op_deref. 02294 // Create the descriptor for the variable. 02295 llvm::Value *Addr = llvm::ConstantInt::get(CGM.Int64Ty, 02296 llvm::DIBuilder::OpDeref); 02297 llvm::DIVariable D = 02298 DBuilder.createComplexVariable(Tag, 02299 llvm::DIDescriptor(Scope), 02300 Name, Unit, Line, Ty, 02301 Addr, ArgNo); 02302 02303 // Insert an llvm.dbg.declare into the current block. 02304 llvm::Instruction *Call = 02305 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); 02306 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 02307 return; 02308 } 02309 02310 // Create the descriptor for the variable. 02311 llvm::DIVariable D = 02312 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), 02313 Name, Unit, Line, Ty, 02314 CGM.getLangOpts().Optimize, Flags, ArgNo); 02315 02316 // Insert an llvm.dbg.declare into the current block. 02317 llvm::Instruction *Call = 02318 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); 02319 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 02320 return; 02321 } 02322 02323 // If VD is an anonymous union then Storage represents value for 02324 // all union fields. 02325 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) { 02326 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); 02327 if (RD->isUnion()) { 02328 for (RecordDecl::field_iterator I = RD->field_begin(), 02329 E = RD->field_end(); 02330 I != E; ++I) { 02331 FieldDecl *Field = &*I; 02332 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 02333 StringRef FieldName = Field->getName(); 02334 02335 // Ignore unnamed fields. Do not ignore unnamed records. 02336 if (FieldName.empty() && !isa<RecordType>(Field->getType())) 02337 continue; 02338 02339 // Use VarDecl's Tag, Scope and Line number. 02340 llvm::DIVariable D = 02341 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope), 02342 FieldName, Unit, Line, FieldTy, 02343 CGM.getLangOpts().Optimize, Flags, 02344 ArgNo); 02345 02346 // Insert an llvm.dbg.declare into the current block. 02347 llvm::Instruction *Call = 02348 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock()); 02349 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 02350 } 02351 } 02352 } 02353 } 02354 02355 void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, 02356 llvm::Value *Storage, 02357 CGBuilderTy &Builder) { 02358 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02359 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder); 02360 } 02361 02362 void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( 02363 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder, 02364 const CGBlockInfo &blockInfo) { 02365 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02366 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); 02367 02368 if (Builder.GetInsertBlock() == 0) 02369 return; 02370 02371 bool isByRef = VD->hasAttr<BlocksAttr>(); 02372 02373 uint64_t XOffset = 0; 02374 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 02375 llvm::DIType Ty; 02376 if (isByRef) 02377 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 02378 else 02379 Ty = getOrCreateType(VD->getType(), Unit); 02380 02381 // Get location information. 02382 unsigned Line = getLineNumber(VD->getLocation()); 02383 unsigned Column = getColumnNumber(VD->getLocation()); 02384 02385 const llvm::TargetData &target = CGM.getTargetData(); 02386 02387 CharUnits offset = CharUnits::fromQuantity( 02388 target.getStructLayout(blockInfo.StructureType) 02389 ->getElementOffset(blockInfo.getCapture(VD).getIndex())); 02390 02391 SmallVector<llvm::Value *, 9> addr; 02392 llvm::Type *Int64Ty = CGM.Int64Ty; 02393 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 02394 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 02395 if (isByRef) { 02396 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); 02397 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 02398 // offset of __forwarding field 02399 offset = CGM.getContext() 02400 .toCharUnitsFromBits(target.getPointerSizeInBits()); 02401 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 02402 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref)); 02403 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus)); 02404 // offset of x field 02405 offset = CGM.getContext().toCharUnitsFromBits(XOffset); 02406 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 02407 } 02408 02409 // Create the descriptor for the variable. 02410 llvm::DIVariable D = 02411 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable, 02412 llvm::DIDescriptor(LexicalBlockStack.back()), 02413 VD->getName(), Unit, Line, Ty, addr); 02414 // Insert an llvm.dbg.declare into the current block. 02415 llvm::Instruction *Call = 02416 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint()); 02417 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, 02418 LexicalBlockStack.back())); 02419 } 02420 02421 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 02422 /// variable declaration. 02423 void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, 02424 unsigned ArgNo, 02425 CGBuilderTy &Builder) { 02426 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02427 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder); 02428 } 02429 02430 namespace { 02431 struct BlockLayoutChunk { 02432 uint64_t OffsetInBits; 02433 const BlockDecl::Capture *Capture; 02434 }; 02435 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) { 02436 return l.OffsetInBits < r.OffsetInBits; 02437 } 02438 } 02439 02440 void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, 02441 llvm::Value *addr, 02442 CGBuilderTy &Builder) { 02443 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02444 ASTContext &C = CGM.getContext(); 02445 const BlockDecl *blockDecl = block.getBlockDecl(); 02446 02447 // Collect some general information about the block's location. 02448 SourceLocation loc = blockDecl->getCaretLocation(); 02449 llvm::DIFile tunit = getOrCreateFile(loc); 02450 unsigned line = getLineNumber(loc); 02451 unsigned column = getColumnNumber(loc); 02452 02453 // Build the debug-info type for the block literal. 02454 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext())); 02455 02456 const llvm::StructLayout *blockLayout = 02457 CGM.getTargetData().getStructLayout(block.StructureType); 02458 02459 SmallVector<llvm::Value*, 16> fields; 02460 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public, 02461 blockLayout->getElementOffsetInBits(0), 02462 tunit, tunit)); 02463 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public, 02464 blockLayout->getElementOffsetInBits(1), 02465 tunit, tunit)); 02466 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public, 02467 blockLayout->getElementOffsetInBits(2), 02468 tunit, tunit)); 02469 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public, 02470 blockLayout->getElementOffsetInBits(3), 02471 tunit, tunit)); 02472 fields.push_back(createFieldType("__descriptor", 02473 C.getPointerType(block.NeedsCopyDispose ? 02474 C.getBlockDescriptorExtendedType() : 02475 C.getBlockDescriptorType()), 02476 0, loc, AS_public, 02477 blockLayout->getElementOffsetInBits(4), 02478 tunit, tunit)); 02479 02480 // We want to sort the captures by offset, not because DWARF 02481 // requires this, but because we're paranoid about debuggers. 02482 SmallVector<BlockLayoutChunk, 8> chunks; 02483 02484 // 'this' capture. 02485 if (blockDecl->capturesCXXThis()) { 02486 BlockLayoutChunk chunk; 02487 chunk.OffsetInBits = 02488 blockLayout->getElementOffsetInBits(block.CXXThisIndex); 02489 chunk.Capture = 0; 02490 chunks.push_back(chunk); 02491 } 02492 02493 // Variable captures. 02494 for (BlockDecl::capture_const_iterator 02495 i = blockDecl->capture_begin(), e = blockDecl->capture_end(); 02496 i != e; ++i) { 02497 const BlockDecl::Capture &capture = *i; 02498 const VarDecl *variable = capture.getVariable(); 02499 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable); 02500 02501 // Ignore constant captures. 02502 if (captureInfo.isConstant()) 02503 continue; 02504 02505 BlockLayoutChunk chunk; 02506 chunk.OffsetInBits = 02507 blockLayout->getElementOffsetInBits(captureInfo.getIndex()); 02508 chunk.Capture = &capture; 02509 chunks.push_back(chunk); 02510 } 02511 02512 // Sort by offset. 02513 llvm::array_pod_sort(chunks.begin(), chunks.end()); 02514 02515 for (SmallVectorImpl<BlockLayoutChunk>::iterator 02516 i = chunks.begin(), e = chunks.end(); i != e; ++i) { 02517 uint64_t offsetInBits = i->OffsetInBits; 02518 const BlockDecl::Capture *capture = i->Capture; 02519 02520 // If we have a null capture, this must be the C++ 'this' capture. 02521 if (!capture) { 02522 const CXXMethodDecl *method = 02523 cast<CXXMethodDecl>(blockDecl->getNonClosureContext()); 02524 QualType type = method->getThisType(C); 02525 02526 fields.push_back(createFieldType("this", type, 0, loc, AS_public, 02527 offsetInBits, tunit, tunit)); 02528 continue; 02529 } 02530 02531 const VarDecl *variable = capture->getVariable(); 02532 StringRef name = variable->getName(); 02533 02534 llvm::DIType fieldType; 02535 if (capture->isByRef()) { 02536 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy); 02537 02538 // FIXME: this creates a second copy of this type! 02539 uint64_t xoffset; 02540 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset); 02541 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first); 02542 fieldType = DBuilder.createMemberType(tunit, name, tunit, line, 02543 ptrInfo.first, ptrInfo.second, 02544 offsetInBits, 0, fieldType); 02545 } else { 02546 fieldType = createFieldType(name, variable->getType(), 0, 02547 loc, AS_public, offsetInBits, tunit, tunit); 02548 } 02549 fields.push_back(fieldType); 02550 } 02551 02552 SmallString<36> typeName; 02553 llvm::raw_svector_ostream(typeName) 02554 << "__block_literal_" << CGM.getUniqueBlockCount(); 02555 02556 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields); 02557 02558 llvm::DIType type = 02559 DBuilder.createStructType(tunit, typeName.str(), tunit, line, 02560 CGM.getContext().toBits(block.BlockSize), 02561 CGM.getContext().toBits(block.BlockAlign), 02562 0, fieldsArray); 02563 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits); 02564 02565 // Get overall information about the block. 02566 unsigned flags = llvm::DIDescriptor::FlagArtificial; 02567 llvm::MDNode *scope = LexicalBlockStack.back(); 02568 StringRef name = ".block_descriptor"; 02569 02570 // Create the descriptor for the parameter. 02571 llvm::DIVariable debugVar = 02572 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable, 02573 llvm::DIDescriptor(scope), 02574 name, tunit, line, type, 02575 CGM.getLangOpts().Optimize, flags, 02576 cast<llvm::Argument>(addr)->getArgNo() + 1); 02577 02578 // Insert an llvm.dbg.value into the current block. 02579 llvm::Instruction *declare = 02580 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar, 02581 Builder.GetInsertBlock()); 02582 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope)); 02583 } 02584 02585 /// EmitGlobalVariable - Emit information about a global variable. 02586 void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 02587 const VarDecl *D) { 02588 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02589 // Create global variable debug descriptor. 02590 llvm::DIFile Unit = getOrCreateFile(D->getLocation()); 02591 unsigned LineNo = getLineNumber(D->getLocation()); 02592 02593 setLocation(D->getLocation()); 02594 02595 QualType T = D->getType(); 02596 if (T->isIncompleteArrayType()) { 02597 02598 // CodeGen turns int[] into int[1] so we'll do the same here. 02599 llvm::APSInt ConstVal(32); 02600 02601 ConstVal = 1; 02602 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 02603 02604 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 02605 ArrayType::Normal, 0); 02606 } 02607 StringRef DeclName = D->getName(); 02608 StringRef LinkageName; 02609 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()) 02610 && !isa<ObjCMethodDecl>(D->getDeclContext())) 02611 LinkageName = Var->getName(); 02612 if (LinkageName == DeclName) 02613 LinkageName = StringRef(); 02614 llvm::DIDescriptor DContext = 02615 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext())); 02616 DBuilder.createStaticVariable(DContext, DeclName, LinkageName, 02617 Unit, LineNo, getOrCreateType(T, Unit), 02618 Var->hasInternalLinkage(), Var); 02619 } 02620 02621 /// EmitGlobalVariable - Emit information about an objective-c interface. 02622 void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 02623 ObjCInterfaceDecl *ID) { 02624 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02625 // Create global variable debug descriptor. 02626 llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); 02627 unsigned LineNo = getLineNumber(ID->getLocation()); 02628 02629 StringRef Name = ID->getName(); 02630 02631 QualType T = CGM.getContext().getObjCInterfaceType(ID); 02632 if (T->isIncompleteArrayType()) { 02633 02634 // CodeGen turns int[] into int[1] so we'll do the same here. 02635 llvm::APSInt ConstVal(32); 02636 02637 ConstVal = 1; 02638 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 02639 02640 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 02641 ArrayType::Normal, 0); 02642 } 02643 02644 DBuilder.createGlobalVariable(Name, Unit, LineNo, 02645 getOrCreateType(T, Unit), 02646 Var->hasInternalLinkage(), Var); 02647 } 02648 02649 /// EmitGlobalVariable - Emit global variable's debug info. 02650 void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, 02651 llvm::Constant *Init) { 02652 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo); 02653 // Create the descriptor for the variable. 02654 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 02655 StringRef Name = VD->getName(); 02656 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit); 02657 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) { 02658 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) 02659 Ty = CreateEnumType(ED); 02660 } 02661 // Do not use DIGlobalVariable for enums. 02662 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type) 02663 return; 02664 DBuilder.createStaticVariable(Unit, Name, Name, Unit, 02665 getLineNumber(VD->getLocation()), 02666 Ty, true, Init); 02667 } 02668 02669 /// getOrCreateNamesSpace - Return namespace descriptor for the given 02670 /// namespace decl. 02671 llvm::DINameSpace 02672 CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) { 02673 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = 02674 NameSpaceCache.find(NSDecl); 02675 if (I != NameSpaceCache.end()) 02676 return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); 02677 02678 unsigned LineNo = getLineNumber(NSDecl->getLocation()); 02679 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation()); 02680 llvm::DIDescriptor Context = 02681 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext())); 02682 llvm::DINameSpace NS = 02683 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo); 02684 NameSpaceCache[NSDecl] = llvm::WeakVH(NS); 02685 return NS; 02686 } 02687 02688 void CGDebugInfo::finalize(void) { 02689 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI 02690 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) { 02691 llvm::DIType Ty, RepTy; 02692 // Verify that the debug info still exists. 02693 if (&*VI->second) 02694 Ty = llvm::DIType(cast<llvm::MDNode>(VI->second)); 02695 02696 llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 02697 TypeCache.find(VI->first); 02698 if (it != TypeCache.end()) { 02699 // Verify that the debug info still exists. 02700 if (&*it->second) 02701 RepTy = llvm::DIType(cast<llvm::MDNode>(it->second)); 02702 } 02703 02704 if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) { 02705 Ty.replaceAllUsesWith(RepTy); 02706 } 02707 } 02708 DBuilder.finalize(); 02709 }