ASTWriterDecl.cpp 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  1. //===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements serialization for Declarations.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ASTCommon.h"
  13. #include "clang/AST/Attr.h"
  14. #include "clang/AST/DeclCXX.h"
  15. #include "clang/AST/DeclTemplate.h"
  16. #include "clang/AST/DeclVisitor.h"
  17. #include "clang/AST/Expr.h"
  18. #include "clang/AST/OpenMPClause.h"
  19. #include "clang/AST/PrettyDeclStackTrace.h"
  20. #include "clang/Basic/SourceManager.h"
  21. #include "clang/Serialization/ASTReader.h"
  22. #include "clang/Serialization/ASTRecordWriter.h"
  23. #include "llvm/Bitstream/BitstreamWriter.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. using namespace clang;
  26. using namespace serialization;
  27. //===----------------------------------------------------------------------===//
  28. // Declaration serialization
  29. //===----------------------------------------------------------------------===//
  30. namespace clang {
  31. class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {
  32. ASTWriter &Writer;
  33. ASTContext &Context;
  34. ASTRecordWriter Record;
  35. serialization::DeclCode Code;
  36. unsigned AbbrevToUse;
  37. public:
  38. ASTDeclWriter(ASTWriter &Writer, ASTContext &Context,
  39. ASTWriter::RecordDataImpl &Record)
  40. : Writer(Writer), Context(Context), Record(Writer, Record),
  41. Code((serialization::DeclCode)0), AbbrevToUse(0) {}
  42. uint64_t Emit(Decl *D) {
  43. if (!Code)
  44. llvm::report_fatal_error(StringRef("unexpected declaration kind '") +
  45. D->getDeclKindName() + "'");
  46. return Record.Emit(Code, AbbrevToUse);
  47. }
  48. void Visit(Decl *D);
  49. void VisitDecl(Decl *D);
  50. void VisitPragmaCommentDecl(PragmaCommentDecl *D);
  51. void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
  52. void VisitTranslationUnitDecl(TranslationUnitDecl *D);
  53. void VisitNamedDecl(NamedDecl *D);
  54. void VisitLabelDecl(LabelDecl *LD);
  55. void VisitNamespaceDecl(NamespaceDecl *D);
  56. void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
  57. void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
  58. void VisitTypeDecl(TypeDecl *D);
  59. void VisitTypedefNameDecl(TypedefNameDecl *D);
  60. void VisitTypedefDecl(TypedefDecl *D);
  61. void VisitTypeAliasDecl(TypeAliasDecl *D);
  62. void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
  63. void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);
  64. void VisitTagDecl(TagDecl *D);
  65. void VisitEnumDecl(EnumDecl *D);
  66. void VisitRecordDecl(RecordDecl *D);
  67. void VisitCXXRecordDecl(CXXRecordDecl *D);
  68. void VisitClassTemplateSpecializationDecl(
  69. ClassTemplateSpecializationDecl *D);
  70. void VisitClassTemplatePartialSpecializationDecl(
  71. ClassTemplatePartialSpecializationDecl *D);
  72. void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
  73. void VisitVarTemplatePartialSpecializationDecl(
  74. VarTemplatePartialSpecializationDecl *D);
  75. void VisitClassScopeFunctionSpecializationDecl(
  76. ClassScopeFunctionSpecializationDecl *D);
  77. void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
  78. void VisitValueDecl(ValueDecl *D);
  79. void VisitEnumConstantDecl(EnumConstantDecl *D);
  80. void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
  81. void VisitDeclaratorDecl(DeclaratorDecl *D);
  82. void VisitFunctionDecl(FunctionDecl *D);
  83. void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D);
  84. void VisitCXXMethodDecl(CXXMethodDecl *D);
  85. void VisitCXXConstructorDecl(CXXConstructorDecl *D);
  86. void VisitCXXDestructorDecl(CXXDestructorDecl *D);
  87. void VisitCXXConversionDecl(CXXConversionDecl *D);
  88. void VisitFieldDecl(FieldDecl *D);
  89. void VisitMSPropertyDecl(MSPropertyDecl *D);
  90. void VisitMSGuidDecl(MSGuidDecl *D);
  91. void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);
  92. void VisitIndirectFieldDecl(IndirectFieldDecl *D);
  93. void VisitVarDecl(VarDecl *D);
  94. void VisitImplicitParamDecl(ImplicitParamDecl *D);
  95. void VisitParmVarDecl(ParmVarDecl *D);
  96. void VisitDecompositionDecl(DecompositionDecl *D);
  97. void VisitBindingDecl(BindingDecl *D);
  98. void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
  99. void VisitTemplateDecl(TemplateDecl *D);
  100. void VisitConceptDecl(ConceptDecl *D);
  101. void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);
  102. void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
  103. void VisitClassTemplateDecl(ClassTemplateDecl *D);
  104. void VisitVarTemplateDecl(VarTemplateDecl *D);
  105. void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
  106. void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
  107. void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
  108. void VisitUsingDecl(UsingDecl *D);
  109. void VisitUsingEnumDecl(UsingEnumDecl *D);
  110. void VisitUsingPackDecl(UsingPackDecl *D);
  111. void VisitUsingShadowDecl(UsingShadowDecl *D);
  112. void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
  113. void VisitLinkageSpecDecl(LinkageSpecDecl *D);
  114. void VisitExportDecl(ExportDecl *D);
  115. void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
  116. void VisitImportDecl(ImportDecl *D);
  117. void VisitAccessSpecDecl(AccessSpecDecl *D);
  118. void VisitFriendDecl(FriendDecl *D);
  119. void VisitFriendTemplateDecl(FriendTemplateDecl *D);
  120. void VisitStaticAssertDecl(StaticAssertDecl *D);
  121. void VisitBlockDecl(BlockDecl *D);
  122. void VisitCapturedDecl(CapturedDecl *D);
  123. void VisitEmptyDecl(EmptyDecl *D);
  124. void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
  125. void VisitDeclContext(DeclContext *DC);
  126. template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
  127. // FIXME: Put in the same order is DeclNodes.td?
  128. void VisitObjCMethodDecl(ObjCMethodDecl *D);
  129. void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
  130. void VisitObjCContainerDecl(ObjCContainerDecl *D);
  131. void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
  132. void VisitObjCIvarDecl(ObjCIvarDecl *D);
  133. void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
  134. void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
  135. void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
  136. void VisitObjCImplDecl(ObjCImplDecl *D);
  137. void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
  138. void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
  139. void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
  140. void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
  141. void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
  142. void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
  143. void VisitOMPAllocateDecl(OMPAllocateDecl *D);
  144. void VisitOMPRequiresDecl(OMPRequiresDecl *D);
  145. void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
  146. void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
  147. void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
  148. /// Add an Objective-C type parameter list to the given record.
  149. void AddObjCTypeParamList(ObjCTypeParamList *typeParams) {
  150. // Empty type parameter list.
  151. if (!typeParams) {
  152. Record.push_back(0);
  153. return;
  154. }
  155. Record.push_back(typeParams->size());
  156. for (auto typeParam : *typeParams) {
  157. Record.AddDeclRef(typeParam);
  158. }
  159. Record.AddSourceLocation(typeParams->getLAngleLoc());
  160. Record.AddSourceLocation(typeParams->getRAngleLoc());
  161. }
  162. /// Add to the record the first declaration from each module file that
  163. /// provides a declaration of D. The intent is to provide a sufficient
  164. /// set such that reloading this set will load all current redeclarations.
  165. void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) {
  166. llvm::MapVector<ModuleFile*, const Decl*> Firsts;
  167. // FIXME: We can skip entries that we know are implied by others.
  168. for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) {
  169. if (R->isFromASTFile())
  170. Firsts[Writer.Chain->getOwningModuleFile(R)] = R;
  171. else if (IncludeLocal)
  172. Firsts[nullptr] = R;
  173. }
  174. for (const auto &F : Firsts)
  175. Record.AddDeclRef(F.second);
  176. }
  177. /// Get the specialization decl from an entry in the specialization list.
  178. template <typename EntryType>
  179. typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
  180. getSpecializationDecl(EntryType &T) {
  181. return RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::getDecl(&T);
  182. }
  183. /// Get the list of partial specializations from a template's common ptr.
  184. template<typename T>
  185. decltype(T::PartialSpecializations) &getPartialSpecializations(T *Common) {
  186. return Common->PartialSpecializations;
  187. }
  188. ArrayRef<Decl> getPartialSpecializations(FunctionTemplateDecl::Common *) {
  189. return None;
  190. }
  191. template<typename DeclTy>
  192. void AddTemplateSpecializations(DeclTy *D) {
  193. auto *Common = D->getCommonPtr();
  194. // If we have any lazy specializations, and the external AST source is
  195. // our chained AST reader, we can just write out the DeclIDs. Otherwise,
  196. // we need to resolve them to actual declarations.
  197. if (Writer.Chain != Writer.Context->getExternalSource() &&
  198. Common->LazySpecializations) {
  199. D->LoadLazySpecializations();
  200. assert(!Common->LazySpecializations);
  201. }
  202. ArrayRef<DeclID> LazySpecializations;
  203. if (auto *LS = Common->LazySpecializations)
  204. LazySpecializations = llvm::makeArrayRef(LS + 1, LS[0]);
  205. // Add a slot to the record for the number of specializations.
  206. unsigned I = Record.size();
  207. Record.push_back(0);
  208. // AddFirstDeclFromEachModule might trigger deserialization, invalidating
  209. // *Specializations iterators.
  210. llvm::SmallVector<const Decl*, 16> Specs;
  211. for (auto &Entry : Common->Specializations)
  212. Specs.push_back(getSpecializationDecl(Entry));
  213. for (auto &Entry : getPartialSpecializations(Common))
  214. Specs.push_back(getSpecializationDecl(Entry));
  215. for (auto *D : Specs) {
  216. assert(D->isCanonicalDecl() && "non-canonical decl in set");
  217. AddFirstDeclFromEachModule(D, /*IncludeLocal*/true);
  218. }
  219. Record.append(LazySpecializations.begin(), LazySpecializations.end());
  220. // Update the size entry we added earlier.
  221. Record[I] = Record.size() - I - 1;
  222. }
  223. /// Ensure that this template specialization is associated with the specified
  224. /// template on reload.
  225. void RegisterTemplateSpecialization(const Decl *Template,
  226. const Decl *Specialization) {
  227. Template = Template->getCanonicalDecl();
  228. // If the canonical template is local, we'll write out this specialization
  229. // when we emit it.
  230. // FIXME: We can do the same thing if there is any local declaration of
  231. // the template, to avoid emitting an update record.
  232. if (!Template->isFromASTFile())
  233. return;
  234. // We only need to associate the first local declaration of the
  235. // specialization. The other declarations will get pulled in by it.
  236. if (Writer.getFirstLocalDecl(Specialization) != Specialization)
  237. return;
  238. Writer.DeclUpdates[Template].push_back(ASTWriter::DeclUpdate(
  239. UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, Specialization));
  240. }
  241. };
  242. }
  243. void ASTDeclWriter::Visit(Decl *D) {
  244. DeclVisitor<ASTDeclWriter>::Visit(D);
  245. // Source locations require array (variable-length) abbreviations. The
  246. // abbreviation infrastructure requires that arrays are encoded last, so
  247. // we handle it here in the case of those classes derived from DeclaratorDecl
  248. if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
  249. if (auto *TInfo = DD->getTypeSourceInfo())
  250. Record.AddTypeLoc(TInfo->getTypeLoc());
  251. }
  252. // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
  253. // have been written. We want it last because we will not read it back when
  254. // retrieving it from the AST, we'll just lazily set the offset.
  255. if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  256. Record.push_back(FD->doesThisDeclarationHaveABody());
  257. if (FD->doesThisDeclarationHaveABody())
  258. Record.AddFunctionDefinition(FD);
  259. }
  260. // If this declaration is also a DeclContext, write blocks for the
  261. // declarations that lexically stored inside its context and those
  262. // declarations that are visible from its context.
  263. if (DeclContext *DC = dyn_cast<DeclContext>(D))
  264. VisitDeclContext(DC);
  265. }
  266. void ASTDeclWriter::VisitDecl(Decl *D) {
  267. Record.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()));
  268. if (D->getDeclContext() != D->getLexicalDeclContext())
  269. Record.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()));
  270. else
  271. Record.push_back(0);
  272. Record.push_back(D->isInvalidDecl());
  273. Record.push_back(D->hasAttrs());
  274. if (D->hasAttrs())
  275. Record.AddAttributes(D->getAttrs());
  276. Record.push_back(D->isImplicit());
  277. Record.push_back(D->isUsed(false));
  278. Record.push_back(D->isReferenced());
  279. Record.push_back(D->isTopLevelDeclInObjCContainer());
  280. Record.push_back(D->getAccess());
  281. Record.push_back(D->isModulePrivate());
  282. Record.push_back(Writer.getSubmoduleID(D->getOwningModule()));
  283. // If this declaration injected a name into a context different from its
  284. // lexical context, and that context is an imported namespace, we need to
  285. // update its visible declarations to include this name.
  286. //
  287. // This happens when we instantiate a class with a friend declaration or a
  288. // function with a local extern declaration, for instance.
  289. //
  290. // FIXME: Can we handle this in AddedVisibleDecl instead?
  291. if (D->isOutOfLine()) {
  292. auto *DC = D->getDeclContext();
  293. while (auto *NS = dyn_cast<NamespaceDecl>(DC->getRedeclContext())) {
  294. if (!NS->isFromASTFile())
  295. break;
  296. Writer.UpdatedDeclContexts.insert(NS->getPrimaryContext());
  297. if (!NS->isInlineNamespace())
  298. break;
  299. DC = NS->getParent();
  300. }
  301. }
  302. }
  303. void ASTDeclWriter::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
  304. StringRef Arg = D->getArg();
  305. Record.push_back(Arg.size());
  306. VisitDecl(D);
  307. Record.AddSourceLocation(D->getBeginLoc());
  308. Record.push_back(D->getCommentKind());
  309. Record.AddString(Arg);
  310. Code = serialization::DECL_PRAGMA_COMMENT;
  311. }
  312. void ASTDeclWriter::VisitPragmaDetectMismatchDecl(
  313. PragmaDetectMismatchDecl *D) {
  314. StringRef Name = D->getName();
  315. StringRef Value = D->getValue();
  316. Record.push_back(Name.size() + 1 + Value.size());
  317. VisitDecl(D);
  318. Record.AddSourceLocation(D->getBeginLoc());
  319. Record.AddString(Name);
  320. Record.AddString(Value);
  321. Code = serialization::DECL_PRAGMA_DETECT_MISMATCH;
  322. }
  323. void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
  324. llvm_unreachable("Translation units aren't directly serialized");
  325. }
  326. void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {
  327. VisitDecl(D);
  328. Record.AddDeclarationName(D->getDeclName());
  329. Record.push_back(needsAnonymousDeclarationNumber(D)
  330. ? Writer.getAnonymousDeclarationNumber(D)
  331. : 0);
  332. }
  333. void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {
  334. VisitNamedDecl(D);
  335. Record.AddSourceLocation(D->getBeginLoc());
  336. Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
  337. }
  338. void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {
  339. VisitRedeclarable(D);
  340. VisitTypeDecl(D);
  341. Record.AddTypeSourceInfo(D->getTypeSourceInfo());
  342. Record.push_back(D->isModed());
  343. if (D->isModed())
  344. Record.AddTypeRef(D->getUnderlyingType());
  345. Record.AddDeclRef(D->getAnonDeclWithTypedefName(false));
  346. }
  347. void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
  348. VisitTypedefNameDecl(D);
  349. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  350. !D->hasAttrs() &&
  351. !D->isImplicit() &&
  352. D->getFirstDecl() == D->getMostRecentDecl() &&
  353. !D->isInvalidDecl() &&
  354. !D->isTopLevelDeclInObjCContainer() &&
  355. !D->isModulePrivate() &&
  356. !needsAnonymousDeclarationNumber(D) &&
  357. D->getDeclName().getNameKind() == DeclarationName::Identifier)
  358. AbbrevToUse = Writer.getDeclTypedefAbbrev();
  359. Code = serialization::DECL_TYPEDEF;
  360. }
  361. void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {
  362. VisitTypedefNameDecl(D);
  363. Record.AddDeclRef(D->getDescribedAliasTemplate());
  364. Code = serialization::DECL_TYPEALIAS;
  365. }
  366. void ASTDeclWriter::VisitTagDecl(TagDecl *D) {
  367. VisitRedeclarable(D);
  368. VisitTypeDecl(D);
  369. Record.push_back(D->getIdentifierNamespace());
  370. Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
  371. if (!isa<CXXRecordDecl>(D))
  372. Record.push_back(D->isCompleteDefinition());
  373. Record.push_back(D->isEmbeddedInDeclarator());
  374. Record.push_back(D->isFreeStanding());
  375. Record.push_back(D->isCompleteDefinitionRequired());
  376. Record.AddSourceRange(D->getBraceRange());
  377. if (D->hasExtInfo()) {
  378. Record.push_back(1);
  379. Record.AddQualifierInfo(*D->getExtInfo());
  380. } else if (auto *TD = D->getTypedefNameForAnonDecl()) {
  381. Record.push_back(2);
  382. Record.AddDeclRef(TD);
  383. Record.AddIdentifierRef(TD->getDeclName().getAsIdentifierInfo());
  384. } else {
  385. Record.push_back(0);
  386. }
  387. }
  388. void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {
  389. VisitTagDecl(D);
  390. Record.AddTypeSourceInfo(D->getIntegerTypeSourceInfo());
  391. if (!D->getIntegerTypeSourceInfo())
  392. Record.AddTypeRef(D->getIntegerType());
  393. Record.AddTypeRef(D->getPromotionType());
  394. Record.push_back(D->getNumPositiveBits());
  395. Record.push_back(D->getNumNegativeBits());
  396. Record.push_back(D->isScoped());
  397. Record.push_back(D->isScopedUsingClassTag());
  398. Record.push_back(D->isFixed());
  399. Record.push_back(D->getODRHash());
  400. if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {
  401. Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
  402. Record.push_back(MemberInfo->getTemplateSpecializationKind());
  403. Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
  404. } else {
  405. Record.AddDeclRef(nullptr);
  406. }
  407. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  408. !D->hasAttrs() &&
  409. !D->isImplicit() &&
  410. !D->isUsed(false) &&
  411. !D->hasExtInfo() &&
  412. !D->getTypedefNameForAnonDecl() &&
  413. D->getFirstDecl() == D->getMostRecentDecl() &&
  414. !D->isInvalidDecl() &&
  415. !D->isReferenced() &&
  416. !D->isTopLevelDeclInObjCContainer() &&
  417. D->getAccess() == AS_none &&
  418. !D->isModulePrivate() &&
  419. !CXXRecordDecl::classofKind(D->getKind()) &&
  420. !D->getIntegerTypeSourceInfo() &&
  421. !D->getMemberSpecializationInfo() &&
  422. !needsAnonymousDeclarationNumber(D) &&
  423. D->getDeclName().getNameKind() == DeclarationName::Identifier)
  424. AbbrevToUse = Writer.getDeclEnumAbbrev();
  425. Code = serialization::DECL_ENUM;
  426. }
  427. void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {
  428. VisitTagDecl(D);
  429. Record.push_back(D->hasFlexibleArrayMember());
  430. Record.push_back(D->isAnonymousStructOrUnion());
  431. Record.push_back(D->hasObjectMember());
  432. Record.push_back(D->hasVolatileMember());
  433. Record.push_back(D->isNonTrivialToPrimitiveDefaultInitialize());
  434. Record.push_back(D->isNonTrivialToPrimitiveCopy());
  435. Record.push_back(D->isNonTrivialToPrimitiveDestroy());
  436. Record.push_back(D->hasNonTrivialToPrimitiveDefaultInitializeCUnion());
  437. Record.push_back(D->hasNonTrivialToPrimitiveDestructCUnion());
  438. Record.push_back(D->hasNonTrivialToPrimitiveCopyCUnion());
  439. Record.push_back(D->isParamDestroyedInCallee());
  440. Record.push_back(D->getArgPassingRestrictions());
  441. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  442. !D->hasAttrs() &&
  443. !D->isImplicit() &&
  444. !D->isUsed(false) &&
  445. !D->hasExtInfo() &&
  446. !D->getTypedefNameForAnonDecl() &&
  447. D->getFirstDecl() == D->getMostRecentDecl() &&
  448. !D->isInvalidDecl() &&
  449. !D->isReferenced() &&
  450. !D->isTopLevelDeclInObjCContainer() &&
  451. D->getAccess() == AS_none &&
  452. !D->isModulePrivate() &&
  453. !CXXRecordDecl::classofKind(D->getKind()) &&
  454. !needsAnonymousDeclarationNumber(D) &&
  455. D->getDeclName().getNameKind() == DeclarationName::Identifier)
  456. AbbrevToUse = Writer.getDeclRecordAbbrev();
  457. Code = serialization::DECL_RECORD;
  458. }
  459. void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {
  460. VisitNamedDecl(D);
  461. Record.AddTypeRef(D->getType());
  462. }
  463. void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
  464. VisitValueDecl(D);
  465. Record.push_back(D->getInitExpr()? 1 : 0);
  466. if (D->getInitExpr())
  467. Record.AddStmt(D->getInitExpr());
  468. Record.AddAPSInt(D->getInitVal());
  469. Code = serialization::DECL_ENUM_CONSTANT;
  470. }
  471. void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
  472. VisitValueDecl(D);
  473. Record.AddSourceLocation(D->getInnerLocStart());
  474. Record.push_back(D->hasExtInfo());
  475. if (D->hasExtInfo()) {
  476. DeclaratorDecl::ExtInfo *Info = D->getExtInfo();
  477. Record.AddQualifierInfo(*Info);
  478. Record.AddStmt(Info->TrailingRequiresClause);
  479. }
  480. // The location information is deferred until the end of the record.
  481. Record.AddTypeRef(D->getTypeSourceInfo() ? D->getTypeSourceInfo()->getType()
  482. : QualType());
  483. }
  484. void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
  485. VisitRedeclarable(D);
  486. VisitDeclaratorDecl(D);
  487. Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
  488. Record.push_back(D->getIdentifierNamespace());
  489. // FunctionDecl's body is handled last at ASTWriterDecl::Visit,
  490. // after everything else is written.
  491. Record.push_back(static_cast<int>(D->getStorageClass())); // FIXME: stable encoding
  492. Record.push_back(D->isInlineSpecified());
  493. Record.push_back(D->isInlined());
  494. Record.push_back(D->isVirtualAsWritten());
  495. Record.push_back(D->isPure());
  496. Record.push_back(D->hasInheritedPrototype());
  497. Record.push_back(D->hasWrittenPrototype());
  498. Record.push_back(D->isDeletedBit());
  499. Record.push_back(D->isTrivial());
  500. Record.push_back(D->isTrivialForCall());
  501. Record.push_back(D->isDefaulted());
  502. Record.push_back(D->isExplicitlyDefaulted());
  503. Record.push_back(D->hasImplicitReturnZero());
  504. Record.push_back(static_cast<uint64_t>(D->getConstexprKind()));
  505. Record.push_back(D->usesSEHTry());
  506. Record.push_back(D->hasSkippedBody());
  507. Record.push_back(D->isMultiVersion());
  508. Record.push_back(D->isLateTemplateParsed());
  509. Record.push_back(D->getLinkageInternal());
  510. Record.AddSourceLocation(D->getEndLoc());
  511. Record.push_back(D->getODRHash());
  512. if (D->isDefaulted()) {
  513. if (auto *FDI = D->getDefaultedFunctionInfo()) {
  514. Record.push_back(FDI->getUnqualifiedLookups().size());
  515. for (DeclAccessPair P : FDI->getUnqualifiedLookups()) {
  516. Record.AddDeclRef(P.getDecl());
  517. Record.push_back(P.getAccess());
  518. }
  519. } else {
  520. Record.push_back(0);
  521. }
  522. }
  523. Record.push_back(D->getTemplatedKind());
  524. switch (D->getTemplatedKind()) {
  525. case FunctionDecl::TK_NonTemplate:
  526. break;
  527. case FunctionDecl::TK_FunctionTemplate:
  528. Record.AddDeclRef(D->getDescribedFunctionTemplate());
  529. break;
  530. case FunctionDecl::TK_MemberSpecialization: {
  531. MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
  532. Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
  533. Record.push_back(MemberInfo->getTemplateSpecializationKind());
  534. Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
  535. break;
  536. }
  537. case FunctionDecl::TK_FunctionTemplateSpecialization: {
  538. FunctionTemplateSpecializationInfo *
  539. FTSInfo = D->getTemplateSpecializationInfo();
  540. RegisterTemplateSpecialization(FTSInfo->getTemplate(), D);
  541. Record.AddDeclRef(FTSInfo->getTemplate());
  542. Record.push_back(FTSInfo->getTemplateSpecializationKind());
  543. // Template arguments.
  544. Record.AddTemplateArgumentList(FTSInfo->TemplateArguments);
  545. // Template args as written.
  546. Record.push_back(FTSInfo->TemplateArgumentsAsWritten != nullptr);
  547. if (FTSInfo->TemplateArgumentsAsWritten) {
  548. Record.push_back(FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs);
  549. for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs;
  550. i!=e; ++i)
  551. Record.AddTemplateArgumentLoc(
  552. (*FTSInfo->TemplateArgumentsAsWritten)[i]);
  553. Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->LAngleLoc);
  554. Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->RAngleLoc);
  555. }
  556. Record.AddSourceLocation(FTSInfo->getPointOfInstantiation());
  557. if (MemberSpecializationInfo *MemberInfo =
  558. FTSInfo->getMemberSpecializationInfo()) {
  559. Record.push_back(1);
  560. Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
  561. Record.push_back(MemberInfo->getTemplateSpecializationKind());
  562. Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
  563. } else {
  564. Record.push_back(0);
  565. }
  566. if (D->isCanonicalDecl()) {
  567. // Write the template that contains the specializations set. We will
  568. // add a FunctionTemplateSpecializationInfo to it when reading.
  569. Record.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl());
  570. }
  571. break;
  572. }
  573. case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
  574. DependentFunctionTemplateSpecializationInfo *
  575. DFTSInfo = D->getDependentSpecializationInfo();
  576. // Templates.
  577. Record.push_back(DFTSInfo->getNumTemplates());
  578. for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
  579. Record.AddDeclRef(DFTSInfo->getTemplate(i));
  580. // Templates args.
  581. Record.push_back(DFTSInfo->getNumTemplateArgs());
  582. for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
  583. Record.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i));
  584. Record.AddSourceLocation(DFTSInfo->getLAngleLoc());
  585. Record.AddSourceLocation(DFTSInfo->getRAngleLoc());
  586. break;
  587. }
  588. }
  589. Record.push_back(D->param_size());
  590. for (auto P : D->parameters())
  591. Record.AddDeclRef(P);
  592. Code = serialization::DECL_FUNCTION;
  593. }
  594. static void addExplicitSpecifier(ExplicitSpecifier ES,
  595. ASTRecordWriter &Record) {
  596. uint64_t Kind = static_cast<uint64_t>(ES.getKind());
  597. Kind = Kind << 1 | static_cast<bool>(ES.getExpr());
  598. Record.push_back(Kind);
  599. if (ES.getExpr()) {
  600. Record.AddStmt(ES.getExpr());
  601. }
  602. }
  603. void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
  604. addExplicitSpecifier(D->getExplicitSpecifier(), Record);
  605. Record.AddDeclRef(D->Ctor);
  606. VisitFunctionDecl(D);
  607. Record.push_back(D->isCopyDeductionCandidate());
  608. Code = serialization::DECL_CXX_DEDUCTION_GUIDE;
  609. }
  610. void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
  611. VisitNamedDecl(D);
  612. // FIXME: convert to LazyStmtPtr?
  613. // Unlike C/C++, method bodies will never be in header files.
  614. bool HasBodyStuff = D->getBody() != nullptr;
  615. Record.push_back(HasBodyStuff);
  616. if (HasBodyStuff) {
  617. Record.AddStmt(D->getBody());
  618. }
  619. Record.AddDeclRef(D->getSelfDecl());
  620. Record.AddDeclRef(D->getCmdDecl());
  621. Record.push_back(D->isInstanceMethod());
  622. Record.push_back(D->isVariadic());
  623. Record.push_back(D->isPropertyAccessor());
  624. Record.push_back(D->isSynthesizedAccessorStub());
  625. Record.push_back(D->isDefined());
  626. Record.push_back(D->isOverriding());
  627. Record.push_back(D->hasSkippedBody());
  628. Record.push_back(D->isRedeclaration());
  629. Record.push_back(D->hasRedeclaration());
  630. if (D->hasRedeclaration()) {
  631. assert(Context.getObjCMethodRedeclaration(D));
  632. Record.AddDeclRef(Context.getObjCMethodRedeclaration(D));
  633. }
  634. // FIXME: stable encoding for @required/@optional
  635. Record.push_back(D->getImplementationControl());
  636. // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway/nullability
  637. Record.push_back(D->getObjCDeclQualifier());
  638. Record.push_back(D->hasRelatedResultType());
  639. Record.AddTypeRef(D->getReturnType());
  640. Record.AddTypeSourceInfo(D->getReturnTypeSourceInfo());
  641. Record.AddSourceLocation(D->getEndLoc());
  642. Record.push_back(D->param_size());
  643. for (const auto *P : D->parameters())
  644. Record.AddDeclRef(P);
  645. Record.push_back(D->getSelLocsKind());
  646. unsigned NumStoredSelLocs = D->getNumStoredSelLocs();
  647. SourceLocation *SelLocs = D->getStoredSelLocs();
  648. Record.push_back(NumStoredSelLocs);
  649. for (unsigned i = 0; i != NumStoredSelLocs; ++i)
  650. Record.AddSourceLocation(SelLocs[i]);
  651. Code = serialization::DECL_OBJC_METHOD;
  652. }
  653. void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
  654. VisitTypedefNameDecl(D);
  655. Record.push_back(D->Variance);
  656. Record.push_back(D->Index);
  657. Record.AddSourceLocation(D->VarianceLoc);
  658. Record.AddSourceLocation(D->ColonLoc);
  659. Code = serialization::DECL_OBJC_TYPE_PARAM;
  660. }
  661. void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
  662. VisitNamedDecl(D);
  663. Record.AddSourceLocation(D->getAtStartLoc());
  664. Record.AddSourceRange(D->getAtEndRange());
  665. // Abstract class (no need to define a stable serialization::DECL code).
  666. }
  667. void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
  668. VisitRedeclarable(D);
  669. VisitObjCContainerDecl(D);
  670. Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
  671. AddObjCTypeParamList(D->TypeParamList);
  672. Record.push_back(D->isThisDeclarationADefinition());
  673. if (D->isThisDeclarationADefinition()) {
  674. // Write the DefinitionData
  675. ObjCInterfaceDecl::DefinitionData &Data = D->data();
  676. Record.AddTypeSourceInfo(D->getSuperClassTInfo());
  677. Record.AddSourceLocation(D->getEndOfDefinitionLoc());
  678. Record.push_back(Data.HasDesignatedInitializers);
  679. // Write out the protocols that are directly referenced by the @interface.
  680. Record.push_back(Data.ReferencedProtocols.size());
  681. for (const auto *P : D->protocols())
  682. Record.AddDeclRef(P);
  683. for (const auto &PL : D->protocol_locs())
  684. Record.AddSourceLocation(PL);
  685. // Write out the protocols that are transitively referenced.
  686. Record.push_back(Data.AllReferencedProtocols.size());
  687. for (ObjCList<ObjCProtocolDecl>::iterator
  688. P = Data.AllReferencedProtocols.begin(),
  689. PEnd = Data.AllReferencedProtocols.end();
  690. P != PEnd; ++P)
  691. Record.AddDeclRef(*P);
  692. if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {
  693. // Ensure that we write out the set of categories for this class.
  694. Writer.ObjCClassesWithCategories.insert(D);
  695. // Make sure that the categories get serialized.
  696. for (; Cat; Cat = Cat->getNextClassCategoryRaw())
  697. (void)Writer.GetDeclRef(Cat);
  698. }
  699. }
  700. Code = serialization::DECL_OBJC_INTERFACE;
  701. }
  702. void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
  703. VisitFieldDecl(D);
  704. // FIXME: stable encoding for @public/@private/@protected/@package
  705. Record.push_back(D->getAccessControl());
  706. Record.push_back(D->getSynthesize());
  707. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  708. !D->hasAttrs() &&
  709. !D->isImplicit() &&
  710. !D->isUsed(false) &&
  711. !D->isInvalidDecl() &&
  712. !D->isReferenced() &&
  713. !D->isModulePrivate() &&
  714. !D->getBitWidth() &&
  715. !D->hasExtInfo() &&
  716. D->getDeclName())
  717. AbbrevToUse = Writer.getDeclObjCIvarAbbrev();
  718. Code = serialization::DECL_OBJC_IVAR;
  719. }
  720. void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
  721. VisitRedeclarable(D);
  722. VisitObjCContainerDecl(D);
  723. Record.push_back(D->isThisDeclarationADefinition());
  724. if (D->isThisDeclarationADefinition()) {
  725. Record.push_back(D->protocol_size());
  726. for (const auto *I : D->protocols())
  727. Record.AddDeclRef(I);
  728. for (const auto &PL : D->protocol_locs())
  729. Record.AddSourceLocation(PL);
  730. }
  731. Code = serialization::DECL_OBJC_PROTOCOL;
  732. }
  733. void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
  734. VisitFieldDecl(D);
  735. Code = serialization::DECL_OBJC_AT_DEFS_FIELD;
  736. }
  737. void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
  738. VisitObjCContainerDecl(D);
  739. Record.AddSourceLocation(D->getCategoryNameLoc());
  740. Record.AddSourceLocation(D->getIvarLBraceLoc());
  741. Record.AddSourceLocation(D->getIvarRBraceLoc());
  742. Record.AddDeclRef(D->getClassInterface());
  743. AddObjCTypeParamList(D->TypeParamList);
  744. Record.push_back(D->protocol_size());
  745. for (const auto *I : D->protocols())
  746. Record.AddDeclRef(I);
  747. for (const auto &PL : D->protocol_locs())
  748. Record.AddSourceLocation(PL);
  749. Code = serialization::DECL_OBJC_CATEGORY;
  750. }
  751. void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
  752. VisitNamedDecl(D);
  753. Record.AddDeclRef(D->getClassInterface());
  754. Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;
  755. }
  756. void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
  757. VisitNamedDecl(D);
  758. Record.AddSourceLocation(D->getAtLoc());
  759. Record.AddSourceLocation(D->getLParenLoc());
  760. Record.AddTypeRef(D->getType());
  761. Record.AddTypeSourceInfo(D->getTypeSourceInfo());
  762. // FIXME: stable encoding
  763. Record.push_back((unsigned)D->getPropertyAttributes());
  764. Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
  765. // FIXME: stable encoding
  766. Record.push_back((unsigned)D->getPropertyImplementation());
  767. Record.AddDeclarationName(D->getGetterName());
  768. Record.AddSourceLocation(D->getGetterNameLoc());
  769. Record.AddDeclarationName(D->getSetterName());
  770. Record.AddSourceLocation(D->getSetterNameLoc());
  771. Record.AddDeclRef(D->getGetterMethodDecl());
  772. Record.AddDeclRef(D->getSetterMethodDecl());
  773. Record.AddDeclRef(D->getPropertyIvarDecl());
  774. Code = serialization::DECL_OBJC_PROPERTY;
  775. }
  776. void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
  777. VisitObjCContainerDecl(D);
  778. Record.AddDeclRef(D->getClassInterface());
  779. // Abstract class (no need to define a stable serialization::DECL code).
  780. }
  781. void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
  782. VisitObjCImplDecl(D);
  783. Record.AddSourceLocation(D->getCategoryNameLoc());
  784. Code = serialization::DECL_OBJC_CATEGORY_IMPL;
  785. }
  786. void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
  787. VisitObjCImplDecl(D);
  788. Record.AddDeclRef(D->getSuperClass());
  789. Record.AddSourceLocation(D->getSuperClassLoc());
  790. Record.AddSourceLocation(D->getIvarLBraceLoc());
  791. Record.AddSourceLocation(D->getIvarRBraceLoc());
  792. Record.push_back(D->hasNonZeroConstructors());
  793. Record.push_back(D->hasDestructors());
  794. Record.push_back(D->NumIvarInitializers);
  795. if (D->NumIvarInitializers)
  796. Record.AddCXXCtorInitializers(
  797. llvm::makeArrayRef(D->init_begin(), D->init_end()));
  798. Code = serialization::DECL_OBJC_IMPLEMENTATION;
  799. }
  800. void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
  801. VisitDecl(D);
  802. Record.AddSourceLocation(D->getBeginLoc());
  803. Record.AddDeclRef(D->getPropertyDecl());
  804. Record.AddDeclRef(D->getPropertyIvarDecl());
  805. Record.AddSourceLocation(D->getPropertyIvarDeclLoc());
  806. Record.AddDeclRef(D->getGetterMethodDecl());
  807. Record.AddDeclRef(D->getSetterMethodDecl());
  808. Record.AddStmt(D->getGetterCXXConstructor());
  809. Record.AddStmt(D->getSetterCXXAssignment());
  810. Code = serialization::DECL_OBJC_PROPERTY_IMPL;
  811. }
  812. void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {
  813. VisitDeclaratorDecl(D);
  814. Record.push_back(D->isMutable());
  815. FieldDecl::InitStorageKind ISK = D->InitStorage.getInt();
  816. Record.push_back(ISK);
  817. if (ISK == FieldDecl::ISK_CapturedVLAType)
  818. Record.AddTypeRef(QualType(D->getCapturedVLAType(), 0));
  819. else if (ISK)
  820. Record.AddStmt(D->getInClassInitializer());
  821. Record.AddStmt(D->getBitWidth());
  822. if (!D->getDeclName())
  823. Record.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D));
  824. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  825. !D->hasAttrs() &&
  826. !D->isImplicit() &&
  827. !D->isUsed(false) &&
  828. !D->isInvalidDecl() &&
  829. !D->isReferenced() &&
  830. !D->isTopLevelDeclInObjCContainer() &&
  831. !D->isModulePrivate() &&
  832. !D->getBitWidth() &&
  833. !D->hasInClassInitializer() &&
  834. !D->hasCapturedVLAType() &&
  835. !D->hasExtInfo() &&
  836. !ObjCIvarDecl::classofKind(D->getKind()) &&
  837. !ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&
  838. D->getDeclName())
  839. AbbrevToUse = Writer.getDeclFieldAbbrev();
  840. Code = serialization::DECL_FIELD;
  841. }
  842. void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {
  843. VisitDeclaratorDecl(D);
  844. Record.AddIdentifierRef(D->getGetterId());
  845. Record.AddIdentifierRef(D->getSetterId());
  846. Code = serialization::DECL_MS_PROPERTY;
  847. }
  848. void ASTDeclWriter::VisitMSGuidDecl(MSGuidDecl *D) {
  849. VisitValueDecl(D);
  850. MSGuidDecl::Parts Parts = D->getParts();
  851. Record.push_back(Parts.Part1);
  852. Record.push_back(Parts.Part2);
  853. Record.push_back(Parts.Part3);
  854. for (auto C : Parts.Part4And5)
  855. Record.push_back(C);
  856. Code = serialization::DECL_MS_GUID;
  857. }
  858. void ASTDeclWriter::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {
  859. VisitValueDecl(D);
  860. Record.AddAPValue(D->getValue());
  861. Code = serialization::DECL_TEMPLATE_PARAM_OBJECT;
  862. }
  863. void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
  864. VisitValueDecl(D);
  865. Record.push_back(D->getChainingSize());
  866. for (const auto *P : D->chain())
  867. Record.AddDeclRef(P);
  868. Code = serialization::DECL_INDIRECTFIELD;
  869. }
  870. void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
  871. VisitRedeclarable(D);
  872. VisitDeclaratorDecl(D);
  873. Record.push_back(D->getStorageClass());
  874. Record.push_back(D->getTSCSpec());
  875. Record.push_back(D->getInitStyle());
  876. Record.push_back(D->isARCPseudoStrong());
  877. if (!isa<ParmVarDecl>(D)) {
  878. Record.push_back(D->isThisDeclarationADemotedDefinition());
  879. Record.push_back(D->isExceptionVariable());
  880. Record.push_back(D->isNRVOVariable());
  881. Record.push_back(D->isCXXForRangeDecl());
  882. Record.push_back(D->isObjCForDecl());
  883. Record.push_back(D->isInline());
  884. Record.push_back(D->isInlineSpecified());
  885. Record.push_back(D->isConstexpr());
  886. Record.push_back(D->isInitCapture());
  887. Record.push_back(D->isPreviousDeclInSameBlockScope());
  888. if (const auto *IPD = dyn_cast<ImplicitParamDecl>(D))
  889. Record.push_back(static_cast<unsigned>(IPD->getParameterKind()));
  890. else
  891. Record.push_back(0);
  892. Record.push_back(D->isEscapingByref());
  893. }
  894. Record.push_back(D->getLinkageInternal());
  895. Record.AddVarDeclInit(D);
  896. if (D->hasAttr<BlocksAttr>() && D->getType()->getAsCXXRecordDecl()) {
  897. BlockVarCopyInit Init = Writer.Context->getBlockVarCopyInit(D);
  898. Record.AddStmt(Init.getCopyExpr());
  899. if (Init.getCopyExpr())
  900. Record.push_back(Init.canThrow());
  901. }
  902. if (D->getStorageDuration() == SD_Static) {
  903. bool ModulesCodegen = false;
  904. if (Writer.WritingModule &&
  905. !D->getDescribedVarTemplate() && !D->getMemberSpecializationInfo() &&
  906. !isa<VarTemplateSpecializationDecl>(D)) {
  907. // When building a C++ Modules TS module interface unit, a strong
  908. // definition in the module interface is provided by the compilation of
  909. // that module interface unit, not by its users. (Inline variables are
  910. // still emitted in module users.)
  911. ModulesCodegen =
  912. (Writer.WritingModule->Kind == Module::ModuleInterfaceUnit ||
  913. (D->hasAttr<DLLExportAttr>() &&
  914. Writer.Context->getLangOpts().BuildingPCHWithObjectFile)) &&
  915. Writer.Context->GetGVALinkageForVariable(D) == GVA_StrongExternal;
  916. }
  917. Record.push_back(ModulesCodegen);
  918. if (ModulesCodegen)
  919. Writer.ModularCodegenDecls.push_back(Writer.GetDeclRef(D));
  920. }
  921. enum {
  922. VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
  923. };
  924. if (VarTemplateDecl *TemplD = D->getDescribedVarTemplate()) {
  925. Record.push_back(VarTemplate);
  926. Record.AddDeclRef(TemplD);
  927. } else if (MemberSpecializationInfo *SpecInfo
  928. = D->getMemberSpecializationInfo()) {
  929. Record.push_back(StaticDataMemberSpecialization);
  930. Record.AddDeclRef(SpecInfo->getInstantiatedFrom());
  931. Record.push_back(SpecInfo->getTemplateSpecializationKind());
  932. Record.AddSourceLocation(SpecInfo->getPointOfInstantiation());
  933. } else {
  934. Record.push_back(VarNotTemplate);
  935. }
  936. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  937. !D->hasAttrs() &&
  938. !D->isImplicit() &&
  939. !D->isUsed(false) &&
  940. !D->isInvalidDecl() &&
  941. !D->isReferenced() &&
  942. !D->isTopLevelDeclInObjCContainer() &&
  943. D->getAccess() == AS_none &&
  944. !D->isModulePrivate() &&
  945. !needsAnonymousDeclarationNumber(D) &&
  946. D->getDeclName().getNameKind() == DeclarationName::Identifier &&
  947. !D->hasExtInfo() &&
  948. D->getFirstDecl() == D->getMostRecentDecl() &&
  949. D->getKind() == Decl::Var &&
  950. !D->isInline() &&
  951. !D->isConstexpr() &&
  952. !D->isInitCapture() &&
  953. !D->isPreviousDeclInSameBlockScope() &&
  954. !(D->hasAttr<BlocksAttr>() && D->getType()->getAsCXXRecordDecl()) &&
  955. !D->isEscapingByref() &&
  956. D->getStorageDuration() != SD_Static &&
  957. !D->getMemberSpecializationInfo())
  958. AbbrevToUse = Writer.getDeclVarAbbrev();
  959. Code = serialization::DECL_VAR;
  960. }
  961. void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
  962. VisitVarDecl(D);
  963. Code = serialization::DECL_IMPLICIT_PARAM;
  964. }
  965. void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
  966. VisitVarDecl(D);
  967. Record.push_back(D->isObjCMethodParameter());
  968. Record.push_back(D->getFunctionScopeDepth());
  969. Record.push_back(D->getFunctionScopeIndex());
  970. Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
  971. Record.push_back(D->isKNRPromoted());
  972. Record.push_back(D->hasInheritedDefaultArg());
  973. Record.push_back(D->hasUninstantiatedDefaultArg());
  974. if (D->hasUninstantiatedDefaultArg())
  975. Record.AddStmt(D->getUninstantiatedDefaultArg());
  976. Code = serialization::DECL_PARM_VAR;
  977. // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
  978. // we dynamically check for the properties that we optimize for, but don't
  979. // know are true of all PARM_VAR_DECLs.
  980. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  981. !D->hasAttrs() &&
  982. !D->hasExtInfo() &&
  983. !D->isImplicit() &&
  984. !D->isUsed(false) &&
  985. !D->isInvalidDecl() &&
  986. !D->isReferenced() &&
  987. D->getAccess() == AS_none &&
  988. !D->isModulePrivate() &&
  989. D->getStorageClass() == 0 &&
  990. D->getInitStyle() == VarDecl::CInit && // Can params have anything else?
  991. D->getFunctionScopeDepth() == 0 &&
  992. D->getObjCDeclQualifier() == 0 &&
  993. !D->isKNRPromoted() &&
  994. !D->hasInheritedDefaultArg() &&
  995. D->getInit() == nullptr &&
  996. !D->hasUninstantiatedDefaultArg()) // No default expr.
  997. AbbrevToUse = Writer.getDeclParmVarAbbrev();
  998. // Check things we know are true of *every* PARM_VAR_DECL, which is more than
  999. // just us assuming it.
  1000. assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");
  1001. assert(!D->isThisDeclarationADemotedDefinition()
  1002. && "PARM_VAR_DECL can't be demoted definition.");
  1003. assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
  1004. assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
  1005. assert(D->getPreviousDecl() == nullptr && "PARM_VAR_DECL can't be redecl");
  1006. assert(!D->isStaticDataMember() &&
  1007. "PARM_VAR_DECL can't be static data member");
  1008. }
  1009. void ASTDeclWriter::VisitDecompositionDecl(DecompositionDecl *D) {
  1010. // Record the number of bindings first to simplify deserialization.
  1011. Record.push_back(D->bindings().size());
  1012. VisitVarDecl(D);
  1013. for (auto *B : D->bindings())
  1014. Record.AddDeclRef(B);
  1015. Code = serialization::DECL_DECOMPOSITION;
  1016. }
  1017. void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {
  1018. VisitValueDecl(D);
  1019. Record.AddStmt(D->getBinding());
  1020. Code = serialization::DECL_BINDING;
  1021. }
  1022. void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
  1023. VisitDecl(D);
  1024. Record.AddStmt(D->getAsmString());
  1025. Record.AddSourceLocation(D->getRParenLoc());
  1026. Code = serialization::DECL_FILE_SCOPE_ASM;
  1027. }
  1028. void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {
  1029. VisitDecl(D);
  1030. Code = serialization::DECL_EMPTY;
  1031. }
  1032. void ASTDeclWriter::VisitLifetimeExtendedTemporaryDecl(
  1033. LifetimeExtendedTemporaryDecl *D) {
  1034. VisitDecl(D);
  1035. Record.AddDeclRef(D->getExtendingDecl());
  1036. Record.AddStmt(D->getTemporaryExpr());
  1037. Record.push_back(static_cast<bool>(D->getValue()));
  1038. if (D->getValue())
  1039. Record.AddAPValue(*D->getValue());
  1040. Record.push_back(D->getManglingNumber());
  1041. Code = serialization::DECL_LIFETIME_EXTENDED_TEMPORARY;
  1042. }
  1043. void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
  1044. VisitDecl(D);
  1045. Record.AddStmt(D->getBody());
  1046. Record.AddTypeSourceInfo(D->getSignatureAsWritten());
  1047. Record.push_back(D->param_size());
  1048. for (ParmVarDecl *P : D->parameters())
  1049. Record.AddDeclRef(P);
  1050. Record.push_back(D->isVariadic());
  1051. Record.push_back(D->blockMissingReturnType());
  1052. Record.push_back(D->isConversionFromLambda());
  1053. Record.push_back(D->doesNotEscape());
  1054. Record.push_back(D->canAvoidCopyToHeap());
  1055. Record.push_back(D->capturesCXXThis());
  1056. Record.push_back(D->getNumCaptures());
  1057. for (const auto &capture : D->captures()) {
  1058. Record.AddDeclRef(capture.getVariable());
  1059. unsigned flags = 0;
  1060. if (capture.isByRef()) flags |= 1;
  1061. if (capture.isNested()) flags |= 2;
  1062. if (capture.hasCopyExpr()) flags |= 4;
  1063. Record.push_back(flags);
  1064. if (capture.hasCopyExpr()) Record.AddStmt(capture.getCopyExpr());
  1065. }
  1066. Code = serialization::DECL_BLOCK;
  1067. }
  1068. void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
  1069. Record.push_back(CD->getNumParams());
  1070. VisitDecl(CD);
  1071. Record.push_back(CD->getContextParamPosition());
  1072. Record.push_back(CD->isNothrow() ? 1 : 0);
  1073. // Body is stored by VisitCapturedStmt.
  1074. for (unsigned I = 0; I < CD->getNumParams(); ++I)
  1075. Record.AddDeclRef(CD->getParam(I));
  1076. Code = serialization::DECL_CAPTURED;
  1077. }
  1078. void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
  1079. VisitDecl(D);
  1080. Record.push_back(D->getLanguage());
  1081. Record.AddSourceLocation(D->getExternLoc());
  1082. Record.AddSourceLocation(D->getRBraceLoc());
  1083. Code = serialization::DECL_LINKAGE_SPEC;
  1084. }
  1085. void ASTDeclWriter::VisitExportDecl(ExportDecl *D) {
  1086. VisitDecl(D);
  1087. Record.AddSourceLocation(D->getRBraceLoc());
  1088. Code = serialization::DECL_EXPORT;
  1089. }
  1090. void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {
  1091. VisitNamedDecl(D);
  1092. Record.AddSourceLocation(D->getBeginLoc());
  1093. Code = serialization::DECL_LABEL;
  1094. }
  1095. void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
  1096. VisitRedeclarable(D);
  1097. VisitNamedDecl(D);
  1098. Record.push_back(D->isInline());
  1099. Record.AddSourceLocation(D->getBeginLoc());
  1100. Record.AddSourceLocation(D->getRBraceLoc());
  1101. if (D->isOriginalNamespace())
  1102. Record.AddDeclRef(D->getAnonymousNamespace());
  1103. Code = serialization::DECL_NAMESPACE;
  1104. if (Writer.hasChain() && D->isAnonymousNamespace() &&
  1105. D == D->getMostRecentDecl()) {
  1106. // This is a most recent reopening of the anonymous namespace. If its parent
  1107. // is in a previous PCH (or is the TU), mark that parent for update, because
  1108. // the original namespace always points to the latest re-opening of its
  1109. // anonymous namespace.
  1110. Decl *Parent = cast<Decl>(
  1111. D->getParent()->getRedeclContext()->getPrimaryContext());
  1112. if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {
  1113. Writer.DeclUpdates[Parent].push_back(
  1114. ASTWriter::DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, D));
  1115. }
  1116. }
  1117. }
  1118. void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
  1119. VisitRedeclarable(D);
  1120. VisitNamedDecl(D);
  1121. Record.AddSourceLocation(D->getNamespaceLoc());
  1122. Record.AddSourceLocation(D->getTargetNameLoc());
  1123. Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
  1124. Record.AddDeclRef(D->getNamespace());
  1125. Code = serialization::DECL_NAMESPACE_ALIAS;
  1126. }
  1127. void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {
  1128. VisitNamedDecl(D);
  1129. Record.AddSourceLocation(D->getUsingLoc());
  1130. Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
  1131. Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
  1132. Record.AddDeclRef(D->FirstUsingShadow.getPointer());
  1133. Record.push_back(D->hasTypename());
  1134. Record.AddDeclRef(Context.getInstantiatedFromUsingDecl(D));
  1135. Code = serialization::DECL_USING;
  1136. }
  1137. void ASTDeclWriter::VisitUsingEnumDecl(UsingEnumDecl *D) {
  1138. VisitNamedDecl(D);
  1139. Record.AddSourceLocation(D->getUsingLoc());
  1140. Record.AddSourceLocation(D->getEnumLoc());
  1141. Record.AddDeclRef(D->getEnumDecl());
  1142. Record.AddDeclRef(D->FirstUsingShadow.getPointer());
  1143. Record.AddDeclRef(Context.getInstantiatedFromUsingEnumDecl(D));
  1144. Code = serialization::DECL_USING_ENUM;
  1145. }
  1146. void ASTDeclWriter::VisitUsingPackDecl(UsingPackDecl *D) {
  1147. Record.push_back(D->NumExpansions);
  1148. VisitNamedDecl(D);
  1149. Record.AddDeclRef(D->getInstantiatedFromUsingDecl());
  1150. for (auto *E : D->expansions())
  1151. Record.AddDeclRef(E);
  1152. Code = serialization::DECL_USING_PACK;
  1153. }
  1154. void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
  1155. VisitRedeclarable(D);
  1156. VisitNamedDecl(D);
  1157. Record.AddDeclRef(D->getTargetDecl());
  1158. Record.push_back(D->getIdentifierNamespace());
  1159. Record.AddDeclRef(D->UsingOrNextShadow);
  1160. Record.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D));
  1161. Code = serialization::DECL_USING_SHADOW;
  1162. }
  1163. void ASTDeclWriter::VisitConstructorUsingShadowDecl(
  1164. ConstructorUsingShadowDecl *D) {
  1165. VisitUsingShadowDecl(D);
  1166. Record.AddDeclRef(D->NominatedBaseClassShadowDecl);
  1167. Record.AddDeclRef(D->ConstructedBaseClassShadowDecl);
  1168. Record.push_back(D->IsVirtual);
  1169. Code = serialization::DECL_CONSTRUCTOR_USING_SHADOW;
  1170. }
  1171. void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
  1172. VisitNamedDecl(D);
  1173. Record.AddSourceLocation(D->getUsingLoc());
  1174. Record.AddSourceLocation(D->getNamespaceKeyLocation());
  1175. Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
  1176. Record.AddDeclRef(D->getNominatedNamespace());
  1177. Record.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()));
  1178. Code = serialization::DECL_USING_DIRECTIVE;
  1179. }
  1180. void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
  1181. VisitValueDecl(D);
  1182. Record.AddSourceLocation(D->getUsingLoc());
  1183. Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
  1184. Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
  1185. Record.AddSourceLocation(D->getEllipsisLoc());
  1186. Code = serialization::DECL_UNRESOLVED_USING_VALUE;
  1187. }
  1188. void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
  1189. UnresolvedUsingTypenameDecl *D) {
  1190. VisitTypeDecl(D);
  1191. Record.AddSourceLocation(D->getTypenameLoc());
  1192. Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
  1193. Record.AddSourceLocation(D->getEllipsisLoc());
  1194. Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;
  1195. }
  1196. void ASTDeclWriter::VisitUnresolvedUsingIfExistsDecl(
  1197. UnresolvedUsingIfExistsDecl *D) {
  1198. VisitNamedDecl(D);
  1199. Code = serialization::DECL_UNRESOLVED_USING_IF_EXISTS;
  1200. }
  1201. void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
  1202. VisitRecordDecl(D);
  1203. enum {
  1204. CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
  1205. };
  1206. if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
  1207. Record.push_back(CXXRecTemplate);
  1208. Record.AddDeclRef(TemplD);
  1209. } else if (MemberSpecializationInfo *MSInfo
  1210. = D->getMemberSpecializationInfo()) {
  1211. Record.push_back(CXXRecMemberSpecialization);
  1212. Record.AddDeclRef(MSInfo->getInstantiatedFrom());
  1213. Record.push_back(MSInfo->getTemplateSpecializationKind());
  1214. Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
  1215. } else {
  1216. Record.push_back(CXXRecNotTemplate);
  1217. }
  1218. Record.push_back(D->isThisDeclarationADefinition());
  1219. if (D->isThisDeclarationADefinition())
  1220. Record.AddCXXDefinitionData(D);
  1221. // Store (what we currently believe to be) the key function to avoid
  1222. // deserializing every method so we can compute it.
  1223. if (D->isCompleteDefinition())
  1224. Record.AddDeclRef(Context.getCurrentKeyFunction(D));
  1225. Code = serialization::DECL_CXX_RECORD;
  1226. }
  1227. void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
  1228. VisitFunctionDecl(D);
  1229. if (D->isCanonicalDecl()) {
  1230. Record.push_back(D->size_overridden_methods());
  1231. for (const CXXMethodDecl *MD : D->overridden_methods())
  1232. Record.AddDeclRef(MD);
  1233. } else {
  1234. // We only need to record overridden methods once for the canonical decl.
  1235. Record.push_back(0);
  1236. }
  1237. if (D->getDeclContext() == D->getLexicalDeclContext() &&
  1238. D->getFirstDecl() == D->getMostRecentDecl() &&
  1239. !D->isInvalidDecl() &&
  1240. !D->hasAttrs() &&
  1241. !D->isTopLevelDeclInObjCContainer() &&
  1242. D->getDeclName().getNameKind() == DeclarationName::Identifier &&
  1243. !D->hasExtInfo() &&
  1244. !D->hasInheritedPrototype() &&
  1245. D->hasWrittenPrototype())
  1246. AbbrevToUse = Writer.getDeclCXXMethodAbbrev();
  1247. Code = serialization::DECL_CXX_METHOD;
  1248. }
  1249. void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
  1250. Record.push_back(D->getTrailingAllocKind());
  1251. addExplicitSpecifier(D->getExplicitSpecifier(), Record);
  1252. if (auto Inherited = D->getInheritedConstructor()) {
  1253. Record.AddDeclRef(Inherited.getShadowDecl());
  1254. Record.AddDeclRef(Inherited.getConstructor());
  1255. }
  1256. VisitCXXMethodDecl(D);
  1257. Code = serialization::DECL_CXX_CONSTRUCTOR;
  1258. }
  1259. void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
  1260. VisitCXXMethodDecl(D);
  1261. Record.AddDeclRef(D->getOperatorDelete());
  1262. if (D->getOperatorDelete())
  1263. Record.AddStmt(D->getOperatorDeleteThisArg());
  1264. Code = serialization::DECL_CXX_DESTRUCTOR;
  1265. }
  1266. void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
  1267. addExplicitSpecifier(D->getExplicitSpecifier(), Record);
  1268. VisitCXXMethodDecl(D);
  1269. Code = serialization::DECL_CXX_CONVERSION;
  1270. }
  1271. void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {
  1272. VisitDecl(D);
  1273. Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));
  1274. ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();
  1275. Record.push_back(!IdentifierLocs.empty());
  1276. if (IdentifierLocs.empty()) {
  1277. Record.AddSourceLocation(D->getEndLoc());
  1278. Record.push_back(1);
  1279. } else {
  1280. for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)
  1281. Record.AddSourceLocation(IdentifierLocs[I]);
  1282. Record.push_back(IdentifierLocs.size());
  1283. }
  1284. // Note: the number of source locations must always be the last element in
  1285. // the record.
  1286. Code = serialization::DECL_IMPORT;
  1287. }
  1288. void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
  1289. VisitDecl(D);
  1290. Record.AddSourceLocation(D->getColonLoc());
  1291. Code = serialization::DECL_ACCESS_SPEC;
  1292. }
  1293. void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {
  1294. // Record the number of friend type template parameter lists here
  1295. // so as to simplify memory allocation during deserialization.
  1296. Record.push_back(D->NumTPLists);
  1297. VisitDecl(D);
  1298. bool hasFriendDecl = D->Friend.is<NamedDecl*>();
  1299. Record.push_back(hasFriendDecl);
  1300. if (hasFriendDecl)
  1301. Record.AddDeclRef(D->getFriendDecl());
  1302. else
  1303. Record.AddTypeSourceInfo(D->getFriendType());
  1304. for (unsigned i = 0; i < D->NumTPLists; ++i)
  1305. Record.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i));
  1306. Record.AddDeclRef(D->getNextFriend());
  1307. Record.push_back(D->UnsupportedFriend);
  1308. Record.AddSourceLocation(D->FriendLoc);
  1309. Code = serialization::DECL_FRIEND;
  1310. }
  1311. void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
  1312. VisitDecl(D);
  1313. Record.push_back(D->getNumTemplateParameters());
  1314. for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
  1315. Record.AddTemplateParameterList(D->getTemplateParameterList(i));
  1316. Record.push_back(D->getFriendDecl() != nullptr);
  1317. if (D->getFriendDecl())
  1318. Record.AddDeclRef(D->getFriendDecl());
  1319. else
  1320. Record.AddTypeSourceInfo(D->getFriendType());
  1321. Record.AddSourceLocation(D->getFriendLoc());
  1322. Code = serialization::DECL_FRIEND_TEMPLATE;
  1323. }
  1324. void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
  1325. VisitNamedDecl(D);
  1326. Record.AddDeclRef(D->getTemplatedDecl());
  1327. Record.AddTemplateParameterList(D->getTemplateParameters());
  1328. }
  1329. void ASTDeclWriter::VisitConceptDecl(ConceptDecl *D) {
  1330. VisitTemplateDecl(D);
  1331. Record.AddStmt(D->getConstraintExpr());
  1332. Code = serialization::DECL_CONCEPT;
  1333. }
  1334. void ASTDeclWriter::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {
  1335. Code = serialization::DECL_REQUIRES_EXPR_BODY;
  1336. }
  1337. void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
  1338. VisitRedeclarable(D);
  1339. // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
  1340. // getCommonPtr() can be used while this is still initializing.
  1341. if (D->isFirstDecl()) {
  1342. // This declaration owns the 'common' pointer, so serialize that data now.
  1343. Record.AddDeclRef(D->getInstantiatedFromMemberTemplate());
  1344. if (D->getInstantiatedFromMemberTemplate())
  1345. Record.push_back(D->isMemberSpecialization());
  1346. }
  1347. VisitTemplateDecl(D);
  1348. Record.push_back(D->getIdentifierNamespace());
  1349. }
  1350. void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
  1351. VisitRedeclarableTemplateDecl(D);
  1352. if (D->isFirstDecl())
  1353. AddTemplateSpecializations(D);
  1354. Code = serialization::DECL_CLASS_TEMPLATE;
  1355. }
  1356. void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
  1357. ClassTemplateSpecializationDecl *D) {
  1358. RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
  1359. VisitCXXRecordDecl(D);
  1360. llvm::PointerUnion<ClassTemplateDecl *,
  1361. ClassTemplatePartialSpecializationDecl *> InstFrom
  1362. = D->getSpecializedTemplateOrPartial();
  1363. if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {
  1364. Record.AddDeclRef(InstFromD);
  1365. } else {
  1366. Record.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>());
  1367. Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
  1368. }
  1369. Record.AddTemplateArgumentList(&D->getTemplateArgs());
  1370. Record.AddSourceLocation(D->getPointOfInstantiation());
  1371. Record.push_back(D->getSpecializationKind());
  1372. Record.push_back(D->isCanonicalDecl());
  1373. if (D->isCanonicalDecl()) {
  1374. // When reading, we'll add it to the folding set of the following template.
  1375. Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
  1376. }
  1377. // Explicit info.
  1378. Record.AddTypeSourceInfo(D->getTypeAsWritten());
  1379. if (D->getTypeAsWritten()) {
  1380. Record.AddSourceLocation(D->getExternLoc());
  1381. Record.AddSourceLocation(D->getTemplateKeywordLoc());
  1382. }
  1383. Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;
  1384. }
  1385. void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
  1386. ClassTemplatePartialSpecializationDecl *D) {
  1387. Record.AddTemplateParameterList(D->getTemplateParameters());
  1388. Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
  1389. VisitClassTemplateSpecializationDecl(D);
  1390. // These are read/set from/to the first declaration.
  1391. if (D->getPreviousDecl() == nullptr) {
  1392. Record.AddDeclRef(D->getInstantiatedFromMember());
  1393. Record.push_back(D->isMemberSpecialization());
  1394. }
  1395. Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
  1396. }
  1397. void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {
  1398. VisitRedeclarableTemplateDecl(D);
  1399. if (D->isFirstDecl())
  1400. AddTemplateSpecializations(D);
  1401. Code = serialization::DECL_VAR_TEMPLATE;
  1402. }
  1403. void ASTDeclWriter::VisitVarTemplateSpecializationDecl(
  1404. VarTemplateSpecializationDecl *D) {
  1405. RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
  1406. VisitVarDecl(D);
  1407. llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
  1408. InstFrom = D->getSpecializedTemplateOrPartial();
  1409. if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {
  1410. Record.AddDeclRef(InstFromD);
  1411. } else {
  1412. Record.AddDeclRef(InstFrom.get<VarTemplatePartialSpecializationDecl *>());
  1413. Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
  1414. }
  1415. // Explicit info.
  1416. Record.AddTypeSourceInfo(D->getTypeAsWritten());
  1417. if (D->getTypeAsWritten()) {
  1418. Record.AddSourceLocation(D->getExternLoc());
  1419. Record.AddSourceLocation(D->getTemplateKeywordLoc());
  1420. }
  1421. Record.AddTemplateArgumentList(&D->getTemplateArgs());
  1422. Record.AddSourceLocation(D->getPointOfInstantiation());
  1423. Record.push_back(D->getSpecializationKind());
  1424. Record.push_back(D->IsCompleteDefinition);
  1425. Record.push_back(D->isCanonicalDecl());
  1426. if (D->isCanonicalDecl()) {
  1427. // When reading, we'll add it to the folding set of the following template.
  1428. Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
  1429. }
  1430. Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;
  1431. }
  1432. void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(
  1433. VarTemplatePartialSpecializationDecl *D) {
  1434. Record.AddTemplateParameterList(D->getTemplateParameters());
  1435. Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
  1436. VisitVarTemplateSpecializationDecl(D);
  1437. // These are read/set from/to the first declaration.
  1438. if (D->getPreviousDecl() == nullptr) {
  1439. Record.AddDeclRef(D->getInstantiatedFromMember());
  1440. Record.push_back(D->isMemberSpecialization());
  1441. }
  1442. Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;
  1443. }
  1444. void ASTDeclWriter::VisitClassScopeFunctionSpecializationDecl(
  1445. ClassScopeFunctionSpecializationDecl *D) {
  1446. VisitDecl(D);
  1447. Record.AddDeclRef(D->getSpecialization());
  1448. Record.push_back(D->hasExplicitTemplateArgs());
  1449. if (D->hasExplicitTemplateArgs())
  1450. Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
  1451. Code = serialization::DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION;
  1452. }
  1453. void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
  1454. VisitRedeclarableTemplateDecl(D);
  1455. if (D->isFirstDecl())
  1456. AddTemplateSpecializations(D);
  1457. Code = serialization::DECL_FUNCTION_TEMPLATE;
  1458. }
  1459. void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
  1460. Record.push_back(D->hasTypeConstraint());
  1461. VisitTypeDecl(D);
  1462. Record.push_back(D->wasDeclaredWithTypename());
  1463. const TypeConstraint *TC = D->getTypeConstraint();
  1464. Record.push_back(TC != nullptr);
  1465. if (TC) {
  1466. Record.AddNestedNameSpecifierLoc(TC->getNestedNameSpecifierLoc());
  1467. Record.AddDeclarationNameInfo(TC->getConceptNameInfo());
  1468. Record.AddDeclRef(TC->getNamedConcept());
  1469. Record.push_back(TC->getTemplateArgsAsWritten() != nullptr);
  1470. if (TC->getTemplateArgsAsWritten())
  1471. Record.AddASTTemplateArgumentListInfo(TC->getTemplateArgsAsWritten());
  1472. Record.AddStmt(TC->getImmediatelyDeclaredConstraint());
  1473. Record.push_back(D->isExpandedParameterPack());
  1474. if (D->isExpandedParameterPack())
  1475. Record.push_back(D->getNumExpansionParameters());
  1476. }
  1477. bool OwnsDefaultArg = D->hasDefaultArgument() &&
  1478. !D->defaultArgumentWasInherited();
  1479. Record.push_back(OwnsDefaultArg);
  1480. if (OwnsDefaultArg)
  1481. Record.AddTypeSourceInfo(D->getDefaultArgumentInfo());
  1482. Code = serialization::DECL_TEMPLATE_TYPE_PARM;
  1483. }
  1484. void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
  1485. // For an expanded parameter pack, record the number of expansion types here
  1486. // so that it's easier for deserialization to allocate the right amount of
  1487. // memory.
  1488. Expr *TypeConstraint = D->getPlaceholderTypeConstraint();
  1489. Record.push_back(!!TypeConstraint);
  1490. if (D->isExpandedParameterPack())
  1491. Record.push_back(D->getNumExpansionTypes());
  1492. VisitDeclaratorDecl(D);
  1493. // TemplateParmPosition.
  1494. Record.push_back(D->getDepth());
  1495. Record.push_back(D->getPosition());
  1496. if (TypeConstraint)
  1497. Record.AddStmt(TypeConstraint);
  1498. if (D->isExpandedParameterPack()) {
  1499. for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
  1500. Record.AddTypeRef(D->getExpansionType(I));
  1501. Record.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I));
  1502. }
  1503. Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;
  1504. } else {
  1505. // Rest of NonTypeTemplateParmDecl.
  1506. Record.push_back(D->isParameterPack());
  1507. bool OwnsDefaultArg = D->hasDefaultArgument() &&
  1508. !D->defaultArgumentWasInherited();
  1509. Record.push_back(OwnsDefaultArg);
  1510. if (OwnsDefaultArg)
  1511. Record.AddStmt(D->getDefaultArgument());
  1512. Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
  1513. }
  1514. }
  1515. void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
  1516. // For an expanded parameter pack, record the number of expansion types here
  1517. // so that it's easier for deserialization to allocate the right amount of
  1518. // memory.
  1519. if (D->isExpandedParameterPack())
  1520. Record.push_back(D->getNumExpansionTemplateParameters());
  1521. VisitTemplateDecl(D);
  1522. // TemplateParmPosition.
  1523. Record.push_back(D->getDepth());
  1524. Record.push_back(D->getPosition());
  1525. if (D->isExpandedParameterPack()) {
  1526. for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
  1527. I != N; ++I)
  1528. Record.AddTemplateParameterList(D->getExpansionTemplateParameters(I));
  1529. Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;
  1530. } else {
  1531. // Rest of TemplateTemplateParmDecl.
  1532. Record.push_back(D->isParameterPack());
  1533. bool OwnsDefaultArg = D->hasDefaultArgument() &&
  1534. !D->defaultArgumentWasInherited();
  1535. Record.push_back(OwnsDefaultArg);
  1536. if (OwnsDefaultArg)
  1537. Record.AddTemplateArgumentLoc(D->getDefaultArgument());
  1538. Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;
  1539. }
  1540. }
  1541. void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
  1542. VisitRedeclarableTemplateDecl(D);
  1543. Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;
  1544. }
  1545. void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
  1546. VisitDecl(D);
  1547. Record.AddStmt(D->getAssertExpr());
  1548. Record.push_back(D->isFailed());
  1549. Record.AddStmt(D->getMessage());
  1550. Record.AddSourceLocation(D->getRParenLoc());
  1551. Code = serialization::DECL_STATIC_ASSERT;
  1552. }
  1553. /// Emit the DeclContext part of a declaration context decl.
  1554. void ASTDeclWriter::VisitDeclContext(DeclContext *DC) {
  1555. Record.AddOffset(Writer.WriteDeclContextLexicalBlock(Context, DC));
  1556. Record.AddOffset(Writer.WriteDeclContextVisibleBlock(Context, DC));
  1557. }
  1558. const Decl *ASTWriter::getFirstLocalDecl(const Decl *D) {
  1559. assert(IsLocalDecl(D) && "expected a local declaration");
  1560. const Decl *Canon = D->getCanonicalDecl();
  1561. if (IsLocalDecl(Canon))
  1562. return Canon;
  1563. const Decl *&CacheEntry = FirstLocalDeclCache[Canon];
  1564. if (CacheEntry)
  1565. return CacheEntry;
  1566. for (const Decl *Redecl = D; Redecl; Redecl = Redecl->getPreviousDecl())
  1567. if (IsLocalDecl(Redecl))
  1568. D = Redecl;
  1569. return CacheEntry = D;
  1570. }
  1571. template <typename T>
  1572. void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {
  1573. T *First = D->getFirstDecl();
  1574. T *MostRecent = First->getMostRecentDecl();
  1575. T *DAsT = static_cast<T *>(D);
  1576. if (MostRecent != First) {
  1577. assert(isRedeclarableDeclKind(DAsT->getKind()) &&
  1578. "Not considered redeclarable?");
  1579. Record.AddDeclRef(First);
  1580. // Write out a list of local redeclarations of this declaration if it's the
  1581. // first local declaration in the chain.
  1582. const Decl *FirstLocal = Writer.getFirstLocalDecl(DAsT);
  1583. if (DAsT == FirstLocal) {
  1584. // Emit a list of all imported first declarations so that we can be sure
  1585. // that all redeclarations visible to this module are before D in the
  1586. // redecl chain.
  1587. unsigned I = Record.size();
  1588. Record.push_back(0);
  1589. if (Writer.Chain)
  1590. AddFirstDeclFromEachModule(DAsT, /*IncludeLocal*/false);
  1591. // This is the number of imported first declarations + 1.
  1592. Record[I] = Record.size() - I;
  1593. // Collect the set of local redeclarations of this declaration, from
  1594. // newest to oldest.
  1595. ASTWriter::RecordData LocalRedecls;
  1596. ASTRecordWriter LocalRedeclWriter(Record, LocalRedecls);
  1597. for (const Decl *Prev = FirstLocal->getMostRecentDecl();
  1598. Prev != FirstLocal; Prev = Prev->getPreviousDecl())
  1599. if (!Prev->isFromASTFile())
  1600. LocalRedeclWriter.AddDeclRef(Prev);
  1601. // If we have any redecls, write them now as a separate record preceding
  1602. // the declaration itself.
  1603. if (LocalRedecls.empty())
  1604. Record.push_back(0);
  1605. else
  1606. Record.AddOffset(LocalRedeclWriter.Emit(LOCAL_REDECLARATIONS));
  1607. } else {
  1608. Record.push_back(0);
  1609. Record.AddDeclRef(FirstLocal);
  1610. }
  1611. // Make sure that we serialize both the previous and the most-recent
  1612. // declarations, which (transitively) ensures that all declarations in the
  1613. // chain get serialized.
  1614. //
  1615. // FIXME: This is not correct; when we reach an imported declaration we
  1616. // won't emit its previous declaration.
  1617. (void)Writer.GetDeclRef(D->getPreviousDecl());
  1618. (void)Writer.GetDeclRef(MostRecent);
  1619. } else {
  1620. // We use the sentinel value 0 to indicate an only declaration.
  1621. Record.push_back(0);
  1622. }
  1623. }
  1624. void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
  1625. Record.writeOMPChildren(D->Data);
  1626. VisitDecl(D);
  1627. Code = serialization::DECL_OMP_THREADPRIVATE;
  1628. }
  1629. void ASTDeclWriter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
  1630. Record.writeOMPChildren(D->Data);
  1631. VisitDecl(D);
  1632. Code = serialization::DECL_OMP_ALLOCATE;
  1633. }
  1634. void ASTDeclWriter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
  1635. Record.writeOMPChildren(D->Data);
  1636. VisitDecl(D);
  1637. Code = serialization::DECL_OMP_REQUIRES;
  1638. }
  1639. void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
  1640. VisitValueDecl(D);
  1641. Record.AddSourceLocation(D->getBeginLoc());
  1642. Record.AddStmt(D->getCombinerIn());
  1643. Record.AddStmt(D->getCombinerOut());
  1644. Record.AddStmt(D->getCombiner());
  1645. Record.AddStmt(D->getInitOrig());
  1646. Record.AddStmt(D->getInitPriv());
  1647. Record.AddStmt(D->getInitializer());
  1648. Record.push_back(D->getInitializerKind());
  1649. Record.AddDeclRef(D->getPrevDeclInScope());
  1650. Code = serialization::DECL_OMP_DECLARE_REDUCTION;
  1651. }
  1652. void ASTDeclWriter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
  1653. Record.writeOMPChildren(D->Data);
  1654. VisitValueDecl(D);
  1655. Record.AddDeclarationName(D->getVarName());
  1656. Record.AddDeclRef(D->getPrevDeclInScope());
  1657. Code = serialization::DECL_OMP_DECLARE_MAPPER;
  1658. }
  1659. void ASTDeclWriter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
  1660. VisitVarDecl(D);
  1661. Code = serialization::DECL_OMP_CAPTUREDEXPR;
  1662. }
  1663. //===----------------------------------------------------------------------===//
  1664. // ASTWriter Implementation
  1665. //===----------------------------------------------------------------------===//
  1666. void ASTWriter::WriteDeclAbbrevs() {
  1667. using namespace llvm;
  1668. std::shared_ptr<BitCodeAbbrev> Abv;
  1669. // Abbreviation for DECL_FIELD
  1670. Abv = std::make_shared<BitCodeAbbrev>();
  1671. Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));
  1672. // Decl
  1673. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1674. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1675. Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl
  1676. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1677. Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
  1678. Abv->Add(BitCodeAbbrevOp(0)); // isUsed
  1679. Abv->Add(BitCodeAbbrevOp(0)); // isReferenced
  1680. Abv->Add(BitCodeAbbrevOp(0)); // TopLevelDeclInObjCContainer
  1681. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // AccessSpecifier
  1682. Abv->Add(BitCodeAbbrevOp(0)); // ModulePrivate
  1683. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1684. // NamedDecl
  1685. Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
  1686. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
  1687. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1688. // ValueDecl
  1689. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  1690. // DeclaratorDecl
  1691. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
  1692. Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
  1693. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
  1694. // FieldDecl
  1695. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
  1696. Abv->Add(BitCodeAbbrevOp(0)); // InitStyle
  1697. // Type Source Info
  1698. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1699. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
  1700. DeclFieldAbbrev = Stream.EmitAbbrev(std::move(Abv));
  1701. // Abbreviation for DECL_OBJC_IVAR
  1702. Abv = std::make_shared<BitCodeAbbrev>();
  1703. Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));
  1704. // Decl
  1705. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1706. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1707. Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl
  1708. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1709. Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
  1710. Abv->Add(BitCodeAbbrevOp(0)); // isUsed
  1711. Abv->Add(BitCodeAbbrevOp(0)); // isReferenced
  1712. Abv->Add(BitCodeAbbrevOp(0)); // TopLevelDeclInObjCContainer
  1713. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // AccessSpecifier
  1714. Abv->Add(BitCodeAbbrevOp(0)); // ModulePrivate
  1715. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1716. // NamedDecl
  1717. Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
  1718. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
  1719. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1720. // ValueDecl
  1721. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  1722. // DeclaratorDecl
  1723. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
  1724. Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
  1725. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
  1726. // FieldDecl
  1727. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
  1728. Abv->Add(BitCodeAbbrevOp(0)); // InitStyle
  1729. // ObjC Ivar
  1730. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl
  1731. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize
  1732. // Type Source Info
  1733. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1734. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
  1735. DeclObjCIvarAbbrev = Stream.EmitAbbrev(std::move(Abv));
  1736. // Abbreviation for DECL_ENUM
  1737. Abv = std::make_shared<BitCodeAbbrev>();
  1738. Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));
  1739. // Redeclarable
  1740. Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
  1741. // Decl
  1742. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1743. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1744. Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl
  1745. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1746. Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
  1747. Abv->Add(BitCodeAbbrevOp(0)); // isUsed
  1748. Abv->Add(BitCodeAbbrevOp(0)); // isReferenced
  1749. Abv->Add(BitCodeAbbrevOp(0)); // TopLevelDeclInObjCContainer
  1750. Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
  1751. Abv->Add(BitCodeAbbrevOp(0)); // ModulePrivate
  1752. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1753. // NamedDecl
  1754. Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
  1755. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
  1756. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1757. // TypeDecl
  1758. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
  1759. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
  1760. // TagDecl
  1761. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace
  1762. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getTagKind
  1763. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
  1764. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
  1765. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
  1766. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
  1767. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
  1768. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
  1769. Abv->Add(BitCodeAbbrevOp(0)); // ExtInfoKind
  1770. // EnumDecl
  1771. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddTypeRef
  1772. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IntegerType
  1773. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getPromotionType
  1774. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getNumPositiveBits
  1775. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getNumNegativeBits
  1776. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScoped
  1777. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScopedUsingClassTag
  1778. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isFixed
  1779. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));// ODRHash
  1780. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedMembEnum
  1781. // DC
  1782. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset
  1783. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset
  1784. DeclEnumAbbrev = Stream.EmitAbbrev(std::move(Abv));
  1785. // Abbreviation for DECL_RECORD
  1786. Abv = std::make_shared<BitCodeAbbrev>();
  1787. Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));
  1788. // Redeclarable
  1789. Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
  1790. // Decl
  1791. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1792. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1793. Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl
  1794. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1795. Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
  1796. Abv->Add(BitCodeAbbrevOp(0)); // isUsed
  1797. Abv->Add(BitCodeAbbrevOp(0)); // isReferenced
  1798. Abv->Add(BitCodeAbbrevOp(0)); // TopLevelDeclInObjCContainer
  1799. Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
  1800. Abv->Add(BitCodeAbbrevOp(0)); // ModulePrivate
  1801. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1802. // NamedDecl
  1803. Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
  1804. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
  1805. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1806. // TypeDecl
  1807. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
  1808. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
  1809. // TagDecl
  1810. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace
  1811. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getTagKind
  1812. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
  1813. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
  1814. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
  1815. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
  1816. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
  1817. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
  1818. Abv->Add(BitCodeAbbrevOp(0)); // ExtInfoKind
  1819. // RecordDecl
  1820. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // FlexibleArrayMember
  1821. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // AnonymousStructUnion
  1822. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasObjectMember
  1823. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasVolatileMember
  1824. // isNonTrivialToPrimitiveDefaultInitialize
  1825. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  1826. // isNonTrivialToPrimitiveCopy
  1827. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  1828. // isNonTrivialToPrimitiveDestroy
  1829. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  1830. // hasNonTrivialToPrimitiveDefaultInitializeCUnion
  1831. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  1832. // hasNonTrivialToPrimitiveDestructCUnion
  1833. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  1834. // hasNonTrivialToPrimitiveCopyCUnion
  1835. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  1836. // isParamDestroyedInCallee
  1837. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  1838. // getArgPassingRestrictions
  1839. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));
  1840. // DC
  1841. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset
  1842. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset
  1843. DeclRecordAbbrev = Stream.EmitAbbrev(std::move(Abv));
  1844. // Abbreviation for DECL_PARM_VAR
  1845. Abv = std::make_shared<BitCodeAbbrev>();
  1846. Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));
  1847. // Redeclarable
  1848. Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
  1849. // Decl
  1850. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1851. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1852. Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl
  1853. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1854. Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
  1855. Abv->Add(BitCodeAbbrevOp(0)); // isUsed
  1856. Abv->Add(BitCodeAbbrevOp(0)); // isReferenced
  1857. Abv->Add(BitCodeAbbrevOp(0)); // TopLevelDeclInObjCContainer
  1858. Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
  1859. Abv->Add(BitCodeAbbrevOp(0)); // ModulePrivate
  1860. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1861. // NamedDecl
  1862. Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
  1863. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
  1864. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1865. // ValueDecl
  1866. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  1867. // DeclaratorDecl
  1868. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
  1869. Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
  1870. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
  1871. // VarDecl
  1872. Abv->Add(BitCodeAbbrevOp(0)); // SClass
  1873. Abv->Add(BitCodeAbbrevOp(0)); // TSCSpec
  1874. Abv->Add(BitCodeAbbrevOp(0)); // InitStyle
  1875. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isARCPseudoStrong
  1876. Abv->Add(BitCodeAbbrevOp(0)); // Linkage
  1877. Abv->Add(BitCodeAbbrevOp(0)); // HasInit
  1878. Abv->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo
  1879. // ParmVarDecl
  1880. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsObjCMethodParameter
  1881. Abv->Add(BitCodeAbbrevOp(0)); // ScopeDepth
  1882. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex
  1883. Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
  1884. Abv->Add(BitCodeAbbrevOp(0)); // KNRPromoted
  1885. Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
  1886. Abv->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg
  1887. // Type Source Info
  1888. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1889. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
  1890. DeclParmVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
  1891. // Abbreviation for DECL_TYPEDEF
  1892. Abv = std::make_shared<BitCodeAbbrev>();
  1893. Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));
  1894. // Redeclarable
  1895. Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
  1896. // Decl
  1897. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1898. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1899. Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl
  1900. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1901. Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
  1902. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isUsed
  1903. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isReferenced
  1904. Abv->Add(BitCodeAbbrevOp(0)); // TopLevelDeclInObjCContainer
  1905. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // C++ AccessSpecifier
  1906. Abv->Add(BitCodeAbbrevOp(0)); // ModulePrivate
  1907. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1908. // NamedDecl
  1909. Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
  1910. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
  1911. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1912. // TypeDecl
  1913. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
  1914. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
  1915. // TypedefDecl
  1916. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1917. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
  1918. DeclTypedefAbbrev = Stream.EmitAbbrev(std::move(Abv));
  1919. // Abbreviation for DECL_VAR
  1920. Abv = std::make_shared<BitCodeAbbrev>();
  1921. Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));
  1922. // Redeclarable
  1923. Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
  1924. // Decl
  1925. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1926. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1927. Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl
  1928. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1929. Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
  1930. Abv->Add(BitCodeAbbrevOp(0)); // isUsed
  1931. Abv->Add(BitCodeAbbrevOp(0)); // isReferenced
  1932. Abv->Add(BitCodeAbbrevOp(0)); // TopLevelDeclInObjCContainer
  1933. Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
  1934. Abv->Add(BitCodeAbbrevOp(0)); // ModulePrivate
  1935. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1936. // NamedDecl
  1937. Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
  1938. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
  1939. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1940. // ValueDecl
  1941. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  1942. // DeclaratorDecl
  1943. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
  1944. Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
  1945. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
  1946. // VarDecl
  1947. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // SClass
  1948. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // TSCSpec
  1949. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // InitStyle
  1950. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isARCPseudoStrong
  1951. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsThisDeclarationADemotedDefinition
  1952. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isExceptionVariable
  1953. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isNRVOVariable
  1954. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCXXForRangeDecl
  1955. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isObjCForDecl
  1956. Abv->Add(BitCodeAbbrevOp(0)); // isInline
  1957. Abv->Add(BitCodeAbbrevOp(0)); // isInlineSpecified
  1958. Abv->Add(BitCodeAbbrevOp(0)); // isConstexpr
  1959. Abv->Add(BitCodeAbbrevOp(0)); // isInitCapture
  1960. Abv->Add(BitCodeAbbrevOp(0)); // isPrevDeclInSameScope
  1961. Abv->Add(BitCodeAbbrevOp(0)); // ImplicitParamKind
  1962. Abv->Add(BitCodeAbbrevOp(0)); // EscapingByref
  1963. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage
  1964. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // HasConstant*
  1965. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // VarKind (local enum)
  1966. // Type Source Info
  1967. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1968. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
  1969. DeclVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
  1970. // Abbreviation for DECL_CXX_METHOD
  1971. Abv = std::make_shared<BitCodeAbbrev>();
  1972. Abv->Add(BitCodeAbbrevOp(serialization::DECL_CXX_METHOD));
  1973. // RedeclarableDecl
  1974. Abv->Add(BitCodeAbbrevOp(0)); // CanonicalDecl
  1975. // Decl
  1976. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
  1977. Abv->Add(BitCodeAbbrevOp(0)); // LexicalDeclContext
  1978. Abv->Add(BitCodeAbbrevOp(0)); // Invalid
  1979. Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
  1980. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Implicit
  1981. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Used
  1982. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Referenced
  1983. Abv->Add(BitCodeAbbrevOp(0)); // InObjCContainer
  1984. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Access
  1985. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModulePrivate
  1986. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
  1987. // NamedDecl
  1988. Abv->Add(BitCodeAbbrevOp(DeclarationName::Identifier)); // NameKind
  1989. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Identifier
  1990. Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
  1991. // ValueDecl
  1992. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  1993. // DeclaratorDecl
  1994. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerLocStart
  1995. Abv->Add(BitCodeAbbrevOp(0)); // HasExtInfo
  1996. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
  1997. // FunctionDecl
  1998. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS
  1999. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // StorageClass
  2000. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Inline
  2001. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InlineSpecified
  2002. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // VirtualAsWritten
  2003. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Pure
  2004. Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedProto
  2005. Abv->Add(BitCodeAbbrevOp(1)); // HasWrittenProto
  2006. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Deleted
  2007. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Trivial
  2008. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // TrivialForCall
  2009. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Defaulted
  2010. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ExplicitlyDefaulted
  2011. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ImplicitReturnZero
  2012. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Constexpr
  2013. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // UsesSEHTry
  2014. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // SkippedBody
  2015. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // MultiVersion
  2016. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // LateParsed
  2017. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage
  2018. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LocEnd
  2019. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // ODRHash
  2020. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // TemplateKind
  2021. // This Array slurps the rest of the record. Fortunately we want to encode
  2022. // (nearly) all the remaining (variable number of) fields in the same way.
  2023. //
  2024. // This is the function template information if any, then
  2025. // NumParams and Params[] from FunctionDecl, and
  2026. // NumOverriddenMethods, OverriddenMethods[] from CXXMethodDecl.
  2027. //
  2028. // Add an AbbrevOp for 'size then elements' and use it here.
  2029. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2030. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  2031. DeclCXXMethodAbbrev = Stream.EmitAbbrev(std::move(Abv));
  2032. // Abbreviation for EXPR_DECL_REF
  2033. Abv = std::make_shared<BitCodeAbbrev>();
  2034. Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));
  2035. //Stmt
  2036. // Expr
  2037. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  2038. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
  2039. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
  2040. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
  2041. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
  2042. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ContainsErrors
  2043. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
  2044. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
  2045. //DeclRefExpr
  2046. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HasQualifier
  2047. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //GetDeclFound
  2048. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ExplicitTemplateArgs
  2049. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HadMultipleCandidates
  2050. Abv->Add(BitCodeAbbrevOp(0)); // RefersToEnclosingVariableOrCapture
  2051. Abv->Add(BitCodeAbbrevOp(0)); // NonOdrUseReason
  2052. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef
  2053. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
  2054. DeclRefExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
  2055. // Abbreviation for EXPR_INTEGER_LITERAL
  2056. Abv = std::make_shared<BitCodeAbbrev>();
  2057. Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));
  2058. //Stmt
  2059. // Expr
  2060. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  2061. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
  2062. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
  2063. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
  2064. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
  2065. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ContainsErrors
  2066. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
  2067. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
  2068. //Integer Literal
  2069. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
  2070. Abv->Add(BitCodeAbbrevOp(32)); // Bit Width
  2071. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value
  2072. IntegerLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
  2073. // Abbreviation for EXPR_CHARACTER_LITERAL
  2074. Abv = std::make_shared<BitCodeAbbrev>();
  2075. Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));
  2076. //Stmt
  2077. // Expr
  2078. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  2079. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
  2080. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
  2081. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
  2082. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
  2083. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ContainsErrors
  2084. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
  2085. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
  2086. //Character Literal
  2087. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue
  2088. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
  2089. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // getKind
  2090. CharacterLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
  2091. // Abbreviation for EXPR_IMPLICIT_CAST
  2092. Abv = std::make_shared<BitCodeAbbrev>();
  2093. Abv->Add(BitCodeAbbrevOp(serialization::EXPR_IMPLICIT_CAST));
  2094. // Stmt
  2095. // Expr
  2096. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
  2097. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
  2098. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
  2099. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
  2100. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
  2101. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ContainsErrors
  2102. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
  2103. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
  2104. // CastExpr
  2105. Abv->Add(BitCodeAbbrevOp(0)); // PathSize
  2106. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // HasFPFeatures
  2107. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // CastKind
  2108. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PartOfExplicitCast
  2109. // ImplicitCastExpr
  2110. ExprImplicitCastAbbrev = Stream.EmitAbbrev(std::move(Abv));
  2111. Abv = std::make_shared<BitCodeAbbrev>();
  2112. Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));
  2113. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
  2114. DeclContextLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
  2115. Abv = std::make_shared<BitCodeAbbrev>();
  2116. Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));
  2117. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
  2118. DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));
  2119. }
  2120. /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
  2121. /// consumers of the AST.
  2122. ///
  2123. /// Such decls will always be deserialized from the AST file, so we would like
  2124. /// this to be as restrictive as possible. Currently the predicate is driven by
  2125. /// code generation requirements, if other clients have a different notion of
  2126. /// what is "required" then we may have to consider an alternate scheme where
  2127. /// clients can iterate over the top-level decls and get information on them,
  2128. /// without necessary deserializing them. We could explicitly require such
  2129. /// clients to use a separate API call to "realize" the decl. This should be
  2130. /// relatively painless since they would presumably only do it for top-level
  2131. /// decls.
  2132. static bool isRequiredDecl(const Decl *D, ASTContext &Context,
  2133. bool WritingModule) {
  2134. // An ObjCMethodDecl is never considered as "required" because its
  2135. // implementation container always is.
  2136. // File scoped assembly or obj-c or OMP declare target implementation must be
  2137. // seen.
  2138. if (isa<FileScopeAsmDecl>(D) || isa<ObjCImplDecl>(D))
  2139. return true;
  2140. if (WritingModule && isPartOfPerModuleInitializer(D)) {
  2141. // These declarations are part of the module initializer, and are emitted
  2142. // if and when the module is imported, rather than being emitted eagerly.
  2143. return false;
  2144. }
  2145. return Context.DeclMustBeEmitted(D);
  2146. }
  2147. void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
  2148. PrettyDeclStackTraceEntry CrashInfo(Context, D, SourceLocation(),
  2149. "serializing");
  2150. // Determine the ID for this declaration.
  2151. serialization::DeclID ID;
  2152. assert(!D->isFromASTFile() && "should not be emitting imported decl");
  2153. serialization::DeclID &IDR = DeclIDs[D];
  2154. if (IDR == 0)
  2155. IDR = NextDeclID++;
  2156. ID = IDR;
  2157. assert(ID >= FirstDeclID && "invalid decl ID");
  2158. RecordData Record;
  2159. ASTDeclWriter W(*this, Context, Record);
  2160. // Build a record for this declaration
  2161. W.Visit(D);
  2162. // Emit this declaration to the bitstream.
  2163. uint64_t Offset = W.Emit(D);
  2164. // Record the offset for this declaration
  2165. SourceLocation Loc = D->getLocation();
  2166. unsigned Index = ID - FirstDeclID;
  2167. if (DeclOffsets.size() == Index)
  2168. DeclOffsets.emplace_back(Loc, Offset, DeclTypesBlockStartOffset);
  2169. else if (DeclOffsets.size() < Index) {
  2170. // FIXME: Can/should this happen?
  2171. DeclOffsets.resize(Index+1);
  2172. DeclOffsets[Index].setLocation(Loc);
  2173. DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);
  2174. } else {
  2175. llvm_unreachable("declarations should be emitted in ID order");
  2176. }
  2177. SourceManager &SM = Context.getSourceManager();
  2178. if (Loc.isValid() && SM.isLocalSourceLocation(Loc))
  2179. associateDeclWithFile(D, ID);
  2180. // Note declarations that should be deserialized eagerly so that we can add
  2181. // them to a record in the AST file later.
  2182. if (isRequiredDecl(D, Context, WritingModule))
  2183. EagerlyDeserializedDecls.push_back(ID);
  2184. }
  2185. void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {
  2186. // Switch case IDs are per function body.
  2187. Writer->ClearSwitchCaseIDs();
  2188. assert(FD->doesThisDeclarationHaveABody());
  2189. bool ModulesCodegen = false;
  2190. if (!FD->isDependentContext()) {
  2191. Optional<GVALinkage> Linkage;
  2192. if (Writer->WritingModule &&
  2193. Writer->WritingModule->Kind == Module::ModuleInterfaceUnit) {
  2194. // When building a C++ Modules TS module interface unit, a strong
  2195. // definition in the module interface is provided by the compilation of
  2196. // that module interface unit, not by its users. (Inline functions are
  2197. // still emitted in module users.)
  2198. Linkage = Writer->Context->GetGVALinkageForFunction(FD);
  2199. ModulesCodegen = *Linkage == GVA_StrongExternal;
  2200. }
  2201. if (Writer->Context->getLangOpts().ModulesCodegen ||
  2202. (FD->hasAttr<DLLExportAttr>() &&
  2203. Writer->Context->getLangOpts().BuildingPCHWithObjectFile)) {
  2204. // Under -fmodules-codegen, codegen is performed for all non-internal,
  2205. // non-always_inline functions, unless they are available elsewhere.
  2206. if (!FD->hasAttr<AlwaysInlineAttr>()) {
  2207. if (!Linkage)
  2208. Linkage = Writer->Context->GetGVALinkageForFunction(FD);
  2209. ModulesCodegen =
  2210. *Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally;
  2211. }
  2212. }
  2213. }
  2214. Record->push_back(ModulesCodegen);
  2215. if (ModulesCodegen)
  2216. Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(FD));
  2217. if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
  2218. Record->push_back(CD->getNumCtorInitializers());
  2219. if (CD->getNumCtorInitializers())
  2220. AddCXXCtorInitializers(
  2221. llvm::makeArrayRef(CD->init_begin(), CD->init_end()));
  2222. }
  2223. AddStmt(FD->getBody());
  2224. }