/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaLookup.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--------------------- SemaLookup.cpp - Name Lookup ------------------===// |
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 name lookup for C, C++, Objective-C, and |
10 | | // Objective-C++. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/AST/ASTContext.h" |
15 | | #include "clang/AST/CXXInheritance.h" |
16 | | #include "clang/AST/Decl.h" |
17 | | #include "clang/AST/DeclCXX.h" |
18 | | #include "clang/AST/DeclLookups.h" |
19 | | #include "clang/AST/DeclObjC.h" |
20 | | #include "clang/AST/DeclTemplate.h" |
21 | | #include "clang/AST/Expr.h" |
22 | | #include "clang/AST/ExprCXX.h" |
23 | | #include "clang/Basic/Builtins.h" |
24 | | #include "clang/Basic/FileManager.h" |
25 | | #include "clang/Basic/LangOptions.h" |
26 | | #include "clang/Lex/HeaderSearch.h" |
27 | | #include "clang/Lex/ModuleLoader.h" |
28 | | #include "clang/Lex/Preprocessor.h" |
29 | | #include "clang/Sema/DeclSpec.h" |
30 | | #include "clang/Sema/Lookup.h" |
31 | | #include "clang/Sema/Overload.h" |
32 | | #include "clang/Sema/Scope.h" |
33 | | #include "clang/Sema/ScopeInfo.h" |
34 | | #include "clang/Sema/Sema.h" |
35 | | #include "clang/Sema/SemaInternal.h" |
36 | | #include "clang/Sema/TemplateDeduction.h" |
37 | | #include "clang/Sema/TypoCorrection.h" |
38 | | #include "llvm/ADT/STLExtras.h" |
39 | | #include "llvm/ADT/SmallPtrSet.h" |
40 | | #include "llvm/ADT/TinyPtrVector.h" |
41 | | #include "llvm/ADT/edit_distance.h" |
42 | | #include "llvm/Support/ErrorHandling.h" |
43 | | #include <algorithm> |
44 | | #include <iterator> |
45 | | #include <list> |
46 | | #include <set> |
47 | | #include <utility> |
48 | | #include <vector> |
49 | | |
50 | | #include "OpenCLBuiltins.inc" |
51 | | |
52 | | using namespace clang; |
53 | | using namespace sema; |
54 | | |
55 | | namespace { |
56 | | class UnqualUsingEntry { |
57 | | const DeclContext *Nominated; |
58 | | const DeclContext *CommonAncestor; |
59 | | |
60 | | public: |
61 | | UnqualUsingEntry(const DeclContext *Nominated, |
62 | | const DeclContext *CommonAncestor) |
63 | 11.2M | : Nominated(Nominated), CommonAncestor(CommonAncestor) { |
64 | 11.2M | } |
65 | | |
66 | 30.1M | const DeclContext *getCommonAncestor() const { |
67 | 30.1M | return CommonAncestor; |
68 | 30.1M | } |
69 | | |
70 | 10.8M | const DeclContext *getNominatedNamespace() const { |
71 | 10.8M | return Nominated; |
72 | 10.8M | } |
73 | | |
74 | | // Sort by the pointer value of the common ancestor. |
75 | | struct Comparator { |
76 | 47.8k | bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { |
77 | 47.8k | return L.getCommonAncestor() < R.getCommonAncestor(); |
78 | 47.8k | } |
79 | | |
80 | 15.2M | bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { |
81 | 15.2M | return E.getCommonAncestor() < DC; |
82 | 15.2M | } |
83 | | |
84 | 14.7M | bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { |
85 | 14.7M | return DC < E.getCommonAncestor(); |
86 | 14.7M | } |
87 | | }; |
88 | | }; |
89 | | |
90 | | /// A collection of using directives, as used by C++ unqualified |
91 | | /// lookup. |
92 | | class UnqualUsingDirectiveSet { |
93 | | Sema &SemaRef; |
94 | | |
95 | | typedef SmallVector<UnqualUsingEntry, 8> ListTy; |
96 | | |
97 | | ListTy list; |
98 | | llvm::SmallPtrSet<DeclContext*, 8> visited; |
99 | | |
100 | | public: |
101 | 85.0M | UnqualUsingDirectiveSet(Sema &SemaRef) : SemaRef(SemaRef) {} |
102 | | |
103 | 63.9M | void visitScopeChain(Scope *S, Scope *InnermostFileScope) { |
104 | | // C++ [namespace.udir]p1: |
105 | | // During unqualified name lookup, the names appear as if they |
106 | | // were declared in the nearest enclosing namespace which contains |
107 | | // both the using-directive and the nominated namespace. |
108 | 63.9M | DeclContext *InnermostFileDC = InnermostFileScope->getEntity(); |
109 | 63.9M | assert(InnermostFileDC && InnermostFileDC->isFileContext()); |
110 | | |
111 | 273M | for (; S; S = S->getParent()209M ) { |
112 | | // C++ [namespace.udir]p1: |
113 | | // A using-directive shall not appear in class scope, but may |
114 | | // appear in namespace scope or in block scope. |
115 | 209M | DeclContext *Ctx = S->getEntity(); |
116 | 209M | if (Ctx && Ctx->isFileContext()154M ) { |
117 | 96.9M | visit(Ctx, Ctx); |
118 | 112M | } else if (!Ctx || Ctx->isFunctionOrMethod()57.4M ) { |
119 | 63.0M | for (auto *I : S->using_directives()) |
120 | 27.8k | if (SemaRef.isVisible(I)) |
121 | 27.8k | visit(I, InnermostFileDC); |
122 | 63.0M | } |
123 | 209M | } |
124 | 63.9M | } |
125 | | |
126 | | // Visits a context and collect all of its using directives |
127 | | // recursively. Treats all using directives as if they were |
128 | | // declared in the context. |
129 | | // |
130 | | // A given context is only every visited once, so it is important |
131 | | // that contexts be visited from the inside out in order to get |
132 | | // the effective DCs right. |
133 | 97.0M | void visit(DeclContext *DC, DeclContext *EffectiveDC) { |
134 | 97.0M | if (!visited.insert(DC).second) |
135 | 2.61k | return; |
136 | | |
137 | 97.0M | addUsingDirectives(DC, EffectiveDC); |
138 | 97.0M | } |
139 | | |
140 | | // Visits a using directive and collects all of its using |
141 | | // directives recursively. Treats all using directives as if they |
142 | | // were declared in the effective DC. |
143 | 27.8k | void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
144 | 27.8k | DeclContext *NS = UD->getNominatedNamespace(); |
145 | 27.8k | if (!visited.insert(NS).second) |
146 | 0 | return; |
147 | | |
148 | 27.8k | addUsingDirective(UD, EffectiveDC); |
149 | 27.8k | addUsingDirectives(NS, EffectiveDC); |
150 | 27.8k | } |
151 | | |
152 | | // Adds all the using directives in a context (and those nominated |
153 | | // by its using directives, transitively) as if they appeared in |
154 | | // the given effective context. |
155 | 97.0M | void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { |
156 | 97.0M | SmallVector<DeclContext*, 4> queue; |
157 | 108M | while (true) { |
158 | 108M | for (auto UD : DC->using_directives()) { |
159 | 22.4M | DeclContext *NS = UD->getNominatedNamespace(); |
160 | 22.4M | if (SemaRef.isVisible(UD) && visited.insert(NS).second22.3M ) { |
161 | 11.2M | addUsingDirective(UD, EffectiveDC); |
162 | 11.2M | queue.push_back(NS); |
163 | 11.2M | } |
164 | 22.4M | } |
165 | | |
166 | 108M | if (queue.empty()) |
167 | 97.0M | return; |
168 | | |
169 | 11.2M | DC = queue.pop_back_val(); |
170 | 11.2M | } |
171 | 97.0M | } |
172 | | |
173 | | // Add a using directive as if it had been declared in the given |
174 | | // context. This helps implement C++ [namespace.udir]p3: |
175 | | // The using-directive is transitive: if a scope contains a |
176 | | // using-directive that nominates a second namespace that itself |
177 | | // contains using-directives, the effect is as if the |
178 | | // using-directives from the second namespace also appeared in |
179 | | // the first. |
180 | 11.2M | void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
181 | | // Find the common ancestor between the effective context and |
182 | | // the nominated namespace. |
183 | 11.2M | DeclContext *Common = UD->getNominatedNamespace(); |
184 | 22.5M | while (!Common->Encloses(EffectiveDC)) |
185 | 11.2M | Common = Common->getParent(); |
186 | 11.2M | Common = Common->getPrimaryContext(); |
187 | | |
188 | 11.2M | list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); |
189 | 11.2M | } |
190 | | |
191 | 63.9M | void done() { llvm::sort(list, UnqualUsingEntry::Comparator()); } |
192 | | |
193 | | typedef ListTy::const_iterator const_iterator; |
194 | | |
195 | 69.1M | const_iterator begin() const { return list.begin(); } |
196 | 69.1M | const_iterator end() const { return list.end(); } |
197 | | |
198 | | llvm::iterator_range<const_iterator> |
199 | 69.1M | getNamespacesFor(DeclContext *DC) const { |
200 | 69.1M | return llvm::make_range(std::equal_range(begin(), end(), |
201 | 69.1M | DC->getPrimaryContext(), |
202 | 69.1M | UnqualUsingEntry::Comparator())); |
203 | 69.1M | } |
204 | | }; |
205 | | } // end anonymous namespace |
206 | | |
207 | | // Retrieve the set of identifier namespaces that correspond to a |
208 | | // specific kind of name lookup. |
209 | | static inline unsigned getIDNS(Sema::LookupNameKind NameKind, |
210 | | bool CPlusPlus, |
211 | 194M | bool Redeclaration) { |
212 | 194M | unsigned IDNS = 0; |
213 | 194M | switch (NameKind) { |
214 | 2.17k | case Sema::LookupObjCImplicitSelfParam: |
215 | 182M | case Sema::LookupOrdinaryName: |
216 | 182M | case Sema::LookupRedeclarationWithLinkage: |
217 | 182M | case Sema::LookupLocalFriendName: |
218 | 182M | case Sema::LookupDestructorName: |
219 | 182M | IDNS = Decl::IDNS_Ordinary; |
220 | 182M | if (CPlusPlus) { |
221 | 89.3M | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; |
222 | 89.3M | if (Redeclaration) |
223 | 24.2M | IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; |
224 | 89.3M | } |
225 | 182M | if (Redeclaration) |
226 | 51.6M | IDNS |= Decl::IDNS_LocalExtern; |
227 | 182M | break; |
228 | | |
229 | 2.87M | case Sema::LookupOperatorName: |
230 | | // Operator lookup is its own crazy thing; it is not the same |
231 | | // as (e.g.) looking up an operator name for redeclaration. |
232 | 2.87M | assert(!Redeclaration && "cannot do redeclaration operator lookup"); |
233 | 0 | IDNS = Decl::IDNS_NonMemberOperator; |
234 | 2.87M | break; |
235 | | |
236 | 2.38M | case Sema::LookupTagName: |
237 | 2.38M | if (CPlusPlus) { |
238 | 870k | IDNS = Decl::IDNS_Type; |
239 | | |
240 | | // When looking for a redeclaration of a tag name, we add: |
241 | | // 1) TagFriend to find undeclared friend decls |
242 | | // 2) Namespace because they can't "overload" with tag decls. |
243 | | // 3) Tag because it includes class templates, which can't |
244 | | // "overload" with tag decls. |
245 | 870k | if (Redeclaration) |
246 | 419k | IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; |
247 | 1.51M | } else { |
248 | 1.51M | IDNS = Decl::IDNS_Tag; |
249 | 1.51M | } |
250 | 2.38M | break; |
251 | | |
252 | 10.5k | case Sema::LookupLabel: |
253 | 10.5k | IDNS = Decl::IDNS_Label; |
254 | 10.5k | break; |
255 | | |
256 | 3.77M | case Sema::LookupMemberName: |
257 | 3.77M | IDNS = Decl::IDNS_Member; |
258 | 3.77M | if (CPlusPlus) |
259 | 1.69M | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
260 | 3.77M | break; |
261 | | |
262 | 1.68M | case Sema::LookupNestedNameSpecifierName: |
263 | 1.68M | IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; |
264 | 1.68M | break; |
265 | | |
266 | 3.23k | case Sema::LookupNamespaceName: |
267 | 3.23k | IDNS = Decl::IDNS_Namespace; |
268 | 3.23k | break; |
269 | | |
270 | 228k | case Sema::LookupUsingDeclName: |
271 | 228k | assert(Redeclaration && "should only be used for redecl lookup"); |
272 | 0 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member | |
273 | 228k | Decl::IDNS_Using | Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend | |
274 | 228k | Decl::IDNS_LocalExtern; |
275 | 228k | break; |
276 | | |
277 | 177k | case Sema::LookupObjCProtocolName: |
278 | 177k | IDNS = Decl::IDNS_ObjCProtocol; |
279 | 177k | break; |
280 | | |
281 | 62.4k | case Sema::LookupOMPReductionName: |
282 | 62.4k | IDNS = Decl::IDNS_OMPReduction; |
283 | 62.4k | break; |
284 | | |
285 | 61.7k | case Sema::LookupOMPMapperName: |
286 | 61.7k | IDNS = Decl::IDNS_OMPMapper; |
287 | 61.7k | break; |
288 | | |
289 | 8.87k | case Sema::LookupAnyName: |
290 | 8.87k | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member |
291 | 8.87k | | Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol |
292 | 8.87k | | Decl::IDNS_Type; |
293 | 8.87k | break; |
294 | 194M | } |
295 | 194M | return IDNS; |
296 | 194M | } |
297 | | |
298 | 194M | void LookupResult::configure() { |
299 | 194M | IDNS = getIDNS(LookupKind, getSema().getLangOpts().CPlusPlus, |
300 | 194M | isForRedeclaration()); |
301 | | |
302 | | // If we're looking for one of the allocation or deallocation |
303 | | // operators, make sure that the implicitly-declared new and delete |
304 | | // operators can be found. |
305 | 194M | switch (NameInfo.getName().getCXXOverloadedOperator()) { |
306 | 11.7k | case OO_New: |
307 | 33.7k | case OO_Delete: |
308 | 38.3k | case OO_Array_New: |
309 | 44.0k | case OO_Array_Delete: |
310 | 44.0k | getSema().DeclareGlobalNewDelete(); |
311 | 44.0k | break; |
312 | | |
313 | 194M | default: |
314 | 194M | break; |
315 | 194M | } |
316 | | |
317 | | // Compiler builtins are always visible, regardless of where they end |
318 | | // up being declared. |
319 | 194M | if (IdentifierInfo *Id = NameInfo.getName().getAsIdentifierInfo()) { |
320 | 188M | if (unsigned BuiltinID = Id->getBuiltinID()) { |
321 | 3.88M | if (!getSema().Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
322 | 2.26M | AllowHidden = true; |
323 | 3.88M | } |
324 | 188M | } |
325 | 194M | } |
326 | | |
327 | 605M | bool LookupResult::checkDebugAssumptions() const { |
328 | | // This function is never called by NDEBUG builds. |
329 | 605M | assert(ResultKind != NotFound || Decls.size() == 0); |
330 | 0 | assert(ResultKind != Found || Decls.size() == 1); |
331 | 0 | assert(ResultKind != FoundOverloaded || Decls.size() > 1 || |
332 | 605M | (Decls.size() == 1 && |
333 | 605M | isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))); |
334 | 0 | assert(ResultKind != FoundUnresolvedValue || checkUnresolved()); |
335 | 0 | assert(ResultKind != Ambiguous || Decls.size() > 1 || |
336 | 605M | (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || |
337 | 605M | Ambiguity == AmbiguousBaseSubobjectTypes))); |
338 | 0 | assert((Paths != nullptr) == (ResultKind == Ambiguous && |
339 | 605M | (Ambiguity == AmbiguousBaseSubobjectTypes || |
340 | 605M | Ambiguity == AmbiguousBaseSubobjects))); |
341 | 0 | return true; |
342 | 605M | } |
343 | | |
344 | | // Necessary because CXXBasePaths is not complete in Sema.h |
345 | 122 | void LookupResult::deletePaths(CXXBasePaths *Paths) { |
346 | 122 | delete Paths; |
347 | 122 | } |
348 | | |
349 | | /// Get a representative context for a declaration such that two declarations |
350 | | /// will have the same context if they were found within the same scope. |
351 | 1.27k | static DeclContext *getContextForScopeMatching(Decl *D) { |
352 | | // For function-local declarations, use that function as the context. This |
353 | | // doesn't account for scopes within the function; the caller must deal with |
354 | | // those. |
355 | 1.27k | DeclContext *DC = D->getLexicalDeclContext(); |
356 | 1.27k | if (DC->isFunctionOrMethod()) |
357 | 118 | return DC; |
358 | | |
359 | | // Otherwise, look at the semantic context of the declaration. The |
360 | | // declaration must have been found there. |
361 | 1.15k | return D->getDeclContext()->getRedeclContext(); |
362 | 1.27k | } |
363 | | |
364 | | /// Determine whether \p D is a better lookup result than \p Existing, |
365 | | /// given that they declare the same entity. |
366 | | static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind, |
367 | 62.1M | NamedDecl *D, NamedDecl *Existing) { |
368 | | // When looking up redeclarations of a using declaration, prefer a using |
369 | | // shadow declaration over any other declaration of the same entity. |
370 | 62.1M | if (Kind == Sema::LookupUsingDeclName && isa<UsingShadowDecl>(D)1.35k && |
371 | 62.1M | !isa<UsingShadowDecl>(Existing)523 ) |
372 | 1 | return true; |
373 | | |
374 | 62.1M | auto *DUnderlying = D->getUnderlyingDecl(); |
375 | 62.1M | auto *EUnderlying = Existing->getUnderlyingDecl(); |
376 | | |
377 | | // If they have different underlying declarations, prefer a typedef over the |
378 | | // original type (this happens when two type declarations denote the same |
379 | | // type), per a generous reading of C++ [dcl.typedef]p3 and p4. The typedef |
380 | | // might carry additional semantic information, such as an alignment override. |
381 | | // However, per C++ [dcl.typedef]p5, when looking up a tag name, prefer a tag |
382 | | // declaration over a typedef. Also prefer a tag over a typedef for |
383 | | // destructor name lookup because in some contexts we only accept a |
384 | | // class-name in a destructor declaration. |
385 | 62.1M | if (DUnderlying->getCanonicalDecl() != EUnderlying->getCanonicalDecl()) { |
386 | 504k | assert(isa<TypeDecl>(DUnderlying) && isa<TypeDecl>(EUnderlying)); |
387 | 0 | bool HaveTag = isa<TagDecl>(EUnderlying); |
388 | 504k | bool WantTag = |
389 | 504k | Kind == Sema::LookupTagName || Kind == Sema::LookupDestructorName490k ; |
390 | 504k | return HaveTag != WantTag; |
391 | 504k | } |
392 | | |
393 | | // Pick the function with more default arguments. |
394 | | // FIXME: In the presence of ambiguous default arguments, we should keep both, |
395 | | // so we can diagnose the ambiguity if the default argument is needed. |
396 | | // See C++ [over.match.best]p3. |
397 | 61.6M | if (auto *DFD = dyn_cast<FunctionDecl>(DUnderlying)) { |
398 | 24.8M | auto *EFD = cast<FunctionDecl>(EUnderlying); |
399 | 24.8M | unsigned DMin = DFD->getMinRequiredArguments(); |
400 | 24.8M | unsigned EMin = EFD->getMinRequiredArguments(); |
401 | | // If D has more default arguments, it is preferred. |
402 | 24.8M | if (DMin != EMin) |
403 | 7 | return DMin < EMin; |
404 | | // FIXME: When we track visibility for default function arguments, check |
405 | | // that we pick the declaration with more visible default arguments. |
406 | 24.8M | } |
407 | | |
408 | | // Pick the template with more default template arguments. |
409 | 61.6M | if (auto *DTD = dyn_cast<TemplateDecl>(DUnderlying)) { |
410 | 2.51M | auto *ETD = cast<TemplateDecl>(EUnderlying); |
411 | 2.51M | unsigned DMin = DTD->getTemplateParameters()->getMinRequiredArguments(); |
412 | 2.51M | unsigned EMin = ETD->getTemplateParameters()->getMinRequiredArguments(); |
413 | | // If D has more default arguments, it is preferred. Note that default |
414 | | // arguments (and their visibility) is monotonically increasing across the |
415 | | // redeclaration chain, so this is a quick proxy for "is more recent". |
416 | 2.51M | if (DMin != EMin) |
417 | 3 | return DMin < EMin; |
418 | | // If D has more *visible* default arguments, it is preferred. Note, an |
419 | | // earlier default argument being visible does not imply that a later |
420 | | // default argument is visible, so we can't just check the first one. |
421 | 2.51M | for (unsigned I = DMin, N = DTD->getTemplateParameters()->size(); |
422 | 3.55M | I != N; ++I1.03M ) { |
423 | 1.03M | if (!S.hasVisibleDefaultArgument( |
424 | 1.03M | ETD->getTemplateParameters()->getParam(I)) && |
425 | 1.03M | S.hasVisibleDefaultArgument( |
426 | 138k | DTD->getTemplateParameters()->getParam(I))) |
427 | 2 | return true; |
428 | 1.03M | } |
429 | 2.51M | } |
430 | | |
431 | | // VarDecl can have incomplete array types, prefer the one with more complete |
432 | | // array type. |
433 | 61.6M | if (VarDecl *DVD = dyn_cast<VarDecl>(DUnderlying)) { |
434 | 115k | VarDecl *EVD = cast<VarDecl>(EUnderlying); |
435 | 115k | if (EVD->getType()->isIncompleteType() && |
436 | 115k | !DVD->getType()->isIncompleteType()5.04k ) { |
437 | | // Prefer the decl with a more complete type if visible. |
438 | 0 | return S.isVisible(DVD); |
439 | 0 | } |
440 | 115k | return false; // Avoid picking up a newer decl, just because it was newer. |
441 | 115k | } |
442 | | |
443 | | // For most kinds of declaration, it doesn't really matter which one we pick. |
444 | 61.5M | if (!isa<FunctionDecl>(DUnderlying) && !isa<VarDecl>(DUnderlying)36.6M ) { |
445 | | // If the existing declaration is hidden, prefer the new one. Otherwise, |
446 | | // keep what we've got. |
447 | 36.6M | return !S.isVisible(Existing); |
448 | 36.6M | } |
449 | | |
450 | | // Pick the newer declaration; it might have a more precise type. |
451 | 25.0M | for (Decl *Prev = DUnderlying->getPreviousDecl(); 24.8M Prev; |
452 | 24.8M | Prev = Prev->getPreviousDecl()164k ) |
453 | 165k | if (Prev == EUnderlying) |
454 | 518 | return true; |
455 | 24.8M | return false; |
456 | 24.8M | } |
457 | | |
458 | | /// Determine whether \p D can hide a tag declaration. |
459 | 616 | static bool canHideTag(NamedDecl *D) { |
460 | | // C++ [basic.scope.declarative]p4: |
461 | | // Given a set of declarations in a single declarative region [...] |
462 | | // exactly one declaration shall declare a class name or enumeration name |
463 | | // that is not a typedef name and the other declarations shall all refer to |
464 | | // the same variable, non-static data member, or enumerator, or all refer |
465 | | // to functions and function templates; in this case the class name or |
466 | | // enumeration name is hidden. |
467 | | // C++ [basic.scope.hiding]p2: |
468 | | // A class name or enumeration name can be hidden by the name of a |
469 | | // variable, data member, function, or enumerator declared in the same |
470 | | // scope. |
471 | | // An UnresolvedUsingValueDecl always instantiates to one of these. |
472 | 616 | D = D->getUnderlyingDecl(); |
473 | 616 | return isa<VarDecl>(D) || isa<EnumConstantDecl>(D)487 || isa<FunctionDecl>(D)466 || |
474 | 616 | isa<FunctionTemplateDecl>(D)125 || isa<FieldDecl>(D)109 || |
475 | 616 | isa<UnresolvedUsingValueDecl>(D)38 ; |
476 | 616 | } |
477 | | |
478 | | /// Resolves the result kind of this lookup. |
479 | 214M | void LookupResult::resolveKind() { |
480 | 214M | unsigned N = Decls.size(); |
481 | | |
482 | | // Fast case: no possible ambiguity. |
483 | 214M | if (N == 0) { |
484 | 24.7M | assert(ResultKind == NotFound || |
485 | 24.7M | ResultKind == NotFoundInCurrentInstantiation); |
486 | 0 | return; |
487 | 24.7M | } |
488 | | |
489 | | // If there's a single decl, we need to examine it to decide what |
490 | | // kind of lookup this is. |
491 | 189M | if (N == 1) { |
492 | 136M | NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); |
493 | 136M | if (isa<FunctionTemplateDecl>(D)) |
494 | 1.50M | ResultKind = FoundOverloaded; |
495 | 135M | else if (isa<UnresolvedUsingValueDecl>(D)) |
496 | 509 | ResultKind = FoundUnresolvedValue; |
497 | 136M | return; |
498 | 136M | } |
499 | | |
500 | | // Don't do any extra resolution if we've already resolved as ambiguous. |
501 | 53.1M | if (ResultKind == Ambiguous) return4.32k ; |
502 | | |
503 | 53.1M | llvm::SmallDenseMap<NamedDecl*, unsigned, 16> Unique; |
504 | 53.1M | llvm::SmallDenseMap<QualType, unsigned, 16> UniqueTypes; |
505 | | |
506 | 53.1M | bool Ambiguous = false; |
507 | 53.1M | bool HasTag = false, HasFunction = false; |
508 | 53.1M | bool HasFunctionTemplate = false, HasUnresolved = false; |
509 | 53.1M | NamedDecl *HasNonFunction = nullptr; |
510 | | |
511 | 53.1M | llvm::SmallVector<NamedDecl*, 4> EquivalentNonFunctions; |
512 | | |
513 | 53.1M | unsigned UniqueTagIndex = 0; |
514 | | |
515 | 53.1M | unsigned I = 0; |
516 | 268M | while (I < N) { |
517 | 215M | NamedDecl *D = Decls[I]->getUnderlyingDecl(); |
518 | 215M | D = cast<NamedDecl>(D->getCanonicalDecl()); |
519 | | |
520 | | // Ignore an invalid declaration unless it's the only one left. |
521 | 215M | if (D->isInvalidDecl() && !(12.1k I == 012.1k && N == 17.50k )) { |
522 | 11.2k | Decls[I] = Decls[--N]; |
523 | 11.2k | continue; |
524 | 11.2k | } |
525 | | |
526 | 215M | llvm::Optional<unsigned> ExistingI; |
527 | | |
528 | | // Redeclarations of types via typedef can occur both within a scope |
529 | | // and, through using declarations and directives, across scopes. There is |
530 | | // no ambiguity if they all refer to the same type, so unique based on the |
531 | | // canonical type. |
532 | 215M | if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { |
533 | 66.5M | QualType T = getSema().Context.getTypeDeclType(TD); |
534 | 66.5M | auto UniqueResult = UniqueTypes.insert( |
535 | 66.5M | std::make_pair(getSema().Context.getCanonicalType(T), I)); |
536 | 66.5M | if (!UniqueResult.second) { |
537 | | // The type is not unique. |
538 | 33.4M | ExistingI = UniqueResult.first->second; |
539 | 33.4M | } |
540 | 66.5M | } |
541 | | |
542 | | // For non-type declarations, check for a prior lookup result naming this |
543 | | // canonical declaration. |
544 | 215M | if (!ExistingI) { |
545 | 182M | auto UniqueResult = Unique.insert(std::make_pair(D, I)); |
546 | 182M | if (!UniqueResult.second) { |
547 | | // We've seen this entity before. |
548 | 28.7M | ExistingI = UniqueResult.first->second; |
549 | 28.7M | } |
550 | 182M | } |
551 | | |
552 | 215M | if (ExistingI) { |
553 | | // This is not a unique lookup result. Pick one of the results and |
554 | | // discard the other. |
555 | 62.1M | if (isPreferredLookupResult(getSema(), getLookupKind(), Decls[I], |
556 | 62.1M | Decls[*ExistingI])) |
557 | 30.1k | Decls[*ExistingI] = Decls[I]; |
558 | 62.1M | Decls[I] = Decls[--N]; |
559 | 62.1M | continue; |
560 | 62.1M | } |
561 | | |
562 | | // Otherwise, do some decl type analysis and then continue. |
563 | | |
564 | 153M | if (isa<UnresolvedUsingValueDecl>(D)) { |
565 | 267 | HasUnresolved = true; |
566 | 153M | } else if (isa<TagDecl>(D)) { |
567 | 788k | if (HasTag) |
568 | 44 | Ambiguous = true; |
569 | 788k | UniqueTagIndex = I; |
570 | 788k | HasTag = true; |
571 | 152M | } else if (isa<FunctionTemplateDecl>(D)) { |
572 | 33.8M | HasFunction = true; |
573 | 33.8M | HasFunctionTemplate = true; |
574 | 118M | } else if (isa<FunctionDecl>(D)) { |
575 | 83.6M | HasFunction = true; |
576 | 83.6M | } else { |
577 | 35.1M | if (HasNonFunction) { |
578 | | // If we're about to create an ambiguity between two declarations that |
579 | | // are equivalent, but one is an internal linkage declaration from one |
580 | | // module and the other is an internal linkage declaration from another |
581 | | // module, just skip it. |
582 | 3.68k | if (getSema().isEquivalentInternalLinkageDeclaration(HasNonFunction, |
583 | 3.68k | D)) { |
584 | 4 | EquivalentNonFunctions.push_back(D); |
585 | 4 | Decls[I] = Decls[--N]; |
586 | 4 | continue; |
587 | 4 | } |
588 | | |
589 | 3.68k | Ambiguous = true; |
590 | 3.68k | } |
591 | 35.1M | HasNonFunction = D; |
592 | 35.1M | } |
593 | 153M | I++; |
594 | 153M | } |
595 | | |
596 | | // C++ [basic.scope.hiding]p2: |
597 | | // A class name or enumeration name can be hidden by the name of |
598 | | // an object, function, or enumerator declared in the same |
599 | | // scope. If a class or enumeration name and an object, function, |
600 | | // or enumerator are declared in the same scope (in any order) |
601 | | // with the same name, the class or enumeration name is hidden |
602 | | // wherever the object, function, or enumerator name is visible. |
603 | | // But it's still an error if there are distinct tag types found, |
604 | | // even if they're not visible. (ref?) |
605 | 53.1M | if (N > 1 && HideTags15.3M && HasTag15.2M && !Ambiguous678 && |
606 | 53.1M | (635 HasFunction635 || HasNonFunction276 || HasUnresolved36 )) { |
607 | 635 | NamedDecl *OtherDecl = Decls[UniqueTagIndex ? 0425 : N - 1210 ]; |
608 | 635 | if (isa<TagDecl>(Decls[UniqueTagIndex]->getUnderlyingDecl()) && |
609 | 635 | getContextForScopeMatching(Decls[UniqueTagIndex])->Equals( |
610 | 635 | getContextForScopeMatching(OtherDecl)) && |
611 | 635 | canHideTag(OtherDecl)616 ) |
612 | 614 | Decls[UniqueTagIndex] = Decls[--N]; |
613 | 21 | else |
614 | 21 | Ambiguous = true; |
615 | 635 | } |
616 | | |
617 | | // FIXME: This diagnostic should really be delayed until we're done with |
618 | | // the lookup result, in case the ambiguity is resolved by the caller. |
619 | 53.1M | if (!EquivalentNonFunctions.empty() && !Ambiguous4 ) |
620 | 4 | getSema().diagnoseEquivalentInternalLinkageDeclarations( |
621 | 4 | getNameLoc(), HasNonFunction, EquivalentNonFunctions); |
622 | | |
623 | 53.1M | Decls.truncate(N); |
624 | | |
625 | 53.1M | if (HasNonFunction && (35.1M HasFunction35.1M || HasUnresolved35.1M )) |
626 | 1.05k | Ambiguous = true; |
627 | | |
628 | 53.1M | if (Ambiguous) |
629 | 4.74k | setAmbiguous(LookupResult::AmbiguousReference); |
630 | 53.1M | else if (HasUnresolved) |
631 | 249 | ResultKind = LookupResult::FoundUnresolvedValue; |
632 | 53.1M | else if (N > 1 || HasFunctionTemplate37.8M ) |
633 | 15.5M | ResultKind = LookupResult::FoundOverloaded; |
634 | 37.6M | else |
635 | 37.6M | ResultKind = LookupResult::Found; |
636 | 53.1M | } |
637 | | |
638 | 122 | void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { |
639 | 122 | CXXBasePaths::const_paths_iterator I, E; |
640 | 371 | for (I = P.begin(), E = P.end(); I != E; ++I249 ) |
641 | 515 | for (DeclContext::lookup_iterator DI = I->Decls, DE = DI.end(); 249 DI != DE; |
642 | 266 | ++DI) |
643 | 266 | addDecl(*DI); |
644 | 122 | } |
645 | | |
646 | 45 | void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { |
647 | 45 | Paths = new CXXBasePaths; |
648 | 45 | Paths->swap(P); |
649 | 45 | addDeclsFromBasePaths(*Paths); |
650 | 45 | resolveKind(); |
651 | 45 | setAmbiguous(AmbiguousBaseSubobjects); |
652 | 45 | } |
653 | | |
654 | 77 | void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { |
655 | 77 | Paths = new CXXBasePaths; |
656 | 77 | Paths->swap(P); |
657 | 77 | addDeclsFromBasePaths(*Paths); |
658 | 77 | resolveKind(); |
659 | 77 | setAmbiguous(AmbiguousBaseSubobjectTypes); |
660 | 77 | } |
661 | | |
662 | 0 | void LookupResult::print(raw_ostream &Out) { |
663 | 0 | Out << Decls.size() << " result(s)"; |
664 | 0 | if (isAmbiguous()) Out << ", ambiguous"; |
665 | 0 | if (Paths) Out << ", base paths present"; |
666 | |
|
667 | 0 | for (iterator I = begin(), E = end(); I != E; ++I) { |
668 | 0 | Out << "\n"; |
669 | 0 | (*I)->print(Out, 2); |
670 | 0 | } |
671 | 0 | } |
672 | | |
673 | 20 | LLVM_DUMP_METHOD void LookupResult::dump() { |
674 | 20 | llvm::errs() << "lookup results for " << getLookupName().getAsString() |
675 | 20 | << ":\n"; |
676 | 20 | for (NamedDecl *D : *this) |
677 | 19 | D->dump(); |
678 | 20 | } |
679 | | |
680 | | /// Diagnose a missing builtin type. |
681 | | static QualType diagOpenCLBuiltinTypeError(Sema &S, llvm::StringRef TypeClass, |
682 | 4 | llvm::StringRef Name) { |
683 | 4 | S.Diag(SourceLocation(), diag::err_opencl_type_not_found) |
684 | 4 | << TypeClass << Name; |
685 | 4 | return S.Context.VoidTy; |
686 | 4 | } |
687 | | |
688 | | /// Lookup an OpenCL enum type. |
689 | 6.88k | static QualType getOpenCLEnumType(Sema &S, llvm::StringRef Name) { |
690 | 6.88k | LookupResult Result(S, &S.Context.Idents.get(Name), SourceLocation(), |
691 | 6.88k | Sema::LookupTagName); |
692 | 6.88k | S.LookupName(Result, S.TUScope); |
693 | 6.88k | if (Result.empty()) |
694 | 0 | return diagOpenCLBuiltinTypeError(S, "enum", Name); |
695 | 6.88k | EnumDecl *Decl = Result.getAsSingle<EnumDecl>(); |
696 | 6.88k | if (!Decl) |
697 | 0 | return diagOpenCLBuiltinTypeError(S, "enum", Name); |
698 | 6.88k | return S.Context.getEnumType(Decl); |
699 | 6.88k | } |
700 | | |
701 | | /// Lookup an OpenCL typedef type. |
702 | 159 | static QualType getOpenCLTypedefType(Sema &S, llvm::StringRef Name) { |
703 | 159 | LookupResult Result(S, &S.Context.Idents.get(Name), SourceLocation(), |
704 | 159 | Sema::LookupOrdinaryName); |
705 | 159 | S.LookupName(Result, S.TUScope); |
706 | 159 | if (Result.empty()) |
707 | 4 | return diagOpenCLBuiltinTypeError(S, "typedef", Name); |
708 | 155 | TypedefNameDecl *Decl = Result.getAsSingle<TypedefNameDecl>(); |
709 | 155 | if (!Decl) |
710 | 0 | return diagOpenCLBuiltinTypeError(S, "typedef", Name); |
711 | 155 | return S.Context.getTypedefType(Decl); |
712 | 155 | } |
713 | | |
714 | | /// Get the QualType instances of the return type and arguments for an OpenCL |
715 | | /// builtin function signature. |
716 | | /// \param S (in) The Sema instance. |
717 | | /// \param OpenCLBuiltin (in) The signature currently handled. |
718 | | /// \param GenTypeMaxCnt (out) Maximum number of types contained in a generic |
719 | | /// type used as return type or as argument. |
720 | | /// Only meaningful for generic types, otherwise equals 1. |
721 | | /// \param RetTypes (out) List of the possible return types. |
722 | | /// \param ArgTypes (out) List of the possible argument types. For each |
723 | | /// argument, ArgTypes contains QualTypes for the Cartesian product |
724 | | /// of (vector sizes) x (types) . |
725 | | static void GetQualTypesForOpenCLBuiltin( |
726 | | Sema &S, const OpenCLBuiltinStruct &OpenCLBuiltin, unsigned &GenTypeMaxCnt, |
727 | | SmallVector<QualType, 1> &RetTypes, |
728 | 174k | SmallVector<SmallVector<QualType, 1>, 5> &ArgTypes) { |
729 | | // Get the QualType instances of the return types. |
730 | 174k | unsigned Sig = SignatureTable[OpenCLBuiltin.SigTableIndex]; |
731 | 174k | OCL2Qual(S, TypeTable[Sig], RetTypes); |
732 | 174k | GenTypeMaxCnt = RetTypes.size(); |
733 | | |
734 | | // Get the QualType instances of the arguments. |
735 | | // First type is the return type, skip it. |
736 | 496k | for (unsigned Index = 1; Index < OpenCLBuiltin.NumTypes; Index++322k ) { |
737 | 322k | SmallVector<QualType, 1> Ty; |
738 | 322k | OCL2Qual(S, TypeTable[SignatureTable[OpenCLBuiltin.SigTableIndex + Index]], |
739 | 322k | Ty); |
740 | 322k | GenTypeMaxCnt = (Ty.size() > GenTypeMaxCnt) ? Ty.size()833 : GenTypeMaxCnt321k ; |
741 | 322k | ArgTypes.push_back(std::move(Ty)); |
742 | 322k | } |
743 | 174k | } |
744 | | |
745 | | /// Create a list of the candidate function overloads for an OpenCL builtin |
746 | | /// function. |
747 | | /// \param Context (in) The ASTContext instance. |
748 | | /// \param GenTypeMaxCnt (in) Maximum number of types contained in a generic |
749 | | /// type used as return type or as argument. |
750 | | /// Only meaningful for generic types, otherwise equals 1. |
751 | | /// \param FunctionList (out) List of FunctionTypes. |
752 | | /// \param RetTypes (in) List of the possible return types. |
753 | | /// \param ArgTypes (in) List of the possible types for the arguments. |
754 | | static void GetOpenCLBuiltinFctOverloads( |
755 | | ASTContext &Context, unsigned GenTypeMaxCnt, |
756 | | std::vector<QualType> &FunctionList, SmallVector<QualType, 1> &RetTypes, |
757 | 174k | SmallVector<SmallVector<QualType, 1>, 5> &ArgTypes) { |
758 | 174k | FunctionProtoType::ExtProtoInfo PI( |
759 | 174k | Context.getDefaultCallingConvention(false, false, true)); |
760 | 174k | PI.Variadic = false; |
761 | | |
762 | | // Do not attempt to create any FunctionTypes if there are no return types, |
763 | | // which happens when a type belongs to a disabled extension. |
764 | 174k | if (RetTypes.size() == 0) |
765 | 44 | return; |
766 | | |
767 | | // Create FunctionTypes for each (gen)type. |
768 | 542k | for (unsigned IGenType = 0; 174k IGenType < GenTypeMaxCnt; IGenType++368k ) { |
769 | 368k | SmallVector<QualType, 5> ArgList; |
770 | | |
771 | 1.18M | for (unsigned A = 0; A < ArgTypes.size(); A++814k ) { |
772 | | // Bail out if there is an argument that has no available types. |
773 | 814k | if (ArgTypes[A].size() == 0) |
774 | 27 | return; |
775 | | |
776 | | // Builtins such as "max" have an "sgentype" argument that represents |
777 | | // the corresponding scalar type of a gentype. The number of gentypes |
778 | | // must be a multiple of the number of sgentypes. |
779 | 814k | assert(GenTypeMaxCnt % ArgTypes[A].size() == 0 && |
780 | 814k | "argument type count not compatible with gentype type count"); |
781 | 0 | unsigned Idx = IGenType % ArgTypes[A].size(); |
782 | 814k | ArgList.push_back(ArgTypes[A][Idx]); |
783 | 814k | } |
784 | | |
785 | 368k | FunctionList.push_back(Context.getFunctionType( |
786 | 368k | RetTypes[(RetTypes.size() != 1) ? IGenType162k : 0205k ], ArgList, PI)); |
787 | 368k | } |
788 | 174k | } |
789 | | |
790 | | /// When trying to resolve a function name, if isOpenCLBuiltin() returns a |
791 | | /// non-null <Index, Len> pair, then the name is referencing an OpenCL |
792 | | /// builtin function. Add all candidate signatures to the LookUpResult. |
793 | | /// |
794 | | /// \param S (in) The Sema instance. |
795 | | /// \param LR (inout) The LookupResult instance. |
796 | | /// \param II (in) The identifier being resolved. |
797 | | /// \param FctIndex (in) Starting index in the BuiltinTable. |
798 | | /// \param Len (in) The signature list has Len elements. |
799 | | static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR, |
800 | | IdentifierInfo *II, |
801 | | const unsigned FctIndex, |
802 | 12.9k | const unsigned Len) { |
803 | | // The builtin function declaration uses generic types (gentype). |
804 | 12.9k | bool HasGenType = false; |
805 | | |
806 | | // Maximum number of types contained in a generic type used as return type or |
807 | | // as argument. Only meaningful for generic types, otherwise equals 1. |
808 | 12.9k | unsigned GenTypeMaxCnt; |
809 | | |
810 | 12.9k | ASTContext &Context = S.Context; |
811 | | |
812 | 205k | for (unsigned SignatureIndex = 0; SignatureIndex < Len; SignatureIndex++192k ) { |
813 | 192k | const OpenCLBuiltinStruct &OpenCLBuiltin = |
814 | 192k | BuiltinTable[FctIndex + SignatureIndex]; |
815 | | |
816 | | // Ignore this builtin function if it is not available in the currently |
817 | | // selected language version. |
818 | 192k | if (!isOpenCLVersionContainedInMask(Context.getLangOpts(), |
819 | 192k | OpenCLBuiltin.Versions)) |
820 | 88 | continue; |
821 | | |
822 | | // Ignore this builtin function if it carries an extension macro that is |
823 | | // not defined. This indicates that the extension is not supported by the |
824 | | // target, so the builtin function should not be available. |
825 | 192k | StringRef Extensions = FunctionExtensionTable[OpenCLBuiltin.Extension]; |
826 | 192k | if (!Extensions.empty()) { |
827 | 39.4k | SmallVector<StringRef, 2> ExtVec; |
828 | 39.4k | Extensions.split(ExtVec, " "); |
829 | 39.4k | bool AllExtensionsDefined = true; |
830 | 48.2k | for (StringRef Ext : ExtVec) { |
831 | 48.2k | if (!S.getPreprocessor().isMacroDefined(Ext)) { |
832 | 17.8k | AllExtensionsDefined = false; |
833 | 17.8k | break; |
834 | 17.8k | } |
835 | 48.2k | } |
836 | 39.4k | if (!AllExtensionsDefined) |
837 | 17.8k | continue; |
838 | 39.4k | } |
839 | | |
840 | 174k | SmallVector<QualType, 1> RetTypes; |
841 | 174k | SmallVector<SmallVector<QualType, 1>, 5> ArgTypes; |
842 | | |
843 | | // Obtain QualType lists for the function signature. |
844 | 174k | GetQualTypesForOpenCLBuiltin(S, OpenCLBuiltin, GenTypeMaxCnt, RetTypes, |
845 | 174k | ArgTypes); |
846 | 174k | if (GenTypeMaxCnt > 1) { |
847 | 10.1k | HasGenType = true; |
848 | 10.1k | } |
849 | | |
850 | | // Create function overload for each type combination. |
851 | 174k | std::vector<QualType> FunctionList; |
852 | 174k | GetOpenCLBuiltinFctOverloads(Context, GenTypeMaxCnt, FunctionList, RetTypes, |
853 | 174k | ArgTypes); |
854 | | |
855 | 174k | SourceLocation Loc = LR.getNameLoc(); |
856 | 174k | DeclContext *Parent = Context.getTranslationUnitDecl(); |
857 | 174k | FunctionDecl *NewOpenCLBuiltin; |
858 | | |
859 | 368k | for (const auto &FTy : FunctionList) { |
860 | 368k | NewOpenCLBuiltin = FunctionDecl::Create( |
861 | 368k | Context, Parent, Loc, Loc, II, FTy, /*TInfo=*/nullptr, SC_Extern, |
862 | 368k | S.getCurFPFeatures().isFPConstrained(), false, |
863 | 368k | FTy->isFunctionProtoType()); |
864 | 368k | NewOpenCLBuiltin->setImplicit(); |
865 | | |
866 | | // Create Decl objects for each parameter, adding them to the |
867 | | // FunctionDecl. |
868 | 368k | const auto *FP = cast<FunctionProtoType>(FTy); |
869 | 368k | SmallVector<ParmVarDecl *, 4> ParmList; |
870 | 1.18M | for (unsigned IParm = 0, e = FP->getNumParams(); IParm != e; ++IParm814k ) { |
871 | 814k | ParmVarDecl *Parm = ParmVarDecl::Create( |
872 | 814k | Context, NewOpenCLBuiltin, SourceLocation(), SourceLocation(), |
873 | 814k | nullptr, FP->getParamType(IParm), nullptr, SC_None, nullptr); |
874 | 814k | Parm->setScopeInfo(0, IParm); |
875 | 814k | ParmList.push_back(Parm); |
876 | 814k | } |
877 | 368k | NewOpenCLBuiltin->setParams(ParmList); |
878 | | |
879 | | // Add function attributes. |
880 | 368k | if (OpenCLBuiltin.IsPure) |
881 | 12.4k | NewOpenCLBuiltin->addAttr(PureAttr::CreateImplicit(Context)); |
882 | 368k | if (OpenCLBuiltin.IsConst) |
883 | 287k | NewOpenCLBuiltin->addAttr(ConstAttr::CreateImplicit(Context)); |
884 | 368k | if (OpenCLBuiltin.IsConv) |
885 | 7.33k | NewOpenCLBuiltin->addAttr(ConvergentAttr::CreateImplicit(Context)); |
886 | | |
887 | 368k | if (!S.getLangOpts().OpenCLCPlusPlus) |
888 | 361k | NewOpenCLBuiltin->addAttr(OverloadableAttr::CreateImplicit(Context)); |
889 | | |
890 | 368k | LR.addDecl(NewOpenCLBuiltin); |
891 | 368k | } |
892 | 174k | } |
893 | | |
894 | | // If we added overloads, need to resolve the lookup result. |
895 | 12.9k | if (Len > 1 || HasGenType2.82k ) |
896 | 12.7k | LR.resolveKind(); |
897 | 12.9k | } |
898 | | |
899 | | /// Lookup a builtin function, when name lookup would otherwise |
900 | | /// fail. |
901 | 15.3M | bool Sema::LookupBuiltin(LookupResult &R) { |
902 | 15.3M | Sema::LookupNameKind NameKind = R.getLookupKind(); |
903 | | |
904 | | // If we didn't find a use of this identifier, and if the identifier |
905 | | // corresponds to a compiler builtin, create the decl object for the builtin |
906 | | // now, injecting it into translation unit scope, and return it. |
907 | 15.3M | if (NameKind == Sema::LookupOrdinaryName || |
908 | 15.3M | NameKind == Sema::LookupRedeclarationWithLinkage1.95M ) { |
909 | 13.4M | IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); |
910 | 13.4M | if (II) { |
911 | 13.3M | if (getLangOpts().CPlusPlus && NameKind == Sema::LookupOrdinaryName10.5M ) { |
912 | 10.5M | if (II == getASTContext().getMakeIntegerSeqName()) { |
913 | 512 | R.addDecl(getASTContext().getMakeIntegerSeqDecl()); |
914 | 512 | return true; |
915 | 10.5M | } else if (II == getASTContext().getTypePackElementName()) { |
916 | 940 | R.addDecl(getASTContext().getTypePackElementDecl()); |
917 | 940 | return true; |
918 | 940 | } |
919 | 10.5M | } |
920 | | |
921 | | // Check if this is an OpenCL Builtin, and if so, insert its overloads. |
922 | 13.3M | if (getLangOpts().OpenCL && getLangOpts().DeclareOpenCLBuiltins73.1k ) { |
923 | 27.0k | auto Index = isOpenCLBuiltin(II->getName()); |
924 | 27.0k | if (Index.first) { |
925 | 12.9k | InsertOCLBuiltinDeclarationsFromTable(*this, R, II, Index.first - 1, |
926 | 12.9k | Index.second); |
927 | 12.9k | return true; |
928 | 12.9k | } |
929 | 27.0k | } |
930 | | |
931 | | // If this is a builtin on this (or all) targets, create the decl. |
932 | 13.3M | if (unsigned BuiltinID = II->getBuiltinID()) { |
933 | | // In C++, C2x, and OpenCL (spec v1.2 s6.9.f), we don't have any |
934 | | // predefined library functions like 'malloc'. Instead, we'll just |
935 | | // error. |
936 | 1.15M | if ((getLangOpts().CPlusPlus || getLangOpts().OpenCL660k || |
937 | 1.15M | getLangOpts().C2x658k ) && |
938 | 1.15M | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)498k ) |
939 | 289k | return false; |
940 | | |
941 | 867k | if (NamedDecl *D = |
942 | 867k | LazilyCreateBuiltin(II, BuiltinID, TUScope, |
943 | 867k | R.isForRedeclaration(), R.getNameLoc())) { |
944 | 867k | R.addDecl(D); |
945 | 867k | return true; |
946 | 867k | } |
947 | 867k | } |
948 | 13.3M | } |
949 | 13.4M | } |
950 | | |
951 | 14.2M | return false; |
952 | 15.3M | } |
953 | | |
954 | | /// Looks up the declaration of "struct objc_super" and |
955 | | /// saves it for later use in building builtin declaration of |
956 | | /// objc_msgSendSuper and objc_msgSendSuper_stret. |
957 | 9 | static void LookupPredefedObjCSuperType(Sema &Sema, Scope *S) { |
958 | 9 | ASTContext &Context = Sema.Context; |
959 | 9 | LookupResult Result(Sema, &Context.Idents.get("objc_super"), SourceLocation(), |
960 | 9 | Sema::LookupTagName); |
961 | 9 | Sema.LookupName(Result, S); |
962 | 9 | if (Result.getResultKind() == LookupResult::Found) |
963 | 9 | if (const TagDecl *TD = Result.getAsSingle<TagDecl>()) |
964 | 9 | Context.setObjCSuperType(Context.getTagDeclType(TD)); |
965 | 9 | } |
966 | | |
967 | 1.01M | void Sema::LookupNecessaryTypesForBuiltin(Scope *S, unsigned ID) { |
968 | 1.01M | if (ID == Builtin::BIobjc_msgSendSuper) |
969 | 9 | LookupPredefedObjCSuperType(*this, S); |
970 | 1.01M | } |
971 | | |
972 | | /// Determine whether we can declare a special member function within |
973 | | /// the class at this point. |
974 | 2.51M | static bool CanDeclareSpecialMemberFunction(const CXXRecordDecl *Class) { |
975 | | // We need to have a definition for the class. |
976 | 2.51M | if (!Class->getDefinition() || Class->isDependentContext()2.51M ) |
977 | 463k | return false; |
978 | | |
979 | | // We can't be in the middle of defining the class. |
980 | 2.04M | return !Class->isBeingDefined(); |
981 | 2.51M | } |
982 | | |
983 | 2.63k | void Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) { |
984 | 2.63k | if (!CanDeclareSpecialMemberFunction(Class)) |
985 | 74 | return; |
986 | | |
987 | | // If the default constructor has not yet been declared, do so now. |
988 | 2.56k | if (Class->needsImplicitDefaultConstructor()) |
989 | 1.88k | DeclareImplicitDefaultConstructor(Class); |
990 | | |
991 | | // If the copy constructor has not yet been declared, do so now. |
992 | 2.56k | if (Class->needsImplicitCopyConstructor()) |
993 | 1.95k | DeclareImplicitCopyConstructor(Class); |
994 | | |
995 | | // If the copy assignment operator has not yet been declared, do so now. |
996 | 2.56k | if (Class->needsImplicitCopyAssignment()) |
997 | 1.98k | DeclareImplicitCopyAssignment(Class); |
998 | | |
999 | 2.56k | if (getLangOpts().CPlusPlus11) { |
1000 | | // If the move constructor has not yet been declared, do so now. |
1001 | 2.45k | if (Class->needsImplicitMoveConstructor()) |
1002 | 1.80k | DeclareImplicitMoveConstructor(Class); |
1003 | | |
1004 | | // If the move assignment operator has not yet been declared, do so now. |
1005 | 2.45k | if (Class->needsImplicitMoveAssignment()) |
1006 | 1.85k | DeclareImplicitMoveAssignment(Class); |
1007 | 2.45k | } |
1008 | | |
1009 | | // If the destructor has not yet been declared, do so now. |
1010 | 2.56k | if (Class->needsImplicitDestructor()) |
1011 | 1.83k | DeclareImplicitDestructor(Class); |
1012 | 2.56k | } |
1013 | | |
1014 | | /// Determine whether this is the name of an implicitly-declared |
1015 | | /// special member function. |
1016 | 85.0M | static bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) { |
1017 | 85.0M | switch (Name.getNameKind()) { |
1018 | 310k | case DeclarationName::CXXConstructorName: |
1019 | 353k | case DeclarationName::CXXDestructorName: |
1020 | 353k | return true; |
1021 | | |
1022 | 3.21M | case DeclarationName::CXXOperatorName: |
1023 | 3.21M | return Name.getCXXOverloadedOperator() == OO_Equal; |
1024 | | |
1025 | 81.4M | default: |
1026 | 81.4M | break; |
1027 | 85.0M | } |
1028 | | |
1029 | 81.4M | return false; |
1030 | 85.0M | } |
1031 | | |
1032 | | /// If there are any implicit member functions with the given name |
1033 | | /// that need to be declared in the given declaration context, do so. |
1034 | | static void DeclareImplicitMemberFunctionsWithName(Sema &S, |
1035 | | DeclarationName Name, |
1036 | | SourceLocation Loc, |
1037 | 102M | const DeclContext *DC) { |
1038 | 102M | if (!DC) |
1039 | 0 | return; |
1040 | | |
1041 | 102M | switch (Name.getNameKind()) { |
1042 | 1.70M | case DeclarationName::CXXConstructorName: |
1043 | 1.70M | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
1044 | 706k | if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { |
1045 | 5.50k | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); |
1046 | 5.50k | if (Record->needsImplicitDefaultConstructor()) |
1047 | 103 | S.DeclareImplicitDefaultConstructor(Class); |
1048 | 5.50k | if (Record->needsImplicitCopyConstructor()) |
1049 | 1.06k | S.DeclareImplicitCopyConstructor(Class); |
1050 | 5.50k | if (S.getLangOpts().CPlusPlus11 && |
1051 | 5.50k | Record->needsImplicitMoveConstructor()5.38k ) |
1052 | 908 | S.DeclareImplicitMoveConstructor(Class); |
1053 | 5.50k | } |
1054 | 1.70M | break; |
1055 | | |
1056 | 275k | case DeclarationName::CXXDestructorName: |
1057 | 275k | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
1058 | 109k | if (Record->getDefinition() && Record->needsImplicitDestructor() && |
1059 | 109k | CanDeclareSpecialMemberFunction(Record)101k ) |
1060 | 218 | S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record)); |
1061 | 275k | break; |
1062 | | |
1063 | 10.5M | case DeclarationName::CXXOperatorName: |
1064 | 10.5M | if (Name.getCXXOverloadedOperator() != OO_Equal) |
1065 | 10.0M | break; |
1066 | | |
1067 | 436k | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) { |
1068 | 186k | if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { |
1069 | 32.2k | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); |
1070 | 32.2k | if (Record->needsImplicitCopyAssignment()) |
1071 | 5.75k | S.DeclareImplicitCopyAssignment(Class); |
1072 | 32.2k | if (S.getLangOpts().CPlusPlus11 && |
1073 | 32.2k | Record->needsImplicitMoveAssignment()29.5k ) |
1074 | 2.81k | S.DeclareImplicitMoveAssignment(Class); |
1075 | 32.2k | } |
1076 | 186k | } |
1077 | 436k | break; |
1078 | | |
1079 | 2.00k | case DeclarationName::CXXDeductionGuideName: |
1080 | 2.00k | S.DeclareImplicitDeductionGuides(Name.getCXXDeductionGuideTemplate(), Loc); |
1081 | 2.00k | break; |
1082 | | |
1083 | 89.8M | default: |
1084 | 89.8M | break; |
1085 | 102M | } |
1086 | 102M | } |
1087 | | |
1088 | | // Adds all qualifying matches for a name within a decl context to the |
1089 | | // given lookup result. Returns true if any matches were found. |
1090 | 100M | static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { |
1091 | 100M | bool Found = false; |
1092 | | |
1093 | | // Lazily declare C++ special member functions. |
1094 | 100M | if (S.getLangOpts().CPlusPlus) |
1095 | 100M | DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), R.getNameLoc(), |
1096 | 100M | DC); |
1097 | | |
1098 | | // Perform lookup into this declaration context. |
1099 | 100M | DeclContext::lookup_result DR = DC->lookup(R.getLookupName()); |
1100 | 100M | for (NamedDecl *D : DR) { |
1101 | 94.5M | if ((D = R.getAcceptableDecl(D))) { |
1102 | 92.3M | R.addDecl(D); |
1103 | 92.3M | Found = true; |
1104 | 92.3M | } |
1105 | 94.5M | } |
1106 | | |
1107 | 100M | if (!Found && DC->isTranslationUnit()48.7M && S.LookupBuiltin(R)11.4M ) |
1108 | 209k | return true; |
1109 | | |
1110 | 100M | if (R.getLookupName().getNameKind() |
1111 | 100M | != DeclarationName::CXXConversionFunctionName || |
1112 | 100M | R.getLookupName().getCXXNameType()->isDependentType()44.9k || |
1113 | 100M | !isa<CXXRecordDecl>(DC)35.3k ) |
1114 | 100M | return Found; |
1115 | | |
1116 | | // C++ [temp.mem]p6: |
1117 | | // A specialization of a conversion function template is not found by |
1118 | | // name lookup. Instead, any conversion function templates visible in the |
1119 | | // context of the use are considered. [...] |
1120 | 22.0k | const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
1121 | 22.0k | if (!Record->isCompleteDefinition()) |
1122 | 21.7k | return Found; |
1123 | | |
1124 | | // For conversion operators, 'operator auto' should only match |
1125 | | // 'operator auto'. Since 'auto' is not a type, it shouldn't be considered |
1126 | | // as a candidate for template substitution. |
1127 | 303 | auto *ContainedDeducedType = |
1128 | 303 | R.getLookupName().getCXXNameType()->getContainedDeducedType(); |
1129 | 303 | if (R.getLookupName().getNameKind() == |
1130 | 303 | DeclarationName::CXXConversionFunctionName && |
1131 | 303 | ContainedDeducedType && ContainedDeducedType->isUndeducedType()66 ) |
1132 | 66 | return Found; |
1133 | | |
1134 | 237 | for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(), |
1135 | 575 | UEnd = Record->conversion_end(); U != UEnd; ++U338 ) { |
1136 | 338 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); |
1137 | 338 | if (!ConvTemplate) |
1138 | 181 | continue; |
1139 | | |
1140 | | // When we're performing lookup for the purposes of redeclaration, just |
1141 | | // add the conversion function template. When we deduce template |
1142 | | // arguments for specializations, we'll end up unifying the return |
1143 | | // type of the new declaration with the type of the function template. |
1144 | 157 | if (R.isForRedeclaration()) { |
1145 | 4 | R.addDecl(ConvTemplate); |
1146 | 4 | Found = true; |
1147 | 4 | continue; |
1148 | 4 | } |
1149 | | |
1150 | | // C++ [temp.mem]p6: |
1151 | | // [...] For each such operator, if argument deduction succeeds |
1152 | | // (14.9.2.3), the resulting specialization is used as if found by |
1153 | | // name lookup. |
1154 | | // |
1155 | | // When referencing a conversion function for any purpose other than |
1156 | | // a redeclaration (such that we'll be building an expression with the |
1157 | | // result), perform template argument deduction and place the |
1158 | | // specialization into the result set. We do this to avoid forcing all |
1159 | | // callers to perform special deduction for conversion functions. |
1160 | 153 | TemplateDeductionInfo Info(R.getNameLoc()); |
1161 | 153 | FunctionDecl *Specialization = nullptr; |
1162 | | |
1163 | 153 | const FunctionProtoType *ConvProto |
1164 | 153 | = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); |
1165 | 153 | assert(ConvProto && "Nonsensical conversion function template type"); |
1166 | | |
1167 | | // Compute the type of the function that we would expect the conversion |
1168 | | // function to have, if it were to match the name given. |
1169 | | // FIXME: Calling convention! |
1170 | 0 | FunctionProtoType::ExtProtoInfo EPI = ConvProto->getExtProtoInfo(); |
1171 | 153 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC_C); |
1172 | 153 | EPI.ExceptionSpec = EST_None; |
1173 | 153 | QualType ExpectedType |
1174 | 153 | = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(), |
1175 | 153 | None, EPI); |
1176 | | |
1177 | | // Perform template argument deduction against the type that we would |
1178 | | // expect the function to have. |
1179 | 153 | if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType, |
1180 | 153 | Specialization, Info) |
1181 | 153 | == Sema::TDK_Success) { |
1182 | 117 | R.addDecl(Specialization); |
1183 | 117 | Found = true; |
1184 | 117 | } |
1185 | 153 | } |
1186 | | |
1187 | 237 | return Found; |
1188 | 303 | } |
1189 | | |
1190 | | // Performs C++ unqualified lookup into the given file context. |
1191 | | static bool |
1192 | | CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, |
1193 | 69.1M | DeclContext *NS, UnqualUsingDirectiveSet &UDirs) { |
1194 | | |
1195 | 69.1M | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); |
1196 | | |
1197 | | // Perform direct name lookup into the LookupCtx. |
1198 | 0 | bool Found = LookupDirect(S, R, NS); |
1199 | | |
1200 | | // Perform direct name lookup into the namespaces nominated by the |
1201 | | // using directives whose common ancestor is this namespace. |
1202 | 69.1M | for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(NS)) |
1203 | 10.8M | if (LookupDirect(S, R, UUE.getNominatedNamespace())) |
1204 | 16.9k | Found = true; |
1205 | | |
1206 | 69.1M | R.resolveKind(); |
1207 | | |
1208 | 69.1M | return Found; |
1209 | 69.1M | } |
1210 | | |
1211 | 291M | static bool isNamespaceOrTranslationUnitScope(Scope *S) { |
1212 | 291M | if (DeclContext *Ctx = S->getEntity()) |
1213 | 210M | return Ctx->isFileContext(); |
1214 | 80.7M | return false; |
1215 | 291M | } |
1216 | | |
1217 | | /// Find the outer declaration context from this scope. This indicates the |
1218 | | /// context that we should search up to (exclusive) before considering the |
1219 | | /// parent of the specified scope. |
1220 | 136M | static DeclContext *findOuterContext(Scope *S) { |
1221 | 146M | for (Scope *OuterS = S->getParent(); OuterS; OuterS = OuterS->getParent()9.52M ) |
1222 | 96.4M | if (DeclContext *DC = OuterS->getLookupEntity()) |
1223 | 86.9M | return DC; |
1224 | 49.9M | return nullptr; |
1225 | 136M | } |
1226 | | |
1227 | | namespace { |
1228 | | /// An RAII object to specify that we want to find block scope extern |
1229 | | /// declarations. |
1230 | | struct FindLocalExternScope { |
1231 | | FindLocalExternScope(LookupResult &R) |
1232 | | : R(R), OldFindLocalExtern(R.getIdentifierNamespace() & |
1233 | 181M | Decl::IDNS_LocalExtern) { |
1234 | 181M | R.setFindLocalExtern(R.getIdentifierNamespace() & |
1235 | 181M | (Decl::IDNS_Ordinary | Decl::IDNS_NonMemberOperator)); |
1236 | 181M | } |
1237 | 223M | void restore() { |
1238 | 223M | R.setFindLocalExtern(OldFindLocalExtern); |
1239 | 223M | } |
1240 | 181M | ~FindLocalExternScope() { |
1241 | 181M | restore(); |
1242 | 181M | } |
1243 | | LookupResult &R; |
1244 | | bool OldFindLocalExtern; |
1245 | | }; |
1246 | | } // end anonymous namespace |
1247 | | |
1248 | 85.0M | bool Sema::CppLookupName(LookupResult &R, Scope *S) { |
1249 | 85.0M | assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup"); |
1250 | | |
1251 | 0 | DeclarationName Name = R.getLookupName(); |
1252 | 85.0M | Sema::LookupNameKind NameKind = R.getLookupKind(); |
1253 | | |
1254 | | // If this is the name of an implicitly-declared special member function, |
1255 | | // go through the scope stack to implicitly declare |
1256 | 85.0M | if (isImplicitlyDeclaredMemberFunctionName(Name)) { |
1257 | 2.27M | for (Scope *PreS = S; PreS; PreS = PreS->getParent()1.85M ) |
1258 | 1.85M | if (DeclContext *DC = PreS->getEntity()) |
1259 | 1.55M | DeclareImplicitMemberFunctionsWithName(*this, Name, R.getNameLoc(), DC); |
1260 | 422k | } |
1261 | | |
1262 | | // Implicitly declare member functions with the name we're looking for, if in |
1263 | | // fact we are in a scope where it matters. |
1264 | | |
1265 | 85.0M | Scope *Initial = S; |
1266 | 85.0M | IdentifierResolver::iterator |
1267 | 85.0M | I = IdResolver.begin(Name), |
1268 | 85.0M | IEnd = IdResolver.end(); |
1269 | | |
1270 | | // First we lookup local scope. |
1271 | | // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] |
1272 | | // ...During unqualified name lookup (3.4.1), the names appear as if |
1273 | | // they were declared in the nearest enclosing namespace which contains |
1274 | | // both the using-directive and the nominated namespace. |
1275 | | // [Note: in this context, "contains" means "contains directly or |
1276 | | // indirectly". |
1277 | | // |
1278 | | // For example: |
1279 | | // namespace A { int i; } |
1280 | | // void foo() { |
1281 | | // int i; |
1282 | | // { |
1283 | | // using namespace A; |
1284 | | // ++i; // finds local 'i', A::i appears at global scope |
1285 | | // } |
1286 | | // } |
1287 | | // |
1288 | 85.0M | UnqualUsingDirectiveSet UDirs(*this); |
1289 | 85.0M | bool VisitedUsingDirectives = false; |
1290 | 85.0M | bool LeftStartingScope = false; |
1291 | | |
1292 | | // When performing a scope lookup, we want to find local extern decls. |
1293 | 85.0M | FindLocalExternScope FindLocals(R); |
1294 | | |
1295 | 221M | for (; S && !isNamespaceOrTranslationUnitScope(S)220M ; S = S->getParent()136M ) { |
1296 | 155M | bool SearchNamespaceScope = true; |
1297 | | // Check whether the IdResolver has anything in this scope. |
1298 | 175M | for (; I != IEnd && S->isDeclScope(*I)109M ; ++I19.7M ) { |
1299 | 19.7M | if (NamedDecl *ND = R.getAcceptableDecl(*I)) { |
1300 | 19.5M | if (NameKind == LookupRedeclarationWithLinkage && |
1301 | 19.5M | !(*I)->isTemplateParameter()156 ) { |
1302 | | // If it's a template parameter, we still find it, so we can diagnose |
1303 | | // the invalid redeclaration. |
1304 | | |
1305 | | // Determine whether this (or a previous) declaration is |
1306 | | // out-of-scope. |
1307 | 152 | if (!LeftStartingScope && !Initial->isDeclScope(*I)141 ) |
1308 | 48 | LeftStartingScope = true; |
1309 | | |
1310 | | // If we found something outside of our starting scope that |
1311 | | // does not have linkage, skip it. |
1312 | 152 | if (LeftStartingScope && !((*I)->hasLinkage())59 ) { |
1313 | 21 | R.setShadowed(); |
1314 | 21 | continue; |
1315 | 21 | } |
1316 | 19.5M | } else { |
1317 | | // We found something in this scope, we should not look at the |
1318 | | // namespace scope |
1319 | 19.5M | SearchNamespaceScope = false; |
1320 | 19.5M | } |
1321 | 19.5M | R.addDecl(ND); |
1322 | 19.5M | } |
1323 | 19.7M | } |
1324 | 155M | if (!SearchNamespaceScope) { |
1325 | 18.1M | R.resolveKind(); |
1326 | 18.1M | if (S->isClassScope()) |
1327 | 3.31M | if (CXXRecordDecl *Record = |
1328 | 3.31M | dyn_cast_or_null<CXXRecordDecl>(S->getEntity())) |
1329 | 3.31M | R.setNamingClass(Record); |
1330 | 18.1M | return true; |
1331 | 18.1M | } |
1332 | | |
1333 | 137M | if (NameKind == LookupLocalFriendName && !S->isClassScope()69 ) { |
1334 | | // C++11 [class.friend]p11: |
1335 | | // If a friend declaration appears in a local class and the name |
1336 | | // specified is an unqualified name, a prior declaration is |
1337 | | // looked up without considering scopes that are outside the |
1338 | | // innermost enclosing non-class scope. |
1339 | 44 | return false; |
1340 | 44 | } |
1341 | | |
1342 | 137M | if (DeclContext *Ctx = S->getLookupEntity()) { |
1343 | 67.7M | DeclContext *OuterCtx = findOuterContext(S); |
1344 | 134M | for (; Ctx && !Ctx->Equals(OuterCtx)134M ; Ctx = Ctx->getLookupParent()66.2M ) { |
1345 | | // We do not directly look into transparent contexts, since |
1346 | | // those entities will be found in the nearest enclosing |
1347 | | // non-transparent context. |
1348 | 67.9M | if (Ctx->isTransparentContext()) |
1349 | 42.6M | continue; |
1350 | | |
1351 | | // We do not look directly into function or method contexts, |
1352 | | // since all of the local variables and parameters of the |
1353 | | // function/method are present within the Scope. |
1354 | 25.3M | if (Ctx->isFunctionOrMethod()) { |
1355 | | // If we have an Objective-C instance method, look for ivars |
1356 | | // in the corresponding interface. |
1357 | 12.3M | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
1358 | 1.73k | if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()1.60k ) |
1359 | 1.58k | if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { |
1360 | 1.57k | ObjCInterfaceDecl *ClassDeclared; |
1361 | 1.57k | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( |
1362 | 1.57k | Name.getAsIdentifierInfo(), |
1363 | 1.57k | ClassDeclared)) { |
1364 | 349 | if (NamedDecl *ND = R.getAcceptableDecl(Ivar)) { |
1365 | 349 | R.addDecl(ND); |
1366 | 349 | R.resolveKind(); |
1367 | 349 | return true; |
1368 | 349 | } |
1369 | 349 | } |
1370 | 1.57k | } |
1371 | 1.73k | } |
1372 | | |
1373 | 12.3M | continue; |
1374 | 12.3M | } |
1375 | | |
1376 | | // If this is a file context, we need to perform unqualified name |
1377 | | // lookup considering using directives. |
1378 | 13.0M | if (Ctx->isFileContext()) { |
1379 | | // If we haven't handled using directives yet, do so now. |
1380 | 3.56k | if (!VisitedUsingDirectives) { |
1381 | | // Add using directives from this context up to the top level. |
1382 | 8.60k | for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent()6.14k ) { |
1383 | 6.14k | if (UCtx->isTransparentContext()) |
1384 | 1 | continue; |
1385 | | |
1386 | 6.14k | UDirs.visit(UCtx, UCtx); |
1387 | 6.14k | } |
1388 | | |
1389 | | // Find the innermost file scope, so we can add using directives |
1390 | | // from local scopes. |
1391 | 2.45k | Scope *InnermostFileScope = S; |
1392 | 5.25k | while (InnermostFileScope && |
1393 | 5.25k | !isNamespaceOrTranslationUnitScope(InnermostFileScope)) |
1394 | 2.79k | InnermostFileScope = InnermostFileScope->getParent(); |
1395 | 2.45k | UDirs.visitScopeChain(Initial, InnermostFileScope); |
1396 | | |
1397 | 2.45k | UDirs.done(); |
1398 | | |
1399 | 2.45k | VisitedUsingDirectives = true; |
1400 | 2.45k | } |
1401 | | |
1402 | 3.56k | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) { |
1403 | 525 | R.resolveKind(); |
1404 | 525 | return true; |
1405 | 525 | } |
1406 | | |
1407 | 3.03k | continue; |
1408 | 3.56k | } |
1409 | | |
1410 | | // Perform qualified name lookup into this context. |
1411 | | // FIXME: In some cases, we know that every name that could be found by |
1412 | | // this qualified name lookup will also be on the identifier chain. For |
1413 | | // example, inside a class without any base classes, we never need to |
1414 | | // perform qualified lookup because all of the members are on top of the |
1415 | | // identifier chain. |
1416 | 12.9M | if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) |
1417 | 1.66M | return true; |
1418 | 12.9M | } |
1419 | 67.7M | } |
1420 | 137M | } |
1421 | | |
1422 | | // Stop if we ran out of scopes. |
1423 | | // FIXME: This really, really shouldn't be happening. |
1424 | 65.2M | if (!S) return false167k ; |
1425 | | |
1426 | | // If we are looking for members, no need to look into global/namespace scope. |
1427 | 65.1M | if (NameKind == LookupMemberName) |
1428 | 1.10M | return false; |
1429 | | |
1430 | | // Collect UsingDirectiveDecls in all scopes, and recursively all |
1431 | | // nominated namespaces by those using-directives. |
1432 | | // |
1433 | | // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we |
1434 | | // don't build it for each lookup! |
1435 | 63.9M | if (!VisitedUsingDirectives) { |
1436 | 63.9M | UDirs.visitScopeChain(Initial, S); |
1437 | 63.9M | UDirs.done(); |
1438 | 63.9M | } |
1439 | | |
1440 | | // If we're not performing redeclaration lookup, do not look for local |
1441 | | // extern declarations outside of a function scope. |
1442 | 63.9M | if (!R.isForRedeclaration()) |
1443 | 42.6M | FindLocals.restore(); |
1444 | | |
1445 | | // Lookup namespace scope, and global scope. |
1446 | | // Unqualified name lookup in C++ requires looking into scopes |
1447 | | // that aren't strictly lexical, and therefore we walk through the |
1448 | | // context as well as walking through the scopes. |
1449 | 70.9M | for (; S; S = S->getParent()6.99M ) { |
1450 | | // Check whether the IdResolver has anything in this scope. |
1451 | 69.1M | bool Found = false; |
1452 | 131M | for (; I != IEnd && S->isDeclScope(*I)64.3M ; ++I61.8M ) { |
1453 | 61.8M | if (NamedDecl *ND = R.getAcceptableDecl(*I)) { |
1454 | | // We found something. Look for anything else in our scope |
1455 | | // with this same name and in an acceptable identifier |
1456 | | // namespace, so that we can construct an overload set if we |
1457 | | // need to. |
1458 | 61.7M | Found = true; |
1459 | 61.7M | R.addDecl(ND); |
1460 | 61.7M | } |
1461 | 61.8M | } |
1462 | | |
1463 | 69.1M | if (Found && S->isTemplateParamScope()40.8M ) { |
1464 | 11 | R.resolveKind(); |
1465 | 11 | return true; |
1466 | 11 | } |
1467 | | |
1468 | 69.1M | DeclContext *Ctx = S->getLookupEntity(); |
1469 | 69.1M | if (Ctx) { |
1470 | 69.1M | DeclContext *OuterCtx = findOuterContext(S); |
1471 | 76.1M | for (; Ctx && !Ctx->Equals(OuterCtx)74.3M ; Ctx = Ctx->getLookupParent()6.99M ) { |
1472 | | // We do not directly look into transparent contexts, since |
1473 | | // those entities will be found in the nearest enclosing |
1474 | | // non-transparent context. |
1475 | 69.1M | if (Ctx->isTransparentContext()) |
1476 | 5.72k | continue; |
1477 | | |
1478 | | // If we have a context, and it's not a context stashed in the |
1479 | | // template parameter scope for an out-of-line definition, also |
1480 | | // look into that context. |
1481 | 69.1M | if (!(Found && S->isTemplateParamScope()40.8M )) { |
1482 | 69.1M | assert(Ctx->isFileContext() && |
1483 | 69.1M | "We should have been looking only at file context here already."); |
1484 | | |
1485 | | // Look into context considering using-directives. |
1486 | 69.1M | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) |
1487 | 44.6M | Found = true; |
1488 | 69.1M | } |
1489 | | |
1490 | 69.1M | if (Found) { |
1491 | 44.6M | R.resolveKind(); |
1492 | 44.6M | return true; |
1493 | 44.6M | } |
1494 | | |
1495 | 24.4M | if (R.isForRedeclaration() && !Ctx->isTransparentContext()17.5M ) |
1496 | 17.5M | return false; |
1497 | 24.4M | } |
1498 | 69.1M | } |
1499 | | |
1500 | 6.99M | if (R.isForRedeclaration() && Ctx0 && !Ctx->isTransparentContext()0 ) |
1501 | 0 | return false; |
1502 | 6.99M | } |
1503 | | |
1504 | 1.82M | return !R.empty(); |
1505 | 63.9M | } |
1506 | | |
1507 | 913 | void Sema::makeMergedDefinitionVisible(NamedDecl *ND) { |
1508 | 913 | if (auto *M = getCurrentModule()) |
1509 | 739 | Context.mergeDefinitionIntoModule(ND, M); |
1510 | 174 | else |
1511 | | // We're not building a module; just make the definition visible. |
1512 | 174 | ND->setVisibleDespiteOwningModule(); |
1513 | | |
1514 | | // If ND is a template declaration, make the template parameters |
1515 | | // visible too. They're not (necessarily) within a mergeable DeclContext. |
1516 | 913 | if (auto *TD = dyn_cast<TemplateDecl>(ND)) |
1517 | 187 | for (auto *Param : *TD->getTemplateParameters()) |
1518 | 224 | makeMergedDefinitionVisible(Param); |
1519 | 913 | } |
1520 | | |
1521 | | /// Find the module in which the given declaration was defined. |
1522 | 3.90k | static Module *getDefiningModule(Sema &S, Decl *Entity) { |
1523 | 3.90k | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Entity)) { |
1524 | | // If this function was instantiated from a template, the defining module is |
1525 | | // the module containing the pattern. |
1526 | 1.00k | if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern()) |
1527 | 733 | Entity = Pattern; |
1528 | 2.90k | } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Entity)) { |
1529 | 1.67k | if (CXXRecordDecl *Pattern = RD->getTemplateInstantiationPattern()) |
1530 | 727 | Entity = Pattern; |
1531 | 1.67k | } else if (EnumDecl *1.22k ED1.22k = dyn_cast<EnumDecl>(Entity)) { |
1532 | 0 | if (auto *Pattern = ED->getTemplateInstantiationPattern()) |
1533 | 0 | Entity = Pattern; |
1534 | 1.22k | } else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) { |
1535 | 36 | if (VarDecl *Pattern = VD->getTemplateInstantiationPattern()) |
1536 | 36 | Entity = Pattern; |
1537 | 36 | } |
1538 | | |
1539 | | // Walk up to the containing context. That might also have been instantiated |
1540 | | // from a template. |
1541 | 3.90k | DeclContext *Context = Entity->getLexicalDeclContext(); |
1542 | 3.90k | if (Context->isFileContext()) |
1543 | 2.78k | return S.getOwningModule(Entity); |
1544 | 1.12k | return getDefiningModule(S, cast<Decl>(Context)); |
1545 | 3.90k | } |
1546 | | |
1547 | 551k | llvm::DenseSet<Module*> &Sema::getLookupModules() { |
1548 | 551k | unsigned N = CodeSynthesisContexts.size(); |
1549 | 551k | for (unsigned I = CodeSynthesisContextLookupModules.size(); |
1550 | 554k | I != N; ++I2.98k ) { |
1551 | 2.98k | Module *M = CodeSynthesisContexts[I].Entity ? |
1552 | 2.78k | getDefiningModule(*this, CodeSynthesisContexts[I].Entity) : |
1553 | 2.98k | nullptr200 ; |
1554 | 2.98k | if (M && !LookupModulesCache.insert(M).second2.74k ) |
1555 | 1.41k | M = nullptr; |
1556 | 2.98k | CodeSynthesisContextLookupModules.push_back(M); |
1557 | 2.98k | } |
1558 | 551k | return LookupModulesCache; |
1559 | 551k | } |
1560 | | |
1561 | | /// Determine if we could use all the declarations in the module. |
1562 | 3.38k | bool Sema::isUsableModule(const Module *M) { |
1563 | 3.38k | assert(M && "We shouldn't check nullness for module here"); |
1564 | | // Return quickly if we cached the result. |
1565 | 3.38k | if (UsableModuleUnitsCache.count(M)) |
1566 | 2.27k | return true; |
1567 | | |
1568 | | // If M is the global module fragment of the current translation unit. So it |
1569 | | // should be usable. |
1570 | | // [module.global.frag]p1: |
1571 | | // The global module fragment can be used to provide declarations that are |
1572 | | // attached to the global module and usable within the module unit. |
1573 | 1.10k | if (M == GlobalModuleFragment || |
1574 | | // If M is the module we're parsing, it should be usable. This covers the |
1575 | | // private module fragment. The private module fragment is usable only if |
1576 | | // it is within the current module unit. And it must be the current |
1577 | | // parsing module unit if it is within the current module unit according |
1578 | | // to the grammar of the private module fragment. NOTE: This is covered by |
1579 | | // the following condition. The intention of the check is to avoid string |
1580 | | // comparison as much as possible. |
1581 | 1.10k | M == getCurrentModule()1.03k || |
1582 | | // The module unit which is in the same module with the current module |
1583 | | // unit is usable. |
1584 | | // |
1585 | | // FIXME: Here we judge if they are in the same module by comparing the |
1586 | | // string. Is there any better solution? |
1587 | 1.10k | M->getPrimaryModuleInterfaceName() == |
1588 | 936 | llvm::StringRef(getLangOpts().CurrentModule).split(':').first) { |
1589 | 187 | UsableModuleUnitsCache.insert(M); |
1590 | 187 | return true; |
1591 | 187 | } |
1592 | | |
1593 | 918 | return false; |
1594 | 1.10k | } |
1595 | | |
1596 | 23.4k | bool Sema::hasVisibleMergedDefinition(NamedDecl *Def) { |
1597 | 23.4k | for (const Module *Merged : Context.getModulesWithMergedDefinition(Def)) |
1598 | 3.04k | if (isModuleVisible(Merged)) |
1599 | 595 | return true; |
1600 | 22.8k | return false; |
1601 | 23.4k | } |
1602 | | |
1603 | 14 | bool Sema::hasMergedDefinitionInCurrentModule(NamedDecl *Def) { |
1604 | 14 | for (const Module *Merged : Context.getModulesWithMergedDefinition(Def)) |
1605 | 0 | if (isUsableModule(Merged)) |
1606 | 0 | return true; |
1607 | 14 | return false; |
1608 | 14 | } |
1609 | | |
1610 | | template <typename ParmDecl> |
1611 | | static bool |
1612 | | hasAcceptableDefaultArgument(Sema &S, const ParmDecl *D, |
1613 | | llvm::SmallVectorImpl<Module *> *Modules, |
1614 | 2.74M | Sema::AcceptableKind Kind) { |
1615 | 2.74M | if (!D->hasDefaultArgument()) |
1616 | 416k | return false; |
1617 | | |
1618 | 2.95M | while (2.32M D) { |
1619 | 2.95M | auto &DefaultArg = D->getDefaultArgStorage(); |
1620 | 2.95M | if (!DefaultArg.isInherited() && S.isAcceptable(D, Kind)2.32M ) |
1621 | 2.32M | return true; |
1622 | | |
1623 | 625k | if (!DefaultArg.isInherited() && Modules390 ) { |
1624 | 18 | auto *NonConstD = const_cast<ParmDecl*>(D); |
1625 | 18 | Modules->push_back(S.getOwningModule(NonConstD)); |
1626 | 18 | } |
1627 | | |
1628 | | // If there was a previous default argument, maybe its parameter is visible. |
1629 | 625k | D = DefaultArg.getInheritedFrom(); |
1630 | 625k | } |
1631 | 352 | return false; |
1632 | 2.32M | } SemaLookup.cpp:bool hasAcceptableDefaultArgument<clang::TemplateTypeParmDecl>(clang::Sema&, clang::TemplateTypeParmDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::AcceptableKind) Line | Count | Source | 1614 | 2.05M | Sema::AcceptableKind Kind) { | 1615 | 2.05M | if (!D->hasDefaultArgument()) | 1616 | 383k | return false; | 1617 | | | 1618 | 2.23M | while (1.66M D) { | 1619 | 2.23M | auto &DefaultArg = D->getDefaultArgStorage(); | 1620 | 2.23M | if (!DefaultArg.isInherited() && S.isAcceptable(D, Kind)1.66M ) | 1621 | 1.66M | return true; | 1622 | | | 1623 | 566k | if (!DefaultArg.isInherited() && Modules264 ) { | 1624 | 18 | auto *NonConstD = const_cast<ParmDecl*>(D); | 1625 | 18 | Modules->push_back(S.getOwningModule(NonConstD)); | 1626 | 18 | } | 1627 | | | 1628 | | // If there was a previous default argument, maybe its parameter is visible. | 1629 | 566k | D = DefaultArg.getInheritedFrom(); | 1630 | 566k | } | 1631 | 248 | return false; | 1632 | 1.66M | } |
SemaLookup.cpp:bool hasAcceptableDefaultArgument<clang::NonTypeTemplateParmDecl>(clang::Sema&, clang::NonTypeTemplateParmDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::AcceptableKind) Line | Count | Source | 1614 | 687k | Sema::AcceptableKind Kind) { | 1615 | 687k | if (!D->hasDefaultArgument()) | 1616 | 32.4k | return false; | 1617 | | | 1618 | 714k | while (655k D) { | 1619 | 714k | auto &DefaultArg = D->getDefaultArgStorage(); | 1620 | 714k | if (!DefaultArg.isInherited() && S.isAcceptable(D, Kind)655k ) | 1621 | 655k | return true; | 1622 | | | 1623 | 59.2k | if (!DefaultArg.isInherited() && Modules64 ) { | 1624 | 0 | auto *NonConstD = const_cast<ParmDecl*>(D); | 1625 | 0 | Modules->push_back(S.getOwningModule(NonConstD)); | 1626 | 0 | } | 1627 | | | 1628 | | // If there was a previous default argument, maybe its parameter is visible. | 1629 | 59.2k | D = DefaultArg.getInheritedFrom(); | 1630 | 59.2k | } | 1631 | 53 | return false; | 1632 | 655k | } |
SemaLookup.cpp:bool hasAcceptableDefaultArgument<clang::TemplateTemplateParmDecl>(clang::Sema&, clang::TemplateTemplateParmDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::AcceptableKind) Line | Count | Source | 1614 | 2.60k | Sema::AcceptableKind Kind) { | 1615 | 2.60k | if (!D->hasDefaultArgument()) | 1616 | 217 | return false; | 1617 | | | 1618 | 2.49k | while (2.38k D) { | 1619 | 2.44k | auto &DefaultArg = D->getDefaultArgStorage(); | 1620 | 2.44k | if (!DefaultArg.isInherited() && S.isAcceptable(D, Kind)2.40k ) | 1621 | 2.33k | return true; | 1622 | | | 1623 | 105 | if (!DefaultArg.isInherited() && Modules62 ) { | 1624 | 0 | auto *NonConstD = const_cast<ParmDecl*>(D); | 1625 | 0 | Modules->push_back(S.getOwningModule(NonConstD)); | 1626 | 0 | } | 1627 | | | 1628 | | // If there was a previous default argument, maybe its parameter is visible. | 1629 | 105 | D = DefaultArg.getInheritedFrom(); | 1630 | 105 | } | 1631 | 51 | return false; | 1632 | 2.38k | } |
|
1633 | | |
1634 | | bool Sema::hasAcceptableDefaultArgument( |
1635 | | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules, |
1636 | 2.74M | Sema::AcceptableKind Kind) { |
1637 | 2.74M | if (auto *P = dyn_cast<TemplateTypeParmDecl>(D)) |
1638 | 2.05M | return ::hasAcceptableDefaultArgument(*this, P, Modules, Kind); |
1639 | | |
1640 | 690k | if (auto *P = dyn_cast<NonTypeTemplateParmDecl>(D)) |
1641 | 687k | return ::hasAcceptableDefaultArgument(*this, P, Modules, Kind); |
1642 | | |
1643 | 2.60k | return ::hasAcceptableDefaultArgument( |
1644 | 2.60k | *this, cast<TemplateTemplateParmDecl>(D), Modules, Kind); |
1645 | 690k | } |
1646 | | |
1647 | | bool Sema::hasVisibleDefaultArgument(const NamedDecl *D, |
1648 | 1.33M | llvm::SmallVectorImpl<Module *> *Modules) { |
1649 | 1.33M | return hasAcceptableDefaultArgument(D, Modules, |
1650 | 1.33M | Sema::AcceptableKind::Visible); |
1651 | 1.33M | } |
1652 | | |
1653 | | bool Sema::hasReachableDefaultArgument( |
1654 | 1.40M | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { |
1655 | 1.40M | return hasAcceptableDefaultArgument(D, Modules, |
1656 | 1.40M | Sema::AcceptableKind::Reachable); |
1657 | 1.40M | } |
1658 | | |
1659 | | template <typename Filter> |
1660 | | static bool |
1661 | | hasAcceptableDeclarationImpl(Sema &S, const NamedDecl *D, |
1662 | | llvm::SmallVectorImpl<Module *> *Modules, Filter F, |
1663 | 13.2k | Sema::AcceptableKind Kind) { |
1664 | 13.2k | bool HasFilteredRedecls = false; |
1665 | | |
1666 | 13.3k | for (auto *Redecl : D->redecls()) { |
1667 | 13.3k | auto *R = cast<NamedDecl>(Redecl); |
1668 | 13.3k | if (!F(R)) |
1669 | 108 | continue; |
1670 | | |
1671 | 13.2k | if (S.isAcceptable(R, Kind)) |
1672 | 13.0k | return true; |
1673 | | |
1674 | 196 | HasFilteredRedecls = true; |
1675 | | |
1676 | 196 | if (Modules) |
1677 | 0 | Modules->push_back(R->getOwningModule()); |
1678 | 196 | } |
1679 | | |
1680 | | // Only return false if there is at least one redecl that is not filtered out. |
1681 | 207 | if (HasFilteredRedecls) |
1682 | 185 | return false; |
1683 | | |
1684 | 22 | return true; |
1685 | 207 | } SemaLookup.cpp:bool hasAcceptableDeclarationImpl<hasAcceptableExplicitSpecialization(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::AcceptableKind)::$_17>(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, hasAcceptableExplicitSpecialization(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::AcceptableKind)::$_17, clang::Sema::AcceptableKind) Line | Count | Source | 1663 | 13.0k | Sema::AcceptableKind Kind) { | 1664 | 13.0k | bool HasFilteredRedecls = false; | 1665 | | | 1666 | 13.0k | for (auto *Redecl : D->redecls()) { | 1667 | 13.0k | auto *R = cast<NamedDecl>(Redecl); | 1668 | 13.0k | if (!F(R)) | 1669 | 0 | continue; | 1670 | | | 1671 | 13.0k | if (S.isAcceptable(R, Kind)) | 1672 | 13.0k | return true; | 1673 | | | 1674 | 80 | HasFilteredRedecls = true; | 1675 | | | 1676 | 80 | if (Modules) | 1677 | 0 | Modules->push_back(R->getOwningModule()); | 1678 | 80 | } | 1679 | | | 1680 | | // Only return false if there is at least one redecl that is not filtered out. | 1681 | 70 | if (HasFilteredRedecls) | 1682 | 70 | return false; | 1683 | | | 1684 | 0 | return true; | 1685 | 70 | } |
SemaLookup.cpp:bool hasAcceptableDeclarationImpl<hasAcceptableMemberSpecialization(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::AcceptableKind)::$_18>(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, hasAcceptableMemberSpecialization(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::AcceptableKind)::$_18, clang::Sema::AcceptableKind) Line | Count | Source | 1663 | 144 | Sema::AcceptableKind Kind) { | 1664 | 144 | bool HasFilteredRedecls = false; | 1665 | | | 1666 | 230 | for (auto *Redecl : D->redecls()) { | 1667 | 230 | auto *R = cast<NamedDecl>(Redecl); | 1668 | 230 | if (!F(R)) | 1669 | 108 | continue; | 1670 | | | 1671 | 122 | if (S.isAcceptable(R, Kind)) | 1672 | 47 | return true; | 1673 | | | 1674 | 75 | HasFilteredRedecls = true; | 1675 | | | 1676 | 75 | if (Modules) | 1677 | 0 | Modules->push_back(R->getOwningModule()); | 1678 | 75 | } | 1679 | | | 1680 | | // Only return false if there is at least one redecl that is not filtered out. | 1681 | 97 | if (HasFilteredRedecls) | 1682 | 75 | return false; | 1683 | | | 1684 | 22 | return true; | 1685 | 97 | } |
SemaLookup.cpp:bool hasAcceptableDeclarationImpl<clang::Sema::hasVisibleDeclarationSlow(clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*)::$_2>(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::hasVisibleDeclarationSlow(clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*)::$_2, clang::Sema::AcceptableKind) Line | Count | Source | 1663 | 41 | Sema::AcceptableKind Kind) { | 1664 | 41 | bool HasFilteredRedecls = false; | 1665 | | | 1666 | 42 | for (auto *Redecl : D->redecls()) { | 1667 | 42 | auto *R = cast<NamedDecl>(Redecl); | 1668 | 42 | if (!F(R)) | 1669 | 0 | continue; | 1670 | | | 1671 | 42 | if (S.isAcceptable(R, Kind)) | 1672 | 1 | return true; | 1673 | | | 1674 | 41 | HasFilteredRedecls = true; | 1675 | | | 1676 | 41 | if (Modules) | 1677 | 0 | Modules->push_back(R->getOwningModule()); | 1678 | 41 | } | 1679 | | | 1680 | | // Only return false if there is at least one redecl that is not filtered out. | 1681 | 40 | if (HasFilteredRedecls) | 1682 | 40 | return false; | 1683 | | | 1684 | 0 | return true; | 1685 | 40 | } |
Unexecuted instantiation: SemaLookup.cpp:bool hasAcceptableDeclarationImpl<clang::Sema::hasReachableDeclarationSlow(clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*)::$_3>(clang::Sema&, clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*, clang::Sema::hasReachableDeclarationSlow(clang::NamedDecl const*, llvm::SmallVectorImpl<clang::Module*>*)::$_3, clang::Sema::AcceptableKind) |
1686 | | |
1687 | | static bool |
1688 | | hasAcceptableExplicitSpecialization(Sema &S, const NamedDecl *D, |
1689 | | llvm::SmallVectorImpl<Module *> *Modules, |
1690 | 13.0k | Sema::AcceptableKind Kind) { |
1691 | 13.0k | return hasAcceptableDeclarationImpl( |
1692 | 13.0k | S, D, Modules, |
1693 | 13.0k | [](const NamedDecl *D) { |
1694 | 13.0k | if (auto *RD = dyn_cast<CXXRecordDecl>(D)) |
1695 | 13.0k | return RD->getTemplateSpecializationKind() == |
1696 | 13.0k | TSK_ExplicitSpecialization; |
1697 | 43 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
1698 | 33 | return FD->getTemplateSpecializationKind() == |
1699 | 33 | TSK_ExplicitSpecialization; |
1700 | 10 | if (auto *VD = dyn_cast<VarDecl>(D)) |
1701 | 10 | return VD->getTemplateSpecializationKind() == |
1702 | 10 | TSK_ExplicitSpecialization; |
1703 | 0 | llvm_unreachable("unknown explicit specialization kind"); |
1704 | 0 | }, |
1705 | 13.0k | Kind); |
1706 | 13.0k | } |
1707 | | |
1708 | | bool Sema::hasVisibleExplicitSpecialization( |
1709 | 8.13k | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { |
1710 | 8.13k | return ::hasAcceptableExplicitSpecialization(*this, D, Modules, |
1711 | 8.13k | Sema::AcceptableKind::Visible); |
1712 | 8.13k | } |
1713 | | |
1714 | | bool Sema::hasReachableExplicitSpecialization( |
1715 | 4.94k | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { |
1716 | 4.94k | return ::hasAcceptableExplicitSpecialization(*this, D, Modules, |
1717 | 4.94k | Sema::AcceptableKind::Reachable); |
1718 | 4.94k | } |
1719 | | |
1720 | | static bool |
1721 | | hasAcceptableMemberSpecialization(Sema &S, const NamedDecl *D, |
1722 | | llvm::SmallVectorImpl<Module *> *Modules, |
1723 | 144 | Sema::AcceptableKind Kind) { |
1724 | 144 | assert(isa<CXXRecordDecl>(D->getDeclContext()) && |
1725 | 144 | "not a member specialization"); |
1726 | 0 | return hasAcceptableDeclarationImpl( |
1727 | 144 | S, D, Modules, |
1728 | 230 | [](const NamedDecl *D) { |
1729 | | // If the specialization is declared at namespace scope, then it's a |
1730 | | // member specialization declaration. If it's lexically inside the class |
1731 | | // definition then it was instantiated. |
1732 | | // |
1733 | | // FIXME: This is a hack. There should be a better way to determine |
1734 | | // this. |
1735 | | // FIXME: What about MS-style explicit specializations declared within a |
1736 | | // class definition? |
1737 | 230 | return D->getLexicalDeclContext()->isFileContext(); |
1738 | 230 | }, |
1739 | 144 | Kind); |
1740 | 144 | } |
1741 | | |
1742 | | bool Sema::hasVisibleMemberSpecialization( |
1743 | 119 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { |
1744 | 119 | return hasAcceptableMemberSpecialization(*this, D, Modules, |
1745 | 119 | Sema::AcceptableKind::Visible); |
1746 | 119 | } |
1747 | | |
1748 | | bool Sema::hasReachableMemberSpecialization( |
1749 | 25 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { |
1750 | 25 | return hasAcceptableMemberSpecialization(*this, D, Modules, |
1751 | 25 | Sema::AcceptableKind::Reachable); |
1752 | 25 | } |
1753 | | |
1754 | | /// Determine whether a declaration is acceptable to name lookup. |
1755 | | /// |
1756 | | /// This routine determines whether the declaration D is acceptable in the |
1757 | | /// current lookup context, taking into account the current template |
1758 | | /// instantiation stack. During template instantiation, a declaration is |
1759 | | /// acceptable if it is acceptable from a module containing any entity on the |
1760 | | /// template instantiation path (by instantiating a template, you allow it to |
1761 | | /// see the declarations that your module can see, including those later on in |
1762 | | /// your module). |
1763 | | bool LookupResult::isAcceptableSlow(Sema &SemaRef, NamedDecl *D, |
1764 | 1.48M | Sema::AcceptableKind Kind) { |
1765 | 1.48M | assert(!D->isUnconditionallyVisible() && |
1766 | 1.48M | "should not call this: not in slow case"); |
1767 | | |
1768 | 0 | Module *DeclModule = SemaRef.getOwningModule(D); |
1769 | 1.48M | assert(DeclModule && "hidden decl has no owning module"); |
1770 | | |
1771 | | // If the owning module is visible, the decl is acceptable. |
1772 | 1.48M | if (SemaRef.isModuleVisible(DeclModule, |
1773 | 1.48M | D->isInvisibleOutsideTheOwningModule())) |
1774 | 936k | return true; |
1775 | | |
1776 | | // Determine whether a decl context is a file context for the purpose of |
1777 | | // visibility/reachability. This looks through some (export and linkage spec) |
1778 | | // transparent contexts, but not others (enums). |
1779 | 544k | auto IsEffectivelyFileContext = [](const DeclContext *DC) 544k { |
1780 | 544k | return DC->isFileContext() || isa<LinkageSpecDecl>(DC)31.8k || |
1781 | 544k | isa<ExportDecl>(DC)9.82k ; |
1782 | 544k | }; |
1783 | | |
1784 | | // If this declaration is not at namespace scope |
1785 | | // then it is acceptable if its lexical parent has a acceptable definition. |
1786 | 544k | DeclContext *DC = D->getLexicalDeclContext(); |
1787 | 544k | if (DC && !IsEffectivelyFileContext(DC)) { |
1788 | | // For a parameter, check whether our current template declaration's |
1789 | | // lexical context is acceptable, not whether there's some other acceptable |
1790 | | // definition of it, because parameters aren't "within" the definition. |
1791 | | // |
1792 | | // In C++ we need to check for a acceptable definition due to ODR merging, |
1793 | | // and in C we must not because each declaration of a function gets its own |
1794 | | // set of declarations for tags in prototype scope. |
1795 | 9.64k | bool AcceptableWithinParent; |
1796 | 9.64k | if (D->isTemplateParameter()) { |
1797 | 348 | bool SearchDefinitions = true; |
1798 | 348 | if (const auto *DCD = dyn_cast<Decl>(DC)) { |
1799 | 348 | if (const auto *TD = DCD->getDescribedTemplate()) { |
1800 | 346 | TemplateParameterList *TPL = TD->getTemplateParameters(); |
1801 | 346 | auto Index = getDepthAndIndex(D).second; |
1802 | 346 | SearchDefinitions = Index >= TPL->size() || TPL->getParam(Index) != D; |
1803 | 346 | } |
1804 | 348 | } |
1805 | 348 | if (SearchDefinitions) |
1806 | 2 | AcceptableWithinParent = |
1807 | 2 | SemaRef.hasAcceptableDefinition(cast<NamedDecl>(DC), Kind); |
1808 | 346 | else |
1809 | 346 | AcceptableWithinParent = |
1810 | 346 | isAcceptable(SemaRef, cast<NamedDecl>(DC), Kind); |
1811 | 9.29k | } else if (isa<ParmVarDecl>(D) || |
1812 | 9.29k | (isa<FunctionDecl>(DC) && !SemaRef.getLangOpts().CPlusPlus1 )) |
1813 | 1 | AcceptableWithinParent = isAcceptable(SemaRef, cast<NamedDecl>(DC), Kind); |
1814 | 9.29k | else if (D->isModulePrivate()) { |
1815 | | // A module-private declaration is only acceptable if an enclosing lexical |
1816 | | // parent was merged with another definition in the current module. |
1817 | 2 | AcceptableWithinParent = false; |
1818 | 2 | do { |
1819 | 2 | if (SemaRef.hasMergedDefinitionInCurrentModule(cast<NamedDecl>(DC))) { |
1820 | 0 | AcceptableWithinParent = true; |
1821 | 0 | break; |
1822 | 0 | } |
1823 | 2 | DC = DC->getLexicalParent(); |
1824 | 2 | } while (!IsEffectivelyFileContext(DC)); |
1825 | 9.29k | } else { |
1826 | 9.29k | AcceptableWithinParent = |
1827 | 9.29k | SemaRef.hasAcceptableDefinition(cast<NamedDecl>(DC), Kind); |
1828 | 9.29k | } |
1829 | | |
1830 | 9.64k | if (AcceptableWithinParent && SemaRef.CodeSynthesisContexts.empty()233 && |
1831 | 9.64k | Kind == Sema::AcceptableKind::Visible228 && |
1832 | | // FIXME: Do something better in this case. |
1833 | 9.64k | !SemaRef.getLangOpts().ModulesLocalVisibility202 ) { |
1834 | | // Cache the fact that this declaration is implicitly visible because |
1835 | | // its parent has a visible definition. |
1836 | 75 | D->setVisibleDespiteOwningModule(); |
1837 | 75 | } |
1838 | 9.64k | return AcceptableWithinParent; |
1839 | 9.64k | } |
1840 | | |
1841 | 535k | if (Kind == Sema::AcceptableKind::Visible) |
1842 | 530k | return false; |
1843 | | |
1844 | 4.42k | assert(Kind == Sema::AcceptableKind::Reachable && |
1845 | 4.42k | "Additional Sema::AcceptableKind?"); |
1846 | 0 | return isReachableSlow(SemaRef, D); |
1847 | 535k | } |
1848 | | |
1849 | 1.48M | bool Sema::isModuleVisible(const Module *M, bool ModulePrivate) { |
1850 | | // [module.global.frag]p2: |
1851 | | // A global-module-fragment specifies the contents of the global module |
1852 | | // fragment for a module unit. The global module fragment can be used to |
1853 | | // provide declarations that are attached to the global module and usable |
1854 | | // within the module unit. |
1855 | | // |
1856 | | // Global module fragment is special. Global Module fragment is only usable |
1857 | | // within the module unit it got defined [module.global.frag]p2. So here we |
1858 | | // check if the Module is the global module fragment in current translation |
1859 | | // unit. |
1860 | 1.48M | if (M->isGlobalModule() && M != this->GlobalModuleFragment1.63k ) |
1861 | 565 | return false; |
1862 | | |
1863 | | // The module might be ordinarily visible. For a module-private query, that |
1864 | | // means it is part of the current module. |
1865 | 1.48M | if (ModulePrivate && isUsableModule(M)3.38k ) |
1866 | 2.46k | return true; |
1867 | | |
1868 | | // For a query which is not module-private, that means it is in our visible |
1869 | | // module set. |
1870 | 1.48M | if (!ModulePrivate && VisibleModules.isVisible(M)1.48M ) |
1871 | 929k | return true; |
1872 | | |
1873 | | // Otherwise, it might be visible by virtue of the query being within a |
1874 | | // template instantiation or similar that is permitted to look inside M. |
1875 | | |
1876 | | // Find the extra places where we need to look. |
1877 | 551k | const auto &LookupModules = getLookupModules(); |
1878 | 551k | if (LookupModules.empty()) |
1879 | 497k | return false; |
1880 | | |
1881 | | // If our lookup set contains the module, it's visible. |
1882 | 54.4k | if (LookupModules.count(M)) |
1883 | 1.80k | return true; |
1884 | | |
1885 | | // For a module-private query, that's everywhere we get to look. |
1886 | 52.6k | if (ModulePrivate) |
1887 | 0 | return false; |
1888 | | |
1889 | | // Check whether M is transitively exported to an import of the lookup set. |
1890 | 74.5k | return llvm::any_of(LookupModules, [&](const Module *LookupM) 52.6k { |
1891 | 74.5k | return LookupM->isModuleVisible(M); |
1892 | 74.5k | }); |
1893 | 52.6k | } |
1894 | | |
1895 | | // FIXME: Return false directly if we don't have an interface dependency on the |
1896 | | // translation unit containing D. |
1897 | 4.42k | bool LookupResult::isReachableSlow(Sema &SemaRef, NamedDecl *D) { |
1898 | 4.42k | assert(!isVisible(SemaRef, D) && "Shouldn't call the slow case.\n"); |
1899 | | |
1900 | 0 | Module *DeclModule = SemaRef.getOwningModule(D); |
1901 | 4.42k | assert(DeclModule && "hidden decl has no owning module"); |
1902 | | |
1903 | | // Entities in module map modules are reachable only if they're visible. |
1904 | 4.42k | if (DeclModule->isModuleMapModule()) |
1905 | 4.19k | return false; |
1906 | | |
1907 | | // If D comes from a module and SemaRef doesn't own a module, it implies D |
1908 | | // comes from another TU. In case SemaRef owns a module, we could judge if D |
1909 | | // comes from another TU by comparing the module unit. |
1910 | | // |
1911 | | // FIXME: It would look better if we have direct method to judge whether D is |
1912 | | // in another TU. |
1913 | 229 | if (SemaRef.getCurrentModule() && |
1914 | 229 | SemaRef.getCurrentModule()->getTopLevelModule() == |
1915 | 19 | DeclModule->getTopLevelModule()) |
1916 | 0 | return true; |
1917 | | |
1918 | | // [module.reach]/p3: |
1919 | | // A declaration D is reachable from a point P if: |
1920 | | // ... |
1921 | | // - D is not discarded ([module.global.frag]), appears in a translation unit |
1922 | | // that is reachable from P, and does not appear within a private module |
1923 | | // fragment. |
1924 | | // |
1925 | | // A declaration that's discarded in the GMF should be module-private. |
1926 | 229 | if (D->isModulePrivate()) |
1927 | 12 | return false; |
1928 | | |
1929 | | // [module.reach]/p1 |
1930 | | // A translation unit U is necessarily reachable from a point P if U is a |
1931 | | // module interface unit on which the translation unit containing P has an |
1932 | | // interface dependency, or the translation unit containing P imports U, in |
1933 | | // either case prior to P ([module.import]). |
1934 | | // |
1935 | | // [module.import]/p10 |
1936 | | // A translation unit has an interface dependency on a translation unit U if |
1937 | | // it contains a declaration (possibly a module-declaration) that imports U |
1938 | | // or if it has an interface dependency on a translation unit that has an |
1939 | | // interface dependency on U. |
1940 | | // |
1941 | | // So we could conclude the module unit U is necessarily reachable if: |
1942 | | // (1) The module unit U is module interface unit. |
1943 | | // (2) The current unit has an interface dependency on the module unit U. |
1944 | | // |
1945 | | // Here we only check for the first condition. Since we couldn't see |
1946 | | // DeclModule if it isn't (transitively) imported. |
1947 | 217 | if (DeclModule->getTopLevelModule()->isModuleInterfaceUnit()) |
1948 | 189 | return true; |
1949 | | |
1950 | | // [module.reach]/p2 |
1951 | | // Additional translation units on |
1952 | | // which the point within the program has an interface dependency may be |
1953 | | // considered reachable, but it is unspecified which are and under what |
1954 | | // circumstances. |
1955 | | // |
1956 | | // The decision here is to treat all additional tranditional units as |
1957 | | // unreachable. |
1958 | 28 | return false; |
1959 | 217 | } |
1960 | | |
1961 | 651k | bool Sema::isAcceptableSlow(const NamedDecl *D, Sema::AcceptableKind Kind) { |
1962 | 651k | return LookupResult::isAcceptable(*this, const_cast<NamedDecl *>(D), Kind); |
1963 | 651k | } |
1964 | | |
1965 | 66.2k | bool Sema::shouldLinkPossiblyHiddenDecl(LookupResult &R, const NamedDecl *New) { |
1966 | | // FIXME: If there are both visible and hidden declarations, we need to take |
1967 | | // into account whether redeclaration is possible. Example: |
1968 | | // |
1969 | | // Non-imported module: |
1970 | | // int f(T); // #1 |
1971 | | // Some TU: |
1972 | | // static int f(U); // #2, not a redeclaration of #1 |
1973 | | // int f(T); // #3, finds both, should link with #1 if T != U, but |
1974 | | // // with #2 if T == U; neither should be ambiguous. |
1975 | 66.2k | for (auto *D : R) { |
1976 | 66.2k | if (isVisible(D)) |
1977 | 66.1k | return true; |
1978 | 122 | assert(D->isExternallyDeclarable() && |
1979 | 122 | "should not have hidden, non-externally-declarable result here"); |
1980 | 122 | } |
1981 | | |
1982 | | // This function is called once "New" is essentially complete, but before a |
1983 | | // previous declaration is attached. We can't query the linkage of "New" in |
1984 | | // general, because attaching the previous declaration can change the |
1985 | | // linkage of New to match the previous declaration. |
1986 | | // |
1987 | | // However, because we've just determined that there is no *visible* prior |
1988 | | // declaration, we can compute the linkage here. There are two possibilities: |
1989 | | // |
1990 | | // * This is not a redeclaration; it's safe to compute the linkage now. |
1991 | | // |
1992 | | // * This is a redeclaration of a prior declaration that is externally |
1993 | | // redeclarable. In that case, the linkage of the declaration is not |
1994 | | // changed by attaching the prior declaration, because both are externally |
1995 | | // declarable (and thus ExternalLinkage or VisibleNoLinkage). |
1996 | | // |
1997 | | // FIXME: This is subtle and fragile. |
1998 | 121 | return New->isExternallyDeclarable(); |
1999 | 66.2k | } |
2000 | | |
2001 | | /// Retrieve the visible declaration corresponding to D, if any. |
2002 | | /// |
2003 | | /// This routine determines whether the declaration D is visible in the current |
2004 | | /// module, with the current imports. If not, it checks whether any |
2005 | | /// redeclaration of D is visible, and if so, returns that declaration. |
2006 | | /// |
2007 | | /// \returns D, or a visible previous declaration of D, whichever is more recent |
2008 | | /// and visible. If no declaration of D is visible, returns null. |
2009 | | static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D, |
2010 | 175k | unsigned IDNS) { |
2011 | 175k | assert(!LookupResult::isAvailableForLookup(SemaRef, D) && "not in slow case"); |
2012 | | |
2013 | 207k | for (auto RD : D->redecls()) { |
2014 | | // Don't bother with extra checks if we already know this one isn't visible. |
2015 | 207k | if (RD == D) |
2016 | 175k | continue; |
2017 | | |
2018 | 31.6k | auto ND = cast<NamedDecl>(RD); |
2019 | | // FIXME: This is wrong in the case where the previous declaration is not |
2020 | | // visible in the same scope as D. This needs to be done much more |
2021 | | // carefully. |
2022 | 31.6k | if (ND->isInIdentifierNamespace(IDNS) && |
2023 | 31.6k | LookupResult::isAvailableForLookup(SemaRef, ND)29.9k ) |
2024 | 5.17k | return ND; |
2025 | 31.6k | } |
2026 | | |
2027 | 170k | return nullptr; |
2028 | 175k | } |
2029 | | |
2030 | | bool Sema::hasVisibleDeclarationSlow(const NamedDecl *D, |
2031 | 41 | llvm::SmallVectorImpl<Module *> *Modules) { |
2032 | 41 | assert(!isVisible(D) && "not in slow case"); |
2033 | 0 | return hasAcceptableDeclarationImpl( |
2034 | 42 | *this, D, Modules, [](const NamedDecl *) { return true; }, |
2035 | 41 | Sema::AcceptableKind::Visible); |
2036 | 41 | } |
2037 | | |
2038 | | bool Sema::hasReachableDeclarationSlow( |
2039 | 0 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { |
2040 | 0 | assert(!isReachable(D) && "not in slow case"); |
2041 | 0 | return hasAcceptableDeclarationImpl( |
2042 | 0 | *this, D, Modules, [](const NamedDecl *) { return true; }, |
2043 | 0 | Sema::AcceptableKind::Reachable); |
2044 | 0 | } |
2045 | | |
2046 | 176k | NamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl *D) const { |
2047 | 176k | if (auto *ND = dyn_cast<NamespaceDecl>(D)) { |
2048 | | // Namespaces are a bit of a special case: we expect there to be a lot of |
2049 | | // redeclarations of some namespaces, all declarations of a namespace are |
2050 | | // essentially interchangeable, all declarations are found by name lookup |
2051 | | // if any is, and namespaces are never looked up during template |
2052 | | // instantiation. So we benefit from caching the check in this case, and |
2053 | | // it is correct to do so. |
2054 | 1.21k | auto *Key = ND->getCanonicalDecl(); |
2055 | 1.21k | if (auto *Acceptable = getSema().VisibleNamespaceCache.lookup(Key)) |
2056 | 922 | return Acceptable; |
2057 | 290 | auto *Acceptable = isVisible(getSema(), Key) |
2058 | 290 | ? Key204 |
2059 | 290 | : findAcceptableDecl(getSema(), Key, IDNS)86 ; |
2060 | 290 | if (Acceptable) |
2061 | 227 | getSema().VisibleNamespaceCache.insert(std::make_pair(Key, Acceptable)); |
2062 | 290 | return Acceptable; |
2063 | 1.21k | } |
2064 | | |
2065 | 175k | return findAcceptableDecl(getSema(), D, IDNS); |
2066 | 176k | } |
2067 | | |
2068 | 271M | bool LookupResult::isVisible(Sema &SemaRef, NamedDecl *D) { |
2069 | | // If this declaration is already visible, return it directly. |
2070 | 271M | if (D->isUnconditionallyVisible()) |
2071 | 270M | return true; |
2072 | | |
2073 | | // During template instantiation, we can refer to hidden declarations, if |
2074 | | // they were visible in any module along the path of instantiation. |
2075 | 1.13M | return isAcceptableSlow(SemaRef, D, Sema::AcceptableKind::Visible); |
2076 | 271M | } |
2077 | | |
2078 | 344k | bool LookupResult::isReachable(Sema &SemaRef, NamedDecl *D) { |
2079 | 344k | if (D->isUnconditionallyVisible()) |
2080 | 0 | return true; |
2081 | | |
2082 | 344k | return isAcceptableSlow(SemaRef, D, Sema::AcceptableKind::Reachable); |
2083 | 344k | } |
2084 | | |
2085 | 271M | bool LookupResult::isAvailableForLookup(Sema &SemaRef, NamedDecl *ND) { |
2086 | | // We should check the visibility at the callsite already. |
2087 | 271M | if (isVisible(SemaRef, ND)) |
2088 | 270M | return true; |
2089 | | |
2090 | 418k | auto *DC = ND->getDeclContext(); |
2091 | | // If ND is not visible and it is at namespace scope, it shouldn't be found |
2092 | | // by name lookup. |
2093 | 418k | if (DC->isFileContext()) |
2094 | 396k | return false; |
2095 | | |
2096 | | // [module.interface]p7 |
2097 | | // Class and enumeration member names can be found by name lookup in any |
2098 | | // context in which a definition of the type is reachable. |
2099 | | // |
2100 | | // FIXME: The current implementation didn't consider about scope. For example, |
2101 | | // ``` |
2102 | | // // m.cppm |
2103 | | // export module m; |
2104 | | // enum E1 { e1 }; |
2105 | | // // Use.cpp |
2106 | | // import m; |
2107 | | // void test() { |
2108 | | // auto a = E1::e1; // Error as expected. |
2109 | | // auto b = e1; // Should be error. namespace-scope name e1 is not visible |
2110 | | // } |
2111 | | // ``` |
2112 | | // For the above example, the current implementation would emit error for `a` |
2113 | | // correctly. However, the implementation wouldn't diagnose about `b` now. |
2114 | | // Since we only check the reachability for the parent only. |
2115 | | // See clang/test/CXX/module/module.interface/p7.cpp for example. |
2116 | 21.8k | if (auto *TD = dyn_cast<TagDecl>(DC)) |
2117 | 1.49k | return SemaRef.hasReachableDefinition(TD); |
2118 | | |
2119 | 20.3k | return false; |
2120 | 21.8k | } |
2121 | | |
2122 | | /// Perform unqualified name lookup starting from a given |
2123 | | /// scope. |
2124 | | /// |
2125 | | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
2126 | | /// used to find names within the current scope. For example, 'x' in |
2127 | | /// @code |
2128 | | /// int x; |
2129 | | /// int f() { |
2130 | | /// return x; // unqualified name look finds 'x' in the global scope |
2131 | | /// } |
2132 | | /// @endcode |
2133 | | /// |
2134 | | /// Different lookup criteria can find different names. For example, a |
2135 | | /// particular scope can have both a struct and a function of the same |
2136 | | /// name, and each can be found by certain lookup criteria. For more |
2137 | | /// information about lookup criteria, see the documentation for the |
2138 | | /// class LookupCriteria. |
2139 | | /// |
2140 | | /// @param S The scope from which unqualified name lookup will |
2141 | | /// begin. If the lookup criteria permits, name lookup may also search |
2142 | | /// in the parent scopes. |
2143 | | /// |
2144 | | /// @param [in,out] R Specifies the lookup to perform (e.g., the name to |
2145 | | /// look up and the lookup kind), and is updated with the results of lookup |
2146 | | /// including zero or more declarations and possibly additional information |
2147 | | /// used to diagnose ambiguities. |
2148 | | /// |
2149 | | /// @returns \c true if lookup succeeded and false otherwise. |
2150 | | bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation, |
2151 | 181M | bool ForceNoCPlusPlus) { |
2152 | 181M | DeclarationName Name = R.getLookupName(); |
2153 | 181M | if (!Name) return false4.34k ; |
2154 | | |
2155 | 181M | LookupNameKind NameKind = R.getLookupKind(); |
2156 | | |
2157 | 181M | if (!getLangOpts().CPlusPlus || ForceNoCPlusPlus85.0M ) { |
2158 | | // Unqualified name lookup in C/Objective-C is purely lexical, so |
2159 | | // search in the declarations attached to the name. |
2160 | 96.2M | if (NameKind == Sema::LookupRedeclarationWithLinkage) { |
2161 | | // Find the nearest non-transparent declaration scope. |
2162 | 1.24k | while (!(S->getFlags() & Scope::DeclScope) || |
2163 | 1.24k | (S->getEntity() && S->getEntity()->isTransparentContext()1.20k )) |
2164 | 0 | S = S->getParent(); |
2165 | 1.24k | } |
2166 | | |
2167 | | // When performing a scope lookup, we want to find local extern decls. |
2168 | 96.2M | FindLocalExternScope FindLocals(R); |
2169 | | |
2170 | | // Scan up the scope chain looking for a decl that matches this |
2171 | | // identifier that is in the appropriate namespace. This search |
2172 | | // should not take long, as shadowing of names is uncommon, and |
2173 | | // deep shadowing is extremely uncommon. |
2174 | 96.2M | bool LeftStartingScope = false; |
2175 | | |
2176 | 96.2M | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
2177 | 96.2M | IEnd = IdResolver.end(); |
2178 | 96.7M | I != IEnd; ++I573k ) |
2179 | 70.8M | if (NamedDecl *D = R.getAcceptableDecl(*I)) { |
2180 | 70.2M | if (NameKind == LookupRedeclarationWithLinkage) { |
2181 | | // Determine whether this (or a previous) declaration is |
2182 | | // out-of-scope. |
2183 | 139 | if (!LeftStartingScope && !S->isDeclScope(*I)131 ) |
2184 | 88 | LeftStartingScope = true; |
2185 | | |
2186 | | // If we found something outside of our starting scope that |
2187 | | // does not have linkage, skip it. |
2188 | 139 | if (LeftStartingScope && !((*I)->hasLinkage())96 ) { |
2189 | 9 | R.setShadowed(); |
2190 | 9 | continue; |
2191 | 9 | } |
2192 | 139 | } |
2193 | 70.2M | else if (NameKind == LookupObjCImplicitSelfParam && |
2194 | 70.2M | !isa<ImplicitParamDecl>(*I)1.85k ) |
2195 | 4 | continue; |
2196 | | |
2197 | 70.2M | R.addDecl(D); |
2198 | | |
2199 | | // Check whether there are any other declarations with the same name |
2200 | | // and in the same scope. |
2201 | 70.2M | if (I != IEnd) { |
2202 | | // Find the scope in which this declaration was declared (if it |
2203 | | // actually exists in a Scope). |
2204 | 116M | while (S && !S->isDeclScope(D)116M ) |
2205 | 45.9M | S = S->getParent(); |
2206 | | |
2207 | | // If the scope containing the declaration is the translation unit, |
2208 | | // then we'll need to perform our checks based on the matching |
2209 | | // DeclContexts rather than matching scopes. |
2210 | 70.2M | if (S && isNamespaceOrTranslationUnitScope(S)70.2M ) |
2211 | 64.7M | S = nullptr; |
2212 | | |
2213 | | // Compute the DeclContext, if we need it. |
2214 | 70.2M | DeclContext *DC = nullptr; |
2215 | 70.2M | if (!S) |
2216 | 64.7M | DC = (*I)->getDeclContext()->getRedeclContext(); |
2217 | | |
2218 | 70.2M | IdentifierResolver::iterator LastI = I; |
2219 | 96.2M | for (++LastI; LastI != IEnd; ++LastI26.0M ) { |
2220 | 26.0M | if (S) { |
2221 | | // Match based on scope. |
2222 | 2.33k | if (!S->isDeclScope(*LastI)) |
2223 | 2.16k | break; |
2224 | 26.0M | } else { |
2225 | | // Match based on DeclContext. |
2226 | 26.0M | DeclContext *LastDC |
2227 | 26.0M | = (*LastI)->getDeclContext()->getRedeclContext(); |
2228 | 26.0M | if (!LastDC->Equals(DC)) |
2229 | 34 | break; |
2230 | 26.0M | } |
2231 | | |
2232 | | // If the declaration is in the right namespace and visible, add it. |
2233 | 26.0M | if (NamedDecl *LastD = R.getAcceptableDecl(*LastI)) |
2234 | 25.6M | R.addDecl(LastD); |
2235 | 26.0M | } |
2236 | | |
2237 | 70.2M | R.resolveKind(); |
2238 | 70.2M | } |
2239 | | |
2240 | 70.2M | return true; |
2241 | 70.2M | } |
2242 | 96.2M | } else { |
2243 | | // Perform C++ unqualified name lookup. |
2244 | 85.0M | if (CppLookupName(R, S)) |
2245 | 64.4M | return true; |
2246 | 85.0M | } |
2247 | | |
2248 | | // If we didn't find a use of this identifier, and if the identifier |
2249 | | // corresponds to a compiler builtin, create the decl object for the builtin |
2250 | | // now, injecting it into translation unit scope, and return it. |
2251 | 46.5M | if (AllowBuiltinCreation && LookupBuiltin(R)3.90M ) |
2252 | 672k | return true; |
2253 | | |
2254 | | // If we didn't find a use of this identifier, the ExternalSource |
2255 | | // may be able to handle the situation. |
2256 | | // Note: some lookup failures are expected! |
2257 | | // See e.g. R.isForRedeclaration(). |
2258 | 45.8M | return (ExternalSource && ExternalSource->LookupUnqualified(R, S)1.90M ); |
2259 | 46.5M | } |
2260 | | |
2261 | | /// Perform qualified name lookup in the namespaces nominated by |
2262 | | /// using directives by the given context. |
2263 | | /// |
2264 | | /// C++98 [namespace.qual]p2: |
2265 | | /// Given X::m (where X is a user-declared namespace), or given \::m |
2266 | | /// (where X is the global namespace), let S be the set of all |
2267 | | /// declarations of m in X and in the transitive closure of all |
2268 | | /// namespaces nominated by using-directives in X and its used |
2269 | | /// namespaces, except that using-directives are ignored in any |
2270 | | /// namespace, including X, directly containing one or more |
2271 | | /// declarations of m. No namespace is searched more than once in |
2272 | | /// the lookup of a name. If S is the empty set, the program is |
2273 | | /// ill-formed. Otherwise, if S has exactly one member, or if the |
2274 | | /// context of the reference is a using-declaration |
2275 | | /// (namespace.udecl), S is the required set of declarations of |
2276 | | /// m. Otherwise if the use of m is not one that allows a unique |
2277 | | /// declaration to be chosen from S, the program is ill-formed. |
2278 | | /// |
2279 | | /// C++98 [namespace.qual]p5: |
2280 | | /// During the lookup of a qualified namespace member name, if the |
2281 | | /// lookup finds more than one declaration of the member, and if one |
2282 | | /// declaration introduces a class name or enumeration name and the |
2283 | | /// other declarations either introduce the same object, the same |
2284 | | /// enumerator or a set of functions, the non-type name hides the |
2285 | | /// class or enumeration name if and only if the declarations are |
2286 | | /// from the same namespace; otherwise (the declarations are from |
2287 | | /// different namespaces), the program is ill-formed. |
2288 | | static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, |
2289 | 34.8k | DeclContext *StartDC) { |
2290 | 34.8k | assert(StartDC->isFileContext() && "start context is not a file context"); |
2291 | | |
2292 | | // We have not yet looked into these namespaces, much less added |
2293 | | // their "using-children" to the queue. |
2294 | 0 | SmallVector<NamespaceDecl*, 8> Queue; |
2295 | | |
2296 | | // We have at least added all these contexts to the queue. |
2297 | 34.8k | llvm::SmallPtrSet<DeclContext*, 8> Visited; |
2298 | 34.8k | Visited.insert(StartDC); |
2299 | | |
2300 | | // We have already looked into the initial namespace; seed the queue |
2301 | | // with its using-children. |
2302 | 34.8k | for (auto *I : StartDC->using_directives()) { |
2303 | 3.58k | NamespaceDecl *ND = I->getNominatedNamespace()->getOriginalNamespace(); |
2304 | 3.58k | if (S.isVisible(I) && Visited.insert(ND).second3.56k ) |
2305 | 3.49k | Queue.push_back(ND); |
2306 | 3.58k | } |
2307 | | |
2308 | | // The easiest way to implement the restriction in [namespace.qual]p5 |
2309 | | // is to check whether any of the individual results found a tag |
2310 | | // and, if so, to declare an ambiguity if the final result is not |
2311 | | // a tag. |
2312 | 34.8k | bool FoundTag = false; |
2313 | 34.8k | bool FoundNonTag = false; |
2314 | | |
2315 | 34.8k | LookupResult LocalR(LookupResult::Temporary, R); |
2316 | | |
2317 | 34.8k | bool Found = false; |
2318 | 38.5k | while (!Queue.empty()) { |
2319 | 3.66k | NamespaceDecl *ND = Queue.pop_back_val(); |
2320 | | |
2321 | | // We go through some convolutions here to avoid copying results |
2322 | | // between LookupResults. |
2323 | 3.66k | bool UseLocal = !R.empty(); |
2324 | 3.66k | LookupResult &DirectR = UseLocal ? LocalR92 : R3.56k ; |
2325 | 3.66k | bool FoundDirect = LookupDirect(S, DirectR, ND); |
2326 | | |
2327 | 3.66k | if (FoundDirect) { |
2328 | | // First do any local hiding. |
2329 | 440 | DirectR.resolveKind(); |
2330 | | |
2331 | | // If the local result is a tag, remember that. |
2332 | 440 | if (DirectR.isSingleTagDecl()) |
2333 | 15 | FoundTag = true; |
2334 | 425 | else |
2335 | 425 | FoundNonTag = true; |
2336 | | |
2337 | | // Append the local results to the total results if necessary. |
2338 | 440 | if (UseLocal) { |
2339 | 70 | R.addAllDecls(LocalR); |
2340 | 70 | LocalR.clear(); |
2341 | 70 | } |
2342 | 440 | } |
2343 | | |
2344 | | // If we find names in this namespace, ignore its using directives. |
2345 | 3.66k | if (FoundDirect) { |
2346 | 440 | Found = true; |
2347 | 440 | continue; |
2348 | 440 | } |
2349 | | |
2350 | 3.22k | for (auto I : ND->using_directives()) { |
2351 | 232 | NamespaceDecl *Nom = I->getNominatedNamespace(); |
2352 | 232 | if (S.isVisible(I) && Visited.insert(Nom).second) |
2353 | 162 | Queue.push_back(Nom); |
2354 | 232 | } |
2355 | 3.22k | } |
2356 | | |
2357 | 34.8k | if (Found) { |
2358 | 370 | if (FoundTag && FoundNonTag14 ) |
2359 | 6 | R.setAmbiguousQualifiedTagHiding(); |
2360 | 364 | else |
2361 | 364 | R.resolveKind(); |
2362 | 370 | } |
2363 | | |
2364 | 34.8k | return Found; |
2365 | 34.8k | } |
2366 | | |
2367 | | /// Perform qualified name lookup into a given context. |
2368 | | /// |
2369 | | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
2370 | | /// names when the context of those names is explicit specified, e.g., |
2371 | | /// "std::vector" or "x->member", or as part of unqualified name lookup. |
2372 | | /// |
2373 | | /// Different lookup criteria can find different names. For example, a |
2374 | | /// particular scope can have both a struct and a function of the same |
2375 | | /// name, and each can be found by certain lookup criteria. For more |
2376 | | /// information about lookup criteria, see the documentation for the |
2377 | | /// class LookupCriteria. |
2378 | | /// |
2379 | | /// \param R captures both the lookup criteria and any lookup results found. |
2380 | | /// |
2381 | | /// \param LookupCtx The context in which qualified name lookup will |
2382 | | /// search. If the lookup criteria permits, name lookup may also search |
2383 | | /// in the parent contexts or (for C++ classes) base classes. |
2384 | | /// |
2385 | | /// \param InUnqualifiedLookup true if this is qualified name lookup that |
2386 | | /// occurs as part of unqualified name lookup. |
2387 | | /// |
2388 | | /// \returns true if lookup succeeded, false if it failed. |
2389 | | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, |
2390 | 20.9M | bool InUnqualifiedLookup) { |
2391 | 20.9M | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
2392 | | |
2393 | 20.9M | if (!R.getLookupName()) |
2394 | 31 | return false; |
2395 | | |
2396 | | // Make sure that the declaration context is complete. |
2397 | 20.9M | assert((!isa<TagDecl>(LookupCtx) || |
2398 | 20.9M | LookupCtx->isDependentContext() || |
2399 | 20.9M | cast<TagDecl>(LookupCtx)->isCompleteDefinition() || |
2400 | 20.9M | cast<TagDecl>(LookupCtx)->isBeingDefined()) && |
2401 | 20.9M | "Declaration context must already be complete!"); |
2402 | | |
2403 | 0 | struct QualifiedLookupInScope { |
2404 | 20.9M | bool oldVal; |
2405 | 20.9M | DeclContext *Context; |
2406 | | // Set flag in DeclContext informing debugger that we're looking for qualified name |
2407 | 20.9M | QualifiedLookupInScope(DeclContext *ctx) : Context(ctx) { |
2408 | 20.9M | oldVal = ctx->setUseQualifiedLookup(); |
2409 | 20.9M | } |
2410 | 20.9M | ~QualifiedLookupInScope() { |
2411 | 20.9M | Context->setUseQualifiedLookup(oldVal); |
2412 | 20.9M | } |
2413 | 20.9M | } QL(LookupCtx); |
2414 | | |
2415 | 20.9M | if (LookupDirect(*this, R, LookupCtx)) { |
2416 | 7.73M | R.resolveKind(); |
2417 | 7.73M | if (isa<CXXRecordDecl>(LookupCtx)) |
2418 | 4.52M | R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); |
2419 | 7.73M | return true; |
2420 | 7.73M | } |
2421 | | |
2422 | | // Don't descend into implied contexts for redeclarations. |
2423 | | // C++98 [namespace.qual]p6: |
2424 | | // In a declaration for a namespace member in which the |
2425 | | // declarator-id is a qualified-id, given that the qualified-id |
2426 | | // for the namespace member has the form |
2427 | | // nested-name-specifier unqualified-id |
2428 | | // the unqualified-id shall name a member of the namespace |
2429 | | // designated by the nested-name-specifier. |
2430 | | // See also [class.mfct]p5 and [class.static.data]p2. |
2431 | 13.1M | if (R.isForRedeclaration()) |
2432 | 5.92M | return false; |
2433 | | |
2434 | | // If this is a namespace, look it up in the implied namespaces. |
2435 | 7.27M | if (LookupCtx->isFileContext()) |
2436 | 34.8k | return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); |
2437 | | |
2438 | | // If this isn't a C++ class, we aren't allowed to look into base |
2439 | | // classes, we're done. |
2440 | 7.24M | CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); |
2441 | 7.24M | if (!LookupRec || !LookupRec->getDefinition()7.23M ) |
2442 | 9.44k | return false; |
2443 | | |
2444 | | // We're done for lookups that can never succeed for C++ classes. |
2445 | 7.23M | if (R.getLookupKind() == LookupOperatorName || |
2446 | 7.23M | R.getLookupKind() == LookupNamespaceName5.96M || |
2447 | 7.23M | R.getLookupKind() == LookupObjCProtocolName5.96M || |
2448 | 7.23M | R.getLookupKind() == LookupLabel5.96M ) |
2449 | 1.26M | return false; |
2450 | | |
2451 | | // If we're performing qualified name lookup into a dependent class, |
2452 | | // then we are actually looking into a current instantiation. If we have any |
2453 | | // dependent base classes, then we either have to delay lookup until |
2454 | | // template instantiation time (at which point all bases will be available) |
2455 | | // or we have to fail. |
2456 | 5.96M | if (!InUnqualifiedLookup && LookupRec->isDependentContext()971k && |
2457 | 5.96M | LookupRec->hasAnyDependentBases()473 ) { |
2458 | 185 | R.setNotFoundInCurrentInstantiation(); |
2459 | 185 | return false; |
2460 | 185 | } |
2461 | | |
2462 | | // Perform lookup into our base classes. |
2463 | | |
2464 | 5.96M | DeclarationName Name = R.getLookupName(); |
2465 | 5.96M | unsigned IDNS = R.getIdentifierNamespace(); |
2466 | | |
2467 | | // Look for this member in our base classes. |
2468 | 5.96M | auto BaseCallback = [Name, IDNS](const CXXBaseSpecifier *Specifier, |
2469 | 5.96M | CXXBasePath &Path) -> bool { |
2470 | 1.20M | CXXRecordDecl *BaseRecord = Specifier->getType()->getAsCXXRecordDecl(); |
2471 | | // Drop leading non-matching lookup results from the declaration list so |
2472 | | // we don't need to consider them again below. |
2473 | 1.20M | for (Path.Decls = BaseRecord->lookup(Name).begin(); |
2474 | 1.20M | Path.Decls != Path.Decls.end(); ++Path.Decls175 ) { |
2475 | 710k | if ((*Path.Decls)->isInIdentifierNamespace(IDNS)) |
2476 | 710k | return true; |
2477 | 710k | } |
2478 | 491k | return false; |
2479 | 1.20M | }; |
2480 | | |
2481 | 5.96M | CXXBasePaths Paths; |
2482 | 5.96M | Paths.setOrigin(LookupRec); |
2483 | 5.96M | if (!LookupRec->lookupInBases(BaseCallback, Paths)) |
2484 | 5.25M | return false; |
2485 | | |
2486 | 710k | R.setNamingClass(LookupRec); |
2487 | | |
2488 | | // C++ [class.member.lookup]p2: |
2489 | | // [...] If the resulting set of declarations are not all from |
2490 | | // sub-objects of the same type, or the set has a nonstatic member |
2491 | | // and includes members from distinct sub-objects, there is an |
2492 | | // ambiguity and the program is ill-formed. Otherwise that set is |
2493 | | // the result of the lookup. |
2494 | 710k | QualType SubobjectType; |
2495 | 710k | int SubobjectNumber = 0; |
2496 | 710k | AccessSpecifier SubobjectAccess = AS_none; |
2497 | | |
2498 | | // Check whether the given lookup result contains only static members. |
2499 | 710k | auto HasOnlyStaticMembers = [&](DeclContext::lookup_iterator Result) { |
2500 | 403 | for (DeclContext::lookup_iterator I = Result, E = I.end(); I != E; ++I177 ) |
2501 | 243 | if ((*I)->isInIdentifierNamespace(IDNS) && (*I)->isCXXInstanceMember()241 ) |
2502 | 66 | return false; |
2503 | 160 | return true; |
2504 | 226 | }; |
2505 | | |
2506 | 710k | bool TemplateNameLookup = R.isTemplateNameLookup(); |
2507 | | |
2508 | | // Determine whether two sets of members contain the same members, as |
2509 | | // required by C++ [class.member.lookup]p6. |
2510 | 710k | auto HasSameDeclarations = [&](DeclContext::lookup_iterator A, |
2511 | 710k | DeclContext::lookup_iterator B) { |
2512 | 102 | using Iterator = DeclContextLookupResult::iterator; |
2513 | 102 | using Result = const void *; |
2514 | | |
2515 | 361 | auto Next = [&](Iterator &It, Iterator End) -> Result { |
2516 | 365 | while (It != End) { |
2517 | 217 | NamedDecl *ND = *It++; |
2518 | 217 | if (!ND->isInIdentifierNamespace(IDNS)) |
2519 | 4 | continue; |
2520 | | |
2521 | | // C++ [temp.local]p3: |
2522 | | // A lookup that finds an injected-class-name (10.2) can result in |
2523 | | // an ambiguity in certain cases (for example, if it is found in |
2524 | | // more than one base class). If all of the injected-class-names |
2525 | | // that are found refer to specializations of the same class |
2526 | | // template, and if the name is used as a template-name, the |
2527 | | // reference refers to the class template itself and not a |
2528 | | // specialization thereof, and is not ambiguous. |
2529 | 213 | if (TemplateNameLookup) |
2530 | 56 | if (auto *TD = getAsTemplateNameDecl(ND)) |
2531 | 50 | ND = TD; |
2532 | | |
2533 | | // C++ [class.member.lookup]p3: |
2534 | | // type declarations (including injected-class-names) are replaced by |
2535 | | // the types they designate |
2536 | 213 | if (const TypeDecl *TD = dyn_cast<TypeDecl>(ND->getUnderlyingDecl())) { |
2537 | 100 | QualType T = Context.getTypeDeclType(TD); |
2538 | 100 | return T.getCanonicalType().getAsOpaquePtr(); |
2539 | 100 | } |
2540 | | |
2541 | 113 | return ND->getUnderlyingDecl()->getCanonicalDecl(); |
2542 | 213 | } |
2543 | 148 | return nullptr; |
2544 | 361 | }; |
2545 | | |
2546 | | // We'll often find the declarations are in the same order. Handle this |
2547 | | // case (and the special case of only one declaration) efficiently. |
2548 | 102 | Iterator AIt = A, BIt = B, AEnd, BEnd; |
2549 | 151 | while (true) { |
2550 | 151 | Result AResult = Next(AIt, AEnd); |
2551 | 151 | Result BResult = Next(BIt, BEnd); |
2552 | 151 | if (!AResult && !BResult49 ) |
2553 | 45 | return true; |
2554 | 106 | if (!AResult || !BResult102 ) |
2555 | 4 | return false; |
2556 | 102 | if (AResult != BResult) { |
2557 | | // Found a mismatch; carefully check both lists, accounting for the |
2558 | | // possibility of declarations appearing more than once. |
2559 | 53 | llvm::SmallDenseMap<Result, bool, 32> AResults; |
2560 | 108 | for (; AResult; AResult = Next(AIt, AEnd)55 ) |
2561 | 55 | AResults.insert({AResult, /*FoundInB*/false}); |
2562 | 53 | unsigned Found = 0; |
2563 | 57 | for (; BResult; BResult = Next(BIt, BEnd)4 ) { |
2564 | 56 | auto It = AResults.find(BResult); |
2565 | 56 | if (It == AResults.end()) |
2566 | 52 | return false; |
2567 | 4 | if (!It->second) { |
2568 | 4 | It->second = true; |
2569 | 4 | ++Found; |
2570 | 4 | } |
2571 | 4 | } |
2572 | 1 | return AResults.size() == Found; |
2573 | 53 | } |
2574 | 102 | } |
2575 | 102 | }; |
2576 | | |
2577 | 710k | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
2578 | 1.42M | Path != PathEnd; ++Path710k ) { |
2579 | 710k | const CXXBasePathElement &PathElement = Path->back(); |
2580 | | |
2581 | | // Pick the best (i.e. most permissive i.e. numerically lowest) access |
2582 | | // across all paths. |
2583 | 710k | SubobjectAccess = std::min(SubobjectAccess, Path->Access); |
2584 | | |
2585 | | // Determine whether we're looking at a distinct sub-object or not. |
2586 | 710k | if (SubobjectType.isNull()) { |
2587 | | // This is the first subobject we've looked at. Record its type. |
2588 | 710k | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
2589 | 710k | SubobjectNumber = PathElement.SubobjectNumber; |
2590 | 710k | continue; |
2591 | 710k | } |
2592 | | |
2593 | 326 | if (SubobjectType != |
2594 | 326 | Context.getCanonicalType(PathElement.Base->getType())) { |
2595 | | // We found members of the given name in two subobjects of |
2596 | | // different types. If the declaration sets aren't the same, this |
2597 | | // lookup is ambiguous. |
2598 | | // |
2599 | | // FIXME: The language rule says that this applies irrespective of |
2600 | | // whether the sets contain only static members. |
2601 | 123 | if (HasOnlyStaticMembers(Path->Decls) && |
2602 | 123 | HasSameDeclarations(Paths.begin()->Decls, Path->Decls)102 ) |
2603 | 46 | continue; |
2604 | | |
2605 | 77 | R.setAmbiguousBaseSubobjectTypes(Paths); |
2606 | 77 | return true; |
2607 | 123 | } |
2608 | | |
2609 | | // FIXME: This language rule no longer exists. Checking for ambiguous base |
2610 | | // subobjects should be done as part of formation of a class member access |
2611 | | // expression (when converting the object parameter to the member's type). |
2612 | 203 | if (SubobjectNumber != PathElement.SubobjectNumber) { |
2613 | | // We have a different subobject of the same type. |
2614 | | |
2615 | | // C++ [class.member.lookup]p5: |
2616 | | // A static member, a nested type or an enumerator defined in |
2617 | | // a base class T can unambiguously be found even if an object |
2618 | | // has more than one base class subobject of type T. |
2619 | 103 | if (HasOnlyStaticMembers(Path->Decls)) |
2620 | 58 | continue; |
2621 | | |
2622 | | // We have found a nonstatic member name in multiple, distinct |
2623 | | // subobjects. Name lookup is ambiguous. |
2624 | 45 | R.setAmbiguousBaseSubobjects(Paths); |
2625 | 45 | return true; |
2626 | 103 | } |
2627 | 203 | } |
2628 | | |
2629 | | // Lookup in a base class succeeded; return these results. |
2630 | | |
2631 | 709k | for (DeclContext::lookup_iterator I = Paths.front().Decls, E = I.end(); |
2632 | 1.42M | I != E; ++I712k ) { |
2633 | 712k | AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, |
2634 | 712k | (*I)->getAccess()); |
2635 | 712k | if (NamedDecl *ND = R.getAcceptableDecl(*I)) |
2636 | 712k | R.addDecl(ND, AS); |
2637 | 712k | } |
2638 | 709k | R.resolveKind(); |
2639 | 709k | return true; |
2640 | 710k | } |
2641 | | |
2642 | | /// Performs qualified name lookup or special type of lookup for |
2643 | | /// "__super::" scope specifier. |
2644 | | /// |
2645 | | /// This routine is a convenience overload meant to be called from contexts |
2646 | | /// that need to perform a qualified name lookup with an optional C++ scope |
2647 | | /// specifier that might require special kind of lookup. |
2648 | | /// |
2649 | | /// \param R captures both the lookup criteria and any lookup results found. |
2650 | | /// |
2651 | | /// \param LookupCtx The context in which qualified name lookup will |
2652 | | /// search. |
2653 | | /// |
2654 | | /// \param SS An optional C++ scope-specifier. |
2655 | | /// |
2656 | | /// \returns true if lookup succeeded, false if it failed. |
2657 | | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, |
2658 | 1.67M | CXXScopeSpec &SS) { |
2659 | 1.67M | auto *NNS = SS.getScopeRep(); |
2660 | 1.67M | if (NNS && NNS->getKind() == NestedNameSpecifier::Super1.13M ) |
2661 | 18 | return LookupInSuper(R, NNS->getAsRecordDecl()); |
2662 | 1.67M | else |
2663 | | |
2664 | 1.67M | return LookupQualifiedName(R, LookupCtx); |
2665 | 1.67M | } |
2666 | | |
2667 | | /// Performs name lookup for a name that was parsed in the |
2668 | | /// source code, and may contain a C++ scope specifier. |
2669 | | /// |
2670 | | /// This routine is a convenience routine meant to be called from |
2671 | | /// contexts that receive a name and an optional C++ scope specifier |
2672 | | /// (e.g., "N::M::x"). It will then perform either qualified or |
2673 | | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
2674 | | /// respectively) on the given name and return those results. It will |
2675 | | /// perform a special type of lookup for "__super::" scope specifier. |
2676 | | /// |
2677 | | /// @param S The scope from which unqualified name lookup will |
2678 | | /// begin. |
2679 | | /// |
2680 | | /// @param SS An optional C++ scope-specifier, e.g., "::N::M". |
2681 | | /// |
2682 | | /// @param EnteringContext Indicates whether we are going to enter the |
2683 | | /// context of the scope-specifier SS (if present). |
2684 | | /// |
2685 | | /// @returns True if any decls were found (but possibly ambiguous) |
2686 | | bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, |
2687 | 32.4M | bool AllowBuiltinCreation, bool EnteringContext) { |
2688 | 32.4M | if (SS && SS->isInvalid()32.4M ) { |
2689 | | // When the scope specifier is invalid, don't even look for |
2690 | | // anything. |
2691 | 3 | return false; |
2692 | 3 | } |
2693 | | |
2694 | 32.4M | if (SS && SS->isSet()32.4M ) { |
2695 | 714k | NestedNameSpecifier *NNS = SS->getScopeRep(); |
2696 | 714k | if (NNS->getKind() == NestedNameSpecifier::Super) |
2697 | 13 | return LookupInSuper(R, NNS->getAsRecordDecl()); |
2698 | | |
2699 | 714k | if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { |
2700 | | // We have resolved the scope specifier to a particular declaration |
2701 | | // contex, and will perform name lookup in that context. |
2702 | 709k | if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)708k ) |
2703 | 11 | return false; |
2704 | | |
2705 | 709k | R.setContextRange(SS->getRange()); |
2706 | 709k | return LookupQualifiedName(R, DC); |
2707 | 709k | } |
2708 | | |
2709 | | // We could not resolve the scope specified to a specific declaration |
2710 | | // context, which means that SS refers to an unknown specialization. |
2711 | | // Name lookup can't find anything in this case. |
2712 | 5.43k | R.setNotFoundInCurrentInstantiation(); |
2713 | 5.43k | R.setContextRange(SS->getRange()); |
2714 | 5.43k | return false; |
2715 | 714k | } |
2716 | | |
2717 | | // Perform unqualified name lookup starting in the given scope. |
2718 | 31.7M | return LookupName(R, S, AllowBuiltinCreation); |
2719 | 32.4M | } |
2720 | | |
2721 | | /// Perform qualified name lookup into all base classes of the given |
2722 | | /// class. |
2723 | | /// |
2724 | | /// \param R captures both the lookup criteria and any lookup results found. |
2725 | | /// |
2726 | | /// \param Class The context in which qualified name lookup will |
2727 | | /// search. Name lookup will search in all base classes merging the results. |
2728 | | /// |
2729 | | /// @returns True if any decls were found (but possibly ambiguous) |
2730 | 31 | bool Sema::LookupInSuper(LookupResult &R, CXXRecordDecl *Class) { |
2731 | | // The access-control rules we use here are essentially the rules for |
2732 | | // doing a lookup in Class that just magically skipped the direct |
2733 | | // members of Class itself. That is, the naming class is Class, and the |
2734 | | // access includes the access of the base. |
2735 | 32 | for (const auto &BaseSpec : Class->bases()) { |
2736 | 32 | CXXRecordDecl *RD = cast<CXXRecordDecl>( |
2737 | 32 | BaseSpec.getType()->castAs<RecordType>()->getDecl()); |
2738 | 32 | LookupResult Result(*this, R.getLookupNameInfo(), R.getLookupKind()); |
2739 | 32 | Result.setBaseObjectType(Context.getRecordType(Class)); |
2740 | 32 | LookupQualifiedName(Result, RD); |
2741 | | |
2742 | | // Copy the lookup results into the target, merging the base's access into |
2743 | | // the path access. |
2744 | 66 | for (auto I = Result.begin(), E = Result.end(); I != E; ++I34 ) { |
2745 | 34 | R.addDecl(I.getDecl(), |
2746 | 34 | CXXRecordDecl::MergeAccess(BaseSpec.getAccessSpecifier(), |
2747 | 34 | I.getAccess())); |
2748 | 34 | } |
2749 | | |
2750 | 32 | Result.suppressDiagnostics(); |
2751 | 32 | } |
2752 | | |
2753 | 31 | R.resolveKind(); |
2754 | 31 | R.setNamingClass(Class); |
2755 | | |
2756 | 31 | return !R.empty(); |
2757 | 31 | } |
2758 | | |
2759 | | /// Produce a diagnostic describing the ambiguity that resulted |
2760 | | /// from name lookup. |
2761 | | /// |
2762 | | /// \param Result The result of the ambiguous lookup to be diagnosed. |
2763 | 197 | void Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { |
2764 | 197 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
2765 | | |
2766 | 0 | DeclarationName Name = Result.getLookupName(); |
2767 | 197 | SourceLocation NameLoc = Result.getNameLoc(); |
2768 | 197 | SourceRange LookupRange = Result.getContextRange(); |
2769 | | |
2770 | 197 | switch (Result.getAmbiguityKind()) { |
2771 | 20 | case LookupResult::AmbiguousBaseSubobjects: { |
2772 | 20 | CXXBasePaths *Paths = Result.getBasePaths(); |
2773 | 20 | QualType SubobjectType = Paths->front().back().Base->getType(); |
2774 | 20 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
2775 | 20 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
2776 | 20 | << LookupRange; |
2777 | | |
2778 | 20 | DeclContext::lookup_iterator Found = Paths->front().Decls; |
2779 | 22 | while (isa<CXXMethodDecl>(*Found) && |
2780 | 22 | cast<CXXMethodDecl>(*Found)->isStatic()7 ) |
2781 | 2 | ++Found; |
2782 | | |
2783 | 20 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
2784 | 20 | break; |
2785 | 0 | } |
2786 | | |
2787 | 70 | case LookupResult::AmbiguousBaseSubobjectTypes: { |
2788 | 70 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
2789 | 70 | << Name << LookupRange; |
2790 | | |
2791 | 70 | CXXBasePaths *Paths = Result.getBasePaths(); |
2792 | 70 | std::set<const NamedDecl *> DeclsPrinted; |
2793 | 70 | for (CXXBasePaths::paths_iterator Path = Paths->begin(), |
2794 | 70 | PathEnd = Paths->end(); |
2795 | 212 | Path != PathEnd; ++Path142 ) { |
2796 | 142 | const NamedDecl *D = *Path->Decls; |
2797 | 142 | if (!D->isInIdentifierNamespace(Result.getIdentifierNamespace())) |
2798 | 0 | continue; |
2799 | 142 | if (DeclsPrinted.insert(D).second) { |
2800 | 142 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D->getUnderlyingDecl())) |
2801 | 22 | Diag(D->getLocation(), diag::note_ambiguous_member_type_found) |
2802 | 22 | << TD->getUnderlyingType(); |
2803 | 120 | else if (const auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl())) |
2804 | 40 | Diag(D->getLocation(), diag::note_ambiguous_member_type_found) |
2805 | 40 | << Context.getTypeDeclType(TD); |
2806 | 80 | else |
2807 | 80 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
2808 | 142 | } |
2809 | 142 | } |
2810 | 70 | break; |
2811 | 0 | } |
2812 | | |
2813 | 1 | case LookupResult::AmbiguousTagHiding: { |
2814 | 1 | Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; |
2815 | | |
2816 | 1 | llvm::SmallPtrSet<NamedDecl*, 8> TagDecls; |
2817 | | |
2818 | 1 | for (auto *D : Result) |
2819 | 2 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
2820 | 1 | TagDecls.insert(TD); |
2821 | 1 | Diag(TD->getLocation(), diag::note_hidden_tag); |
2822 | 1 | } |
2823 | | |
2824 | 1 | for (auto *D : Result) |
2825 | 2 | if (!isa<TagDecl>(D)) |
2826 | 1 | Diag(D->getLocation(), diag::note_hiding_object); |
2827 | | |
2828 | | // For recovery purposes, go ahead and implement the hiding. |
2829 | 1 | LookupResult::Filter F = Result.makeFilter(); |
2830 | 3 | while (F.hasNext()) { |
2831 | 2 | if (TagDecls.count(F.next())) |
2832 | 1 | F.erase(); |
2833 | 2 | } |
2834 | 1 | F.done(); |
2835 | 1 | break; |
2836 | 0 | } |
2837 | | |
2838 | 106 | case LookupResult::AmbiguousReference: { |
2839 | 106 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
2840 | | |
2841 | 106 | for (auto *D : Result) |
2842 | 216 | Diag(D->getLocation(), diag::note_ambiguous_candidate) << D; |
2843 | 106 | break; |
2844 | 0 | } |
2845 | 197 | } |
2846 | 197 | } |
2847 | | |
2848 | | namespace { |
2849 | | struct AssociatedLookup { |
2850 | | AssociatedLookup(Sema &S, SourceLocation InstantiationLoc, |
2851 | | Sema::AssociatedNamespaceSet &Namespaces, |
2852 | | Sema::AssociatedClassSet &Classes) |
2853 | | : S(S), Namespaces(Namespaces), Classes(Classes), |
2854 | 797k | InstantiationLoc(InstantiationLoc) { |
2855 | 797k | } |
2856 | | |
2857 | 355k | bool addClassTransitive(CXXRecordDecl *RD) { |
2858 | 355k | Classes.insert(RD); |
2859 | 355k | return ClassesTransitive.insert(RD); |
2860 | 355k | } |
2861 | | |
2862 | | Sema &S; |
2863 | | Sema::AssociatedNamespaceSet &Namespaces; |
2864 | | Sema::AssociatedClassSet &Classes; |
2865 | | SourceLocation InstantiationLoc; |
2866 | | |
2867 | | private: |
2868 | | Sema::AssociatedClassSet ClassesTransitive; |
2869 | | }; |
2870 | | } // end anonymous namespace |
2871 | | |
2872 | | static void |
2873 | | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T); |
2874 | | |
2875 | | // Given the declaration context \param Ctx of a class, class template or |
2876 | | // enumeration, add the associated namespaces to \param Namespaces as described |
2877 | | // in [basic.lookup.argdep]p2. |
2878 | | static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, |
2879 | 543k | DeclContext *Ctx) { |
2880 | | // The exact wording has been changed in C++14 as a result of |
2881 | | // CWG 1691 (see also CWG 1690 and CWG 1692). We apply it unconditionally |
2882 | | // to all language versions since it is possible to return a local type |
2883 | | // from a lambda in C++11. |
2884 | | // |
2885 | | // C++14 [basic.lookup.argdep]p2: |
2886 | | // If T is a class type [...]. Its associated namespaces are the innermost |
2887 | | // enclosing namespaces of its associated classes. [...] |
2888 | | // |
2889 | | // If T is an enumeration type, its associated namespace is the innermost |
2890 | | // enclosing namespace of its declaration. [...] |
2891 | | |
2892 | | // We additionally skip inline namespaces. The innermost non-inline namespace |
2893 | | // contains all names of all its nested inline namespaces anyway, so we can |
2894 | | // replace the entire inline namespace tree with its root. |
2895 | 847k | while (!Ctx->isFileContext() || Ctx->isInlineNamespace()800k ) |
2896 | 303k | Ctx = Ctx->getParent(); |
2897 | | |
2898 | 543k | Namespaces.insert(Ctx->getPrimaryContext()); |
2899 | 543k | } |
2900 | | |
2901 | | // Add the associated classes and namespaces for argument-dependent |
2902 | | // lookup that involves a template argument (C++ [basic.lookup.argdep]p2). |
2903 | | static void |
2904 | | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
2905 | 200k | const TemplateArgument &Arg) { |
2906 | | // C++ [basic.lookup.argdep]p2, last bullet: |
2907 | | // -- [...] ; |
2908 | 200k | switch (Arg.getKind()) { |
2909 | 0 | case TemplateArgument::Null: |
2910 | 0 | break; |
2911 | | |
2912 | 162k | case TemplateArgument::Type: |
2913 | | // [...] the namespaces and classes associated with the types of the |
2914 | | // template arguments provided for template type parameters (excluding |
2915 | | // template template parameters) |
2916 | 162k | addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); |
2917 | 162k | break; |
2918 | | |
2919 | 36 | case TemplateArgument::Template: |
2920 | 36 | case TemplateArgument::TemplateExpansion: { |
2921 | | // [...] the namespaces in which any template template arguments are |
2922 | | // defined; and the classes in which any member templates used as |
2923 | | // template template arguments are defined. |
2924 | 36 | TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); |
2925 | 36 | if (ClassTemplateDecl *ClassTemplate |
2926 | 36 | = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { |
2927 | 36 | DeclContext *Ctx = ClassTemplate->getDeclContext(); |
2928 | 36 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
2929 | 1 | Result.Classes.insert(EnclosingClass); |
2930 | | // Add the associated namespace for this class. |
2931 | 36 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
2932 | 36 | } |
2933 | 36 | break; |
2934 | 36 | } |
2935 | | |
2936 | 88 | case TemplateArgument::Declaration: |
2937 | 36.3k | case TemplateArgument::Integral: |
2938 | 36.3k | case TemplateArgument::Expression: |
2939 | 36.3k | case TemplateArgument::NullPtr: |
2940 | | // [Note: non-type template arguments do not contribute to the set of |
2941 | | // associated namespaces. ] |
2942 | 36.3k | break; |
2943 | | |
2944 | 1.52k | case TemplateArgument::Pack: |
2945 | 1.52k | for (const auto &P : Arg.pack_elements()) |
2946 | 3.05k | addAssociatedClassesAndNamespaces(Result, P); |
2947 | 1.52k | break; |
2948 | 200k | } |
2949 | 200k | } |
2950 | | |
2951 | | // Add the associated classes and namespaces for argument-dependent lookup |
2952 | | // with an argument of class type (C++ [basic.lookup.argdep]p2). |
2953 | | static void |
2954 | | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
2955 | 327k | CXXRecordDecl *Class) { |
2956 | | |
2957 | | // Just silently ignore anything whose name is __va_list_tag. |
2958 | 327k | if (Class->getDeclName() == Result.S.VAListTagName) |
2959 | 15 | return; |
2960 | | |
2961 | | // C++ [basic.lookup.argdep]p2: |
2962 | | // [...] |
2963 | | // -- If T is a class type (including unions), its associated |
2964 | | // classes are: the class itself; the class of which it is a |
2965 | | // member, if any; and its direct and indirect base classes. |
2966 | | // Its associated namespaces are the innermost enclosing |
2967 | | // namespaces of its associated classes. |
2968 | | |
2969 | | // Add the class of which it is a member, if any. |
2970 | 327k | DeclContext *Ctx = Class->getDeclContext(); |
2971 | 327k | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
2972 | 7.74k | Result.Classes.insert(EnclosingClass); |
2973 | | |
2974 | | // Add the associated namespace for this class. |
2975 | 327k | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
2976 | | |
2977 | | // -- If T is a template-id, its associated namespaces and classes are |
2978 | | // the namespace in which the template is defined; for member |
2979 | | // templates, the member template's class; the namespaces and classes |
2980 | | // associated with the types of the template arguments provided for |
2981 | | // template type parameters (excluding template template parameters); the |
2982 | | // namespaces in which any template template arguments are defined; and |
2983 | | // the classes in which any member templates used as template template |
2984 | | // arguments are defined. [Note: non-type template arguments do not |
2985 | | // contribute to the set of associated namespaces. ] |
2986 | 327k | if (ClassTemplateSpecializationDecl *Spec |
2987 | 327k | = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { |
2988 | 113k | DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); |
2989 | 113k | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
2990 | 13 | Result.Classes.insert(EnclosingClass); |
2991 | | // Add the associated namespace for this class. |
2992 | 113k | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
2993 | | |
2994 | 113k | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
2995 | 310k | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I197k ) |
2996 | 197k | addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); |
2997 | 113k | } |
2998 | | |
2999 | | // Add the class itself. If we've already transitively visited this class, |
3000 | | // we don't need to visit base classes. |
3001 | 327k | if (!Result.addClassTransitive(Class)) |
3002 | 59.1k | return; |
3003 | | |
3004 | | // Only recurse into base classes for complete types. |
3005 | 268k | if (!Result.S.isCompleteType(Result.InstantiationLoc, |
3006 | 268k | Result.S.Context.getRecordType(Class))) |
3007 | 11.2k | return; |
3008 | | |
3009 | | // Add direct and indirect base classes along with their associated |
3010 | | // namespaces. |
3011 | 257k | SmallVector<CXXRecordDecl *, 32> Bases; |
3012 | 257k | Bases.push_back(Class); |
3013 | 517k | while (!Bases.empty()) { |
3014 | | // Pop this class off the stack. |
3015 | 260k | Class = Bases.pop_back_val(); |
3016 | | |
3017 | | // Visit the base classes. |
3018 | 260k | for (const auto &Base : Class->bases()) { |
3019 | 28.2k | const RecordType *BaseType = Base.getType()->getAs<RecordType>(); |
3020 | | // In dependent contexts, we do ADL twice, and the first time around, |
3021 | | // the base type might be a dependent TemplateSpecializationType, or a |
3022 | | // TemplateTypeParmType. If that happens, simply ignore it. |
3023 | | // FIXME: If we want to support export, we probably need to add the |
3024 | | // namespace of the template in a TemplateSpecializationType, or even |
3025 | | // the classes and namespaces of known non-dependent arguments. |
3026 | 28.2k | if (!BaseType) |
3027 | 0 | continue; |
3028 | 28.2k | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
3029 | 28.2k | if (Result.addClassTransitive(BaseDecl)) { |
3030 | | // Find the associated namespace for this base class. |
3031 | 11.7k | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
3032 | 11.7k | CollectEnclosingNamespace(Result.Namespaces, BaseCtx); |
3033 | | |
3034 | | // Make sure we visit the bases of this base class. |
3035 | 11.7k | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
3036 | 2.99k | Bases.push_back(BaseDecl); |
3037 | 11.7k | } |
3038 | 28.2k | } |
3039 | 260k | } |
3040 | 257k | } |
3041 | | |
3042 | | // Add the associated classes and namespaces for |
3043 | | // argument-dependent lookup with an argument of type T |
3044 | | // (C++ [basic.lookup.koenig]p2). |
3045 | | static void |
3046 | 1.19M | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { |
3047 | | // C++ [basic.lookup.koenig]p2: |
3048 | | // |
3049 | | // For each argument type T in the function call, there is a set |
3050 | | // of zero or more associated namespaces and a set of zero or more |
3051 | | // associated classes to be considered. The sets of namespaces and |
3052 | | // classes is determined entirely by the types of the function |
3053 | | // arguments (and the namespace of any template template |
3054 | | // argument). Typedef names and using-declarations used to specify |
3055 | | // the types do not contribute to this set. The sets of namespaces |
3056 | | // and classes are determined in the following way: |
3057 | | |
3058 | 1.19M | SmallVector<const Type *, 16> Queue; |
3059 | 1.19M | const Type *T = Ty->getCanonicalTypeInternal().getTypePtr(); |
3060 | | |
3061 | 1.46M | while (true) { |
3062 | 1.46M | switch (T->getTypeClass()) { |
3063 | | |
3064 | 0 | #define TYPE(Class, Base) |
3065 | 1.11k | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
3066 | 1.10k | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
3067 | 1.70k | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
3068 | 0 | #define ABSTRACT_TYPE(Class, Base) |
3069 | 514 | #include "clang/AST/TypeNodes.inc"0 |
3070 | | // T is canonical. We can also ignore dependent types because |
3071 | | // we don't need to do ADL at the definition point, but if we |
3072 | | // wanted to implement template export (or if we find some other |
3073 | | // use for associated classes and namespaces...) this would be |
3074 | | // wrong. |
3075 | 514 | break; |
3076 | | |
3077 | | // -- If T is a pointer to U or an array of U, its associated |
3078 | | // namespaces and classes are those associated with U. |
3079 | 155k | case Type::Pointer: |
3080 | 155k | T = cast<PointerType>(T)->getPointeeType().getTypePtr(); |
3081 | 155k | continue; |
3082 | 96.8k | case Type::ConstantArray: |
3083 | 96.9k | case Type::IncompleteArray: |
3084 | 97.8k | case Type::VariableArray: |
3085 | 97.8k | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
3086 | 97.8k | continue; |
3087 | | |
3088 | | // -- If T is a fundamental type, its associated sets of |
3089 | | // namespaces and classes are both empty. |
3090 | 674k | case Type::Builtin: |
3091 | 674k | break; |
3092 | | |
3093 | | // -- If T is a class type (including unions), its associated |
3094 | | // classes are: the class itself; the class of which it is |
3095 | | // a member, if any; and its direct and indirect base classes. |
3096 | | // Its associated namespaces are the innermost enclosing |
3097 | | // namespaces of its associated classes. |
3098 | 327k | case Type::Record: { |
3099 | 327k | CXXRecordDecl *Class = |
3100 | 327k | cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()); |
3101 | 327k | addAssociatedClassesAndNamespaces(Result, Class); |
3102 | 327k | break; |
3103 | 96.9k | } |
3104 | | |
3105 | | // -- If T is an enumeration type, its associated namespace |
3106 | | // is the innermost enclosing namespace of its declaration. |
3107 | | // If it is a class member, its associated class is the |
3108 | | // member’s class; else it has no associated class. |
3109 | 91.1k | case Type::Enum: { |
3110 | 91.1k | EnumDecl *Enum = cast<EnumType>(T)->getDecl(); |
3111 | | |
3112 | 91.1k | DeclContext *Ctx = Enum->getDeclContext(); |
3113 | 91.1k | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
3114 | 13.8k | Result.Classes.insert(EnclosingClass); |
3115 | | |
3116 | | // Add the associated namespace for this enumeration. |
3117 | 91.1k | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
3118 | | |
3119 | 91.1k | break; |
3120 | 96.9k | } |
3121 | | |
3122 | | // -- If T is a function type, its associated namespaces and |
3123 | | // classes are those associated with the function parameter |
3124 | | // types and those associated with the return type. |
3125 | 5.02k | case Type::FunctionProto: { |
3126 | 5.02k | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
3127 | 5.02k | for (const auto &Arg : Proto->param_types()) |
3128 | 4.24k | Queue.push_back(Arg.getTypePtr()); |
3129 | | // fallthrough |
3130 | 5.02k | LLVM_FALLTHROUGH; |
3131 | 5.02k | } |
3132 | 5.02k | case Type::FunctionNoProto: { |
3133 | 5.02k | const FunctionType *FnType = cast<FunctionType>(T); |
3134 | 5.02k | T = FnType->getReturnType().getTypePtr(); |
3135 | 5.02k | continue; |
3136 | 5.02k | } |
3137 | | |
3138 | | // -- If T is a pointer to a member function of a class X, its |
3139 | | // associated namespaces and classes are those associated |
3140 | | // with the function parameter types and return type, |
3141 | | // together with those associated with X. |
3142 | | // |
3143 | | // -- If T is a pointer to a data member of class X, its |
3144 | | // associated namespaces and classes are those associated |
3145 | | // with the member type together with those associated with |
3146 | | // X. |
3147 | 284 | case Type::MemberPointer: { |
3148 | 284 | const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); |
3149 | | |
3150 | | // Queue up the class type into which this points. |
3151 | 284 | Queue.push_back(MemberPtr->getClass()); |
3152 | | |
3153 | | // And directly continue with the pointee type. |
3154 | 284 | T = MemberPtr->getPointeeType().getTypePtr(); |
3155 | 284 | continue; |
3156 | 5.02k | } |
3157 | | |
3158 | | // As an extension, treat this like a normal pointer. |
3159 | 590 | case Type::BlockPointer: |
3160 | 590 | T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr(); |
3161 | 590 | continue; |
3162 | | |
3163 | | // References aren't covered by the standard, but that's such an |
3164 | | // obvious defect that we cover them anyway. |
3165 | 6.38k | case Type::LValueReference: |
3166 | 6.44k | case Type::RValueReference: |
3167 | 6.44k | T = cast<ReferenceType>(T)->getPointeeType().getTypePtr(); |
3168 | 6.44k | continue; |
3169 | | |
3170 | | // These are fundamental types. |
3171 | 92.0k | case Type::Vector: |
3172 | 92.4k | case Type::ExtVector: |
3173 | 92.5k | case Type::ConstantMatrix: |
3174 | 92.6k | case Type::Complex: |
3175 | 92.6k | case Type::BitInt: |
3176 | 92.6k | break; |
3177 | | |
3178 | | // Non-deduced auto types only get here for error cases. |
3179 | 171 | case Type::Auto: |
3180 | 171 | case Type::DeducedTemplateSpecialization: |
3181 | 171 | break; |
3182 | | |
3183 | | // If T is an Objective-C object or interface type, or a pointer to an |
3184 | | // object or interface type, the associated namespace is the global |
3185 | | // namespace. |
3186 | 0 | case Type::ObjCObject: |
3187 | 8 | case Type::ObjCInterface: |
3188 | 9.22k | case Type::ObjCObjectPointer: |
3189 | 9.22k | Result.Namespaces.insert(Result.S.Context.getTranslationUnitDecl()); |
3190 | 9.22k | break; |
3191 | | |
3192 | | // Atomic types are just wrappers; use the associations of the |
3193 | | // contained type. |
3194 | 78 | case Type::Atomic: |
3195 | 78 | T = cast<AtomicType>(T)->getValueType().getTypePtr(); |
3196 | 78 | continue; |
3197 | 0 | case Type::Pipe: |
3198 | 0 | T = cast<PipeType>(T)->getElementType().getTypePtr(); |
3199 | 0 | continue; |
3200 | 1.46M | } |
3201 | | |
3202 | 1.19M | if (Queue.empty()) |
3203 | 1.19M | break; |
3204 | 4.52k | T = Queue.pop_back_val(); |
3205 | 4.52k | } |
3206 | 1.19M | } |
3207 | | |
3208 | | /// Find the associated classes and namespaces for |
3209 | | /// argument-dependent lookup for a call with the given set of |
3210 | | /// arguments. |
3211 | | /// |
3212 | | /// This routine computes the sets of associated classes and associated |
3213 | | /// namespaces searched by argument-dependent lookup |
3214 | | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
3215 | | void Sema::FindAssociatedClassesAndNamespaces( |
3216 | | SourceLocation InstantiationLoc, ArrayRef<Expr *> Args, |
3217 | | AssociatedNamespaceSet &AssociatedNamespaces, |
3218 | 797k | AssociatedClassSet &AssociatedClasses) { |
3219 | 797k | AssociatedNamespaces.clear(); |
3220 | 797k | AssociatedClasses.clear(); |
3221 | | |
3222 | 797k | AssociatedLookup Result(*this, InstantiationLoc, |
3223 | 797k | AssociatedNamespaces, AssociatedClasses); |
3224 | | |
3225 | | // C++ [basic.lookup.koenig]p2: |
3226 | | // For each argument type T in the function call, there is a set |
3227 | | // of zero or more associated namespaces and a set of zero or more |
3228 | | // associated classes to be considered. The sets of namespaces and |
3229 | | // classes is determined entirely by the types of the function |
3230 | | // arguments (and the namespace of any template template |
3231 | | // argument). |
3232 | 1.82M | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx1.02M ) { |
3233 | 1.02M | Expr *Arg = Args[ArgIdx]; |
3234 | | |
3235 | 1.02M | if (Arg->getType() != Context.OverloadTy) { |
3236 | 1.02M | addAssociatedClassesAndNamespaces(Result, Arg->getType()); |
3237 | 1.02M | continue; |
3238 | 1.02M | } |
3239 | | |
3240 | | // [...] In addition, if the argument is the name or address of a |
3241 | | // set of overloaded functions and/or function templates, its |
3242 | | // associated classes and namespaces are the union of those |
3243 | | // associated with each of the members of the set: the namespace |
3244 | | // in which the function or function template is defined and the |
3245 | | // classes and namespaces associated with its (non-dependent) |
3246 | | // parameter types and return type. |
3247 | 724 | OverloadExpr *OE = OverloadExpr::find(Arg).Expression; |
3248 | | |
3249 | 1.16k | for (const NamedDecl *D : OE->decls()) { |
3250 | | // Look through any using declarations to find the underlying function. |
3251 | 1.16k | const FunctionDecl *FDecl = D->getUnderlyingDecl()->getAsFunction(); |
3252 | | |
3253 | | // Add the classes and namespaces associated with the parameter |
3254 | | // types and return type of this function. |
3255 | 1.16k | addAssociatedClassesAndNamespaces(Result, FDecl->getType()); |
3256 | 1.16k | } |
3257 | 724 | } |
3258 | 797k | } |
3259 | | |
3260 | | NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, |
3261 | | SourceLocation Loc, |
3262 | | LookupNameKind NameKind, |
3263 | 3.50M | RedeclarationKind Redecl) { |
3264 | 3.50M | LookupResult R(*this, Name, Loc, NameKind, Redecl); |
3265 | 3.50M | LookupName(R, S); |
3266 | 3.50M | return R.getAsSingle<NamedDecl>(); |
3267 | 3.50M | } |
3268 | | |
3269 | | /// Find the protocol with the given name, if any. |
3270 | | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, |
3271 | | SourceLocation IdLoc, |
3272 | 177k | RedeclarationKind Redecl) { |
3273 | 177k | Decl *D = LookupSingleName(TUScope, II, IdLoc, |
3274 | 177k | LookupObjCProtocolName, Redecl); |
3275 | 177k | return cast_or_null<ObjCProtocolDecl>(D); |
3276 | 177k | } |
3277 | | |
3278 | | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, |
3279 | 2.82M | UnresolvedSetImpl &Functions) { |
3280 | | // C++ [over.match.oper]p3: |
3281 | | // -- The set of non-member candidates is the result of the |
3282 | | // unqualified lookup of operator@ in the context of the |
3283 | | // expression according to the usual rules for name lookup in |
3284 | | // unqualified function calls (3.4.2) except that all member |
3285 | | // functions are ignored. |
3286 | 2.82M | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
3287 | 2.82M | LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); |
3288 | 2.82M | LookupName(Operators, S); |
3289 | | |
3290 | 2.82M | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
3291 | 0 | Functions.append(Operators.begin(), Operators.end()); |
3292 | 2.82M | } |
3293 | | |
3294 | | Sema::SpecialMemberOverloadResult Sema::LookupSpecialMember(CXXRecordDecl *RD, |
3295 | | CXXSpecialMember SM, |
3296 | | bool ConstArg, |
3297 | | bool VolatileArg, |
3298 | | bool RValueThis, |
3299 | | bool ConstThis, |
3300 | 922k | bool VolatileThis) { |
3301 | 922k | assert(CanDeclareSpecialMemberFunction(RD) && |
3302 | 922k | "doing special member lookup into record that isn't fully complete"); |
3303 | 0 | RD = RD->getDefinition(); |
3304 | 922k | if (RValueThis || ConstThis922k || VolatileThis922k ) |
3305 | 312 | assert((SM == CXXCopyAssignment || SM == CXXMoveAssignment) && |
3306 | 922k | "constructors and destructors always have unqualified lvalue this"); |
3307 | 922k | if (ConstArg || VolatileArg752k ) |
3308 | 169k | assert((SM != CXXDefaultConstructor && SM != CXXDestructor) && |
3309 | 922k | "parameter-less special members can't have qualified arguments"); |
3310 | | |
3311 | | // FIXME: Get the caller to pass in a location for the lookup. |
3312 | 0 | SourceLocation LookupLoc = RD->getLocation(); |
3313 | | |
3314 | 922k | llvm::FoldingSetNodeID ID; |
3315 | 922k | ID.AddPointer(RD); |
3316 | 922k | ID.AddInteger(SM); |
3317 | 922k | ID.AddInteger(ConstArg); |
3318 | 922k | ID.AddInteger(VolatileArg); |
3319 | 922k | ID.AddInteger(RValueThis); |
3320 | 922k | ID.AddInteger(ConstThis); |
3321 | 922k | ID.AddInteger(VolatileThis); |
3322 | | |
3323 | 922k | void *InsertPoint; |
3324 | 922k | SpecialMemberOverloadResultEntry *Result = |
3325 | 922k | SpecialMemberCache.FindNodeOrInsertPos(ID, InsertPoint); |
3326 | | |
3327 | | // This was already cached |
3328 | 922k | if (Result) |
3329 | 641k | return *Result; |
3330 | | |
3331 | 280k | Result = BumpAlloc.Allocate<SpecialMemberOverloadResultEntry>(); |
3332 | 280k | Result = new (Result) SpecialMemberOverloadResultEntry(ID); |
3333 | 280k | SpecialMemberCache.InsertNode(Result, InsertPoint); |
3334 | | |
3335 | 280k | if (SM == CXXDestructor) { |
3336 | 118k | if (RD->needsImplicitDestructor()) { |
3337 | 86.1k | runWithSufficientStackSpace(RD->getLocation(), [&] { |
3338 | 86.1k | DeclareImplicitDestructor(RD); |
3339 | 86.1k | }); |
3340 | 86.1k | } |
3341 | 118k | CXXDestructorDecl *DD = RD->getDestructor(); |
3342 | 118k | Result->setMethod(DD); |
3343 | 118k | Result->setKind(DD && !DD->isDeleted() |
3344 | 118k | ? SpecialMemberOverloadResult::Success117k |
3345 | 118k | : SpecialMemberOverloadResult::NoMemberOrDeleted233 ); |
3346 | 118k | return *Result; |
3347 | 118k | } |
3348 | | |
3349 | | // Prepare for overload resolution. Here we construct a synthetic argument |
3350 | | // if necessary and make sure that implicit functions are declared. |
3351 | 162k | CanQualType CanTy = Context.getCanonicalType(Context.getTagDeclType(RD)); |
3352 | 162k | DeclarationName Name; |
3353 | 162k | Expr *Arg = nullptr; |
3354 | 162k | unsigned NumArgs; |
3355 | | |
3356 | 162k | QualType ArgType = CanTy; |
3357 | 162k | ExprValueKind VK = VK_LValue; |
3358 | | |
3359 | 162k | if (SM == CXXDefaultConstructor) { |
3360 | 45.2k | Name = Context.DeclarationNames.getCXXConstructorName(CanTy); |
3361 | 45.2k | NumArgs = 0; |
3362 | 45.2k | if (RD->needsImplicitDefaultConstructor()) { |
3363 | 25.1k | runWithSufficientStackSpace(RD->getLocation(), [&] { |
3364 | 25.1k | DeclareImplicitDefaultConstructor(RD); |
3365 | 25.1k | }); |
3366 | 25.1k | } |
3367 | 117k | } else { |
3368 | 117k | if (SM == CXXCopyConstructor || SM == CXXMoveConstructor76.0k ) { |
3369 | 72.9k | Name = Context.DeclarationNames.getCXXConstructorName(CanTy); |
3370 | 72.9k | if (RD->needsImplicitCopyConstructor()) { |
3371 | 26.2k | runWithSufficientStackSpace(RD->getLocation(), [&] { |
3372 | 26.2k | DeclareImplicitCopyConstructor(RD); |
3373 | 26.2k | }); |
3374 | 26.2k | } |
3375 | 72.9k | if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveConstructor()72.7k ) { |
3376 | 24.9k | runWithSufficientStackSpace(RD->getLocation(), [&] { |
3377 | 24.9k | DeclareImplicitMoveConstructor(RD); |
3378 | 24.9k | }); |
3379 | 24.9k | } |
3380 | 72.9k | } else { |
3381 | 44.5k | Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
3382 | 44.5k | if (RD->needsImplicitCopyAssignment()) { |
3383 | 15.6k | runWithSufficientStackSpace(RD->getLocation(), [&] { |
3384 | 15.6k | DeclareImplicitCopyAssignment(RD); |
3385 | 15.6k | }); |
3386 | 15.6k | } |
3387 | 44.5k | if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveAssignment()44.4k ) { |
3388 | 14.6k | runWithSufficientStackSpace(RD->getLocation(), [&] { |
3389 | 14.6k | DeclareImplicitMoveAssignment(RD); |
3390 | 14.6k | }); |
3391 | 14.6k | } |
3392 | 44.5k | } |
3393 | | |
3394 | 117k | if (ConstArg) |
3395 | 67.5k | ArgType.addConst(); |
3396 | 117k | if (VolatileArg) |
3397 | 48 | ArgType.addVolatile(); |
3398 | | |
3399 | | // This isn't /really/ specified by the standard, but it's implied |
3400 | | // we should be working from a PRValue in the case of move to ensure |
3401 | | // that we prefer to bind to rvalue references, and an LValue in the |
3402 | | // case of copy to ensure we don't bind to rvalue references. |
3403 | | // Possibly an XValue is actually correct in the case of move, but |
3404 | | // there is no semantic difference for class types in this restricted |
3405 | | // case. |
3406 | 117k | if (SM == CXXCopyConstructor || SM == CXXCopyAssignment76.0k ) |
3407 | 67.9k | VK = VK_LValue; |
3408 | 49.4k | else |
3409 | 49.4k | VK = VK_PRValue; |
3410 | 117k | } |
3411 | | |
3412 | 162k | OpaqueValueExpr FakeArg(LookupLoc, ArgType, VK); |
3413 | | |
3414 | 162k | if (SM != CXXDefaultConstructor) { |
3415 | 117k | NumArgs = 1; |
3416 | 117k | Arg = &FakeArg; |
3417 | 117k | } |
3418 | | |
3419 | | // Create the object argument |
3420 | 162k | QualType ThisTy = CanTy; |
3421 | 162k | if (ConstThis) |
3422 | 119 | ThisTy.addConst(); |
3423 | 162k | if (VolatileThis) |
3424 | 22 | ThisTy.addVolatile(); |
3425 | 162k | Expr::Classification Classification = |
3426 | 162k | OpaqueValueExpr(LookupLoc, ThisTy, RValueThis ? VK_PRValue18 : VK_LValue162k ) |
3427 | 162k | .Classify(Context); |
3428 | | |
3429 | | // Now we perform lookup on the name we computed earlier and do overload |
3430 | | // resolution. Lookup is only performed directly into the class since there |
3431 | | // will always be a (possibly implicit) declaration to shadow any others. |
3432 | 162k | OverloadCandidateSet OCS(LookupLoc, OverloadCandidateSet::CSK_Normal); |
3433 | 162k | DeclContext::lookup_result R = RD->lookup(Name); |
3434 | | |
3435 | 162k | if (R.empty()) { |
3436 | | // We might have no default constructor because we have a lambda's closure |
3437 | | // type, rather than because there's some other declared constructor. |
3438 | | // Every class has a copy/move constructor, copy/move assignment, and |
3439 | | // destructor. |
3440 | 3 | assert(SM == CXXDefaultConstructor && |
3441 | 3 | "lookup for a constructor or assignment operator was empty"); |
3442 | 0 | Result->setMethod(nullptr); |
3443 | 3 | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); |
3444 | 3 | return *Result; |
3445 | 3 | } |
3446 | | |
3447 | | // Copy the candidates as our processing of them may load new declarations |
3448 | | // from an external source and invalidate lookup_result. |
3449 | 162k | SmallVector<NamedDecl *, 8> Candidates(R.begin(), R.end()); |
3450 | | |
3451 | 444k | for (NamedDecl *CandDecl : Candidates) { |
3452 | 444k | if (CandDecl->isInvalidDecl()) |
3453 | 31 | continue; |
3454 | | |
3455 | 444k | DeclAccessPair Cand = DeclAccessPair::make(CandDecl, AS_public); |
3456 | 444k | auto CtorInfo = getConstructorInfo(Cand); |
3457 | 444k | if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(Cand->getUnderlyingDecl())) { |
3458 | 375k | if (SM == CXXCopyAssignment || SM == CXXMoveAssignment329k ) |
3459 | 80.6k | AddMethodCandidate(M, Cand, RD, ThisTy, Classification, |
3460 | 80.6k | llvm::makeArrayRef(&Arg, NumArgs), OCS, true); |
3461 | 295k | else if (CtorInfo) |
3462 | 295k | AddOverloadCandidate(CtorInfo.Constructor, CtorInfo.FoundDecl, |
3463 | 295k | llvm::makeArrayRef(&Arg, NumArgs), OCS, |
3464 | 295k | /*SuppressUserConversions*/ true); |
3465 | 2 | else |
3466 | 2 | AddOverloadCandidate(M, Cand, llvm::makeArrayRef(&Arg, NumArgs), OCS, |
3467 | 2 | /*SuppressUserConversions*/ true); |
3468 | 375k | } else if (FunctionTemplateDecl *68.4k Tmpl68.4k = |
3469 | 68.4k | dyn_cast<FunctionTemplateDecl>(Cand->getUnderlyingDecl())) { |
3470 | 67.8k | if (SM == CXXCopyAssignment || SM == CXXMoveAssignment66.5k ) |
3471 | 1.88k | AddMethodTemplateCandidate( |
3472 | 1.88k | Tmpl, Cand, RD, nullptr, ThisTy, Classification, |
3473 | 1.88k | llvm::makeArrayRef(&Arg, NumArgs), OCS, true); |
3474 | 65.9k | else if (CtorInfo) |
3475 | 65.9k | AddTemplateOverloadCandidate( |
3476 | 65.9k | CtorInfo.ConstructorTmpl, CtorInfo.FoundDecl, nullptr, |
3477 | 65.9k | llvm::makeArrayRef(&Arg, NumArgs), OCS, true); |
3478 | 0 | else |
3479 | 0 | AddTemplateOverloadCandidate( |
3480 | 0 | Tmpl, Cand, nullptr, llvm::makeArrayRef(&Arg, NumArgs), OCS, true); |
3481 | 67.8k | } else { |
3482 | 613 | assert(isa<UsingDecl>(Cand.getDecl()) && |
3483 | 613 | "illegal Kind of operator = Decl"); |
3484 | 613 | } |
3485 | 444k | } |
3486 | | |
3487 | 162k | OverloadCandidateSet::iterator Best; |
3488 | 162k | switch (OCS.BestViableFunction(*this, LookupLoc, Best)) { |
3489 | 150k | case OR_Success: |
3490 | 150k | Result->setMethod(cast<CXXMethodDecl>(Best->Function)); |
3491 | 150k | Result->setKind(SpecialMemberOverloadResult::Success); |
3492 | 150k | break; |
3493 | | |
3494 | 11.3k | case OR_Deleted: |
3495 | 11.3k | Result->setMethod(cast<CXXMethodDecl>(Best->Function)); |
3496 | 11.3k | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); |
3497 | 11.3k | break; |
3498 | | |
3499 | 23 | case OR_Ambiguous: |
3500 | 23 | Result->setMethod(nullptr); |
3501 | 23 | Result->setKind(SpecialMemberOverloadResult::Ambiguous); |
3502 | 23 | break; |
3503 | | |
3504 | 1.15k | case OR_No_Viable_Function: |
3505 | 1.15k | Result->setMethod(nullptr); |
3506 | 1.15k | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); |
3507 | 1.15k | break; |
3508 | 162k | } |
3509 | | |
3510 | 162k | return *Result; |
3511 | 162k | } |
3512 | | |
3513 | | /// Look up the default constructor for the given class. |
3514 | 77.7k | CXXConstructorDecl *Sema::LookupDefaultConstructor(CXXRecordDecl *Class) { |
3515 | 77.7k | SpecialMemberOverloadResult Result = |
3516 | 77.7k | LookupSpecialMember(Class, CXXDefaultConstructor, false, false, false, |
3517 | 77.7k | false, false); |
3518 | | |
3519 | 77.7k | return cast_or_null<CXXConstructorDecl>(Result.getMethod()); |
3520 | 77.7k | } |
3521 | | |
3522 | | /// Look up the copying constructor for the given class. |
3523 | | CXXConstructorDecl *Sema::LookupCopyingConstructor(CXXRecordDecl *Class, |
3524 | 28 | unsigned Quals) { |
3525 | 28 | assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
3526 | 28 | "non-const, non-volatile qualifiers for copy ctor arg"); |
3527 | 0 | SpecialMemberOverloadResult Result = |
3528 | 28 | LookupSpecialMember(Class, CXXCopyConstructor, Quals & Qualifiers::Const, |
3529 | 28 | Quals & Qualifiers::Volatile, false, false, false); |
3530 | | |
3531 | 28 | return cast_or_null<CXXConstructorDecl>(Result.getMethod()); |
3532 | 28 | } |
3533 | | |
3534 | | /// Look up the moving constructor for the given class. |
3535 | | CXXConstructorDecl *Sema::LookupMovingConstructor(CXXRecordDecl *Class, |
3536 | 0 | unsigned Quals) { |
3537 | 0 | SpecialMemberOverloadResult Result = |
3538 | 0 | LookupSpecialMember(Class, CXXMoveConstructor, Quals & Qualifiers::Const, |
3539 | 0 | Quals & Qualifiers::Volatile, false, false, false); |
3540 | |
|
3541 | 0 | return cast_or_null<CXXConstructorDecl>(Result.getMethod()); |
3542 | 0 | } |
3543 | | |
3544 | | /// Look up the constructors for the given class. |
3545 | 593k | DeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) { |
3546 | | // If the implicit constructors have not yet been declared, do so now. |
3547 | 593k | if (CanDeclareSpecialMemberFunction(Class)) { |
3548 | 593k | runWithSufficientStackSpace(Class->getLocation(), [&] { |
3549 | 593k | if (Class->needsImplicitDefaultConstructor()) |
3550 | 34.5k | DeclareImplicitDefaultConstructor(Class); |
3551 | 593k | if (Class->needsImplicitCopyConstructor()) |
3552 | 76.1k | DeclareImplicitCopyConstructor(Class); |
3553 | 593k | if (getLangOpts().CPlusPlus11 && Class->needsImplicitMoveConstructor()577k ) |
3554 | 67.5k | DeclareImplicitMoveConstructor(Class); |
3555 | 593k | }); |
3556 | 593k | } |
3557 | | |
3558 | 593k | CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class)); |
3559 | 593k | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T); |
3560 | 593k | return Class->lookup(Name); |
3561 | 593k | } |
3562 | | |
3563 | | /// Look up the copying assignment operator for the given class. |
3564 | | CXXMethodDecl *Sema::LookupCopyingAssignment(CXXRecordDecl *Class, |
3565 | | unsigned Quals, bool RValueThis, |
3566 | 0 | unsigned ThisQuals) { |
3567 | 0 | assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
3568 | 0 | "non-const, non-volatile qualifiers for copy assignment arg"); |
3569 | 0 | assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
3570 | 0 | "non-const, non-volatile qualifiers for copy assignment this"); |
3571 | 0 | SpecialMemberOverloadResult Result = |
3572 | 0 | LookupSpecialMember(Class, CXXCopyAssignment, Quals & Qualifiers::Const, |
3573 | 0 | Quals & Qualifiers::Volatile, RValueThis, |
3574 | 0 | ThisQuals & Qualifiers::Const, |
3575 | 0 | ThisQuals & Qualifiers::Volatile); |
3576 | |
|
3577 | 0 | return Result.getMethod(); |
3578 | 0 | } |
3579 | | |
3580 | | /// Look up the moving assignment operator for the given class. |
3581 | | CXXMethodDecl *Sema::LookupMovingAssignment(CXXRecordDecl *Class, |
3582 | | unsigned Quals, |
3583 | | bool RValueThis, |
3584 | 0 | unsigned ThisQuals) { |
3585 | 0 | assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
3586 | 0 | "non-const, non-volatile qualifiers for copy assignment this"); |
3587 | 0 | SpecialMemberOverloadResult Result = |
3588 | 0 | LookupSpecialMember(Class, CXXMoveAssignment, Quals & Qualifiers::Const, |
3589 | 0 | Quals & Qualifiers::Volatile, RValueThis, |
3590 | 0 | ThisQuals & Qualifiers::Const, |
3591 | 0 | ThisQuals & Qualifiers::Volatile); |
3592 | |
|
3593 | 0 | return Result.getMethod(); |
3594 | 0 | } |
3595 | | |
3596 | | /// Look for the destructor of the given class. |
3597 | | /// |
3598 | | /// During semantic analysis, this routine should be used in lieu of |
3599 | | /// CXXRecordDecl::getDestructor(). |
3600 | | /// |
3601 | | /// \returns The destructor for this class. |
3602 | 332k | CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) { |
3603 | 332k | return cast<CXXDestructorDecl>(LookupSpecialMember(Class, CXXDestructor, |
3604 | 332k | false, false, false, |
3605 | 332k | false, false).getMethod()); |
3606 | 332k | } |
3607 | | |
3608 | | /// LookupLiteralOperator - Determine which literal operator should be used for |
3609 | | /// a user-defined literal, per C++11 [lex.ext]. |
3610 | | /// |
3611 | | /// Normal overload resolution is not used to select which literal operator to |
3612 | | /// call for a user-defined literal. Look up the provided literal operator name, |
3613 | | /// and filter the results to the appropriate set for the given argument types. |
3614 | | Sema::LiteralOperatorLookupResult |
3615 | | Sema::LookupLiteralOperator(Scope *S, LookupResult &R, |
3616 | | ArrayRef<QualType> ArgTys, bool AllowRaw, |
3617 | | bool AllowTemplate, bool AllowStringTemplatePack, |
3618 | 374 | bool DiagnoseMissing, StringLiteral *StringLit) { |
3619 | 374 | LookupName(R, S); |
3620 | 374 | assert(R.getResultKind() != LookupResult::Ambiguous && |
3621 | 374 | "literal operator lookup can't be ambiguous"); |
3622 | | |
3623 | | // Filter the lookup results appropriately. |
3624 | 0 | LookupResult::Filter F = R.makeFilter(); |
3625 | | |
3626 | 374 | bool AllowCooked = true; |
3627 | 374 | bool FoundRaw = false; |
3628 | 374 | bool FoundTemplate = false; |
3629 | 374 | bool FoundStringTemplatePack = false; |
3630 | 374 | bool FoundCooked = false; |
3631 | | |
3632 | 1.09k | while (F.hasNext()) { |
3633 | 725 | Decl *D = F.next(); |
3634 | 725 | if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) |
3635 | 12 | D = USD->getTargetDecl(); |
3636 | | |
3637 | | // If the declaration we found is invalid, skip it. |
3638 | 725 | if (D->isInvalidDecl()) { |
3639 | 2 | F.erase(); |
3640 | 2 | continue; |
3641 | 2 | } |
3642 | | |
3643 | 723 | bool IsRaw = false; |
3644 | 723 | bool IsTemplate = false; |
3645 | 723 | bool IsStringTemplatePack = false; |
3646 | 723 | bool IsCooked = false; |
3647 | | |
3648 | 723 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
3649 | 628 | if (FD->getNumParams() == 1 && |
3650 | 628 | FD->getParamDecl(0)->getType()->getAs<PointerType>()429 ) |
3651 | 78 | IsRaw = true; |
3652 | 550 | else if (FD->getNumParams() == ArgTys.size()) { |
3653 | 397 | IsCooked = true; |
3654 | 699 | for (unsigned ArgIdx = 0; ArgIdx != ArgTys.size(); ++ArgIdx302 ) { |
3655 | 467 | QualType ParamTy = FD->getParamDecl(ArgIdx)->getType(); |
3656 | 467 | if (!Context.hasSameUnqualifiedType(ArgTys[ArgIdx], ParamTy)) { |
3657 | 165 | IsCooked = false; |
3658 | 165 | break; |
3659 | 165 | } |
3660 | 467 | } |
3661 | 397 | } |
3662 | 628 | } |
3663 | 723 | if (FunctionTemplateDecl *FD = dyn_cast<FunctionTemplateDecl>(D)) { |
3664 | 95 | TemplateParameterList *Params = FD->getTemplateParameters(); |
3665 | 95 | if (Params->size() == 1) { |
3666 | 79 | IsTemplate = true; |
3667 | 79 | if (!Params->getParam(0)->isTemplateParameterPack() && !StringLit31 ) { |
3668 | | // Implied but not stated: user-defined integer and floating literals |
3669 | | // only ever use numeric literal operator templates, not templates |
3670 | | // taking a parameter of class type. |
3671 | 6 | F.erase(); |
3672 | 6 | continue; |
3673 | 6 | } |
3674 | | |
3675 | | // A string literal template is only considered if the string literal |
3676 | | // is a well-formed template argument for the template parameter. |
3677 | 73 | if (StringLit) { |
3678 | 31 | SFINAETrap Trap(*this); |
3679 | 31 | SmallVector<TemplateArgument, 1> Checked; |
3680 | 31 | TemplateArgumentLoc Arg(TemplateArgument(StringLit), StringLit); |
3681 | 31 | if (CheckTemplateArgument(Params->getParam(0), Arg, FD, |
3682 | 31 | R.getNameLoc(), R.getNameLoc(), 0, |
3683 | 31 | Checked) || |
3684 | 31 | Trap.hasErrorOccurred()14 ) |
3685 | 17 | IsTemplate = false; |
3686 | 31 | } |
3687 | 73 | } else { |
3688 | 16 | IsStringTemplatePack = true; |
3689 | 16 | } |
3690 | 95 | } |
3691 | | |
3692 | 717 | if (AllowTemplate && StringLit505 && IsTemplate264 ) { |
3693 | 14 | FoundTemplate = true; |
3694 | 14 | AllowRaw = false; |
3695 | 14 | AllowCooked = false; |
3696 | 14 | AllowStringTemplatePack = false; |
3697 | 14 | if (FoundRaw || FoundCooked || FoundStringTemplatePack9 ) { |
3698 | 5 | F.restart(); |
3699 | 5 | FoundRaw = FoundCooked = FoundStringTemplatePack = false; |
3700 | 5 | } |
3701 | 703 | } else if (AllowCooked && IsCooked698 ) { |
3702 | 227 | FoundCooked = true; |
3703 | 227 | AllowRaw = false; |
3704 | 227 | AllowTemplate = StringLit; |
3705 | 227 | AllowStringTemplatePack = false; |
3706 | 227 | if (FoundRaw || FoundTemplate220 || FoundStringTemplatePack220 ) { |
3707 | | // Go through again and remove the raw and template decls we've |
3708 | | // already found. |
3709 | 7 | F.restart(); |
3710 | 7 | FoundRaw = FoundTemplate = FoundStringTemplatePack = false; |
3711 | 7 | } |
3712 | 476 | } else if (AllowRaw && IsRaw134 ) { |
3713 | 44 | FoundRaw = true; |
3714 | 432 | } else if (AllowTemplate && IsTemplate275 ) { |
3715 | 37 | FoundTemplate = true; |
3716 | 395 | } else if (AllowStringTemplatePack && IsStringTemplatePack125 ) { |
3717 | 13 | FoundStringTemplatePack = true; |
3718 | 382 | } else { |
3719 | 382 | F.erase(); |
3720 | 382 | } |
3721 | 717 | } |
3722 | | |
3723 | 374 | F.done(); |
3724 | | |
3725 | | // Per C++20 [lex.ext]p5, we prefer the template form over the non-template |
3726 | | // form for string literal operator templates. |
3727 | 374 | if (StringLit && FoundTemplate100 ) |
3728 | 9 | return LOLR_Template; |
3729 | | |
3730 | | // C++11 [lex.ext]p3, p4: If S contains a literal operator with a matching |
3731 | | // parameter type, that is used in preference to a raw literal operator |
3732 | | // or literal operator template. |
3733 | 365 | if (FoundCooked) |
3734 | 214 | return LOLR_Cooked; |
3735 | | |
3736 | | // C++11 [lex.ext]p3, p4: S shall contain a raw literal operator or a literal |
3737 | | // operator template, but not both. |
3738 | 151 | if (FoundRaw && FoundTemplate37 ) { |
3739 | 4 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
3740 | 12 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I8 ) |
3741 | 8 | NoteOverloadCandidate(*I, (*I)->getUnderlyingDecl()->getAsFunction()); |
3742 | 4 | return LOLR_Error; |
3743 | 4 | } |
3744 | | |
3745 | 147 | if (FoundRaw) |
3746 | 33 | return LOLR_Raw; |
3747 | | |
3748 | 114 | if (FoundTemplate) |
3749 | 29 | return LOLR_Template; |
3750 | | |
3751 | 85 | if (FoundStringTemplatePack) |
3752 | 13 | return LOLR_StringTemplatePack; |
3753 | | |
3754 | | // Didn't find anything we could use. |
3755 | 72 | if (DiagnoseMissing) { |
3756 | 42 | Diag(R.getNameLoc(), diag::err_ovl_no_viable_literal_operator) |
3757 | 42 | << R.getLookupName() << (int)ArgTys.size() << ArgTys[0] |
3758 | 42 | << (ArgTys.size() == 2 ? ArgTys[1]18 : QualType()24 ) << AllowRaw |
3759 | 42 | << (AllowTemplate || AllowStringTemplatePack10 ); |
3760 | 42 | return LOLR_Error; |
3761 | 42 | } |
3762 | | |
3763 | 30 | return LOLR_ErrorNoDiagnostic; |
3764 | 72 | } |
3765 | | |
3766 | 849k | void ADLResult::insert(NamedDecl *New) { |
3767 | 849k | NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; |
3768 | | |
3769 | | // If we haven't yet seen a decl for this key, or the last decl |
3770 | | // was exactly this one, we're done. |
3771 | 849k | if (Old == nullptr || Old == New30 ) { |
3772 | 849k | Old = New; |
3773 | 849k | return; |
3774 | 849k | } |
3775 | | |
3776 | | // Otherwise, decide which is a more recent redeclaration. |
3777 | 30 | FunctionDecl *OldFD = Old->getAsFunction(); |
3778 | 30 | FunctionDecl *NewFD = New->getAsFunction(); |
3779 | | |
3780 | 30 | FunctionDecl *Cursor = NewFD; |
3781 | 32 | while (true) { |
3782 | 32 | Cursor = Cursor->getPreviousDecl(); |
3783 | | |
3784 | | // If we got to the end without finding OldFD, OldFD is the newer |
3785 | | // declaration; leave things as they are. |
3786 | 32 | if (!Cursor) return1 ; |
3787 | | |
3788 | | // If we do find OldFD, then NewFD is newer. |
3789 | 31 | if (Cursor == OldFD) break29 ; |
3790 | | |
3791 | | // Otherwise, keep looking. |
3792 | 31 | } |
3793 | | |
3794 | 29 | Old = New; |
3795 | 29 | } |
3796 | | |
3797 | | void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc, |
3798 | 714k | ArrayRef<Expr *> Args, ADLResult &Result) { |
3799 | | // Find all of the associated namespaces and classes based on the |
3800 | | // arguments we have. |
3801 | 714k | AssociatedNamespaceSet AssociatedNamespaces; |
3802 | 714k | AssociatedClassSet AssociatedClasses; |
3803 | 714k | FindAssociatedClassesAndNamespaces(Loc, Args, |
3804 | 714k | AssociatedNamespaces, |
3805 | 714k | AssociatedClasses); |
3806 | | |
3807 | | // C++ [basic.lookup.argdep]p3: |
3808 | | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
3809 | | // and let Y be the lookup set produced by argument dependent |
3810 | | // lookup (defined as follows). If X contains [...] then Y is |
3811 | | // empty. Otherwise Y is the set of declarations found in the |
3812 | | // namespaces associated with the argument types as described |
3813 | | // below. The set of declarations found by the lookup of the name |
3814 | | // is the union of X and Y. |
3815 | | // |
3816 | | // Here, we compute Y and add its members to the overloaded |
3817 | | // candidate set. |
3818 | 714k | for (auto *NS : AssociatedNamespaces) { |
3819 | | // When considering an associated namespace, the lookup is the |
3820 | | // same as the lookup performed when the associated namespace is |
3821 | | // used as a qualifier (3.4.3.2) except that: |
3822 | | // |
3823 | | // -- Any using-directives in the associated namespace are |
3824 | | // ignored. |
3825 | | // |
3826 | | // -- Any namespace-scope friend functions declared in |
3827 | | // associated classes are visible within their respective |
3828 | | // namespaces even if they are not visible during an ordinary |
3829 | | // lookup (11.4). |
3830 | 255k | DeclContext::lookup_result R = NS->lookup(Name); |
3831 | 1.00M | for (auto *D : R) { |
3832 | 1.00M | auto *Underlying = D; |
3833 | 1.00M | if (auto *USD = dyn_cast<UsingShadowDecl>(D)) |
3834 | 49 | Underlying = USD->getTargetDecl(); |
3835 | | |
3836 | 1.00M | if (!isa<FunctionDecl>(Underlying) && |
3837 | 1.00M | !isa<FunctionTemplateDecl>(Underlying)697k ) |
3838 | 94 | continue; |
3839 | | |
3840 | | // The declaration is visible to argument-dependent lookup if either |
3841 | | // it's ordinarily visible or declared as a friend in an associated |
3842 | | // class. |
3843 | 1.00M | bool Visible = false; |
3844 | 1.15M | for (D = D->getMostRecentDecl(); D; |
3845 | 1.00M | D = cast_or_null<NamedDecl>(D->getPreviousDecl())156k ) { |
3846 | 1.00M | if (D->getIdentifierNamespace() & Decl::IDNS_Ordinary) { |
3847 | 872k | if (isVisible(D)) { |
3848 | 843k | Visible = true; |
3849 | 843k | break; |
3850 | 843k | } |
3851 | 872k | } else if (133k D->getFriendObjectKind()133k ) { |
3852 | 133k | auto *RD = cast<CXXRecordDecl>(D->getLexicalDeclContext()); |
3853 | | // [basic.lookup.argdep]p4: |
3854 | | // Argument-dependent lookup finds all declarations of functions and |
3855 | | // function templates that |
3856 | | // - ... |
3857 | | // - are declared as a friend ([class.friend]) of any class with a |
3858 | | // reachable definition in the set of associated entities, |
3859 | | // |
3860 | | // FIXME: If there's a merged definition of D that is reachable, then |
3861 | | // the friend declaration should be considered. |
3862 | 133k | if (AssociatedClasses.count(RD) && isReachable(D)5.26k ) { |
3863 | 5.26k | Visible = true; |
3864 | 5.26k | break; |
3865 | 5.26k | } |
3866 | 133k | } |
3867 | 1.00M | } |
3868 | | |
3869 | | // FIXME: Preserve D as the FoundDecl. |
3870 | 1.00M | if (Visible) |
3871 | 849k | Result.insert(Underlying); |
3872 | 1.00M | } |
3873 | 255k | } |
3874 | 714k | } |
3875 | | |
3876 | | //---------------------------------------------------------------------------- |
3877 | | // Search for all visible declarations. |
3878 | | //---------------------------------------------------------------------------- |
3879 | 7.69k | VisibleDeclConsumer::~VisibleDeclConsumer() { } |
3880 | | |
3881 | 992 | bool VisibleDeclConsumer::includeHiddenDecls() const { return false; } |
3882 | | |
3883 | | namespace { |
3884 | | |
3885 | | class ShadowContextRAII; |
3886 | | |
3887 | | class VisibleDeclsRecord { |
3888 | | public: |
3889 | | /// An entry in the shadow map, which is optimized to store a |
3890 | | /// single declaration (the common case) but can also store a list |
3891 | | /// of declarations. |
3892 | | typedef llvm::TinyPtrVector<NamedDecl*> ShadowMapEntry; |
3893 | | |
3894 | | private: |
3895 | | /// A mapping from declaration names to the declarations that have |
3896 | | /// this name within a particular scope. |
3897 | | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
3898 | | |
3899 | | /// A list of shadow maps, which is used to model name hiding. |
3900 | | std::list<ShadowMap> ShadowMaps; |
3901 | | |
3902 | | /// The declaration contexts we have already visited. |
3903 | | llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; |
3904 | | |
3905 | | friend class ShadowContextRAII; |
3906 | | |
3907 | | public: |
3908 | | /// Determine whether we have already visited this context |
3909 | | /// (and, if not, note that we are going to visit that context now). |
3910 | 2.51k | bool visitedContext(DeclContext *Ctx) { |
3911 | 2.51k | return !VisitedContexts.insert(Ctx).second; |
3912 | 2.51k | } |
3913 | | |
3914 | 696 | bool alreadyVisitedContext(DeclContext *Ctx) { |
3915 | 696 | return VisitedContexts.count(Ctx); |
3916 | 696 | } |
3917 | | |
3918 | | /// Determine whether the given declaration is hidden in the |
3919 | | /// current scope. |
3920 | | /// |
3921 | | /// \returns the declaration that hides the given declaration, or |
3922 | | /// NULL if no such declaration exists. |
3923 | | NamedDecl *checkHidden(NamedDecl *ND); |
3924 | | |
3925 | | /// Add a declaration to the current shadow map. |
3926 | 24.8k | void add(NamedDecl *ND) { |
3927 | 24.8k | ShadowMaps.back()[ND->getDeclName()].push_back(ND); |
3928 | 24.8k | } |
3929 | | }; |
3930 | | |
3931 | | /// RAII object that records when we've entered a shadow context. |
3932 | | class ShadowContextRAII { |
3933 | | VisibleDeclsRecord &Visible; |
3934 | | |
3935 | | typedef VisibleDeclsRecord::ShadowMap ShadowMap; |
3936 | | |
3937 | | public: |
3938 | 5.01k | ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { |
3939 | 5.01k | Visible.ShadowMaps.emplace_back(); |
3940 | 5.01k | } |
3941 | | |
3942 | 5.01k | ~ShadowContextRAII() { |
3943 | 5.01k | Visible.ShadowMaps.pop_back(); |
3944 | 5.01k | } |
3945 | | }; |
3946 | | |
3947 | | } // end anonymous namespace |
3948 | | |
3949 | 24.8k | NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { |
3950 | 24.8k | unsigned IDNS = ND->getIdentifierNamespace(); |
3951 | 24.8k | std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); |
3952 | 24.8k | for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); |
3953 | 55.1k | SM != SMEnd; ++SM30.3k ) { |
3954 | 35.4k | ShadowMap::iterator Pos = SM->find(ND->getDeclName()); |
3955 | 35.4k | if (Pos == SM->end()) |
3956 | 25.3k | continue; |
3957 | | |
3958 | 12.3k | for (auto *D : Pos->second)10.0k { |
3959 | | // A tag declaration does not hide a non-tag declaration. |
3960 | 12.3k | if (D->hasTagIdentifierNamespace() && |
3961 | 12.3k | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
3962 | 362 | Decl::IDNS_ObjCProtocol))) |
3963 | 3 | continue; |
3964 | | |
3965 | | // Protocols are in distinct namespaces from everything else. |
3966 | 12.3k | if (((D->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
3967 | 12.3k | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
3968 | 12.3k | D->getIdentifierNamespace() != IDNS0 ) |
3969 | 0 | continue; |
3970 | | |
3971 | | // Functions and function templates in the same scope overload |
3972 | | // rather than hide. FIXME: Look for hiding based on function |
3973 | | // signatures! |
3974 | 12.3k | if (D->getUnderlyingDecl()->isFunctionOrFunctionTemplate() && |
3975 | 12.3k | ND->getUnderlyingDecl()->isFunctionOrFunctionTemplate()7.48k && |
3976 | 12.3k | SM == ShadowMaps.rbegin()7.48k ) |
3977 | 7.23k | continue; |
3978 | | |
3979 | | // A shadow declaration that's created by a resolved using declaration |
3980 | | // is not hidden by the same using declaration. |
3981 | 5.10k | if (isa<UsingShadowDecl>(ND) && isa<UsingDecl>(D)7 && |
3982 | 5.10k | cast<UsingShadowDecl>(ND)->getIntroducer() == D2 ) |
3983 | 2 | continue; |
3984 | | |
3985 | | // We've found a declaration that hides this one. |
3986 | 5.10k | return D; |
3987 | 5.10k | } |
3988 | 10.0k | } |
3989 | | |
3990 | 19.7k | return nullptr; |
3991 | 24.8k | } |
3992 | | |
3993 | | namespace { |
3994 | | class LookupVisibleHelper { |
3995 | | public: |
3996 | | LookupVisibleHelper(VisibleDeclConsumer &Consumer, bool IncludeDependentBases, |
3997 | | bool LoadExternal) |
3998 | | : Consumer(Consumer), IncludeDependentBases(IncludeDependentBases), |
3999 | 1.86k | LoadExternal(LoadExternal) {} |
4000 | | |
4001 | | void lookupVisibleDecls(Sema &SemaRef, Scope *S, Sema::LookupNameKind Kind, |
4002 | 696 | bool IncludeGlobalScope) { |
4003 | | // Determine the set of using directives available during |
4004 | | // unqualified name lookup. |
4005 | 696 | Scope *Initial = S; |
4006 | 696 | UnqualUsingDirectiveSet UDirs(SemaRef); |
4007 | 696 | if (SemaRef.getLangOpts().CPlusPlus) { |
4008 | | // Find the first namespace or translation-unit scope. |
4009 | 959 | while (S && !isNamespaceOrTranslationUnitScope(S)) |
4010 | 467 | S = S->getParent(); |
4011 | | |
4012 | 492 | UDirs.visitScopeChain(Initial, S); |
4013 | 492 | } |
4014 | 696 | UDirs.done(); |
4015 | | |
4016 | | // Look for visible declarations. |
4017 | 696 | LookupResult Result(SemaRef, DeclarationName(), SourceLocation(), Kind); |
4018 | 696 | Result.setAllowHidden(Consumer.includeHiddenDecls()); |
4019 | 696 | if (!IncludeGlobalScope) |
4020 | 137 | Visited.visitedContext(SemaRef.getASTContext().getTranslationUnitDecl()); |
4021 | 696 | ShadowContextRAII Shadow(Visited); |
4022 | 696 | lookupInScope(Initial, Result, UDirs); |
4023 | 696 | } |
4024 | | |
4025 | | void lookupVisibleDecls(Sema &SemaRef, DeclContext *Ctx, |
4026 | 1.17k | Sema::LookupNameKind Kind, bool IncludeGlobalScope) { |
4027 | 1.17k | LookupResult Result(SemaRef, DeclarationName(), SourceLocation(), Kind); |
4028 | 1.17k | Result.setAllowHidden(Consumer.includeHiddenDecls()); |
4029 | 1.17k | if (!IncludeGlobalScope) |
4030 | 5 | Visited.visitedContext(SemaRef.getASTContext().getTranslationUnitDecl()); |
4031 | | |
4032 | 1.17k | ShadowContextRAII Shadow(Visited); |
4033 | 1.17k | lookupInDeclContext(Ctx, Result, /*QualifiedNameLookup=*/true, |
4034 | 1.17k | /*InBaseClass=*/false); |
4035 | 1.17k | } |
4036 | | |
4037 | | private: |
4038 | | void lookupInDeclContext(DeclContext *Ctx, LookupResult &Result, |
4039 | 2.36k | bool QualifiedNameLookup, bool InBaseClass) { |
4040 | 2.36k | if (!Ctx) |
4041 | 0 | return; |
4042 | | |
4043 | | // Make sure we don't visit the same context twice. |
4044 | 2.36k | if (Visited.visitedContext(Ctx->getPrimaryContext())) |
4045 | 145 | return; |
4046 | | |
4047 | 2.22k | Consumer.EnteredContext(Ctx); |
4048 | | |
4049 | | // Outside C++, lookup results for the TU live on identifiers. |
4050 | 2.22k | if (isa<TranslationUnitDecl>(Ctx) && |
4051 | 2.22k | !Result.getSema().getLangOpts().CPlusPlus705 ) { |
4052 | 176 | auto &S = Result.getSema(); |
4053 | 176 | auto &Idents = S.Context.Idents; |
4054 | | |
4055 | | // Ensure all external identifiers are in the identifier table. |
4056 | 176 | if (LoadExternal) |
4057 | 176 | if (IdentifierInfoLookup *External = |
4058 | 176 | Idents.getExternalIdentifierLookup()) { |
4059 | 58 | std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers()); |
4060 | 192k | for (StringRef Name = Iter->Next(); !Name.empty(); |
4061 | 192k | Name = Iter->Next()) |
4062 | 192k | Idents.get(Name); |
4063 | 58 | } |
4064 | | |
4065 | | // Walk all lookup results in the TU for each identifier. |
4066 | 646k | for (const auto &Ident : Idents) { |
4067 | 646k | for (auto I = S.IdResolver.begin(Ident.getValue()), |
4068 | 646k | E = S.IdResolver.end(); |
4069 | 648k | I != E; ++I2.24k ) { |
4070 | 2.24k | if (S.IdResolver.isDeclInScope(*I, Ctx)) { |
4071 | 2.08k | if (NamedDecl *ND = Result.getAcceptableDecl(*I)) { |
4072 | 1.90k | Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); |
4073 | 1.90k | Visited.add(ND); |
4074 | 1.90k | } |
4075 | 2.08k | } |
4076 | 2.24k | } |
4077 | 646k | } |
4078 | | |
4079 | 176 | return; |
4080 | 176 | } |
4081 | | |
4082 | 2.04k | if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) |
4083 | 911 | Result.getSema().ForceDeclarationOfImplicitMembers(Class); |
4084 | | |
4085 | 2.04k | llvm::SmallVector<NamedDecl *, 4> DeclsToVisit; |
4086 | | // We sometimes skip loading namespace-level results (they tend to be huge). |
4087 | 2.04k | bool Load = LoadExternal || |
4088 | 2.04k | !(6 isa<TranslationUnitDecl>(Ctx)6 || isa<NamespaceDecl>(Ctx)5 ); |
4089 | | // Enumerate all of the results in this context. |
4090 | 2.04k | for (DeclContextLookupResult R : |
4091 | 2.04k | Load ? Ctx->lookups()2.04k |
4092 | 13.8k | : Ctx->noload_lookups(/*PreserveInternalState=*/false)5 ) { |
4093 | 16.0k | for (auto *D : R) { |
4094 | 16.0k | if (auto *ND = Result.getAcceptableDecl(D)) { |
4095 | | // Rather than visit immediately, we put ND into a vector and visit |
4096 | | // all decls, in order, outside of this loop. The reason is that |
4097 | | // Consumer.FoundDecl() may invalidate the iterators used in the two |
4098 | | // loops above. |
4099 | 15.4k | DeclsToVisit.push_back(ND); |
4100 | 15.4k | } |
4101 | 16.0k | } |
4102 | 13.8k | } |
4103 | | |
4104 | 15.4k | for (auto *ND : DeclsToVisit) { |
4105 | 15.4k | Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); |
4106 | 15.4k | Visited.add(ND); |
4107 | 15.4k | } |
4108 | 2.04k | DeclsToVisit.clear(); |
4109 | | |
4110 | | // Traverse using directives for qualified name lookup. |
4111 | 2.04k | if (QualifiedNameLookup) { |
4112 | 1.43k | ShadowContextRAII Shadow(Visited); |
4113 | 1.43k | for (auto I : Ctx->using_directives()) { |
4114 | 42 | if (!Result.getSema().isVisible(I)) |
4115 | 0 | continue; |
4116 | 42 | lookupInDeclContext(I->getNominatedNamespace(), Result, |
4117 | 42 | QualifiedNameLookup, InBaseClass); |
4118 | 42 | } |
4119 | 1.43k | } |
4120 | | |
4121 | | // Traverse the contexts of inherited C++ classes. |
4122 | 2.04k | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
4123 | 911 | if (!Record->hasDefinition()) |
4124 | 8 | return; |
4125 | | |
4126 | 903 | for (const auto &B : Record->bases()) { |
4127 | 129 | QualType BaseType = B.getType(); |
4128 | | |
4129 | 129 | RecordDecl *RD; |
4130 | 129 | if (BaseType->isDependentType()) { |
4131 | 2 | if (!IncludeDependentBases) { |
4132 | | // Don't look into dependent bases, because name lookup can't look |
4133 | | // there anyway. |
4134 | 0 | continue; |
4135 | 0 | } |
4136 | 2 | const auto *TST = BaseType->getAs<TemplateSpecializationType>(); |
4137 | 2 | if (!TST) |
4138 | 0 | continue; |
4139 | 2 | TemplateName TN = TST->getTemplateName(); |
4140 | 2 | const auto *TD = |
4141 | 2 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl()); |
4142 | 2 | if (!TD) |
4143 | 0 | continue; |
4144 | 2 | RD = TD->getTemplatedDecl(); |
4145 | 127 | } else { |
4146 | 127 | const auto *Record = BaseType->getAs<RecordType>(); |
4147 | 127 | if (!Record) |
4148 | 0 | continue; |
4149 | 127 | RD = Record->getDecl(); |
4150 | 127 | } |
4151 | | |
4152 | | // FIXME: It would be nice to be able to determine whether referencing |
4153 | | // a particular member would be ambiguous. For example, given |
4154 | | // |
4155 | | // struct A { int member; }; |
4156 | | // struct B { int member; }; |
4157 | | // struct C : A, B { }; |
4158 | | // |
4159 | | // void f(C *c) { c->### } |
4160 | | // |
4161 | | // accessing 'member' would result in an ambiguity. However, we |
4162 | | // could be smart enough to qualify the member with the base |
4163 | | // class, e.g., |
4164 | | // |
4165 | | // c->B::member |
4166 | | // |
4167 | | // or |
4168 | | // |
4169 | | // c->A::member |
4170 | | |
4171 | | // Find results in this base class (and its bases). |
4172 | 129 | ShadowContextRAII Shadow(Visited); |
4173 | 129 | lookupInDeclContext(RD, Result, QualifiedNameLookup, |
4174 | 129 | /*InBaseClass=*/true); |
4175 | 129 | } |
4176 | 903 | } |
4177 | | |
4178 | | // Traverse the contexts of Objective-C classes. |
4179 | 2.04k | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { |
4180 | | // Traverse categories. |
4181 | 125 | for (auto *Cat : IFace->visible_categories()) { |
4182 | 125 | ShadowContextRAII Shadow(Visited); |
4183 | 125 | lookupInDeclContext(Cat, Result, QualifiedNameLookup, |
4184 | 125 | /*InBaseClass=*/false); |
4185 | 125 | } |
4186 | | |
4187 | | // Traverse protocols. |
4188 | 103 | for (auto *I : IFace->all_referenced_protocols()) { |
4189 | 17 | ShadowContextRAII Shadow(Visited); |
4190 | 17 | lookupInDeclContext(I, Result, QualifiedNameLookup, |
4191 | 17 | /*InBaseClass=*/false); |
4192 | 17 | } |
4193 | | |
4194 | | // Traverse the superclass. |
4195 | 103 | if (IFace->getSuperClass()) { |
4196 | 23 | ShadowContextRAII Shadow(Visited); |
4197 | 23 | lookupInDeclContext(IFace->getSuperClass(), Result, QualifiedNameLookup, |
4198 | 23 | /*InBaseClass=*/true); |
4199 | 23 | } |
4200 | | |
4201 | | // If there is an implementation, traverse it. We do this to find |
4202 | | // synthesized ivars. |
4203 | 103 | if (IFace->getImplementation()) { |
4204 | 61 | ShadowContextRAII Shadow(Visited); |
4205 | 61 | lookupInDeclContext(IFace->getImplementation(), Result, |
4206 | 61 | QualifiedNameLookup, InBaseClass); |
4207 | 61 | } |
4208 | 1.93k | } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { |
4209 | 25 | for (auto *I : Protocol->protocols()) { |
4210 | 0 | ShadowContextRAII Shadow(Visited); |
4211 | 0 | lookupInDeclContext(I, Result, QualifiedNameLookup, |
4212 | 0 | /*InBaseClass=*/false); |
4213 | 0 | } |
4214 | 1.91k | } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { |
4215 | 125 | for (auto *I : Category->protocols()) { |
4216 | 8 | ShadowContextRAII Shadow(Visited); |
4217 | 8 | lookupInDeclContext(I, Result, QualifiedNameLookup, |
4218 | 8 | /*InBaseClass=*/false); |
4219 | 8 | } |
4220 | | |
4221 | | // If there is an implementation, traverse it. |
4222 | 125 | if (Category->getImplementation()) { |
4223 | 0 | ShadowContextRAII Shadow(Visited); |
4224 | 0 | lookupInDeclContext(Category->getImplementation(), Result, |
4225 | 0 | QualifiedNameLookup, /*InBaseClass=*/true); |
4226 | 0 | } |
4227 | 125 | } |
4228 | 2.04k | } |
4229 | | |
4230 | | void lookupInScope(Scope *S, LookupResult &Result, |
4231 | 2.04k | UnqualUsingDirectiveSet &UDirs) { |
4232 | | // No clients run in this mode and it's not supported. Please add tests and |
4233 | | // remove the assertion if you start relying on it. |
4234 | 2.04k | assert(!IncludeDependentBases && "Unsupported flag for lookupInScope"); |
4235 | | |
4236 | 2.04k | if (!S) |
4237 | 696 | return; |
4238 | | |
4239 | 1.34k | if (!S->getEntity() || |
4240 | 1.34k | (1.30k !S->getParent()1.30k && !Visited.alreadyVisitedContext(S->getEntity())696 ) || |
4241 | 1.34k | (S->getEntity())->isFunctionOrMethod()747 ) { |
4242 | 1.17k | FindLocalExternScope FindLocals(Result); |
4243 | | // Walk through the declarations in this Scope. The consumer might add new |
4244 | | // decls to the scope as part of deserialization, so make a copy first. |
4245 | 1.17k | SmallVector<Decl *, 8> ScopeDecls(S->decls().begin(), S->decls().end()); |
4246 | 7.73k | for (Decl *D : ScopeDecls) { |
4247 | 7.73k | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
4248 | 7.73k | if ((ND = Result.getAcceptableDecl(ND))) { |
4249 | 7.43k | Consumer.FoundDecl(ND, Visited.checkHidden(ND), nullptr, false); |
4250 | 7.43k | Visited.add(ND); |
4251 | 7.43k | } |
4252 | 7.73k | } |
4253 | 1.17k | } |
4254 | | |
4255 | 1.34k | DeclContext *Entity = S->getLookupEntity(); |
4256 | 1.34k | if (Entity) { |
4257 | | // Look into this scope's declaration context, along with any of its |
4258 | | // parent lookup contexts (e.g., enclosing classes), up to the point |
4259 | | // where we hit the context stored in the next outer scope. |
4260 | 1.30k | DeclContext *OuterCtx = findOuterContext(S); |
4261 | | |
4262 | 2.60k | for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx)1.90k ; |
4263 | 1.33k | Ctx = Ctx->getLookupParent()1.29k ) { |
4264 | 1.33k | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
4265 | 34 | if (Method->isInstanceMethod()) { |
4266 | | // For instance methods, look for ivars in the method's interface. |
4267 | 31 | LookupResult IvarResult(Result.getSema(), Result.getLookupName(), |
4268 | 31 | Result.getNameLoc(), |
4269 | 31 | Sema::LookupMemberName); |
4270 | 31 | if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) { |
4271 | 31 | lookupInDeclContext(IFace, IvarResult, |
4272 | 31 | /*QualifiedNameLookup=*/false, |
4273 | 31 | /*InBaseClass=*/false); |
4274 | 31 | } |
4275 | 31 | } |
4276 | | |
4277 | | // We've already performed all of the name lookup that we need |
4278 | | // to for Objective-C methods; the next context will be the |
4279 | | // outer scope. |
4280 | 34 | break; |
4281 | 34 | } |
4282 | | |
4283 | 1.29k | if (Ctx->isFunctionOrMethod()) |
4284 | 537 | continue; |
4285 | | |
4286 | 761 | lookupInDeclContext(Ctx, Result, /*QualifiedNameLookup=*/false, |
4287 | 761 | /*InBaseClass=*/false); |
4288 | 761 | } |
4289 | 1.30k | } else if (43 !S->getParent()43 ) { |
4290 | | // Look into the translation unit scope. We walk through the translation |
4291 | | // unit's declaration context, because the Scope itself won't have all of |
4292 | | // the declarations if we loaded a precompiled header. |
4293 | | // FIXME: We would like the translation unit's Scope object to point to |
4294 | | // the translation unit, so we don't need this special "if" branch. |
4295 | | // However, doing so would force the normal C++ name-lookup code to look |
4296 | | // into the translation unit decl when the IdentifierInfo chains would |
4297 | | // suffice. Once we fix that problem (which is part of a more general |
4298 | | // "don't look in DeclContexts unless we have to" optimization), we can |
4299 | | // eliminate this. |
4300 | 0 | Entity = Result.getSema().Context.getTranslationUnitDecl(); |
4301 | 0 | lookupInDeclContext(Entity, Result, /*QualifiedNameLookup=*/false, |
4302 | 0 | /*InBaseClass=*/false); |
4303 | 0 | } |
4304 | | |
4305 | 1.34k | if (Entity) { |
4306 | | // Lookup visible declarations in any namespaces found by using |
4307 | | // directives. |
4308 | 1.30k | for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(Entity)) |
4309 | 1 | lookupInDeclContext( |
4310 | 1 | const_cast<DeclContext *>(UUE.getNominatedNamespace()), Result, |
4311 | 1 | /*QualifiedNameLookup=*/false, |
4312 | 1 | /*InBaseClass=*/false); |
4313 | 1.30k | } |
4314 | | |
4315 | | // Lookup names in the parent scope. |
4316 | 1.34k | ShadowContextRAII Shadow(Visited); |
4317 | 1.34k | lookupInScope(S->getParent(), Result, UDirs); |
4318 | 1.34k | } |
4319 | | |
4320 | | private: |
4321 | | VisibleDeclsRecord Visited; |
4322 | | VisibleDeclConsumer &Consumer; |
4323 | | bool IncludeDependentBases; |
4324 | | bool LoadExternal; |
4325 | | }; |
4326 | | } // namespace |
4327 | | |
4328 | | void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, |
4329 | | VisibleDeclConsumer &Consumer, |
4330 | 696 | bool IncludeGlobalScope, bool LoadExternal) { |
4331 | 696 | LookupVisibleHelper H(Consumer, /*IncludeDependentBases=*/false, |
4332 | 696 | LoadExternal); |
4333 | 696 | H.lookupVisibleDecls(*this, S, Kind, IncludeGlobalScope); |
4334 | 696 | } |
4335 | | |
4336 | | void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, |
4337 | | VisibleDeclConsumer &Consumer, |
4338 | | bool IncludeGlobalScope, |
4339 | 1.17k | bool IncludeDependentBases, bool LoadExternal) { |
4340 | 1.17k | LookupVisibleHelper H(Consumer, IncludeDependentBases, LoadExternal); |
4341 | 1.17k | H.lookupVisibleDecls(*this, Ctx, Kind, IncludeGlobalScope); |
4342 | 1.17k | } |
4343 | | |
4344 | | /// LookupOrCreateLabel - Do a name lookup of a label with the specified name. |
4345 | | /// If GnuLabelLoc is a valid source location, then this is a definition |
4346 | | /// of an __label__ label name, otherwise it is a normal label definition |
4347 | | /// or use. |
4348 | | LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc, |
4349 | 10.5k | SourceLocation GnuLabelLoc) { |
4350 | | // Do a lookup to see if we have a label with this name already. |
4351 | 10.5k | NamedDecl *Res = nullptr; |
4352 | | |
4353 | 10.5k | if (GnuLabelLoc.isValid()) { |
4354 | | // Local label definitions always shadow existing labels. |
4355 | 11 | Res = LabelDecl::Create(Context, CurContext, Loc, II, GnuLabelLoc); |
4356 | 11 | Scope *S = CurScope; |
4357 | 11 | PushOnScopeChains(Res, S, true); |
4358 | 11 | return cast<LabelDecl>(Res); |
4359 | 11 | } |
4360 | | |
4361 | | // Not a GNU local label. |
4362 | 10.5k | Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration); |
4363 | | // If we found a label, check to see if it is in the same context as us. |
4364 | | // When in a Block, we don't want to reuse a label in an enclosing function. |
4365 | 10.5k | if (Res && Res->getDeclContext() != CurContext6.84k ) |
4366 | 246 | Res = nullptr; |
4367 | 10.5k | if (!Res) { |
4368 | | // If not forward referenced or defined already, create the backing decl. |
4369 | 3.96k | Res = LabelDecl::Create(Context, CurContext, Loc, II); |
4370 | 3.96k | Scope *S = CurScope->getFnParent(); |
4371 | 3.96k | assert(S && "Not in a function?"); |
4372 | 0 | PushOnScopeChains(Res, S, true); |
4373 | 3.96k | } |
4374 | 0 | return cast<LabelDecl>(Res); |
4375 | 10.5k | } |
4376 | | |
4377 | | //===----------------------------------------------------------------------===// |
4378 | | // Typo correction |
4379 | | //===----------------------------------------------------------------------===// |
4380 | | |
4381 | | static bool isCandidateViable(CorrectionCandidateCallback &CCC, |
4382 | 4.63k | TypoCorrection &Candidate) { |
4383 | 4.63k | Candidate.setCallbackDistance(CCC.RankCandidate(Candidate)); |
4384 | 4.63k | return Candidate.getEditDistance(false) != TypoCorrection::InvalidDistance; |
4385 | 4.63k | } |
4386 | | |
4387 | | static void LookupPotentialTypoResult(Sema &SemaRef, |
4388 | | LookupResult &Res, |
4389 | | IdentifierInfo *Name, |
4390 | | Scope *S, CXXScopeSpec *SS, |
4391 | | DeclContext *MemberContext, |
4392 | | bool EnteringContext, |
4393 | | bool isObjCIvarLookup, |
4394 | | bool FindHidden); |
4395 | | |
4396 | | /// Check whether the declarations found for a typo correction are |
4397 | | /// visible. Set the correction's RequiresImport flag to true if none of the |
4398 | | /// declarations are visible, false otherwise. |
4399 | 5.48k | static void checkCorrectionVisibility(Sema &SemaRef, TypoCorrection &TC) { |
4400 | 5.48k | TypoCorrection::decl_iterator DI = TC.begin(), DE = TC.end(); |
4401 | | |
4402 | 13.2k | for (/**/; DI != DE; ++DI7.78k ) |
4403 | 8.24k | if (!LookupResult::isVisible(SemaRef, *DI)) |
4404 | 466 | break; |
4405 | | // No filtering needed if all decls are visible. |
4406 | 5.48k | if (DI == DE) { |
4407 | 5.02k | TC.setRequiresImport(false); |
4408 | 5.02k | return; |
4409 | 5.02k | } |
4410 | | |
4411 | 466 | llvm::SmallVector<NamedDecl*, 4> NewDecls(TC.begin(), DI); |
4412 | 466 | bool AnyVisibleDecls = !NewDecls.empty(); |
4413 | | |
4414 | 932 | for (/**/; DI != DE; ++DI466 ) { |
4415 | 466 | if (LookupResult::isVisible(SemaRef, *DI)) { |
4416 | 0 | if (!AnyVisibleDecls) { |
4417 | | // Found a visible decl, discard all hidden ones. |
4418 | 0 | AnyVisibleDecls = true; |
4419 | 0 | NewDecls.clear(); |
4420 | 0 | } |
4421 | 0 | NewDecls.push_back(*DI); |
4422 | 466 | } else if (!AnyVisibleDecls && !(*DI)->isModulePrivate()) |
4423 | 454 | NewDecls.push_back(*DI); |
4424 | 466 | } |
4425 | | |
4426 | 466 | if (NewDecls.empty()) |
4427 | 12 | TC = TypoCorrection(); |
4428 | 454 | else { |
4429 | 454 | TC.setCorrectionDecls(NewDecls); |
4430 | 454 | TC.setRequiresImport(!AnyVisibleDecls); |
4431 | 454 | } |
4432 | 466 | } |
4433 | | |
4434 | | // Fill the supplied vector with the IdentifierInfo pointers for each piece of |
4435 | | // the given NestedNameSpecifier (i.e. given a NestedNameSpecifier "foo::bar::", |
4436 | | // fill the vector with the IdentifierInfo pointers for "foo" and "bar"). |
4437 | | static void getNestedNameSpecifierIdentifiers( |
4438 | | NestedNameSpecifier *NNS, |
4439 | 26.3k | SmallVectorImpl<const IdentifierInfo*> &Identifiers) { |
4440 | 26.3k | if (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
4441 | 10.4k | getNestedNameSpecifierIdentifiers(Prefix, Identifiers); |
4442 | 15.8k | else |
4443 | 15.8k | Identifiers.clear(); |
4444 | | |
4445 | 26.3k | const IdentifierInfo *II = nullptr; |
4446 | | |
4447 | 26.3k | switch (NNS->getKind()) { |
4448 | 6 | case NestedNameSpecifier::Identifier: |
4449 | 6 | II = NNS->getAsIdentifier(); |
4450 | 6 | break; |
4451 | | |
4452 | 13.8k | case NestedNameSpecifier::Namespace: |
4453 | 13.8k | if (NNS->getAsNamespace()->isAnonymousNamespace()) |
4454 | 0 | return; |
4455 | 13.8k | II = NNS->getAsNamespace()->getIdentifier(); |
4456 | 13.8k | break; |
4457 | | |
4458 | 4 | case NestedNameSpecifier::NamespaceAlias: |
4459 | 4 | II = NNS->getAsNamespaceAlias()->getIdentifier(); |
4460 | 4 | break; |
4461 | | |
4462 | 0 | case NestedNameSpecifier::TypeSpecWithTemplate: |
4463 | 11.6k | case NestedNameSpecifier::TypeSpec: |
4464 | 11.6k | II = QualType(NNS->getAsType(), 0).getBaseTypeIdentifier(); |
4465 | 11.6k | break; |
4466 | | |
4467 | 772 | case NestedNameSpecifier::Global: |
4468 | 780 | case NestedNameSpecifier::Super: |
4469 | 780 | return; |
4470 | 26.3k | } |
4471 | | |
4472 | 25.5k | if (II) |
4473 | 25.3k | Identifiers.push_back(II); |
4474 | 25.5k | } |
4475 | | |
4476 | | void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, |
4477 | 7.14k | DeclContext *Ctx, bool InBaseClass) { |
4478 | | // Don't consider hidden names for typo correction. |
4479 | 7.14k | if (Hiding) |
4480 | 238 | return; |
4481 | | |
4482 | | // Only consider entities with identifiers for names, ignoring |
4483 | | // special names (constructors, overloaded operators, selectors, |
4484 | | // etc.). |
4485 | 6.91k | IdentifierInfo *Name = ND->getIdentifier(); |
4486 | 6.91k | if (!Name) |
4487 | 3.72k | return; |
4488 | | |
4489 | | // Only consider visible declarations and declarations from modules with |
4490 | | // names that exactly match. |
4491 | 3.19k | if (!LookupResult::isVisible(SemaRef, ND) && Name != Typo410 ) |
4492 | 356 | return; |
4493 | | |
4494 | 2.83k | FoundName(Name->getName()); |
4495 | 2.83k | } |
4496 | | |
4497 | 25.4M | void TypoCorrectionConsumer::FoundName(StringRef Name) { |
4498 | | // Compute the edit distance between the typo and the name of this |
4499 | | // entity, and add the identifier to the list of results. |
4500 | 25.4M | addName(Name, nullptr); |
4501 | 25.4M | } |
4502 | | |
4503 | 127k | void TypoCorrectionConsumer::addKeywordResult(StringRef Keyword) { |
4504 | | // Compute the edit distance between the typo and this keyword, |
4505 | | // and add the keyword to the list of results. |
4506 | 127k | addName(Keyword, nullptr, nullptr, true); |
4507 | 127k | } |
4508 | | |
4509 | | void TypoCorrectionConsumer::addName(StringRef Name, NamedDecl *ND, |
4510 | 25.5M | NestedNameSpecifier *NNS, bool isKeyword) { |
4511 | | // Use a simple length-based heuristic to determine the minimum possible |
4512 | | // edit distance. If the minimum isn't good enough, bail out early. |
4513 | 25.5M | StringRef TypoStr = Typo->getName(); |
4514 | 25.5M | unsigned MinED = abs((int)Name.size() - (int)TypoStr.size()); |
4515 | 25.5M | if (MinED && TypoStr.size() / MinED < 325.1M ) |
4516 | 22.8M | return; |
4517 | | |
4518 | | // Compute an upper bound on the allowable edit distance, so that the |
4519 | | // edit-distance algorithm can short-circuit. |
4520 | 2.71M | unsigned UpperBound = (TypoStr.size() + 2) / 3; |
4521 | 2.71M | unsigned ED = TypoStr.edit_distance(Name, true, UpperBound); |
4522 | 2.71M | if (ED > UpperBound) return2.66M ; |
4523 | | |
4524 | 50.6k | TypoCorrection TC(&SemaRef.Context.Idents.get(Name), ND, NNS, ED); |
4525 | 50.6k | if (isKeyword) TC.makeKeyword()162 ; |
4526 | 50.6k | TC.setCorrectionRange(nullptr, Result.getLookupNameInfo()); |
4527 | 50.6k | addCorrection(TC); |
4528 | 50.6k | } |
4529 | | |
4530 | | static const unsigned MaxTypoDistanceResultSets = 5; |
4531 | | |
4532 | 52.6k | void TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) { |
4533 | 52.6k | StringRef TypoStr = Typo->getName(); |
4534 | 52.6k | StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName(); |
4535 | | |
4536 | | // For very short typos, ignore potential corrections that have a different |
4537 | | // base identifier from the typo or which have a normalized edit distance |
4538 | | // longer than the typo itself. |
4539 | 52.6k | if (TypoStr.size() < 3 && |
4540 | 52.6k | (28.6k Name != TypoStr28.6k || Correction.getEditDistance(true) > TypoStr.size()3.38k )) |
4541 | 25.7k | return; |
4542 | | |
4543 | | // If the correction is resolved but is not viable, ignore it. |
4544 | 26.9k | if (Correction.isResolved()) { |
4545 | 1.76k | checkCorrectionVisibility(SemaRef, Correction); |
4546 | 1.76k | if (!Correction || !isCandidateViable(*CorrectionValidator, Correction)) |
4547 | 837 | return; |
4548 | 1.76k | } |
4549 | | |
4550 | 26.0k | TypoResultList &CList = |
4551 | 26.0k | CorrectionResults[Correction.getEditDistance(false)][Name]; |
4552 | | |
4553 | 26.0k | if (!CList.empty() && !CList.back().isResolved()1.10k ) |
4554 | 712 | CList.pop_back(); |
4555 | 26.0k | if (NamedDecl *NewND = Correction.getCorrectionDecl()) { |
4556 | 813 | auto RI = llvm::find_if(CList, [NewND](const TypoCorrection &TypoCorr) { |
4557 | 769 | return TypoCorr.getCorrectionDecl() == NewND; |
4558 | 769 | }); |
4559 | 813 | if (RI != CList.end()) { |
4560 | | // The Correction refers to a decl already in the list. No insertion is |
4561 | | // necessary and all further cases will return. |
4562 | | |
4563 | 270 | auto IsDeprecated = [](Decl *D) { |
4564 | 588 | while (D) { |
4565 | 324 | if (D->isDeprecated()) |
4566 | 6 | return true; |
4567 | 318 | D = llvm::dyn_cast_or_null<NamespaceDecl>(D->getDeclContext()); |
4568 | 318 | } |
4569 | 264 | return false; |
4570 | 270 | }; |
4571 | | |
4572 | | // Prefer non deprecated Corrections over deprecated and only then |
4573 | | // sort using an alphabetical order. |
4574 | 135 | std::pair<bool, std::string> NewKey = { |
4575 | 135 | IsDeprecated(Correction.getFoundDecl()), |
4576 | 135 | Correction.getAsString(SemaRef.getLangOpts())}; |
4577 | | |
4578 | 135 | std::pair<bool, std::string> PrevKey = { |
4579 | 135 | IsDeprecated(RI->getFoundDecl()), |
4580 | 135 | RI->getAsString(SemaRef.getLangOpts())}; |
4581 | | |
4582 | 135 | if (NewKey < PrevKey) |
4583 | 9 | *RI = Correction; |
4584 | 135 | return; |
4585 | 135 | } |
4586 | 813 | } |
4587 | 25.9k | if (CList.empty() || Correction.isResolved()262 ) |
4588 | 25.9k | CList.push_back(Correction); |
4589 | | |
4590 | 26.8k | while (CorrectionResults.size() > MaxTypoDistanceResultSets) |
4591 | 891 | CorrectionResults.erase(std::prev(CorrectionResults.end())); |
4592 | 25.9k | } |
4593 | | |
4594 | | void TypoCorrectionConsumer::addNamespaces( |
4595 | 4.66k | const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces) { |
4596 | 4.66k | SearchNamespaces = true; |
4597 | | |
4598 | 4.66k | for (auto KNPair : KnownNamespaces) |
4599 | 23.6k | Namespaces.addNameSpecifier(KNPair.first); |
4600 | | |
4601 | 4.66k | bool SSIsTemplate = false; |
4602 | 4.66k | if (NestedNameSpecifier *NNS = |
4603 | 4.66k | (SS && SS->isValid()) ? SS->getScopeRep() : nullptr) { |
4604 | 456 | if (const Type *T = NNS->getAsType()) |
4605 | 247 | SSIsTemplate = T->getTypeClass() == Type::TemplateSpecialization; |
4606 | 456 | } |
4607 | | // Do not transform this into an iterator-based loop. The loop body can |
4608 | | // trigger the creation of further types (through lazy deserialization) and |
4609 | | // invalid iterators into this list. |
4610 | 4.66k | auto &Types = SemaRef.getASTContext().getTypes(); |
4611 | 1.76M | for (unsigned I = 0; I != Types.size(); ++I1.75M ) { |
4612 | 1.75M | const auto *TI = Types[I]; |
4613 | 1.75M | if (CXXRecordDecl *CD = TI->getAsCXXRecordDecl()) { |
4614 | 204k | CD = CD->getCanonicalDecl(); |
4615 | 204k | if (!CD->isDependentType() && !CD->isAnonymousStructOrUnion()136k && |
4616 | 204k | !CD->isUnion()135k && CD->getIdentifier()134k && |
4617 | 204k | (94.5k SSIsTemplate94.5k || !isa<ClassTemplateSpecializationDecl>(CD)88.7k ) && |
4618 | 204k | (63.6k CD->isBeingDefined()63.6k || CD->isCompleteDefinition()63.5k )) |
4619 | 54.4k | Namespaces.addNameSpecifier(CD); |
4620 | 204k | } |
4621 | 1.75M | } |
4622 | 4.66k | } |
4623 | | |
4624 | 12.2k | const TypoCorrection &TypoCorrectionConsumer::getNextCorrection() { |
4625 | 12.2k | if (++CurrentTCIndex < ValidatedCorrections.size()) |
4626 | 488 | return ValidatedCorrections[CurrentTCIndex]; |
4627 | | |
4628 | 11.7k | CurrentTCIndex = ValidatedCorrections.size(); |
4629 | 48.2k | while (!CorrectionResults.empty()) { |
4630 | 38.6k | auto DI = CorrectionResults.begin(); |
4631 | 38.6k | if (DI->second.empty()) { |
4632 | 8.99k | CorrectionResults.erase(DI); |
4633 | 8.99k | continue; |
4634 | 8.99k | } |
4635 | | |
4636 | 29.6k | auto RI = DI->second.begin(); |
4637 | 29.6k | if (RI->second.empty()) { |
4638 | 14.5k | DI->second.erase(RI); |
4639 | 14.5k | performQualifiedLookups(); |
4640 | 14.5k | continue; |
4641 | 14.5k | } |
4642 | | |
4643 | 15.1k | TypoCorrection TC = RI->second.pop_back_val(); |
4644 | 15.1k | if (TC.isResolved() || TC.requiresImport()14.4k || resolveCorrection(TC)14.4k ) { |
4645 | 2.18k | ValidatedCorrections.push_back(TC); |
4646 | 2.18k | return ValidatedCorrections[CurrentTCIndex]; |
4647 | 2.18k | } |
4648 | 15.1k | } |
4649 | 9.59k | return ValidatedCorrections[0]; // The empty correction. |
4650 | 11.7k | } |
4651 | | |
4652 | 14.4k | bool TypoCorrectionConsumer::resolveCorrection(TypoCorrection &Candidate) { |
4653 | 14.4k | IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo(); |
4654 | 14.4k | DeclContext *TempMemberContext = MemberContext; |
4655 | 14.4k | CXXScopeSpec *TempSS = SS.get(); |
4656 | 22.6k | retry_lookup: |
4657 | 22.6k | LookupPotentialTypoResult(SemaRef, Result, Name, S, TempSS, TempMemberContext, |
4658 | 22.6k | EnteringContext, |
4659 | 22.6k | CorrectionValidator->IsObjCIvarLookup, |
4660 | 22.6k | Name == Typo && !Candidate.WillReplaceSpecifier()10.9k ); |
4661 | 22.6k | switch (Result.getResultKind()) { |
4662 | 19.7k | case LookupResult::NotFound: |
4663 | 19.7k | case LookupResult::NotFoundInCurrentInstantiation: |
4664 | 19.7k | case LookupResult::FoundUnresolvedValue: |
4665 | 19.7k | if (TempSS) { |
4666 | | // Immediately retry the lookup without the given CXXScopeSpec |
4667 | 7.94k | TempSS = nullptr; |
4668 | 7.94k | Candidate.WillReplaceSpecifier(true); |
4669 | 7.94k | goto retry_lookup; |
4670 | 7.94k | } |
4671 | 11.7k | if (TempMemberContext) { |
4672 | 161 | if (SS && !TempSS) |
4673 | 161 | TempSS = SS.get(); |
4674 | 161 | TempMemberContext = nullptr; |
4675 | 161 | goto retry_lookup; |
4676 | 161 | } |
4677 | 11.6k | if (SearchNamespaces) |
4678 | 6.81k | QualifiedResults.push_back(Candidate); |
4679 | 11.6k | break; |
4680 | | |
4681 | 0 | case LookupResult::Ambiguous: |
4682 | | // We don't deal with ambiguities. |
4683 | 0 | break; |
4684 | | |
4685 | 2.61k | case LookupResult::Found: |
4686 | 2.87k | case LookupResult::FoundOverloaded: |
4687 | | // Store all of the Decls for overloaded symbols |
4688 | 2.87k | for (auto *TRD : Result) |
4689 | 4.85k | Candidate.addCorrectionDecl(TRD); |
4690 | 2.87k | checkCorrectionVisibility(SemaRef, Candidate); |
4691 | 2.87k | if (!isCandidateViable(*CorrectionValidator, Candidate)) { |
4692 | 1.31k | if (SearchNamespaces) |
4693 | 922 | QualifiedResults.push_back(Candidate); |
4694 | 1.31k | break; |
4695 | 1.31k | } |
4696 | 1.55k | Candidate.setCorrectionRange(SS.get(), Result.getLookupNameInfo()); |
4697 | 1.55k | return true; |
4698 | 22.6k | } |
4699 | 12.9k | return false; |
4700 | 22.6k | } |
4701 | | |
4702 | 14.5k | void TypoCorrectionConsumer::performQualifiedLookups() { |
4703 | 14.5k | unsigned TypoLen = Typo->getName().size(); |
4704 | 14.5k | for (const TypoCorrection &QR : QualifiedResults) { |
4705 | 131k | for (const auto &NSI : Namespaces) { |
4706 | 131k | DeclContext *Ctx = NSI.DeclCtx; |
4707 | 131k | const Type *NSType = NSI.NameSpecifier->getAsType(); |
4708 | | |
4709 | | // If the current NestedNameSpecifier refers to a class and the |
4710 | | // current correction candidate is the name of that class, then skip |
4711 | | // it as it is unlikely a qualified version of the class' constructor |
4712 | | // is an appropriate correction. |
4713 | 131k | if (CXXRecordDecl *NSDecl = NSType ? NSType->getAsCXXRecordDecl() : |
4714 | 131k | nullptr) { |
4715 | 87.4k | if (NSDecl->getIdentifier() == QR.getCorrectionAsIdentifierInfo()) |
4716 | 173 | continue; |
4717 | 87.4k | } |
4718 | | |
4719 | 130k | TypoCorrection TC(QR); |
4720 | 130k | TC.ClearCorrectionDecls(); |
4721 | 130k | TC.setCorrectionSpecifier(NSI.NameSpecifier); |
4722 | 130k | TC.setQualifierDistance(NSI.EditDistance); |
4723 | 130k | TC.setCallbackDistance(0); // Reset the callback distance |
4724 | | |
4725 | | // If the current correction candidate and namespace combination are |
4726 | | // too far away from the original typo based on the normalized edit |
4727 | | // distance, then skip performing a qualified name lookup. |
4728 | 130k | unsigned TmpED = TC.getEditDistance(true); |
4729 | 130k | if (QR.getCorrectionAsIdentifierInfo() != Typo && TmpED52.9k && |
4730 | 130k | TypoLen / TmpED < 352.9k ) |
4731 | 47.3k | continue; |
4732 | | |
4733 | 83.5k | Result.clear(); |
4734 | 83.5k | Result.setLookupName(QR.getCorrectionAsIdentifierInfo()); |
4735 | 83.5k | if (!SemaRef.LookupQualifiedName(Result, Ctx)) |
4736 | 81.3k | continue; |
4737 | | |
4738 | | // Any corrections added below will be validated in subsequent |
4739 | | // iterations of the main while() loop over the Consumer's contents. |
4740 | 2.20k | switch (Result.getResultKind()) { |
4741 | 1.92k | case LookupResult::Found: |
4742 | 2.19k | case LookupResult::FoundOverloaded: { |
4743 | 2.19k | if (SS && SS->isValid()1.66k ) { |
4744 | 676 | std::string NewQualified = TC.getAsString(SemaRef.getLangOpts()); |
4745 | 676 | std::string OldQualified; |
4746 | 676 | llvm::raw_string_ostream OldOStream(OldQualified); |
4747 | 676 | SS->getScopeRep()->print(OldOStream, SemaRef.getPrintingPolicy()); |
4748 | 676 | OldOStream << Typo->getName(); |
4749 | | // If correction candidate would be an identical written qualified |
4750 | | // identifier, then the existing CXXScopeSpec probably included a |
4751 | | // typedef that didn't get accounted for properly. |
4752 | 676 | if (OldOStream.str() == NewQualified) |
4753 | 5 | break; |
4754 | 676 | } |
4755 | 2.19k | for (LookupResult::iterator TRD = Result.begin(), TRDEnd = Result.end(); |
4756 | 4.61k | TRD != TRDEnd; ++TRD2.42k ) { |
4757 | 2.42k | if (SemaRef.CheckMemberAccess(TC.getCorrectionRange().getBegin(), |
4758 | 2.42k | NSType ? NSType->getAsCXXRecordDecl()986 |
4759 | 2.42k | : nullptr1.44k , |
4760 | 2.42k | TRD.getPair()) == Sema::AR_accessible) |
4761 | 2.26k | TC.addCorrectionDecl(*TRD); |
4762 | 2.42k | } |
4763 | 2.19k | if (TC.isResolved()) { |
4764 | 2.02k | TC.setCorrectionRange(SS.get(), Result.getLookupNameInfo()); |
4765 | 2.02k | addCorrection(TC); |
4766 | 2.02k | } |
4767 | 2.19k | break; |
4768 | 2.19k | } |
4769 | 0 | case LookupResult::NotFound: |
4770 | 0 | case LookupResult::NotFoundInCurrentInstantiation: |
4771 | 11 | case LookupResult::Ambiguous: |
4772 | 11 | case LookupResult::FoundUnresolvedValue: |
4773 | 11 | break; |
4774 | 2.20k | } |
4775 | 2.20k | } |
4776 | 7.73k | } |
4777 | 14.5k | QualifiedResults.clear(); |
4778 | 14.5k | } |
4779 | | |
4780 | | TypoCorrectionConsumer::NamespaceSpecifierSet::NamespaceSpecifierSet( |
4781 | | ASTContext &Context, DeclContext *CurContext, CXXScopeSpec *CurScopeSpec) |
4782 | 6.71k | : Context(Context), CurContextChain(buildContextChain(CurContext)) { |
4783 | 6.71k | if (NestedNameSpecifier *NNS = |
4784 | 6.71k | CurScopeSpec ? CurScopeSpec->getScopeRep() : nullptr) { |
4785 | 643 | llvm::raw_string_ostream SpecifierOStream(CurNameSpecifier); |
4786 | 643 | NNS->print(SpecifierOStream, Context.getPrintingPolicy()); |
4787 | | |
4788 | 643 | getNestedNameSpecifierIdentifiers(NNS, CurNameSpecifierIdentifiers); |
4789 | 643 | } |
4790 | | // Build the list of identifiers that would be used for an absolute |
4791 | | // (from the global context) NestedNameSpecifier referring to the current |
4792 | | // context. |
4793 | 15.9k | for (DeclContext *C : llvm::reverse(CurContextChain)) { |
4794 | 15.9k | if (auto *ND = dyn_cast_or_null<NamespaceDecl>(C)) |
4795 | 1.39k | CurContextIdentifiers.push_back(ND->getIdentifier()); |
4796 | 15.9k | } |
4797 | | |
4798 | | // Add the global context as a NestedNameSpecifier |
4799 | 6.71k | SpecifierInfo SI = {cast<DeclContext>(Context.getTranslationUnitDecl()), |
4800 | 6.71k | NestedNameSpecifier::GlobalSpecifier(Context), 1}; |
4801 | 6.71k | DistanceMap[1].push_back(SI); |
4802 | 6.71k | } |
4803 | | |
4804 | | auto TypoCorrectionConsumer::NamespaceSpecifierSet::buildContextChain( |
4805 | 84.7k | DeclContext *Start) -> DeclContextList { |
4806 | 84.7k | assert(Start && "Building a context chain from a null context"); |
4807 | 0 | DeclContextList Chain; |
4808 | 297k | for (DeclContext *DC = Start->getPrimaryContext(); DC != nullptr; |
4809 | 213k | DC = DC->getLookupParent()) { |
4810 | 213k | NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(DC); |
4811 | 213k | if (!DC->isInlineNamespace() && !DC->isTransparentContext()213k && |
4812 | 213k | !(213k ND213k && ND->isAnonymousNamespace()62.9k )) |
4813 | 212k | Chain.push_back(DC->getPrimaryContext()); |
4814 | 213k | } |
4815 | 84.7k | return Chain; |
4816 | 84.7k | } |
4817 | | |
4818 | | unsigned |
4819 | | TypoCorrectionConsumer::NamespaceSpecifierSet::buildNestedNameSpecifier( |
4820 | 79.9k | DeclContextList &DeclChain, NestedNameSpecifier *&NNS) { |
4821 | 79.9k | unsigned NumSpecifiers = 0; |
4822 | 119k | for (DeclContext *C : llvm::reverse(DeclChain)) { |
4823 | 119k | if (auto *ND = dyn_cast_or_null<NamespaceDecl>(C)) { |
4824 | 59.3k | NNS = NestedNameSpecifier::Create(Context, NNS, ND); |
4825 | 59.3k | ++NumSpecifiers; |
4826 | 59.6k | } else if (auto *RD = dyn_cast_or_null<RecordDecl>(C)) { |
4827 | 56.9k | NNS = NestedNameSpecifier::Create(Context, NNS, RD->isTemplateDecl(), |
4828 | 56.9k | RD->getTypeForDecl()); |
4829 | 56.9k | ++NumSpecifiers; |
4830 | 56.9k | } |
4831 | 119k | } |
4832 | 79.9k | return NumSpecifiers; |
4833 | 79.9k | } |
4834 | | |
4835 | | void TypoCorrectionConsumer::NamespaceSpecifierSet::addNameSpecifier( |
4836 | 78.0k | DeclContext *Ctx) { |
4837 | 78.0k | NestedNameSpecifier *NNS = nullptr; |
4838 | 78.0k | unsigned NumSpecifiers = 0; |
4839 | 78.0k | DeclContextList NamespaceDeclChain(buildContextChain(Ctx)); |
4840 | 78.0k | DeclContextList FullNamespaceDeclChain(NamespaceDeclChain); |
4841 | | |
4842 | | // Eliminate common elements from the two DeclContext chains. |
4843 | 152k | for (DeclContext *C : llvm::reverse(CurContextChain)) { |
4844 | 152k | if (NamespaceDeclChain.empty() || NamespaceDeclChain.back() != C151k ) |
4845 | 70.2k | break; |
4846 | 82.3k | NamespaceDeclChain.pop_back(); |
4847 | 82.3k | } |
4848 | | |
4849 | | // Build the NestedNameSpecifier from what is left of the NamespaceDeclChain |
4850 | 78.0k | NumSpecifiers = buildNestedNameSpecifier(NamespaceDeclChain, NNS); |
4851 | | |
4852 | | // Add an explicit leading '::' specifier if needed. |
4853 | 78.0k | if (NamespaceDeclChain.empty()) { |
4854 | | // Rebuild the NestedNameSpecifier as a globally-qualified specifier. |
4855 | 1.41k | NNS = NestedNameSpecifier::GlobalSpecifier(Context); |
4856 | 1.41k | NumSpecifiers = |
4857 | 1.41k | buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); |
4858 | 76.6k | } else if (NamedDecl *ND = |
4859 | 76.6k | dyn_cast_or_null<NamedDecl>(NamespaceDeclChain.back())) { |
4860 | 76.6k | IdentifierInfo *Name = ND->getIdentifier(); |
4861 | 76.6k | bool SameNameSpecifier = false; |
4862 | 76.6k | if (llvm::is_contained(CurNameSpecifierIdentifiers, Name)) { |
4863 | 902 | std::string NewNameSpecifier; |
4864 | 902 | llvm::raw_string_ostream SpecifierOStream(NewNameSpecifier); |
4865 | 902 | SmallVector<const IdentifierInfo *, 4> NewNameSpecifierIdentifiers; |
4866 | 902 | getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); |
4867 | 902 | NNS->print(SpecifierOStream, Context.getPrintingPolicy()); |
4868 | 902 | SpecifierOStream.flush(); |
4869 | 902 | SameNameSpecifier = NewNameSpecifier == CurNameSpecifier; |
4870 | 902 | } |
4871 | 76.6k | if (SameNameSpecifier || llvm::is_contained(CurContextIdentifiers, Name)76.1k ) { |
4872 | | // Rebuild the NestedNameSpecifier as a globally-qualified specifier. |
4873 | 453 | NNS = NestedNameSpecifier::GlobalSpecifier(Context); |
4874 | 453 | NumSpecifiers = |
4875 | 453 | buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); |
4876 | 453 | } |
4877 | 76.6k | } |
4878 | | |
4879 | | // If the built NestedNameSpecifier would be replacing an existing |
4880 | | // NestedNameSpecifier, use the number of component identifiers that |
4881 | | // would need to be changed as the edit distance instead of the number |
4882 | | // of components in the built NestedNameSpecifier. |
4883 | 78.0k | if (NNS && !CurNameSpecifierIdentifiers.empty()) { |
4884 | 14.2k | SmallVector<const IdentifierInfo*, 4> NewNameSpecifierIdentifiers; |
4885 | 14.2k | getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); |
4886 | 14.2k | NumSpecifiers = llvm::ComputeEditDistance( |
4887 | 14.2k | llvm::makeArrayRef(CurNameSpecifierIdentifiers), |
4888 | 14.2k | llvm::makeArrayRef(NewNameSpecifierIdentifiers)); |
4889 | 14.2k | } |
4890 | | |
4891 | 78.0k | SpecifierInfo SI = {Ctx, NNS, NumSpecifiers}; |
4892 | 78.0k | DistanceMap[NumSpecifiers].push_back(SI); |
4893 | 78.0k | } |
4894 | | |
4895 | | /// Perform name lookup for a possible result for typo correction. |
4896 | | static void LookupPotentialTypoResult(Sema &SemaRef, |
4897 | | LookupResult &Res, |
4898 | | IdentifierInfo *Name, |
4899 | | Scope *S, CXXScopeSpec *SS, |
4900 | | DeclContext *MemberContext, |
4901 | | bool EnteringContext, |
4902 | | bool isObjCIvarLookup, |
4903 | 22.6k | bool FindHidden) { |
4904 | 22.6k | Res.suppressDiagnostics(); |
4905 | 22.6k | Res.clear(); |
4906 | 22.6k | Res.setLookupName(Name); |
4907 | 22.6k | Res.setAllowHidden(FindHidden); |
4908 | 22.6k | if (MemberContext) { |
4909 | 598 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) { |
4910 | 31 | if (isObjCIvarLookup) { |
4911 | 13 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) { |
4912 | 13 | Res.addDecl(Ivar); |
4913 | 13 | Res.resolveKind(); |
4914 | 13 | return; |
4915 | 13 | } |
4916 | 13 | } |
4917 | | |
4918 | 18 | if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration( |
4919 | 18 | Name, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
4920 | 15 | Res.addDecl(Prop); |
4921 | 15 | Res.resolveKind(); |
4922 | 15 | return; |
4923 | 15 | } |
4924 | 18 | } |
4925 | | |
4926 | 570 | SemaRef.LookupQualifiedName(Res, MemberContext); |
4927 | 570 | return; |
4928 | 598 | } |
4929 | | |
4930 | 22.0k | SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, |
4931 | 22.0k | EnteringContext); |
4932 | | |
4933 | | // Fake ivar lookup; this should really be part of |
4934 | | // LookupParsedName. |
4935 | 22.0k | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
4936 | 316 | if (Method->isInstanceMethod() && Method->getClassInterface()299 && |
4937 | 316 | (289 Res.empty()289 || |
4938 | 289 | (24 Res.isSingleResult()24 && |
4939 | 283 | Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()24 ))) { |
4940 | 283 | if (ObjCIvarDecl *IV |
4941 | 283 | = Method->getClassInterface()->lookupInstanceVariable(Name)) { |
4942 | 36 | Res.addDecl(IV); |
4943 | 36 | Res.resolveKind(); |
4944 | 36 | } |
4945 | 283 | } |
4946 | 316 | } |
4947 | 22.0k | } |
4948 | | |
4949 | | /// Add keywords to the consumer as possible typo corrections. |
4950 | | static void AddKeywordsToConsumer(Sema &SemaRef, |
4951 | | TypoCorrectionConsumer &Consumer, |
4952 | | Scope *S, CorrectionCandidateCallback &CCC, |
4953 | 6.52k | bool AfterNestedNameSpecifier) { |
4954 | 6.52k | if (AfterNestedNameSpecifier) { |
4955 | | // For 'X::', we know exactly which keywords can appear next. |
4956 | 456 | Consumer.addKeywordResult("template"); |
4957 | 456 | if (CCC.WantExpressionKeywords) |
4958 | 291 | Consumer.addKeywordResult("operator"); |
4959 | 456 | return; |
4960 | 456 | } |
4961 | | |
4962 | 6.07k | if (CCC.WantObjCSuper) |
4963 | 5 | Consumer.addKeywordResult("super"); |
4964 | | |
4965 | 6.07k | if (CCC.WantTypeSpecifiers) { |
4966 | | // Add type-specifier keywords to the set of results. |
4967 | 1.54k | static const char *const CTypeSpecs[] = { |
4968 | 1.54k | "char", "const", "double", "enum", "float", "int", "long", "short", |
4969 | 1.54k | "signed", "struct", "union", "unsigned", "void", "volatile", |
4970 | 1.54k | "_Complex", "_Imaginary", |
4971 | | // storage-specifiers as well |
4972 | 1.54k | "extern", "inline", "static", "typedef" |
4973 | 1.54k | }; |
4974 | | |
4975 | 1.54k | const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs); |
4976 | 32.3k | for (unsigned I = 0; I != NumCTypeSpecs; ++I30.8k ) |
4977 | 30.8k | Consumer.addKeywordResult(CTypeSpecs[I]); |
4978 | | |
4979 | 1.54k | if (SemaRef.getLangOpts().C99) |
4980 | 535 | Consumer.addKeywordResult("restrict"); |
4981 | 1.54k | if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus397 ) |
4982 | 1.14k | Consumer.addKeywordResult("bool"); |
4983 | 397 | else if (SemaRef.getLangOpts().C99) |
4984 | 391 | Consumer.addKeywordResult("_Bool"); |
4985 | | |
4986 | 1.54k | if (SemaRef.getLangOpts().CPlusPlus) { |
4987 | 1.00k | Consumer.addKeywordResult("class"); |
4988 | 1.00k | Consumer.addKeywordResult("typename"); |
4989 | 1.00k | Consumer.addKeywordResult("wchar_t"); |
4990 | | |
4991 | 1.00k | if (SemaRef.getLangOpts().CPlusPlus11) { |
4992 | 839 | Consumer.addKeywordResult("char16_t"); |
4993 | 839 | Consumer.addKeywordResult("char32_t"); |
4994 | 839 | Consumer.addKeywordResult("constexpr"); |
4995 | 839 | Consumer.addKeywordResult("decltype"); |
4996 | 839 | Consumer.addKeywordResult("thread_local"); |
4997 | 839 | } |
4998 | 1.00k | } |
4999 | | |
5000 | 1.54k | if (SemaRef.getLangOpts().GNUKeywords) |
5001 | 748 | Consumer.addKeywordResult("typeof"); |
5002 | 4.53k | } else if (CCC.WantFunctionLikeCasts) { |
5003 | 474 | static const char *const CastableTypeSpecs[] = { |
5004 | 474 | "char", "double", "float", "int", "long", "short", |
5005 | 474 | "signed", "unsigned", "void" |
5006 | 474 | }; |
5007 | 474 | for (auto *kw : CastableTypeSpecs) |
5008 | 4.26k | Consumer.addKeywordResult(kw); |
5009 | 474 | } |
5010 | | |
5011 | 6.07k | if (CCC.WantCXXNamedCasts && SemaRef.getLangOpts().CPlusPlus4.14k ) { |
5012 | 3.03k | Consumer.addKeywordResult("const_cast"); |
5013 | 3.03k | Consumer.addKeywordResult("dynamic_cast"); |
5014 | 3.03k | Consumer.addKeywordResult("reinterpret_cast"); |
5015 | 3.03k | Consumer.addKeywordResult("static_cast"); |
5016 | 3.03k | } |
5017 | | |
5018 | 6.07k | if (CCC.WantExpressionKeywords) { |
5019 | 5.00k | Consumer.addKeywordResult("sizeof"); |
5020 | 5.00k | if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus1.08k ) { |
5021 | 3.91k | Consumer.addKeywordResult("false"); |
5022 | 3.91k | Consumer.addKeywordResult("true"); |
5023 | 3.91k | } |
5024 | | |
5025 | 5.00k | if (SemaRef.getLangOpts().CPlusPlus) { |
5026 | 3.67k | static const char *const CXXExprs[] = { |
5027 | 3.67k | "delete", "new", "operator", "throw", "typeid" |
5028 | 3.67k | }; |
5029 | 3.67k | const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs); |
5030 | 22.0k | for (unsigned I = 0; I != NumCXXExprs; ++I18.3k ) |
5031 | 18.3k | Consumer.addKeywordResult(CXXExprs[I]); |
5032 | | |
5033 | 3.67k | if (isa<CXXMethodDecl>(SemaRef.CurContext) && |
5034 | 3.67k | cast<CXXMethodDecl>(SemaRef.CurContext)->isInstance()295 ) |
5035 | 295 | Consumer.addKeywordResult("this"); |
5036 | | |
5037 | 3.67k | if (SemaRef.getLangOpts().CPlusPlus11) { |
5038 | 3.32k | Consumer.addKeywordResult("alignof"); |
5039 | 3.32k | Consumer.addKeywordResult("nullptr"); |
5040 | 3.32k | } |
5041 | 3.67k | } |
5042 | | |
5043 | 5.00k | if (SemaRef.getLangOpts().C11) { |
5044 | | // FIXME: We should not suggest _Alignof if the alignof macro |
5045 | | // is present. |
5046 | 1.04k | Consumer.addKeywordResult("_Alignof"); |
5047 | 1.04k | } |
5048 | 5.00k | } |
5049 | | |
5050 | 6.07k | if (CCC.WantRemainingKeywords) { |
|