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