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