ASTWriterDecl.cpp 100 KB

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