1//===- MultiplexConsumer.cpp - AST Consumer for PCH Generation --*- C++ -*-===//
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 defines the MultiplexConsumer class. It also declares and defines
10// MultiplexASTDeserializationListener and MultiplexASTMutationListener, which
11// are implementation details of MultiplexConsumer.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Frontend/MultiplexConsumer.h"
16#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/DeclGroup.h"
18
19using namespace clang;
20
21namespace clang {
22
23class NamespaceDecl;
24class TranslationUnitDecl;
25
26MultiplexASTDeserializationListener::MultiplexASTDeserializationListener(
27 const std::vector<ASTDeserializationListener*>& L)
28 : Listeners(L) {
29}
30
31void MultiplexASTDeserializationListener::ReaderInitialized(
32 ASTReader *Reader) {
33 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
34 Listeners[i]->ReaderInitialized(Reader);
35}
36
37void MultiplexASTDeserializationListener::IdentifierRead(
38 serialization::IdentID ID, IdentifierInfo *II) {
39 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
40 Listeners[i]->IdentifierRead(ID, II);
41}
42
43void MultiplexASTDeserializationListener::MacroRead(
44 serialization::MacroID ID, MacroInfo *MI) {
45 for (auto &Listener : Listeners)
46 Listener->MacroRead(ID, MI);
47}
48
49void MultiplexASTDeserializationListener::TypeRead(
50 serialization::TypeIdx Idx, QualType T) {
51 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
52 Listeners[i]->TypeRead(Idx, T);
53}
54
55void MultiplexASTDeserializationListener::DeclRead(
56 serialization::DeclID ID, const Decl *D) {
57 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
58 Listeners[i]->DeclRead(ID, D);
59}
60
61void MultiplexASTDeserializationListener::SelectorRead(
62 serialization::SelectorID ID, Selector Sel) {
63 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
64 Listeners[i]->SelectorRead(iD: ID, Sel);
65}
66
67void MultiplexASTDeserializationListener::MacroDefinitionRead(
68 serialization::PreprocessedEntityID ID, MacroDefinitionRecord *MD) {
69 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
70 Listeners[i]->MacroDefinitionRead(ID, MD);
71}
72
73void MultiplexASTDeserializationListener::ModuleRead(
74 serialization::SubmoduleID ID, Module *Mod) {
75 for (auto &Listener : Listeners)
76 Listener->ModuleRead(ID, Mod);
77}
78
79void MultiplexASTDeserializationListener::ModuleImportRead(
80 serialization::SubmoduleID ID, SourceLocation ImportLoc) {
81 for (auto &Listener : Listeners)
82 Listener->ModuleImportRead(ID, ImportLoc);
83}
84
85// This ASTMutationListener forwards its notifications to a set of
86// child listeners.
87class MultiplexASTMutationListener : public ASTMutationListener {
88public:
89 // Does NOT take ownership of the elements in L.
90 MultiplexASTMutationListener(ArrayRef<ASTMutationListener*> L);
91 void CompletedTagDefinition(const TagDecl *D) override;
92 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
93 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
94 void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
95 const ClassTemplateSpecializationDecl *D) override;
96 void AddedCXXTemplateSpecialization(const VarTemplateDecl *TD,
97 const VarTemplateSpecializationDecl *D) override;
98 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
99 const FunctionDecl *D) override;
100 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
101 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
102 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
103 const FunctionDecl *Delete,
104 Expr *ThisArg) override;
105 void CompletedImplicitDefinition(const FunctionDecl *D) override;
106 void InstantiationRequested(const ValueDecl *D) override;
107 void VariableDefinitionInstantiated(const VarDecl *D) override;
108 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
109 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
110 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
111 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
112 const ObjCInterfaceDecl *IFD) override;
113 void DeclarationMarkedUsed(const Decl *D) override;
114 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
115 void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
116 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
117 const Attr *Attr) override;
118 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
119 void AddedAttributeToRecord(const Attr *Attr,
120 const RecordDecl *Record) override;
121 void EnteringModulePurview() override;
122 void AddedManglingNumber(const Decl *D, unsigned) override;
123 void AddedStaticLocalNumbers(const Decl *D, unsigned) override;
124 void AddedAnonymousNamespace(const TranslationUnitDecl *,
125 NamespaceDecl *AnonNamespace) override;
126
127private:
128 std::vector<ASTMutationListener*> Listeners;
129};
130
131MultiplexASTMutationListener::MultiplexASTMutationListener(
132 ArrayRef<ASTMutationListener*> L)
133 : Listeners(L.begin(), L.end()) {
134}
135
136void MultiplexASTMutationListener::CompletedTagDefinition(const TagDecl *D) {
137 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
138 Listeners[i]->CompletedTagDefinition(D);
139}
140
141void MultiplexASTMutationListener::AddedVisibleDecl(
142 const DeclContext *DC, const Decl *D) {
143 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
144 Listeners[i]->AddedVisibleDecl(DC, D);
145}
146
147void MultiplexASTMutationListener::AddedCXXImplicitMember(
148 const CXXRecordDecl *RD, const Decl *D) {
149 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
150 Listeners[i]->AddedCXXImplicitMember(RD, D);
151}
152void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
153 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
154 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
155 Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
156}
157void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
158 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
159 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
160 Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
161}
162void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
163 const FunctionTemplateDecl *TD, const FunctionDecl *D) {
164 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
165 Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
166}
167void MultiplexASTMutationListener::ResolvedExceptionSpec(
168 const FunctionDecl *FD) {
169 for (auto &Listener : Listeners)
170 Listener->ResolvedExceptionSpec(FD);
171}
172void MultiplexASTMutationListener::DeducedReturnType(const FunctionDecl *FD,
173 QualType ReturnType) {
174 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
175 Listeners[i]->DeducedReturnType(FD, ReturnType);
176}
177void MultiplexASTMutationListener::ResolvedOperatorDelete(
178 const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
179 for (auto *L : Listeners)
180 L->ResolvedOperatorDelete(DD, Delete, ThisArg);
181}
182void MultiplexASTMutationListener::CompletedImplicitDefinition(
183 const FunctionDecl *D) {
184 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
185 Listeners[i]->CompletedImplicitDefinition(D);
186}
187void MultiplexASTMutationListener::InstantiationRequested(const ValueDecl *D) {
188 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
189 Listeners[i]->InstantiationRequested(D);
190}
191void MultiplexASTMutationListener::VariableDefinitionInstantiated(
192 const VarDecl *D) {
193 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
194 Listeners[i]->VariableDefinitionInstantiated(D);
195}
196void MultiplexASTMutationListener::FunctionDefinitionInstantiated(
197 const FunctionDecl *D) {
198 for (auto &Listener : Listeners)
199 Listener->FunctionDefinitionInstantiated(D);
200}
201void MultiplexASTMutationListener::DefaultArgumentInstantiated(
202 const ParmVarDecl *D) {
203 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
204 Listeners[i]->DefaultArgumentInstantiated(D);
205}
206void MultiplexASTMutationListener::DefaultMemberInitializerInstantiated(
207 const FieldDecl *D) {
208 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
209 Listeners[i]->DefaultMemberInitializerInstantiated(D);
210}
211void MultiplexASTMutationListener::AddedObjCCategoryToInterface(
212 const ObjCCategoryDecl *CatD,
213 const ObjCInterfaceDecl *IFD) {
214 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
215 Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD);
216}
217void MultiplexASTMutationListener::DeclarationMarkedUsed(const Decl *D) {
218 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
219 Listeners[i]->DeclarationMarkedUsed(D);
220}
221void MultiplexASTMutationListener::DeclarationMarkedOpenMPThreadPrivate(
222 const Decl *D) {
223 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
224 Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
225}
226void MultiplexASTMutationListener::DeclarationMarkedOpenMPAllocate(
227 const Decl *D, const Attr *A) {
228 for (ASTMutationListener *L : Listeners)
229 L->DeclarationMarkedOpenMPAllocate(D, A);
230}
231void MultiplexASTMutationListener::DeclarationMarkedOpenMPDeclareTarget(
232 const Decl *D, const Attr *Attr) {
233 for (auto *L : Listeners)
234 L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
235}
236void MultiplexASTMutationListener::RedefinedHiddenDefinition(const NamedDecl *D,
237 Module *M) {
238 for (auto *L : Listeners)
239 L->RedefinedHiddenDefinition(D, M);
240}
241
242void MultiplexASTMutationListener::AddedAttributeToRecord(
243 const Attr *Attr,
244 const RecordDecl *Record) {
245 for (auto *L : Listeners)
246 L->AddedAttributeToRecord(Attr, Record);
247}
248
249void MultiplexASTMutationListener::EnteringModulePurview() {
250 for (auto *L : Listeners)
251 L->EnteringModulePurview();
252}
253
254void MultiplexASTMutationListener::AddedManglingNumber(const Decl *D,
255 unsigned Number) {
256 for (auto *L : Listeners)
257 L->AddedManglingNumber(D, Number);
258}
259void MultiplexASTMutationListener::AddedStaticLocalNumbers(const Decl *D,
260 unsigned Number) {
261 for (auto *L : Listeners)
262 L->AddedStaticLocalNumbers(D, Number);
263}
264void MultiplexASTMutationListener::AddedAnonymousNamespace(
265 const TranslationUnitDecl *TU, NamespaceDecl *AnonNamespace) {
266 for (auto *L : Listeners)
267 L->AddedAnonymousNamespace(TU, AnonNamespace);
268}
269
270} // end namespace clang
271
272MultiplexConsumer::MultiplexConsumer(
273 std::vector<std::unique_ptr<ASTConsumer>> C)
274 : Consumers(std::move(C)) {
275 // Collect the mutation listeners and deserialization listeners of all
276 // children, and create a multiplex listener each if so.
277 std::vector<ASTMutationListener *> mutationListeners;
278 std::vector<ASTDeserializationListener*> serializationListeners;
279 for (auto &Consumer : Consumers) {
280 if (auto *mutationListener = Consumer->GetASTMutationListener())
281 mutationListeners.push_back(x: mutationListener);
282 if (auto *serializationListener = Consumer->GetASTDeserializationListener())
283 serializationListeners.push_back(x: serializationListener);
284 }
285 if (!mutationListeners.empty()) {
286 MutationListener =
287 std::make_unique<MultiplexASTMutationListener>(args&: mutationListeners);
288 }
289 if (!serializationListeners.empty()) {
290 DeserializationListener =
291 std::make_unique<MultiplexASTDeserializationListener>(
292 args&: serializationListeners);
293 }
294}
295
296MultiplexConsumer::~MultiplexConsumer() {}
297
298void MultiplexConsumer::Initialize(ASTContext &Context) {
299 for (auto &Consumer : Consumers)
300 Consumer->Initialize(Context);
301}
302
303bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) {
304 bool Continue = true;
305 for (auto &Consumer : Consumers)
306 Continue = Continue && Consumer->HandleTopLevelDecl(D);
307 return Continue;
308}
309
310void MultiplexConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {
311 for (auto &Consumer : Consumers)
312 Consumer->HandleInlineFunctionDefinition(D);
313}
314
315void MultiplexConsumer::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
316 for (auto &Consumer : Consumers)
317 Consumer->HandleCXXStaticMemberVarInstantiation(D: VD);
318}
319
320void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) {
321 for (auto &Consumer : Consumers)
322 Consumer->HandleInterestingDecl(D);
323}
324
325void MultiplexConsumer::HandleTranslationUnit(ASTContext &Ctx) {
326 for (auto &Consumer : Consumers)
327 Consumer->HandleTranslationUnit(Ctx);
328}
329
330void MultiplexConsumer::HandleTagDeclDefinition(TagDecl *D) {
331 for (auto &Consumer : Consumers)
332 Consumer->HandleTagDeclDefinition(D);
333}
334
335void MultiplexConsumer::HandleTagDeclRequiredDefinition(const TagDecl *D) {
336 for (auto &Consumer : Consumers)
337 Consumer->HandleTagDeclRequiredDefinition(D);
338}
339
340void MultiplexConsumer::HandleCXXImplicitFunctionInstantiation(FunctionDecl *D){
341 for (auto &Consumer : Consumers)
342 Consumer->HandleCXXImplicitFunctionInstantiation(D);
343}
344
345void MultiplexConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
346 for (auto &Consumer : Consumers)
347 Consumer->HandleTopLevelDeclInObjCContainer(D);
348}
349
350void MultiplexConsumer::HandleImplicitImportDecl(ImportDecl *D) {
351 for (auto &Consumer : Consumers)
352 Consumer->HandleImplicitImportDecl(D);
353}
354
355void MultiplexConsumer::CompleteTentativeDefinition(VarDecl *D) {
356 for (auto &Consumer : Consumers)
357 Consumer->CompleteTentativeDefinition(D);
358}
359
360void MultiplexConsumer::CompleteExternalDeclaration(VarDecl *D) {
361 for (auto &Consumer : Consumers)
362 Consumer->CompleteExternalDeclaration(D);
363}
364
365void MultiplexConsumer::AssignInheritanceModel(CXXRecordDecl *RD) {
366 for (auto &Consumer : Consumers)
367 Consumer->AssignInheritanceModel(RD);
368}
369
370void MultiplexConsumer::HandleVTable(CXXRecordDecl *RD) {
371 for (auto &Consumer : Consumers)
372 Consumer->HandleVTable(RD);
373}
374
375ASTMutationListener *MultiplexConsumer::GetASTMutationListener() {
376 return MutationListener.get();
377}
378
379ASTDeserializationListener *MultiplexConsumer::GetASTDeserializationListener() {
380 return DeserializationListener.get();
381}
382
383void MultiplexConsumer::PrintStats() {
384 for (auto &Consumer : Consumers)
385 Consumer->PrintStats();
386}
387
388bool MultiplexConsumer::shouldSkipFunctionBody(Decl *D) {
389 bool Skip = true;
390 for (auto &Consumer : Consumers)
391 Skip = Skip && Consumer->shouldSkipFunctionBody(D);
392 return Skip;
393}
394
395void MultiplexConsumer::InitializeSema(Sema &S) {
396 for (auto &Consumer : Consumers)
397 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Val: Consumer.get()))
398 SC->InitializeSema(S);
399}
400
401void MultiplexConsumer::ForgetSema() {
402 for (auto &Consumer : Consumers)
403 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Val: Consumer.get()))
404 SC->ForgetSema();
405}
406

source code of clang/lib/Frontend/MultiplexConsumer.cpp