1 //===--- MultiplexExternalSemaSource.cpp  ---------------------------------===//
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 implements the event dispatching to the subscribed clients.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "clang/Sema/MultiplexExternalSemaSource.h"
13 #include "clang/AST/DeclContextInternals.h"
14 #include "clang/Sema/Lookup.h"
15 
16 using namespace clang;
17 
18 char MultiplexExternalSemaSource::ID;
19 
20 ///Constructs a new multiplexing external sema source and appends the
21 /// given element to it.
22 ///
MultiplexExternalSemaSource(ExternalSemaSource & s1,ExternalSemaSource & s2)23 MultiplexExternalSemaSource::MultiplexExternalSemaSource(ExternalSemaSource &s1,
24                                                         ExternalSemaSource &s2){
25   Sources.push_back(&s1);
26   Sources.push_back(&s2);
27 }
28 
29 // pin the vtable here.
~MultiplexExternalSemaSource()30 MultiplexExternalSemaSource::~MultiplexExternalSemaSource() {}
31 
32 ///Appends new source to the source list.
33 ///
34 ///\param[in] source - An ExternalSemaSource.
35 ///
addSource(ExternalSemaSource & source)36 void MultiplexExternalSemaSource::addSource(ExternalSemaSource &source) {
37   Sources.push_back(&source);
38 }
39 
40 //===----------------------------------------------------------------------===//
41 // ExternalASTSource.
42 //===----------------------------------------------------------------------===//
43 
GetExternalDecl(uint32_t ID)44 Decl *MultiplexExternalSemaSource::GetExternalDecl(uint32_t ID) {
45   for(size_t i = 0; i < Sources.size(); ++i)
46     if (Decl *Result = Sources[i]->GetExternalDecl(ID))
47       return Result;
48   return nullptr;
49 }
50 
CompleteRedeclChain(const Decl * D)51 void MultiplexExternalSemaSource::CompleteRedeclChain(const Decl *D) {
52   for (size_t i = 0; i < Sources.size(); ++i)
53     Sources[i]->CompleteRedeclChain(D);
54 }
55 
GetExternalSelector(uint32_t ID)56 Selector MultiplexExternalSemaSource::GetExternalSelector(uint32_t ID) {
57   Selector Sel;
58   for(size_t i = 0; i < Sources.size(); ++i) {
59     Sel = Sources[i]->GetExternalSelector(ID);
60     if (!Sel.isNull())
61       return Sel;
62   }
63   return Sel;
64 }
65 
GetNumExternalSelectors()66 uint32_t MultiplexExternalSemaSource::GetNumExternalSelectors() {
67   uint32_t total = 0;
68   for(size_t i = 0; i < Sources.size(); ++i)
69     total += Sources[i]->GetNumExternalSelectors();
70   return total;
71 }
72 
GetExternalDeclStmt(uint64_t Offset)73 Stmt *MultiplexExternalSemaSource::GetExternalDeclStmt(uint64_t Offset) {
74   for(size_t i = 0; i < Sources.size(); ++i)
75     if (Stmt *Result = Sources[i]->GetExternalDeclStmt(Offset))
76       return Result;
77   return nullptr;
78 }
79 
GetExternalCXXBaseSpecifiers(uint64_t Offset)80 CXXBaseSpecifier *MultiplexExternalSemaSource::GetExternalCXXBaseSpecifiers(
81                                                                uint64_t Offset){
82   for(size_t i = 0; i < Sources.size(); ++i)
83     if (CXXBaseSpecifier *R = Sources[i]->GetExternalCXXBaseSpecifiers(Offset))
84       return R;
85   return nullptr;
86 }
87 
88 CXXCtorInitializer **
GetExternalCXXCtorInitializers(uint64_t Offset)89 MultiplexExternalSemaSource::GetExternalCXXCtorInitializers(uint64_t Offset) {
90   for (auto *S : Sources)
91     if (auto *R = S->GetExternalCXXCtorInitializers(Offset))
92       return R;
93   return nullptr;
94 }
95 
96 ExternalASTSource::ExtKind
hasExternalDefinitions(const Decl * D)97 MultiplexExternalSemaSource::hasExternalDefinitions(const Decl *D) {
98   for (const auto &S : Sources)
99     if (auto EK = S->hasExternalDefinitions(D))
100       if (EK != EK_ReplyHazy)
101         return EK;
102   return EK_ReplyHazy;
103 }
104 
105 bool MultiplexExternalSemaSource::
FindExternalVisibleDeclsByName(const DeclContext * DC,DeclarationName Name)106 FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) {
107   bool AnyDeclsFound = false;
108   for (size_t i = 0; i < Sources.size(); ++i)
109     AnyDeclsFound |= Sources[i]->FindExternalVisibleDeclsByName(DC, Name);
110   return AnyDeclsFound;
111 }
112 
completeVisibleDeclsMap(const DeclContext * DC)113 void MultiplexExternalSemaSource::completeVisibleDeclsMap(const DeclContext *DC){
114   for(size_t i = 0; i < Sources.size(); ++i)
115     Sources[i]->completeVisibleDeclsMap(DC);
116 }
117 
FindExternalLexicalDecls(const DeclContext * DC,llvm::function_ref<bool (Decl::Kind)> IsKindWeWant,SmallVectorImpl<Decl * > & Result)118 void MultiplexExternalSemaSource::FindExternalLexicalDecls(
119     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
120     SmallVectorImpl<Decl *> &Result) {
121   for(size_t i = 0; i < Sources.size(); ++i)
122     Sources[i]->FindExternalLexicalDecls(DC, IsKindWeWant, Result);
123 }
124 
FindFileRegionDecls(FileID File,unsigned Offset,unsigned Length,SmallVectorImpl<Decl * > & Decls)125 void MultiplexExternalSemaSource::FindFileRegionDecls(FileID File,
126                                                       unsigned Offset,
127                                                       unsigned Length,
128                                                 SmallVectorImpl<Decl *> &Decls){
129   for(size_t i = 0; i < Sources.size(); ++i)
130     Sources[i]->FindFileRegionDecls(File, Offset, Length, Decls);
131 }
132 
CompleteType(TagDecl * Tag)133 void MultiplexExternalSemaSource::CompleteType(TagDecl *Tag) {
134   for(size_t i = 0; i < Sources.size(); ++i)
135     Sources[i]->CompleteType(Tag);
136 }
137 
CompleteType(ObjCInterfaceDecl * Class)138 void MultiplexExternalSemaSource::CompleteType(ObjCInterfaceDecl *Class) {
139   for(size_t i = 0; i < Sources.size(); ++i)
140     Sources[i]->CompleteType(Class);
141 }
142 
ReadComments()143 void MultiplexExternalSemaSource::ReadComments() {
144   for(size_t i = 0; i < Sources.size(); ++i)
145     Sources[i]->ReadComments();
146 }
147 
StartedDeserializing()148 void MultiplexExternalSemaSource::StartedDeserializing() {
149   for(size_t i = 0; i < Sources.size(); ++i)
150     Sources[i]->StartedDeserializing();
151 }
152 
FinishedDeserializing()153 void MultiplexExternalSemaSource::FinishedDeserializing() {
154   for(size_t i = 0; i < Sources.size(); ++i)
155     Sources[i]->FinishedDeserializing();
156 }
157 
StartTranslationUnit(ASTConsumer * Consumer)158 void MultiplexExternalSemaSource::StartTranslationUnit(ASTConsumer *Consumer) {
159   for(size_t i = 0; i < Sources.size(); ++i)
160     Sources[i]->StartTranslationUnit(Consumer);
161 }
162 
PrintStats()163 void MultiplexExternalSemaSource::PrintStats() {
164   for(size_t i = 0; i < Sources.size(); ++i)
165     Sources[i]->PrintStats();
166 }
167 
getModule(unsigned ID)168 Module *MultiplexExternalSemaSource::getModule(unsigned ID) {
169   for (size_t i = 0; i < Sources.size(); ++i)
170     if (auto M = Sources[i]->getModule(ID))
171       return M;
172   return nullptr;
173 }
174 
DeclIsFromPCHWithObjectFile(const Decl * D)175 bool MultiplexExternalSemaSource::DeclIsFromPCHWithObjectFile(const Decl *D) {
176   for (auto *S : Sources)
177     if (S->DeclIsFromPCHWithObjectFile(D))
178       return true;
179   return false;
180 }
181 
layoutRecordType(const RecordDecl * Record,uint64_t & Size,uint64_t & Alignment,llvm::DenseMap<const FieldDecl *,uint64_t> & FieldOffsets,llvm::DenseMap<const CXXRecordDecl *,CharUnits> & BaseOffsets,llvm::DenseMap<const CXXRecordDecl *,CharUnits> & VirtualBaseOffsets)182 bool MultiplexExternalSemaSource::layoutRecordType(const RecordDecl *Record,
183                                                    uint64_t &Size,
184                                                    uint64_t &Alignment,
185                       llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
186                   llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
187           llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets){
188   for(size_t i = 0; i < Sources.size(); ++i)
189     if (Sources[i]->layoutRecordType(Record, Size, Alignment, FieldOffsets,
190                                      BaseOffsets, VirtualBaseOffsets))
191       return true;
192   return false;
193 }
194 
195 void MultiplexExternalSemaSource::
getMemoryBufferSizes(MemoryBufferSizes & sizes) const196 getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
197   for(size_t i = 0; i < Sources.size(); ++i)
198     Sources[i]->getMemoryBufferSizes(sizes);
199 
200 }
201 
202 //===----------------------------------------------------------------------===//
203 // ExternalSemaSource.
204 //===----------------------------------------------------------------------===//
205 
206 
InitializeSema(Sema & S)207 void MultiplexExternalSemaSource::InitializeSema(Sema &S) {
208   for(size_t i = 0; i < Sources.size(); ++i)
209     Sources[i]->InitializeSema(S);
210 }
211 
ForgetSema()212 void MultiplexExternalSemaSource::ForgetSema() {
213   for(size_t i = 0; i < Sources.size(); ++i)
214     Sources[i]->ForgetSema();
215 }
216 
ReadMethodPool(Selector Sel)217 void MultiplexExternalSemaSource::ReadMethodPool(Selector Sel) {
218   for(size_t i = 0; i < Sources.size(); ++i)
219     Sources[i]->ReadMethodPool(Sel);
220 }
221 
updateOutOfDateSelector(Selector Sel)222 void MultiplexExternalSemaSource::updateOutOfDateSelector(Selector Sel) {
223   for(size_t i = 0; i < Sources.size(); ++i)
224     Sources[i]->updateOutOfDateSelector(Sel);
225 }
226 
ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl * > & Namespaces)227 void MultiplexExternalSemaSource::ReadKnownNamespaces(
228                                    SmallVectorImpl<NamespaceDecl*> &Namespaces){
229   for(size_t i = 0; i < Sources.size(); ++i)
230     Sources[i]->ReadKnownNamespaces(Namespaces);
231 }
232 
ReadUndefinedButUsed(llvm::MapVector<NamedDecl *,SourceLocation> & Undefined)233 void MultiplexExternalSemaSource::ReadUndefinedButUsed(
234     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
235   for(size_t i = 0; i < Sources.size(); ++i)
236     Sources[i]->ReadUndefinedButUsed(Undefined);
237 }
238 
ReadMismatchingDeleteExpressions(llvm::MapVector<FieldDecl *,llvm::SmallVector<std::pair<SourceLocation,bool>,4>> & Exprs)239 void MultiplexExternalSemaSource::ReadMismatchingDeleteExpressions(
240     llvm::MapVector<FieldDecl *,
241                     llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
242         Exprs) {
243   for (auto &Source : Sources)
244     Source->ReadMismatchingDeleteExpressions(Exprs);
245 }
246 
LookupUnqualified(LookupResult & R,Scope * S)247 bool MultiplexExternalSemaSource::LookupUnqualified(LookupResult &R, Scope *S){
248   for(size_t i = 0; i < Sources.size(); ++i)
249     Sources[i]->LookupUnqualified(R, S);
250 
251   return !R.empty();
252 }
253 
ReadTentativeDefinitions(SmallVectorImpl<VarDecl * > & TentativeDefs)254 void MultiplexExternalSemaSource::ReadTentativeDefinitions(
255                                      SmallVectorImpl<VarDecl*> &TentativeDefs) {
256   for(size_t i = 0; i < Sources.size(); ++i)
257     Sources[i]->ReadTentativeDefinitions(TentativeDefs);
258 }
259 
ReadUnusedFileScopedDecls(SmallVectorImpl<const DeclaratorDecl * > & Decls)260 void MultiplexExternalSemaSource::ReadUnusedFileScopedDecls(
261                                 SmallVectorImpl<const DeclaratorDecl*> &Decls) {
262   for(size_t i = 0; i < Sources.size(); ++i)
263     Sources[i]->ReadUnusedFileScopedDecls(Decls);
264 }
265 
ReadDelegatingConstructors(SmallVectorImpl<CXXConstructorDecl * > & Decls)266 void MultiplexExternalSemaSource::ReadDelegatingConstructors(
267                                   SmallVectorImpl<CXXConstructorDecl*> &Decls) {
268   for(size_t i = 0; i < Sources.size(); ++i)
269     Sources[i]->ReadDelegatingConstructors(Decls);
270 }
271 
ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl * > & Decls)272 void MultiplexExternalSemaSource::ReadExtVectorDecls(
273                                      SmallVectorImpl<TypedefNameDecl*> &Decls) {
274   for(size_t i = 0; i < Sources.size(); ++i)
275     Sources[i]->ReadExtVectorDecls(Decls);
276 }
277 
ReadUnusedLocalTypedefNameCandidates(llvm::SmallSetVector<const TypedefNameDecl *,4> & Decls)278 void MultiplexExternalSemaSource::ReadUnusedLocalTypedefNameCandidates(
279     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
280   for(size_t i = 0; i < Sources.size(); ++i)
281     Sources[i]->ReadUnusedLocalTypedefNameCandidates(Decls);
282 }
283 
ReadReferencedSelectors(SmallVectorImpl<std::pair<Selector,SourceLocation>> & Sels)284 void MultiplexExternalSemaSource::ReadReferencedSelectors(
285                   SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
286   for(size_t i = 0; i < Sources.size(); ++i)
287     Sources[i]->ReadReferencedSelectors(Sels);
288 }
289 
ReadWeakUndeclaredIdentifiers(SmallVectorImpl<std::pair<IdentifierInfo *,WeakInfo>> & WI)290 void MultiplexExternalSemaSource::ReadWeakUndeclaredIdentifiers(
291                    SmallVectorImpl<std::pair<IdentifierInfo*, WeakInfo> > &WI) {
292   for(size_t i = 0; i < Sources.size(); ++i)
293     Sources[i]->ReadWeakUndeclaredIdentifiers(WI);
294 }
295 
ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> & VTables)296 void MultiplexExternalSemaSource::ReadUsedVTables(
297                                   SmallVectorImpl<ExternalVTableUse> &VTables) {
298   for(size_t i = 0; i < Sources.size(); ++i)
299     Sources[i]->ReadUsedVTables(VTables);
300 }
301 
ReadPendingInstantiations(SmallVectorImpl<std::pair<ValueDecl *,SourceLocation>> & Pending)302 void MultiplexExternalSemaSource::ReadPendingInstantiations(
303                                            SmallVectorImpl<std::pair<ValueDecl*,
304                                                    SourceLocation> > &Pending) {
305   for(size_t i = 0; i < Sources.size(); ++i)
306     Sources[i]->ReadPendingInstantiations(Pending);
307 }
308 
ReadLateParsedTemplates(llvm::MapVector<const FunctionDecl *,std::unique_ptr<LateParsedTemplate>> & LPTMap)309 void MultiplexExternalSemaSource::ReadLateParsedTemplates(
310     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
311         &LPTMap) {
312   for (size_t i = 0; i < Sources.size(); ++i)
313     Sources[i]->ReadLateParsedTemplates(LPTMap);
314 }
315 
CorrectTypo(const DeclarationNameInfo & Typo,int LookupKind,Scope * S,CXXScopeSpec * SS,CorrectionCandidateCallback & CCC,DeclContext * MemberContext,bool EnteringContext,const ObjCObjectPointerType * OPT)316 TypoCorrection MultiplexExternalSemaSource::CorrectTypo(
317                                      const DeclarationNameInfo &Typo,
318                                      int LookupKind, Scope *S, CXXScopeSpec *SS,
319                                      CorrectionCandidateCallback &CCC,
320                                      DeclContext *MemberContext,
321                                      bool EnteringContext,
322                                      const ObjCObjectPointerType *OPT) {
323   for (size_t I = 0, E = Sources.size(); I < E; ++I) {
324     if (TypoCorrection C = Sources[I]->CorrectTypo(Typo, LookupKind, S, SS, CCC,
325                                                    MemberContext,
326                                                    EnteringContext, OPT))
327       return C;
328   }
329   return TypoCorrection();
330 }
331 
MaybeDiagnoseMissingCompleteType(SourceLocation Loc,QualType T)332 bool MultiplexExternalSemaSource::MaybeDiagnoseMissingCompleteType(
333     SourceLocation Loc, QualType T) {
334   for (size_t I = 0, E = Sources.size(); I < E; ++I) {
335     if (Sources[I]->MaybeDiagnoseMissingCompleteType(Loc, T))
336       return true;
337   }
338   return false;
339 }
340