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