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