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
13 #include "ASTCommon.h"
14 #include "clang/AST/Attr.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclTemplate.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/OpenMPClause.h"
20 #include "clang/AST/PrettyDeclStackTrace.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Serialization/ASTReader.h"
23 #include "clang/Serialization/ASTRecordWriter.h"
24 #include "llvm/Bitstream/BitstreamWriter.h"
25 #include "llvm/Support/ErrorHandling.h"
26 using namespace clang;
27 using namespace serialization;
28
29 //===----------------------------------------------------------------------===//
30 // Utility functions
31 //===----------------------------------------------------------------------===//
32
33 namespace {
34
35 // Helper function that returns true if the decl passed in the argument is
36 // a defintion in dependent contxt.
isDefinitionInDependentContext(DT * D)37 template <typename DT> bool isDefinitionInDependentContext(DT *D) {
38 return D->isDependentContext() && D->isThisDeclarationADefinition();
39 }
40
41 } // namespace
42
43 //===----------------------------------------------------------------------===//
44 // Declaration serialization
45 //===----------------------------------------------------------------------===//
46
47 namespace clang {
48 class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {
49 ASTWriter &Writer;
50 ASTRecordWriter Record;
51
52 serialization::DeclCode Code;
53 unsigned AbbrevToUse;
54
55 bool GeneratingReducedBMI = false;
56
57 public:
ASTDeclWriter(ASTWriter & Writer,ASTContext & Context,ASTWriter::RecordDataImpl & Record,bool GeneratingReducedBMI)58 ASTDeclWriter(ASTWriter &Writer, ASTContext &Context,
59 ASTWriter::RecordDataImpl &Record, bool GeneratingReducedBMI)
60 : Writer(Writer), Record(Context, Writer, Record),
61 Code((serialization::DeclCode)0), AbbrevToUse(0),
62 GeneratingReducedBMI(GeneratingReducedBMI) {}
63
Emit(Decl * D)64 uint64_t Emit(Decl *D) {
65 if (!Code)
66 llvm::report_fatal_error(StringRef("unexpected declaration kind '") +
67 D->getDeclKindName() + "'");
68 return Record.Emit(Code, AbbrevToUse);
69 }
70
71 void Visit(Decl *D);
72
73 void VisitDecl(Decl *D);
74 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
75 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
76 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
77 void VisitNamedDecl(NamedDecl *D);
78 void VisitLabelDecl(LabelDecl *LD);
79 void VisitNamespaceDecl(NamespaceDecl *D);
80 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
81 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
82 void VisitTypeDecl(TypeDecl *D);
83 void VisitTypedefNameDecl(TypedefNameDecl *D);
84 void VisitTypedefDecl(TypedefDecl *D);
85 void VisitTypeAliasDecl(TypeAliasDecl *D);
86 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
87 void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);
88 void VisitTagDecl(TagDecl *D);
89 void VisitEnumDecl(EnumDecl *D);
90 void VisitRecordDecl(RecordDecl *D);
91 void VisitCXXRecordDecl(CXXRecordDecl *D);
92 void VisitClassTemplateSpecializationDecl(
93 ClassTemplateSpecializationDecl *D);
94 void VisitClassTemplatePartialSpecializationDecl(
95 ClassTemplatePartialSpecializationDecl *D);
96 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
97 void VisitVarTemplatePartialSpecializationDecl(
98 VarTemplatePartialSpecializationDecl *D);
99 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
100 void VisitValueDecl(ValueDecl *D);
101 void VisitEnumConstantDecl(EnumConstantDecl *D);
102 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
103 void VisitDeclaratorDecl(DeclaratorDecl *D);
104 void VisitFunctionDecl(FunctionDecl *D);
105 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D);
106 void VisitCXXMethodDecl(CXXMethodDecl *D);
107 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
108 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
109 void VisitCXXConversionDecl(CXXConversionDecl *D);
110 void VisitFieldDecl(FieldDecl *D);
111 void VisitMSPropertyDecl(MSPropertyDecl *D);
112 void VisitMSGuidDecl(MSGuidDecl *D);
113 void VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl *D);
114 void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);
115 void VisitIndirectFieldDecl(IndirectFieldDecl *D);
116 void VisitVarDecl(VarDecl *D);
117 void VisitImplicitParamDecl(ImplicitParamDecl *D);
118 void VisitParmVarDecl(ParmVarDecl *D);
119 void VisitDecompositionDecl(DecompositionDecl *D);
120 void VisitBindingDecl(BindingDecl *D);
121 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
122 void VisitTemplateDecl(TemplateDecl *D);
123 void VisitConceptDecl(ConceptDecl *D);
124 void VisitImplicitConceptSpecializationDecl(
125 ImplicitConceptSpecializationDecl *D);
126 void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);
127 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
128 void VisitClassTemplateDecl(ClassTemplateDecl *D);
129 void VisitVarTemplateDecl(VarTemplateDecl *D);
130 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
131 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
132 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
133 void VisitUsingDecl(UsingDecl *D);
134 void VisitUsingEnumDecl(UsingEnumDecl *D);
135 void VisitUsingPackDecl(UsingPackDecl *D);
136 void VisitUsingShadowDecl(UsingShadowDecl *D);
137 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
138 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
139 void VisitExportDecl(ExportDecl *D);
140 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
141 void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);
142 void VisitImportDecl(ImportDecl *D);
143 void VisitAccessSpecDecl(AccessSpecDecl *D);
144 void VisitFriendDecl(FriendDecl *D);
145 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
146 void VisitStaticAssertDecl(StaticAssertDecl *D);
147 void VisitBlockDecl(BlockDecl *D);
148 void VisitOutlinedFunctionDecl(OutlinedFunctionDecl *D);
149 void VisitCapturedDecl(CapturedDecl *D);
150 void VisitEmptyDecl(EmptyDecl *D);
151 void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
152 void VisitDeclContext(DeclContext *DC);
153 template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
154 void VisitHLSLBufferDecl(HLSLBufferDecl *D);
155
156 // FIXME: Put in the same order is DeclNodes.td?
157 void VisitObjCMethodDecl(ObjCMethodDecl *D);
158 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
159 void VisitObjCContainerDecl(ObjCContainerDecl *D);
160 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
161 void VisitObjCIvarDecl(ObjCIvarDecl *D);
162 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
163 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
164 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
165 void VisitObjCImplDecl(ObjCImplDecl *D);
166 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
167 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
168 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
169 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
170 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
171 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
172 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
173 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
174 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
175 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
176 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
177
178 void VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D);
179 void VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D);
180
181 /// Add an Objective-C type parameter list to the given record.
AddObjCTypeParamList(ObjCTypeParamList * typeParams)182 void AddObjCTypeParamList(ObjCTypeParamList *typeParams) {
183 // Empty type parameter list.
184 if (!typeParams) {
185 Record.push_back(0);
186 return;
187 }
188
189 Record.push_back(typeParams->size());
190 for (auto *typeParam : *typeParams) {
191 Record.AddDeclRef(typeParam);
192 }
193 Record.AddSourceLocation(typeParams->getLAngleLoc());
194 Record.AddSourceLocation(typeParams->getRAngleLoc());
195 }
196
197 /// Collect the first declaration from each module file that provides a
198 /// declaration of D.
CollectFirstDeclFromEachModule(const Decl * D,bool IncludeLocal,llvm::MapVector<ModuleFile *,const Decl * > & Firsts)199 void CollectFirstDeclFromEachModule(
200 const Decl *D, bool IncludeLocal,
201 llvm::MapVector<ModuleFile *, const Decl *> &Firsts) {
202
203 // FIXME: We can skip entries that we know are implied by others.
204 for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) {
205 if (R->isFromASTFile())
206 Firsts[Writer.Chain->getOwningModuleFile(R)] = R;
207 else if (IncludeLocal)
208 Firsts[nullptr] = R;
209 }
210 }
211
212 /// Add to the record the first declaration from each module file that
213 /// provides a declaration of D. The intent is to provide a sufficient
214 /// set such that reloading this set will load all current redeclarations.
AddFirstDeclFromEachModule(const Decl * D,bool IncludeLocal)215 void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) {
216 llvm::MapVector<ModuleFile *, const Decl *> Firsts;
217 CollectFirstDeclFromEachModule(D, IncludeLocal, Firsts);
218
219 for (const auto &F : Firsts)
220 Record.AddDeclRef(F.second);
221 }
222
shouldSkipWritingSpecializations(T * Spec)223 template <typename T> bool shouldSkipWritingSpecializations(T *Spec) {
224 // Now we will only avoid writing specializations if we're generating
225 // reduced BMI.
226 if (!GeneratingReducedBMI)
227 return false;
228
229 assert((isa<FunctionDecl, ClassTemplateSpecializationDecl,
230 VarTemplateSpecializationDecl>(Spec)));
231
232 ArrayRef<TemplateArgument> Args;
233 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Spec))
234 Args = CTSD->getTemplateArgs().asArray();
235 else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Spec))
236 Args = VTSD->getTemplateArgs().asArray();
237 else
238 Args = cast<FunctionDecl>(Spec)
239 ->getTemplateSpecializationArgs()
240 ->asArray();
241
242 // If there is any template argument is TULocal, we can avoid writing the
243 // specialization since the consumers of reduced BMI won't get the
244 // specialization anyway.
245 for (const TemplateArgument &TA : Args) {
246 switch (TA.getKind()) {
247 case TemplateArgument::Type: {
248 Linkage L = TA.getAsType()->getLinkage();
249 if (!isExternallyVisible(L))
250 return true;
251 break;
252 }
253 case TemplateArgument::Declaration:
254 if (!TA.getAsDecl()->isExternallyVisible())
255 return true;
256 break;
257 default:
258 break;
259 }
260 }
261
262 return false;
263 }
264
265 /// Add to the record the first template specialization from each module
266 /// file that provides a declaration of D. We store the DeclId and an
267 /// ODRHash of the template arguments of D which should provide enough
268 /// information to load D only if the template instantiator needs it.
AddFirstSpecializationDeclFromEachModule(const Decl * D,llvm::SmallVectorImpl<const Decl * > & SpecsInMap,llvm::SmallVectorImpl<const Decl * > & PartialSpecsInMap)269 void AddFirstSpecializationDeclFromEachModule(
270 const Decl *D, llvm::SmallVectorImpl<const Decl *> &SpecsInMap,
271 llvm::SmallVectorImpl<const Decl *> &PartialSpecsInMap) {
272 assert((isa<ClassTemplateSpecializationDecl>(D) ||
273 isa<VarTemplateSpecializationDecl>(D) || isa<FunctionDecl>(D)) &&
274 "Must not be called with other decls");
275 llvm::MapVector<ModuleFile *, const Decl *> Firsts;
276 CollectFirstDeclFromEachModule(D, /*IncludeLocal*/ true, Firsts);
277
278 for (const auto &F : Firsts) {
279 if (shouldSkipWritingSpecializations(F.second))
280 continue;
281
282 if (isa<ClassTemplatePartialSpecializationDecl,
283 VarTemplatePartialSpecializationDecl>(F.second))
284 PartialSpecsInMap.push_back(F.second);
285 else
286 SpecsInMap.push_back(F.second);
287 }
288 }
289
290 /// Get the specialization decl from an entry in the specialization list.
291 template <typename EntryType>
292 typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
getSpecializationDecl(EntryType & T)293 getSpecializationDecl(EntryType &T) {
294 return RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::getDecl(&T);
295 }
296
297 /// Get the list of partial specializations from a template's common ptr.
298 template<typename T>
getPartialSpecializations(T * Common)299 decltype(T::PartialSpecializations) &getPartialSpecializations(T *Common) {
300 return Common->PartialSpecializations;
301 }
302 MutableArrayRef<FunctionTemplateSpecializationInfo>
getPartialSpecializations(FunctionTemplateDecl::Common *)303 getPartialSpecializations(FunctionTemplateDecl::Common *) {
304 return {};
305 }
306
307 template<typename DeclTy>
AddTemplateSpecializations(DeclTy * D)308 void AddTemplateSpecializations(DeclTy *D) {
309 auto *Common = D->getCommonPtr();
310
311 // If we have any lazy specializations, and the external AST source is
312 // our chained AST reader, we can just write out the DeclIDs. Otherwise,
313 // we need to resolve them to actual declarations.
314 if (Writer.Chain != Record.getASTContext().getExternalSource() &&
315 Writer.Chain && Writer.Chain->haveUnloadedSpecializations(D)) {
316 D->LoadLazySpecializations();
317 assert(!Writer.Chain->haveUnloadedSpecializations(D));
318 }
319
320 // AddFirstSpecializationDeclFromEachModule might trigger deserialization,
321 // invalidating *Specializations iterators.
322 llvm::SmallVector<const Decl *, 16> AllSpecs;
323 for (auto &Entry : Common->Specializations)
324 AllSpecs.push_back(getSpecializationDecl(Entry));
325 for (auto &Entry : getPartialSpecializations(Common))
326 AllSpecs.push_back(getSpecializationDecl(Entry));
327
328 llvm::SmallVector<const Decl *, 16> Specs;
329 llvm::SmallVector<const Decl *, 16> PartialSpecs;
330 for (auto *D : AllSpecs) {
331 assert(D->isCanonicalDecl() && "non-canonical decl in set");
332 AddFirstSpecializationDeclFromEachModule(D, Specs, PartialSpecs);
333 }
334
335 Record.AddOffset(Writer.WriteSpecializationInfoLookupTable(
336 D, Specs, /*IsPartial=*/false));
337
338 // Function Template Decl doesn't have partial decls.
339 if (isa<FunctionTemplateDecl>(D)) {
340 assert(PartialSpecs.empty());
341 return;
342 }
343
344 Record.AddOffset(Writer.WriteSpecializationInfoLookupTable(
345 D, PartialSpecs, /*IsPartial=*/true));
346 }
347
348 /// Ensure that this template specialization is associated with the specified
349 /// template on reload.
RegisterTemplateSpecialization(const Decl * Template,const Decl * Specialization)350 void RegisterTemplateSpecialization(const Decl *Template,
351 const Decl *Specialization) {
352 Template = Template->getCanonicalDecl();
353
354 // If the canonical template is local, we'll write out this specialization
355 // when we emit it.
356 // FIXME: We can do the same thing if there is any local declaration of
357 // the template, to avoid emitting an update record.
358 if (!Template->isFromASTFile())
359 return;
360
361 // We only need to associate the first local declaration of the
362 // specialization. The other declarations will get pulled in by it.
363 if (Writer.getFirstLocalDecl(Specialization) != Specialization)
364 return;
365
366 if (isa<ClassTemplatePartialSpecializationDecl,
367 VarTemplatePartialSpecializationDecl>(Specialization))
368 Writer.PartialSpecializationsUpdates[cast<NamedDecl>(Template)]
369 .push_back(cast<NamedDecl>(Specialization));
370 else
371 Writer.SpecializationsUpdates[cast<NamedDecl>(Template)].push_back(
372 cast<NamedDecl>(Specialization));
373 }
374 };
375 }
376
377 // When building a C++20 module interface unit or a partition unit, a
378 // strong definition in the module interface is provided by the
379 // compilation of that unit, not by its users. (Inline variables are still
380 // emitted in module users.)
shouldVarGenerateHereOnly(const VarDecl * VD)381 static bool shouldVarGenerateHereOnly(const VarDecl *VD) {
382 if (VD->getStorageDuration() != SD_Static)
383 return false;
384
385 if (VD->getDescribedVarTemplate())
386 return false;
387
388 Module *M = VD->getOwningModule();
389 if (!M)
390 return false;
391
392 M = M->getTopLevelModule();
393 ASTContext &Ctx = VD->getASTContext();
394 if (!M->isInterfaceOrPartition() &&
395 (!VD->hasAttr<DLLExportAttr>() ||
396 !Ctx.getLangOpts().BuildingPCHWithObjectFile))
397 return false;
398
399 return Ctx.GetGVALinkageForVariable(VD) >= GVA_StrongExternal;
400 }
401
shouldFunctionGenerateHereOnly(const FunctionDecl * FD)402 static bool shouldFunctionGenerateHereOnly(const FunctionDecl *FD) {
403 if (FD->isDependentContext())
404 return false;
405
406 ASTContext &Ctx = FD->getASTContext();
407 auto Linkage = Ctx.GetGVALinkageForFunction(FD);
408 if (Ctx.getLangOpts().ModulesCodegen ||
409 (FD->hasAttr<DLLExportAttr>() &&
410 Ctx.getLangOpts().BuildingPCHWithObjectFile))
411 // Under -fmodules-codegen, codegen is performed for all non-internal,
412 // non-always_inline functions, unless they are available elsewhere.
413 if (!FD->hasAttr<AlwaysInlineAttr>() && Linkage != GVA_Internal &&
414 Linkage != GVA_AvailableExternally)
415 return true;
416
417 Module *M = FD->getOwningModule();
418 if (!M)
419 return false;
420
421 M = M->getTopLevelModule();
422 if (M->isInterfaceOrPartition())
423 if (Linkage >= GVA_StrongExternal)
424 return true;
425
426 return false;
427 }
428
CanElideDeclDef(const Decl * D)429 bool clang::CanElideDeclDef(const Decl *D) {
430 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
431 if (FD->isInlined() || FD->isConstexpr() || FD->isConsteval())
432 return false;
433
434 // If the function should be generated somewhere else, we shouldn't elide
435 // it.
436 if (!shouldFunctionGenerateHereOnly(FD))
437 return false;
438 }
439
440 if (auto *VD = dyn_cast<VarDecl>(D)) {
441 if (VD->getDeclContext()->isDependentContext())
442 return false;
443
444 // Constant initialized variable may not affect the ABI, but they
445 // may be used in constant evaluation in the frontend, so we have
446 // to remain them.
447 if (VD->hasConstantInitialization() || VD->isConstexpr())
448 return false;
449
450 // If the variable should be generated somewhere else, we shouldn't elide
451 // it.
452 if (!shouldVarGenerateHereOnly(VD))
453 return false;
454 }
455
456 return true;
457 }
458
Visit(Decl * D)459 void ASTDeclWriter::Visit(Decl *D) {
460 DeclVisitor<ASTDeclWriter>::Visit(D);
461
462 // Source locations require array (variable-length) abbreviations. The
463 // abbreviation infrastructure requires that arrays are encoded last, so
464 // we handle it here in the case of those classes derived from DeclaratorDecl
465 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
466 if (auto *TInfo = DD->getTypeSourceInfo())
467 Record.AddTypeLoc(TInfo->getTypeLoc());
468 }
469
470 // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
471 // have been written. We want it last because we will not read it back when
472 // retrieving it from the AST, we'll just lazily set the offset.
473 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
474 if (!GeneratingReducedBMI || !CanElideDeclDef(FD)) {
475 Record.push_back(FD->doesThisDeclarationHaveABody());
476 if (FD->doesThisDeclarationHaveABody())
477 Record.AddFunctionDefinition(FD);
478 } else
479 Record.push_back(0);
480 }
481
482 // Similar to FunctionDecls, handle VarDecl's initializer here and write it
483 // after all other Stmts/Exprs. We will not read the initializer until after
484 // we have finished recursive deserialization, because it can recursively
485 // refer back to the variable.
486 if (auto *VD = dyn_cast<VarDecl>(D)) {
487 if (!GeneratingReducedBMI || !CanElideDeclDef(VD))
488 Record.AddVarDeclInit(VD);
489 else
490 Record.push_back(0);
491 }
492
493 // And similarly for FieldDecls. We already serialized whether there is a
494 // default member initializer.
495 if (auto *FD = dyn_cast<FieldDecl>(D)) {
496 if (FD->hasInClassInitializer()) {
497 if (Expr *Init = FD->getInClassInitializer()) {
498 Record.push_back(1);
499 Record.AddStmt(Init);
500 } else {
501 Record.push_back(0);
502 // Initializer has not been instantiated yet.
503 }
504 }
505 }
506
507 // If this declaration is also a DeclContext, write blocks for the
508 // declarations that lexically stored inside its context and those
509 // declarations that are visible from its context.
510 if (auto *DC = dyn_cast<DeclContext>(D))
511 VisitDeclContext(DC);
512 }
513
VisitDecl(Decl * D)514 void ASTDeclWriter::VisitDecl(Decl *D) {
515 BitsPacker DeclBits;
516
517 // The order matters here. It will be better to put the bit with higher
518 // probability to be 0 in the end of the bits.
519 //
520 // Since we're using VBR6 format to store it.
521 // It will be pretty effient if all the higher bits are 0.
522 // For example, if we need to pack 8 bits into a value and the stored value
523 // is 0xf0, the actual stored value will be 0b000111'110000, which takes 12
524 // bits actually. However, if we changed the order to be 0x0f, then we can
525 // store it as 0b001111, which takes 6 bits only now.
526 DeclBits.addBits((uint64_t)D->getModuleOwnershipKind(), /*BitWidth=*/3);
527 DeclBits.addBit(D->isReferenced());
528 DeclBits.addBit(D->isUsed(false));
529 DeclBits.addBits(D->getAccess(), /*BitWidth=*/2);
530 DeclBits.addBit(D->isImplicit());
531 DeclBits.addBit(D->getDeclContext() != D->getLexicalDeclContext());
532 DeclBits.addBit(D->hasAttrs());
533 DeclBits.addBit(D->isTopLevelDeclInObjCContainer());
534 DeclBits.addBit(D->isInvalidDecl());
535 Record.push_back(DeclBits);
536
537 Record.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()));
538 if (D->getDeclContext() != D->getLexicalDeclContext())
539 Record.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()));
540
541 if (D->hasAttrs())
542 Record.AddAttributes(D->getAttrs());
543
544 Record.push_back(Writer.getSubmoduleID(D->getOwningModule()));
545
546 // If this declaration injected a name into a context different from its
547 // lexical context, and that context is an imported namespace, we need to
548 // update its visible declarations to include this name.
549 //
550 // This happens when we instantiate a class with a friend declaration or a
551 // function with a local extern declaration, for instance.
552 //
553 // FIXME: Can we handle this in AddedVisibleDecl instead?
554 if (D->isOutOfLine()) {
555 auto *DC = D->getDeclContext();
556 while (auto *NS = dyn_cast<NamespaceDecl>(DC->getRedeclContext())) {
557 if (!NS->isFromASTFile())
558 break;
559 Writer.UpdatedDeclContexts.insert(NS->getPrimaryContext());
560 if (!NS->isInlineNamespace())
561 break;
562 DC = NS->getParent();
563 }
564 }
565 }
566
VisitPragmaCommentDecl(PragmaCommentDecl * D)567 void ASTDeclWriter::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
568 StringRef Arg = D->getArg();
569 Record.push_back(Arg.size());
570 VisitDecl(D);
571 Record.AddSourceLocation(D->getBeginLoc());
572 Record.push_back(D->getCommentKind());
573 Record.AddString(Arg);
574 Code = serialization::DECL_PRAGMA_COMMENT;
575 }
576
VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl * D)577 void ASTDeclWriter::VisitPragmaDetectMismatchDecl(
578 PragmaDetectMismatchDecl *D) {
579 StringRef Name = D->getName();
580 StringRef Value = D->getValue();
581 Record.push_back(Name.size() + 1 + Value.size());
582 VisitDecl(D);
583 Record.AddSourceLocation(D->getBeginLoc());
584 Record.AddString(Name);
585 Record.AddString(Value);
586 Code = serialization::DECL_PRAGMA_DETECT_MISMATCH;
587 }
588
VisitTranslationUnitDecl(TranslationUnitDecl * D)589 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
590 llvm_unreachable("Translation units aren't directly serialized");
591 }
592
VisitNamedDecl(NamedDecl * D)593 void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {
594 VisitDecl(D);
595 Record.AddDeclarationName(D->getDeclName());
596 Record.push_back(needsAnonymousDeclarationNumber(D)
597 ? Writer.getAnonymousDeclarationNumber(D)
598 : 0);
599 }
600
VisitTypeDecl(TypeDecl * D)601 void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {
602 VisitNamedDecl(D);
603 Record.AddSourceLocation(D->getBeginLoc());
604 Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
605 }
606
VisitTypedefNameDecl(TypedefNameDecl * D)607 void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {
608 VisitRedeclarable(D);
609 VisitTypeDecl(D);
610 Record.AddTypeSourceInfo(D->getTypeSourceInfo());
611 Record.push_back(D->isModed());
612 if (D->isModed())
613 Record.AddTypeRef(D->getUnderlyingType());
614 Record.AddDeclRef(D->getAnonDeclWithTypedefName(false));
615 }
616
VisitTypedefDecl(TypedefDecl * D)617 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
618 VisitTypedefNameDecl(D);
619 if (D->getDeclContext() == D->getLexicalDeclContext() &&
620 !D->hasAttrs() &&
621 !D->isImplicit() &&
622 D->getFirstDecl() == D->getMostRecentDecl() &&
623 !D->isInvalidDecl() &&
624 !D->isTopLevelDeclInObjCContainer() &&
625 !D->isModulePrivate() &&
626 !needsAnonymousDeclarationNumber(D) &&
627 D->getDeclName().getNameKind() == DeclarationName::Identifier)
628 AbbrevToUse = Writer.getDeclTypedefAbbrev();
629
630 Code = serialization::DECL_TYPEDEF;
631 }
632
VisitTypeAliasDecl(TypeAliasDecl * D)633 void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {
634 VisitTypedefNameDecl(D);
635 Record.AddDeclRef(D->getDescribedAliasTemplate());
636 Code = serialization::DECL_TYPEALIAS;
637 }
638
VisitTagDecl(TagDecl * D)639 void ASTDeclWriter::VisitTagDecl(TagDecl *D) {
640 static_assert(DeclContext::NumTagDeclBits == 23,
641 "You need to update the serializer after you change the "
642 "TagDeclBits");
643
644 VisitRedeclarable(D);
645 VisitTypeDecl(D);
646 Record.push_back(D->getIdentifierNamespace());
647
648 BitsPacker TagDeclBits;
649 TagDeclBits.addBits(llvm::to_underlying(D->getTagKind()), /*BitWidth=*/3);
650 TagDeclBits.addBit(!isa<CXXRecordDecl>(D) ? D->isCompleteDefinition() : 0);
651 TagDeclBits.addBit(D->isEmbeddedInDeclarator());
652 TagDeclBits.addBit(D->isFreeStanding());
653 TagDeclBits.addBit(D->isCompleteDefinitionRequired());
654 TagDeclBits.addBits(
655 D->hasExtInfo() ? 1 : (D->getTypedefNameForAnonDecl() ? 2 : 0),
656 /*BitWidth=*/2);
657 Record.push_back(TagDeclBits);
658
659 Record.AddSourceRange(D->getBraceRange());
660
661 if (D->hasExtInfo()) {
662 Record.AddQualifierInfo(*D->getExtInfo());
663 } else if (auto *TD = D->getTypedefNameForAnonDecl()) {
664 Record.AddDeclRef(TD);
665 Record.AddIdentifierRef(TD->getDeclName().getAsIdentifierInfo());
666 }
667 }
668
VisitEnumDecl(EnumDecl * D)669 void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {
670 static_assert(DeclContext::NumEnumDeclBits == 43,
671 "You need to update the serializer after you change the "
672 "EnumDeclBits");
673
674 VisitTagDecl(D);
675 Record.AddTypeSourceInfo(D->getIntegerTypeSourceInfo());
676 if (!D->getIntegerTypeSourceInfo())
677 Record.AddTypeRef(D->getIntegerType());
678 Record.AddTypeRef(D->getPromotionType());
679
680 BitsPacker EnumDeclBits;
681 EnumDeclBits.addBits(D->getNumPositiveBits(), /*BitWidth=*/8);
682 EnumDeclBits.addBits(D->getNumNegativeBits(), /*BitWidth=*/8);
683 EnumDeclBits.addBit(D->isScoped());
684 EnumDeclBits.addBit(D->isScopedUsingClassTag());
685 EnumDeclBits.addBit(D->isFixed());
686 Record.push_back(EnumDeclBits);
687
688 Record.push_back(D->getODRHash());
689
690 if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {
691 Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
692 Record.push_back(MemberInfo->getTemplateSpecializationKind());
693 Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
694 } else {
695 Record.AddDeclRef(nullptr);
696 }
697
698 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
699 !D->isInvalidDecl() && !D->isImplicit() && !D->hasExtInfo() &&
700 !D->getTypedefNameForAnonDecl() &&
701 D->getFirstDecl() == D->getMostRecentDecl() &&
702 !D->isTopLevelDeclInObjCContainer() &&
703 !CXXRecordDecl::classofKind(D->getKind()) &&
704 !D->getIntegerTypeSourceInfo() && !D->getMemberSpecializationInfo() &&
705 !needsAnonymousDeclarationNumber(D) &&
706 D->getDeclName().getNameKind() == DeclarationName::Identifier)
707 AbbrevToUse = Writer.getDeclEnumAbbrev();
708
709 Code = serialization::DECL_ENUM;
710 }
711
VisitRecordDecl(RecordDecl * D)712 void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {
713 static_assert(DeclContext::NumRecordDeclBits == 64,
714 "You need to update the serializer after you change the "
715 "RecordDeclBits");
716
717 VisitTagDecl(D);
718
719 BitsPacker RecordDeclBits;
720 RecordDeclBits.addBit(D->hasFlexibleArrayMember());
721 RecordDeclBits.addBit(D->isAnonymousStructOrUnion());
722 RecordDeclBits.addBit(D->hasObjectMember());
723 RecordDeclBits.addBit(D->hasVolatileMember());
724 RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDefaultInitialize());
725 RecordDeclBits.addBit(D->isNonTrivialToPrimitiveCopy());
726 RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDestroy());
727 RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDefaultInitializeCUnion());
728 RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDestructCUnion());
729 RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveCopyCUnion());
730 RecordDeclBits.addBit(D->hasUninitializedExplicitInitFields());
731 RecordDeclBits.addBit(D->isParamDestroyedInCallee());
732 RecordDeclBits.addBits(llvm::to_underlying(D->getArgPassingRestrictions()), 2);
733 Record.push_back(RecordDeclBits);
734
735 // Only compute this for C/Objective-C, in C++ this is computed as part
736 // of CXXRecordDecl.
737 if (!isa<CXXRecordDecl>(D))
738 Record.push_back(D->getODRHash());
739
740 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
741 !D->isImplicit() && !D->isInvalidDecl() && !D->hasExtInfo() &&
742 !D->getTypedefNameForAnonDecl() &&
743 D->getFirstDecl() == D->getMostRecentDecl() &&
744 !D->isTopLevelDeclInObjCContainer() &&
745 !CXXRecordDecl::classofKind(D->getKind()) &&
746 !needsAnonymousDeclarationNumber(D) &&
747 D->getDeclName().getNameKind() == DeclarationName::Identifier)
748 AbbrevToUse = Writer.getDeclRecordAbbrev();
749
750 Code = serialization::DECL_RECORD;
751 }
752
VisitValueDecl(ValueDecl * D)753 void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {
754 VisitNamedDecl(D);
755 Record.AddTypeRef(D->getType());
756 }
757
VisitEnumConstantDecl(EnumConstantDecl * D)758 void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
759 VisitValueDecl(D);
760 Record.push_back(D->getInitExpr()? 1 : 0);
761 if (D->getInitExpr())
762 Record.AddStmt(D->getInitExpr());
763 Record.AddAPSInt(D->getInitVal());
764
765 Code = serialization::DECL_ENUM_CONSTANT;
766 }
767
VisitDeclaratorDecl(DeclaratorDecl * D)768 void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
769 VisitValueDecl(D);
770 Record.AddSourceLocation(D->getInnerLocStart());
771 Record.push_back(D->hasExtInfo());
772 if (D->hasExtInfo()) {
773 DeclaratorDecl::ExtInfo *Info = D->getExtInfo();
774 Record.AddQualifierInfo(*Info);
775 Record.AddStmt(
776 const_cast<Expr *>(Info->TrailingRequiresClause.ConstraintExpr));
777 Record.writeUnsignedOrNone(Info->TrailingRequiresClause.ArgPackSubstIndex);
778 }
779 // The location information is deferred until the end of the record.
780 Record.AddTypeRef(D->getTypeSourceInfo() ? D->getTypeSourceInfo()->getType()
781 : QualType());
782 }
783
VisitFunctionDecl(FunctionDecl * D)784 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
785 static_assert(DeclContext::NumFunctionDeclBits == 45,
786 "You need to update the serializer after you change the "
787 "FunctionDeclBits");
788
789 VisitRedeclarable(D);
790
791 Record.push_back(D->getTemplatedKind());
792 switch (D->getTemplatedKind()) {
793 case FunctionDecl::TK_NonTemplate:
794 break;
795 case FunctionDecl::TK_DependentNonTemplate:
796 Record.AddDeclRef(D->getInstantiatedFromDecl());
797 break;
798 case FunctionDecl::TK_FunctionTemplate:
799 Record.AddDeclRef(D->getDescribedFunctionTemplate());
800 break;
801 case FunctionDecl::TK_MemberSpecialization: {
802 MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
803 Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
804 Record.push_back(MemberInfo->getTemplateSpecializationKind());
805 Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
806 break;
807 }
808 case FunctionDecl::TK_FunctionTemplateSpecialization: {
809 FunctionTemplateSpecializationInfo *
810 FTSInfo = D->getTemplateSpecializationInfo();
811
812 RegisterTemplateSpecialization(FTSInfo->getTemplate(), D);
813
814 Record.AddDeclRef(FTSInfo->getTemplate());
815 Record.push_back(FTSInfo->getTemplateSpecializationKind());
816
817 // Template arguments.
818 Record.AddTemplateArgumentList(FTSInfo->TemplateArguments);
819
820 // Template args as written.
821 Record.push_back(FTSInfo->TemplateArgumentsAsWritten != nullptr);
822 if (FTSInfo->TemplateArgumentsAsWritten)
823 Record.AddASTTemplateArgumentListInfo(
824 FTSInfo->TemplateArgumentsAsWritten);
825
826 Record.AddSourceLocation(FTSInfo->getPointOfInstantiation());
827
828 if (MemberSpecializationInfo *MemberInfo =
829 FTSInfo->getMemberSpecializationInfo()) {
830 Record.push_back(1);
831 Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
832 Record.push_back(MemberInfo->getTemplateSpecializationKind());
833 Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
834 } else {
835 Record.push_back(0);
836 }
837
838 if (D->isCanonicalDecl()) {
839 // Write the template that contains the specializations set. We will
840 // add a FunctionTemplateSpecializationInfo to it when reading.
841 Record.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl());
842 }
843 break;
844 }
845 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
846 DependentFunctionTemplateSpecializationInfo *
847 DFTSInfo = D->getDependentSpecializationInfo();
848
849 // Candidates.
850 Record.push_back(DFTSInfo->getCandidates().size());
851 for (FunctionTemplateDecl *FTD : DFTSInfo->getCandidates())
852 Record.AddDeclRef(FTD);
853
854 // Templates args.
855 Record.push_back(DFTSInfo->TemplateArgumentsAsWritten != nullptr);
856 if (DFTSInfo->TemplateArgumentsAsWritten)
857 Record.AddASTTemplateArgumentListInfo(
858 DFTSInfo->TemplateArgumentsAsWritten);
859 break;
860 }
861 }
862
863 VisitDeclaratorDecl(D);
864 Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
865 Record.push_back(D->getIdentifierNamespace());
866
867 // The order matters here. It will be better to put the bit with higher
868 // probability to be 0 in the end of the bits. See the comments in VisitDecl
869 // for details.
870 BitsPacker FunctionDeclBits;
871 // FIXME: stable encoding
872 FunctionDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()), 3);
873 FunctionDeclBits.addBits((uint32_t)D->getStorageClass(), /*BitWidth=*/3);
874 FunctionDeclBits.addBit(D->isInlineSpecified());
875 FunctionDeclBits.addBit(D->isInlined());
876 FunctionDeclBits.addBit(D->hasSkippedBody());
877 FunctionDeclBits.addBit(D->isVirtualAsWritten());
878 FunctionDeclBits.addBit(D->isPureVirtual());
879 FunctionDeclBits.addBit(D->hasInheritedPrototype());
880 FunctionDeclBits.addBit(D->hasWrittenPrototype());
881 FunctionDeclBits.addBit(D->isDeletedBit());
882 FunctionDeclBits.addBit(D->isTrivial());
883 FunctionDeclBits.addBit(D->isTrivialForCall());
884 FunctionDeclBits.addBit(D->isDefaulted());
885 FunctionDeclBits.addBit(D->isExplicitlyDefaulted());
886 FunctionDeclBits.addBit(D->isIneligibleOrNotSelected());
887 FunctionDeclBits.addBits((uint64_t)(D->getConstexprKind()), /*BitWidth=*/2);
888 FunctionDeclBits.addBit(D->hasImplicitReturnZero());
889 FunctionDeclBits.addBit(D->isMultiVersion());
890 FunctionDeclBits.addBit(D->isLateTemplateParsed());
891 FunctionDeclBits.addBit(D->isInstantiatedFromMemberTemplate());
892 FunctionDeclBits.addBit(D->FriendConstraintRefersToEnclosingTemplate());
893 FunctionDeclBits.addBit(D->usesSEHTry());
894 FunctionDeclBits.addBit(D->isDestroyingOperatorDelete());
895 FunctionDeclBits.addBit(D->isTypeAwareOperatorNewOrDelete());
896 Record.push_back(FunctionDeclBits);
897
898 Record.AddSourceLocation(D->getEndLoc());
899 if (D->isExplicitlyDefaulted())
900 Record.AddSourceLocation(D->getDefaultLoc());
901
902 Record.push_back(D->getODRHash());
903
904 if (D->isDefaulted() || D->isDeletedAsWritten()) {
905 if (auto *FDI = D->getDefalutedOrDeletedInfo()) {
906 // Store both that there is an DefaultedOrDeletedInfo and whether it
907 // contains a DeletedMessage.
908 StringLiteral *DeletedMessage = FDI->getDeletedMessage();
909 Record.push_back(1 | (DeletedMessage ? 2 : 0));
910 if (DeletedMessage)
911 Record.AddStmt(DeletedMessage);
912
913 Record.push_back(FDI->getUnqualifiedLookups().size());
914 for (DeclAccessPair P : FDI->getUnqualifiedLookups()) {
915 Record.AddDeclRef(P.getDecl());
916 Record.push_back(P.getAccess());
917 }
918 } else {
919 Record.push_back(0);
920 }
921 }
922
923 if (D->getFriendObjectKind()) {
924 // For a friend function defined inline within a class template, we have to
925 // force the definition to be the one inside the definition of the template
926 // class. Remember this relation to deserialize them together.
927 if (auto *RD = dyn_cast<CXXRecordDecl>(D->getLexicalParent());
928 RD && isDefinitionInDependentContext(RD)) {
929 Writer.RelatedDeclsMap[Writer.GetDeclRef(RD)].push_back(
930 Writer.GetDeclRef(D));
931 }
932 }
933
934 Record.push_back(D->param_size());
935 for (auto *P : D->parameters())
936 Record.AddDeclRef(P);
937 Code = serialization::DECL_FUNCTION;
938 }
939
addExplicitSpecifier(ExplicitSpecifier ES,ASTRecordWriter & Record)940 static void addExplicitSpecifier(ExplicitSpecifier ES,
941 ASTRecordWriter &Record) {
942 uint64_t Kind = static_cast<uint64_t>(ES.getKind());
943 Kind = Kind << 1 | static_cast<bool>(ES.getExpr());
944 Record.push_back(Kind);
945 if (ES.getExpr()) {
946 Record.AddStmt(ES.getExpr());
947 }
948 }
949
VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl * D)950 void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
951 addExplicitSpecifier(D->getExplicitSpecifier(), Record);
952 Record.AddDeclRef(D->Ctor);
953 VisitFunctionDecl(D);
954 Record.push_back(static_cast<unsigned char>(D->getDeductionCandidateKind()));
955 Record.AddDeclRef(D->getSourceDeductionGuide());
956 Record.push_back(
957 static_cast<unsigned char>(D->getSourceDeductionGuideKind()));
958 Code = serialization::DECL_CXX_DEDUCTION_GUIDE;
959 }
960
VisitObjCMethodDecl(ObjCMethodDecl * D)961 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
962 static_assert(DeclContext::NumObjCMethodDeclBits == 37,
963 "You need to update the serializer after you change the "
964 "ObjCMethodDeclBits");
965
966 VisitNamedDecl(D);
967 // FIXME: convert to LazyStmtPtr?
968 // Unlike C/C++, method bodies will never be in header files.
969 bool HasBodyStuff = D->getBody() != nullptr;
970 Record.push_back(HasBodyStuff);
971 if (HasBodyStuff) {
972 Record.AddStmt(D->getBody());
973 }
974 Record.AddDeclRef(D->getSelfDecl());
975 Record.AddDeclRef(D->getCmdDecl());
976 Record.push_back(D->isInstanceMethod());
977 Record.push_back(D->isVariadic());
978 Record.push_back(D->isPropertyAccessor());
979 Record.push_back(D->isSynthesizedAccessorStub());
980 Record.push_back(D->isDefined());
981 Record.push_back(D->isOverriding());
982 Record.push_back(D->hasSkippedBody());
983
984 Record.push_back(D->isRedeclaration());
985 Record.push_back(D->hasRedeclaration());
986 if (D->hasRedeclaration()) {
987 assert(Record.getASTContext().getObjCMethodRedeclaration(D));
988 Record.AddDeclRef(Record.getASTContext().getObjCMethodRedeclaration(D));
989 }
990
991 // FIXME: stable encoding for @required/@optional
992 Record.push_back(llvm::to_underlying(D->getImplementationControl()));
993 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway/nullability
994 Record.push_back(D->getObjCDeclQualifier());
995 Record.push_back(D->hasRelatedResultType());
996 Record.AddTypeRef(D->getReturnType());
997 Record.AddTypeSourceInfo(D->getReturnTypeSourceInfo());
998 Record.AddSourceLocation(D->getEndLoc());
999 Record.push_back(D->param_size());
1000 for (const auto *P : D->parameters())
1001 Record.AddDeclRef(P);
1002
1003 Record.push_back(D->getSelLocsKind());
1004 unsigned NumStoredSelLocs = D->getNumStoredSelLocs();
1005 SourceLocation *SelLocs = D->getStoredSelLocs();
1006 Record.push_back(NumStoredSelLocs);
1007 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
1008 Record.AddSourceLocation(SelLocs[i]);
1009
1010 Code = serialization::DECL_OBJC_METHOD;
1011 }
1012
VisitObjCTypeParamDecl(ObjCTypeParamDecl * D)1013 void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
1014 VisitTypedefNameDecl(D);
1015 Record.push_back(D->Variance);
1016 Record.push_back(D->Index);
1017 Record.AddSourceLocation(D->VarianceLoc);
1018 Record.AddSourceLocation(D->ColonLoc);
1019
1020 Code = serialization::DECL_OBJC_TYPE_PARAM;
1021 }
1022
VisitObjCContainerDecl(ObjCContainerDecl * D)1023 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
1024 static_assert(DeclContext::NumObjCContainerDeclBits == 64,
1025 "You need to update the serializer after you change the "
1026 "ObjCContainerDeclBits");
1027
1028 VisitNamedDecl(D);
1029 Record.AddSourceLocation(D->getAtStartLoc());
1030 Record.AddSourceRange(D->getAtEndRange());
1031 // Abstract class (no need to define a stable serialization::DECL code).
1032 }
1033
VisitObjCInterfaceDecl(ObjCInterfaceDecl * D)1034 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1035 VisitRedeclarable(D);
1036 VisitObjCContainerDecl(D);
1037 Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
1038 AddObjCTypeParamList(D->TypeParamList);
1039
1040 Record.push_back(D->isThisDeclarationADefinition());
1041 if (D->isThisDeclarationADefinition()) {
1042 // Write the DefinitionData
1043 ObjCInterfaceDecl::DefinitionData &Data = D->data();
1044
1045 Record.AddTypeSourceInfo(D->getSuperClassTInfo());
1046 Record.AddSourceLocation(D->getEndOfDefinitionLoc());
1047 Record.push_back(Data.HasDesignatedInitializers);
1048 Record.push_back(D->getODRHash());
1049
1050 // Write out the protocols that are directly referenced by the @interface.
1051 Record.push_back(Data.ReferencedProtocols.size());
1052 for (const auto *P : D->protocols())
1053 Record.AddDeclRef(P);
1054 for (const auto &PL : D->protocol_locs())
1055 Record.AddSourceLocation(PL);
1056
1057 // Write out the protocols that are transitively referenced.
1058 Record.push_back(Data.AllReferencedProtocols.size());
1059 for (ObjCList<ObjCProtocolDecl>::iterator
1060 P = Data.AllReferencedProtocols.begin(),
1061 PEnd = Data.AllReferencedProtocols.end();
1062 P != PEnd; ++P)
1063 Record.AddDeclRef(*P);
1064
1065
1066 if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {
1067 // Ensure that we write out the set of categories for this class.
1068 Writer.ObjCClassesWithCategories.insert(D);
1069
1070 // Make sure that the categories get serialized.
1071 for (; Cat; Cat = Cat->getNextClassCategoryRaw())
1072 (void)Writer.GetDeclRef(Cat);
1073 }
1074 }
1075
1076 Code = serialization::DECL_OBJC_INTERFACE;
1077 }
1078
VisitObjCIvarDecl(ObjCIvarDecl * D)1079 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1080 VisitFieldDecl(D);
1081 // FIXME: stable encoding for @public/@private/@protected/@package
1082 Record.push_back(D->getAccessControl());
1083 Record.push_back(D->getSynthesize());
1084
1085 if (D->getDeclContext() == D->getLexicalDeclContext() &&
1086 !D->hasAttrs() &&
1087 !D->isImplicit() &&
1088 !D->isUsed(false) &&
1089 !D->isInvalidDecl() &&
1090 !D->isReferenced() &&
1091 !D->isModulePrivate() &&
1092 !D->getBitWidth() &&
1093 !D->hasExtInfo() &&
1094 D->getDeclName())
1095 AbbrevToUse = Writer.getDeclObjCIvarAbbrev();
1096
1097 Code = serialization::DECL_OBJC_IVAR;
1098 }
1099
VisitObjCProtocolDecl(ObjCProtocolDecl * D)1100 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
1101 VisitRedeclarable(D);
1102 VisitObjCContainerDecl(D);
1103
1104 Record.push_back(D->isThisDeclarationADefinition());
1105 if (D->isThisDeclarationADefinition()) {
1106 Record.push_back(D->protocol_size());
1107 for (const auto *I : D->protocols())
1108 Record.AddDeclRef(I);
1109 for (const auto &PL : D->protocol_locs())
1110 Record.AddSourceLocation(PL);
1111 Record.push_back(D->getODRHash());
1112 }
1113
1114 Code = serialization::DECL_OBJC_PROTOCOL;
1115 }
1116
VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl * D)1117 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
1118 VisitFieldDecl(D);
1119 Code = serialization::DECL_OBJC_AT_DEFS_FIELD;
1120 }
1121
VisitObjCCategoryDecl(ObjCCategoryDecl * D)1122 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
1123 VisitObjCContainerDecl(D);
1124 Record.AddSourceLocation(D->getCategoryNameLoc());
1125 Record.AddSourceLocation(D->getIvarLBraceLoc());
1126 Record.AddSourceLocation(D->getIvarRBraceLoc());
1127 Record.AddDeclRef(D->getClassInterface());
1128 AddObjCTypeParamList(D->TypeParamList);
1129 Record.push_back(D->protocol_size());
1130 for (const auto *I : D->protocols())
1131 Record.AddDeclRef(I);
1132 for (const auto &PL : D->protocol_locs())
1133 Record.AddSourceLocation(PL);
1134 Code = serialization::DECL_OBJC_CATEGORY;
1135 }
1136
VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl * D)1137 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
1138 VisitNamedDecl(D);
1139 Record.AddDeclRef(D->getClassInterface());
1140 Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;
1141 }
1142
VisitObjCPropertyDecl(ObjCPropertyDecl * D)1143 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
1144 VisitNamedDecl(D);
1145 Record.AddSourceLocation(D->getAtLoc());
1146 Record.AddSourceLocation(D->getLParenLoc());
1147 Record.AddTypeRef(D->getType());
1148 Record.AddTypeSourceInfo(D->getTypeSourceInfo());
1149 // FIXME: stable encoding
1150 Record.push_back((unsigned)D->getPropertyAttributes());
1151 Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
1152 // FIXME: stable encoding
1153 Record.push_back((unsigned)D->getPropertyImplementation());
1154 Record.AddDeclarationName(D->getGetterName());
1155 Record.AddSourceLocation(D->getGetterNameLoc());
1156 Record.AddDeclarationName(D->getSetterName());
1157 Record.AddSourceLocation(D->getSetterNameLoc());
1158 Record.AddDeclRef(D->getGetterMethodDecl());
1159 Record.AddDeclRef(D->getSetterMethodDecl());
1160 Record.AddDeclRef(D->getPropertyIvarDecl());
1161 Code = serialization::DECL_OBJC_PROPERTY;
1162 }
1163
VisitObjCImplDecl(ObjCImplDecl * D)1164 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
1165 VisitObjCContainerDecl(D);
1166 Record.AddDeclRef(D->getClassInterface());
1167 // Abstract class (no need to define a stable serialization::DECL code).
1168 }
1169
VisitObjCCategoryImplDecl(ObjCCategoryImplDecl * D)1170 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1171 VisitObjCImplDecl(D);
1172 Record.AddSourceLocation(D->getCategoryNameLoc());
1173 Code = serialization::DECL_OBJC_CATEGORY_IMPL;
1174 }
1175
VisitObjCImplementationDecl(ObjCImplementationDecl * D)1176 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1177 VisitObjCImplDecl(D);
1178 Record.AddDeclRef(D->getSuperClass());
1179 Record.AddSourceLocation(D->getSuperClassLoc());
1180 Record.AddSourceLocation(D->getIvarLBraceLoc());
1181 Record.AddSourceLocation(D->getIvarRBraceLoc());
1182 Record.push_back(D->hasNonZeroConstructors());
1183 Record.push_back(D->hasDestructors());
1184 Record.push_back(D->NumIvarInitializers);
1185 if (D->NumIvarInitializers)
1186 Record.AddCXXCtorInitializers(
1187 llvm::ArrayRef(D->init_begin(), D->init_end()));
1188 Code = serialization::DECL_OBJC_IMPLEMENTATION;
1189 }
1190
VisitObjCPropertyImplDecl(ObjCPropertyImplDecl * D)1191 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
1192 VisitDecl(D);
1193 Record.AddSourceLocation(D->getBeginLoc());
1194 Record.AddDeclRef(D->getPropertyDecl());
1195 Record.AddDeclRef(D->getPropertyIvarDecl());
1196 Record.AddSourceLocation(D->getPropertyIvarDeclLoc());
1197 Record.AddDeclRef(D->getGetterMethodDecl());
1198 Record.AddDeclRef(D->getSetterMethodDecl());
1199 Record.AddStmt(D->getGetterCXXConstructor());
1200 Record.AddStmt(D->getSetterCXXAssignment());
1201 Code = serialization::DECL_OBJC_PROPERTY_IMPL;
1202 }
1203
VisitFieldDecl(FieldDecl * D)1204 void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {
1205 VisitDeclaratorDecl(D);
1206 Record.push_back(D->isMutable());
1207
1208 Record.push_back((D->StorageKind << 1) | D->BitField);
1209 if (D->StorageKind == FieldDecl::ISK_CapturedVLAType)
1210 Record.AddTypeRef(QualType(D->getCapturedVLAType(), 0));
1211 else if (D->BitField)
1212 Record.AddStmt(D->getBitWidth());
1213
1214 if (!D->getDeclName() || D->isPlaceholderVar(Writer.getLangOpts()))
1215 Record.AddDeclRef(
1216 Record.getASTContext().getInstantiatedFromUnnamedFieldDecl(D));
1217
1218 if (D->getDeclContext() == D->getLexicalDeclContext() &&
1219 !D->hasAttrs() &&
1220 !D->isImplicit() &&
1221 !D->isUsed(false) &&
1222 !D->isInvalidDecl() &&
1223 !D->isReferenced() &&
1224 !D->isTopLevelDeclInObjCContainer() &&
1225 !D->isModulePrivate() &&
1226 !D->getBitWidth() &&
1227 !D->hasInClassInitializer() &&
1228 !D->hasCapturedVLAType() &&
1229 !D->hasExtInfo() &&
1230 !ObjCIvarDecl::classofKind(D->getKind()) &&
1231 !ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&
1232 D->getDeclName())
1233 AbbrevToUse = Writer.getDeclFieldAbbrev();
1234
1235 Code = serialization::DECL_FIELD;
1236 }
1237
VisitMSPropertyDecl(MSPropertyDecl * D)1238 void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {
1239 VisitDeclaratorDecl(D);
1240 Record.AddIdentifierRef(D->getGetterId());
1241 Record.AddIdentifierRef(D->getSetterId());
1242 Code = serialization::DECL_MS_PROPERTY;
1243 }
1244
VisitMSGuidDecl(MSGuidDecl * D)1245 void ASTDeclWriter::VisitMSGuidDecl(MSGuidDecl *D) {
1246 VisitValueDecl(D);
1247 MSGuidDecl::Parts Parts = D->getParts();
1248 Record.push_back(Parts.Part1);
1249 Record.push_back(Parts.Part2);
1250 Record.push_back(Parts.Part3);
1251 Record.append(std::begin(Parts.Part4And5), std::end(Parts.Part4And5));
1252 Code = serialization::DECL_MS_GUID;
1253 }
1254
VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl * D)1255 void ASTDeclWriter::VisitUnnamedGlobalConstantDecl(
1256 UnnamedGlobalConstantDecl *D) {
1257 VisitValueDecl(D);
1258 Record.AddAPValue(D->getValue());
1259 Code = serialization::DECL_UNNAMED_GLOBAL_CONSTANT;
1260 }
1261
VisitTemplateParamObjectDecl(TemplateParamObjectDecl * D)1262 void ASTDeclWriter::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {
1263 VisitValueDecl(D);
1264 Record.AddAPValue(D->getValue());
1265 Code = serialization::DECL_TEMPLATE_PARAM_OBJECT;
1266 }
1267
VisitIndirectFieldDecl(IndirectFieldDecl * D)1268 void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
1269 VisitValueDecl(D);
1270 Record.push_back(D->getChainingSize());
1271
1272 for (const auto *P : D->chain())
1273 Record.AddDeclRef(P);
1274 Code = serialization::DECL_INDIRECTFIELD;
1275 }
1276
VisitVarDecl(VarDecl * D)1277 void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
1278 VisitRedeclarable(D);
1279 VisitDeclaratorDecl(D);
1280
1281 // The order matters here. It will be better to put the bit with higher
1282 // probability to be 0 in the end of the bits. See the comments in VisitDecl
1283 // for details.
1284 BitsPacker VarDeclBits;
1285 VarDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()),
1286 /*BitWidth=*/3);
1287
1288 bool ModulesCodegen = shouldVarGenerateHereOnly(D);
1289 VarDeclBits.addBit(ModulesCodegen);
1290
1291 VarDeclBits.addBits(D->getStorageClass(), /*BitWidth=*/3);
1292 VarDeclBits.addBits(D->getTSCSpec(), /*BitWidth=*/2);
1293 VarDeclBits.addBits(D->getInitStyle(), /*BitWidth=*/2);
1294 VarDeclBits.addBit(D->isARCPseudoStrong());
1295
1296 bool HasDeducedType = false;
1297 if (!isa<ParmVarDecl>(D)) {
1298 VarDeclBits.addBit(D->isThisDeclarationADemotedDefinition());
1299 VarDeclBits.addBit(D->isExceptionVariable());
1300 VarDeclBits.addBit(D->isNRVOVariable());
1301 VarDeclBits.addBit(D->isCXXForRangeDecl());
1302
1303 VarDeclBits.addBit(D->isInline());
1304 VarDeclBits.addBit(D->isInlineSpecified());
1305 VarDeclBits.addBit(D->isConstexpr());
1306 VarDeclBits.addBit(D->isInitCapture());
1307 VarDeclBits.addBit(D->isPreviousDeclInSameBlockScope());
1308
1309 VarDeclBits.addBit(D->isEscapingByref());
1310 HasDeducedType = D->getType()->getContainedDeducedType();
1311 VarDeclBits.addBit(HasDeducedType);
1312
1313 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(D))
1314 VarDeclBits.addBits(llvm::to_underlying(IPD->getParameterKind()),
1315 /*Width=*/3);
1316 else
1317 VarDeclBits.addBits(0, /*Width=*/3);
1318
1319 VarDeclBits.addBit(D->isObjCForDecl());
1320 VarDeclBits.addBit(D->isCXXForRangeImplicitVar());
1321 }
1322
1323 Record.push_back(VarDeclBits);
1324
1325 if (ModulesCodegen)
1326 Writer.AddDeclRef(D, Writer.ModularCodegenDecls);
1327
1328 if (D->hasAttr<BlocksAttr>()) {
1329 BlockVarCopyInit Init = Record.getASTContext().getBlockVarCopyInit(D);
1330 Record.AddStmt(Init.getCopyExpr());
1331 if (Init.getCopyExpr())
1332 Record.push_back(Init.canThrow());
1333 }
1334
1335 enum {
1336 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1337 };
1338 if (VarTemplateDecl *TemplD = D->getDescribedVarTemplate()) {
1339 Record.push_back(VarTemplate);
1340 Record.AddDeclRef(TemplD);
1341 } else if (MemberSpecializationInfo *SpecInfo
1342 = D->getMemberSpecializationInfo()) {
1343 Record.push_back(StaticDataMemberSpecialization);
1344 Record.AddDeclRef(SpecInfo->getInstantiatedFrom());
1345 Record.push_back(SpecInfo->getTemplateSpecializationKind());
1346 Record.AddSourceLocation(SpecInfo->getPointOfInstantiation());
1347 } else {
1348 Record.push_back(VarNotTemplate);
1349 }
1350
1351 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
1352 !D->isTopLevelDeclInObjCContainer() &&
1353 !needsAnonymousDeclarationNumber(D) &&
1354 D->getDeclName().getNameKind() == DeclarationName::Identifier &&
1355 !D->hasExtInfo() && D->getFirstDecl() == D->getMostRecentDecl() &&
1356 D->getKind() == Decl::Var && !D->isInline() && !D->isConstexpr() &&
1357 !D->isInitCapture() && !D->isPreviousDeclInSameBlockScope() &&
1358 !D->hasInitWithSideEffects() && !D->isEscapingByref() &&
1359 !HasDeducedType && D->getStorageDuration() != SD_Static &&
1360 !D->getDescribedVarTemplate() && !D->getMemberSpecializationInfo() &&
1361 !D->isObjCForDecl() && !isa<ImplicitParamDecl>(D) &&
1362 !D->isEscapingByref())
1363 AbbrevToUse = Writer.getDeclVarAbbrev();
1364
1365 Code = serialization::DECL_VAR;
1366 }
1367
VisitImplicitParamDecl(ImplicitParamDecl * D)1368 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
1369 VisitVarDecl(D);
1370 Code = serialization::DECL_IMPLICIT_PARAM;
1371 }
1372
VisitParmVarDecl(ParmVarDecl * D)1373 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
1374 VisitVarDecl(D);
1375
1376 // See the implementation of `ParmVarDecl::getParameterIndex()`, which may
1377 // exceed the size of the normal bitfield. So it may be better to not pack
1378 // these bits.
1379 Record.push_back(D->getFunctionScopeIndex());
1380
1381 BitsPacker ParmVarDeclBits;
1382 ParmVarDeclBits.addBit(D->isObjCMethodParameter());
1383 ParmVarDeclBits.addBits(D->getFunctionScopeDepth(), /*BitsWidth=*/7);
1384 // FIXME: stable encoding
1385 ParmVarDeclBits.addBits(D->getObjCDeclQualifier(), /*BitsWidth=*/7);
1386 ParmVarDeclBits.addBit(D->isKNRPromoted());
1387 ParmVarDeclBits.addBit(D->hasInheritedDefaultArg());
1388 ParmVarDeclBits.addBit(D->hasUninstantiatedDefaultArg());
1389 ParmVarDeclBits.addBit(D->getExplicitObjectParamThisLoc().isValid());
1390 Record.push_back(ParmVarDeclBits);
1391
1392 if (D->hasUninstantiatedDefaultArg())
1393 Record.AddStmt(D->getUninstantiatedDefaultArg());
1394 if (D->getExplicitObjectParamThisLoc().isValid())
1395 Record.AddSourceLocation(D->getExplicitObjectParamThisLoc());
1396 Code = serialization::DECL_PARM_VAR;
1397
1398 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
1399 // we dynamically check for the properties that we optimize for, but don't
1400 // know are true of all PARM_VAR_DECLs.
1401 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
1402 !D->hasExtInfo() && D->getStorageClass() == 0 && !D->isInvalidDecl() &&
1403 !D->isTopLevelDeclInObjCContainer() &&
1404 D->getInitStyle() == VarDecl::CInit && // Can params have anything else?
1405 D->getInit() == nullptr) // No default expr.
1406 AbbrevToUse = Writer.getDeclParmVarAbbrev();
1407
1408 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
1409 // just us assuming it.
1410 assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");
1411 assert(!D->isThisDeclarationADemotedDefinition()
1412 && "PARM_VAR_DECL can't be demoted definition.");
1413 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
1414 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
1415 assert(D->getPreviousDecl() == nullptr && "PARM_VAR_DECL can't be redecl");
1416 assert(!D->isStaticDataMember() &&
1417 "PARM_VAR_DECL can't be static data member");
1418 }
1419
VisitDecompositionDecl(DecompositionDecl * D)1420 void ASTDeclWriter::VisitDecompositionDecl(DecompositionDecl *D) {
1421 // Record the number of bindings first to simplify deserialization.
1422 Record.push_back(D->bindings().size());
1423
1424 VisitVarDecl(D);
1425 for (auto *B : D->bindings())
1426 Record.AddDeclRef(B);
1427 Code = serialization::DECL_DECOMPOSITION;
1428 }
1429
VisitBindingDecl(BindingDecl * D)1430 void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {
1431 VisitValueDecl(D);
1432 Record.AddStmt(D->getBinding());
1433 Code = serialization::DECL_BINDING;
1434 }
1435
VisitFileScopeAsmDecl(FileScopeAsmDecl * D)1436 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
1437 VisitDecl(D);
1438 Record.AddStmt(D->getAsmStringExpr());
1439 Record.AddSourceLocation(D->getRParenLoc());
1440 Code = serialization::DECL_FILE_SCOPE_ASM;
1441 }
1442
VisitTopLevelStmtDecl(TopLevelStmtDecl * D)1443 void ASTDeclWriter::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {
1444 VisitDecl(D);
1445 Record.AddStmt(D->getStmt());
1446 Code = serialization::DECL_TOP_LEVEL_STMT_DECL;
1447 }
1448
VisitEmptyDecl(EmptyDecl * D)1449 void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {
1450 VisitDecl(D);
1451 Code = serialization::DECL_EMPTY;
1452 }
1453
VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl * D)1454 void ASTDeclWriter::VisitLifetimeExtendedTemporaryDecl(
1455 LifetimeExtendedTemporaryDecl *D) {
1456 VisitDecl(D);
1457 Record.AddDeclRef(D->getExtendingDecl());
1458 Record.AddStmt(D->getTemporaryExpr());
1459 Record.push_back(static_cast<bool>(D->getValue()));
1460 if (D->getValue())
1461 Record.AddAPValue(*D->getValue());
1462 Record.push_back(D->getManglingNumber());
1463 Code = serialization::DECL_LIFETIME_EXTENDED_TEMPORARY;
1464 }
VisitBlockDecl(BlockDecl * D)1465 void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
1466 VisitDecl(D);
1467 Record.AddStmt(D->getBody());
1468 Record.AddTypeSourceInfo(D->getSignatureAsWritten());
1469 Record.push_back(D->param_size());
1470 for (ParmVarDecl *P : D->parameters())
1471 Record.AddDeclRef(P);
1472 Record.push_back(D->isVariadic());
1473 Record.push_back(D->blockMissingReturnType());
1474 Record.push_back(D->isConversionFromLambda());
1475 Record.push_back(D->doesNotEscape());
1476 Record.push_back(D->canAvoidCopyToHeap());
1477 Record.push_back(D->capturesCXXThis());
1478 Record.push_back(D->getNumCaptures());
1479 for (const auto &capture : D->captures()) {
1480 Record.AddDeclRef(capture.getVariable());
1481
1482 unsigned flags = 0;
1483 if (capture.isByRef()) flags |= 1;
1484 if (capture.isNested()) flags |= 2;
1485 if (capture.hasCopyExpr()) flags |= 4;
1486 Record.push_back(flags);
1487
1488 if (capture.hasCopyExpr()) Record.AddStmt(capture.getCopyExpr());
1489 }
1490
1491 Code = serialization::DECL_BLOCK;
1492 }
1493
VisitOutlinedFunctionDecl(OutlinedFunctionDecl * D)1494 void ASTDeclWriter::VisitOutlinedFunctionDecl(OutlinedFunctionDecl *D) {
1495 Record.push_back(D->getNumParams());
1496 VisitDecl(D);
1497 for (unsigned I = 0; I < D->getNumParams(); ++I)
1498 Record.AddDeclRef(D->getParam(I));
1499 Record.push_back(D->isNothrow() ? 1 : 0);
1500 Record.AddStmt(D->getBody());
1501 Code = serialization::DECL_OUTLINEDFUNCTION;
1502 }
1503
VisitCapturedDecl(CapturedDecl * CD)1504 void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
1505 Record.push_back(CD->getNumParams());
1506 VisitDecl(CD);
1507 Record.push_back(CD->getContextParamPosition());
1508 Record.push_back(CD->isNothrow() ? 1 : 0);
1509 // Body is stored by VisitCapturedStmt.
1510 for (unsigned I = 0; I < CD->getNumParams(); ++I)
1511 Record.AddDeclRef(CD->getParam(I));
1512 Code = serialization::DECL_CAPTURED;
1513 }
1514
VisitLinkageSpecDecl(LinkageSpecDecl * D)1515 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1516 static_assert(DeclContext::NumLinkageSpecDeclBits == 17,
1517 "You need to update the serializer after you change the"
1518 "LinkageSpecDeclBits");
1519
1520 VisitDecl(D);
1521 Record.push_back(llvm::to_underlying(D->getLanguage()));
1522 Record.AddSourceLocation(D->getExternLoc());
1523 Record.AddSourceLocation(D->getRBraceLoc());
1524 Code = serialization::DECL_LINKAGE_SPEC;
1525 }
1526
VisitExportDecl(ExportDecl * D)1527 void ASTDeclWriter::VisitExportDecl(ExportDecl *D) {
1528 VisitDecl(D);
1529 Record.AddSourceLocation(D->getRBraceLoc());
1530 Code = serialization::DECL_EXPORT;
1531 }
1532
VisitLabelDecl(LabelDecl * D)1533 void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {
1534 VisitNamedDecl(D);
1535 Record.AddSourceLocation(D->getBeginLoc());
1536 Code = serialization::DECL_LABEL;
1537 }
1538
1539
VisitNamespaceDecl(NamespaceDecl * D)1540 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
1541 VisitRedeclarable(D);
1542 VisitNamedDecl(D);
1543
1544 BitsPacker NamespaceDeclBits;
1545 NamespaceDeclBits.addBit(D->isInline());
1546 NamespaceDeclBits.addBit(D->isNested());
1547 Record.push_back(NamespaceDeclBits);
1548
1549 Record.AddSourceLocation(D->getBeginLoc());
1550 Record.AddSourceLocation(D->getRBraceLoc());
1551
1552 if (D->isFirstDecl())
1553 Record.AddDeclRef(D->getAnonymousNamespace());
1554 Code = serialization::DECL_NAMESPACE;
1555
1556 if (Writer.hasChain() && D->isAnonymousNamespace() &&
1557 D == D->getMostRecentDecl()) {
1558 // This is a most recent reopening of the anonymous namespace. If its parent
1559 // is in a previous PCH (or is the TU), mark that parent for update, because
1560 // the original namespace always points to the latest re-opening of its
1561 // anonymous namespace.
1562 Decl *Parent = cast<Decl>(
1563 D->getParent()->getRedeclContext()->getPrimaryContext());
1564 if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {
1565 Writer.DeclUpdates[Parent].push_back(
1566 ASTWriter::DeclUpdate(DeclUpdateKind::CXXAddedAnonymousNamespace, D));
1567 }
1568 }
1569 }
1570
VisitNamespaceAliasDecl(NamespaceAliasDecl * D)1571 void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1572 VisitRedeclarable(D);
1573 VisitNamedDecl(D);
1574 Record.AddSourceLocation(D->getNamespaceLoc());
1575 Record.AddSourceLocation(D->getTargetNameLoc());
1576 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1577 Record.AddDeclRef(D->getNamespace());
1578 Code = serialization::DECL_NAMESPACE_ALIAS;
1579 }
1580
VisitUsingDecl(UsingDecl * D)1581 void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {
1582 VisitNamedDecl(D);
1583 Record.AddSourceLocation(D->getUsingLoc());
1584 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1585 Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
1586 Record.AddDeclRef(D->FirstUsingShadow.getPointer());
1587 Record.push_back(D->hasTypename());
1588 Record.AddDeclRef(Record.getASTContext().getInstantiatedFromUsingDecl(D));
1589 Code = serialization::DECL_USING;
1590 }
1591
VisitUsingEnumDecl(UsingEnumDecl * D)1592 void ASTDeclWriter::VisitUsingEnumDecl(UsingEnumDecl *D) {
1593 VisitNamedDecl(D);
1594 Record.AddSourceLocation(D->getUsingLoc());
1595 Record.AddSourceLocation(D->getEnumLoc());
1596 Record.AddTypeSourceInfo(D->getEnumType());
1597 Record.AddDeclRef(D->FirstUsingShadow.getPointer());
1598 Record.AddDeclRef(Record.getASTContext().getInstantiatedFromUsingEnumDecl(D));
1599 Code = serialization::DECL_USING_ENUM;
1600 }
1601
VisitUsingPackDecl(UsingPackDecl * D)1602 void ASTDeclWriter::VisitUsingPackDecl(UsingPackDecl *D) {
1603 Record.push_back(D->NumExpansions);
1604 VisitNamedDecl(D);
1605 Record.AddDeclRef(D->getInstantiatedFromUsingDecl());
1606 for (auto *E : D->expansions())
1607 Record.AddDeclRef(E);
1608 Code = serialization::DECL_USING_PACK;
1609 }
1610
VisitUsingShadowDecl(UsingShadowDecl * D)1611 void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1612 VisitRedeclarable(D);
1613 VisitNamedDecl(D);
1614 Record.AddDeclRef(D->getTargetDecl());
1615 Record.push_back(D->getIdentifierNamespace());
1616 Record.AddDeclRef(D->UsingOrNextShadow);
1617 Record.AddDeclRef(
1618 Record.getASTContext().getInstantiatedFromUsingShadowDecl(D));
1619
1620 if (D->getDeclContext() == D->getLexicalDeclContext() &&
1621 D->getFirstDecl() == D->getMostRecentDecl() && !D->hasAttrs() &&
1622 !needsAnonymousDeclarationNumber(D) &&
1623 D->getDeclName().getNameKind() == DeclarationName::Identifier)
1624 AbbrevToUse = Writer.getDeclUsingShadowAbbrev();
1625
1626 Code = serialization::DECL_USING_SHADOW;
1627 }
1628
VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl * D)1629 void ASTDeclWriter::VisitConstructorUsingShadowDecl(
1630 ConstructorUsingShadowDecl *D) {
1631 VisitUsingShadowDecl(D);
1632 Record.AddDeclRef(D->NominatedBaseClassShadowDecl);
1633 Record.AddDeclRef(D->ConstructedBaseClassShadowDecl);
1634 Record.push_back(D->IsVirtual);
1635 Code = serialization::DECL_CONSTRUCTOR_USING_SHADOW;
1636 }
1637
VisitUsingDirectiveDecl(UsingDirectiveDecl * D)1638 void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1639 VisitNamedDecl(D);
1640 Record.AddSourceLocation(D->getUsingLoc());
1641 Record.AddSourceLocation(D->getNamespaceKeyLocation());
1642 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1643 Record.AddDeclRef(D->getNominatedNamespace());
1644 Record.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()));
1645 Code = serialization::DECL_USING_DIRECTIVE;
1646 }
1647
VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl * D)1648 void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1649 VisitValueDecl(D);
1650 Record.AddSourceLocation(D->getUsingLoc());
1651 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1652 Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
1653 Record.AddSourceLocation(D->getEllipsisLoc());
1654 Code = serialization::DECL_UNRESOLVED_USING_VALUE;
1655 }
1656
VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl * D)1657 void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
1658 UnresolvedUsingTypenameDecl *D) {
1659 VisitTypeDecl(D);
1660 Record.AddSourceLocation(D->getTypenameLoc());
1661 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1662 Record.AddSourceLocation(D->getEllipsisLoc());
1663 Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;
1664 }
1665
VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl * D)1666 void ASTDeclWriter::VisitUnresolvedUsingIfExistsDecl(
1667 UnresolvedUsingIfExistsDecl *D) {
1668 VisitNamedDecl(D);
1669 Code = serialization::DECL_UNRESOLVED_USING_IF_EXISTS;
1670 }
1671
VisitCXXRecordDecl(CXXRecordDecl * D)1672 void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
1673 VisitRecordDecl(D);
1674
1675 enum {
1676 CXXRecNotTemplate = 0,
1677 CXXRecTemplate,
1678 CXXRecMemberSpecialization,
1679 CXXLambda
1680 };
1681 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
1682 Record.push_back(CXXRecTemplate);
1683 Record.AddDeclRef(TemplD);
1684 } else if (MemberSpecializationInfo *MSInfo
1685 = D->getMemberSpecializationInfo()) {
1686 Record.push_back(CXXRecMemberSpecialization);
1687 Record.AddDeclRef(MSInfo->getInstantiatedFrom());
1688 Record.push_back(MSInfo->getTemplateSpecializationKind());
1689 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
1690 } else if (D->isLambda()) {
1691 // For a lambda, we need some information early for merging.
1692 Record.push_back(CXXLambda);
1693 if (auto *Context = D->getLambdaContextDecl()) {
1694 Record.AddDeclRef(Context);
1695 Record.push_back(D->getLambdaIndexInContext());
1696 } else {
1697 Record.push_back(0);
1698 }
1699 // For lambdas inside template functions, remember the mapping to
1700 // deserialize them together.
1701 if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D->getDeclContext());
1702 FD && isDefinitionInDependentContext(FD)) {
1703 Writer.RelatedDeclsMap[Writer.GetDeclRef(FD)].push_back(
1704 Writer.GetDeclRef(D->getLambdaCallOperator()));
1705 }
1706 } else {
1707 Record.push_back(CXXRecNotTemplate);
1708 }
1709
1710 Record.push_back(D->isThisDeclarationADefinition());
1711 if (D->isThisDeclarationADefinition())
1712 Record.AddCXXDefinitionData(D);
1713
1714 if (D->isCompleteDefinition() && D->isInNamedModule())
1715 Writer.AddDeclRef(D, Writer.ModularCodegenDecls);
1716
1717 // Store (what we currently believe to be) the key function to avoid
1718 // deserializing every method so we can compute it.
1719 //
1720 // FIXME: Avoid adding the key function if the class is defined in
1721 // module purview since in that case the key function is meaningless.
1722 if (D->isCompleteDefinition())
1723 Record.AddDeclRef(Record.getASTContext().getCurrentKeyFunction(D));
1724
1725 Code = serialization::DECL_CXX_RECORD;
1726 }
1727
VisitCXXMethodDecl(CXXMethodDecl * D)1728 void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1729 VisitFunctionDecl(D);
1730 if (D->isCanonicalDecl()) {
1731 Record.push_back(D->size_overridden_methods());
1732 for (const CXXMethodDecl *MD : D->overridden_methods())
1733 Record.AddDeclRef(MD);
1734 } else {
1735 // We only need to record overridden methods once for the canonical decl.
1736 Record.push_back(0);
1737 }
1738
1739 if (D->getDeclContext() == D->getLexicalDeclContext() &&
1740 D->getFirstDecl() == D->getMostRecentDecl() && !D->isInvalidDecl() &&
1741 !D->hasAttrs() && !D->isTopLevelDeclInObjCContainer() &&
1742 D->getDeclName().getNameKind() == DeclarationName::Identifier &&
1743 !D->hasExtInfo() && !D->isExplicitlyDefaulted()) {
1744 if (D->getTemplatedKind() == FunctionDecl::TK_NonTemplate ||
1745 D->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate ||
1746 D->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization ||
1747 D->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate)
1748 AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());
1749 else if (D->getTemplatedKind() ==
1750 FunctionDecl::TK_FunctionTemplateSpecialization) {
1751 FunctionTemplateSpecializationInfo *FTSInfo =
1752 D->getTemplateSpecializationInfo();
1753
1754 if (FTSInfo->TemplateArguments->size() == 1) {
1755 const TemplateArgument &TA = FTSInfo->TemplateArguments->get(0);
1756 if (TA.getKind() == TemplateArgument::Type &&
1757 !FTSInfo->TemplateArgumentsAsWritten &&
1758 !FTSInfo->getMemberSpecializationInfo())
1759 AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());
1760 }
1761 } else if (D->getTemplatedKind() ==
1762 FunctionDecl::TK_DependentFunctionTemplateSpecialization) {
1763 DependentFunctionTemplateSpecializationInfo *DFTSInfo =
1764 D->getDependentSpecializationInfo();
1765 if (!DFTSInfo->TemplateArgumentsAsWritten)
1766 AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());
1767 }
1768 }
1769
1770 Code = serialization::DECL_CXX_METHOD;
1771 }
1772
VisitCXXConstructorDecl(CXXConstructorDecl * D)1773 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1774 static_assert(DeclContext::NumCXXConstructorDeclBits == 64,
1775 "You need to update the serializer after you change the "
1776 "CXXConstructorDeclBits");
1777
1778 Record.push_back(D->getTrailingAllocKind());
1779 addExplicitSpecifier(D->getExplicitSpecifierInternal(), Record);
1780 if (auto Inherited = D->getInheritedConstructor()) {
1781 Record.AddDeclRef(Inherited.getShadowDecl());
1782 Record.AddDeclRef(Inherited.getConstructor());
1783 }
1784
1785 VisitCXXMethodDecl(D);
1786 Code = serialization::DECL_CXX_CONSTRUCTOR;
1787 }
1788
VisitCXXDestructorDecl(CXXDestructorDecl * D)1789 void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1790 VisitCXXMethodDecl(D);
1791
1792 Record.AddDeclRef(D->getOperatorDelete());
1793 if (D->getOperatorDelete())
1794 Record.AddStmt(D->getOperatorDeleteThisArg());
1795
1796 Code = serialization::DECL_CXX_DESTRUCTOR;
1797 }
1798
VisitCXXConversionDecl(CXXConversionDecl * D)1799 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1800 addExplicitSpecifier(D->getExplicitSpecifier(), Record);
1801 VisitCXXMethodDecl(D);
1802 Code = serialization::DECL_CXX_CONVERSION;
1803 }
1804
VisitImportDecl(ImportDecl * D)1805 void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {
1806 VisitDecl(D);
1807 Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));
1808 ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();
1809 Record.push_back(!IdentifierLocs.empty());
1810 if (IdentifierLocs.empty()) {
1811 Record.AddSourceLocation(D->getEndLoc());
1812 Record.push_back(1);
1813 } else {
1814 for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)
1815 Record.AddSourceLocation(IdentifierLocs[I]);
1816 Record.push_back(IdentifierLocs.size());
1817 }
1818 // Note: the number of source locations must always be the last element in
1819 // the record.
1820 Code = serialization::DECL_IMPORT;
1821 }
1822
VisitAccessSpecDecl(AccessSpecDecl * D)1823 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1824 VisitDecl(D);
1825 Record.AddSourceLocation(D->getColonLoc());
1826 Code = serialization::DECL_ACCESS_SPEC;
1827 }
1828
VisitFriendDecl(FriendDecl * D)1829 void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {
1830 // Record the number of friend type template parameter lists here
1831 // so as to simplify memory allocation during deserialization.
1832 Record.push_back(D->NumTPLists);
1833 VisitDecl(D);
1834 bool hasFriendDecl = isa<NamedDecl *>(D->Friend);
1835 Record.push_back(hasFriendDecl);
1836 if (hasFriendDecl)
1837 Record.AddDeclRef(D->getFriendDecl());
1838 else
1839 Record.AddTypeSourceInfo(D->getFriendType());
1840 for (unsigned i = 0; i < D->NumTPLists; ++i)
1841 Record.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i));
1842 Record.AddDeclRef(D->getNextFriend());
1843 Record.push_back(D->UnsupportedFriend);
1844 Record.AddSourceLocation(D->FriendLoc);
1845 Record.AddSourceLocation(D->EllipsisLoc);
1846 Code = serialization::DECL_FRIEND;
1847 }
1848
VisitFriendTemplateDecl(FriendTemplateDecl * D)1849 void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1850 VisitDecl(D);
1851 Record.push_back(D->getNumTemplateParameters());
1852 for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
1853 Record.AddTemplateParameterList(D->getTemplateParameterList(i));
1854 Record.push_back(D->getFriendDecl() != nullptr);
1855 if (D->getFriendDecl())
1856 Record.AddDeclRef(D->getFriendDecl());
1857 else
1858 Record.AddTypeSourceInfo(D->getFriendType());
1859 Record.AddSourceLocation(D->getFriendLoc());
1860 Code = serialization::DECL_FRIEND_TEMPLATE;
1861 }
1862
VisitTemplateDecl(TemplateDecl * D)1863 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
1864 VisitNamedDecl(D);
1865
1866 Record.AddTemplateParameterList(D->getTemplateParameters());
1867 Record.AddDeclRef(D->getTemplatedDecl());
1868 }
1869
VisitConceptDecl(ConceptDecl * D)1870 void ASTDeclWriter::VisitConceptDecl(ConceptDecl *D) {
1871 VisitTemplateDecl(D);
1872 Record.AddStmt(D->getConstraintExpr());
1873 Code = serialization::DECL_CONCEPT;
1874 }
1875
VisitImplicitConceptSpecializationDecl(ImplicitConceptSpecializationDecl * D)1876 void ASTDeclWriter::VisitImplicitConceptSpecializationDecl(
1877 ImplicitConceptSpecializationDecl *D) {
1878 Record.push_back(D->getTemplateArguments().size());
1879 VisitDecl(D);
1880 for (const TemplateArgument &Arg : D->getTemplateArguments())
1881 Record.AddTemplateArgument(Arg);
1882 Code = serialization::DECL_IMPLICIT_CONCEPT_SPECIALIZATION;
1883 }
1884
VisitRequiresExprBodyDecl(RequiresExprBodyDecl * D)1885 void ASTDeclWriter::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {
1886 Code = serialization::DECL_REQUIRES_EXPR_BODY;
1887 }
1888
VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl * D)1889 void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1890 VisitRedeclarable(D);
1891
1892 // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
1893 // getCommonPtr() can be used while this is still initializing.
1894 if (D->isFirstDecl()) {
1895 // This declaration owns the 'common' pointer, so serialize that data now.
1896 Record.AddDeclRef(D->getInstantiatedFromMemberTemplate());
1897 if (D->getInstantiatedFromMemberTemplate())
1898 Record.push_back(D->isMemberSpecialization());
1899 }
1900
1901 VisitTemplateDecl(D);
1902 Record.push_back(D->getIdentifierNamespace());
1903 }
1904
VisitClassTemplateDecl(ClassTemplateDecl * D)1905 void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1906 VisitRedeclarableTemplateDecl(D);
1907
1908 if (D->isFirstDecl())
1909 AddTemplateSpecializations(D);
1910
1911 // Force emitting the corresponding deduction guide in reduced BMI mode.
1912 // Otherwise, the deduction guide may be optimized out incorrectly.
1913 if (Writer.isGeneratingReducedBMI()) {
1914 auto Name =
1915 Record.getASTContext().DeclarationNames.getCXXDeductionGuideName(D);
1916 for (auto *DG : D->getDeclContext()->noload_lookup(Name))
1917 Writer.GetDeclRef(DG->getCanonicalDecl());
1918 }
1919
1920 Code = serialization::DECL_CLASS_TEMPLATE;
1921 }
1922
VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl * D)1923 void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
1924 ClassTemplateSpecializationDecl *D) {
1925 RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
1926
1927 VisitCXXRecordDecl(D);
1928
1929 llvm::PointerUnion<ClassTemplateDecl *,
1930 ClassTemplatePartialSpecializationDecl *> InstFrom
1931 = D->getSpecializedTemplateOrPartial();
1932 if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {
1933 Record.AddDeclRef(InstFromD);
1934 } else {
1935 Record.AddDeclRef(cast<ClassTemplatePartialSpecializationDecl *>(InstFrom));
1936 Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
1937 }
1938
1939 Record.AddTemplateArgumentList(&D->getTemplateArgs());
1940 Record.AddSourceLocation(D->getPointOfInstantiation());
1941 Record.push_back(D->getSpecializationKind());
1942 Record.push_back(D->hasStrictPackMatch());
1943 Record.push_back(D->isCanonicalDecl());
1944
1945 if (D->isCanonicalDecl()) {
1946 // When reading, we'll add it to the folding set of the following template.
1947 Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
1948 }
1949
1950 bool ExplicitInstantiation =
1951 D->getTemplateSpecializationKind() ==
1952 TSK_ExplicitInstantiationDeclaration ||
1953 D->getTemplateSpecializationKind() == TSK_ExplicitInstantiationDefinition;
1954 Record.push_back(ExplicitInstantiation);
1955 if (ExplicitInstantiation) {
1956 Record.AddSourceLocation(D->getExternKeywordLoc());
1957 Record.AddSourceLocation(D->getTemplateKeywordLoc());
1958 }
1959
1960 const ASTTemplateArgumentListInfo *ArgsWritten =
1961 D->getTemplateArgsAsWritten();
1962 Record.push_back(!!ArgsWritten);
1963 if (ArgsWritten)
1964 Record.AddASTTemplateArgumentListInfo(ArgsWritten);
1965
1966 // Mention the implicitly generated C++ deduction guide to make sure the
1967 // deduction guide will be rewritten as expected.
1968 //
1969 // FIXME: Would it be more efficient to add a callback register function
1970 // in sema to register the deduction guide?
1971 if (Writer.isWritingStdCXXNamedModules()) {
1972 auto Name =
1973 Record.getASTContext().DeclarationNames.getCXXDeductionGuideName(
1974 D->getSpecializedTemplate());
1975 for (auto *DG : D->getDeclContext()->noload_lookup(Name))
1976 Writer.GetDeclRef(DG->getCanonicalDecl());
1977 }
1978
1979 Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;
1980 }
1981
VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl * D)1982 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
1983 ClassTemplatePartialSpecializationDecl *D) {
1984 Record.AddTemplateParameterList(D->getTemplateParameters());
1985
1986 VisitClassTemplateSpecializationDecl(D);
1987
1988 // These are read/set from/to the first declaration.
1989 if (D->getPreviousDecl() == nullptr) {
1990 Record.AddDeclRef(D->getInstantiatedFromMember());
1991 Record.push_back(D->isMemberSpecialization());
1992 }
1993
1994 Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
1995 }
1996
VisitVarTemplateDecl(VarTemplateDecl * D)1997 void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {
1998 VisitRedeclarableTemplateDecl(D);
1999
2000 if (D->isFirstDecl())
2001 AddTemplateSpecializations(D);
2002 Code = serialization::DECL_VAR_TEMPLATE;
2003 }
2004
VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl * D)2005 void ASTDeclWriter::VisitVarTemplateSpecializationDecl(
2006 VarTemplateSpecializationDecl *D) {
2007 RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
2008
2009 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2010 InstFrom = D->getSpecializedTemplateOrPartial();
2011 if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {
2012 Record.AddDeclRef(InstFromD);
2013 } else {
2014 Record.AddDeclRef(cast<VarTemplatePartialSpecializationDecl *>(InstFrom));
2015 Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
2016 }
2017
2018 bool ExplicitInstantiation =
2019 D->getTemplateSpecializationKind() ==
2020 TSK_ExplicitInstantiationDeclaration ||
2021 D->getTemplateSpecializationKind() == TSK_ExplicitInstantiationDefinition;
2022 Record.push_back(ExplicitInstantiation);
2023 if (ExplicitInstantiation) {
2024 Record.AddSourceLocation(D->getExternKeywordLoc());
2025 Record.AddSourceLocation(D->getTemplateKeywordLoc());
2026 }
2027
2028 const ASTTemplateArgumentListInfo *ArgsWritten =
2029 D->getTemplateArgsAsWritten();
2030 Record.push_back(!!ArgsWritten);
2031 if (ArgsWritten)
2032 Record.AddASTTemplateArgumentListInfo(ArgsWritten);
2033
2034 Record.AddTemplateArgumentList(&D->getTemplateArgs());
2035 Record.AddSourceLocation(D->getPointOfInstantiation());
2036 Record.push_back(D->getSpecializationKind());
2037 Record.push_back(D->IsCompleteDefinition);
2038
2039 VisitVarDecl(D);
2040
2041 Record.push_back(D->isCanonicalDecl());
2042
2043 if (D->isCanonicalDecl()) {
2044 // When reading, we'll add it to the folding set of the following template.
2045 Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
2046 }
2047
2048 Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;
2049 }
2050
VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl * D)2051 void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(
2052 VarTemplatePartialSpecializationDecl *D) {
2053 Record.AddTemplateParameterList(D->getTemplateParameters());
2054
2055 VisitVarTemplateSpecializationDecl(D);
2056
2057 // These are read/set from/to the first declaration.
2058 if (D->getPreviousDecl() == nullptr) {
2059 Record.AddDeclRef(D->getInstantiatedFromMember());
2060 Record.push_back(D->isMemberSpecialization());
2061 }
2062
2063 Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;
2064 }
2065
VisitFunctionTemplateDecl(FunctionTemplateDecl * D)2066 void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
2067 VisitRedeclarableTemplateDecl(D);
2068
2069 if (D->isFirstDecl())
2070 AddTemplateSpecializations(D);
2071 Code = serialization::DECL_FUNCTION_TEMPLATE;
2072 }
2073
VisitTemplateTypeParmDecl(TemplateTypeParmDecl * D)2074 void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
2075 Record.push_back(D->hasTypeConstraint());
2076 VisitTypeDecl(D);
2077
2078 Record.push_back(D->wasDeclaredWithTypename());
2079
2080 const TypeConstraint *TC = D->getTypeConstraint();
2081 if (D->hasTypeConstraint())
2082 Record.push_back(/*TypeConstraintInitialized=*/TC != nullptr);
2083 if (TC) {
2084 auto *CR = TC->getConceptReference();
2085 Record.push_back(CR != nullptr);
2086 if (CR)
2087 Record.AddConceptReference(CR);
2088 Record.AddStmt(TC->getImmediatelyDeclaredConstraint());
2089 Record.writeUnsignedOrNone(TC->getArgPackSubstIndex());
2090 Record.writeUnsignedOrNone(D->getNumExpansionParameters());
2091 }
2092
2093 bool OwnsDefaultArg = D->hasDefaultArgument() &&
2094 !D->defaultArgumentWasInherited();
2095 Record.push_back(OwnsDefaultArg);
2096 if (OwnsDefaultArg)
2097 Record.AddTemplateArgumentLoc(D->getDefaultArgument());
2098
2099 if (!D->hasTypeConstraint() && !OwnsDefaultArg &&
2100 D->getDeclContext() == D->getLexicalDeclContext() &&
2101 !D->isInvalidDecl() && !D->hasAttrs() &&
2102 !D->isTopLevelDeclInObjCContainer() && !D->isImplicit() &&
2103 D->getDeclName().getNameKind() == DeclarationName::Identifier)
2104 AbbrevToUse = Writer.getDeclTemplateTypeParmAbbrev();
2105
2106 Code = serialization::DECL_TEMPLATE_TYPE_PARM;
2107 }
2108
VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl * D)2109 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
2110 // For an expanded parameter pack, record the number of expansion types here
2111 // so that it's easier for deserialization to allocate the right amount of
2112 // memory.
2113 Record.push_back(D->hasPlaceholderTypeConstraint());
2114 if (D->isExpandedParameterPack())
2115 Record.push_back(D->getNumExpansionTypes());
2116
2117 VisitDeclaratorDecl(D);
2118 // TemplateParmPosition.
2119 Record.push_back(D->getDepth());
2120 Record.push_back(D->getPosition());
2121
2122 if (D->hasPlaceholderTypeConstraint())
2123 Record.AddStmt(D->getPlaceholderTypeConstraint());
2124
2125 if (D->isExpandedParameterPack()) {
2126 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
2127 Record.AddTypeRef(D->getExpansionType(I));
2128 Record.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I));
2129 }
2130
2131 Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;
2132 } else {
2133 // Rest of NonTypeTemplateParmDecl.
2134 Record.push_back(D->isParameterPack());
2135 bool OwnsDefaultArg = D->hasDefaultArgument() &&
2136 !D->defaultArgumentWasInherited();
2137 Record.push_back(OwnsDefaultArg);
2138 if (OwnsDefaultArg)
2139 Record.AddTemplateArgumentLoc(D->getDefaultArgument());
2140 Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
2141 }
2142 }
2143
VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl * D)2144 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
2145 // For an expanded parameter pack, record the number of expansion types here
2146 // so that it's easier for deserialization to allocate the right amount of
2147 // memory.
2148 if (D->isExpandedParameterPack())
2149 Record.push_back(D->getNumExpansionTemplateParameters());
2150
2151 VisitTemplateDecl(D);
2152 Record.push_back(D->wasDeclaredWithTypename());
2153 // TemplateParmPosition.
2154 Record.push_back(D->getDepth());
2155 Record.push_back(D->getPosition());
2156
2157 if (D->isExpandedParameterPack()) {
2158 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2159 I != N; ++I)
2160 Record.AddTemplateParameterList(D->getExpansionTemplateParameters(I));
2161 Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;
2162 } else {
2163 // Rest of TemplateTemplateParmDecl.
2164 Record.push_back(D->isParameterPack());
2165 bool OwnsDefaultArg = D->hasDefaultArgument() &&
2166 !D->defaultArgumentWasInherited();
2167 Record.push_back(OwnsDefaultArg);
2168 if (OwnsDefaultArg)
2169 Record.AddTemplateArgumentLoc(D->getDefaultArgument());
2170 Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;
2171 }
2172 }
2173
VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl * D)2174 void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2175 VisitRedeclarableTemplateDecl(D);
2176 Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;
2177 }
2178
VisitStaticAssertDecl(StaticAssertDecl * D)2179 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2180 VisitDecl(D);
2181 Record.AddStmt(D->getAssertExpr());
2182 Record.push_back(D->isFailed());
2183 Record.AddStmt(D->getMessage());
2184 Record.AddSourceLocation(D->getRParenLoc());
2185 Code = serialization::DECL_STATIC_ASSERT;
2186 }
2187
2188 /// Emit the DeclContext part of a declaration context decl.
VisitDeclContext(DeclContext * DC)2189 void ASTDeclWriter::VisitDeclContext(DeclContext *DC) {
2190 static_assert(DeclContext::NumDeclContextBits == 13,
2191 "You need to update the serializer after you change the "
2192 "DeclContextBits");
2193 LookupBlockOffsets Offsets;
2194
2195 if (Writer.isGeneratingReducedBMI() && isa<NamespaceDecl>(DC) &&
2196 cast<NamespaceDecl>(DC)->isFromExplicitGlobalModule()) {
2197 // In reduced BMI, delay writing lexical and visible block for namespace
2198 // in the global module fragment. See the comments of DelayedNamespace for
2199 // details.
2200 Writer.DelayedNamespace.push_back(cast<NamespaceDecl>(DC));
2201 } else {
2202 Offsets.LexicalOffset =
2203 Writer.WriteDeclContextLexicalBlock(Record.getASTContext(), DC);
2204 Writer.WriteDeclContextVisibleBlock(Record.getASTContext(), DC, Offsets);
2205 }
2206
2207 Record.AddLookupOffsets(Offsets);
2208 }
2209
getFirstLocalDecl(const Decl * D)2210 const Decl *ASTWriter::getFirstLocalDecl(const Decl *D) {
2211 assert(IsLocalDecl(D) && "expected a local declaration");
2212
2213 const Decl *Canon = D->getCanonicalDecl();
2214 if (IsLocalDecl(Canon))
2215 return Canon;
2216
2217 const Decl *&CacheEntry = FirstLocalDeclCache[Canon];
2218 if (CacheEntry)
2219 return CacheEntry;
2220
2221 for (const Decl *Redecl = D; Redecl; Redecl = Redecl->getPreviousDecl())
2222 if (IsLocalDecl(Redecl))
2223 D = Redecl;
2224 return CacheEntry = D;
2225 }
2226
2227 template <typename T>
VisitRedeclarable(Redeclarable<T> * D)2228 void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {
2229 T *First = D->getFirstDecl();
2230 T *MostRecent = First->getMostRecentDecl();
2231 T *DAsT = static_cast<T *>(D);
2232 if (MostRecent != First) {
2233 assert(isRedeclarableDeclKind(DAsT->getKind()) &&
2234 "Not considered redeclarable?");
2235
2236 Record.AddDeclRef(First);
2237
2238 // Write out a list of local redeclarations of this declaration if it's the
2239 // first local declaration in the chain.
2240 const Decl *FirstLocal = Writer.getFirstLocalDecl(DAsT);
2241 if (DAsT == FirstLocal) {
2242 // Emit a list of all imported first declarations so that we can be sure
2243 // that all redeclarations visible to this module are before D in the
2244 // redecl chain.
2245 unsigned I = Record.size();
2246 Record.push_back(0);
2247 if (Writer.Chain)
2248 AddFirstDeclFromEachModule(DAsT, /*IncludeLocal*/false);
2249 // This is the number of imported first declarations + 1.
2250 Record[I] = Record.size() - I;
2251
2252 // Collect the set of local redeclarations of this declaration, from
2253 // newest to oldest.
2254 ASTWriter::RecordData LocalRedecls;
2255 ASTRecordWriter LocalRedeclWriter(Record, LocalRedecls);
2256 for (const Decl *Prev = FirstLocal->getMostRecentDecl();
2257 Prev != FirstLocal; Prev = Prev->getPreviousDecl())
2258 if (!Prev->isFromASTFile())
2259 LocalRedeclWriter.AddDeclRef(Prev);
2260
2261 // If we have any redecls, write them now as a separate record preceding
2262 // the declaration itself.
2263 if (LocalRedecls.empty())
2264 Record.push_back(0);
2265 else
2266 Record.AddOffset(LocalRedeclWriter.Emit(LOCAL_REDECLARATIONS));
2267 } else {
2268 Record.push_back(0);
2269 Record.AddDeclRef(FirstLocal);
2270 }
2271
2272 // Make sure that we serialize both the previous and the most-recent
2273 // declarations, which (transitively) ensures that all declarations in the
2274 // chain get serialized.
2275 //
2276 // FIXME: This is not correct; when we reach an imported declaration we
2277 // won't emit its previous declaration.
2278 (void)Writer.GetDeclRef(D->getPreviousDecl());
2279 (void)Writer.GetDeclRef(MostRecent);
2280 } else {
2281 // We use the sentinel value 0 to indicate an only declaration.
2282 Record.push_back(0);
2283 }
2284 }
2285
VisitHLSLBufferDecl(HLSLBufferDecl * D)2286 void ASTDeclWriter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
2287 VisitNamedDecl(D);
2288 VisitDeclContext(D);
2289 Record.push_back(D->isCBuffer());
2290 Record.AddSourceLocation(D->getLocStart());
2291 Record.AddSourceLocation(D->getLBraceLoc());
2292 Record.AddSourceLocation(D->getRBraceLoc());
2293
2294 Code = serialization::DECL_HLSL_BUFFER;
2295 }
2296
VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl * D)2297 void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2298 Record.writeOMPChildren(D->Data);
2299 VisitDecl(D);
2300 Code = serialization::DECL_OMP_THREADPRIVATE;
2301 }
2302
VisitOMPAllocateDecl(OMPAllocateDecl * D)2303 void ASTDeclWriter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
2304 Record.writeOMPChildren(D->Data);
2305 VisitDecl(D);
2306 Code = serialization::DECL_OMP_ALLOCATE;
2307 }
2308
VisitOMPRequiresDecl(OMPRequiresDecl * D)2309 void ASTDeclWriter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
2310 Record.writeOMPChildren(D->Data);
2311 VisitDecl(D);
2312 Code = serialization::DECL_OMP_REQUIRES;
2313 }
2314
VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl * D)2315 void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
2316 static_assert(DeclContext::NumOMPDeclareReductionDeclBits == 15,
2317 "You need to update the serializer after you change the "
2318 "NumOMPDeclareReductionDeclBits");
2319
2320 VisitValueDecl(D);
2321 Record.AddSourceLocation(D->getBeginLoc());
2322 Record.AddStmt(D->getCombinerIn());
2323 Record.AddStmt(D->getCombinerOut());
2324 Record.AddStmt(D->getCombiner());
2325 Record.AddStmt(D->getInitOrig());
2326 Record.AddStmt(D->getInitPriv());
2327 Record.AddStmt(D->getInitializer());
2328 Record.push_back(llvm::to_underlying(D->getInitializerKind()));
2329 Record.AddDeclRef(D->getPrevDeclInScope());
2330 Code = serialization::DECL_OMP_DECLARE_REDUCTION;
2331 }
2332
VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl * D)2333 void ASTDeclWriter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2334 Record.writeOMPChildren(D->Data);
2335 VisitValueDecl(D);
2336 Record.AddDeclarationName(D->getVarName());
2337 Record.AddDeclRef(D->getPrevDeclInScope());
2338 Code = serialization::DECL_OMP_DECLARE_MAPPER;
2339 }
2340
VisitOMPCapturedExprDecl(OMPCapturedExprDecl * D)2341 void ASTDeclWriter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
2342 VisitVarDecl(D);
2343 Code = serialization::DECL_OMP_CAPTUREDEXPR;
2344 }
2345
VisitOpenACCDeclareDecl(OpenACCDeclareDecl * D)2346 void ASTDeclWriter::VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D) {
2347 Record.writeUInt32(D->clauses().size());
2348 VisitDecl(D);
2349 Record.writeEnum(D->DirKind);
2350 Record.AddSourceLocation(D->DirectiveLoc);
2351 Record.AddSourceLocation(D->EndLoc);
2352 Record.writeOpenACCClauseList(D->clauses());
2353 Code = serialization::DECL_OPENACC_DECLARE;
2354 }
VisitOpenACCRoutineDecl(OpenACCRoutineDecl * D)2355 void ASTDeclWriter::VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D) {
2356 Record.writeUInt32(D->clauses().size());
2357 VisitDecl(D);
2358 Record.writeEnum(D->DirKind);
2359 Record.AddSourceLocation(D->DirectiveLoc);
2360 Record.AddSourceLocation(D->EndLoc);
2361 Record.AddSourceRange(D->ParensLoc);
2362 Record.AddStmt(D->FuncRef);
2363 Record.writeOpenACCClauseList(D->clauses());
2364 Code = serialization::DECL_OPENACC_ROUTINE;
2365 }
2366
2367 //===----------------------------------------------------------------------===//
2368 // ASTWriter Implementation
2369 //===----------------------------------------------------------------------===//
2370
2371 namespace {
2372 template <FunctionDecl::TemplatedKind Kind>
2373 std::shared_ptr<llvm::BitCodeAbbrev>
getFunctionDeclAbbrev(serialization::DeclCode Code)2374 getFunctionDeclAbbrev(serialization::DeclCode Code) {
2375 using namespace llvm;
2376
2377 auto Abv = std::make_shared<BitCodeAbbrev>();
2378 Abv->Add(BitCodeAbbrevOp(Code));
2379 // RedeclarableDecl
2380 Abv->Add(BitCodeAbbrevOp(0)); // CanonicalDecl
2381 Abv->Add(BitCodeAbbrevOp(Kind));
2382 if constexpr (Kind == FunctionDecl::TK_NonTemplate) {
2383
2384 } else if constexpr (Kind == FunctionDecl::TK_FunctionTemplate) {
2385 // DescribedFunctionTemplate
2386 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2387 } else if constexpr (Kind == FunctionDecl::TK_DependentNonTemplate) {
2388 // Instantiated From Decl
2389 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2390 } else if constexpr (Kind == FunctionDecl::TK_MemberSpecialization) {
2391 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedFrom
2392 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2393 3)); // TemplateSpecializationKind
2394 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Specialized Location
2395 } else if constexpr (Kind ==
2396 FunctionDecl::TK_FunctionTemplateSpecialization) {
2397 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template
2398 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2399 3)); // TemplateSpecializationKind
2400 Abv->Add(BitCodeAbbrevOp(1)); // Template Argument Size
2401 Abv->Add(BitCodeAbbrevOp(TemplateArgument::Type)); // Template Argument Kind
2402 Abv->Add(
2403 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template Argument Type
2404 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is Defaulted
2405 Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten
2406 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2407 Abv->Add(BitCodeAbbrevOp(0));
2408 Abv->Add(
2409 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Canonical Decl of template
2410 } else if constexpr (Kind == FunctionDecl::
2411 TK_DependentFunctionTemplateSpecialization) {
2412 // Candidates of specialization
2413 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2414 Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten
2415 } else {
2416 llvm_unreachable("Unknown templated kind?");
2417 }
2418 // Decl
2419 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2420 8)); // Packed DeclBits: ModuleOwnershipKind,
2421 // isUsed, isReferenced, AccessSpecifier,
2422 // isImplicit
2423 //
2424 // The following bits should be 0:
2425 // HasStandaloneLexicalDC, HasAttrs,
2426 // TopLevelDeclInObjCContainer,
2427 // isInvalidDecl
2428 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2429 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2430 // NamedDecl
2431 Abv->Add(BitCodeAbbrevOp(DeclarationName::Identifier)); // NameKind
2432 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Identifier
2433 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2434 // ValueDecl
2435 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2436 // DeclaratorDecl
2437 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerLocStart
2438 Abv->Add(BitCodeAbbrevOp(0)); // HasExtInfo
2439 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2440 // FunctionDecl
2441 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS
2442 Abv->Add(BitCodeAbbrevOp(
2443 BitCodeAbbrevOp::Fixed,
2444 28)); // Packed Function Bits: StorageClass, Inline, InlineSpecified,
2445 // VirtualAsWritten, Pure, HasInheritedProto, HasWrittenProto,
2446 // Deleted, Trivial, TrivialForCall, Defaulted, ExplicitlyDefaulted,
2447 // IsIneligibleOrNotSelected, ImplicitReturnZero, Constexpr,
2448 // UsesSEHTry, SkippedBody, MultiVersion, LateParsed,
2449 // FriendConstraintRefersToEnclosingTemplate, Linkage,
2450 // ShouldSkipCheckingODR
2451 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LocEnd
2452 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // ODRHash
2453 // This Array slurps the rest of the record. Fortunately we want to encode
2454 // (nearly) all the remaining (variable number of) fields in the same way.
2455 //
2456 // This is:
2457 // NumParams and Params[] from FunctionDecl, and
2458 // NumOverriddenMethods, OverriddenMethods[] from CXXMethodDecl.
2459 //
2460 // Add an AbbrevOp for 'size then elements' and use it here.
2461 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2462 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2463 return Abv;
2464 }
2465
2466 template <FunctionDecl::TemplatedKind Kind>
getCXXMethodAbbrev()2467 std::shared_ptr<llvm::BitCodeAbbrev> getCXXMethodAbbrev() {
2468 return getFunctionDeclAbbrev<Kind>(serialization::DECL_CXX_METHOD);
2469 }
2470 } // namespace
2471
WriteDeclAbbrevs()2472 void ASTWriter::WriteDeclAbbrevs() {
2473 using namespace llvm;
2474
2475 std::shared_ptr<BitCodeAbbrev> Abv;
2476
2477 // Abbreviation for DECL_FIELD
2478 Abv = std::make_shared<BitCodeAbbrev>();
2479 Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));
2480 // Decl
2481 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2482 7)); // Packed DeclBits: ModuleOwnershipKind,
2483 // isUsed, isReferenced, AccessSpecifier,
2484 //
2485 // The following bits should be 0:
2486 // isImplicit, HasStandaloneLexicalDC, HasAttrs,
2487 // TopLevelDeclInObjCContainer,
2488 // isInvalidDecl
2489 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2490 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2491 // NamedDecl
2492 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2493 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2494 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2495 // ValueDecl
2496 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2497 // DeclaratorDecl
2498 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2499 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2500 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2501 // FieldDecl
2502 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
2503 Abv->Add(BitCodeAbbrevOp(0)); // StorageKind
2504 // Type Source Info
2505 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2506 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2507 DeclFieldAbbrev = Stream.EmitAbbrev(std::move(Abv));
2508
2509 // Abbreviation for DECL_OBJC_IVAR
2510 Abv = std::make_shared<BitCodeAbbrev>();
2511 Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));
2512 // Decl
2513 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2514 12)); // Packed DeclBits: HasStandaloneLexicalDC,
2515 // isInvalidDecl, HasAttrs, isImplicit, isUsed,
2516 // isReferenced, TopLevelDeclInObjCContainer,
2517 // AccessSpecifier, ModuleOwnershipKind
2518 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2519 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2520 // NamedDecl
2521 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2522 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2523 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2524 // ValueDecl
2525 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2526 // DeclaratorDecl
2527 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2528 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2529 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2530 // FieldDecl
2531 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
2532 Abv->Add(BitCodeAbbrevOp(0)); // InitStyle
2533 // ObjC Ivar
2534 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl
2535 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize
2536 // Type Source Info
2537 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2538 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2539 DeclObjCIvarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2540
2541 // Abbreviation for DECL_ENUM
2542 Abv = std::make_shared<BitCodeAbbrev>();
2543 Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));
2544 // Redeclarable
2545 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2546 // Decl
2547 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2548 7)); // Packed DeclBits: ModuleOwnershipKind,
2549 // isUsed, isReferenced, AccessSpecifier,
2550 //
2551 // The following bits should be 0:
2552 // isImplicit, HasStandaloneLexicalDC, HasAttrs,
2553 // TopLevelDeclInObjCContainer,
2554 // isInvalidDecl
2555 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2556 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2557 // NamedDecl
2558 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2559 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2560 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2561 // TypeDecl
2562 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2563 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2564 // TagDecl
2565 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace
2566 Abv->Add(BitCodeAbbrevOp(
2567 BitCodeAbbrevOp::Fixed,
2568 9)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,
2569 // EmbeddedInDeclarator, IsFreeStanding,
2570 // isCompleteDefinitionRequired, ExtInfoKind
2571 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2572 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2573 // EnumDecl
2574 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddTypeRef
2575 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IntegerType
2576 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getPromotionType
2577 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 20)); // Enum Decl Bits
2578 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));// ODRHash
2579 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedMembEnum
2580 // DC
2581 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset
2582 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset
2583 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ModuleLocalOffset
2584 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TULocalOffset
2585 DeclEnumAbbrev = Stream.EmitAbbrev(std::move(Abv));
2586
2587 // Abbreviation for DECL_RECORD
2588 Abv = std::make_shared<BitCodeAbbrev>();
2589 Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));
2590 // Redeclarable
2591 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2592 // Decl
2593 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2594 7)); // Packed DeclBits: ModuleOwnershipKind,
2595 // isUsed, isReferenced, AccessSpecifier,
2596 //
2597 // The following bits should be 0:
2598 // isImplicit, HasStandaloneLexicalDC, HasAttrs,
2599 // TopLevelDeclInObjCContainer,
2600 // isInvalidDecl
2601 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2602 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2603 // NamedDecl
2604 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2605 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2606 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2607 // TypeDecl
2608 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2609 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2610 // TagDecl
2611 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace
2612 Abv->Add(BitCodeAbbrevOp(
2613 BitCodeAbbrevOp::Fixed,
2614 9)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,
2615 // EmbeddedInDeclarator, IsFreeStanding,
2616 // isCompleteDefinitionRequired, ExtInfoKind
2617 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2618 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2619 // RecordDecl
2620 Abv->Add(BitCodeAbbrevOp(
2621 BitCodeAbbrevOp::Fixed,
2622 14)); // Packed Record Decl Bits: FlexibleArrayMember,
2623 // AnonymousStructUnion, hasObjectMember, hasVolatileMember,
2624 // isNonTrivialToPrimitiveDefaultInitialize,
2625 // isNonTrivialToPrimitiveCopy, isNonTrivialToPrimitiveDestroy,
2626 // hasNonTrivialToPrimitiveDefaultInitializeCUnion,
2627 // hasNonTrivialToPrimitiveDestructCUnion,
2628 // hasNonTrivialToPrimitiveCopyCUnion,
2629 // hasUninitializedExplicitInitFields, isParamDestroyedInCallee,
2630 // getArgPassingRestrictions
2631 // ODRHash
2632 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 26));
2633
2634 // DC
2635 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset
2636 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset
2637 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ModuleLocalOffset
2638 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TULocalOffset
2639 DeclRecordAbbrev = Stream.EmitAbbrev(std::move(Abv));
2640
2641 // Abbreviation for DECL_PARM_VAR
2642 Abv = std::make_shared<BitCodeAbbrev>();
2643 Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));
2644 // Redeclarable
2645 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2646 // Decl
2647 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2648 8)); // Packed DeclBits: ModuleOwnershipKind, isUsed,
2649 // isReferenced, AccessSpecifier,
2650 // HasStandaloneLexicalDC, HasAttrs, isImplicit,
2651 // TopLevelDeclInObjCContainer,
2652 // isInvalidDecl,
2653 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2654 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2655 // NamedDecl
2656 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2657 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2658 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2659 // ValueDecl
2660 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2661 // DeclaratorDecl
2662 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2663 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2664 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2665 // VarDecl
2666 Abv->Add(
2667 BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2668 12)); // Packed Var Decl bits: SClass, TSCSpec, InitStyle,
2669 // isARCPseudoStrong, Linkage, ModulesCodegen
2670 Abv->Add(BitCodeAbbrevOp(0)); // VarKind (local enum)
2671 // ParmVarDecl
2672 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex
2673 Abv->Add(BitCodeAbbrevOp(
2674 BitCodeAbbrevOp::Fixed,
2675 19)); // Packed Parm Var Decl bits: IsObjCMethodParameter, ScopeDepth,
2676 // ObjCDeclQualifier, KNRPromoted,
2677 // HasInheritedDefaultArg, HasUninstantiatedDefaultArg
2678 // Type Source Info
2679 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2680 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2681 DeclParmVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2682
2683 // Abbreviation for DECL_TYPEDEF
2684 Abv = std::make_shared<BitCodeAbbrev>();
2685 Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));
2686 // Redeclarable
2687 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2688 // Decl
2689 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2690 7)); // Packed DeclBits: ModuleOwnershipKind,
2691 // isReferenced, isUsed, AccessSpecifier. Other
2692 // higher bits should be 0: isImplicit,
2693 // HasStandaloneLexicalDC, HasAttrs,
2694 // TopLevelDeclInObjCContainer, isInvalidDecl
2695 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2696 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2697 // NamedDecl
2698 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2699 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2700 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2701 // TypeDecl
2702 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2703 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2704 // TypedefDecl
2705 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2706 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2707 DeclTypedefAbbrev = Stream.EmitAbbrev(std::move(Abv));
2708
2709 // Abbreviation for DECL_VAR
2710 Abv = std::make_shared<BitCodeAbbrev>();
2711 Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));
2712 // Redeclarable
2713 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2714 // Decl
2715 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2716 12)); // Packed DeclBits: HasStandaloneLexicalDC,
2717 // isInvalidDecl, HasAttrs, isImplicit, isUsed,
2718 // isReferenced, TopLevelDeclInObjCContainer,
2719 // AccessSpecifier, ModuleOwnershipKind
2720 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2721 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2722 // NamedDecl
2723 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2724 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2725 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2726 // ValueDecl
2727 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2728 // DeclaratorDecl
2729 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2730 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2731 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2732 // VarDecl
2733 Abv->Add(BitCodeAbbrevOp(
2734 BitCodeAbbrevOp::Fixed,
2735 22)); // Packed Var Decl bits: Linkage, ModulesCodegen,
2736 // SClass, TSCSpec, InitStyle,
2737 // isARCPseudoStrong, IsThisDeclarationADemotedDefinition,
2738 // isExceptionVariable, isNRVOVariable, isCXXForRangeDecl,
2739 // isInline, isInlineSpecified, isConstexpr,
2740 // isInitCapture, isPrevDeclInSameScope, hasInitWithSideEffects,
2741 // EscapingByref, HasDeducedType, ImplicitParamKind, isObjCForDecl
2742 // IsCXXForRangeImplicitVar
2743 Abv->Add(BitCodeAbbrevOp(0)); // VarKind (local enum)
2744 // Type Source Info
2745 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2746 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2747 DeclVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2748
2749 // Abbreviation for DECL_CXX_METHOD
2750 DeclCXXMethodAbbrev =
2751 Stream.EmitAbbrev(getCXXMethodAbbrev<FunctionDecl::TK_NonTemplate>());
2752 DeclTemplateCXXMethodAbbrev = Stream.EmitAbbrev(
2753 getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplate>());
2754 DeclDependentNonTemplateCXXMethodAbbrev = Stream.EmitAbbrev(
2755 getCXXMethodAbbrev<FunctionDecl::TK_DependentNonTemplate>());
2756 DeclMemberSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(
2757 getCXXMethodAbbrev<FunctionDecl::TK_MemberSpecialization>());
2758 DeclTemplateSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(
2759 getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplateSpecialization>());
2760 DeclDependentSpecializationCXXMethodAbbrev = Stream.EmitAbbrev(
2761 getCXXMethodAbbrev<
2762 FunctionDecl::TK_DependentFunctionTemplateSpecialization>());
2763
2764 // Abbreviation for DECL_TEMPLATE_TYPE_PARM
2765 Abv = std::make_shared<BitCodeAbbrev>();
2766 Abv->Add(BitCodeAbbrevOp(serialization::DECL_TEMPLATE_TYPE_PARM));
2767 Abv->Add(BitCodeAbbrevOp(0)); // hasTypeConstraint
2768 // Decl
2769 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2770 7)); // Packed DeclBits: ModuleOwnershipKind,
2771 // isReferenced, isUsed, AccessSpecifier. Other
2772 // higher bits should be 0: isImplicit,
2773 // HasStandaloneLexicalDC, HasAttrs,
2774 // TopLevelDeclInObjCContainer, isInvalidDecl
2775 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2776 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2777 // NamedDecl
2778 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2779 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2780 Abv->Add(BitCodeAbbrevOp(0));
2781 // TypeDecl
2782 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2783 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2784 // TemplateTypeParmDecl
2785 Abv->Add(
2786 BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // wasDeclaredWithTypename
2787 Abv->Add(BitCodeAbbrevOp(0)); // OwnsDefaultArg
2788 DeclTemplateTypeParmAbbrev = Stream.EmitAbbrev(std::move(Abv));
2789
2790 // Abbreviation for DECL_USING_SHADOW
2791 Abv = std::make_shared<BitCodeAbbrev>();
2792 Abv->Add(BitCodeAbbrevOp(serialization::DECL_USING_SHADOW));
2793 // Redeclarable
2794 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2795 // Decl
2796 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2797 12)); // Packed DeclBits: HasStandaloneLexicalDC,
2798 // isInvalidDecl, HasAttrs, isImplicit, isUsed,
2799 // isReferenced, TopLevelDeclInObjCContainer,
2800 // AccessSpecifier, ModuleOwnershipKind
2801 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2802 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2803 // NamedDecl
2804 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2805 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2806 Abv->Add(BitCodeAbbrevOp(0));
2807 // UsingShadowDecl
2808 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TargetDecl
2809 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS
2810 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // UsingOrNextShadow
2811 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR,
2812 6)); // InstantiatedFromUsingShadowDecl
2813 DeclUsingShadowAbbrev = Stream.EmitAbbrev(std::move(Abv));
2814
2815 // Abbreviation for EXPR_DECL_REF
2816 Abv = std::make_shared<BitCodeAbbrev>();
2817 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));
2818 // Stmt
2819 // Expr
2820 // PackingBits: DependenceKind, ValueKind. ObjectKind should be 0.
2821 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2822 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2823 // DeclRefExpr
2824 // Packing Bits: , HadMultipleCandidates, RefersToEnclosingVariableOrCapture,
2825 // IsImmediateEscalating, NonOdrUseReason.
2826 // GetDeclFound, HasQualifier and ExplicitTemplateArgs should be 0.
2827 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));
2828 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef
2829 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2830 DeclRefExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2831
2832 // Abbreviation for EXPR_INTEGER_LITERAL
2833 Abv = std::make_shared<BitCodeAbbrev>();
2834 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));
2835 //Stmt
2836 // Expr
2837 // DependenceKind, ValueKind, ObjectKind
2838 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2839 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2840 // Integer Literal
2841 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2842 Abv->Add(BitCodeAbbrevOp(32)); // Bit Width
2843 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value
2844 IntegerLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
2845
2846 // Abbreviation for EXPR_CHARACTER_LITERAL
2847 Abv = std::make_shared<BitCodeAbbrev>();
2848 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));
2849 //Stmt
2850 // Expr
2851 // DependenceKind, ValueKind, ObjectKind
2852 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2853 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2854 // Character Literal
2855 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue
2856 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2857 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // getKind
2858 CharacterLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
2859
2860 // Abbreviation for EXPR_IMPLICIT_CAST
2861 Abv = std::make_shared<BitCodeAbbrev>();
2862 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_IMPLICIT_CAST));
2863 // Stmt
2864 // Expr
2865 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2866 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2867 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2868 // CastExpr
2869 Abv->Add(BitCodeAbbrevOp(0)); // PathSize
2870 // Packing Bits: CastKind, StoredFPFeatures, isPartOfExplicitCast
2871 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 9));
2872 // ImplicitCastExpr
2873 ExprImplicitCastAbbrev = Stream.EmitAbbrev(std::move(Abv));
2874
2875 // Abbreviation for EXPR_BINARY_OPERATOR
2876 Abv = std::make_shared<BitCodeAbbrev>();
2877 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_BINARY_OPERATOR));
2878 // Stmt
2879 // Expr
2880 // Packing Bits: DependenceKind. ValueKind and ObjectKind should
2881 // be 0 in this case.
2882 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));
2883 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2884 // BinaryOperator
2885 Abv->Add(
2886 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures
2887 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2888 BinaryOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));
2889
2890 // Abbreviation for EXPR_COMPOUND_ASSIGN_OPERATOR
2891 Abv = std::make_shared<BitCodeAbbrev>();
2892 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_COMPOUND_ASSIGN_OPERATOR));
2893 // Stmt
2894 // Expr
2895 // Packing Bits: DependenceKind. ValueKind and ObjectKind should
2896 // be 0 in this case.
2897 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));
2898 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2899 // BinaryOperator
2900 // Packing Bits: OpCode. The HasFPFeatures bit should be 0
2901 Abv->Add(
2902 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures
2903 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2904 // CompoundAssignOperator
2905 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHSType
2906 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Result Type
2907 CompoundAssignOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));
2908
2909 // Abbreviation for EXPR_CALL
2910 Abv = std::make_shared<BitCodeAbbrev>();
2911 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CALL));
2912 // Stmt
2913 // Expr
2914 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2915 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2916 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2917 // CallExpr
2918 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs
2919 Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind
2920 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2921 CallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2922
2923 // Abbreviation for EXPR_CXX_OPERATOR_CALL
2924 Abv = std::make_shared<BitCodeAbbrev>();
2925 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_OPERATOR_CALL));
2926 // Stmt
2927 // Expr
2928 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2929 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2930 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2931 // CallExpr
2932 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs
2933 Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind
2934 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2935 // CXXOperatorCallExpr
2936 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Operator Kind
2937 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2938 CXXOperatorCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2939
2940 // Abbreviation for EXPR_CXX_MEMBER_CALL
2941 Abv = std::make_shared<BitCodeAbbrev>();
2942 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_MEMBER_CALL));
2943 // Stmt
2944 // Expr
2945 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2946 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2947 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2948 // CallExpr
2949 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs
2950 Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind
2951 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2952 // CXXMemberCallExpr
2953 CXXMemberCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2954
2955 // Abbreviation for STMT_COMPOUND
2956 Abv = std::make_shared<BitCodeAbbrev>();
2957 Abv->Add(BitCodeAbbrevOp(serialization::STMT_COMPOUND));
2958 // Stmt
2959 // CompoundStmt
2960 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Num Stmts
2961 Abv->Add(BitCodeAbbrevOp(0)); // hasStoredFPFeatures
2962 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2963 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2964 CompoundStmtAbbrev = Stream.EmitAbbrev(std::move(Abv));
2965
2966 Abv = std::make_shared<BitCodeAbbrev>();
2967 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));
2968 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2969 DeclContextLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
2970
2971 Abv = std::make_shared<BitCodeAbbrev>();
2972 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));
2973 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2974 DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));
2975
2976 Abv = std::make_shared<BitCodeAbbrev>();
2977 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_MODULE_LOCAL_VISIBLE));
2978 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2979 DeclModuleLocalVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));
2980
2981 Abv = std::make_shared<BitCodeAbbrev>();
2982 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_TU_LOCAL_VISIBLE));
2983 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2984 DeclTULocalLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));
2985
2986 Abv = std::make_shared<BitCodeAbbrev>();
2987 Abv->Add(BitCodeAbbrevOp(serialization::DECL_SPECIALIZATIONS));
2988 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2989 DeclSpecializationsAbbrev = Stream.EmitAbbrev(std::move(Abv));
2990
2991 Abv = std::make_shared<BitCodeAbbrev>();
2992 Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARTIAL_SPECIALIZATIONS));
2993 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2994 DeclPartialSpecializationsAbbrev = Stream.EmitAbbrev(std::move(Abv));
2995 }
2996
2997 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
2998 /// consumers of the AST.
2999 ///
3000 /// Such decls will always be deserialized from the AST file, so we would like
3001 /// this to be as restrictive as possible. Currently the predicate is driven by
3002 /// code generation requirements, if other clients have a different notion of
3003 /// what is "required" then we may have to consider an alternate scheme where
3004 /// clients can iterate over the top-level decls and get information on them,
3005 /// without necessary deserializing them. We could explicitly require such
3006 /// clients to use a separate API call to "realize" the decl. This should be
3007 /// relatively painless since they would presumably only do it for top-level
3008 /// decls.
isRequiredDecl(const Decl * D,ASTContext & Context,Module * WritingModule)3009 static bool isRequiredDecl(const Decl *D, ASTContext &Context,
3010 Module *WritingModule) {
3011 // Named modules have different semantics than header modules. Every named
3012 // module units owns a translation unit. So the importer of named modules
3013 // doesn't need to deserilize everything ahead of time.
3014 if (WritingModule && WritingModule->isNamedModule()) {
3015 // The PragmaCommentDecl and PragmaDetectMismatchDecl are MSVC's extension.
3016 // And the behavior of MSVC for such cases will leak this to the module
3017 // users. Given pragma is not a standard thing, the compiler has the space
3018 // to do their own decision. Let's follow MSVC here.
3019 if (isa<PragmaCommentDecl, PragmaDetectMismatchDecl>(D))
3020 return true;
3021 return false;
3022 }
3023
3024 // An ObjCMethodDecl is never considered as "required" because its
3025 // implementation container always is.
3026
3027 // File scoped assembly or obj-c or OMP declare target implementation must be
3028 // seen.
3029 if (isa<FileScopeAsmDecl, TopLevelStmtDecl, ObjCImplDecl>(D))
3030 return true;
3031
3032 if (WritingModule && isPartOfPerModuleInitializer(D)) {
3033 // These declarations are part of the module initializer, and are emitted
3034 // if and when the module is imported, rather than being emitted eagerly.
3035 return false;
3036 }
3037
3038 return Context.DeclMustBeEmitted(D);
3039 }
3040
WriteDecl(ASTContext & Context,Decl * D)3041 void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
3042 PrettyDeclStackTraceEntry CrashInfo(Context, D, SourceLocation(),
3043 "serializing");
3044
3045 // Determine the ID for this declaration.
3046 LocalDeclID ID;
3047 assert(!D->isFromASTFile() && "should not be emitting imported decl");
3048 LocalDeclID &IDR = DeclIDs[D];
3049 if (IDR.isInvalid())
3050 IDR = NextDeclID++;
3051
3052 ID = IDR;
3053
3054 assert(ID >= FirstDeclID && "invalid decl ID");
3055
3056 RecordData Record;
3057 ASTDeclWriter W(*this, Context, Record, GeneratingReducedBMI);
3058
3059 // Build a record for this declaration
3060 W.Visit(D);
3061
3062 // Emit this declaration to the bitstream.
3063 uint64_t Offset = W.Emit(D);
3064
3065 // Record the offset for this declaration
3066 SourceLocation Loc = D->getLocation();
3067 SourceLocationEncoding::RawLocEncoding RawLoc =
3068 getRawSourceLocationEncoding(getAdjustedLocation(Loc));
3069
3070 unsigned Index = ID.getRawValue() - FirstDeclID.getRawValue();
3071 if (DeclOffsets.size() == Index)
3072 DeclOffsets.emplace_back(RawLoc, Offset, DeclTypesBlockStartOffset);
3073 else if (DeclOffsets.size() < Index) {
3074 // FIXME: Can/should this happen?
3075 DeclOffsets.resize(Index+1);
3076 DeclOffsets[Index].setRawLoc(RawLoc);
3077 DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);
3078 } else {
3079 llvm_unreachable("declarations should be emitted in ID order");
3080 }
3081
3082 SourceManager &SM = Context.getSourceManager();
3083 if (Loc.isValid() && SM.isLocalSourceLocation(Loc))
3084 associateDeclWithFile(D, ID);
3085
3086 // Note declarations that should be deserialized eagerly so that we can add
3087 // them to a record in the AST file later.
3088 if (isRequiredDecl(D, Context, WritingModule))
3089 AddDeclRef(D, EagerlyDeserializedDecls);
3090 }
3091
AddFunctionDefinition(const FunctionDecl * FD)3092 void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {
3093 // Switch case IDs are per function body.
3094 Writer->ClearSwitchCaseIDs();
3095
3096 assert(FD->doesThisDeclarationHaveABody());
3097 bool ModulesCodegen = shouldFunctionGenerateHereOnly(FD);
3098 Record->push_back(ModulesCodegen);
3099 if (ModulesCodegen)
3100 Writer->AddDeclRef(FD, Writer->ModularCodegenDecls);
3101 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
3102 Record->push_back(CD->getNumCtorInitializers());
3103 if (CD->getNumCtorInitializers())
3104 AddCXXCtorInitializers(llvm::ArrayRef(CD->init_begin(), CD->init_end()));
3105 }
3106 AddStmt(FD->getBody());
3107 }
3108