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