CGDebugInfo.cpp 78 KB

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