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 
110 ExternalLoadResult MultiplexExternalSemaSource::
FindExternalLexicalDecls(const DeclContext * DC,bool (* isKindWeWant)(Decl::Kind),SmallVectorImpl<Decl * > & Result)111 FindExternalLexicalDecls(const DeclContext *DC,
112                          bool (*isKindWeWant)(Decl::Kind),
113                          SmallVectorImpl<Decl*> &Result) {
114   for(size_t i = 0; i < Sources.size(); ++i)
115     // FIXME: The semantics of the return result is unclear to me...
116     Sources[i]->FindExternalLexicalDecls(DC, isKindWeWant, Result);
117 
118   return ELR_Success;
119 }
120 
FindFileRegionDecls(FileID File,unsigned Offset,unsigned Length,SmallVectorImpl<Decl * > & Decls)121 void MultiplexExternalSemaSource::FindFileRegionDecls(FileID File,
122                                                       unsigned Offset,
123                                                       unsigned Length,
124                                                 SmallVectorImpl<Decl *> &Decls){
125   for(size_t i = 0; i < Sources.size(); ++i)
126     Sources[i]->FindFileRegionDecls(File, Offset, Length, Decls);
127 }
128 
CompleteType(TagDecl * Tag)129 void MultiplexExternalSemaSource::CompleteType(TagDecl *Tag) {
130   for(size_t i = 0; i < Sources.size(); ++i)
131     Sources[i]->CompleteType(Tag);
132 }
133 
CompleteType(ObjCInterfaceDecl * Class)134 void MultiplexExternalSemaSource::CompleteType(ObjCInterfaceDecl *Class) {
135   for(size_t i = 0; i < Sources.size(); ++i)
136     Sources[i]->CompleteType(Class);
137 }
138 
ReadComments()139 void MultiplexExternalSemaSource::ReadComments() {
140   for(size_t i = 0; i < Sources.size(); ++i)
141     Sources[i]->ReadComments();
142 }
143 
StartedDeserializing()144 void MultiplexExternalSemaSource::StartedDeserializing() {
145   for(size_t i = 0; i < Sources.size(); ++i)
146     Sources[i]->StartedDeserializing();
147 }
148 
FinishedDeserializing()149 void MultiplexExternalSemaSource::FinishedDeserializing() {
150   for(size_t i = 0; i < Sources.size(); ++i)
151     Sources[i]->FinishedDeserializing();
152 }
153 
StartTranslationUnit(ASTConsumer * Consumer)154 void MultiplexExternalSemaSource::StartTranslationUnit(ASTConsumer *Consumer) {
155   for(size_t i = 0; i < Sources.size(); ++i)
156     Sources[i]->StartTranslationUnit(Consumer);
157 }
158 
PrintStats()159 void MultiplexExternalSemaSource::PrintStats() {
160   for(size_t i = 0; i < Sources.size(); ++i)
161     Sources[i]->PrintStats();
162 }
163 
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)164 bool MultiplexExternalSemaSource::layoutRecordType(const RecordDecl *Record,
165                                                    uint64_t &Size,
166                                                    uint64_t &Alignment,
167                       llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
168                   llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
169           llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets){
170   for(size_t i = 0; i < Sources.size(); ++i)
171     if (Sources[i]->layoutRecordType(Record, Size, Alignment, FieldOffsets,
172                                      BaseOffsets, VirtualBaseOffsets))
173       return true;
174   return false;
175 }
176 
177 void MultiplexExternalSemaSource::
getMemoryBufferSizes(MemoryBufferSizes & sizes) const178 getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
179   for(size_t i = 0; i < Sources.size(); ++i)
180     Sources[i]->getMemoryBufferSizes(sizes);
181 
182 }
183 
184 //===----------------------------------------------------------------------===//
185 // ExternalSemaSource.
186 //===----------------------------------------------------------------------===//
187 
188 
InitializeSema(Sema & S)189 void MultiplexExternalSemaSource::InitializeSema(Sema &S) {
190   for(size_t i = 0; i < Sources.size(); ++i)
191     Sources[i]->InitializeSema(S);
192 }
193 
ForgetSema()194 void MultiplexExternalSemaSource::ForgetSema() {
195   for(size_t i = 0; i < Sources.size(); ++i)
196     Sources[i]->ForgetSema();
197 }
198 
ReadMethodPool(Selector Sel)199 void MultiplexExternalSemaSource::ReadMethodPool(Selector Sel) {
200   for(size_t i = 0; i < Sources.size(); ++i)
201     Sources[i]->ReadMethodPool(Sel);
202 }
203 
ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl * > & Namespaces)204 void MultiplexExternalSemaSource::ReadKnownNamespaces(
205                                    SmallVectorImpl<NamespaceDecl*> &Namespaces){
206   for(size_t i = 0; i < Sources.size(); ++i)
207     Sources[i]->ReadKnownNamespaces(Namespaces);
208 }
209 
ReadUndefinedButUsed(llvm::DenseMap<NamedDecl *,SourceLocation> & Undefined)210 void MultiplexExternalSemaSource::ReadUndefinedButUsed(
211                          llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined){
212   for(size_t i = 0; i < Sources.size(); ++i)
213     Sources[i]->ReadUndefinedButUsed(Undefined);
214 }
215 
LookupUnqualified(LookupResult & R,Scope * S)216 bool MultiplexExternalSemaSource::LookupUnqualified(LookupResult &R, Scope *S){
217   for(size_t i = 0; i < Sources.size(); ++i)
218     Sources[i]->LookupUnqualified(R, S);
219 
220   return !R.empty();
221 }
222 
ReadTentativeDefinitions(SmallVectorImpl<VarDecl * > & TentativeDefs)223 void MultiplexExternalSemaSource::ReadTentativeDefinitions(
224                                      SmallVectorImpl<VarDecl*> &TentativeDefs) {
225   for(size_t i = 0; i < Sources.size(); ++i)
226     Sources[i]->ReadTentativeDefinitions(TentativeDefs);
227 }
228 
ReadUnusedFileScopedDecls(SmallVectorImpl<const DeclaratorDecl * > & Decls)229 void MultiplexExternalSemaSource::ReadUnusedFileScopedDecls(
230                                 SmallVectorImpl<const DeclaratorDecl*> &Decls) {
231   for(size_t i = 0; i < Sources.size(); ++i)
232     Sources[i]->ReadUnusedFileScopedDecls(Decls);
233 }
234 
ReadDelegatingConstructors(SmallVectorImpl<CXXConstructorDecl * > & Decls)235 void MultiplexExternalSemaSource::ReadDelegatingConstructors(
236                                   SmallVectorImpl<CXXConstructorDecl*> &Decls) {
237   for(size_t i = 0; i < Sources.size(); ++i)
238     Sources[i]->ReadDelegatingConstructors(Decls);
239 }
240 
ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl * > & Decls)241 void MultiplexExternalSemaSource::ReadExtVectorDecls(
242                                      SmallVectorImpl<TypedefNameDecl*> &Decls) {
243   for(size_t i = 0; i < Sources.size(); ++i)
244     Sources[i]->ReadExtVectorDecls(Decls);
245 }
246 
ReadUnusedLocalTypedefNameCandidates(llvm::SmallSetVector<const TypedefNameDecl *,4> & Decls)247 void MultiplexExternalSemaSource::ReadUnusedLocalTypedefNameCandidates(
248     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
249   for(size_t i = 0; i < Sources.size(); ++i)
250     Sources[i]->ReadUnusedLocalTypedefNameCandidates(Decls);
251 }
252 
ReadReferencedSelectors(SmallVectorImpl<std::pair<Selector,SourceLocation>> & Sels)253 void MultiplexExternalSemaSource::ReadReferencedSelectors(
254                   SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
255   for(size_t i = 0; i < Sources.size(); ++i)
256     Sources[i]->ReadReferencedSelectors(Sels);
257 }
258 
ReadWeakUndeclaredIdentifiers(SmallVectorImpl<std::pair<IdentifierInfo *,WeakInfo>> & WI)259 void MultiplexExternalSemaSource::ReadWeakUndeclaredIdentifiers(
260                    SmallVectorImpl<std::pair<IdentifierInfo*, WeakInfo> > &WI) {
261   for(size_t i = 0; i < Sources.size(); ++i)
262     Sources[i]->ReadWeakUndeclaredIdentifiers(WI);
263 }
264 
ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> & VTables)265 void MultiplexExternalSemaSource::ReadUsedVTables(
266                                   SmallVectorImpl<ExternalVTableUse> &VTables) {
267   for(size_t i = 0; i < Sources.size(); ++i)
268     Sources[i]->ReadUsedVTables(VTables);
269 }
270 
ReadPendingInstantiations(SmallVectorImpl<std::pair<ValueDecl *,SourceLocation>> & Pending)271 void MultiplexExternalSemaSource::ReadPendingInstantiations(
272                                            SmallVectorImpl<std::pair<ValueDecl*,
273                                                    SourceLocation> > &Pending) {
274   for(size_t i = 0; i < Sources.size(); ++i)
275     Sources[i]->ReadPendingInstantiations(Pending);
276 }
277 
ReadLateParsedTemplates(llvm::MapVector<const FunctionDecl *,LateParsedTemplate * > & LPTMap)278 void MultiplexExternalSemaSource::ReadLateParsedTemplates(
279     llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
280   for (size_t i = 0; i < Sources.size(); ++i)
281     Sources[i]->ReadLateParsedTemplates(LPTMap);
282 }
283 
CorrectTypo(const DeclarationNameInfo & Typo,int LookupKind,Scope * S,CXXScopeSpec * SS,CorrectionCandidateCallback & CCC,DeclContext * MemberContext,bool EnteringContext,const ObjCObjectPointerType * OPT)284 TypoCorrection MultiplexExternalSemaSource::CorrectTypo(
285                                      const DeclarationNameInfo &Typo,
286                                      int LookupKind, Scope *S, CXXScopeSpec *SS,
287                                      CorrectionCandidateCallback &CCC,
288                                      DeclContext *MemberContext,
289                                      bool EnteringContext,
290                                      const ObjCObjectPointerType *OPT) {
291   for (size_t I = 0, E = Sources.size(); I < E; ++I) {
292     if (TypoCorrection C = Sources[I]->CorrectTypo(Typo, LookupKind, S, SS, CCC,
293                                                    MemberContext,
294                                                    EnteringContext, OPT))
295       return C;
296   }
297   return TypoCorrection();
298 }
299 
MaybeDiagnoseMissingCompleteType(SourceLocation Loc,QualType T)300 bool MultiplexExternalSemaSource::MaybeDiagnoseMissingCompleteType(
301     SourceLocation Loc, QualType T) {
302   for (size_t I = 0, E = Sources.size(); I < E; ++I) {
303     if (Sources[I]->MaybeDiagnoseMissingCompleteType(Loc, T))
304       return true;
305   }
306   return false;
307 }
308