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