clang API Documentation

MultiplexConsumer.cpp
Go to the documentation of this file.
00001 //===- MultiplexConsumer.cpp - AST Consumer for PCH Generation --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the MultiplexConsumer class. It also declares and defines
00011 //  MultiplexASTDeserializationListener and  MultiplexASTMutationListener, which
00012 //  are implementation details of MultiplexConsumer.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "clang/Frontend/MultiplexConsumer.h"
00017 #include "clang/AST/ASTMutationListener.h"
00018 #include "clang/AST/DeclGroup.h"
00019 #include "clang/Serialization/ASTDeserializationListener.h"
00020 
00021 using namespace clang;
00022 
00023 namespace clang {
00024 
00025 // This ASTDeserializationListener forwards its notifications to a set of
00026 // child listeners.
00027 class MultiplexASTDeserializationListener
00028     : public ASTDeserializationListener {
00029 public:
00030   // Does NOT take ownership of the elements in L.
00031   MultiplexASTDeserializationListener(
00032       const std::vector<ASTDeserializationListener*>& L);
00033   void ReaderInitialized(ASTReader *Reader) override;
00034   void IdentifierRead(serialization::IdentID ID,
00035                       IdentifierInfo *II) override;
00036   void TypeRead(serialization::TypeIdx Idx, QualType T) override;
00037   void DeclRead(serialization::DeclID ID, const Decl *D) override;
00038   void SelectorRead(serialization::SelectorID iD, Selector Sel) override;
00039   void MacroDefinitionRead(serialization::PreprocessedEntityID,
00040                            MacroDefinition *MD) override;
00041 private:
00042   std::vector<ASTDeserializationListener*> Listeners;
00043 };
00044 
00045 MultiplexASTDeserializationListener::MultiplexASTDeserializationListener(
00046       const std::vector<ASTDeserializationListener*>& L)
00047     : Listeners(L) {
00048 }
00049 
00050 void MultiplexASTDeserializationListener::ReaderInitialized(
00051     ASTReader *Reader) {
00052   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00053     Listeners[i]->ReaderInitialized(Reader);
00054 }
00055 
00056 void MultiplexASTDeserializationListener::IdentifierRead(
00057     serialization::IdentID ID, IdentifierInfo *II) {
00058   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00059     Listeners[i]->IdentifierRead(ID, II);
00060 }
00061 
00062 void MultiplexASTDeserializationListener::TypeRead(
00063     serialization::TypeIdx Idx, QualType T) {
00064   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00065     Listeners[i]->TypeRead(Idx, T);
00066 }
00067 
00068 void MultiplexASTDeserializationListener::DeclRead(
00069     serialization::DeclID ID, const Decl *D) {
00070   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00071     Listeners[i]->DeclRead(ID, D);
00072 }
00073 
00074 void MultiplexASTDeserializationListener::SelectorRead(
00075     serialization::SelectorID ID, Selector Sel) {
00076   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00077     Listeners[i]->SelectorRead(ID, Sel);
00078 }
00079 
00080 void MultiplexASTDeserializationListener::MacroDefinitionRead(
00081     serialization::PreprocessedEntityID ID, MacroDefinition *MD) {
00082   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00083     Listeners[i]->MacroDefinitionRead(ID, MD);
00084 }
00085 
00086 // This ASTMutationListener forwards its notifications to a set of
00087 // child listeners.
00088 class MultiplexASTMutationListener : public ASTMutationListener {
00089 public:
00090   // Does NOT take ownership of the elements in L.
00091   MultiplexASTMutationListener(ArrayRef<ASTMutationListener*> L);
00092   void CompletedTagDefinition(const TagDecl *D) override;
00093   void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
00094   void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
00095   void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
00096                             const ClassTemplateSpecializationDecl *D) override;
00097   void AddedCXXTemplateSpecialization(const VarTemplateDecl *TD,
00098                                const VarTemplateSpecializationDecl *D) override;
00099   void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
00100                                       const FunctionDecl *D) override;
00101   void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
00102   void CompletedImplicitDefinition(const FunctionDecl *D) override;
00103   void StaticDataMemberInstantiated(const VarDecl *D) override;
00104   void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
00105                                     const ObjCInterfaceDecl *IFD) override;
00106   void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
00107                                     const ObjCPropertyDecl *OrigProp,
00108                                     const ObjCCategoryDecl *ClassExt) override;
00109   void DeclarationMarkedUsed(const Decl *D) override;
00110   void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
00111 
00112 private:
00113   std::vector<ASTMutationListener*> Listeners;
00114 };
00115 
00116 MultiplexASTMutationListener::MultiplexASTMutationListener(
00117     ArrayRef<ASTMutationListener*> L)
00118     : Listeners(L.begin(), L.end()) {
00119 }
00120 
00121 void MultiplexASTMutationListener::CompletedTagDefinition(const TagDecl *D) {
00122   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00123     Listeners[i]->CompletedTagDefinition(D);
00124 }
00125 
00126 void MultiplexASTMutationListener::AddedVisibleDecl(
00127     const DeclContext *DC, const Decl *D) {
00128   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00129     Listeners[i]->AddedVisibleDecl(DC, D);
00130 }
00131 
00132 void MultiplexASTMutationListener::AddedCXXImplicitMember(
00133     const CXXRecordDecl *RD, const Decl *D) {
00134   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00135     Listeners[i]->AddedCXXImplicitMember(RD, D);
00136 }
00137 void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
00138     const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
00139   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00140     Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
00141 }
00142 void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
00143     const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
00144   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00145     Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
00146 }
00147 void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
00148     const FunctionTemplateDecl *TD, const FunctionDecl *D) {
00149   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00150     Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
00151 }
00152 void MultiplexASTMutationListener::DeducedReturnType(const FunctionDecl *FD,
00153                                                      QualType ReturnType) {
00154   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00155     Listeners[i]->DeducedReturnType(FD, ReturnType);
00156 }
00157 void MultiplexASTMutationListener::CompletedImplicitDefinition(
00158                                                         const FunctionDecl *D) {
00159   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00160     Listeners[i]->CompletedImplicitDefinition(D);
00161 }
00162 void MultiplexASTMutationListener::StaticDataMemberInstantiated(
00163                                                              const VarDecl *D) {
00164   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00165     Listeners[i]->StaticDataMemberInstantiated(D);
00166 }
00167 void MultiplexASTMutationListener::AddedObjCCategoryToInterface(
00168                                                  const ObjCCategoryDecl *CatD,
00169                                                  const ObjCInterfaceDecl *IFD) {
00170   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00171     Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD);
00172 }
00173 void MultiplexASTMutationListener::AddedObjCPropertyInClassExtension(
00174                                              const ObjCPropertyDecl *Prop,
00175                                              const ObjCPropertyDecl *OrigProp,
00176                                              const ObjCCategoryDecl *ClassExt) {
00177   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00178     Listeners[i]->AddedObjCPropertyInClassExtension(Prop, OrigProp, ClassExt);
00179 }
00180 void MultiplexASTMutationListener::DeclarationMarkedUsed(const Decl *D) {
00181   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00182     Listeners[i]->DeclarationMarkedUsed(D);
00183 }
00184 void MultiplexASTMutationListener::DeclarationMarkedOpenMPThreadPrivate(
00185     const Decl *D) {
00186   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
00187     Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
00188 }
00189 
00190 }  // end namespace clang
00191 
00192 MultiplexConsumer::MultiplexConsumer(
00193     std::vector<std::unique_ptr<ASTConsumer>> C)
00194     : Consumers(std::move(C)), MutationListener(), DeserializationListener() {
00195   // Collect the mutation listeners and deserialization listeners of all
00196   // children, and create a multiplex listener each if so.
00197   std::vector<ASTMutationListener*> mutationListeners;
00198   std::vector<ASTDeserializationListener*> serializationListeners;
00199   for (auto &Consumer : Consumers) {
00200     if (auto *mutationListener = Consumer->GetASTMutationListener())
00201       mutationListeners.push_back(mutationListener);
00202     if (auto *serializationListener = Consumer->GetASTDeserializationListener())
00203       serializationListeners.push_back(serializationListener);
00204   }
00205   if (!mutationListeners.empty()) {
00206     MutationListener =
00207         llvm::make_unique<MultiplexASTMutationListener>(mutationListeners);
00208   }
00209   if (!serializationListeners.empty()) {
00210     DeserializationListener =
00211         llvm::make_unique<MultiplexASTDeserializationListener>(
00212             serializationListeners);
00213   }
00214 }
00215 
00216 MultiplexConsumer::~MultiplexConsumer() {}
00217 
00218 void MultiplexConsumer::Initialize(ASTContext &Context) {
00219   for (auto &Consumer : Consumers)
00220     Consumer->Initialize(Context);
00221 }
00222 
00223 bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) {
00224   bool Continue = true;
00225   for (auto &Consumer : Consumers)
00226     Continue = Continue && Consumer->HandleTopLevelDecl(D);
00227   return Continue;
00228 }
00229 
00230 void MultiplexConsumer::HandleInlineMethodDefinition(CXXMethodDecl *D) {
00231   for (auto &Consumer : Consumers)
00232     Consumer->HandleInlineMethodDefinition(D);
00233 }
00234 
00235 void MultiplexConsumer::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
00236   for (auto &Consumer : Consumers)
00237     Consumer->HandleCXXStaticMemberVarInstantiation(VD);
00238 }
00239 
00240 void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) {
00241   for (auto &Consumer : Consumers)
00242     Consumer->HandleInterestingDecl(D);
00243 }
00244 
00245 void MultiplexConsumer::HandleTranslationUnit(ASTContext &Ctx) {
00246   for (auto &Consumer : Consumers)
00247     Consumer->HandleTranslationUnit(Ctx);
00248 }
00249 
00250 void MultiplexConsumer::HandleTagDeclDefinition(TagDecl *D) {
00251   for (auto &Consumer : Consumers)
00252     Consumer->HandleTagDeclDefinition(D);
00253 }
00254 
00255 void MultiplexConsumer::HandleTagDeclRequiredDefinition(const TagDecl *D) {
00256   for (auto &Consumer : Consumers)
00257     Consumer->HandleTagDeclRequiredDefinition(D);
00258 }
00259 
00260 void MultiplexConsumer::HandleCXXImplicitFunctionInstantiation(FunctionDecl *D){
00261   for (auto &Consumer : Consumers)
00262     Consumer->HandleCXXImplicitFunctionInstantiation(D);
00263 }
00264 
00265 void MultiplexConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
00266   for (auto &Consumer : Consumers)
00267     Consumer->HandleTopLevelDeclInObjCContainer(D);
00268 }
00269 
00270 void MultiplexConsumer::HandleImplicitImportDecl(ImportDecl *D) {
00271   for (auto &Consumer : Consumers)
00272     Consumer->HandleImplicitImportDecl(D);
00273 }
00274 
00275 void MultiplexConsumer::HandleLinkerOptionPragma(llvm::StringRef Opts) {
00276   for (auto &Consumer : Consumers)
00277     Consumer->HandleLinkerOptionPragma(Opts);
00278 }
00279 
00280 void MultiplexConsumer::HandleDetectMismatch(llvm::StringRef Name, llvm::StringRef Value) {
00281   for (auto &Consumer : Consumers)
00282     Consumer->HandleDetectMismatch(Name, Value);
00283 }
00284 
00285 void MultiplexConsumer::HandleDependentLibrary(llvm::StringRef Lib) {
00286   for (auto &Consumer : Consumers)
00287     Consumer->HandleDependentLibrary(Lib);
00288 }
00289 
00290 void MultiplexConsumer::CompleteTentativeDefinition(VarDecl *D) {
00291   for (auto &Consumer : Consumers)
00292     Consumer->CompleteTentativeDefinition(D);
00293 }
00294 
00295 void MultiplexConsumer::HandleVTable(
00296     CXXRecordDecl *RD, bool DefinitionRequired) {
00297   for (auto &Consumer : Consumers)
00298     Consumer->HandleVTable(RD, DefinitionRequired);
00299 }
00300 
00301 ASTMutationListener *MultiplexConsumer::GetASTMutationListener() {
00302   return MutationListener.get();
00303 }
00304 
00305 ASTDeserializationListener *MultiplexConsumer::GetASTDeserializationListener() {
00306   return DeserializationListener.get();
00307 }
00308 
00309 void MultiplexConsumer::PrintStats() {
00310   for (auto &Consumer : Consumers)
00311     Consumer->PrintStats();
00312 }
00313 
00314 void MultiplexConsumer::InitializeSema(Sema &S) {
00315   for (auto &Consumer : Consumers)
00316     if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
00317       SC->InitializeSema(S);
00318 }
00319 
00320 void MultiplexConsumer::ForgetSema() {
00321   for (auto &Consumer : Consumers)
00322     if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
00323       SC->ForgetSema();
00324 }