xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/MultiplexConsumer.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
19 using namespace clang;
20 
21 namespace clang {
22 
23 class NamespaceDecl;
24 class TranslationUnitDecl;
25 
MultiplexASTDeserializationListener(const std::vector<ASTDeserializationListener * > & L)26 MultiplexASTDeserializationListener::MultiplexASTDeserializationListener(
27       const std::vector<ASTDeserializationListener*>& L)
28     : Listeners(L) {
29 }
30 
ReaderInitialized(ASTReader * Reader)31 void MultiplexASTDeserializationListener::ReaderInitialized(
32     ASTReader *Reader) {
33   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
34     Listeners[i]->ReaderInitialized(Reader);
35 }
36 
IdentifierRead(serialization::IdentifierID ID,IdentifierInfo * II)37 void MultiplexASTDeserializationListener::IdentifierRead(
38     serialization::IdentifierID ID, IdentifierInfo *II) {
39   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
40     Listeners[i]->IdentifierRead(ID, II);
41 }
42 
MacroRead(serialization::MacroID ID,MacroInfo * MI)43 void MultiplexASTDeserializationListener::MacroRead(
44     serialization::MacroID ID, MacroInfo *MI) {
45   for (auto &Listener : Listeners)
46     Listener->MacroRead(ID, MI);
47 }
48 
TypeRead(serialization::TypeIdx Idx,QualType T)49 void 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 
DeclRead(GlobalDeclID ID,const Decl * D)55 void MultiplexASTDeserializationListener::DeclRead(GlobalDeclID ID,
56                                                    const Decl *D) {
57   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
58     Listeners[i]->DeclRead(ID, D);
59 }
60 
SelectorRead(serialization::SelectorID ID,Selector Sel)61 void 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, Sel);
65 }
66 
MacroDefinitionRead(serialization::PreprocessedEntityID ID,MacroDefinitionRecord * MD)67 void 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 
ModuleRead(serialization::SubmoduleID ID,Module * Mod)73 void MultiplexASTDeserializationListener::ModuleRead(
74     serialization::SubmoduleID ID, Module *Mod) {
75   for (auto &Listener : Listeners)
76     Listener->ModuleRead(ID, Mod);
77 }
78 
ModuleImportRead(serialization::SubmoduleID ID,SourceLocation ImportLoc)79 void 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.
87 class MultiplexASTMutationListener : public ASTMutationListener {
88 public:
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 
127 private:
128   std::vector<ASTMutationListener*> Listeners;
129 };
130 
MultiplexASTMutationListener(ArrayRef<ASTMutationListener * > L)131 MultiplexASTMutationListener::MultiplexASTMutationListener(
132     ArrayRef<ASTMutationListener*> L)
133     : Listeners(L.begin(), L.end()) {
134 }
135 
CompletedTagDefinition(const TagDecl * D)136 void MultiplexASTMutationListener::CompletedTagDefinition(const TagDecl *D) {
137   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
138     Listeners[i]->CompletedTagDefinition(D);
139 }
140 
AddedVisibleDecl(const DeclContext * DC,const Decl * D)141 void 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 
AddedCXXImplicitMember(const CXXRecordDecl * RD,const Decl * D)147 void 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 }
AddedCXXTemplateSpecialization(const ClassTemplateDecl * TD,const ClassTemplateSpecializationDecl * D)152 void 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 }
AddedCXXTemplateSpecialization(const VarTemplateDecl * TD,const VarTemplateSpecializationDecl * D)157 void 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 }
AddedCXXTemplateSpecialization(const FunctionTemplateDecl * TD,const FunctionDecl * D)162 void 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 }
ResolvedExceptionSpec(const FunctionDecl * FD)167 void MultiplexASTMutationListener::ResolvedExceptionSpec(
168     const FunctionDecl *FD) {
169   for (auto &Listener : Listeners)
170     Listener->ResolvedExceptionSpec(FD);
171 }
DeducedReturnType(const FunctionDecl * FD,QualType ReturnType)172 void 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 }
ResolvedOperatorDelete(const CXXDestructorDecl * DD,const FunctionDecl * Delete,Expr * ThisArg)177 void MultiplexASTMutationListener::ResolvedOperatorDelete(
178     const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
179   for (auto *L : Listeners)
180     L->ResolvedOperatorDelete(DD, Delete, ThisArg);
181 }
CompletedImplicitDefinition(const FunctionDecl * D)182 void MultiplexASTMutationListener::CompletedImplicitDefinition(
183                                                         const FunctionDecl *D) {
184   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
185     Listeners[i]->CompletedImplicitDefinition(D);
186 }
InstantiationRequested(const ValueDecl * D)187 void MultiplexASTMutationListener::InstantiationRequested(const ValueDecl *D) {
188   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
189     Listeners[i]->InstantiationRequested(D);
190 }
VariableDefinitionInstantiated(const VarDecl * D)191 void MultiplexASTMutationListener::VariableDefinitionInstantiated(
192     const VarDecl *D) {
193   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
194     Listeners[i]->VariableDefinitionInstantiated(D);
195 }
FunctionDefinitionInstantiated(const FunctionDecl * D)196 void MultiplexASTMutationListener::FunctionDefinitionInstantiated(
197     const FunctionDecl *D) {
198   for (auto &Listener : Listeners)
199     Listener->FunctionDefinitionInstantiated(D);
200 }
DefaultArgumentInstantiated(const ParmVarDecl * D)201 void MultiplexASTMutationListener::DefaultArgumentInstantiated(
202                                                          const ParmVarDecl *D) {
203   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
204     Listeners[i]->DefaultArgumentInstantiated(D);
205 }
DefaultMemberInitializerInstantiated(const FieldDecl * D)206 void MultiplexASTMutationListener::DefaultMemberInitializerInstantiated(
207                                                            const FieldDecl *D) {
208   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
209     Listeners[i]->DefaultMemberInitializerInstantiated(D);
210 }
AddedObjCCategoryToInterface(const ObjCCategoryDecl * CatD,const ObjCInterfaceDecl * IFD)211 void 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 }
DeclarationMarkedUsed(const Decl * D)217 void MultiplexASTMutationListener::DeclarationMarkedUsed(const Decl *D) {
218   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
219     Listeners[i]->DeclarationMarkedUsed(D);
220 }
DeclarationMarkedOpenMPThreadPrivate(const Decl * D)221 void MultiplexASTMutationListener::DeclarationMarkedOpenMPThreadPrivate(
222     const Decl *D) {
223   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
224     Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
225 }
DeclarationMarkedOpenMPAllocate(const Decl * D,const Attr * A)226 void MultiplexASTMutationListener::DeclarationMarkedOpenMPAllocate(
227     const Decl *D, const Attr *A) {
228   for (ASTMutationListener *L : Listeners)
229     L->DeclarationMarkedOpenMPAllocate(D, A);
230 }
DeclarationMarkedOpenMPDeclareTarget(const Decl * D,const Attr * Attr)231 void MultiplexASTMutationListener::DeclarationMarkedOpenMPDeclareTarget(
232     const Decl *D, const Attr *Attr) {
233   for (auto *L : Listeners)
234     L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
235 }
RedefinedHiddenDefinition(const NamedDecl * D,Module * M)236 void MultiplexASTMutationListener::RedefinedHiddenDefinition(const NamedDecl *D,
237                                                              Module *M) {
238   for (auto *L : Listeners)
239     L->RedefinedHiddenDefinition(D, M);
240 }
241 
AddedAttributeToRecord(const Attr * Attr,const RecordDecl * Record)242 void MultiplexASTMutationListener::AddedAttributeToRecord(
243                                                     const Attr *Attr,
244                                                     const RecordDecl *Record) {
245   for (auto *L : Listeners)
246     L->AddedAttributeToRecord(Attr, Record);
247 }
248 
EnteringModulePurview()249 void MultiplexASTMutationListener::EnteringModulePurview() {
250   for (auto *L : Listeners)
251     L->EnteringModulePurview();
252 }
253 
AddedManglingNumber(const Decl * D,unsigned Number)254 void MultiplexASTMutationListener::AddedManglingNumber(const Decl *D,
255                                                        unsigned Number) {
256   for (auto *L : Listeners)
257     L->AddedManglingNumber(D, Number);
258 }
AddedStaticLocalNumbers(const Decl * D,unsigned Number)259 void MultiplexASTMutationListener::AddedStaticLocalNumbers(const Decl *D,
260                                                            unsigned Number) {
261   for (auto *L : Listeners)
262     L->AddedStaticLocalNumbers(D, Number);
263 }
AddedAnonymousNamespace(const TranslationUnitDecl * TU,NamespaceDecl * AnonNamespace)264 void 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 
MultiplexConsumer(std::vector<std::unique_ptr<ASTConsumer>> C)272 MultiplexConsumer::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(mutationListener);
282     if (auto *serializationListener = Consumer->GetASTDeserializationListener())
283       serializationListeners.push_back(serializationListener);
284   }
285   if (!mutationListeners.empty()) {
286     MutationListener =
287         std::make_unique<MultiplexASTMutationListener>(mutationListeners);
288   }
289   if (!serializationListeners.empty()) {
290     DeserializationListener =
291         std::make_unique<MultiplexASTDeserializationListener>(
292             serializationListeners);
293   }
294 }
295 
~MultiplexConsumer()296 MultiplexConsumer::~MultiplexConsumer() {}
297 
Initialize(ASTContext & Context)298 void MultiplexConsumer::Initialize(ASTContext &Context) {
299   for (auto &Consumer : Consumers)
300     Consumer->Initialize(Context);
301 }
302 
HandleTopLevelDecl(DeclGroupRef D)303 bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) {
304   bool Continue = true;
305   for (auto &Consumer : Consumers)
306     Continue = Continue && Consumer->HandleTopLevelDecl(D);
307   return Continue;
308 }
309 
HandleInlineFunctionDefinition(FunctionDecl * D)310 void MultiplexConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {
311   for (auto &Consumer : Consumers)
312     Consumer->HandleInlineFunctionDefinition(D);
313 }
314 
HandleCXXStaticMemberVarInstantiation(VarDecl * VD)315 void MultiplexConsumer::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
316   for (auto &Consumer : Consumers)
317     Consumer->HandleCXXStaticMemberVarInstantiation(VD);
318 }
319 
HandleInterestingDecl(DeclGroupRef D)320 void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) {
321   for (auto &Consumer : Consumers)
322     Consumer->HandleInterestingDecl(D);
323 }
324 
HandleTranslationUnit(ASTContext & Ctx)325 void MultiplexConsumer::HandleTranslationUnit(ASTContext &Ctx) {
326   for (auto &Consumer : Consumers)
327     Consumer->HandleTranslationUnit(Ctx);
328 }
329 
HandleTagDeclDefinition(TagDecl * D)330 void MultiplexConsumer::HandleTagDeclDefinition(TagDecl *D) {
331   for (auto &Consumer : Consumers)
332     Consumer->HandleTagDeclDefinition(D);
333 }
334 
HandleTagDeclRequiredDefinition(const TagDecl * D)335 void MultiplexConsumer::HandleTagDeclRequiredDefinition(const TagDecl *D) {
336   for (auto &Consumer : Consumers)
337     Consumer->HandleTagDeclRequiredDefinition(D);
338 }
339 
HandleCXXImplicitFunctionInstantiation(FunctionDecl * D)340 void MultiplexConsumer::HandleCXXImplicitFunctionInstantiation(FunctionDecl *D){
341   for (auto &Consumer : Consumers)
342     Consumer->HandleCXXImplicitFunctionInstantiation(D);
343 }
344 
HandleTopLevelDeclInObjCContainer(DeclGroupRef D)345 void MultiplexConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
346   for (auto &Consumer : Consumers)
347     Consumer->HandleTopLevelDeclInObjCContainer(D);
348 }
349 
HandleImplicitImportDecl(ImportDecl * D)350 void MultiplexConsumer::HandleImplicitImportDecl(ImportDecl *D) {
351   for (auto &Consumer : Consumers)
352     Consumer->HandleImplicitImportDecl(D);
353 }
354 
CompleteTentativeDefinition(VarDecl * D)355 void MultiplexConsumer::CompleteTentativeDefinition(VarDecl *D) {
356   for (auto &Consumer : Consumers)
357     Consumer->CompleteTentativeDefinition(D);
358 }
359 
CompleteExternalDeclaration(DeclaratorDecl * D)360 void MultiplexConsumer::CompleteExternalDeclaration(DeclaratorDecl *D) {
361   for (auto &Consumer : Consumers)
362     Consumer->CompleteExternalDeclaration(D);
363 }
364 
AssignInheritanceModel(CXXRecordDecl * RD)365 void MultiplexConsumer::AssignInheritanceModel(CXXRecordDecl *RD) {
366   for (auto &Consumer : Consumers)
367     Consumer->AssignInheritanceModel(RD);
368 }
369 
HandleVTable(CXXRecordDecl * RD)370 void MultiplexConsumer::HandleVTable(CXXRecordDecl *RD) {
371   for (auto &Consumer : Consumers)
372     Consumer->HandleVTable(RD);
373 }
374 
GetASTMutationListener()375 ASTMutationListener *MultiplexConsumer::GetASTMutationListener() {
376   return MutationListener.get();
377 }
378 
GetASTDeserializationListener()379 ASTDeserializationListener *MultiplexConsumer::GetASTDeserializationListener() {
380   return DeserializationListener.get();
381 }
382 
PrintStats()383 void MultiplexConsumer::PrintStats() {
384   for (auto &Consumer : Consumers)
385     Consumer->PrintStats();
386 }
387 
shouldSkipFunctionBody(Decl * D)388 bool MultiplexConsumer::shouldSkipFunctionBody(Decl *D) {
389   bool Skip = true;
390   for (auto &Consumer : Consumers)
391     Skip = Skip && Consumer->shouldSkipFunctionBody(D);
392   return Skip;
393 }
394 
InitializeSema(Sema & S)395 void MultiplexConsumer::InitializeSema(Sema &S) {
396   for (auto &Consumer : Consumers)
397     if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
398       SC->InitializeSema(S);
399 }
400 
ForgetSema()401 void MultiplexConsumer::ForgetSema() {
402   for (auto &Consumer : Consumers)
403     if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
404       SC->ForgetSema();
405 }
406