Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 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 | |
| 18 | #include "clang/AST/ASTMutationListener.h" |
| 19 | #include "clang/AST/DeclGroup.h" |
| 20 | #include "clang/Serialization/ASTDeserializationListener.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace clang { |
| 25 | |
| 26 | // This ASTDeserializationListener forwards its notifications to a set of |
| 27 | // child listeners. |
| 28 | class MultiplexASTDeserializationListener |
| 29 | : public ASTDeserializationListener { |
| 30 | public: |
| 31 | // Does NOT take ownership of the elements in L. |
| 32 | MultiplexASTDeserializationListener( |
| 33 | const std::vector<ASTDeserializationListener*>& L); |
| 34 | virtual void ReaderInitialized(ASTReader *Reader); |
| 35 | virtual void IdentifierRead(serialization::IdentID ID, |
| 36 | IdentifierInfo *II); |
| 37 | virtual void TypeRead(serialization::TypeIdx Idx, QualType T); |
| 38 | virtual void DeclRead(serialization::DeclID ID, const Decl *D); |
| 39 | virtual void SelectorRead(serialization::SelectorID iD, Selector Sel); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 40 | virtual void MacroDefinitionRead(serialization::PreprocessedEntityID, |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 41 | MacroDefinition *MD); |
| 42 | private: |
| 43 | std::vector<ASTDeserializationListener*> Listeners; |
| 44 | }; |
| 45 | |
| 46 | MultiplexASTDeserializationListener::MultiplexASTDeserializationListener( |
| 47 | const std::vector<ASTDeserializationListener*>& L) |
| 48 | : Listeners(L) { |
| 49 | } |
| 50 | |
| 51 | void MultiplexASTDeserializationListener::ReaderInitialized( |
| 52 | ASTReader *Reader) { |
| 53 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 54 | Listeners[i]->ReaderInitialized(Reader); |
| 55 | } |
| 56 | |
| 57 | void MultiplexASTDeserializationListener::IdentifierRead( |
| 58 | serialization::IdentID ID, IdentifierInfo *II) { |
| 59 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 60 | Listeners[i]->IdentifierRead(ID, II); |
| 61 | } |
| 62 | |
| 63 | void MultiplexASTDeserializationListener::TypeRead( |
| 64 | serialization::TypeIdx Idx, QualType T) { |
| 65 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 66 | Listeners[i]->TypeRead(Idx, T); |
| 67 | } |
| 68 | |
| 69 | void MultiplexASTDeserializationListener::DeclRead( |
| 70 | serialization::DeclID ID, const Decl *D) { |
| 71 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 72 | Listeners[i]->DeclRead(ID, D); |
| 73 | } |
| 74 | |
| 75 | void MultiplexASTDeserializationListener::SelectorRead( |
| 76 | serialization::SelectorID ID, Selector Sel) { |
| 77 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 78 | Listeners[i]->SelectorRead(ID, Sel); |
| 79 | } |
| 80 | |
| 81 | void MultiplexASTDeserializationListener::MacroDefinitionRead( |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 82 | serialization::PreprocessedEntityID ID, MacroDefinition *MD) { |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 83 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 84 | Listeners[i]->MacroDefinitionRead(ID, MD); |
| 85 | } |
| 86 | |
| 87 | // This ASTMutationListener forwards its notifications to a set of |
| 88 | // child listeners. |
| 89 | class MultiplexASTMutationListener : public ASTMutationListener { |
| 90 | public: |
| 91 | // Does NOT take ownership of the elements in L. |
Argyrios Kyrtzidis | 409e245 | 2012-02-10 20:10:38 +0000 | [diff] [blame] | 92 | MultiplexASTMutationListener(ArrayRef<ASTMutationListener*> L); |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 93 | virtual void CompletedTagDefinition(const TagDecl *D); |
| 94 | virtual void AddedVisibleDecl(const DeclContext *DC, const Decl *D); |
| 95 | virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D); |
| 96 | virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, |
| 97 | const ClassTemplateSpecializationDecl *D); |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 98 | virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, |
| 99 | const FunctionDecl *D); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 100 | virtual void CompletedImplicitDefinition(const FunctionDecl *D); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 101 | virtual void StaticDataMemberInstantiated(const VarDecl *D); |
Argyrios Kyrtzidis | 1da95db | 2012-02-10 20:10:36 +0000 | [diff] [blame] | 102 | virtual void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, |
| 103 | const ObjCInterfaceDecl *IFD); |
| 104 | virtual void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop, |
| 105 | const ObjCPropertyDecl *OrigProp, |
| 106 | const ObjCCategoryDecl *ClassExt); |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 107 | private: |
| 108 | std::vector<ASTMutationListener*> Listeners; |
| 109 | }; |
| 110 | |
| 111 | MultiplexASTMutationListener::MultiplexASTMutationListener( |
Argyrios Kyrtzidis | 409e245 | 2012-02-10 20:10:38 +0000 | [diff] [blame] | 112 | ArrayRef<ASTMutationListener*> L) |
| 113 | : Listeners(L.begin(), L.end()) { |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void MultiplexASTMutationListener::CompletedTagDefinition(const TagDecl *D) { |
| 117 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 118 | Listeners[i]->CompletedTagDefinition(D); |
| 119 | } |
| 120 | |
| 121 | void MultiplexASTMutationListener::AddedVisibleDecl( |
| 122 | const DeclContext *DC, const Decl *D) { |
| 123 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 124 | Listeners[i]->AddedVisibleDecl(DC, D); |
| 125 | } |
| 126 | |
| 127 | void MultiplexASTMutationListener::AddedCXXImplicitMember( |
| 128 | const CXXRecordDecl *RD, const Decl *D) { |
| 129 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 130 | Listeners[i]->AddedCXXImplicitMember(RD, D); |
| 131 | } |
| 132 | void MultiplexASTMutationListener::AddedCXXTemplateSpecialization( |
| 133 | const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) { |
| 134 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 135 | Listeners[i]->AddedCXXTemplateSpecialization(TD, D); |
| 136 | } |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 137 | void MultiplexASTMutationListener::AddedCXXTemplateSpecialization( |
| 138 | const FunctionTemplateDecl *TD, const FunctionDecl *D) { |
| 139 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 140 | Listeners[i]->AddedCXXTemplateSpecialization(TD, D); |
| 141 | } |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 142 | void MultiplexASTMutationListener::CompletedImplicitDefinition( |
| 143 | const FunctionDecl *D) { |
| 144 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 145 | Listeners[i]->CompletedImplicitDefinition(D); |
| 146 | } |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 147 | void MultiplexASTMutationListener::StaticDataMemberInstantiated( |
| 148 | const VarDecl *D) { |
| 149 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 150 | Listeners[i]->StaticDataMemberInstantiated(D); |
| 151 | } |
Argyrios Kyrtzidis | 1da95db | 2012-02-10 20:10:36 +0000 | [diff] [blame] | 152 | void MultiplexASTMutationListener::AddedObjCCategoryToInterface( |
| 153 | const ObjCCategoryDecl *CatD, |
| 154 | const ObjCInterfaceDecl *IFD) { |
| 155 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 156 | Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD); |
| 157 | } |
| 158 | void MultiplexASTMutationListener::AddedObjCPropertyInClassExtension( |
| 159 | const ObjCPropertyDecl *Prop, |
| 160 | const ObjCPropertyDecl *OrigProp, |
| 161 | const ObjCCategoryDecl *ClassExt) { |
| 162 | for (size_t i = 0, e = Listeners.size(); i != e; ++i) |
| 163 | Listeners[i]->AddedObjCPropertyInClassExtension(Prop, OrigProp, ClassExt); |
| 164 | } |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 165 | |
| 166 | } // end namespace clang |
| 167 | |
| 168 | |
Argyrios Kyrtzidis | 9ec9ce1 | 2012-02-04 05:23:00 +0000 | [diff] [blame] | 169 | MultiplexConsumer::MultiplexConsumer(ArrayRef<ASTConsumer*> C) |
| 170 | : Consumers(C.begin(), C.end()), |
| 171 | MutationListener(0), DeserializationListener(0) { |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 172 | // Collect the mutation listeners and deserialization listeners of all |
| 173 | // children, and create a multiplex listener each if so. |
| 174 | std::vector<ASTMutationListener*> mutationListeners; |
| 175 | std::vector<ASTDeserializationListener*> serializationListeners; |
| 176 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) { |
| 177 | ASTMutationListener* mutationListener = |
| 178 | Consumers[i]->GetASTMutationListener(); |
| 179 | if (mutationListener) |
| 180 | mutationListeners.push_back(mutationListener); |
| 181 | ASTDeserializationListener* serializationListener = |
| 182 | Consumers[i]->GetASTDeserializationListener(); |
| 183 | if (serializationListener) |
| 184 | serializationListeners.push_back(serializationListener); |
| 185 | } |
| 186 | if (mutationListeners.size()) { |
| 187 | MutationListener.reset(new MultiplexASTMutationListener(mutationListeners)); |
| 188 | } |
| 189 | if (serializationListeners.size()) { |
| 190 | DeserializationListener.reset( |
| 191 | new MultiplexASTDeserializationListener(serializationListeners)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | MultiplexConsumer::~MultiplexConsumer() { |
| 196 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 197 | delete Consumers[i]; |
| 198 | } |
| 199 | |
| 200 | void MultiplexConsumer::Initialize(ASTContext &Context) { |
| 201 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 202 | Consumers[i]->Initialize(Context); |
| 203 | } |
| 204 | |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 205 | bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) { |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 206 | bool Continue = true; |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 207 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 208 | Continue = Continue && Consumers[i]->HandleTopLevelDecl(D); |
| 209 | return Continue; |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Rafael Espindola | 0250393 | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 212 | void MultiplexConsumer::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { |
Rafael Espindola | 234fe65 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 213 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
Rafael Espindola | 0250393 | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 214 | Consumers[i]->HandleCXXStaticMemberVarInstantiation(VD); |
Rafael Espindola | 234fe65 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 217 | void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) { |
| 218 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 219 | Consumers[i]->HandleInterestingDecl(D); |
| 220 | } |
| 221 | |
| 222 | void MultiplexConsumer::HandleTranslationUnit(ASTContext &Ctx) { |
| 223 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 224 | Consumers[i]->HandleTranslationUnit(Ctx); |
| 225 | } |
| 226 | |
| 227 | void MultiplexConsumer::HandleTagDeclDefinition(TagDecl *D) { |
| 228 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 229 | Consumers[i]->HandleTagDeclDefinition(D); |
| 230 | } |
| 231 | |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 232 | void MultiplexConsumer::HandleCXXImplicitFunctionInstantiation(FunctionDecl *D){ |
| 233 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 234 | Consumers[i]->HandleCXXImplicitFunctionInstantiation(D); |
| 235 | } |
| 236 | |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 237 | void MultiplexConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef D) { |
| 238 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 239 | Consumers[i]->HandleTopLevelDeclInObjCContainer(D); |
| 240 | } |
| 241 | |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 242 | void MultiplexConsumer::CompleteTentativeDefinition(VarDecl *D) { |
| 243 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 244 | Consumers[i]->CompleteTentativeDefinition(D); |
| 245 | } |
| 246 | |
| 247 | void MultiplexConsumer::HandleVTable( |
| 248 | CXXRecordDecl *RD, bool DefinitionRequired) { |
| 249 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 250 | Consumers[i]->HandleVTable(RD, DefinitionRequired); |
| 251 | } |
| 252 | |
| 253 | ASTMutationListener *MultiplexConsumer::GetASTMutationListener() { |
| 254 | return MutationListener.get(); |
| 255 | } |
| 256 | |
| 257 | ASTDeserializationListener *MultiplexConsumer::GetASTDeserializationListener() { |
| 258 | return DeserializationListener.get(); |
| 259 | } |
| 260 | |
| 261 | void MultiplexConsumer::PrintStats() { |
| 262 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 263 | Consumers[i]->PrintStats(); |
| 264 | } |
| 265 | |
| 266 | void MultiplexConsumer::InitializeSema(Sema &S) { |
| 267 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 268 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumers[i])) |
| 269 | SC->InitializeSema(S); |
| 270 | } |
| 271 | |
| 272 | void MultiplexConsumer::ForgetSema() { |
| 273 | for (size_t i = 0, e = Consumers.size(); i != e; ++i) |
| 274 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumers[i])) |
| 275 | SC->ForgetSema(); |
| 276 | } |