[Flang] remove whole-archive option for AIX linker (#76039)
[llvm-project.git] / clang / lib / Serialization / ASTWriterDecl.cpp
blob2554abc682a1d62ba1ebc4dc663224c4cce5404a
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.
11 //===----------------------------------------------------------------------===//
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 #include <optional>
27 using namespace clang;
28 using namespace serialization;
30 //===----------------------------------------------------------------------===//
31 // Declaration serialization
32 //===----------------------------------------------------------------------===//
34 namespace clang {
35 class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {
36 ASTWriter &Writer;
37 ASTContext &Context;
38 ASTRecordWriter Record;
40 serialization::DeclCode Code;
41 unsigned AbbrevToUse;
43 public:
44 ASTDeclWriter(ASTWriter &Writer, ASTContext &Context,
45 ASTWriter::RecordDataImpl &Record)
46 : Writer(Writer), Context(Context), Record(Writer, Record),
47 Code((serialization::DeclCode)0), AbbrevToUse(0) {}
49 uint64_t Emit(Decl *D) {
50 if (!Code)
51 llvm::report_fatal_error(StringRef("unexpected declaration kind '") +
52 D->getDeclKindName() + "'");
53 return Record.Emit(Code, AbbrevToUse);
56 void Visit(Decl *D);
58 void VisitDecl(Decl *D);
59 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
60 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
61 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62 void VisitNamedDecl(NamedDecl *D);
63 void VisitLabelDecl(LabelDecl *LD);
64 void VisitNamespaceDecl(NamespaceDecl *D);
65 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
66 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
67 void VisitTypeDecl(TypeDecl *D);
68 void VisitTypedefNameDecl(TypedefNameDecl *D);
69 void VisitTypedefDecl(TypedefDecl *D);
70 void VisitTypeAliasDecl(TypeAliasDecl *D);
71 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
72 void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);
73 void VisitTagDecl(TagDecl *D);
74 void VisitEnumDecl(EnumDecl *D);
75 void VisitRecordDecl(RecordDecl *D);
76 void VisitCXXRecordDecl(CXXRecordDecl *D);
77 void VisitClassTemplateSpecializationDecl(
78 ClassTemplateSpecializationDecl *D);
79 void VisitClassTemplatePartialSpecializationDecl(
80 ClassTemplatePartialSpecializationDecl *D);
81 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
82 void VisitVarTemplatePartialSpecializationDecl(
83 VarTemplatePartialSpecializationDecl *D);
84 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
85 void VisitValueDecl(ValueDecl *D);
86 void VisitEnumConstantDecl(EnumConstantDecl *D);
87 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
88 void VisitDeclaratorDecl(DeclaratorDecl *D);
89 void VisitFunctionDecl(FunctionDecl *D);
90 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D);
91 void VisitCXXMethodDecl(CXXMethodDecl *D);
92 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
93 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
94 void VisitCXXConversionDecl(CXXConversionDecl *D);
95 void VisitFieldDecl(FieldDecl *D);
96 void VisitMSPropertyDecl(MSPropertyDecl *D);
97 void VisitMSGuidDecl(MSGuidDecl *D);
98 void VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl *D);
99 void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);
100 void VisitIndirectFieldDecl(IndirectFieldDecl *D);
101 void VisitVarDecl(VarDecl *D);
102 void VisitImplicitParamDecl(ImplicitParamDecl *D);
103 void VisitParmVarDecl(ParmVarDecl *D);
104 void VisitDecompositionDecl(DecompositionDecl *D);
105 void VisitBindingDecl(BindingDecl *D);
106 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
107 void VisitTemplateDecl(TemplateDecl *D);
108 void VisitConceptDecl(ConceptDecl *D);
109 void VisitImplicitConceptSpecializationDecl(
110 ImplicitConceptSpecializationDecl *D);
111 void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);
112 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
113 void VisitClassTemplateDecl(ClassTemplateDecl *D);
114 void VisitVarTemplateDecl(VarTemplateDecl *D);
115 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
116 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
117 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
118 void VisitUsingDecl(UsingDecl *D);
119 void VisitUsingEnumDecl(UsingEnumDecl *D);
120 void VisitUsingPackDecl(UsingPackDecl *D);
121 void VisitUsingShadowDecl(UsingShadowDecl *D);
122 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
123 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
124 void VisitExportDecl(ExportDecl *D);
125 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
126 void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);
127 void VisitImportDecl(ImportDecl *D);
128 void VisitAccessSpecDecl(AccessSpecDecl *D);
129 void VisitFriendDecl(FriendDecl *D);
130 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
131 void VisitStaticAssertDecl(StaticAssertDecl *D);
132 void VisitBlockDecl(BlockDecl *D);
133 void VisitCapturedDecl(CapturedDecl *D);
134 void VisitEmptyDecl(EmptyDecl *D);
135 void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
136 void VisitDeclContext(DeclContext *DC);
137 template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
138 void VisitHLSLBufferDecl(HLSLBufferDecl *D);
140 // FIXME: Put in the same order is DeclNodes.td?
141 void VisitObjCMethodDecl(ObjCMethodDecl *D);
142 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
143 void VisitObjCContainerDecl(ObjCContainerDecl *D);
144 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
145 void VisitObjCIvarDecl(ObjCIvarDecl *D);
146 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
147 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
148 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
149 void VisitObjCImplDecl(ObjCImplDecl *D);
150 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
151 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
152 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
153 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
154 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
155 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
156 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
157 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
158 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
159 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
160 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
162 /// Add an Objective-C type parameter list to the given record.
163 void AddObjCTypeParamList(ObjCTypeParamList *typeParams) {
164 // Empty type parameter list.
165 if (!typeParams) {
166 Record.push_back(0);
167 return;
170 Record.push_back(typeParams->size());
171 for (auto *typeParam : *typeParams) {
172 Record.AddDeclRef(typeParam);
174 Record.AddSourceLocation(typeParams->getLAngleLoc());
175 Record.AddSourceLocation(typeParams->getRAngleLoc());
178 /// Add to the record the first declaration from each module file that
179 /// provides a declaration of D. The intent is to provide a sufficient
180 /// set such that reloading this set will load all current redeclarations.
181 void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) {
182 llvm::MapVector<ModuleFile*, const Decl*> Firsts;
183 // FIXME: We can skip entries that we know are implied by others.
184 for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) {
185 if (R->isFromASTFile())
186 Firsts[Writer.Chain->getOwningModuleFile(R)] = R;
187 else if (IncludeLocal)
188 Firsts[nullptr] = R;
190 for (const auto &F : Firsts)
191 Record.AddDeclRef(F.second);
194 /// Get the specialization decl from an entry in the specialization list.
195 template <typename EntryType>
196 typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
197 getSpecializationDecl(EntryType &T) {
198 return RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::getDecl(&T);
201 /// Get the list of partial specializations from a template's common ptr.
202 template<typename T>
203 decltype(T::PartialSpecializations) &getPartialSpecializations(T *Common) {
204 return Common->PartialSpecializations;
206 ArrayRef<Decl> getPartialSpecializations(FunctionTemplateDecl::Common *) {
207 return std::nullopt;
210 template<typename DeclTy>
211 void AddTemplateSpecializations(DeclTy *D) {
212 auto *Common = D->getCommonPtr();
214 // If we have any lazy specializations, and the external AST source is
215 // our chained AST reader, we can just write out the DeclIDs. Otherwise,
216 // we need to resolve them to actual declarations.
217 if (Writer.Chain != Writer.Context->getExternalSource() &&
218 Common->LazySpecializations) {
219 D->LoadLazySpecializations();
220 assert(!Common->LazySpecializations);
223 ArrayRef<DeclID> LazySpecializations;
224 if (auto *LS = Common->LazySpecializations)
225 LazySpecializations = llvm::ArrayRef(LS + 1, LS[0]);
227 // Add a slot to the record for the number of specializations.
228 unsigned I = Record.size();
229 Record.push_back(0);
231 // AddFirstDeclFromEachModule might trigger deserialization, invalidating
232 // *Specializations iterators.
233 llvm::SmallVector<const Decl*, 16> Specs;
234 for (auto &Entry : Common->Specializations)
235 Specs.push_back(getSpecializationDecl(Entry));
236 for (auto &Entry : getPartialSpecializations(Common))
237 Specs.push_back(getSpecializationDecl(Entry));
239 for (auto *D : Specs) {
240 assert(D->isCanonicalDecl() && "non-canonical decl in set");
241 AddFirstDeclFromEachModule(D, /*IncludeLocal*/true);
243 Record.append(LazySpecializations.begin(), LazySpecializations.end());
245 // Update the size entry we added earlier.
246 Record[I] = Record.size() - I - 1;
249 /// Ensure that this template specialization is associated with the specified
250 /// template on reload.
251 void RegisterTemplateSpecialization(const Decl *Template,
252 const Decl *Specialization) {
253 Template = Template->getCanonicalDecl();
255 // If the canonical template is local, we'll write out this specialization
256 // when we emit it.
257 // FIXME: We can do the same thing if there is any local declaration of
258 // the template, to avoid emitting an update record.
259 if (!Template->isFromASTFile())
260 return;
262 // We only need to associate the first local declaration of the
263 // specialization. The other declarations will get pulled in by it.
264 if (Writer.getFirstLocalDecl(Specialization) != Specialization)
265 return;
267 Writer.DeclUpdates[Template].push_back(ASTWriter::DeclUpdate(
268 UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, Specialization));
273 void ASTDeclWriter::Visit(Decl *D) {
274 DeclVisitor<ASTDeclWriter>::Visit(D);
276 // Source locations require array (variable-length) abbreviations. The
277 // abbreviation infrastructure requires that arrays are encoded last, so
278 // we handle it here in the case of those classes derived from DeclaratorDecl
279 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
280 if (auto *TInfo = DD->getTypeSourceInfo())
281 Record.AddTypeLoc(TInfo->getTypeLoc());
284 // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
285 // have been written. We want it last because we will not read it back when
286 // retrieving it from the AST, we'll just lazily set the offset.
287 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
288 Record.push_back(FD->doesThisDeclarationHaveABody());
289 if (FD->doesThisDeclarationHaveABody())
290 Record.AddFunctionDefinition(FD);
293 // Similar to FunctionDecls, handle VarDecl's initializer here and write it
294 // after all other Stmts/Exprs. We will not read the initializer until after
295 // we have finished recursive deserialization, because it can recursively
296 // refer back to the variable.
297 if (auto *VD = dyn_cast<VarDecl>(D)) {
298 Record.AddVarDeclInit(VD);
301 // And similarly for FieldDecls. We already serialized whether there is a
302 // default member initializer.
303 if (auto *FD = dyn_cast<FieldDecl>(D)) {
304 if (FD->hasInClassInitializer()) {
305 if (Expr *Init = FD->getInClassInitializer()) {
306 Record.push_back(1);
307 Record.AddStmt(Init);
308 } else {
309 Record.push_back(0);
310 // Initializer has not been instantiated yet.
315 // If this declaration is also a DeclContext, write blocks for the
316 // declarations that lexically stored inside its context and those
317 // declarations that are visible from its context.
318 if (auto *DC = dyn_cast<DeclContext>(D))
319 VisitDeclContext(DC);
322 void ASTDeclWriter::VisitDecl(Decl *D) {
323 BitsPacker DeclBits;
325 // The order matters here. It will be better to put the bit with higher
326 // probability to be 0 in the end of the bits.
328 // Since we're using VBR6 format to store it.
329 // It will be pretty effient if all the higher bits are 0.
330 // For example, if we need to pack 8 bits into a value and the stored value
331 // is 0xf0, the actual stored value will be 0b000111'110000, which takes 12
332 // bits actually. However, if we changed the order to be 0x0f, then we can
333 // store it as 0b001111, which takes 6 bits only now.
334 DeclBits.addBits((uint64_t)D->getModuleOwnershipKind(), /*BitWidth=*/3);
335 DeclBits.addBit(D->isReferenced());
336 DeclBits.addBit(D->isUsed(false));
337 DeclBits.addBits(D->getAccess(), /*BitWidth=*/2);
338 DeclBits.addBit(D->isImplicit());
339 DeclBits.addBit(D->getDeclContext() != D->getLexicalDeclContext());
340 DeclBits.addBit(D->hasAttrs());
341 DeclBits.addBit(D->isTopLevelDeclInObjCContainer());
342 DeclBits.addBit(D->isInvalidDecl());
343 Record.push_back(DeclBits);
345 Record.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()));
346 if (D->getDeclContext() != D->getLexicalDeclContext())
347 Record.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()));
349 if (D->hasAttrs())
350 Record.AddAttributes(D->getAttrs());
352 Record.push_back(Writer.getSubmoduleID(D->getOwningModule()));
354 // If this declaration injected a name into a context different from its
355 // lexical context, and that context is an imported namespace, we need to
356 // update its visible declarations to include this name.
358 // This happens when we instantiate a class with a friend declaration or a
359 // function with a local extern declaration, for instance.
361 // FIXME: Can we handle this in AddedVisibleDecl instead?
362 if (D->isOutOfLine()) {
363 auto *DC = D->getDeclContext();
364 while (auto *NS = dyn_cast<NamespaceDecl>(DC->getRedeclContext())) {
365 if (!NS->isFromASTFile())
366 break;
367 Writer.UpdatedDeclContexts.insert(NS->getPrimaryContext());
368 if (!NS->isInlineNamespace())
369 break;
370 DC = NS->getParent();
375 void ASTDeclWriter::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
376 StringRef Arg = D->getArg();
377 Record.push_back(Arg.size());
378 VisitDecl(D);
379 Record.AddSourceLocation(D->getBeginLoc());
380 Record.push_back(D->getCommentKind());
381 Record.AddString(Arg);
382 Code = serialization::DECL_PRAGMA_COMMENT;
385 void ASTDeclWriter::VisitPragmaDetectMismatchDecl(
386 PragmaDetectMismatchDecl *D) {
387 StringRef Name = D->getName();
388 StringRef Value = D->getValue();
389 Record.push_back(Name.size() + 1 + Value.size());
390 VisitDecl(D);
391 Record.AddSourceLocation(D->getBeginLoc());
392 Record.AddString(Name);
393 Record.AddString(Value);
394 Code = serialization::DECL_PRAGMA_DETECT_MISMATCH;
397 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
398 llvm_unreachable("Translation units aren't directly serialized");
401 void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {
402 VisitDecl(D);
403 Record.AddDeclarationName(D->getDeclName());
404 Record.push_back(needsAnonymousDeclarationNumber(D)
405 ? Writer.getAnonymousDeclarationNumber(D)
406 : 0);
409 void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {
410 VisitNamedDecl(D);
411 Record.AddSourceLocation(D->getBeginLoc());
412 Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
415 void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {
416 VisitRedeclarable(D);
417 VisitTypeDecl(D);
418 Record.AddTypeSourceInfo(D->getTypeSourceInfo());
419 Record.push_back(D->isModed());
420 if (D->isModed())
421 Record.AddTypeRef(D->getUnderlyingType());
422 Record.AddDeclRef(D->getAnonDeclWithTypedefName(false));
425 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
426 VisitTypedefNameDecl(D);
427 if (D->getDeclContext() == D->getLexicalDeclContext() &&
428 !D->hasAttrs() &&
429 !D->isImplicit() &&
430 D->getFirstDecl() == D->getMostRecentDecl() &&
431 !D->isInvalidDecl() &&
432 !D->isTopLevelDeclInObjCContainer() &&
433 !D->isModulePrivate() &&
434 !needsAnonymousDeclarationNumber(D) &&
435 D->getDeclName().getNameKind() == DeclarationName::Identifier)
436 AbbrevToUse = Writer.getDeclTypedefAbbrev();
438 Code = serialization::DECL_TYPEDEF;
441 void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {
442 VisitTypedefNameDecl(D);
443 Record.AddDeclRef(D->getDescribedAliasTemplate());
444 Code = serialization::DECL_TYPEALIAS;
447 void ASTDeclWriter::VisitTagDecl(TagDecl *D) {
448 static_assert(DeclContext::NumTagDeclBits == 23,
449 "You need to update the serializer after you change the "
450 "TagDeclBits");
452 VisitRedeclarable(D);
453 VisitTypeDecl(D);
454 Record.push_back(D->getIdentifierNamespace());
456 BitsPacker TagDeclBits;
457 TagDeclBits.addBits(llvm::to_underlying(D->getTagKind()), /*BitWidth=*/3);
458 TagDeclBits.addBit(!isa<CXXRecordDecl>(D) ? D->isCompleteDefinition() : 0);
459 TagDeclBits.addBit(D->isEmbeddedInDeclarator());
460 TagDeclBits.addBit(D->isFreeStanding());
461 TagDeclBits.addBit(D->isCompleteDefinitionRequired());
462 TagDeclBits.addBits(
463 D->hasExtInfo() ? 1 : (D->getTypedefNameForAnonDecl() ? 2 : 0),
464 /*BitWidth=*/2);
465 Record.push_back(TagDeclBits);
467 Record.AddSourceRange(D->getBraceRange());
469 if (D->hasExtInfo()) {
470 Record.AddQualifierInfo(*D->getExtInfo());
471 } else if (auto *TD = D->getTypedefNameForAnonDecl()) {
472 Record.AddDeclRef(TD);
473 Record.AddIdentifierRef(TD->getDeclName().getAsIdentifierInfo());
477 void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {
478 static_assert(DeclContext::NumEnumDeclBits == 43,
479 "You need to update the serializer after you change the "
480 "EnumDeclBits");
482 VisitTagDecl(D);
483 Record.AddTypeSourceInfo(D->getIntegerTypeSourceInfo());
484 if (!D->getIntegerTypeSourceInfo())
485 Record.AddTypeRef(D->getIntegerType());
486 Record.AddTypeRef(D->getPromotionType());
488 BitsPacker EnumDeclBits;
489 EnumDeclBits.addBits(D->getNumPositiveBits(), /*BitWidth=*/8);
490 EnumDeclBits.addBits(D->getNumNegativeBits(), /*BitWidth=*/8);
491 EnumDeclBits.addBit(D->isScoped());
492 EnumDeclBits.addBit(D->isScopedUsingClassTag());
493 EnumDeclBits.addBit(D->isFixed());
494 Record.push_back(EnumDeclBits);
496 Record.push_back(D->getODRHash());
498 if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {
499 Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
500 Record.push_back(MemberInfo->getTemplateSpecializationKind());
501 Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
502 } else {
503 Record.AddDeclRef(nullptr);
506 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
507 !D->isInvalidDecl() && !D->isImplicit() && !D->hasExtInfo() &&
508 !D->getTypedefNameForAnonDecl() &&
509 D->getFirstDecl() == D->getMostRecentDecl() &&
510 !D->isTopLevelDeclInObjCContainer() &&
511 !CXXRecordDecl::classofKind(D->getKind()) &&
512 !D->getIntegerTypeSourceInfo() && !D->getMemberSpecializationInfo() &&
513 !needsAnonymousDeclarationNumber(D) &&
514 D->getDeclName().getNameKind() == DeclarationName::Identifier)
515 AbbrevToUse = Writer.getDeclEnumAbbrev();
517 Code = serialization::DECL_ENUM;
520 void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {
521 static_assert(DeclContext::NumRecordDeclBits == 64,
522 "You need to update the serializer after you change the "
523 "RecordDeclBits");
525 VisitTagDecl(D);
527 BitsPacker RecordDeclBits;
528 RecordDeclBits.addBit(D->hasFlexibleArrayMember());
529 RecordDeclBits.addBit(D->isAnonymousStructOrUnion());
530 RecordDeclBits.addBit(D->hasObjectMember());
531 RecordDeclBits.addBit(D->hasVolatileMember());
532 RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDefaultInitialize());
533 RecordDeclBits.addBit(D->isNonTrivialToPrimitiveCopy());
534 RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDestroy());
535 RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDefaultInitializeCUnion());
536 RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDestructCUnion());
537 RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveCopyCUnion());
538 RecordDeclBits.addBit(D->isParamDestroyedInCallee());
539 RecordDeclBits.addBits(llvm::to_underlying(D->getArgPassingRestrictions()), 2);
540 Record.push_back(RecordDeclBits);
542 // Only compute this for C/Objective-C, in C++ this is computed as part
543 // of CXXRecordDecl.
544 if (!isa<CXXRecordDecl>(D))
545 Record.push_back(D->getODRHash());
547 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
548 !D->isImplicit() && !D->isInvalidDecl() && !D->hasExtInfo() &&
549 !D->getTypedefNameForAnonDecl() &&
550 D->getFirstDecl() == D->getMostRecentDecl() &&
551 !D->isTopLevelDeclInObjCContainer() &&
552 !CXXRecordDecl::classofKind(D->getKind()) &&
553 !needsAnonymousDeclarationNumber(D) &&
554 D->getDeclName().getNameKind() == DeclarationName::Identifier)
555 AbbrevToUse = Writer.getDeclRecordAbbrev();
557 Code = serialization::DECL_RECORD;
560 void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {
561 VisitNamedDecl(D);
562 Record.AddTypeRef(D->getType());
565 void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
566 VisitValueDecl(D);
567 Record.push_back(D->getInitExpr()? 1 : 0);
568 if (D->getInitExpr())
569 Record.AddStmt(D->getInitExpr());
570 Record.AddAPSInt(D->getInitVal());
572 Code = serialization::DECL_ENUM_CONSTANT;
575 void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
576 VisitValueDecl(D);
577 Record.AddSourceLocation(D->getInnerLocStart());
578 Record.push_back(D->hasExtInfo());
579 if (D->hasExtInfo()) {
580 DeclaratorDecl::ExtInfo *Info = D->getExtInfo();
581 Record.AddQualifierInfo(*Info);
582 Record.AddStmt(Info->TrailingRequiresClause);
584 // The location information is deferred until the end of the record.
585 Record.AddTypeRef(D->getTypeSourceInfo() ? D->getTypeSourceInfo()->getType()
586 : QualType());
589 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
590 static_assert(DeclContext::NumFunctionDeclBits == 44,
591 "You need to update the serializer after you change the "
592 "FunctionDeclBits");
594 VisitRedeclarable(D);
596 Record.push_back(D->getTemplatedKind());
597 switch (D->getTemplatedKind()) {
598 case FunctionDecl::TK_NonTemplate:
599 break;
600 case FunctionDecl::TK_DependentNonTemplate:
601 Record.AddDeclRef(D->getInstantiatedFromDecl());
602 break;
603 case FunctionDecl::TK_FunctionTemplate:
604 Record.AddDeclRef(D->getDescribedFunctionTemplate());
605 break;
606 case FunctionDecl::TK_MemberSpecialization: {
607 MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
608 Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
609 Record.push_back(MemberInfo->getTemplateSpecializationKind());
610 Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
611 break;
613 case FunctionDecl::TK_FunctionTemplateSpecialization: {
614 FunctionTemplateSpecializationInfo *
615 FTSInfo = D->getTemplateSpecializationInfo();
617 RegisterTemplateSpecialization(FTSInfo->getTemplate(), D);
619 Record.AddDeclRef(FTSInfo->getTemplate());
620 Record.push_back(FTSInfo->getTemplateSpecializationKind());
622 // Template arguments.
623 Record.AddTemplateArgumentList(FTSInfo->TemplateArguments);
625 // Template args as written.
626 Record.push_back(FTSInfo->TemplateArgumentsAsWritten != nullptr);
627 if (FTSInfo->TemplateArgumentsAsWritten)
628 Record.AddASTTemplateArgumentListInfo(
629 FTSInfo->TemplateArgumentsAsWritten);
631 Record.AddSourceLocation(FTSInfo->getPointOfInstantiation());
633 if (MemberSpecializationInfo *MemberInfo =
634 FTSInfo->getMemberSpecializationInfo()) {
635 Record.push_back(1);
636 Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
637 Record.push_back(MemberInfo->getTemplateSpecializationKind());
638 Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
639 } else {
640 Record.push_back(0);
643 if (D->isCanonicalDecl()) {
644 // Write the template that contains the specializations set. We will
645 // add a FunctionTemplateSpecializationInfo to it when reading.
646 Record.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl());
648 break;
650 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
651 DependentFunctionTemplateSpecializationInfo *
652 DFTSInfo = D->getDependentSpecializationInfo();
654 // Candidates.
655 Record.push_back(DFTSInfo->getCandidates().size());
656 for (FunctionTemplateDecl *FTD : DFTSInfo->getCandidates())
657 Record.AddDeclRef(FTD);
659 // Templates args.
660 Record.push_back(DFTSInfo->TemplateArgumentsAsWritten != nullptr);
661 if (DFTSInfo->TemplateArgumentsAsWritten)
662 Record.AddASTTemplateArgumentListInfo(
663 DFTSInfo->TemplateArgumentsAsWritten);
664 break;
668 VisitDeclaratorDecl(D);
669 Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
670 Record.push_back(D->getIdentifierNamespace());
672 // The order matters here. It will be better to put the bit with higher
673 // probability to be 0 in the end of the bits. See the comments in VisitDecl
674 // for details.
675 BitsPacker FunctionDeclBits;
676 // FIXME: stable encoding
677 FunctionDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()), 3);
678 FunctionDeclBits.addBits((uint32_t)D->getStorageClass(), /*BitWidth=*/3);
679 FunctionDeclBits.addBit(D->isInlineSpecified());
680 FunctionDeclBits.addBit(D->isInlined());
681 FunctionDeclBits.addBit(D->hasSkippedBody());
682 FunctionDeclBits.addBit(D->isVirtualAsWritten());
683 FunctionDeclBits.addBit(D->isPure());
684 FunctionDeclBits.addBit(D->hasInheritedPrototype());
685 FunctionDeclBits.addBit(D->hasWrittenPrototype());
686 FunctionDeclBits.addBit(D->isDeletedBit());
687 FunctionDeclBits.addBit(D->isTrivial());
688 FunctionDeclBits.addBit(D->isTrivialForCall());
689 FunctionDeclBits.addBit(D->isDefaulted());
690 FunctionDeclBits.addBit(D->isExplicitlyDefaulted());
691 FunctionDeclBits.addBit(D->isIneligibleOrNotSelected());
692 FunctionDeclBits.addBits((uint64_t)(D->getConstexprKind()), /*BitWidth=*/2);
693 FunctionDeclBits.addBit(D->hasImplicitReturnZero());
694 FunctionDeclBits.addBit(D->isMultiVersion());
695 FunctionDeclBits.addBit(D->isLateTemplateParsed());
696 FunctionDeclBits.addBit(D->FriendConstraintRefersToEnclosingTemplate());
697 FunctionDeclBits.addBit(D->usesSEHTry());
698 Record.push_back(FunctionDeclBits);
700 Record.AddSourceLocation(D->getEndLoc());
701 if (D->isExplicitlyDefaulted())
702 Record.AddSourceLocation(D->getDefaultLoc());
704 Record.push_back(D->getODRHash());
706 if (D->isDefaulted()) {
707 if (auto *FDI = D->getDefaultedFunctionInfo()) {
708 Record.push_back(FDI->getUnqualifiedLookups().size());
709 for (DeclAccessPair P : FDI->getUnqualifiedLookups()) {
710 Record.AddDeclRef(P.getDecl());
711 Record.push_back(P.getAccess());
713 } else {
714 Record.push_back(0);
718 Record.push_back(D->param_size());
719 for (auto *P : D->parameters())
720 Record.AddDeclRef(P);
721 Code = serialization::DECL_FUNCTION;
724 static void addExplicitSpecifier(ExplicitSpecifier ES,
725 ASTRecordWriter &Record) {
726 uint64_t Kind = static_cast<uint64_t>(ES.getKind());
727 Kind = Kind << 1 | static_cast<bool>(ES.getExpr());
728 Record.push_back(Kind);
729 if (ES.getExpr()) {
730 Record.AddStmt(ES.getExpr());
734 void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
735 addExplicitSpecifier(D->getExplicitSpecifier(), Record);
736 Record.AddDeclRef(D->Ctor);
737 VisitFunctionDecl(D);
738 Record.push_back(static_cast<unsigned char>(D->getDeductionCandidateKind()));
739 Code = serialization::DECL_CXX_DEDUCTION_GUIDE;
742 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
743 static_assert(DeclContext::NumObjCMethodDeclBits == 37,
744 "You need to update the serializer after you change the "
745 "ObjCMethodDeclBits");
747 VisitNamedDecl(D);
748 // FIXME: convert to LazyStmtPtr?
749 // Unlike C/C++, method bodies will never be in header files.
750 bool HasBodyStuff = D->getBody() != nullptr;
751 Record.push_back(HasBodyStuff);
752 if (HasBodyStuff) {
753 Record.AddStmt(D->getBody());
755 Record.AddDeclRef(D->getSelfDecl());
756 Record.AddDeclRef(D->getCmdDecl());
757 Record.push_back(D->isInstanceMethod());
758 Record.push_back(D->isVariadic());
759 Record.push_back(D->isPropertyAccessor());
760 Record.push_back(D->isSynthesizedAccessorStub());
761 Record.push_back(D->isDefined());
762 Record.push_back(D->isOverriding());
763 Record.push_back(D->hasSkippedBody());
765 Record.push_back(D->isRedeclaration());
766 Record.push_back(D->hasRedeclaration());
767 if (D->hasRedeclaration()) {
768 assert(Context.getObjCMethodRedeclaration(D));
769 Record.AddDeclRef(Context.getObjCMethodRedeclaration(D));
772 // FIXME: stable encoding for @required/@optional
773 Record.push_back(llvm::to_underlying(D->getImplementationControl()));
774 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway/nullability
775 Record.push_back(D->getObjCDeclQualifier());
776 Record.push_back(D->hasRelatedResultType());
777 Record.AddTypeRef(D->getReturnType());
778 Record.AddTypeSourceInfo(D->getReturnTypeSourceInfo());
779 Record.AddSourceLocation(D->getEndLoc());
780 Record.push_back(D->param_size());
781 for (const auto *P : D->parameters())
782 Record.AddDeclRef(P);
784 Record.push_back(D->getSelLocsKind());
785 unsigned NumStoredSelLocs = D->getNumStoredSelLocs();
786 SourceLocation *SelLocs = D->getStoredSelLocs();
787 Record.push_back(NumStoredSelLocs);
788 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
789 Record.AddSourceLocation(SelLocs[i]);
791 Code = serialization::DECL_OBJC_METHOD;
794 void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
795 VisitTypedefNameDecl(D);
796 Record.push_back(D->Variance);
797 Record.push_back(D->Index);
798 Record.AddSourceLocation(D->VarianceLoc);
799 Record.AddSourceLocation(D->ColonLoc);
801 Code = serialization::DECL_OBJC_TYPE_PARAM;
804 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
805 static_assert(DeclContext::NumObjCContainerDeclBits == 64,
806 "You need to update the serializer after you change the "
807 "ObjCContainerDeclBits");
809 VisitNamedDecl(D);
810 Record.AddSourceLocation(D->getAtStartLoc());
811 Record.AddSourceRange(D->getAtEndRange());
812 // Abstract class (no need to define a stable serialization::DECL code).
815 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
816 VisitRedeclarable(D);
817 VisitObjCContainerDecl(D);
818 Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
819 AddObjCTypeParamList(D->TypeParamList);
821 Record.push_back(D->isThisDeclarationADefinition());
822 if (D->isThisDeclarationADefinition()) {
823 // Write the DefinitionData
824 ObjCInterfaceDecl::DefinitionData &Data = D->data();
826 Record.AddTypeSourceInfo(D->getSuperClassTInfo());
827 Record.AddSourceLocation(D->getEndOfDefinitionLoc());
828 Record.push_back(Data.HasDesignatedInitializers);
829 Record.push_back(D->getODRHash());
831 // Write out the protocols that are directly referenced by the @interface.
832 Record.push_back(Data.ReferencedProtocols.size());
833 for (const auto *P : D->protocols())
834 Record.AddDeclRef(P);
835 for (const auto &PL : D->protocol_locs())
836 Record.AddSourceLocation(PL);
838 // Write out the protocols that are transitively referenced.
839 Record.push_back(Data.AllReferencedProtocols.size());
840 for (ObjCList<ObjCProtocolDecl>::iterator
841 P = Data.AllReferencedProtocols.begin(),
842 PEnd = Data.AllReferencedProtocols.end();
843 P != PEnd; ++P)
844 Record.AddDeclRef(*P);
847 if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {
848 // Ensure that we write out the set of categories for this class.
849 Writer.ObjCClassesWithCategories.insert(D);
851 // Make sure that the categories get serialized.
852 for (; Cat; Cat = Cat->getNextClassCategoryRaw())
853 (void)Writer.GetDeclRef(Cat);
857 Code = serialization::DECL_OBJC_INTERFACE;
860 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
861 VisitFieldDecl(D);
862 // FIXME: stable encoding for @public/@private/@protected/@package
863 Record.push_back(D->getAccessControl());
864 Record.push_back(D->getSynthesize());
866 if (D->getDeclContext() == D->getLexicalDeclContext() &&
867 !D->hasAttrs() &&
868 !D->isImplicit() &&
869 !D->isUsed(false) &&
870 !D->isInvalidDecl() &&
871 !D->isReferenced() &&
872 !D->isModulePrivate() &&
873 !D->getBitWidth() &&
874 !D->hasExtInfo() &&
875 D->getDeclName())
876 AbbrevToUse = Writer.getDeclObjCIvarAbbrev();
878 Code = serialization::DECL_OBJC_IVAR;
881 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
882 VisitRedeclarable(D);
883 VisitObjCContainerDecl(D);
885 Record.push_back(D->isThisDeclarationADefinition());
886 if (D->isThisDeclarationADefinition()) {
887 Record.push_back(D->protocol_size());
888 for (const auto *I : D->protocols())
889 Record.AddDeclRef(I);
890 for (const auto &PL : D->protocol_locs())
891 Record.AddSourceLocation(PL);
892 Record.push_back(D->getODRHash());
895 Code = serialization::DECL_OBJC_PROTOCOL;
898 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
899 VisitFieldDecl(D);
900 Code = serialization::DECL_OBJC_AT_DEFS_FIELD;
903 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
904 VisitObjCContainerDecl(D);
905 Record.AddSourceLocation(D->getCategoryNameLoc());
906 Record.AddSourceLocation(D->getIvarLBraceLoc());
907 Record.AddSourceLocation(D->getIvarRBraceLoc());
908 Record.AddDeclRef(D->getClassInterface());
909 AddObjCTypeParamList(D->TypeParamList);
910 Record.push_back(D->protocol_size());
911 for (const auto *I : D->protocols())
912 Record.AddDeclRef(I);
913 for (const auto &PL : D->protocol_locs())
914 Record.AddSourceLocation(PL);
915 Code = serialization::DECL_OBJC_CATEGORY;
918 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
919 VisitNamedDecl(D);
920 Record.AddDeclRef(D->getClassInterface());
921 Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;
924 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
925 VisitNamedDecl(D);
926 Record.AddSourceLocation(D->getAtLoc());
927 Record.AddSourceLocation(D->getLParenLoc());
928 Record.AddTypeRef(D->getType());
929 Record.AddTypeSourceInfo(D->getTypeSourceInfo());
930 // FIXME: stable encoding
931 Record.push_back((unsigned)D->getPropertyAttributes());
932 Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
933 // FIXME: stable encoding
934 Record.push_back((unsigned)D->getPropertyImplementation());
935 Record.AddDeclarationName(D->getGetterName());
936 Record.AddSourceLocation(D->getGetterNameLoc());
937 Record.AddDeclarationName(D->getSetterName());
938 Record.AddSourceLocation(D->getSetterNameLoc());
939 Record.AddDeclRef(D->getGetterMethodDecl());
940 Record.AddDeclRef(D->getSetterMethodDecl());
941 Record.AddDeclRef(D->getPropertyIvarDecl());
942 Code = serialization::DECL_OBJC_PROPERTY;
945 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
946 VisitObjCContainerDecl(D);
947 Record.AddDeclRef(D->getClassInterface());
948 // Abstract class (no need to define a stable serialization::DECL code).
951 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
952 VisitObjCImplDecl(D);
953 Record.AddSourceLocation(D->getCategoryNameLoc());
954 Code = serialization::DECL_OBJC_CATEGORY_IMPL;
957 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
958 VisitObjCImplDecl(D);
959 Record.AddDeclRef(D->getSuperClass());
960 Record.AddSourceLocation(D->getSuperClassLoc());
961 Record.AddSourceLocation(D->getIvarLBraceLoc());
962 Record.AddSourceLocation(D->getIvarRBraceLoc());
963 Record.push_back(D->hasNonZeroConstructors());
964 Record.push_back(D->hasDestructors());
965 Record.push_back(D->NumIvarInitializers);
966 if (D->NumIvarInitializers)
967 Record.AddCXXCtorInitializers(
968 llvm::ArrayRef(D->init_begin(), D->init_end()));
969 Code = serialization::DECL_OBJC_IMPLEMENTATION;
972 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
973 VisitDecl(D);
974 Record.AddSourceLocation(D->getBeginLoc());
975 Record.AddDeclRef(D->getPropertyDecl());
976 Record.AddDeclRef(D->getPropertyIvarDecl());
977 Record.AddSourceLocation(D->getPropertyIvarDeclLoc());
978 Record.AddDeclRef(D->getGetterMethodDecl());
979 Record.AddDeclRef(D->getSetterMethodDecl());
980 Record.AddStmt(D->getGetterCXXConstructor());
981 Record.AddStmt(D->getSetterCXXAssignment());
982 Code = serialization::DECL_OBJC_PROPERTY_IMPL;
985 void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {
986 VisitDeclaratorDecl(D);
987 Record.push_back(D->isMutable());
989 Record.push_back((D->StorageKind << 1) | D->BitField);
990 if (D->StorageKind == FieldDecl::ISK_CapturedVLAType)
991 Record.AddTypeRef(QualType(D->getCapturedVLAType(), 0));
992 else if (D->BitField)
993 Record.AddStmt(D->getBitWidth());
995 if (!D->getDeclName())
996 Record.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D));
998 if (D->getDeclContext() == D->getLexicalDeclContext() &&
999 !D->hasAttrs() &&
1000 !D->isImplicit() &&
1001 !D->isUsed(false) &&
1002 !D->isInvalidDecl() &&
1003 !D->isReferenced() &&
1004 !D->isTopLevelDeclInObjCContainer() &&
1005 !D->isModulePrivate() &&
1006 !D->getBitWidth() &&
1007 !D->hasInClassInitializer() &&
1008 !D->hasCapturedVLAType() &&
1009 !D->hasExtInfo() &&
1010 !ObjCIvarDecl::classofKind(D->getKind()) &&
1011 !ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&
1012 D->getDeclName())
1013 AbbrevToUse = Writer.getDeclFieldAbbrev();
1015 Code = serialization::DECL_FIELD;
1018 void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {
1019 VisitDeclaratorDecl(D);
1020 Record.AddIdentifierRef(D->getGetterId());
1021 Record.AddIdentifierRef(D->getSetterId());
1022 Code = serialization::DECL_MS_PROPERTY;
1025 void ASTDeclWriter::VisitMSGuidDecl(MSGuidDecl *D) {
1026 VisitValueDecl(D);
1027 MSGuidDecl::Parts Parts = D->getParts();
1028 Record.push_back(Parts.Part1);
1029 Record.push_back(Parts.Part2);
1030 Record.push_back(Parts.Part3);
1031 Record.append(std::begin(Parts.Part4And5), std::end(Parts.Part4And5));
1032 Code = serialization::DECL_MS_GUID;
1035 void ASTDeclWriter::VisitUnnamedGlobalConstantDecl(
1036 UnnamedGlobalConstantDecl *D) {
1037 VisitValueDecl(D);
1038 Record.AddAPValue(D->getValue());
1039 Code = serialization::DECL_UNNAMED_GLOBAL_CONSTANT;
1042 void ASTDeclWriter::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {
1043 VisitValueDecl(D);
1044 Record.AddAPValue(D->getValue());
1045 Code = serialization::DECL_TEMPLATE_PARAM_OBJECT;
1048 void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
1049 VisitValueDecl(D);
1050 Record.push_back(D->getChainingSize());
1052 for (const auto *P : D->chain())
1053 Record.AddDeclRef(P);
1054 Code = serialization::DECL_INDIRECTFIELD;
1057 void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
1058 VisitRedeclarable(D);
1059 VisitDeclaratorDecl(D);
1061 // The order matters here. It will be better to put the bit with higher
1062 // probability to be 0 in the end of the bits. See the comments in VisitDecl
1063 // for details.
1064 BitsPacker VarDeclBits;
1065 VarDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()),
1066 /*BitWidth=*/3);
1068 bool ModulesCodegen = false;
1069 if (Writer.WritingModule && D->getStorageDuration() == SD_Static &&
1070 !D->getDescribedVarTemplate()) {
1071 // When building a C++20 module interface unit or a partition unit, a
1072 // strong definition in the module interface is provided by the
1073 // compilation of that unit, not by its users. (Inline variables are still
1074 // emitted in module users.)
1075 ModulesCodegen =
1076 (Writer.WritingModule->isInterfaceOrPartition() ||
1077 (D->hasAttr<DLLExportAttr>() &&
1078 Writer.Context->getLangOpts().BuildingPCHWithObjectFile)) &&
1079 Writer.Context->GetGVALinkageForVariable(D) >= GVA_StrongExternal;
1081 VarDeclBits.addBit(ModulesCodegen);
1083 VarDeclBits.addBits(D->getStorageClass(), /*BitWidth=*/3);
1084 VarDeclBits.addBits(D->getTSCSpec(), /*BitWidth=*/2);
1085 VarDeclBits.addBits(D->getInitStyle(), /*BitWidth=*/2);
1086 VarDeclBits.addBit(D->isARCPseudoStrong());
1088 bool HasDeducedType = false;
1089 if (!isa<ParmVarDecl>(D)) {
1090 VarDeclBits.addBit(D->isThisDeclarationADemotedDefinition());
1091 VarDeclBits.addBit(D->isExceptionVariable());
1092 VarDeclBits.addBit(D->isNRVOVariable());
1093 VarDeclBits.addBit(D->isCXXForRangeDecl());
1095 VarDeclBits.addBit(D->isInline());
1096 VarDeclBits.addBit(D->isInlineSpecified());
1097 VarDeclBits.addBit(D->isConstexpr());
1098 VarDeclBits.addBit(D->isInitCapture());
1099 VarDeclBits.addBit(D->isPreviousDeclInSameBlockScope());
1101 VarDeclBits.addBit(D->isEscapingByref());
1102 HasDeducedType = D->getType()->getContainedDeducedType();
1103 VarDeclBits.addBit(HasDeducedType);
1105 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(D))
1106 VarDeclBits.addBits(llvm::to_underlying(IPD->getParameterKind()),
1107 /*Width=*/3);
1108 else
1109 VarDeclBits.addBits(0, /*Width=*/3);
1111 VarDeclBits.addBit(D->isObjCForDecl());
1114 Record.push_back(VarDeclBits);
1116 if (ModulesCodegen)
1117 Writer.ModularCodegenDecls.push_back(Writer.GetDeclRef(D));
1119 if (D->hasAttr<BlocksAttr>()) {
1120 BlockVarCopyInit Init = Writer.Context->getBlockVarCopyInit(D);
1121 Record.AddStmt(Init.getCopyExpr());
1122 if (Init.getCopyExpr())
1123 Record.push_back(Init.canThrow());
1126 enum {
1127 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1129 if (VarTemplateDecl *TemplD = D->getDescribedVarTemplate()) {
1130 Record.push_back(VarTemplate);
1131 Record.AddDeclRef(TemplD);
1132 } else if (MemberSpecializationInfo *SpecInfo
1133 = D->getMemberSpecializationInfo()) {
1134 Record.push_back(StaticDataMemberSpecialization);
1135 Record.AddDeclRef(SpecInfo->getInstantiatedFrom());
1136 Record.push_back(SpecInfo->getTemplateSpecializationKind());
1137 Record.AddSourceLocation(SpecInfo->getPointOfInstantiation());
1138 } else {
1139 Record.push_back(VarNotTemplate);
1142 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
1143 !D->isTopLevelDeclInObjCContainer() &&
1144 !needsAnonymousDeclarationNumber(D) &&
1145 D->getDeclName().getNameKind() == DeclarationName::Identifier &&
1146 !D->hasExtInfo() && D->getFirstDecl() == D->getMostRecentDecl() &&
1147 D->getKind() == Decl::Var && !D->isInline() && !D->isConstexpr() &&
1148 !D->isInitCapture() && !D->isPreviousDeclInSameBlockScope() &&
1149 !D->isEscapingByref() && !HasDeducedType &&
1150 D->getStorageDuration() != SD_Static && !D->getDescribedVarTemplate() &&
1151 !D->getMemberSpecializationInfo() && !D->isObjCForDecl() &&
1152 !isa<ImplicitParamDecl>(D) && !D->isEscapingByref())
1153 AbbrevToUse = Writer.getDeclVarAbbrev();
1155 Code = serialization::DECL_VAR;
1158 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
1159 VisitVarDecl(D);
1160 Code = serialization::DECL_IMPLICIT_PARAM;
1163 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
1164 VisitVarDecl(D);
1166 BitsPacker ParmVarDeclBits;
1167 ParmVarDeclBits.addBit(D->isObjCMethodParameter());
1168 ParmVarDeclBits.addBits(D->getFunctionScopeDepth(), /*BitsWidth=*/7);
1169 ParmVarDeclBits.addBits(D->getFunctionScopeIndex(), /*BitsWidth=*/8);
1170 // FIXME: stable encoding
1171 ParmVarDeclBits.addBits(D->getObjCDeclQualifier(), /*BitsWidth=*/7);
1172 ParmVarDeclBits.addBit(D->isKNRPromoted());
1173 ParmVarDeclBits.addBit(D->hasInheritedDefaultArg());
1174 ParmVarDeclBits.addBit(D->hasUninstantiatedDefaultArg());
1175 ParmVarDeclBits.addBit(D->getExplicitObjectParamThisLoc().isValid());
1176 Record.push_back(ParmVarDeclBits);
1178 if (D->hasUninstantiatedDefaultArg())
1179 Record.AddStmt(D->getUninstantiatedDefaultArg());
1180 if (D->getExplicitObjectParamThisLoc().isValid())
1181 Record.AddSourceLocation(D->getExplicitObjectParamThisLoc());
1182 Code = serialization::DECL_PARM_VAR;
1184 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
1185 // we dynamically check for the properties that we optimize for, but don't
1186 // know are true of all PARM_VAR_DECLs.
1187 if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&
1188 !D->hasExtInfo() && D->getStorageClass() == 0 && !D->isInvalidDecl() &&
1189 !D->isTopLevelDeclInObjCContainer() &&
1190 D->getInitStyle() == VarDecl::CInit && // Can params have anything else?
1191 D->getInit() == nullptr) // No default expr.
1192 AbbrevToUse = Writer.getDeclParmVarAbbrev();
1194 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
1195 // just us assuming it.
1196 assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");
1197 assert(!D->isThisDeclarationADemotedDefinition()
1198 && "PARM_VAR_DECL can't be demoted definition.");
1199 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
1200 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
1201 assert(D->getPreviousDecl() == nullptr && "PARM_VAR_DECL can't be redecl");
1202 assert(!D->isStaticDataMember() &&
1203 "PARM_VAR_DECL can't be static data member");
1206 void ASTDeclWriter::VisitDecompositionDecl(DecompositionDecl *D) {
1207 // Record the number of bindings first to simplify deserialization.
1208 Record.push_back(D->bindings().size());
1210 VisitVarDecl(D);
1211 for (auto *B : D->bindings())
1212 Record.AddDeclRef(B);
1213 Code = serialization::DECL_DECOMPOSITION;
1216 void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {
1217 VisitValueDecl(D);
1218 Record.AddStmt(D->getBinding());
1219 Code = serialization::DECL_BINDING;
1222 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
1223 VisitDecl(D);
1224 Record.AddStmt(D->getAsmString());
1225 Record.AddSourceLocation(D->getRParenLoc());
1226 Code = serialization::DECL_FILE_SCOPE_ASM;
1229 void ASTDeclWriter::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {
1230 VisitDecl(D);
1231 Record.AddStmt(D->getStmt());
1232 Code = serialization::DECL_TOP_LEVEL_STMT_DECL;
1235 void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {
1236 VisitDecl(D);
1237 Code = serialization::DECL_EMPTY;
1240 void ASTDeclWriter::VisitLifetimeExtendedTemporaryDecl(
1241 LifetimeExtendedTemporaryDecl *D) {
1242 VisitDecl(D);
1243 Record.AddDeclRef(D->getExtendingDecl());
1244 Record.AddStmt(D->getTemporaryExpr());
1245 Record.push_back(static_cast<bool>(D->getValue()));
1246 if (D->getValue())
1247 Record.AddAPValue(*D->getValue());
1248 Record.push_back(D->getManglingNumber());
1249 Code = serialization::DECL_LIFETIME_EXTENDED_TEMPORARY;
1251 void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
1252 VisitDecl(D);
1253 Record.AddStmt(D->getBody());
1254 Record.AddTypeSourceInfo(D->getSignatureAsWritten());
1255 Record.push_back(D->param_size());
1256 for (ParmVarDecl *P : D->parameters())
1257 Record.AddDeclRef(P);
1258 Record.push_back(D->isVariadic());
1259 Record.push_back(D->blockMissingReturnType());
1260 Record.push_back(D->isConversionFromLambda());
1261 Record.push_back(D->doesNotEscape());
1262 Record.push_back(D->canAvoidCopyToHeap());
1263 Record.push_back(D->capturesCXXThis());
1264 Record.push_back(D->getNumCaptures());
1265 for (const auto &capture : D->captures()) {
1266 Record.AddDeclRef(capture.getVariable());
1268 unsigned flags = 0;
1269 if (capture.isByRef()) flags |= 1;
1270 if (capture.isNested()) flags |= 2;
1271 if (capture.hasCopyExpr()) flags |= 4;
1272 Record.push_back(flags);
1274 if (capture.hasCopyExpr()) Record.AddStmt(capture.getCopyExpr());
1277 Code = serialization::DECL_BLOCK;
1280 void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
1281 Record.push_back(CD->getNumParams());
1282 VisitDecl(CD);
1283 Record.push_back(CD->getContextParamPosition());
1284 Record.push_back(CD->isNothrow() ? 1 : 0);
1285 // Body is stored by VisitCapturedStmt.
1286 for (unsigned I = 0; I < CD->getNumParams(); ++I)
1287 Record.AddDeclRef(CD->getParam(I));
1288 Code = serialization::DECL_CAPTURED;
1291 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1292 static_assert(DeclContext::NumLinkageSpecDeclBits == 17,
1293 "You need to update the serializer after you change the"
1294 "LinkageSpecDeclBits");
1296 VisitDecl(D);
1297 Record.push_back(llvm::to_underlying(D->getLanguage()));
1298 Record.AddSourceLocation(D->getExternLoc());
1299 Record.AddSourceLocation(D->getRBraceLoc());
1300 Code = serialization::DECL_LINKAGE_SPEC;
1303 void ASTDeclWriter::VisitExportDecl(ExportDecl *D) {
1304 VisitDecl(D);
1305 Record.AddSourceLocation(D->getRBraceLoc());
1306 Code = serialization::DECL_EXPORT;
1309 void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {
1310 VisitNamedDecl(D);
1311 Record.AddSourceLocation(D->getBeginLoc());
1312 Code = serialization::DECL_LABEL;
1316 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
1317 VisitRedeclarable(D);
1318 VisitNamedDecl(D);
1320 BitsPacker NamespaceDeclBits;
1321 NamespaceDeclBits.addBit(D->isInline());
1322 NamespaceDeclBits.addBit(D->isNested());
1323 Record.push_back(NamespaceDeclBits);
1325 Record.AddSourceLocation(D->getBeginLoc());
1326 Record.AddSourceLocation(D->getRBraceLoc());
1328 if (D->isOriginalNamespace())
1329 Record.AddDeclRef(D->getAnonymousNamespace());
1330 Code = serialization::DECL_NAMESPACE;
1332 if (Writer.hasChain() && D->isAnonymousNamespace() &&
1333 D == D->getMostRecentDecl()) {
1334 // This is a most recent reopening of the anonymous namespace. If its parent
1335 // is in a previous PCH (or is the TU), mark that parent for update, because
1336 // the original namespace always points to the latest re-opening of its
1337 // anonymous namespace.
1338 Decl *Parent = cast<Decl>(
1339 D->getParent()->getRedeclContext()->getPrimaryContext());
1340 if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {
1341 Writer.DeclUpdates[Parent].push_back(
1342 ASTWriter::DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, D));
1347 void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1348 VisitRedeclarable(D);
1349 VisitNamedDecl(D);
1350 Record.AddSourceLocation(D->getNamespaceLoc());
1351 Record.AddSourceLocation(D->getTargetNameLoc());
1352 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1353 Record.AddDeclRef(D->getNamespace());
1354 Code = serialization::DECL_NAMESPACE_ALIAS;
1357 void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {
1358 VisitNamedDecl(D);
1359 Record.AddSourceLocation(D->getUsingLoc());
1360 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1361 Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
1362 Record.AddDeclRef(D->FirstUsingShadow.getPointer());
1363 Record.push_back(D->hasTypename());
1364 Record.AddDeclRef(Context.getInstantiatedFromUsingDecl(D));
1365 Code = serialization::DECL_USING;
1368 void ASTDeclWriter::VisitUsingEnumDecl(UsingEnumDecl *D) {
1369 VisitNamedDecl(D);
1370 Record.AddSourceLocation(D->getUsingLoc());
1371 Record.AddSourceLocation(D->getEnumLoc());
1372 Record.AddTypeSourceInfo(D->getEnumType());
1373 Record.AddDeclRef(D->FirstUsingShadow.getPointer());
1374 Record.AddDeclRef(Context.getInstantiatedFromUsingEnumDecl(D));
1375 Code = serialization::DECL_USING_ENUM;
1378 void ASTDeclWriter::VisitUsingPackDecl(UsingPackDecl *D) {
1379 Record.push_back(D->NumExpansions);
1380 VisitNamedDecl(D);
1381 Record.AddDeclRef(D->getInstantiatedFromUsingDecl());
1382 for (auto *E : D->expansions())
1383 Record.AddDeclRef(E);
1384 Code = serialization::DECL_USING_PACK;
1387 void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1388 VisitRedeclarable(D);
1389 VisitNamedDecl(D);
1390 Record.AddDeclRef(D->getTargetDecl());
1391 Record.push_back(D->getIdentifierNamespace());
1392 Record.AddDeclRef(D->UsingOrNextShadow);
1393 Record.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D));
1395 if (D->getDeclContext() == D->getLexicalDeclContext() &&
1396 D->getFirstDecl() == D->getMostRecentDecl() && !D->hasAttrs() &&
1397 !needsAnonymousDeclarationNumber(D) &&
1398 D->getDeclName().getNameKind() == DeclarationName::Identifier)
1399 AbbrevToUse = Writer.getDeclUsingShadowAbbrev();
1401 Code = serialization::DECL_USING_SHADOW;
1404 void ASTDeclWriter::VisitConstructorUsingShadowDecl(
1405 ConstructorUsingShadowDecl *D) {
1406 VisitUsingShadowDecl(D);
1407 Record.AddDeclRef(D->NominatedBaseClassShadowDecl);
1408 Record.AddDeclRef(D->ConstructedBaseClassShadowDecl);
1409 Record.push_back(D->IsVirtual);
1410 Code = serialization::DECL_CONSTRUCTOR_USING_SHADOW;
1413 void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1414 VisitNamedDecl(D);
1415 Record.AddSourceLocation(D->getUsingLoc());
1416 Record.AddSourceLocation(D->getNamespaceKeyLocation());
1417 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1418 Record.AddDeclRef(D->getNominatedNamespace());
1419 Record.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()));
1420 Code = serialization::DECL_USING_DIRECTIVE;
1423 void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1424 VisitValueDecl(D);
1425 Record.AddSourceLocation(D->getUsingLoc());
1426 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1427 Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
1428 Record.AddSourceLocation(D->getEllipsisLoc());
1429 Code = serialization::DECL_UNRESOLVED_USING_VALUE;
1432 void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
1433 UnresolvedUsingTypenameDecl *D) {
1434 VisitTypeDecl(D);
1435 Record.AddSourceLocation(D->getTypenameLoc());
1436 Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1437 Record.AddSourceLocation(D->getEllipsisLoc());
1438 Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;
1441 void ASTDeclWriter::VisitUnresolvedUsingIfExistsDecl(
1442 UnresolvedUsingIfExistsDecl *D) {
1443 VisitNamedDecl(D);
1444 Code = serialization::DECL_UNRESOLVED_USING_IF_EXISTS;
1447 void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
1448 VisitRecordDecl(D);
1450 enum {
1451 CXXRecNotTemplate = 0,
1452 CXXRecTemplate,
1453 CXXRecMemberSpecialization,
1454 CXXLambda
1456 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
1457 Record.push_back(CXXRecTemplate);
1458 Record.AddDeclRef(TemplD);
1459 } else if (MemberSpecializationInfo *MSInfo
1460 = D->getMemberSpecializationInfo()) {
1461 Record.push_back(CXXRecMemberSpecialization);
1462 Record.AddDeclRef(MSInfo->getInstantiatedFrom());
1463 Record.push_back(MSInfo->getTemplateSpecializationKind());
1464 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
1465 } else if (D->isLambda()) {
1466 // For a lambda, we need some information early for merging.
1467 Record.push_back(CXXLambda);
1468 if (auto *Context = D->getLambdaContextDecl()) {
1469 Record.AddDeclRef(Context);
1470 Record.push_back(D->getLambdaIndexInContext());
1471 } else {
1472 Record.push_back(0);
1474 } else {
1475 Record.push_back(CXXRecNotTemplate);
1478 Record.push_back(D->isThisDeclarationADefinition());
1479 if (D->isThisDeclarationADefinition())
1480 Record.AddCXXDefinitionData(D);
1482 // Store (what we currently believe to be) the key function to avoid
1483 // deserializing every method so we can compute it.
1484 if (D->isCompleteDefinition())
1485 Record.AddDeclRef(Context.getCurrentKeyFunction(D));
1487 Code = serialization::DECL_CXX_RECORD;
1490 void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1491 VisitFunctionDecl(D);
1492 if (D->isCanonicalDecl()) {
1493 Record.push_back(D->size_overridden_methods());
1494 for (const CXXMethodDecl *MD : D->overridden_methods())
1495 Record.AddDeclRef(MD);
1496 } else {
1497 // We only need to record overridden methods once for the canonical decl.
1498 Record.push_back(0);
1501 if (D->getDeclContext() == D->getLexicalDeclContext() &&
1502 D->getFirstDecl() == D->getMostRecentDecl() && !D->isInvalidDecl() &&
1503 !D->hasAttrs() && !D->isTopLevelDeclInObjCContainer() &&
1504 D->getDeclName().getNameKind() == DeclarationName::Identifier &&
1505 !D->hasExtInfo() && !D->isExplicitlyDefaulted()) {
1506 if (D->getTemplatedKind() == FunctionDecl::TK_NonTemplate ||
1507 D->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate ||
1508 D->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization ||
1509 D->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate)
1510 AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());
1511 else if (D->getTemplatedKind() ==
1512 FunctionDecl::TK_FunctionTemplateSpecialization) {
1513 FunctionTemplateSpecializationInfo *FTSInfo =
1514 D->getTemplateSpecializationInfo();
1516 if (FTSInfo->TemplateArguments->size() == 1) {
1517 const TemplateArgument &TA = FTSInfo->TemplateArguments->get(0);
1518 if (TA.getKind() == TemplateArgument::Type &&
1519 !FTSInfo->TemplateArgumentsAsWritten &&
1520 !FTSInfo->getMemberSpecializationInfo())
1521 AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());
1523 } else if (D->getTemplatedKind() ==
1524 FunctionDecl::TK_DependentFunctionTemplateSpecialization) {
1525 DependentFunctionTemplateSpecializationInfo *DFTSInfo =
1526 D->getDependentSpecializationInfo();
1527 if (!DFTSInfo->TemplateArgumentsAsWritten)
1528 AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());
1532 Code = serialization::DECL_CXX_METHOD;
1535 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1536 static_assert(DeclContext::NumCXXConstructorDeclBits == 64,
1537 "You need to update the serializer after you change the "
1538 "CXXConstructorDeclBits");
1540 Record.push_back(D->getTrailingAllocKind());
1541 addExplicitSpecifier(D->getExplicitSpecifier(), Record);
1542 if (auto Inherited = D->getInheritedConstructor()) {
1543 Record.AddDeclRef(Inherited.getShadowDecl());
1544 Record.AddDeclRef(Inherited.getConstructor());
1547 VisitCXXMethodDecl(D);
1548 Code = serialization::DECL_CXX_CONSTRUCTOR;
1551 void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1552 VisitCXXMethodDecl(D);
1554 Record.AddDeclRef(D->getOperatorDelete());
1555 if (D->getOperatorDelete())
1556 Record.AddStmt(D->getOperatorDeleteThisArg());
1558 Code = serialization::DECL_CXX_DESTRUCTOR;
1561 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1562 addExplicitSpecifier(D->getExplicitSpecifier(), Record);
1563 VisitCXXMethodDecl(D);
1564 Code = serialization::DECL_CXX_CONVERSION;
1567 void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {
1568 VisitDecl(D);
1569 Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));
1570 ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();
1571 Record.push_back(!IdentifierLocs.empty());
1572 if (IdentifierLocs.empty()) {
1573 Record.AddSourceLocation(D->getEndLoc());
1574 Record.push_back(1);
1575 } else {
1576 for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)
1577 Record.AddSourceLocation(IdentifierLocs[I]);
1578 Record.push_back(IdentifierLocs.size());
1580 // Note: the number of source locations must always be the last element in
1581 // the record.
1582 Code = serialization::DECL_IMPORT;
1585 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1586 VisitDecl(D);
1587 Record.AddSourceLocation(D->getColonLoc());
1588 Code = serialization::DECL_ACCESS_SPEC;
1591 void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {
1592 // Record the number of friend type template parameter lists here
1593 // so as to simplify memory allocation during deserialization.
1594 Record.push_back(D->NumTPLists);
1595 VisitDecl(D);
1596 bool hasFriendDecl = D->Friend.is<NamedDecl*>();
1597 Record.push_back(hasFriendDecl);
1598 if (hasFriendDecl)
1599 Record.AddDeclRef(D->getFriendDecl());
1600 else
1601 Record.AddTypeSourceInfo(D->getFriendType());
1602 for (unsigned i = 0; i < D->NumTPLists; ++i)
1603 Record.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i));
1604 Record.AddDeclRef(D->getNextFriend());
1605 Record.push_back(D->UnsupportedFriend);
1606 Record.AddSourceLocation(D->FriendLoc);
1607 Code = serialization::DECL_FRIEND;
1610 void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1611 VisitDecl(D);
1612 Record.push_back(D->getNumTemplateParameters());
1613 for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
1614 Record.AddTemplateParameterList(D->getTemplateParameterList(i));
1615 Record.push_back(D->getFriendDecl() != nullptr);
1616 if (D->getFriendDecl())
1617 Record.AddDeclRef(D->getFriendDecl());
1618 else
1619 Record.AddTypeSourceInfo(D->getFriendType());
1620 Record.AddSourceLocation(D->getFriendLoc());
1621 Code = serialization::DECL_FRIEND_TEMPLATE;
1624 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
1625 VisitNamedDecl(D);
1627 Record.AddTemplateParameterList(D->getTemplateParameters());
1628 Record.AddDeclRef(D->getTemplatedDecl());
1631 void ASTDeclWriter::VisitConceptDecl(ConceptDecl *D) {
1632 VisitTemplateDecl(D);
1633 Record.AddStmt(D->getConstraintExpr());
1634 Code = serialization::DECL_CONCEPT;
1637 void ASTDeclWriter::VisitImplicitConceptSpecializationDecl(
1638 ImplicitConceptSpecializationDecl *D) {
1639 Record.push_back(D->getTemplateArguments().size());
1640 VisitDecl(D);
1641 for (const TemplateArgument &Arg : D->getTemplateArguments())
1642 Record.AddTemplateArgument(Arg);
1643 Code = serialization::DECL_IMPLICIT_CONCEPT_SPECIALIZATION;
1646 void ASTDeclWriter::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {
1647 Code = serialization::DECL_REQUIRES_EXPR_BODY;
1650 void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1651 VisitRedeclarable(D);
1653 // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
1654 // getCommonPtr() can be used while this is still initializing.
1655 if (D->isFirstDecl()) {
1656 // This declaration owns the 'common' pointer, so serialize that data now.
1657 Record.AddDeclRef(D->getInstantiatedFromMemberTemplate());
1658 if (D->getInstantiatedFromMemberTemplate())
1659 Record.push_back(D->isMemberSpecialization());
1662 VisitTemplateDecl(D);
1663 Record.push_back(D->getIdentifierNamespace());
1666 void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1667 VisitRedeclarableTemplateDecl(D);
1669 if (D->isFirstDecl())
1670 AddTemplateSpecializations(D);
1671 Code = serialization::DECL_CLASS_TEMPLATE;
1674 void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
1675 ClassTemplateSpecializationDecl *D) {
1676 RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
1678 VisitCXXRecordDecl(D);
1680 llvm::PointerUnion<ClassTemplateDecl *,
1681 ClassTemplatePartialSpecializationDecl *> InstFrom
1682 = D->getSpecializedTemplateOrPartial();
1683 if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {
1684 Record.AddDeclRef(InstFromD);
1685 } else {
1686 Record.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>());
1687 Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
1690 Record.AddTemplateArgumentList(&D->getTemplateArgs());
1691 Record.AddSourceLocation(D->getPointOfInstantiation());
1692 Record.push_back(D->getSpecializationKind());
1693 Record.push_back(D->isCanonicalDecl());
1695 if (D->isCanonicalDecl()) {
1696 // When reading, we'll add it to the folding set of the following template.
1697 Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
1700 // Explicit info.
1701 Record.AddTypeSourceInfo(D->getTypeAsWritten());
1702 if (D->getTypeAsWritten()) {
1703 Record.AddSourceLocation(D->getExternLoc());
1704 Record.AddSourceLocation(D->getTemplateKeywordLoc());
1707 Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;
1710 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
1711 ClassTemplatePartialSpecializationDecl *D) {
1712 Record.AddTemplateParameterList(D->getTemplateParameters());
1713 Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
1715 VisitClassTemplateSpecializationDecl(D);
1717 // These are read/set from/to the first declaration.
1718 if (D->getPreviousDecl() == nullptr) {
1719 Record.AddDeclRef(D->getInstantiatedFromMember());
1720 Record.push_back(D->isMemberSpecialization());
1723 Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
1726 void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {
1727 VisitRedeclarableTemplateDecl(D);
1729 if (D->isFirstDecl())
1730 AddTemplateSpecializations(D);
1731 Code = serialization::DECL_VAR_TEMPLATE;
1734 void ASTDeclWriter::VisitVarTemplateSpecializationDecl(
1735 VarTemplateSpecializationDecl *D) {
1736 RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
1738 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
1739 InstFrom = D->getSpecializedTemplateOrPartial();
1740 if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {
1741 Record.AddDeclRef(InstFromD);
1742 } else {
1743 Record.AddDeclRef(InstFrom.get<VarTemplatePartialSpecializationDecl *>());
1744 Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
1747 // Explicit info.
1748 Record.AddTypeSourceInfo(D->getTypeAsWritten());
1749 if (D->getTypeAsWritten()) {
1750 Record.AddSourceLocation(D->getExternLoc());
1751 Record.AddSourceLocation(D->getTemplateKeywordLoc());
1754 Record.AddTemplateArgumentList(&D->getTemplateArgs());
1755 Record.AddSourceLocation(D->getPointOfInstantiation());
1756 Record.push_back(D->getSpecializationKind());
1757 Record.push_back(D->IsCompleteDefinition);
1759 VisitVarDecl(D);
1761 Record.push_back(D->isCanonicalDecl());
1763 if (D->isCanonicalDecl()) {
1764 // When reading, we'll add it to the folding set of the following template.
1765 Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
1768 Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;
1771 void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(
1772 VarTemplatePartialSpecializationDecl *D) {
1773 Record.AddTemplateParameterList(D->getTemplateParameters());
1774 Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
1776 VisitVarTemplateSpecializationDecl(D);
1778 // These are read/set from/to the first declaration.
1779 if (D->getPreviousDecl() == nullptr) {
1780 Record.AddDeclRef(D->getInstantiatedFromMember());
1781 Record.push_back(D->isMemberSpecialization());
1784 Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;
1787 void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1788 VisitRedeclarableTemplateDecl(D);
1790 if (D->isFirstDecl())
1791 AddTemplateSpecializations(D);
1792 Code = serialization::DECL_FUNCTION_TEMPLATE;
1795 void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1796 Record.push_back(D->hasTypeConstraint());
1797 VisitTypeDecl(D);
1799 Record.push_back(D->wasDeclaredWithTypename());
1801 const TypeConstraint *TC = D->getTypeConstraint();
1802 assert((bool)TC == D->hasTypeConstraint());
1803 if (TC) {
1804 auto *CR = TC->getConceptReference();
1805 Record.push_back(CR != nullptr);
1806 if (CR)
1807 Record.AddConceptReference(CR);
1808 Record.AddStmt(TC->getImmediatelyDeclaredConstraint());
1809 Record.push_back(D->isExpandedParameterPack());
1810 if (D->isExpandedParameterPack())
1811 Record.push_back(D->getNumExpansionParameters());
1814 bool OwnsDefaultArg = D->hasDefaultArgument() &&
1815 !D->defaultArgumentWasInherited();
1816 Record.push_back(OwnsDefaultArg);
1817 if (OwnsDefaultArg)
1818 Record.AddTypeSourceInfo(D->getDefaultArgumentInfo());
1820 if (!TC && !OwnsDefaultArg &&
1821 D->getDeclContext() == D->getLexicalDeclContext() &&
1822 !D->isInvalidDecl() && !D->hasAttrs() &&
1823 !D->isTopLevelDeclInObjCContainer() && !D->isImplicit() &&
1824 D->getDeclName().getNameKind() == DeclarationName::Identifier)
1825 AbbrevToUse = Writer.getDeclTemplateTypeParmAbbrev();
1827 Code = serialization::DECL_TEMPLATE_TYPE_PARM;
1830 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1831 // For an expanded parameter pack, record the number of expansion types here
1832 // so that it's easier for deserialization to allocate the right amount of
1833 // memory.
1834 Expr *TypeConstraint = D->getPlaceholderTypeConstraint();
1835 Record.push_back(!!TypeConstraint);
1836 if (D->isExpandedParameterPack())
1837 Record.push_back(D->getNumExpansionTypes());
1839 VisitDeclaratorDecl(D);
1840 // TemplateParmPosition.
1841 Record.push_back(D->getDepth());
1842 Record.push_back(D->getPosition());
1843 if (TypeConstraint)
1844 Record.AddStmt(TypeConstraint);
1846 if (D->isExpandedParameterPack()) {
1847 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1848 Record.AddTypeRef(D->getExpansionType(I));
1849 Record.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I));
1852 Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;
1853 } else {
1854 // Rest of NonTypeTemplateParmDecl.
1855 Record.push_back(D->isParameterPack());
1856 bool OwnsDefaultArg = D->hasDefaultArgument() &&
1857 !D->defaultArgumentWasInherited();
1858 Record.push_back(OwnsDefaultArg);
1859 if (OwnsDefaultArg)
1860 Record.AddStmt(D->getDefaultArgument());
1861 Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
1865 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1866 // For an expanded parameter pack, record the number of expansion types here
1867 // so that it's easier for deserialization to allocate the right amount of
1868 // memory.
1869 if (D->isExpandedParameterPack())
1870 Record.push_back(D->getNumExpansionTemplateParameters());
1872 VisitTemplateDecl(D);
1873 // TemplateParmPosition.
1874 Record.push_back(D->getDepth());
1875 Record.push_back(D->getPosition());
1877 if (D->isExpandedParameterPack()) {
1878 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1879 I != N; ++I)
1880 Record.AddTemplateParameterList(D->getExpansionTemplateParameters(I));
1881 Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;
1882 } else {
1883 // Rest of TemplateTemplateParmDecl.
1884 Record.push_back(D->isParameterPack());
1885 bool OwnsDefaultArg = D->hasDefaultArgument() &&
1886 !D->defaultArgumentWasInherited();
1887 Record.push_back(OwnsDefaultArg);
1888 if (OwnsDefaultArg)
1889 Record.AddTemplateArgumentLoc(D->getDefaultArgument());
1890 Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;
1894 void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1895 VisitRedeclarableTemplateDecl(D);
1896 Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;
1899 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1900 VisitDecl(D);
1901 Record.AddStmt(D->getAssertExpr());
1902 Record.push_back(D->isFailed());
1903 Record.AddStmt(D->getMessage());
1904 Record.AddSourceLocation(D->getRParenLoc());
1905 Code = serialization::DECL_STATIC_ASSERT;
1908 /// Emit the DeclContext part of a declaration context decl.
1909 void ASTDeclWriter::VisitDeclContext(DeclContext *DC) {
1910 static_assert(DeclContext::NumDeclContextBits == 13,
1911 "You need to update the serializer after you change the "
1912 "DeclContextBits");
1914 Record.AddOffset(Writer.WriteDeclContextLexicalBlock(Context, DC));
1915 Record.AddOffset(Writer.WriteDeclContextVisibleBlock(Context, DC));
1918 const Decl *ASTWriter::getFirstLocalDecl(const Decl *D) {
1919 assert(IsLocalDecl(D) && "expected a local declaration");
1921 const Decl *Canon = D->getCanonicalDecl();
1922 if (IsLocalDecl(Canon))
1923 return Canon;
1925 const Decl *&CacheEntry = FirstLocalDeclCache[Canon];
1926 if (CacheEntry)
1927 return CacheEntry;
1929 for (const Decl *Redecl = D; Redecl; Redecl = Redecl->getPreviousDecl())
1930 if (IsLocalDecl(Redecl))
1931 D = Redecl;
1932 return CacheEntry = D;
1935 template <typename T>
1936 void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {
1937 T *First = D->getFirstDecl();
1938 T *MostRecent = First->getMostRecentDecl();
1939 T *DAsT = static_cast<T *>(D);
1940 if (MostRecent != First) {
1941 assert(isRedeclarableDeclKind(DAsT->getKind()) &&
1942 "Not considered redeclarable?");
1944 Record.AddDeclRef(First);
1946 // Write out a list of local redeclarations of this declaration if it's the
1947 // first local declaration in the chain.
1948 const Decl *FirstLocal = Writer.getFirstLocalDecl(DAsT);
1949 if (DAsT == FirstLocal) {
1950 // Emit a list of all imported first declarations so that we can be sure
1951 // that all redeclarations visible to this module are before D in the
1952 // redecl chain.
1953 unsigned I = Record.size();
1954 Record.push_back(0);
1955 if (Writer.Chain)
1956 AddFirstDeclFromEachModule(DAsT, /*IncludeLocal*/false);
1957 // This is the number of imported first declarations + 1.
1958 Record[I] = Record.size() - I;
1960 // Collect the set of local redeclarations of this declaration, from
1961 // newest to oldest.
1962 ASTWriter::RecordData LocalRedecls;
1963 ASTRecordWriter LocalRedeclWriter(Record, LocalRedecls);
1964 for (const Decl *Prev = FirstLocal->getMostRecentDecl();
1965 Prev != FirstLocal; Prev = Prev->getPreviousDecl())
1966 if (!Prev->isFromASTFile())
1967 LocalRedeclWriter.AddDeclRef(Prev);
1969 // If we have any redecls, write them now as a separate record preceding
1970 // the declaration itself.
1971 if (LocalRedecls.empty())
1972 Record.push_back(0);
1973 else
1974 Record.AddOffset(LocalRedeclWriter.Emit(LOCAL_REDECLARATIONS));
1975 } else {
1976 Record.push_back(0);
1977 Record.AddDeclRef(FirstLocal);
1980 // Make sure that we serialize both the previous and the most-recent
1981 // declarations, which (transitively) ensures that all declarations in the
1982 // chain get serialized.
1984 // FIXME: This is not correct; when we reach an imported declaration we
1985 // won't emit its previous declaration.
1986 (void)Writer.GetDeclRef(D->getPreviousDecl());
1987 (void)Writer.GetDeclRef(MostRecent);
1988 } else {
1989 // We use the sentinel value 0 to indicate an only declaration.
1990 Record.push_back(0);
1994 void ASTDeclWriter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
1995 VisitNamedDecl(D);
1996 VisitDeclContext(D);
1997 Record.push_back(D->isCBuffer());
1998 Record.AddSourceLocation(D->getLocStart());
1999 Record.AddSourceLocation(D->getLBraceLoc());
2000 Record.AddSourceLocation(D->getRBraceLoc());
2002 Code = serialization::DECL_HLSL_BUFFER;
2005 void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2006 Record.writeOMPChildren(D->Data);
2007 VisitDecl(D);
2008 Code = serialization::DECL_OMP_THREADPRIVATE;
2011 void ASTDeclWriter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
2012 Record.writeOMPChildren(D->Data);
2013 VisitDecl(D);
2014 Code = serialization::DECL_OMP_ALLOCATE;
2017 void ASTDeclWriter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
2018 Record.writeOMPChildren(D->Data);
2019 VisitDecl(D);
2020 Code = serialization::DECL_OMP_REQUIRES;
2023 void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
2024 static_assert(DeclContext::NumOMPDeclareReductionDeclBits == 15,
2025 "You need to update the serializer after you change the "
2026 "NumOMPDeclareReductionDeclBits");
2028 VisitValueDecl(D);
2029 Record.AddSourceLocation(D->getBeginLoc());
2030 Record.AddStmt(D->getCombinerIn());
2031 Record.AddStmt(D->getCombinerOut());
2032 Record.AddStmt(D->getCombiner());
2033 Record.AddStmt(D->getInitOrig());
2034 Record.AddStmt(D->getInitPriv());
2035 Record.AddStmt(D->getInitializer());
2036 Record.push_back(llvm::to_underlying(D->getInitializerKind()));
2037 Record.AddDeclRef(D->getPrevDeclInScope());
2038 Code = serialization::DECL_OMP_DECLARE_REDUCTION;
2041 void ASTDeclWriter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2042 Record.writeOMPChildren(D->Data);
2043 VisitValueDecl(D);
2044 Record.AddDeclarationName(D->getVarName());
2045 Record.AddDeclRef(D->getPrevDeclInScope());
2046 Code = serialization::DECL_OMP_DECLARE_MAPPER;
2049 void ASTDeclWriter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
2050 VisitVarDecl(D);
2051 Code = serialization::DECL_OMP_CAPTUREDEXPR;
2054 //===----------------------------------------------------------------------===//
2055 // ASTWriter Implementation
2056 //===----------------------------------------------------------------------===//
2058 namespace {
2059 template <FunctionDecl::TemplatedKind Kind>
2060 std::shared_ptr<llvm::BitCodeAbbrev>
2061 getFunctionDeclAbbrev(serialization::DeclCode Code) {
2062 using namespace llvm;
2064 auto Abv = std::make_shared<BitCodeAbbrev>();
2065 Abv->Add(BitCodeAbbrevOp(Code));
2066 // RedeclarableDecl
2067 Abv->Add(BitCodeAbbrevOp(0)); // CanonicalDecl
2068 Abv->Add(BitCodeAbbrevOp(Kind));
2069 if constexpr (Kind == FunctionDecl::TK_NonTemplate) {
2071 } else if constexpr (Kind == FunctionDecl::TK_FunctionTemplate) {
2072 // DescribedFunctionTemplate
2073 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2074 } else if constexpr (Kind == FunctionDecl::TK_DependentNonTemplate) {
2075 // Instantiated From Decl
2076 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2077 } else if constexpr (Kind == FunctionDecl::TK_MemberSpecialization) {
2078 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedFrom
2079 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2080 3)); // TemplateSpecializationKind
2081 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Specialized Location
2082 } else if constexpr (Kind ==
2083 FunctionDecl::TK_FunctionTemplateSpecialization) {
2084 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template
2085 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2086 3)); // TemplateSpecializationKind
2087 Abv->Add(BitCodeAbbrevOp(1)); // Template Argument Size
2088 Abv->Add(BitCodeAbbrevOp(TemplateArgument::Type)); // Template Argument Kind
2089 Abv->Add(
2090 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template Argument Type
2091 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is Defaulted
2092 Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten
2093 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2094 Abv->Add(BitCodeAbbrevOp(0));
2095 Abv->Add(
2096 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Canonical Decl of template
2097 } else if constexpr (Kind == FunctionDecl::
2098 TK_DependentFunctionTemplateSpecialization) {
2099 // Candidates of specialization
2100 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2101 Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten
2102 } else {
2103 llvm_unreachable("Unknown templated kind?");
2105 // Decl
2106 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2107 8)); // Packed DeclBits: ModuleOwnershipKind,
2108 // isUsed, isReferenced, AccessSpecifier,
2109 // isImplicit
2111 // The following bits should be 0:
2112 // HasStandaloneLexicalDC, HasAttrs,
2113 // TopLevelDeclInObjCContainer,
2114 // isInvalidDecl
2115 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2116 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2117 // NamedDecl
2118 Abv->Add(BitCodeAbbrevOp(DeclarationName::Identifier)); // NameKind
2119 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Identifier
2120 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2121 // ValueDecl
2122 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2123 // DeclaratorDecl
2124 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerLocStart
2125 Abv->Add(BitCodeAbbrevOp(0)); // HasExtInfo
2126 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2127 // FunctionDecl
2128 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS
2129 Abv->Add(BitCodeAbbrevOp(
2130 BitCodeAbbrevOp::Fixed,
2131 27)); // Packed Function Bits: StorageClass, Inline, InlineSpecified,
2132 // VirtualAsWritten, Pure, HasInheritedProto, HasWrittenProto,
2133 // Deleted, Trivial, TrivialForCall, Defaulted, ExplicitlyDefaulted,
2134 // IsIneligibleOrNotSelected, ImplicitReturnZero, Constexpr,
2135 // UsesSEHTry, SkippedBody, MultiVersion, LateParsed,
2136 // FriendConstraintRefersToEnclosingTemplate, Linkage
2137 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LocEnd
2138 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // ODRHash
2139 // This Array slurps the rest of the record. Fortunately we want to encode
2140 // (nearly) all the remaining (variable number of) fields in the same way.
2142 // This is:
2143 // NumParams and Params[] from FunctionDecl, and
2144 // NumOverriddenMethods, OverriddenMethods[] from CXXMethodDecl.
2146 // Add an AbbrevOp for 'size then elements' and use it here.
2147 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2148 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2149 return Abv;
2152 template <FunctionDecl::TemplatedKind Kind>
2153 std::shared_ptr<llvm::BitCodeAbbrev> getCXXMethodAbbrev() {
2154 return getFunctionDeclAbbrev<Kind>(serialization::DECL_CXX_METHOD);
2156 } // namespace
2158 void ASTWriter::WriteDeclAbbrevs() {
2159 using namespace llvm;
2161 std::shared_ptr<BitCodeAbbrev> Abv;
2163 // Abbreviation for DECL_FIELD
2164 Abv = std::make_shared<BitCodeAbbrev>();
2165 Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));
2166 // Decl
2167 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2168 7)); // Packed DeclBits: ModuleOwnershipKind,
2169 // isUsed, isReferenced, AccessSpecifier,
2171 // The following bits should be 0:
2172 // isImplicit, HasStandaloneLexicalDC, HasAttrs,
2173 // TopLevelDeclInObjCContainer,
2174 // isInvalidDecl
2175 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2176 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2177 // NamedDecl
2178 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2179 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2180 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2181 // ValueDecl
2182 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2183 // DeclaratorDecl
2184 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2185 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2186 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2187 // FieldDecl
2188 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
2189 Abv->Add(BitCodeAbbrevOp(0)); // StorageKind
2190 // Type Source Info
2191 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2192 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2193 DeclFieldAbbrev = Stream.EmitAbbrev(std::move(Abv));
2195 // Abbreviation for DECL_OBJC_IVAR
2196 Abv = std::make_shared<BitCodeAbbrev>();
2197 Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));
2198 // Decl
2199 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2200 12)); // Packed DeclBits: HasStandaloneLexicalDC,
2201 // isInvalidDecl, HasAttrs, isImplicit, isUsed,
2202 // isReferenced, TopLevelDeclInObjCContainer,
2203 // AccessSpecifier, ModuleOwnershipKind
2204 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2205 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2206 // NamedDecl
2207 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2208 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2209 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2210 // ValueDecl
2211 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2212 // DeclaratorDecl
2213 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2214 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2215 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2216 // FieldDecl
2217 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
2218 Abv->Add(BitCodeAbbrevOp(0)); // InitStyle
2219 // ObjC Ivar
2220 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl
2221 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize
2222 // Type Source Info
2223 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2224 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2225 DeclObjCIvarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2227 // Abbreviation for DECL_ENUM
2228 Abv = std::make_shared<BitCodeAbbrev>();
2229 Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));
2230 // Redeclarable
2231 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2232 // Decl
2233 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2234 7)); // Packed DeclBits: ModuleOwnershipKind,
2235 // isUsed, isReferenced, AccessSpecifier,
2237 // The following bits should be 0:
2238 // isImplicit, HasStandaloneLexicalDC, HasAttrs,
2239 // TopLevelDeclInObjCContainer,
2240 // isInvalidDecl
2241 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2242 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2243 // NamedDecl
2244 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2245 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2246 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2247 // TypeDecl
2248 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2249 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2250 // TagDecl
2251 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace
2252 Abv->Add(BitCodeAbbrevOp(
2253 BitCodeAbbrevOp::Fixed,
2254 9)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,
2255 // EmbeddedInDeclarator, IsFreeStanding,
2256 // isCompleteDefinitionRequired, ExtInfoKind
2257 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2258 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2259 // EnumDecl
2260 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddTypeRef
2261 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IntegerType
2262 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getPromotionType
2263 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 19)); // Enum Decl Bits
2264 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));// ODRHash
2265 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedMembEnum
2266 // DC
2267 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset
2268 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset
2269 DeclEnumAbbrev = Stream.EmitAbbrev(std::move(Abv));
2271 // Abbreviation for DECL_RECORD
2272 Abv = std::make_shared<BitCodeAbbrev>();
2273 Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));
2274 // Redeclarable
2275 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2276 // Decl
2277 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2278 7)); // Packed DeclBits: ModuleOwnershipKind,
2279 // isUsed, isReferenced, AccessSpecifier,
2281 // The following bits should be 0:
2282 // isImplicit, HasStandaloneLexicalDC, HasAttrs,
2283 // TopLevelDeclInObjCContainer,
2284 // isInvalidDecl
2285 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2286 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2287 // NamedDecl
2288 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2289 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2290 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2291 // TypeDecl
2292 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2293 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2294 // TagDecl
2295 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace
2296 Abv->Add(BitCodeAbbrevOp(
2297 BitCodeAbbrevOp::Fixed,
2298 9)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,
2299 // EmbeddedInDeclarator, IsFreeStanding,
2300 // isCompleteDefinitionRequired, ExtInfoKind
2301 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2302 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation
2303 // RecordDecl
2304 Abv->Add(BitCodeAbbrevOp(
2305 BitCodeAbbrevOp::Fixed,
2306 13)); // Packed Record Decl Bits: FlexibleArrayMember,
2307 // AnonymousStructUnion, hasObjectMember, hasVolatileMember,
2308 // isNonTrivialToPrimitiveDefaultInitialize,
2309 // isNonTrivialToPrimitiveCopy, isNonTrivialToPrimitiveDestroy,
2310 // hasNonTrivialToPrimitiveDefaultInitializeCUnion,
2311 // hasNonTrivialToPrimitiveDestructCUnion,
2312 // hasNonTrivialToPrimitiveCopyCUnion, isParamDestroyedInCallee,
2313 // getArgPassingRestrictions
2314 // ODRHash
2315 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 26));
2317 // DC
2318 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset
2319 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset
2320 DeclRecordAbbrev = Stream.EmitAbbrev(std::move(Abv));
2322 // Abbreviation for DECL_PARM_VAR
2323 Abv = std::make_shared<BitCodeAbbrev>();
2324 Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));
2325 // Redeclarable
2326 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2327 // Decl
2328 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2329 8)); // Packed DeclBits: ModuleOwnershipKind, isUsed,
2330 // isReferenced, AccessSpecifier,
2331 // HasStandaloneLexicalDC, HasAttrs, isImplicit,
2332 // TopLevelDeclInObjCContainer,
2333 // isInvalidDecl,
2334 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2335 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2336 // NamedDecl
2337 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2338 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2339 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2340 // ValueDecl
2341 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2342 // DeclaratorDecl
2343 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2344 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2345 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2346 // VarDecl
2347 Abv->Add(
2348 BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2349 12)); // Packed Var Decl bits: SClass, TSCSpec, InitStyle,
2350 // isARCPseudoStrong, Linkage, ModulesCodegen
2351 Abv->Add(BitCodeAbbrevOp(0)); // VarKind (local enum)
2352 // ParmVarDecl
2353 Abv->Add(BitCodeAbbrevOp(
2354 BitCodeAbbrevOp::Fixed,
2355 27)); // Packed Parm Var Decl bits: IsObjCMethodParameter, ScopeDepth,
2356 // ScopeIndex, ObjCDeclQualifier, KNRPromoted,
2357 // HasInheritedDefaultArg, HasUninstantiatedDefaultArg
2358 // Type Source Info
2359 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2360 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2361 DeclParmVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2363 // Abbreviation for DECL_TYPEDEF
2364 Abv = std::make_shared<BitCodeAbbrev>();
2365 Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));
2366 // Redeclarable
2367 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2368 // Decl
2369 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2370 7)); // Packed DeclBits: ModuleOwnershipKind,
2371 // isReferenced, isUsed, AccessSpecifier. Other
2372 // higher bits should be 0: isImplicit,
2373 // HasStandaloneLexicalDC, HasAttrs,
2374 // TopLevelDeclInObjCContainer, isInvalidDecl
2375 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2376 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2377 // NamedDecl
2378 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2379 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2380 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2381 // TypeDecl
2382 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2383 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2384 // TypedefDecl
2385 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2386 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2387 DeclTypedefAbbrev = Stream.EmitAbbrev(std::move(Abv));
2389 // Abbreviation for DECL_VAR
2390 Abv = std::make_shared<BitCodeAbbrev>();
2391 Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));
2392 // Redeclarable
2393 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2394 // Decl
2395 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2396 12)); // Packed DeclBits: HasStandaloneLexicalDC,
2397 // isInvalidDecl, HasAttrs, isImplicit, isUsed,
2398 // isReferenced, TopLevelDeclInObjCContainer,
2399 // AccessSpecifier, ModuleOwnershipKind
2400 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2401 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2402 // NamedDecl
2403 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2404 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2405 Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber
2406 // ValueDecl
2407 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2408 // DeclaratorDecl
2409 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2410 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
2411 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2412 // VarDecl
2413 Abv->Add(BitCodeAbbrevOp(
2414 BitCodeAbbrevOp::Fixed,
2415 21)); // Packed Var Decl bits: Linkage, ModulesCodegen,
2416 // SClass, TSCSpec, InitStyle,
2417 // isARCPseudoStrong, IsThisDeclarationADemotedDefinition,
2418 // isExceptionVariable, isNRVOVariable, isCXXForRangeDecl,
2419 // isInline, isInlineSpecified, isConstexpr,
2420 // isInitCapture, isPrevDeclInSameScope,
2421 // EscapingByref, HasDeducedType, ImplicitParamKind, isObjCForDecl
2422 Abv->Add(BitCodeAbbrevOp(0)); // VarKind (local enum)
2423 // Type Source Info
2424 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2425 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2426 DeclVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2428 // Abbreviation for DECL_CXX_METHOD
2429 DeclCXXMethodAbbrev =
2430 Stream.EmitAbbrev(getCXXMethodAbbrev<FunctionDecl::TK_NonTemplate>());
2431 DeclTemplateCXXMethodAbbrev = Stream.EmitAbbrev(
2432 getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplate>());
2433 DeclDependentNonTemplateCXXMethodAbbrev = Stream.EmitAbbrev(
2434 getCXXMethodAbbrev<FunctionDecl::TK_DependentNonTemplate>());
2435 DeclMemberSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(
2436 getCXXMethodAbbrev<FunctionDecl::TK_MemberSpecialization>());
2437 DeclTemplateSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(
2438 getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplateSpecialization>());
2439 DeclDependentSpecializationCXXMethodAbbrev = Stream.EmitAbbrev(
2440 getCXXMethodAbbrev<
2441 FunctionDecl::TK_DependentFunctionTemplateSpecialization>());
2443 // Abbreviation for DECL_TEMPLATE_TYPE_PARM
2444 Abv = std::make_shared<BitCodeAbbrev>();
2445 Abv->Add(BitCodeAbbrevOp(serialization::DECL_TEMPLATE_TYPE_PARM));
2446 Abv->Add(BitCodeAbbrevOp(0)); // hasTypeConstraint
2447 // Decl
2448 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2449 7)); // Packed DeclBits: ModuleOwnershipKind,
2450 // isReferenced, isUsed, AccessSpecifier. Other
2451 // higher bits should be 0: isImplicit,
2452 // HasStandaloneLexicalDC, HasAttrs,
2453 // TopLevelDeclInObjCContainer, isInvalidDecl
2454 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2455 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2456 // NamedDecl
2457 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2458 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2459 Abv->Add(BitCodeAbbrevOp(0));
2460 // TypeDecl
2461 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2462 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2463 // TemplateTypeParmDecl
2464 Abv->Add(
2465 BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // wasDeclaredWithTypename
2466 Abv->Add(BitCodeAbbrevOp(0)); // OwnsDefaultArg
2467 DeclTemplateTypeParmAbbrev = Stream.EmitAbbrev(std::move(Abv));
2469 // Abbreviation for DECL_USING_SHADOW
2470 Abv = std::make_shared<BitCodeAbbrev>();
2471 Abv->Add(BitCodeAbbrevOp(serialization::DECL_USING_SHADOW));
2472 // Redeclarable
2473 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
2474 // Decl
2475 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2476 12)); // Packed DeclBits: HasStandaloneLexicalDC,
2477 // isInvalidDecl, HasAttrs, isImplicit, isUsed,
2478 // isReferenced, TopLevelDeclInObjCContainer,
2479 // AccessSpecifier, ModuleOwnershipKind
2480 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2481 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2482 // NamedDecl
2483 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
2484 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2485 Abv->Add(BitCodeAbbrevOp(0));
2486 // UsingShadowDecl
2487 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TargetDecl
2488 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS
2489 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // UsingOrNextShadow
2490 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR,
2491 6)); // InstantiatedFromUsingShadowDecl
2492 DeclUsingShadowAbbrev = Stream.EmitAbbrev(std::move(Abv));
2494 // Abbreviation for EXPR_DECL_REF
2495 Abv = std::make_shared<BitCodeAbbrev>();
2496 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));
2497 // Stmt
2498 // Expr
2499 // PackingBits: DependenceKind, ValueKind. ObjectKind should be 0.
2500 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2501 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2502 // DeclRefExpr
2503 // Packing Bits: , HadMultipleCandidates, RefersToEnclosingVariableOrCapture,
2504 // IsImmediateEscalating, NonOdrUseReason.
2505 // GetDeclFound, HasQualifier and ExplicitTemplateArgs should be 0.
2506 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));
2507 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef
2508 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2509 DeclRefExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2511 // Abbreviation for EXPR_INTEGER_LITERAL
2512 Abv = std::make_shared<BitCodeAbbrev>();
2513 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));
2514 //Stmt
2515 // Expr
2516 // DependenceKind, ValueKind, ObjectKind
2517 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2518 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2519 // Integer Literal
2520 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2521 Abv->Add(BitCodeAbbrevOp(32)); // Bit Width
2522 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value
2523 IntegerLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
2525 // Abbreviation for EXPR_CHARACTER_LITERAL
2526 Abv = std::make_shared<BitCodeAbbrev>();
2527 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));
2528 //Stmt
2529 // Expr
2530 // DependenceKind, ValueKind, ObjectKind
2531 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2532 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2533 // Character Literal
2534 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue
2535 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2536 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // getKind
2537 CharacterLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
2539 // Abbreviation for EXPR_IMPLICIT_CAST
2540 Abv = std::make_shared<BitCodeAbbrev>();
2541 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_IMPLICIT_CAST));
2542 // Stmt
2543 // Expr
2544 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2545 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2546 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2547 // CastExpr
2548 Abv->Add(BitCodeAbbrevOp(0)); // PathSize
2549 // Packing Bits: CastKind, StoredFPFeatures, isPartOfExplicitCast
2550 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 9));
2551 // ImplicitCastExpr
2552 ExprImplicitCastAbbrev = Stream.EmitAbbrev(std::move(Abv));
2554 // Abbreviation for EXPR_BINARY_OPERATOR
2555 Abv = std::make_shared<BitCodeAbbrev>();
2556 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_BINARY_OPERATOR));
2557 // Stmt
2558 // Expr
2559 // Packing Bits: DependenceKind. ValueKind and ObjectKind should
2560 // be 0 in this case.
2561 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));
2562 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2563 // BinaryOperator
2564 Abv->Add(
2565 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures
2566 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2567 BinaryOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));
2569 // Abbreviation for EXPR_COMPOUND_ASSIGN_OPERATOR
2570 Abv = std::make_shared<BitCodeAbbrev>();
2571 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_COMPOUND_ASSIGN_OPERATOR));
2572 // Stmt
2573 // Expr
2574 // Packing Bits: DependenceKind. ValueKind and ObjectKind should
2575 // be 0 in this case.
2576 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));
2577 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2578 // BinaryOperator
2579 // Packing Bits: OpCode. The HasFPFeatures bit should be 0
2580 Abv->Add(
2581 BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures
2582 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2583 // CompoundAssignOperator
2584 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHSType
2585 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Result Type
2586 CompoundAssignOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));
2588 // Abbreviation for EXPR_CALL
2589 Abv = std::make_shared<BitCodeAbbrev>();
2590 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CALL));
2591 // Stmt
2592 // Expr
2593 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2594 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2595 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2596 // CallExpr
2597 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs
2598 Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind
2599 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2600 CallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2602 // Abbreviation for EXPR_CXX_OPERATOR_CALL
2603 Abv = std::make_shared<BitCodeAbbrev>();
2604 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_OPERATOR_CALL));
2605 // Stmt
2606 // Expr
2607 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2608 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2609 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2610 // CallExpr
2611 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs
2612 Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind
2613 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2614 // CXXOperatorCallExpr
2615 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Operator Kind
2616 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2617 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2618 CXXOperatorCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2620 // Abbreviation for EXPR_CXX_MEMBER_CALL
2621 Abv = std::make_shared<BitCodeAbbrev>();
2622 Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_MEMBER_CALL));
2623 // Stmt
2624 // Expr
2625 // Packing Bits: DependenceKind, ValueKind, ObjectKind,
2626 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));
2627 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2628 // CallExpr
2629 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs
2630 Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind
2631 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2632 // CXXMemberCallExpr
2633 CXXMemberCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2635 // Abbreviation for STMT_COMPOUND
2636 Abv = std::make_shared<BitCodeAbbrev>();
2637 Abv->Add(BitCodeAbbrevOp(serialization::STMT_COMPOUND));
2638 // Stmt
2639 // CompoundStmt
2640 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Num Stmts
2641 Abv->Add(BitCodeAbbrevOp(0)); // hasStoredFPFeatures
2642 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2643 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2644 CompoundStmtAbbrev = Stream.EmitAbbrev(std::move(Abv));
2646 Abv = std::make_shared<BitCodeAbbrev>();
2647 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));
2648 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2649 DeclContextLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
2651 Abv = std::make_shared<BitCodeAbbrev>();
2652 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));
2653 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2654 DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));
2657 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
2658 /// consumers of the AST.
2660 /// Such decls will always be deserialized from the AST file, so we would like
2661 /// this to be as restrictive as possible. Currently the predicate is driven by
2662 /// code generation requirements, if other clients have a different notion of
2663 /// what is "required" then we may have to consider an alternate scheme where
2664 /// clients can iterate over the top-level decls and get information on them,
2665 /// without necessary deserializing them. We could explicitly require such
2666 /// clients to use a separate API call to "realize" the decl. This should be
2667 /// relatively painless since they would presumably only do it for top-level
2668 /// decls.
2669 static bool isRequiredDecl(const Decl *D, ASTContext &Context,
2670 Module *WritingModule) {
2671 // Named modules have different semantics than header modules. Every named
2672 // module units owns a translation unit. So the importer of named modules
2673 // doesn't need to deserilize everything ahead of time.
2674 if (WritingModule && WritingModule->isNamedModule()) {
2675 // The PragmaCommentDecl and PragmaDetectMismatchDecl are MSVC's extension.
2676 // And the behavior of MSVC for such cases will leak this to the module
2677 // users. Given pragma is not a standard thing, the compiler has the space
2678 // to do their own decision. Let's follow MSVC here.
2679 if (isa<PragmaCommentDecl, PragmaDetectMismatchDecl>(D))
2680 return true;
2681 return false;
2684 // An ObjCMethodDecl is never considered as "required" because its
2685 // implementation container always is.
2687 // File scoped assembly or obj-c or OMP declare target implementation must be
2688 // seen.
2689 if (isa<FileScopeAsmDecl, TopLevelStmtDecl, ObjCImplDecl>(D))
2690 return true;
2692 if (WritingModule && isPartOfPerModuleInitializer(D)) {
2693 // These declarations are part of the module initializer, and are emitted
2694 // if and when the module is imported, rather than being emitted eagerly.
2695 return false;
2698 return Context.DeclMustBeEmitted(D);
2701 void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
2702 PrettyDeclStackTraceEntry CrashInfo(Context, D, SourceLocation(),
2703 "serializing");
2705 // Determine the ID for this declaration.
2706 serialization::DeclID ID;
2707 assert(!D->isFromASTFile() && "should not be emitting imported decl");
2708 serialization::DeclID &IDR = DeclIDs[D];
2709 if (IDR == 0)
2710 IDR = NextDeclID++;
2712 ID = IDR;
2714 assert(ID >= FirstDeclID && "invalid decl ID");
2716 RecordData Record;
2717 ASTDeclWriter W(*this, Context, Record);
2719 // Build a record for this declaration
2720 W.Visit(D);
2722 // Emit this declaration to the bitstream.
2723 uint64_t Offset = W.Emit(D);
2725 // Record the offset for this declaration
2726 SourceLocation Loc = D->getLocation();
2727 unsigned Index = ID - FirstDeclID;
2728 if (DeclOffsets.size() == Index)
2729 DeclOffsets.emplace_back(getAdjustedLocation(Loc), Offset,
2730 DeclTypesBlockStartOffset);
2731 else if (DeclOffsets.size() < Index) {
2732 // FIXME: Can/should this happen?
2733 DeclOffsets.resize(Index+1);
2734 DeclOffsets[Index].setLocation(getAdjustedLocation(Loc));
2735 DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);
2736 } else {
2737 llvm_unreachable("declarations should be emitted in ID order");
2740 SourceManager &SM = Context.getSourceManager();
2741 if (Loc.isValid() && SM.isLocalSourceLocation(Loc))
2742 associateDeclWithFile(D, ID);
2744 // Note declarations that should be deserialized eagerly so that we can add
2745 // them to a record in the AST file later.
2746 if (isRequiredDecl(D, Context, WritingModule))
2747 EagerlyDeserializedDecls.push_back(ID);
2750 void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {
2751 // Switch case IDs are per function body.
2752 Writer->ClearSwitchCaseIDs();
2754 assert(FD->doesThisDeclarationHaveABody());
2755 bool ModulesCodegen = false;
2756 if (!FD->isDependentContext()) {
2757 std::optional<GVALinkage> Linkage;
2758 if (Writer->WritingModule &&
2759 Writer->WritingModule->isInterfaceOrPartition()) {
2760 // When building a C++20 module interface unit or a partition unit, a
2761 // strong definition in the module interface is provided by the
2762 // compilation of that unit, not by its users. (Inline functions are still
2763 // emitted in module users.)
2764 Linkage = Writer->Context->GetGVALinkageForFunction(FD);
2765 ModulesCodegen = *Linkage >= GVA_StrongExternal;
2767 if (Writer->Context->getLangOpts().ModulesCodegen ||
2768 (FD->hasAttr<DLLExportAttr>() &&
2769 Writer->Context->getLangOpts().BuildingPCHWithObjectFile)) {
2771 // Under -fmodules-codegen, codegen is performed for all non-internal,
2772 // non-always_inline functions, unless they are available elsewhere.
2773 if (!FD->hasAttr<AlwaysInlineAttr>()) {
2774 if (!Linkage)
2775 Linkage = Writer->Context->GetGVALinkageForFunction(FD);
2776 ModulesCodegen =
2777 *Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally;
2781 Record->push_back(ModulesCodegen);
2782 if (ModulesCodegen)
2783 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(FD));
2784 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
2785 Record->push_back(CD->getNumCtorInitializers());
2786 if (CD->getNumCtorInitializers())
2787 AddCXXCtorInitializers(llvm::ArrayRef(CD->init_begin(), CD->init_end()));
2789 AddStmt(FD->getBody());