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