clang  6.0.0svn
MultiplexConsumer.cpp
Go to the documentation of this file.
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 
18 #include "clang/AST/DeclGroup.h"
20 
21 using namespace clang;
22 
23 namespace clang {
24 
25 // This ASTDeserializationListener forwards its notifications to a set of
26 // child listeners.
29 public:
30  // Does NOT take ownership of the elements in L.
32  const std::vector<ASTDeserializationListener*>& L);
33  void ReaderInitialized(ASTReader *Reader) override;
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;
41  MacroDefinitionRecord *MD) override;
42  void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
43 
44 private:
45  std::vector<ASTDeserializationListener *> Listeners;
46 };
47 
49  const std::vector<ASTDeserializationListener*>& L)
50  : Listeners(L) {
51 }
52 
54  ASTReader *Reader) {
55  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
56  Listeners[i]->ReaderInitialized(Reader);
57 }
58 
61  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
62  Listeners[i]->IdentifierRead(ID, II);
63 }
64 
67  for (auto &Listener : Listeners)
68  Listener->MacroRead(ID, MI);
69 }
70 
73  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
74  Listeners[i]->TypeRead(Idx, T);
75 }
76 
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 
85  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
86  Listeners[i]->SelectorRead(ID, Sel);
87 }
88 
91  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
92  Listeners[i]->MacroDefinitionRead(ID, MD);
93 }
94 
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.
104 public:
105  // Does NOT take ownership of the elements in 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,
120  Expr *ThisArg) override;
121  void CompletedImplicitDefinition(const FunctionDecl *D) override;
122  void InstantiationRequested(const ValueDecl *D) override;
123  void VariableDefinitionInstantiated(const VarDecl *D) override;
124  void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
125  void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
126  void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
127  void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
128  const ObjCInterfaceDecl *IFD) override;
129  void DeclarationMarkedUsed(const Decl *D) override;
130  void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
131  void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
132  const Attr *Attr) override;
133  void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
134  void AddedAttributeToRecord(const Attr *Attr,
135  const RecordDecl *Record) override;
136 
137 private:
138  std::vector<ASTMutationListener*> Listeners;
139 };
140 
143  : Listeners(L.begin(), L.end()) {
144 }
145 
147  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
148  Listeners[i]->CompletedTagDefinition(D);
149 }
150 
152  const DeclContext *DC, const Decl *D) {
153  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
154  Listeners[i]->AddedVisibleDecl(DC, D);
155 }
156 
158  const CXXRecordDecl *RD, const Decl *D) {
159  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
160  Listeners[i]->AddedCXXImplicitMember(RD, D);
161 }
164  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
165  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
166 }
168  const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
169  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
170  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
171 }
173  const FunctionTemplateDecl *TD, const FunctionDecl *D) {
174  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
175  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
176 }
178  const FunctionDecl *FD) {
179  for (auto &Listener : Listeners)
180  Listener->ResolvedExceptionSpec(FD);
181 }
183  QualType ReturnType) {
184  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
185  Listeners[i]->DeducedReturnType(FD, ReturnType);
186 }
188  const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
189  for (auto *L : Listeners)
190  L->ResolvedOperatorDelete(DD, Delete, ThisArg);
191 }
193  const FunctionDecl *D) {
194  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
195  Listeners[i]->CompletedImplicitDefinition(D);
196 }
198  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
199  Listeners[i]->InstantiationRequested(D);
200 }
202  const VarDecl *D) {
203  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
204  Listeners[i]->VariableDefinitionInstantiated(D);
205 }
207  const FunctionDecl *D) {
208  for (auto &Listener : Listeners)
209  Listener->FunctionDefinitionInstantiated(D);
210 }
212  const ParmVarDecl *D) {
213  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
214  Listeners[i]->DefaultArgumentInstantiated(D);
215 }
217  const FieldDecl *D) {
218  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
219  Listeners[i]->DefaultMemberInitializerInstantiated(D);
220 }
222  const ObjCCategoryDecl *CatD,
223  const ObjCInterfaceDecl *IFD) {
224  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
225  Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD);
226 }
228  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
229  Listeners[i]->DeclarationMarkedUsed(D);
230 }
232  const Decl *D) {
233  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
234  Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
235 }
237  const Decl *D, const Attr *Attr) {
238  for (auto *L : Listeners)
239  L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
240 }
242  Module *M) {
243  for (auto *L : Listeners)
244  L->RedefinedHiddenDefinition(D, M);
245 }
246 
248  const Attr *Attr,
249  const RecordDecl *Record) {
250  for (auto *L : Listeners)
251  L->AddedAttributeToRecord(Attr, Record);
252 }
253 
254 } // end namespace clang
255 
257  std::vector<std::unique_ptr<ASTConsumer>> C)
258  : Consumers(std::move(C)), MutationListener(), DeserializationListener() {
259  // Collect the mutation listeners and deserialization listeners of all
260  // children, and create a multiplex listener each if so.
261  std::vector<ASTMutationListener*> mutationListeners;
262  std::vector<ASTDeserializationListener*> serializationListeners;
263  for (auto &Consumer : Consumers) {
264  if (auto *mutationListener = Consumer->GetASTMutationListener())
265  mutationListeners.push_back(mutationListener);
266  if (auto *serializationListener = Consumer->GetASTDeserializationListener())
267  serializationListeners.push_back(serializationListener);
268  }
269  if (!mutationListeners.empty()) {
270  MutationListener =
271  llvm::make_unique<MultiplexASTMutationListener>(mutationListeners);
272  }
273  if (!serializationListeners.empty()) {
274  DeserializationListener =
275  llvm::make_unique<MultiplexASTDeserializationListener>(
276  serializationListeners);
277  }
278 }
279 
281 
283  for (auto &Consumer : Consumers)
284  Consumer->Initialize(Context);
285 }
286 
288  bool Continue = true;
289  for (auto &Consumer : Consumers)
290  Continue = Continue && Consumer->HandleTopLevelDecl(D);
291  return Continue;
292 }
293 
295  for (auto &Consumer : Consumers)
296  Consumer->HandleInlineFunctionDefinition(D);
297 }
298 
300  for (auto &Consumer : Consumers)
301  Consumer->HandleCXXStaticMemberVarInstantiation(VD);
302 }
303 
305  for (auto &Consumer : Consumers)
306  Consumer->HandleInterestingDecl(D);
307 }
308 
310  for (auto &Consumer : Consumers)
311  Consumer->HandleTranslationUnit(Ctx);
312 }
313 
315  for (auto &Consumer : Consumers)
316  Consumer->HandleTagDeclDefinition(D);
317 }
318 
320  for (auto &Consumer : Consumers)
321  Consumer->HandleTagDeclRequiredDefinition(D);
322 }
323 
325  for (auto &Consumer : Consumers)
326  Consumer->HandleCXXImplicitFunctionInstantiation(D);
327 }
328 
330  for (auto &Consumer : Consumers)
331  Consumer->HandleTopLevelDeclInObjCContainer(D);
332 }
333 
335  for (auto &Consumer : Consumers)
336  Consumer->HandleImplicitImportDecl(D);
337 }
338 
340  for (auto &Consumer : Consumers)
341  Consumer->CompleteTentativeDefinition(D);
342 }
343 
345  for (auto &Consumer : Consumers)
346  Consumer->AssignInheritanceModel(RD);
347 }
348 
350  for (auto &Consumer : Consumers)
351  Consumer->HandleVTable(RD);
352 }
353 
355  return MutationListener.get();
356 }
357 
359  return DeserializationListener.get();
360 }
361 
363  for (auto &Consumer : Consumers)
364  Consumer->PrintStats();
365 }
366 
368  bool Skip = true;
369  for (auto &Consumer : Consumers)
370  Skip = Skip && Consumer->shouldSkipFunctionBody(D);
371  return Skip;
372 }
373 
375  for (auto &Consumer : Consumers)
376  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
377  SC->InitializeSema(S);
378 }
379 
381  for (auto &Consumer : Consumers)
382  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
383  SC->ForgetSema();
384 }
MultiplexASTMutationListener(ArrayRef< ASTMutationListener *> L)
void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override
An implicit member was added after the definition was completed.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1698
void DeclarationMarkedUsed(const Decl *D) override
A declaration is marked used which was not previously marked used.
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation, it should return a pointer to an ASTMutationListener here.
void CompletedImplicitDefinition(const FunctionDecl *D) override
An implicit member got a definition.
Smart pointer class that efficiently represents Objective-C method names.
void SelectorRead(serialization::SelectorID iD, Selector Sel) override
A selector was read from the AST file.
void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override
Handle the specified top-level declaration that occurred inside and ObjC container.
A (possibly-)qualified type.
Definition: Type.h:653
void TypeRead(serialization::TypeIdx Idx, QualType T) override
A type was deserialized from the AST file.
void ResolvedExceptionSpec(const FunctionDecl *FD) override
A function&#39;s exception specification has been evaluated or instantiated.
void VariableDefinitionInstantiated(const VarDecl *D) override
A templated variable&#39;s definition was implicitly instantiated.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
void HandleInlineFunctionDefinition(FunctionDecl *D) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
void HandleVTable(CXXRecordDecl *RD) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
Declaration of a variable template.
void DeclRead(serialization::DeclID ID, const Decl *D) override
A decl was deserialized from the AST file.
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:807
Represents a variable template specialization, which refers to a variable template with a given set o...
void MacroDefinitionRead(serialization::PreprocessedEntityID, MacroDefinitionRecord *MD) override
A macro definition was read from the AST file.
void CompleteTentativeDefinition(VarDecl *D) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
ParmVarDecl - Represents a parameter to a function.
Definition: Decl.h:1514
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3482
Represents a class template specialization, which refers to a class template with a given set of temp...
One of these records is kept for each identifier that is lexed.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
Record the location of a macro definition.
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
Definition: Decl.h:2461
void ResolvedOperatorDelete(const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) override
A virtual destructor&#39;s operator delete has been resolved.
Definition: Format.h:1900
void PrintStats() override
PrintStats - If desired, print any statistics.
uint32_t MacroID
An ID number that refers to a macro in an AST file.
Definition: ASTBitCodes.h:141
void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override
A declaration is marked as OpenMP threadprivate which was not previously marked as threadprivate...
Describes a module or submodule.
Definition: Module.h:65
An abstract interface that should be implemented by clients that read ASTs and then require further s...
Definition: SemaConsumer.h:26
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
MultiplexConsumer(std::vector< std::unique_ptr< ASTConsumer >> C)
void AddedAttributeToRecord(const Attr *Attr, const RecordDecl *Record) override
An attribute was added to a RecordDecl.
void DefaultArgumentInstantiated(const ParmVarDecl *D) override
A default argument was instantiated.
void HandleTagDeclRequiredDefinition(const TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
void ReaderInitialized(ASTReader *Reader) override
The ASTReader was initialized.
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:274
void Initialize(ASTContext &Context) override
Initialize - This is called to initialize the consumer, providing the ASTContext. ...
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
Definition: Decl.h:628
Expr - This represents one expression.
Definition: Expr.h:106
const FunctionProtoType * T
void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) override
A template specialization (or partial one) was added to the template declaration. ...
void FunctionDefinitionInstantiated(const FunctionDecl *D) override
A function template&#39;s definition was instantiated.
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2620
void HandleTagDeclDefinition(TagDecl *D) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
void DefaultMemberInitializerInstantiated(const FieldDecl *D) override
A default member initializer was instantiated.
void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override
A function&#39;s return type has been deduced.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
void HandleInterestingDecl(DeclGroupRef D) override
HandleInterestingDecl - Handle the specified interesting declaration.
void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override
An identifier was deserialized from the AST file.
void DeclarationMarkedOpenMPDeclareTarget(const Decl *D, const Attr *Attr) override
A declaration is marked as OpenMP declaretarget which was not previously marked as declaretarget...
void CompletedTagDefinition(const TagDecl *D) override
A new TagDecl definition was completed.
void AssignInheritanceModel(CXXRecordDecl *RD) override
Callback invoked when an MSInheritanceAttr has been attached to a CXXRecordDecl.
uint32_t SubmoduleID
An ID number that refers to a submodule in a module file.
Definition: ASTBitCodes.h:172
void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override
Invoked when a function is implicitly instantiated.
void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override
A module definition was read from the AST file.
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2938
ObjCCategoryDecl - Represents a category declaration.
Definition: DeclObjC.h:2299
void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override
A new declaration with name has been added to a DeclContext.
void HandleImplicitImportDecl(ImportDecl *D) override
Handle an ImportDecl that was implicitly created due to an inclusion directive.
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
Definition: ASTBitCodes.h:169
void ForgetSema() override
Inform the semantic consumer that Sema is no longer available.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:3965
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Definition: ASTBitCodes.h:154
Dataflow Directional Tag Classes.
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1252
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:358
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:69
void InstantiationRequested(const ValueDecl *D) override
The instantiation of a templated function or variable was requested.
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:40
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:135
MultiplexASTDeserializationListener(const std::vector< ASTDeserializationListener *> &L)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
void MacroRead(serialization::MacroID ID, MacroInfo *MI) override
A macro was read from the AST file.
Declaration of a class template.
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, const ObjCInterfaceDecl *IFD) override
A new objc category class was added for an interface.
NamedDecl - This represents a decl with a name.
Definition: Decl.h:245
bool shouldSkipFunctionBody(Decl *D) override
This callback is called for each function if the Parser was initialized with SkipFunctionBodies set t...
Declaration of a template function.
Definition: DeclTemplate.h:958
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:89
Attr - This represents one attribute.
Definition: Attr.h:43
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override
A definition has been made visible by being redefined locally.