1f4a2713aSLionel Sambuc //===--------------------- SemaLookup.cpp - Name Lookup ------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements name lookup for C, C++, Objective-C, and
11f4a2713aSLionel Sambuc // Objective-C++.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/AST/CXXInheritance.h"
17f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/DeclLookups.h"
20f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
21f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
22f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
23f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
24f4a2713aSLionel Sambuc #include "clang/Basic/Builtins.h"
25f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
26*0a6a1f1dSLionel Sambuc #include "clang/Lex/ModuleLoader.h"
27f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
28f4a2713aSLionel Sambuc #include "clang/Sema/ExternalSemaSource.h"
29f4a2713aSLionel Sambuc #include "clang/Sema/Overload.h"
30f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
31f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
32f4a2713aSLionel Sambuc #include "clang/Sema/Sema.h"
33f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
34f4a2713aSLionel Sambuc #include "clang/Sema/TemplateDeduction.h"
35f4a2713aSLionel Sambuc #include "clang/Sema/TypoCorrection.h"
36f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
37f4a2713aSLionel Sambuc #include "llvm/ADT/SetVector.h"
38f4a2713aSLionel Sambuc #include "llvm/ADT/SmallPtrSet.h"
39f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
40f4a2713aSLionel Sambuc #include "llvm/ADT/TinyPtrVector.h"
41f4a2713aSLionel Sambuc #include "llvm/ADT/edit_distance.h"
42f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
43f4a2713aSLionel Sambuc #include <algorithm>
44f4a2713aSLionel Sambuc #include <iterator>
45f4a2713aSLionel Sambuc #include <limits>
46f4a2713aSLionel Sambuc #include <list>
47f4a2713aSLionel Sambuc #include <map>
48f4a2713aSLionel Sambuc #include <set>
49f4a2713aSLionel Sambuc #include <utility>
50f4a2713aSLionel Sambuc #include <vector>
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc using namespace clang;
53f4a2713aSLionel Sambuc using namespace sema;
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc namespace {
56f4a2713aSLionel Sambuc class UnqualUsingEntry {
57f4a2713aSLionel Sambuc const DeclContext *Nominated;
58f4a2713aSLionel Sambuc const DeclContext *CommonAncestor;
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc public:
UnqualUsingEntry(const DeclContext * Nominated,const DeclContext * CommonAncestor)61f4a2713aSLionel Sambuc UnqualUsingEntry(const DeclContext *Nominated,
62f4a2713aSLionel Sambuc const DeclContext *CommonAncestor)
63f4a2713aSLionel Sambuc : Nominated(Nominated), CommonAncestor(CommonAncestor) {
64f4a2713aSLionel Sambuc }
65f4a2713aSLionel Sambuc
getCommonAncestor() const66f4a2713aSLionel Sambuc const DeclContext *getCommonAncestor() const {
67f4a2713aSLionel Sambuc return CommonAncestor;
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc
getNominatedNamespace() const70f4a2713aSLionel Sambuc const DeclContext *getNominatedNamespace() const {
71f4a2713aSLionel Sambuc return Nominated;
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc // Sort by the pointer value of the common ancestor.
75f4a2713aSLionel Sambuc struct Comparator {
operator ()__anonbd765e620111::UnqualUsingEntry::Comparator76f4a2713aSLionel Sambuc bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) {
77f4a2713aSLionel Sambuc return L.getCommonAncestor() < R.getCommonAncestor();
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
operator ()__anonbd765e620111::UnqualUsingEntry::Comparator80f4a2713aSLionel Sambuc bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) {
81f4a2713aSLionel Sambuc return E.getCommonAncestor() < DC;
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc
operator ()__anonbd765e620111::UnqualUsingEntry::Comparator84f4a2713aSLionel Sambuc bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) {
85f4a2713aSLionel Sambuc return DC < E.getCommonAncestor();
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc };
88f4a2713aSLionel Sambuc };
89f4a2713aSLionel Sambuc
90f4a2713aSLionel Sambuc /// A collection of using directives, as used by C++ unqualified
91f4a2713aSLionel Sambuc /// lookup.
92f4a2713aSLionel Sambuc class UnqualUsingDirectiveSet {
93f4a2713aSLionel Sambuc typedef SmallVector<UnqualUsingEntry, 8> ListTy;
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc ListTy list;
96f4a2713aSLionel Sambuc llvm::SmallPtrSet<DeclContext*, 8> visited;
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc public:
UnqualUsingDirectiveSet()99f4a2713aSLionel Sambuc UnqualUsingDirectiveSet() {}
100f4a2713aSLionel Sambuc
visitScopeChain(Scope * S,Scope * InnermostFileScope)101f4a2713aSLionel Sambuc void visitScopeChain(Scope *S, Scope *InnermostFileScope) {
102f4a2713aSLionel Sambuc // C++ [namespace.udir]p1:
103f4a2713aSLionel Sambuc // During unqualified name lookup, the names appear as if they
104f4a2713aSLionel Sambuc // were declared in the nearest enclosing namespace which contains
105f4a2713aSLionel Sambuc // both the using-directive and the nominated namespace.
106f4a2713aSLionel Sambuc DeclContext *InnermostFileDC = InnermostFileScope->getEntity();
107f4a2713aSLionel Sambuc assert(InnermostFileDC && InnermostFileDC->isFileContext());
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc for (; S; S = S->getParent()) {
110f4a2713aSLionel Sambuc // C++ [namespace.udir]p1:
111f4a2713aSLionel Sambuc // A using-directive shall not appear in class scope, but may
112f4a2713aSLionel Sambuc // appear in namespace scope or in block scope.
113f4a2713aSLionel Sambuc DeclContext *Ctx = S->getEntity();
114f4a2713aSLionel Sambuc if (Ctx && Ctx->isFileContext()) {
115f4a2713aSLionel Sambuc visit(Ctx, Ctx);
116f4a2713aSLionel Sambuc } else if (!Ctx || Ctx->isFunctionOrMethod()) {
117*0a6a1f1dSLionel Sambuc for (auto *I : S->using_directives())
118*0a6a1f1dSLionel Sambuc visit(I, InnermostFileDC);
119f4a2713aSLionel Sambuc }
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc // Visits a context and collect all of its using directives
124f4a2713aSLionel Sambuc // recursively. Treats all using directives as if they were
125f4a2713aSLionel Sambuc // declared in the context.
126f4a2713aSLionel Sambuc //
127f4a2713aSLionel Sambuc // A given context is only every visited once, so it is important
128f4a2713aSLionel Sambuc // that contexts be visited from the inside out in order to get
129f4a2713aSLionel Sambuc // the effective DCs right.
visit(DeclContext * DC,DeclContext * EffectiveDC)130f4a2713aSLionel Sambuc void visit(DeclContext *DC, DeclContext *EffectiveDC) {
131*0a6a1f1dSLionel Sambuc if (!visited.insert(DC).second)
132f4a2713aSLionel Sambuc return;
133f4a2713aSLionel Sambuc
134f4a2713aSLionel Sambuc addUsingDirectives(DC, EffectiveDC);
135f4a2713aSLionel Sambuc }
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc // Visits a using directive and collects all of its using
138f4a2713aSLionel Sambuc // directives recursively. Treats all using directives as if they
139f4a2713aSLionel Sambuc // were declared in the effective DC.
visit(UsingDirectiveDecl * UD,DeclContext * EffectiveDC)140f4a2713aSLionel Sambuc void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) {
141f4a2713aSLionel Sambuc DeclContext *NS = UD->getNominatedNamespace();
142*0a6a1f1dSLionel Sambuc if (!visited.insert(NS).second)
143f4a2713aSLionel Sambuc return;
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc addUsingDirective(UD, EffectiveDC);
146f4a2713aSLionel Sambuc addUsingDirectives(NS, EffectiveDC);
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc // Adds all the using directives in a context (and those nominated
150f4a2713aSLionel Sambuc // by its using directives, transitively) as if they appeared in
151f4a2713aSLionel Sambuc // the given effective context.
addUsingDirectives(DeclContext * DC,DeclContext * EffectiveDC)152f4a2713aSLionel Sambuc void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) {
153f4a2713aSLionel Sambuc SmallVector<DeclContext*,4> queue;
154f4a2713aSLionel Sambuc while (true) {
155*0a6a1f1dSLionel Sambuc for (auto UD : DC->using_directives()) {
156f4a2713aSLionel Sambuc DeclContext *NS = UD->getNominatedNamespace();
157*0a6a1f1dSLionel Sambuc if (visited.insert(NS).second) {
158f4a2713aSLionel Sambuc addUsingDirective(UD, EffectiveDC);
159f4a2713aSLionel Sambuc queue.push_back(NS);
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc if (queue.empty())
164f4a2713aSLionel Sambuc return;
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc DC = queue.pop_back_val();
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc
170f4a2713aSLionel Sambuc // Add a using directive as if it had been declared in the given
171f4a2713aSLionel Sambuc // context. This helps implement C++ [namespace.udir]p3:
172f4a2713aSLionel Sambuc // The using-directive is transitive: if a scope contains a
173f4a2713aSLionel Sambuc // using-directive that nominates a second namespace that itself
174f4a2713aSLionel Sambuc // contains using-directives, the effect is as if the
175f4a2713aSLionel Sambuc // using-directives from the second namespace also appeared in
176f4a2713aSLionel Sambuc // the first.
addUsingDirective(UsingDirectiveDecl * UD,DeclContext * EffectiveDC)177f4a2713aSLionel Sambuc void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) {
178f4a2713aSLionel Sambuc // Find the common ancestor between the effective context and
179f4a2713aSLionel Sambuc // the nominated namespace.
180f4a2713aSLionel Sambuc DeclContext *Common = UD->getNominatedNamespace();
181f4a2713aSLionel Sambuc while (!Common->Encloses(EffectiveDC))
182f4a2713aSLionel Sambuc Common = Common->getParent();
183f4a2713aSLionel Sambuc Common = Common->getPrimaryContext();
184f4a2713aSLionel Sambuc
185f4a2713aSLionel Sambuc list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common));
186f4a2713aSLionel Sambuc }
187f4a2713aSLionel Sambuc
done()188f4a2713aSLionel Sambuc void done() {
189f4a2713aSLionel Sambuc std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator());
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambuc typedef ListTy::const_iterator const_iterator;
193f4a2713aSLionel Sambuc
begin() const194f4a2713aSLionel Sambuc const_iterator begin() const { return list.begin(); }
end() const195f4a2713aSLionel Sambuc const_iterator end() const { return list.end(); }
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc std::pair<const_iterator,const_iterator>
getNamespacesFor(DeclContext * DC) const198f4a2713aSLionel Sambuc getNamespacesFor(DeclContext *DC) const {
199f4a2713aSLionel Sambuc return std::equal_range(begin(), end(), DC->getPrimaryContext(),
200f4a2713aSLionel Sambuc UnqualUsingEntry::Comparator());
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc };
203f4a2713aSLionel Sambuc }
204f4a2713aSLionel Sambuc
205f4a2713aSLionel Sambuc // Retrieve the set of identifier namespaces that correspond to a
206f4a2713aSLionel Sambuc // specific kind of name lookup.
getIDNS(Sema::LookupNameKind NameKind,bool CPlusPlus,bool Redeclaration)207f4a2713aSLionel Sambuc static inline unsigned getIDNS(Sema::LookupNameKind NameKind,
208f4a2713aSLionel Sambuc bool CPlusPlus,
209f4a2713aSLionel Sambuc bool Redeclaration) {
210f4a2713aSLionel Sambuc unsigned IDNS = 0;
211f4a2713aSLionel Sambuc switch (NameKind) {
212f4a2713aSLionel Sambuc case Sema::LookupObjCImplicitSelfParam:
213f4a2713aSLionel Sambuc case Sema::LookupOrdinaryName:
214f4a2713aSLionel Sambuc case Sema::LookupRedeclarationWithLinkage:
215f4a2713aSLionel Sambuc case Sema::LookupLocalFriendName:
216f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Ordinary;
217f4a2713aSLionel Sambuc if (CPlusPlus) {
218f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace;
219f4a2713aSLionel Sambuc if (Redeclaration)
220f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend;
221f4a2713aSLionel Sambuc }
222f4a2713aSLionel Sambuc if (Redeclaration)
223f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_LocalExtern;
224f4a2713aSLionel Sambuc break;
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc case Sema::LookupOperatorName:
227f4a2713aSLionel Sambuc // Operator lookup is its own crazy thing; it is not the same
228f4a2713aSLionel Sambuc // as (e.g.) looking up an operator name for redeclaration.
229f4a2713aSLionel Sambuc assert(!Redeclaration && "cannot do redeclaration operator lookup");
230f4a2713aSLionel Sambuc IDNS = Decl::IDNS_NonMemberOperator;
231f4a2713aSLionel Sambuc break;
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc case Sema::LookupTagName:
234f4a2713aSLionel Sambuc if (CPlusPlus) {
235f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Type;
236f4a2713aSLionel Sambuc
237f4a2713aSLionel Sambuc // When looking for a redeclaration of a tag name, we add:
238f4a2713aSLionel Sambuc // 1) TagFriend to find undeclared friend decls
239f4a2713aSLionel Sambuc // 2) Namespace because they can't "overload" with tag decls.
240f4a2713aSLionel Sambuc // 3) Tag because it includes class templates, which can't
241f4a2713aSLionel Sambuc // "overload" with tag decls.
242f4a2713aSLionel Sambuc if (Redeclaration)
243f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace;
244f4a2713aSLionel Sambuc } else {
245f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Tag;
246f4a2713aSLionel Sambuc }
247f4a2713aSLionel Sambuc break;
248*0a6a1f1dSLionel Sambuc
249f4a2713aSLionel Sambuc case Sema::LookupLabel:
250f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Label;
251f4a2713aSLionel Sambuc break;
252f4a2713aSLionel Sambuc
253f4a2713aSLionel Sambuc case Sema::LookupMemberName:
254f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Member;
255f4a2713aSLionel Sambuc if (CPlusPlus)
256f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary;
257f4a2713aSLionel Sambuc break;
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc case Sema::LookupNestedNameSpecifierName:
260f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace;
261f4a2713aSLionel Sambuc break;
262f4a2713aSLionel Sambuc
263f4a2713aSLionel Sambuc case Sema::LookupNamespaceName:
264f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Namespace;
265f4a2713aSLionel Sambuc break;
266f4a2713aSLionel Sambuc
267f4a2713aSLionel Sambuc case Sema::LookupUsingDeclName:
268*0a6a1f1dSLionel Sambuc assert(Redeclaration && "should only be used for redecl lookup");
269*0a6a1f1dSLionel Sambuc IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member |
270*0a6a1f1dSLionel Sambuc Decl::IDNS_Using | Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend |
271*0a6a1f1dSLionel Sambuc Decl::IDNS_LocalExtern;
272f4a2713aSLionel Sambuc break;
273f4a2713aSLionel Sambuc
274f4a2713aSLionel Sambuc case Sema::LookupObjCProtocolName:
275f4a2713aSLionel Sambuc IDNS = Decl::IDNS_ObjCProtocol;
276f4a2713aSLionel Sambuc break;
277f4a2713aSLionel Sambuc
278f4a2713aSLionel Sambuc case Sema::LookupAnyName:
279f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member
280f4a2713aSLionel Sambuc | Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol
281f4a2713aSLionel Sambuc | Decl::IDNS_Type;
282f4a2713aSLionel Sambuc break;
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc return IDNS;
285f4a2713aSLionel Sambuc }
286f4a2713aSLionel Sambuc
configure()287f4a2713aSLionel Sambuc void LookupResult::configure() {
288*0a6a1f1dSLionel Sambuc IDNS = getIDNS(LookupKind, getSema().getLangOpts().CPlusPlus,
289f4a2713aSLionel Sambuc isForRedeclaration());
290f4a2713aSLionel Sambuc
291f4a2713aSLionel Sambuc // If we're looking for one of the allocation or deallocation
292f4a2713aSLionel Sambuc // operators, make sure that the implicitly-declared new and delete
293f4a2713aSLionel Sambuc // operators can be found.
294f4a2713aSLionel Sambuc switch (NameInfo.getName().getCXXOverloadedOperator()) {
295f4a2713aSLionel Sambuc case OO_New:
296f4a2713aSLionel Sambuc case OO_Delete:
297f4a2713aSLionel Sambuc case OO_Array_New:
298f4a2713aSLionel Sambuc case OO_Array_Delete:
299*0a6a1f1dSLionel Sambuc getSema().DeclareGlobalNewDelete();
300f4a2713aSLionel Sambuc break;
301f4a2713aSLionel Sambuc
302f4a2713aSLionel Sambuc default:
303f4a2713aSLionel Sambuc break;
304f4a2713aSLionel Sambuc }
305f4a2713aSLionel Sambuc
306f4a2713aSLionel Sambuc // Compiler builtins are always visible, regardless of where they end
307f4a2713aSLionel Sambuc // up being declared.
308f4a2713aSLionel Sambuc if (IdentifierInfo *Id = NameInfo.getName().getAsIdentifierInfo()) {
309f4a2713aSLionel Sambuc if (unsigned BuiltinID = Id->getBuiltinID()) {
310*0a6a1f1dSLionel Sambuc if (!getSema().Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
311f4a2713aSLionel Sambuc AllowHidden = true;
312f4a2713aSLionel Sambuc }
313f4a2713aSLionel Sambuc }
314f4a2713aSLionel Sambuc }
315f4a2713aSLionel Sambuc
sanity() const316*0a6a1f1dSLionel Sambuc bool LookupResult::sanity() const {
317*0a6a1f1dSLionel Sambuc // This function is never called by NDEBUG builds.
318f4a2713aSLionel Sambuc assert(ResultKind != NotFound || Decls.size() == 0);
319f4a2713aSLionel Sambuc assert(ResultKind != Found || Decls.size() == 1);
320f4a2713aSLionel Sambuc assert(ResultKind != FoundOverloaded || Decls.size() > 1 ||
321f4a2713aSLionel Sambuc (Decls.size() == 1 &&
322f4a2713aSLionel Sambuc isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl())));
323f4a2713aSLionel Sambuc assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved());
324f4a2713aSLionel Sambuc assert(ResultKind != Ambiguous || Decls.size() > 1 ||
325f4a2713aSLionel Sambuc (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects ||
326f4a2713aSLionel Sambuc Ambiguity == AmbiguousBaseSubobjectTypes)));
327*0a6a1f1dSLionel Sambuc assert((Paths != nullptr) == (ResultKind == Ambiguous &&
328f4a2713aSLionel Sambuc (Ambiguity == AmbiguousBaseSubobjectTypes ||
329f4a2713aSLionel Sambuc Ambiguity == AmbiguousBaseSubobjects)));
330*0a6a1f1dSLionel Sambuc return true;
331f4a2713aSLionel Sambuc }
332f4a2713aSLionel Sambuc
333f4a2713aSLionel Sambuc // Necessary because CXXBasePaths is not complete in Sema.h
deletePaths(CXXBasePaths * Paths)334f4a2713aSLionel Sambuc void LookupResult::deletePaths(CXXBasePaths *Paths) {
335f4a2713aSLionel Sambuc delete Paths;
336f4a2713aSLionel Sambuc }
337f4a2713aSLionel Sambuc
338f4a2713aSLionel Sambuc /// Get a representative context for a declaration such that two declarations
339f4a2713aSLionel Sambuc /// will have the same context if they were found within the same scope.
getContextForScopeMatching(Decl * D)340f4a2713aSLionel Sambuc static DeclContext *getContextForScopeMatching(Decl *D) {
341f4a2713aSLionel Sambuc // For function-local declarations, use that function as the context. This
342f4a2713aSLionel Sambuc // doesn't account for scopes within the function; the caller must deal with
343f4a2713aSLionel Sambuc // those.
344f4a2713aSLionel Sambuc DeclContext *DC = D->getLexicalDeclContext();
345f4a2713aSLionel Sambuc if (DC->isFunctionOrMethod())
346f4a2713aSLionel Sambuc return DC;
347f4a2713aSLionel Sambuc
348f4a2713aSLionel Sambuc // Otherwise, look at the semantic context of the declaration. The
349f4a2713aSLionel Sambuc // declaration must have been found there.
350f4a2713aSLionel Sambuc return D->getDeclContext()->getRedeclContext();
351f4a2713aSLionel Sambuc }
352f4a2713aSLionel Sambuc
353f4a2713aSLionel Sambuc /// Resolves the result kind of this lookup.
resolveKind()354f4a2713aSLionel Sambuc void LookupResult::resolveKind() {
355f4a2713aSLionel Sambuc unsigned N = Decls.size();
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc // Fast case: no possible ambiguity.
358f4a2713aSLionel Sambuc if (N == 0) {
359f4a2713aSLionel Sambuc assert(ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation);
360f4a2713aSLionel Sambuc return;
361f4a2713aSLionel Sambuc }
362f4a2713aSLionel Sambuc
363f4a2713aSLionel Sambuc // If there's a single decl, we need to examine it to decide what
364f4a2713aSLionel Sambuc // kind of lookup this is.
365f4a2713aSLionel Sambuc if (N == 1) {
366f4a2713aSLionel Sambuc NamedDecl *D = (*Decls.begin())->getUnderlyingDecl();
367f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(D))
368f4a2713aSLionel Sambuc ResultKind = FoundOverloaded;
369f4a2713aSLionel Sambuc else if (isa<UnresolvedUsingValueDecl>(D))
370f4a2713aSLionel Sambuc ResultKind = FoundUnresolvedValue;
371f4a2713aSLionel Sambuc return;
372f4a2713aSLionel Sambuc }
373f4a2713aSLionel Sambuc
374f4a2713aSLionel Sambuc // Don't do any extra resolution if we've already resolved as ambiguous.
375f4a2713aSLionel Sambuc if (ResultKind == Ambiguous) return;
376f4a2713aSLionel Sambuc
377f4a2713aSLionel Sambuc llvm::SmallPtrSet<NamedDecl*, 16> Unique;
378f4a2713aSLionel Sambuc llvm::SmallPtrSet<QualType, 16> UniqueTypes;
379f4a2713aSLionel Sambuc
380f4a2713aSLionel Sambuc bool Ambiguous = false;
381f4a2713aSLionel Sambuc bool HasTag = false, HasFunction = false, HasNonFunction = false;
382f4a2713aSLionel Sambuc bool HasFunctionTemplate = false, HasUnresolved = false;
383f4a2713aSLionel Sambuc
384f4a2713aSLionel Sambuc unsigned UniqueTagIndex = 0;
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc unsigned I = 0;
387f4a2713aSLionel Sambuc while (I < N) {
388f4a2713aSLionel Sambuc NamedDecl *D = Decls[I]->getUnderlyingDecl();
389f4a2713aSLionel Sambuc D = cast<NamedDecl>(D->getCanonicalDecl());
390f4a2713aSLionel Sambuc
391f4a2713aSLionel Sambuc // Ignore an invalid declaration unless it's the only one left.
392f4a2713aSLionel Sambuc if (D->isInvalidDecl() && I < N-1) {
393f4a2713aSLionel Sambuc Decls[I] = Decls[--N];
394f4a2713aSLionel Sambuc continue;
395f4a2713aSLionel Sambuc }
396f4a2713aSLionel Sambuc
397f4a2713aSLionel Sambuc // Redeclarations of types via typedef can occur both within a scope
398f4a2713aSLionel Sambuc // and, through using declarations and directives, across scopes. There is
399f4a2713aSLionel Sambuc // no ambiguity if they all refer to the same type, so unique based on the
400f4a2713aSLionel Sambuc // canonical type.
401f4a2713aSLionel Sambuc if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
402f4a2713aSLionel Sambuc if (!TD->getDeclContext()->isRecord()) {
403*0a6a1f1dSLionel Sambuc QualType T = getSema().Context.getTypeDeclType(TD);
404*0a6a1f1dSLionel Sambuc if (!UniqueTypes.insert(getSema().Context.getCanonicalType(T)).second) {
405f4a2713aSLionel Sambuc // The type is not unique; pull something off the back and continue
406f4a2713aSLionel Sambuc // at this index.
407f4a2713aSLionel Sambuc Decls[I] = Decls[--N];
408f4a2713aSLionel Sambuc continue;
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc }
412f4a2713aSLionel Sambuc
413*0a6a1f1dSLionel Sambuc if (!Unique.insert(D).second) {
414f4a2713aSLionel Sambuc // If it's not unique, pull something off the back (and
415f4a2713aSLionel Sambuc // continue at this index).
416f4a2713aSLionel Sambuc Decls[I] = Decls[--N];
417f4a2713aSLionel Sambuc continue;
418f4a2713aSLionel Sambuc }
419f4a2713aSLionel Sambuc
420f4a2713aSLionel Sambuc // Otherwise, do some decl type analysis and then continue.
421f4a2713aSLionel Sambuc
422f4a2713aSLionel Sambuc if (isa<UnresolvedUsingValueDecl>(D)) {
423f4a2713aSLionel Sambuc HasUnresolved = true;
424f4a2713aSLionel Sambuc } else if (isa<TagDecl>(D)) {
425f4a2713aSLionel Sambuc if (HasTag)
426f4a2713aSLionel Sambuc Ambiguous = true;
427f4a2713aSLionel Sambuc UniqueTagIndex = I;
428f4a2713aSLionel Sambuc HasTag = true;
429f4a2713aSLionel Sambuc } else if (isa<FunctionTemplateDecl>(D)) {
430f4a2713aSLionel Sambuc HasFunction = true;
431f4a2713aSLionel Sambuc HasFunctionTemplate = true;
432f4a2713aSLionel Sambuc } else if (isa<FunctionDecl>(D)) {
433f4a2713aSLionel Sambuc HasFunction = true;
434f4a2713aSLionel Sambuc } else {
435f4a2713aSLionel Sambuc if (HasNonFunction)
436f4a2713aSLionel Sambuc Ambiguous = true;
437f4a2713aSLionel Sambuc HasNonFunction = true;
438f4a2713aSLionel Sambuc }
439f4a2713aSLionel Sambuc I++;
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc
442f4a2713aSLionel Sambuc // C++ [basic.scope.hiding]p2:
443f4a2713aSLionel Sambuc // A class name or enumeration name can be hidden by the name of
444f4a2713aSLionel Sambuc // an object, function, or enumerator declared in the same
445f4a2713aSLionel Sambuc // scope. If a class or enumeration name and an object, function,
446f4a2713aSLionel Sambuc // or enumerator are declared in the same scope (in any order)
447f4a2713aSLionel Sambuc // with the same name, the class or enumeration name is hidden
448f4a2713aSLionel Sambuc // wherever the object, function, or enumerator name is visible.
449f4a2713aSLionel Sambuc // But it's still an error if there are distinct tag types found,
450f4a2713aSLionel Sambuc // even if they're not visible. (ref?)
451f4a2713aSLionel Sambuc if (HideTags && HasTag && !Ambiguous &&
452f4a2713aSLionel Sambuc (HasFunction || HasNonFunction || HasUnresolved)) {
453f4a2713aSLionel Sambuc if (getContextForScopeMatching(Decls[UniqueTagIndex])->Equals(
454f4a2713aSLionel Sambuc getContextForScopeMatching(Decls[UniqueTagIndex ? 0 : N - 1])))
455f4a2713aSLionel Sambuc Decls[UniqueTagIndex] = Decls[--N];
456f4a2713aSLionel Sambuc else
457f4a2713aSLionel Sambuc Ambiguous = true;
458f4a2713aSLionel Sambuc }
459f4a2713aSLionel Sambuc
460f4a2713aSLionel Sambuc Decls.set_size(N);
461f4a2713aSLionel Sambuc
462f4a2713aSLionel Sambuc if (HasNonFunction && (HasFunction || HasUnresolved))
463f4a2713aSLionel Sambuc Ambiguous = true;
464f4a2713aSLionel Sambuc
465f4a2713aSLionel Sambuc if (Ambiguous)
466f4a2713aSLionel Sambuc setAmbiguous(LookupResult::AmbiguousReference);
467f4a2713aSLionel Sambuc else if (HasUnresolved)
468f4a2713aSLionel Sambuc ResultKind = LookupResult::FoundUnresolvedValue;
469f4a2713aSLionel Sambuc else if (N > 1 || HasFunctionTemplate)
470f4a2713aSLionel Sambuc ResultKind = LookupResult::FoundOverloaded;
471f4a2713aSLionel Sambuc else
472f4a2713aSLionel Sambuc ResultKind = LookupResult::Found;
473f4a2713aSLionel Sambuc }
474f4a2713aSLionel Sambuc
addDeclsFromBasePaths(const CXXBasePaths & P)475f4a2713aSLionel Sambuc void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) {
476f4a2713aSLionel Sambuc CXXBasePaths::const_paths_iterator I, E;
477f4a2713aSLionel Sambuc for (I = P.begin(), E = P.end(); I != E; ++I)
478f4a2713aSLionel Sambuc for (DeclContext::lookup_iterator DI = I->Decls.begin(),
479f4a2713aSLionel Sambuc DE = I->Decls.end(); DI != DE; ++DI)
480f4a2713aSLionel Sambuc addDecl(*DI);
481f4a2713aSLionel Sambuc }
482f4a2713aSLionel Sambuc
setAmbiguousBaseSubobjects(CXXBasePaths & P)483f4a2713aSLionel Sambuc void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) {
484f4a2713aSLionel Sambuc Paths = new CXXBasePaths;
485f4a2713aSLionel Sambuc Paths->swap(P);
486f4a2713aSLionel Sambuc addDeclsFromBasePaths(*Paths);
487f4a2713aSLionel Sambuc resolveKind();
488f4a2713aSLionel Sambuc setAmbiguous(AmbiguousBaseSubobjects);
489f4a2713aSLionel Sambuc }
490f4a2713aSLionel Sambuc
setAmbiguousBaseSubobjectTypes(CXXBasePaths & P)491f4a2713aSLionel Sambuc void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) {
492f4a2713aSLionel Sambuc Paths = new CXXBasePaths;
493f4a2713aSLionel Sambuc Paths->swap(P);
494f4a2713aSLionel Sambuc addDeclsFromBasePaths(*Paths);
495f4a2713aSLionel Sambuc resolveKind();
496f4a2713aSLionel Sambuc setAmbiguous(AmbiguousBaseSubobjectTypes);
497f4a2713aSLionel Sambuc }
498f4a2713aSLionel Sambuc
print(raw_ostream & Out)499f4a2713aSLionel Sambuc void LookupResult::print(raw_ostream &Out) {
500f4a2713aSLionel Sambuc Out << Decls.size() << " result(s)";
501f4a2713aSLionel Sambuc if (isAmbiguous()) Out << ", ambiguous";
502f4a2713aSLionel Sambuc if (Paths) Out << ", base paths present";
503f4a2713aSLionel Sambuc
504f4a2713aSLionel Sambuc for (iterator I = begin(), E = end(); I != E; ++I) {
505f4a2713aSLionel Sambuc Out << "\n";
506f4a2713aSLionel Sambuc (*I)->print(Out, 2);
507f4a2713aSLionel Sambuc }
508f4a2713aSLionel Sambuc }
509f4a2713aSLionel Sambuc
510f4a2713aSLionel Sambuc /// \brief Lookup a builtin function, when name lookup would otherwise
511f4a2713aSLionel Sambuc /// fail.
LookupBuiltin(Sema & S,LookupResult & R)512f4a2713aSLionel Sambuc static bool LookupBuiltin(Sema &S, LookupResult &R) {
513f4a2713aSLionel Sambuc Sema::LookupNameKind NameKind = R.getLookupKind();
514f4a2713aSLionel Sambuc
515f4a2713aSLionel Sambuc // If we didn't find a use of this identifier, and if the identifier
516f4a2713aSLionel Sambuc // corresponds to a compiler builtin, create the decl object for the builtin
517f4a2713aSLionel Sambuc // now, injecting it into translation unit scope, and return it.
518f4a2713aSLionel Sambuc if (NameKind == Sema::LookupOrdinaryName ||
519f4a2713aSLionel Sambuc NameKind == Sema::LookupRedeclarationWithLinkage) {
520f4a2713aSLionel Sambuc IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo();
521f4a2713aSLionel Sambuc if (II) {
522f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 && S.getLangOpts().GNUMode &&
523f4a2713aSLionel Sambuc II == S.getFloat128Identifier()) {
524f4a2713aSLionel Sambuc // libstdc++4.7's type_traits expects type __float128 to exist, so
525f4a2713aSLionel Sambuc // insert a dummy type to make that header build in gnu++11 mode.
526f4a2713aSLionel Sambuc R.addDecl(S.getASTContext().getFloat128StubType());
527f4a2713aSLionel Sambuc return true;
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc
530f4a2713aSLionel Sambuc // If this is a builtin on this (or all) targets, create the decl.
531f4a2713aSLionel Sambuc if (unsigned BuiltinID = II->getBuiltinID()) {
532f4a2713aSLionel Sambuc // In C++, we don't have any predefined library functions like
533f4a2713aSLionel Sambuc // 'malloc'. Instead, we'll just error.
534f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus &&
535f4a2713aSLionel Sambuc S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
536f4a2713aSLionel Sambuc return false;
537f4a2713aSLionel Sambuc
538f4a2713aSLionel Sambuc if (NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II,
539f4a2713aSLionel Sambuc BuiltinID, S.TUScope,
540f4a2713aSLionel Sambuc R.isForRedeclaration(),
541f4a2713aSLionel Sambuc R.getNameLoc())) {
542f4a2713aSLionel Sambuc R.addDecl(D);
543f4a2713aSLionel Sambuc return true;
544f4a2713aSLionel Sambuc }
545f4a2713aSLionel Sambuc }
546f4a2713aSLionel Sambuc }
547f4a2713aSLionel Sambuc }
548f4a2713aSLionel Sambuc
549f4a2713aSLionel Sambuc return false;
550f4a2713aSLionel Sambuc }
551f4a2713aSLionel Sambuc
552f4a2713aSLionel Sambuc /// \brief Determine whether we can declare a special member function within
553f4a2713aSLionel Sambuc /// the class at this point.
CanDeclareSpecialMemberFunction(const CXXRecordDecl * Class)554f4a2713aSLionel Sambuc static bool CanDeclareSpecialMemberFunction(const CXXRecordDecl *Class) {
555f4a2713aSLionel Sambuc // We need to have a definition for the class.
556f4a2713aSLionel Sambuc if (!Class->getDefinition() || Class->isDependentContext())
557f4a2713aSLionel Sambuc return false;
558f4a2713aSLionel Sambuc
559f4a2713aSLionel Sambuc // We can't be in the middle of defining the class.
560f4a2713aSLionel Sambuc return !Class->isBeingDefined();
561f4a2713aSLionel Sambuc }
562f4a2713aSLionel Sambuc
ForceDeclarationOfImplicitMembers(CXXRecordDecl * Class)563f4a2713aSLionel Sambuc void Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) {
564f4a2713aSLionel Sambuc if (!CanDeclareSpecialMemberFunction(Class))
565f4a2713aSLionel Sambuc return;
566f4a2713aSLionel Sambuc
567f4a2713aSLionel Sambuc // If the default constructor has not yet been declared, do so now.
568f4a2713aSLionel Sambuc if (Class->needsImplicitDefaultConstructor())
569f4a2713aSLionel Sambuc DeclareImplicitDefaultConstructor(Class);
570f4a2713aSLionel Sambuc
571f4a2713aSLionel Sambuc // If the copy constructor has not yet been declared, do so now.
572f4a2713aSLionel Sambuc if (Class->needsImplicitCopyConstructor())
573f4a2713aSLionel Sambuc DeclareImplicitCopyConstructor(Class);
574f4a2713aSLionel Sambuc
575f4a2713aSLionel Sambuc // If the copy assignment operator has not yet been declared, do so now.
576f4a2713aSLionel Sambuc if (Class->needsImplicitCopyAssignment())
577f4a2713aSLionel Sambuc DeclareImplicitCopyAssignment(Class);
578f4a2713aSLionel Sambuc
579f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11) {
580f4a2713aSLionel Sambuc // If the move constructor has not yet been declared, do so now.
581f4a2713aSLionel Sambuc if (Class->needsImplicitMoveConstructor())
582f4a2713aSLionel Sambuc DeclareImplicitMoveConstructor(Class); // might not actually do it
583f4a2713aSLionel Sambuc
584f4a2713aSLionel Sambuc // If the move assignment operator has not yet been declared, do so now.
585f4a2713aSLionel Sambuc if (Class->needsImplicitMoveAssignment())
586f4a2713aSLionel Sambuc DeclareImplicitMoveAssignment(Class); // might not actually do it
587f4a2713aSLionel Sambuc }
588f4a2713aSLionel Sambuc
589f4a2713aSLionel Sambuc // If the destructor has not yet been declared, do so now.
590f4a2713aSLionel Sambuc if (Class->needsImplicitDestructor())
591f4a2713aSLionel Sambuc DeclareImplicitDestructor(Class);
592f4a2713aSLionel Sambuc }
593f4a2713aSLionel Sambuc
594f4a2713aSLionel Sambuc /// \brief Determine whether this is the name of an implicitly-declared
595f4a2713aSLionel Sambuc /// special member function.
isImplicitlyDeclaredMemberFunctionName(DeclarationName Name)596f4a2713aSLionel Sambuc static bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) {
597f4a2713aSLionel Sambuc switch (Name.getNameKind()) {
598f4a2713aSLionel Sambuc case DeclarationName::CXXConstructorName:
599f4a2713aSLionel Sambuc case DeclarationName::CXXDestructorName:
600f4a2713aSLionel Sambuc return true;
601f4a2713aSLionel Sambuc
602f4a2713aSLionel Sambuc case DeclarationName::CXXOperatorName:
603f4a2713aSLionel Sambuc return Name.getCXXOverloadedOperator() == OO_Equal;
604f4a2713aSLionel Sambuc
605f4a2713aSLionel Sambuc default:
606f4a2713aSLionel Sambuc break;
607f4a2713aSLionel Sambuc }
608f4a2713aSLionel Sambuc
609f4a2713aSLionel Sambuc return false;
610f4a2713aSLionel Sambuc }
611f4a2713aSLionel Sambuc
612f4a2713aSLionel Sambuc /// \brief If there are any implicit member functions with the given name
613f4a2713aSLionel Sambuc /// that need to be declared in the given declaration context, do so.
DeclareImplicitMemberFunctionsWithName(Sema & S,DeclarationName Name,const DeclContext * DC)614f4a2713aSLionel Sambuc static void DeclareImplicitMemberFunctionsWithName(Sema &S,
615f4a2713aSLionel Sambuc DeclarationName Name,
616f4a2713aSLionel Sambuc const DeclContext *DC) {
617f4a2713aSLionel Sambuc if (!DC)
618f4a2713aSLionel Sambuc return;
619f4a2713aSLionel Sambuc
620f4a2713aSLionel Sambuc switch (Name.getNameKind()) {
621f4a2713aSLionel Sambuc case DeclarationName::CXXConstructorName:
622f4a2713aSLionel Sambuc if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC))
623f4a2713aSLionel Sambuc if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) {
624f4a2713aSLionel Sambuc CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record);
625f4a2713aSLionel Sambuc if (Record->needsImplicitDefaultConstructor())
626f4a2713aSLionel Sambuc S.DeclareImplicitDefaultConstructor(Class);
627f4a2713aSLionel Sambuc if (Record->needsImplicitCopyConstructor())
628f4a2713aSLionel Sambuc S.DeclareImplicitCopyConstructor(Class);
629f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 &&
630f4a2713aSLionel Sambuc Record->needsImplicitMoveConstructor())
631f4a2713aSLionel Sambuc S.DeclareImplicitMoveConstructor(Class);
632f4a2713aSLionel Sambuc }
633f4a2713aSLionel Sambuc break;
634f4a2713aSLionel Sambuc
635f4a2713aSLionel Sambuc case DeclarationName::CXXDestructorName:
636f4a2713aSLionel Sambuc if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC))
637f4a2713aSLionel Sambuc if (Record->getDefinition() && Record->needsImplicitDestructor() &&
638f4a2713aSLionel Sambuc CanDeclareSpecialMemberFunction(Record))
639f4a2713aSLionel Sambuc S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record));
640f4a2713aSLionel Sambuc break;
641f4a2713aSLionel Sambuc
642f4a2713aSLionel Sambuc case DeclarationName::CXXOperatorName:
643f4a2713aSLionel Sambuc if (Name.getCXXOverloadedOperator() != OO_Equal)
644f4a2713aSLionel Sambuc break;
645f4a2713aSLionel Sambuc
646f4a2713aSLionel Sambuc if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) {
647f4a2713aSLionel Sambuc if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) {
648f4a2713aSLionel Sambuc CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record);
649f4a2713aSLionel Sambuc if (Record->needsImplicitCopyAssignment())
650f4a2713aSLionel Sambuc S.DeclareImplicitCopyAssignment(Class);
651f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 &&
652f4a2713aSLionel Sambuc Record->needsImplicitMoveAssignment())
653f4a2713aSLionel Sambuc S.DeclareImplicitMoveAssignment(Class);
654f4a2713aSLionel Sambuc }
655f4a2713aSLionel Sambuc }
656f4a2713aSLionel Sambuc break;
657f4a2713aSLionel Sambuc
658f4a2713aSLionel Sambuc default:
659f4a2713aSLionel Sambuc break;
660f4a2713aSLionel Sambuc }
661f4a2713aSLionel Sambuc }
662f4a2713aSLionel Sambuc
663f4a2713aSLionel Sambuc // Adds all qualifying matches for a name within a decl context to the
664f4a2713aSLionel Sambuc // given lookup result. Returns true if any matches were found.
LookupDirect(Sema & S,LookupResult & R,const DeclContext * DC)665f4a2713aSLionel Sambuc static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) {
666f4a2713aSLionel Sambuc bool Found = false;
667f4a2713aSLionel Sambuc
668f4a2713aSLionel Sambuc // Lazily declare C++ special member functions.
669f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus)
670f4a2713aSLionel Sambuc DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), DC);
671f4a2713aSLionel Sambuc
672f4a2713aSLionel Sambuc // Perform lookup into this declaration context.
673f4a2713aSLionel Sambuc DeclContext::lookup_const_result DR = DC->lookup(R.getLookupName());
674f4a2713aSLionel Sambuc for (DeclContext::lookup_const_iterator I = DR.begin(), E = DR.end(); I != E;
675f4a2713aSLionel Sambuc ++I) {
676f4a2713aSLionel Sambuc NamedDecl *D = *I;
677f4a2713aSLionel Sambuc if ((D = R.getAcceptableDecl(D))) {
678f4a2713aSLionel Sambuc R.addDecl(D);
679f4a2713aSLionel Sambuc Found = true;
680f4a2713aSLionel Sambuc }
681f4a2713aSLionel Sambuc }
682f4a2713aSLionel Sambuc
683f4a2713aSLionel Sambuc if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R))
684f4a2713aSLionel Sambuc return true;
685f4a2713aSLionel Sambuc
686f4a2713aSLionel Sambuc if (R.getLookupName().getNameKind()
687f4a2713aSLionel Sambuc != DeclarationName::CXXConversionFunctionName ||
688f4a2713aSLionel Sambuc R.getLookupName().getCXXNameType()->isDependentType() ||
689f4a2713aSLionel Sambuc !isa<CXXRecordDecl>(DC))
690f4a2713aSLionel Sambuc return Found;
691f4a2713aSLionel Sambuc
692f4a2713aSLionel Sambuc // C++ [temp.mem]p6:
693f4a2713aSLionel Sambuc // A specialization of a conversion function template is not found by
694f4a2713aSLionel Sambuc // name lookup. Instead, any conversion function templates visible in the
695f4a2713aSLionel Sambuc // context of the use are considered. [...]
696f4a2713aSLionel Sambuc const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
697f4a2713aSLionel Sambuc if (!Record->isCompleteDefinition())
698f4a2713aSLionel Sambuc return Found;
699f4a2713aSLionel Sambuc
700f4a2713aSLionel Sambuc for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(),
701f4a2713aSLionel Sambuc UEnd = Record->conversion_end(); U != UEnd; ++U) {
702f4a2713aSLionel Sambuc FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U);
703f4a2713aSLionel Sambuc if (!ConvTemplate)
704f4a2713aSLionel Sambuc continue;
705f4a2713aSLionel Sambuc
706f4a2713aSLionel Sambuc // When we're performing lookup for the purposes of redeclaration, just
707f4a2713aSLionel Sambuc // add the conversion function template. When we deduce template
708f4a2713aSLionel Sambuc // arguments for specializations, we'll end up unifying the return
709f4a2713aSLionel Sambuc // type of the new declaration with the type of the function template.
710f4a2713aSLionel Sambuc if (R.isForRedeclaration()) {
711f4a2713aSLionel Sambuc R.addDecl(ConvTemplate);
712f4a2713aSLionel Sambuc Found = true;
713f4a2713aSLionel Sambuc continue;
714f4a2713aSLionel Sambuc }
715f4a2713aSLionel Sambuc
716f4a2713aSLionel Sambuc // C++ [temp.mem]p6:
717f4a2713aSLionel Sambuc // [...] For each such operator, if argument deduction succeeds
718f4a2713aSLionel Sambuc // (14.9.2.3), the resulting specialization is used as if found by
719f4a2713aSLionel Sambuc // name lookup.
720f4a2713aSLionel Sambuc //
721f4a2713aSLionel Sambuc // When referencing a conversion function for any purpose other than
722f4a2713aSLionel Sambuc // a redeclaration (such that we'll be building an expression with the
723f4a2713aSLionel Sambuc // result), perform template argument deduction and place the
724f4a2713aSLionel Sambuc // specialization into the result set. We do this to avoid forcing all
725f4a2713aSLionel Sambuc // callers to perform special deduction for conversion functions.
726f4a2713aSLionel Sambuc TemplateDeductionInfo Info(R.getNameLoc());
727*0a6a1f1dSLionel Sambuc FunctionDecl *Specialization = nullptr;
728f4a2713aSLionel Sambuc
729f4a2713aSLionel Sambuc const FunctionProtoType *ConvProto
730f4a2713aSLionel Sambuc = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>();
731f4a2713aSLionel Sambuc assert(ConvProto && "Nonsensical conversion function template type");
732f4a2713aSLionel Sambuc
733f4a2713aSLionel Sambuc // Compute the type of the function that we would expect the conversion
734f4a2713aSLionel Sambuc // function to have, if it were to match the name given.
735f4a2713aSLionel Sambuc // FIXME: Calling convention!
736f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = ConvProto->getExtProtoInfo();
737f4a2713aSLionel Sambuc EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC_C);
738*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec = EST_None;
739f4a2713aSLionel Sambuc QualType ExpectedType
740f4a2713aSLionel Sambuc = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(),
741f4a2713aSLionel Sambuc None, EPI);
742f4a2713aSLionel Sambuc
743f4a2713aSLionel Sambuc // Perform template argument deduction against the type that we would
744f4a2713aSLionel Sambuc // expect the function to have.
745*0a6a1f1dSLionel Sambuc if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType,
746f4a2713aSLionel Sambuc Specialization, Info)
747f4a2713aSLionel Sambuc == Sema::TDK_Success) {
748f4a2713aSLionel Sambuc R.addDecl(Specialization);
749f4a2713aSLionel Sambuc Found = true;
750f4a2713aSLionel Sambuc }
751f4a2713aSLionel Sambuc }
752f4a2713aSLionel Sambuc
753f4a2713aSLionel Sambuc return Found;
754f4a2713aSLionel Sambuc }
755f4a2713aSLionel Sambuc
756f4a2713aSLionel Sambuc // Performs C++ unqualified lookup into the given file context.
757f4a2713aSLionel Sambuc static bool
CppNamespaceLookup(Sema & S,LookupResult & R,ASTContext & Context,DeclContext * NS,UnqualUsingDirectiveSet & UDirs)758f4a2713aSLionel Sambuc CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context,
759f4a2713aSLionel Sambuc DeclContext *NS, UnqualUsingDirectiveSet &UDirs) {
760f4a2713aSLionel Sambuc
761f4a2713aSLionel Sambuc assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!");
762f4a2713aSLionel Sambuc
763f4a2713aSLionel Sambuc // Perform direct name lookup into the LookupCtx.
764f4a2713aSLionel Sambuc bool Found = LookupDirect(S, R, NS);
765f4a2713aSLionel Sambuc
766f4a2713aSLionel Sambuc // Perform direct name lookup into the namespaces nominated by the
767f4a2713aSLionel Sambuc // using directives whose common ancestor is this namespace.
768f4a2713aSLionel Sambuc UnqualUsingDirectiveSet::const_iterator UI, UEnd;
769*0a6a1f1dSLionel Sambuc std::tie(UI, UEnd) = UDirs.getNamespacesFor(NS);
770f4a2713aSLionel Sambuc
771f4a2713aSLionel Sambuc for (; UI != UEnd; ++UI)
772f4a2713aSLionel Sambuc if (LookupDirect(S, R, UI->getNominatedNamespace()))
773f4a2713aSLionel Sambuc Found = true;
774f4a2713aSLionel Sambuc
775f4a2713aSLionel Sambuc R.resolveKind();
776f4a2713aSLionel Sambuc
777f4a2713aSLionel Sambuc return Found;
778f4a2713aSLionel Sambuc }
779f4a2713aSLionel Sambuc
isNamespaceOrTranslationUnitScope(Scope * S)780f4a2713aSLionel Sambuc static bool isNamespaceOrTranslationUnitScope(Scope *S) {
781f4a2713aSLionel Sambuc if (DeclContext *Ctx = S->getEntity())
782f4a2713aSLionel Sambuc return Ctx->isFileContext();
783f4a2713aSLionel Sambuc return false;
784f4a2713aSLionel Sambuc }
785f4a2713aSLionel Sambuc
786f4a2713aSLionel Sambuc // Find the next outer declaration context from this scope. This
787f4a2713aSLionel Sambuc // routine actually returns the semantic outer context, which may
788f4a2713aSLionel Sambuc // differ from the lexical context (encoded directly in the Scope
789f4a2713aSLionel Sambuc // stack) when we are parsing a member of a class template. In this
790f4a2713aSLionel Sambuc // case, the second element of the pair will be true, to indicate that
791f4a2713aSLionel Sambuc // name lookup should continue searching in this semantic context when
792f4a2713aSLionel Sambuc // it leaves the current template parameter scope.
findOuterContext(Scope * S)793f4a2713aSLionel Sambuc static std::pair<DeclContext *, bool> findOuterContext(Scope *S) {
794f4a2713aSLionel Sambuc DeclContext *DC = S->getEntity();
795*0a6a1f1dSLionel Sambuc DeclContext *Lexical = nullptr;
796f4a2713aSLionel Sambuc for (Scope *OuterS = S->getParent(); OuterS;
797f4a2713aSLionel Sambuc OuterS = OuterS->getParent()) {
798f4a2713aSLionel Sambuc if (OuterS->getEntity()) {
799f4a2713aSLionel Sambuc Lexical = OuterS->getEntity();
800f4a2713aSLionel Sambuc break;
801f4a2713aSLionel Sambuc }
802f4a2713aSLionel Sambuc }
803f4a2713aSLionel Sambuc
804f4a2713aSLionel Sambuc // C++ [temp.local]p8:
805f4a2713aSLionel Sambuc // In the definition of a member of a class template that appears
806f4a2713aSLionel Sambuc // outside of the namespace containing the class template
807f4a2713aSLionel Sambuc // definition, the name of a template-parameter hides the name of
808f4a2713aSLionel Sambuc // a member of this namespace.
809f4a2713aSLionel Sambuc //
810f4a2713aSLionel Sambuc // Example:
811f4a2713aSLionel Sambuc //
812f4a2713aSLionel Sambuc // namespace N {
813f4a2713aSLionel Sambuc // class C { };
814f4a2713aSLionel Sambuc //
815f4a2713aSLionel Sambuc // template<class T> class B {
816f4a2713aSLionel Sambuc // void f(T);
817f4a2713aSLionel Sambuc // };
818f4a2713aSLionel Sambuc // }
819f4a2713aSLionel Sambuc //
820f4a2713aSLionel Sambuc // template<class C> void N::B<C>::f(C) {
821f4a2713aSLionel Sambuc // C b; // C is the template parameter, not N::C
822f4a2713aSLionel Sambuc // }
823f4a2713aSLionel Sambuc //
824f4a2713aSLionel Sambuc // In this example, the lexical context we return is the
825f4a2713aSLionel Sambuc // TranslationUnit, while the semantic context is the namespace N.
826f4a2713aSLionel Sambuc if (!Lexical || !DC || !S->getParent() ||
827f4a2713aSLionel Sambuc !S->getParent()->isTemplateParamScope())
828f4a2713aSLionel Sambuc return std::make_pair(Lexical, false);
829f4a2713aSLionel Sambuc
830f4a2713aSLionel Sambuc // Find the outermost template parameter scope.
831f4a2713aSLionel Sambuc // For the example, this is the scope for the template parameters of
832f4a2713aSLionel Sambuc // template<class C>.
833f4a2713aSLionel Sambuc Scope *OutermostTemplateScope = S->getParent();
834f4a2713aSLionel Sambuc while (OutermostTemplateScope->getParent() &&
835f4a2713aSLionel Sambuc OutermostTemplateScope->getParent()->isTemplateParamScope())
836f4a2713aSLionel Sambuc OutermostTemplateScope = OutermostTemplateScope->getParent();
837f4a2713aSLionel Sambuc
838f4a2713aSLionel Sambuc // Find the namespace context in which the original scope occurs. In
839f4a2713aSLionel Sambuc // the example, this is namespace N.
840f4a2713aSLionel Sambuc DeclContext *Semantic = DC;
841f4a2713aSLionel Sambuc while (!Semantic->isFileContext())
842f4a2713aSLionel Sambuc Semantic = Semantic->getParent();
843f4a2713aSLionel Sambuc
844f4a2713aSLionel Sambuc // Find the declaration context just outside of the template
845f4a2713aSLionel Sambuc // parameter scope. This is the context in which the template is
846f4a2713aSLionel Sambuc // being lexically declaration (a namespace context). In the
847f4a2713aSLionel Sambuc // example, this is the global scope.
848f4a2713aSLionel Sambuc if (Lexical->isFileContext() && !Lexical->Equals(Semantic) &&
849f4a2713aSLionel Sambuc Lexical->Encloses(Semantic))
850f4a2713aSLionel Sambuc return std::make_pair(Semantic, true);
851f4a2713aSLionel Sambuc
852f4a2713aSLionel Sambuc return std::make_pair(Lexical, false);
853f4a2713aSLionel Sambuc }
854f4a2713aSLionel Sambuc
855f4a2713aSLionel Sambuc namespace {
856f4a2713aSLionel Sambuc /// An RAII object to specify that we want to find block scope extern
857f4a2713aSLionel Sambuc /// declarations.
858f4a2713aSLionel Sambuc struct FindLocalExternScope {
FindLocalExternScope__anonbd765e620211::FindLocalExternScope859f4a2713aSLionel Sambuc FindLocalExternScope(LookupResult &R)
860f4a2713aSLionel Sambuc : R(R), OldFindLocalExtern(R.getIdentifierNamespace() &
861f4a2713aSLionel Sambuc Decl::IDNS_LocalExtern) {
862f4a2713aSLionel Sambuc R.setFindLocalExtern(R.getIdentifierNamespace() & Decl::IDNS_Ordinary);
863f4a2713aSLionel Sambuc }
restore__anonbd765e620211::FindLocalExternScope864f4a2713aSLionel Sambuc void restore() {
865f4a2713aSLionel Sambuc R.setFindLocalExtern(OldFindLocalExtern);
866f4a2713aSLionel Sambuc }
~FindLocalExternScope__anonbd765e620211::FindLocalExternScope867f4a2713aSLionel Sambuc ~FindLocalExternScope() {
868f4a2713aSLionel Sambuc restore();
869f4a2713aSLionel Sambuc }
870f4a2713aSLionel Sambuc LookupResult &R;
871f4a2713aSLionel Sambuc bool OldFindLocalExtern;
872f4a2713aSLionel Sambuc };
873f4a2713aSLionel Sambuc }
874f4a2713aSLionel Sambuc
CppLookupName(LookupResult & R,Scope * S)875f4a2713aSLionel Sambuc bool Sema::CppLookupName(LookupResult &R, Scope *S) {
876f4a2713aSLionel Sambuc assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup");
877f4a2713aSLionel Sambuc
878f4a2713aSLionel Sambuc DeclarationName Name = R.getLookupName();
879f4a2713aSLionel Sambuc Sema::LookupNameKind NameKind = R.getLookupKind();
880f4a2713aSLionel Sambuc
881f4a2713aSLionel Sambuc // If this is the name of an implicitly-declared special member function,
882f4a2713aSLionel Sambuc // go through the scope stack to implicitly declare
883f4a2713aSLionel Sambuc if (isImplicitlyDeclaredMemberFunctionName(Name)) {
884f4a2713aSLionel Sambuc for (Scope *PreS = S; PreS; PreS = PreS->getParent())
885f4a2713aSLionel Sambuc if (DeclContext *DC = PreS->getEntity())
886f4a2713aSLionel Sambuc DeclareImplicitMemberFunctionsWithName(*this, Name, DC);
887f4a2713aSLionel Sambuc }
888f4a2713aSLionel Sambuc
889f4a2713aSLionel Sambuc // Implicitly declare member functions with the name we're looking for, if in
890f4a2713aSLionel Sambuc // fact we are in a scope where it matters.
891f4a2713aSLionel Sambuc
892f4a2713aSLionel Sambuc Scope *Initial = S;
893f4a2713aSLionel Sambuc IdentifierResolver::iterator
894f4a2713aSLionel Sambuc I = IdResolver.begin(Name),
895f4a2713aSLionel Sambuc IEnd = IdResolver.end();
896f4a2713aSLionel Sambuc
897f4a2713aSLionel Sambuc // First we lookup local scope.
898f4a2713aSLionel Sambuc // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir]
899f4a2713aSLionel Sambuc // ...During unqualified name lookup (3.4.1), the names appear as if
900f4a2713aSLionel Sambuc // they were declared in the nearest enclosing namespace which contains
901f4a2713aSLionel Sambuc // both the using-directive and the nominated namespace.
902f4a2713aSLionel Sambuc // [Note: in this context, "contains" means "contains directly or
903f4a2713aSLionel Sambuc // indirectly".
904f4a2713aSLionel Sambuc //
905f4a2713aSLionel Sambuc // For example:
906f4a2713aSLionel Sambuc // namespace A { int i; }
907f4a2713aSLionel Sambuc // void foo() {
908f4a2713aSLionel Sambuc // int i;
909f4a2713aSLionel Sambuc // {
910f4a2713aSLionel Sambuc // using namespace A;
911f4a2713aSLionel Sambuc // ++i; // finds local 'i', A::i appears at global scope
912f4a2713aSLionel Sambuc // }
913f4a2713aSLionel Sambuc // }
914f4a2713aSLionel Sambuc //
915f4a2713aSLionel Sambuc UnqualUsingDirectiveSet UDirs;
916f4a2713aSLionel Sambuc bool VisitedUsingDirectives = false;
917f4a2713aSLionel Sambuc bool LeftStartingScope = false;
918*0a6a1f1dSLionel Sambuc DeclContext *OutsideOfTemplateParamDC = nullptr;
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc // When performing a scope lookup, we want to find local extern decls.
921f4a2713aSLionel Sambuc FindLocalExternScope FindLocals(R);
922f4a2713aSLionel Sambuc
923f4a2713aSLionel Sambuc for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) {
924f4a2713aSLionel Sambuc DeclContext *Ctx = S->getEntity();
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc // Check whether the IdResolver has anything in this scope.
927f4a2713aSLionel Sambuc bool Found = false;
928f4a2713aSLionel Sambuc for (; I != IEnd && S->isDeclScope(*I); ++I) {
929f4a2713aSLionel Sambuc if (NamedDecl *ND = R.getAcceptableDecl(*I)) {
930f4a2713aSLionel Sambuc if (NameKind == LookupRedeclarationWithLinkage) {
931f4a2713aSLionel Sambuc // Determine whether this (or a previous) declaration is
932f4a2713aSLionel Sambuc // out-of-scope.
933f4a2713aSLionel Sambuc if (!LeftStartingScope && !Initial->isDeclScope(*I))
934f4a2713aSLionel Sambuc LeftStartingScope = true;
935f4a2713aSLionel Sambuc
936f4a2713aSLionel Sambuc // If we found something outside of our starting scope that
937f4a2713aSLionel Sambuc // does not have linkage, skip it. If it's a template parameter,
938f4a2713aSLionel Sambuc // we still find it, so we can diagnose the invalid redeclaration.
939f4a2713aSLionel Sambuc if (LeftStartingScope && !((*I)->hasLinkage()) &&
940f4a2713aSLionel Sambuc !(*I)->isTemplateParameter()) {
941f4a2713aSLionel Sambuc R.setShadowed();
942f4a2713aSLionel Sambuc continue;
943f4a2713aSLionel Sambuc }
944f4a2713aSLionel Sambuc }
945f4a2713aSLionel Sambuc
946f4a2713aSLionel Sambuc Found = true;
947f4a2713aSLionel Sambuc R.addDecl(ND);
948f4a2713aSLionel Sambuc }
949f4a2713aSLionel Sambuc }
950f4a2713aSLionel Sambuc if (Found) {
951f4a2713aSLionel Sambuc R.resolveKind();
952f4a2713aSLionel Sambuc if (S->isClassScope())
953f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx))
954f4a2713aSLionel Sambuc R.setNamingClass(Record);
955f4a2713aSLionel Sambuc return true;
956f4a2713aSLionel Sambuc }
957f4a2713aSLionel Sambuc
958f4a2713aSLionel Sambuc if (NameKind == LookupLocalFriendName && !S->isClassScope()) {
959f4a2713aSLionel Sambuc // C++11 [class.friend]p11:
960f4a2713aSLionel Sambuc // If a friend declaration appears in a local class and the name
961f4a2713aSLionel Sambuc // specified is an unqualified name, a prior declaration is
962f4a2713aSLionel Sambuc // looked up without considering scopes that are outside the
963f4a2713aSLionel Sambuc // innermost enclosing non-class scope.
964f4a2713aSLionel Sambuc return false;
965f4a2713aSLionel Sambuc }
966f4a2713aSLionel Sambuc
967f4a2713aSLionel Sambuc if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC &&
968f4a2713aSLionel Sambuc S->getParent() && !S->getParent()->isTemplateParamScope()) {
969f4a2713aSLionel Sambuc // We've just searched the last template parameter scope and
970f4a2713aSLionel Sambuc // found nothing, so look into the contexts between the
971f4a2713aSLionel Sambuc // lexical and semantic declaration contexts returned by
972f4a2713aSLionel Sambuc // findOuterContext(). This implements the name lookup behavior
973f4a2713aSLionel Sambuc // of C++ [temp.local]p8.
974f4a2713aSLionel Sambuc Ctx = OutsideOfTemplateParamDC;
975*0a6a1f1dSLionel Sambuc OutsideOfTemplateParamDC = nullptr;
976f4a2713aSLionel Sambuc }
977f4a2713aSLionel Sambuc
978f4a2713aSLionel Sambuc if (Ctx) {
979f4a2713aSLionel Sambuc DeclContext *OuterCtx;
980f4a2713aSLionel Sambuc bool SearchAfterTemplateScope;
981*0a6a1f1dSLionel Sambuc std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S);
982f4a2713aSLionel Sambuc if (SearchAfterTemplateScope)
983f4a2713aSLionel Sambuc OutsideOfTemplateParamDC = OuterCtx;
984f4a2713aSLionel Sambuc
985f4a2713aSLionel Sambuc for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) {
986f4a2713aSLionel Sambuc // We do not directly look into transparent contexts, since
987f4a2713aSLionel Sambuc // those entities will be found in the nearest enclosing
988f4a2713aSLionel Sambuc // non-transparent context.
989f4a2713aSLionel Sambuc if (Ctx->isTransparentContext())
990f4a2713aSLionel Sambuc continue;
991f4a2713aSLionel Sambuc
992f4a2713aSLionel Sambuc // We do not look directly into function or method contexts,
993f4a2713aSLionel Sambuc // since all of the local variables and parameters of the
994f4a2713aSLionel Sambuc // function/method are present within the Scope.
995f4a2713aSLionel Sambuc if (Ctx->isFunctionOrMethod()) {
996f4a2713aSLionel Sambuc // If we have an Objective-C instance method, look for ivars
997f4a2713aSLionel Sambuc // in the corresponding interface.
998f4a2713aSLionel Sambuc if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) {
999f4a2713aSLionel Sambuc if (Method->isInstanceMethod() && Name.getAsIdentifierInfo())
1000f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *Class = Method->getClassInterface()) {
1001f4a2713aSLionel Sambuc ObjCInterfaceDecl *ClassDeclared;
1002f4a2713aSLionel Sambuc if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(
1003f4a2713aSLionel Sambuc Name.getAsIdentifierInfo(),
1004f4a2713aSLionel Sambuc ClassDeclared)) {
1005f4a2713aSLionel Sambuc if (NamedDecl *ND = R.getAcceptableDecl(Ivar)) {
1006f4a2713aSLionel Sambuc R.addDecl(ND);
1007f4a2713aSLionel Sambuc R.resolveKind();
1008f4a2713aSLionel Sambuc return true;
1009f4a2713aSLionel Sambuc }
1010f4a2713aSLionel Sambuc }
1011f4a2713aSLionel Sambuc }
1012f4a2713aSLionel Sambuc }
1013f4a2713aSLionel Sambuc
1014f4a2713aSLionel Sambuc continue;
1015f4a2713aSLionel Sambuc }
1016f4a2713aSLionel Sambuc
1017f4a2713aSLionel Sambuc // If this is a file context, we need to perform unqualified name
1018f4a2713aSLionel Sambuc // lookup considering using directives.
1019f4a2713aSLionel Sambuc if (Ctx->isFileContext()) {
1020f4a2713aSLionel Sambuc // If we haven't handled using directives yet, do so now.
1021f4a2713aSLionel Sambuc if (!VisitedUsingDirectives) {
1022f4a2713aSLionel Sambuc // Add using directives from this context up to the top level.
1023f4a2713aSLionel Sambuc for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent()) {
1024f4a2713aSLionel Sambuc if (UCtx->isTransparentContext())
1025f4a2713aSLionel Sambuc continue;
1026f4a2713aSLionel Sambuc
1027f4a2713aSLionel Sambuc UDirs.visit(UCtx, UCtx);
1028f4a2713aSLionel Sambuc }
1029f4a2713aSLionel Sambuc
1030f4a2713aSLionel Sambuc // Find the innermost file scope, so we can add using directives
1031f4a2713aSLionel Sambuc // from local scopes.
1032f4a2713aSLionel Sambuc Scope *InnermostFileScope = S;
1033f4a2713aSLionel Sambuc while (InnermostFileScope &&
1034f4a2713aSLionel Sambuc !isNamespaceOrTranslationUnitScope(InnermostFileScope))
1035f4a2713aSLionel Sambuc InnermostFileScope = InnermostFileScope->getParent();
1036f4a2713aSLionel Sambuc UDirs.visitScopeChain(Initial, InnermostFileScope);
1037f4a2713aSLionel Sambuc
1038f4a2713aSLionel Sambuc UDirs.done();
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc VisitedUsingDirectives = true;
1041f4a2713aSLionel Sambuc }
1042f4a2713aSLionel Sambuc
1043f4a2713aSLionel Sambuc if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) {
1044f4a2713aSLionel Sambuc R.resolveKind();
1045f4a2713aSLionel Sambuc return true;
1046f4a2713aSLionel Sambuc }
1047f4a2713aSLionel Sambuc
1048f4a2713aSLionel Sambuc continue;
1049f4a2713aSLionel Sambuc }
1050f4a2713aSLionel Sambuc
1051f4a2713aSLionel Sambuc // Perform qualified name lookup into this context.
1052f4a2713aSLionel Sambuc // FIXME: In some cases, we know that every name that could be found by
1053f4a2713aSLionel Sambuc // this qualified name lookup will also be on the identifier chain. For
1054f4a2713aSLionel Sambuc // example, inside a class without any base classes, we never need to
1055f4a2713aSLionel Sambuc // perform qualified lookup because all of the members are on top of the
1056f4a2713aSLionel Sambuc // identifier chain.
1057f4a2713aSLionel Sambuc if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true))
1058f4a2713aSLionel Sambuc return true;
1059f4a2713aSLionel Sambuc }
1060f4a2713aSLionel Sambuc }
1061f4a2713aSLionel Sambuc }
1062f4a2713aSLionel Sambuc
1063f4a2713aSLionel Sambuc // Stop if we ran out of scopes.
1064f4a2713aSLionel Sambuc // FIXME: This really, really shouldn't be happening.
1065f4a2713aSLionel Sambuc if (!S) return false;
1066f4a2713aSLionel Sambuc
1067f4a2713aSLionel Sambuc // If we are looking for members, no need to look into global/namespace scope.
1068f4a2713aSLionel Sambuc if (NameKind == LookupMemberName)
1069f4a2713aSLionel Sambuc return false;
1070f4a2713aSLionel Sambuc
1071f4a2713aSLionel Sambuc // Collect UsingDirectiveDecls in all scopes, and recursively all
1072f4a2713aSLionel Sambuc // nominated namespaces by those using-directives.
1073f4a2713aSLionel Sambuc //
1074f4a2713aSLionel Sambuc // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we
1075f4a2713aSLionel Sambuc // don't build it for each lookup!
1076f4a2713aSLionel Sambuc if (!VisitedUsingDirectives) {
1077f4a2713aSLionel Sambuc UDirs.visitScopeChain(Initial, S);
1078f4a2713aSLionel Sambuc UDirs.done();
1079f4a2713aSLionel Sambuc }
1080f4a2713aSLionel Sambuc
1081f4a2713aSLionel Sambuc // If we're not performing redeclaration lookup, do not look for local
1082f4a2713aSLionel Sambuc // extern declarations outside of a function scope.
1083f4a2713aSLionel Sambuc if (!R.isForRedeclaration())
1084f4a2713aSLionel Sambuc FindLocals.restore();
1085f4a2713aSLionel Sambuc
1086f4a2713aSLionel Sambuc // Lookup namespace scope, and global scope.
1087f4a2713aSLionel Sambuc // Unqualified name lookup in C++ requires looking into scopes
1088f4a2713aSLionel Sambuc // that aren't strictly lexical, and therefore we walk through the
1089f4a2713aSLionel Sambuc // context as well as walking through the scopes.
1090f4a2713aSLionel Sambuc for (; S; S = S->getParent()) {
1091f4a2713aSLionel Sambuc // Check whether the IdResolver has anything in this scope.
1092f4a2713aSLionel Sambuc bool Found = false;
1093f4a2713aSLionel Sambuc for (; I != IEnd && S->isDeclScope(*I); ++I) {
1094f4a2713aSLionel Sambuc if (NamedDecl *ND = R.getAcceptableDecl(*I)) {
1095f4a2713aSLionel Sambuc // We found something. Look for anything else in our scope
1096f4a2713aSLionel Sambuc // with this same name and in an acceptable identifier
1097f4a2713aSLionel Sambuc // namespace, so that we can construct an overload set if we
1098f4a2713aSLionel Sambuc // need to.
1099f4a2713aSLionel Sambuc Found = true;
1100f4a2713aSLionel Sambuc R.addDecl(ND);
1101f4a2713aSLionel Sambuc }
1102f4a2713aSLionel Sambuc }
1103f4a2713aSLionel Sambuc
1104f4a2713aSLionel Sambuc if (Found && S->isTemplateParamScope()) {
1105f4a2713aSLionel Sambuc R.resolveKind();
1106f4a2713aSLionel Sambuc return true;
1107f4a2713aSLionel Sambuc }
1108f4a2713aSLionel Sambuc
1109f4a2713aSLionel Sambuc DeclContext *Ctx = S->getEntity();
1110f4a2713aSLionel Sambuc if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC &&
1111f4a2713aSLionel Sambuc S->getParent() && !S->getParent()->isTemplateParamScope()) {
1112f4a2713aSLionel Sambuc // We've just searched the last template parameter scope and
1113f4a2713aSLionel Sambuc // found nothing, so look into the contexts between the
1114f4a2713aSLionel Sambuc // lexical and semantic declaration contexts returned by
1115f4a2713aSLionel Sambuc // findOuterContext(). This implements the name lookup behavior
1116f4a2713aSLionel Sambuc // of C++ [temp.local]p8.
1117f4a2713aSLionel Sambuc Ctx = OutsideOfTemplateParamDC;
1118*0a6a1f1dSLionel Sambuc OutsideOfTemplateParamDC = nullptr;
1119f4a2713aSLionel Sambuc }
1120f4a2713aSLionel Sambuc
1121f4a2713aSLionel Sambuc if (Ctx) {
1122f4a2713aSLionel Sambuc DeclContext *OuterCtx;
1123f4a2713aSLionel Sambuc bool SearchAfterTemplateScope;
1124*0a6a1f1dSLionel Sambuc std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S);
1125f4a2713aSLionel Sambuc if (SearchAfterTemplateScope)
1126f4a2713aSLionel Sambuc OutsideOfTemplateParamDC = OuterCtx;
1127f4a2713aSLionel Sambuc
1128f4a2713aSLionel Sambuc for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) {
1129f4a2713aSLionel Sambuc // We do not directly look into transparent contexts, since
1130f4a2713aSLionel Sambuc // those entities will be found in the nearest enclosing
1131f4a2713aSLionel Sambuc // non-transparent context.
1132f4a2713aSLionel Sambuc if (Ctx->isTransparentContext())
1133f4a2713aSLionel Sambuc continue;
1134f4a2713aSLionel Sambuc
1135f4a2713aSLionel Sambuc // If we have a context, and it's not a context stashed in the
1136f4a2713aSLionel Sambuc // template parameter scope for an out-of-line definition, also
1137f4a2713aSLionel Sambuc // look into that context.
1138f4a2713aSLionel Sambuc if (!(Found && S && S->isTemplateParamScope())) {
1139f4a2713aSLionel Sambuc assert(Ctx->isFileContext() &&
1140f4a2713aSLionel Sambuc "We should have been looking only at file context here already.");
1141f4a2713aSLionel Sambuc
1142f4a2713aSLionel Sambuc // Look into context considering using-directives.
1143f4a2713aSLionel Sambuc if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs))
1144f4a2713aSLionel Sambuc Found = true;
1145f4a2713aSLionel Sambuc }
1146f4a2713aSLionel Sambuc
1147f4a2713aSLionel Sambuc if (Found) {
1148f4a2713aSLionel Sambuc R.resolveKind();
1149f4a2713aSLionel Sambuc return true;
1150f4a2713aSLionel Sambuc }
1151f4a2713aSLionel Sambuc
1152f4a2713aSLionel Sambuc if (R.isForRedeclaration() && !Ctx->isTransparentContext())
1153f4a2713aSLionel Sambuc return false;
1154f4a2713aSLionel Sambuc }
1155f4a2713aSLionel Sambuc }
1156f4a2713aSLionel Sambuc
1157f4a2713aSLionel Sambuc if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext())
1158f4a2713aSLionel Sambuc return false;
1159f4a2713aSLionel Sambuc }
1160f4a2713aSLionel Sambuc
1161f4a2713aSLionel Sambuc return !R.empty();
1162f4a2713aSLionel Sambuc }
1163f4a2713aSLionel Sambuc
1164f4a2713aSLionel Sambuc /// \brief Find the declaration that a class temploid member specialization was
1165f4a2713aSLionel Sambuc /// instantiated from, or the member itself if it is an explicit specialization.
getInstantiatedFrom(Decl * D,MemberSpecializationInfo * MSInfo)1166f4a2713aSLionel Sambuc static Decl *getInstantiatedFrom(Decl *D, MemberSpecializationInfo *MSInfo) {
1167f4a2713aSLionel Sambuc return MSInfo->isExplicitSpecialization() ? D : MSInfo->getInstantiatedFrom();
1168f4a2713aSLionel Sambuc }
1169f4a2713aSLionel Sambuc
1170f4a2713aSLionel Sambuc /// \brief Find the module in which the given declaration was defined.
getDefiningModule(Decl * Entity)1171f4a2713aSLionel Sambuc static Module *getDefiningModule(Decl *Entity) {
1172f4a2713aSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Entity)) {
1173f4a2713aSLionel Sambuc // If this function was instantiated from a template, the defining module is
1174f4a2713aSLionel Sambuc // the module containing the pattern.
1175f4a2713aSLionel Sambuc if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern())
1176f4a2713aSLionel Sambuc Entity = Pattern;
1177f4a2713aSLionel Sambuc } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Entity)) {
1178*0a6a1f1dSLionel Sambuc if (CXXRecordDecl *Pattern = RD->getTemplateInstantiationPattern())
1179*0a6a1f1dSLionel Sambuc Entity = Pattern;
1180f4a2713aSLionel Sambuc } else if (EnumDecl *ED = dyn_cast<EnumDecl>(Entity)) {
1181f4a2713aSLionel Sambuc if (MemberSpecializationInfo *MSInfo = ED->getMemberSpecializationInfo())
1182f4a2713aSLionel Sambuc Entity = getInstantiatedFrom(ED, MSInfo);
1183f4a2713aSLionel Sambuc } else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) {
1184f4a2713aSLionel Sambuc // FIXME: Map from variable template specializations back to the template.
1185f4a2713aSLionel Sambuc if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo())
1186f4a2713aSLionel Sambuc Entity = getInstantiatedFrom(VD, MSInfo);
1187f4a2713aSLionel Sambuc }
1188f4a2713aSLionel Sambuc
1189f4a2713aSLionel Sambuc // Walk up to the containing context. That might also have been instantiated
1190f4a2713aSLionel Sambuc // from a template.
1191f4a2713aSLionel Sambuc DeclContext *Context = Entity->getDeclContext();
1192f4a2713aSLionel Sambuc if (Context->isFileContext())
1193f4a2713aSLionel Sambuc return Entity->getOwningModule();
1194f4a2713aSLionel Sambuc return getDefiningModule(cast<Decl>(Context));
1195f4a2713aSLionel Sambuc }
1196f4a2713aSLionel Sambuc
getLookupModules()1197f4a2713aSLionel Sambuc llvm::DenseSet<Module*> &Sema::getLookupModules() {
1198f4a2713aSLionel Sambuc unsigned N = ActiveTemplateInstantiations.size();
1199f4a2713aSLionel Sambuc for (unsigned I = ActiveTemplateInstantiationLookupModules.size();
1200f4a2713aSLionel Sambuc I != N; ++I) {
1201f4a2713aSLionel Sambuc Module *M = getDefiningModule(ActiveTemplateInstantiations[I].Entity);
1202f4a2713aSLionel Sambuc if (M && !LookupModulesCache.insert(M).second)
1203*0a6a1f1dSLionel Sambuc M = nullptr;
1204f4a2713aSLionel Sambuc ActiveTemplateInstantiationLookupModules.push_back(M);
1205f4a2713aSLionel Sambuc }
1206f4a2713aSLionel Sambuc return LookupModulesCache;
1207f4a2713aSLionel Sambuc }
1208f4a2713aSLionel Sambuc
1209f4a2713aSLionel Sambuc /// \brief Determine whether a declaration is visible to name lookup.
1210f4a2713aSLionel Sambuc ///
1211f4a2713aSLionel Sambuc /// This routine determines whether the declaration D is visible in the current
1212f4a2713aSLionel Sambuc /// lookup context, taking into account the current template instantiation
1213f4a2713aSLionel Sambuc /// stack. During template instantiation, a declaration is visible if it is
1214f4a2713aSLionel Sambuc /// visible from a module containing any entity on the template instantiation
1215f4a2713aSLionel Sambuc /// path (by instantiating a template, you allow it to see the declarations that
1216f4a2713aSLionel Sambuc /// your module can see, including those later on in your module).
isVisibleSlow(Sema & SemaRef,NamedDecl * D)1217f4a2713aSLionel Sambuc bool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) {
1218f4a2713aSLionel Sambuc assert(D->isHidden() && !SemaRef.ActiveTemplateInstantiations.empty() &&
1219f4a2713aSLionel Sambuc "should not call this: not in slow case");
1220f4a2713aSLionel Sambuc Module *DeclModule = D->getOwningModule();
1221f4a2713aSLionel Sambuc assert(DeclModule && "hidden decl not from a module");
1222f4a2713aSLionel Sambuc
1223f4a2713aSLionel Sambuc // Find the extra places where we need to look.
1224f4a2713aSLionel Sambuc llvm::DenseSet<Module*> &LookupModules = SemaRef.getLookupModules();
1225f4a2713aSLionel Sambuc if (LookupModules.empty())
1226f4a2713aSLionel Sambuc return false;
1227f4a2713aSLionel Sambuc
1228f4a2713aSLionel Sambuc // If our lookup set contains the decl's module, it's visible.
1229f4a2713aSLionel Sambuc if (LookupModules.count(DeclModule))
1230f4a2713aSLionel Sambuc return true;
1231f4a2713aSLionel Sambuc
1232f4a2713aSLionel Sambuc // If the declaration isn't exported, it's not visible in any other module.
1233f4a2713aSLionel Sambuc if (D->isModulePrivate())
1234f4a2713aSLionel Sambuc return false;
1235f4a2713aSLionel Sambuc
1236f4a2713aSLionel Sambuc // Check whether DeclModule is transitively exported to an import of
1237f4a2713aSLionel Sambuc // the lookup set.
1238f4a2713aSLionel Sambuc for (llvm::DenseSet<Module *>::iterator I = LookupModules.begin(),
1239f4a2713aSLionel Sambuc E = LookupModules.end();
1240f4a2713aSLionel Sambuc I != E; ++I)
1241f4a2713aSLionel Sambuc if ((*I)->isModuleVisible(DeclModule))
1242f4a2713aSLionel Sambuc return true;
1243f4a2713aSLionel Sambuc return false;
1244f4a2713aSLionel Sambuc }
1245f4a2713aSLionel Sambuc
1246f4a2713aSLionel Sambuc /// \brief Retrieve the visible declaration corresponding to D, if any.
1247f4a2713aSLionel Sambuc ///
1248f4a2713aSLionel Sambuc /// This routine determines whether the declaration D is visible in the current
1249f4a2713aSLionel Sambuc /// module, with the current imports. If not, it checks whether any
1250f4a2713aSLionel Sambuc /// redeclaration of D is visible, and if so, returns that declaration.
1251f4a2713aSLionel Sambuc ///
1252f4a2713aSLionel Sambuc /// \returns D, or a visible previous declaration of D, whichever is more recent
1253f4a2713aSLionel Sambuc /// and visible. If no declaration of D is visible, returns null.
findAcceptableDecl(Sema & SemaRef,NamedDecl * D)1254f4a2713aSLionel Sambuc static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
1255f4a2713aSLionel Sambuc assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
1256f4a2713aSLionel Sambuc
1257*0a6a1f1dSLionel Sambuc for (auto RD : D->redecls()) {
1258*0a6a1f1dSLionel Sambuc if (auto ND = dyn_cast<NamedDecl>(RD)) {
1259f4a2713aSLionel Sambuc if (LookupResult::isVisible(SemaRef, ND))
1260f4a2713aSLionel Sambuc return ND;
1261f4a2713aSLionel Sambuc }
1262f4a2713aSLionel Sambuc }
1263f4a2713aSLionel Sambuc
1264*0a6a1f1dSLionel Sambuc return nullptr;
1265f4a2713aSLionel Sambuc }
1266f4a2713aSLionel Sambuc
getAcceptableDeclSlow(NamedDecl * D) const1267f4a2713aSLionel Sambuc NamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl *D) const {
1268*0a6a1f1dSLionel Sambuc return findAcceptableDecl(getSema(), D);
1269f4a2713aSLionel Sambuc }
1270f4a2713aSLionel Sambuc
1271f4a2713aSLionel Sambuc /// @brief Perform unqualified name lookup starting from a given
1272f4a2713aSLionel Sambuc /// scope.
1273f4a2713aSLionel Sambuc ///
1274f4a2713aSLionel Sambuc /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
1275f4a2713aSLionel Sambuc /// used to find names within the current scope. For example, 'x' in
1276f4a2713aSLionel Sambuc /// @code
1277f4a2713aSLionel Sambuc /// int x;
1278f4a2713aSLionel Sambuc /// int f() {
1279f4a2713aSLionel Sambuc /// return x; // unqualified name look finds 'x' in the global scope
1280f4a2713aSLionel Sambuc /// }
1281f4a2713aSLionel Sambuc /// @endcode
1282f4a2713aSLionel Sambuc ///
1283f4a2713aSLionel Sambuc /// Different lookup criteria can find different names. For example, a
1284f4a2713aSLionel Sambuc /// particular scope can have both a struct and a function of the same
1285f4a2713aSLionel Sambuc /// name, and each can be found by certain lookup criteria. For more
1286f4a2713aSLionel Sambuc /// information about lookup criteria, see the documentation for the
1287f4a2713aSLionel Sambuc /// class LookupCriteria.
1288f4a2713aSLionel Sambuc ///
1289f4a2713aSLionel Sambuc /// @param S The scope from which unqualified name lookup will
1290f4a2713aSLionel Sambuc /// begin. If the lookup criteria permits, name lookup may also search
1291f4a2713aSLionel Sambuc /// in the parent scopes.
1292f4a2713aSLionel Sambuc ///
1293f4a2713aSLionel Sambuc /// @param [in,out] R Specifies the lookup to perform (e.g., the name to
1294f4a2713aSLionel Sambuc /// look up and the lookup kind), and is updated with the results of lookup
1295f4a2713aSLionel Sambuc /// including zero or more declarations and possibly additional information
1296f4a2713aSLionel Sambuc /// used to diagnose ambiguities.
1297f4a2713aSLionel Sambuc ///
1298f4a2713aSLionel Sambuc /// @returns \c true if lookup succeeded and false otherwise.
LookupName(LookupResult & R,Scope * S,bool AllowBuiltinCreation)1299f4a2713aSLionel Sambuc bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) {
1300f4a2713aSLionel Sambuc DeclarationName Name = R.getLookupName();
1301f4a2713aSLionel Sambuc if (!Name) return false;
1302f4a2713aSLionel Sambuc
1303f4a2713aSLionel Sambuc LookupNameKind NameKind = R.getLookupKind();
1304f4a2713aSLionel Sambuc
1305f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus) {
1306f4a2713aSLionel Sambuc // Unqualified name lookup in C/Objective-C is purely lexical, so
1307f4a2713aSLionel Sambuc // search in the declarations attached to the name.
1308f4a2713aSLionel Sambuc if (NameKind == Sema::LookupRedeclarationWithLinkage) {
1309f4a2713aSLionel Sambuc // Find the nearest non-transparent declaration scope.
1310f4a2713aSLionel Sambuc while (!(S->getFlags() & Scope::DeclScope) ||
1311f4a2713aSLionel Sambuc (S->getEntity() && S->getEntity()->isTransparentContext()))
1312f4a2713aSLionel Sambuc S = S->getParent();
1313f4a2713aSLionel Sambuc }
1314f4a2713aSLionel Sambuc
1315f4a2713aSLionel Sambuc // When performing a scope lookup, we want to find local extern decls.
1316f4a2713aSLionel Sambuc FindLocalExternScope FindLocals(R);
1317f4a2713aSLionel Sambuc
1318f4a2713aSLionel Sambuc // Scan up the scope chain looking for a decl that matches this
1319f4a2713aSLionel Sambuc // identifier that is in the appropriate namespace. This search
1320f4a2713aSLionel Sambuc // should not take long, as shadowing of names is uncommon, and
1321f4a2713aSLionel Sambuc // deep shadowing is extremely uncommon.
1322f4a2713aSLionel Sambuc bool LeftStartingScope = false;
1323f4a2713aSLionel Sambuc
1324f4a2713aSLionel Sambuc for (IdentifierResolver::iterator I = IdResolver.begin(Name),
1325f4a2713aSLionel Sambuc IEnd = IdResolver.end();
1326f4a2713aSLionel Sambuc I != IEnd; ++I)
1327f4a2713aSLionel Sambuc if (NamedDecl *D = R.getAcceptableDecl(*I)) {
1328f4a2713aSLionel Sambuc if (NameKind == LookupRedeclarationWithLinkage) {
1329f4a2713aSLionel Sambuc // Determine whether this (or a previous) declaration is
1330f4a2713aSLionel Sambuc // out-of-scope.
1331f4a2713aSLionel Sambuc if (!LeftStartingScope && !S->isDeclScope(*I))
1332f4a2713aSLionel Sambuc LeftStartingScope = true;
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc // If we found something outside of our starting scope that
1335f4a2713aSLionel Sambuc // does not have linkage, skip it.
1336f4a2713aSLionel Sambuc if (LeftStartingScope && !((*I)->hasLinkage())) {
1337f4a2713aSLionel Sambuc R.setShadowed();
1338f4a2713aSLionel Sambuc continue;
1339f4a2713aSLionel Sambuc }
1340f4a2713aSLionel Sambuc }
1341f4a2713aSLionel Sambuc else if (NameKind == LookupObjCImplicitSelfParam &&
1342f4a2713aSLionel Sambuc !isa<ImplicitParamDecl>(*I))
1343f4a2713aSLionel Sambuc continue;
1344f4a2713aSLionel Sambuc
1345f4a2713aSLionel Sambuc R.addDecl(D);
1346f4a2713aSLionel Sambuc
1347f4a2713aSLionel Sambuc // Check whether there are any other declarations with the same name
1348f4a2713aSLionel Sambuc // and in the same scope.
1349f4a2713aSLionel Sambuc if (I != IEnd) {
1350f4a2713aSLionel Sambuc // Find the scope in which this declaration was declared (if it
1351f4a2713aSLionel Sambuc // actually exists in a Scope).
1352f4a2713aSLionel Sambuc while (S && !S->isDeclScope(D))
1353f4a2713aSLionel Sambuc S = S->getParent();
1354f4a2713aSLionel Sambuc
1355f4a2713aSLionel Sambuc // If the scope containing the declaration is the translation unit,
1356f4a2713aSLionel Sambuc // then we'll need to perform our checks based on the matching
1357f4a2713aSLionel Sambuc // DeclContexts rather than matching scopes.
1358f4a2713aSLionel Sambuc if (S && isNamespaceOrTranslationUnitScope(S))
1359*0a6a1f1dSLionel Sambuc S = nullptr;
1360f4a2713aSLionel Sambuc
1361f4a2713aSLionel Sambuc // Compute the DeclContext, if we need it.
1362*0a6a1f1dSLionel Sambuc DeclContext *DC = nullptr;
1363f4a2713aSLionel Sambuc if (!S)
1364f4a2713aSLionel Sambuc DC = (*I)->getDeclContext()->getRedeclContext();
1365f4a2713aSLionel Sambuc
1366f4a2713aSLionel Sambuc IdentifierResolver::iterator LastI = I;
1367f4a2713aSLionel Sambuc for (++LastI; LastI != IEnd; ++LastI) {
1368f4a2713aSLionel Sambuc if (S) {
1369f4a2713aSLionel Sambuc // Match based on scope.
1370f4a2713aSLionel Sambuc if (!S->isDeclScope(*LastI))
1371f4a2713aSLionel Sambuc break;
1372f4a2713aSLionel Sambuc } else {
1373f4a2713aSLionel Sambuc // Match based on DeclContext.
1374f4a2713aSLionel Sambuc DeclContext *LastDC
1375f4a2713aSLionel Sambuc = (*LastI)->getDeclContext()->getRedeclContext();
1376f4a2713aSLionel Sambuc if (!LastDC->Equals(DC))
1377f4a2713aSLionel Sambuc break;
1378f4a2713aSLionel Sambuc }
1379f4a2713aSLionel Sambuc
1380f4a2713aSLionel Sambuc // If the declaration is in the right namespace and visible, add it.
1381f4a2713aSLionel Sambuc if (NamedDecl *LastD = R.getAcceptableDecl(*LastI))
1382f4a2713aSLionel Sambuc R.addDecl(LastD);
1383f4a2713aSLionel Sambuc }
1384f4a2713aSLionel Sambuc
1385f4a2713aSLionel Sambuc R.resolveKind();
1386f4a2713aSLionel Sambuc }
1387f4a2713aSLionel Sambuc
1388f4a2713aSLionel Sambuc return true;
1389f4a2713aSLionel Sambuc }
1390f4a2713aSLionel Sambuc } else {
1391f4a2713aSLionel Sambuc // Perform C++ unqualified name lookup.
1392f4a2713aSLionel Sambuc if (CppLookupName(R, S))
1393f4a2713aSLionel Sambuc return true;
1394f4a2713aSLionel Sambuc }
1395f4a2713aSLionel Sambuc
1396f4a2713aSLionel Sambuc // If we didn't find a use of this identifier, and if the identifier
1397f4a2713aSLionel Sambuc // corresponds to a compiler builtin, create the decl object for the builtin
1398f4a2713aSLionel Sambuc // now, injecting it into translation unit scope, and return it.
1399f4a2713aSLionel Sambuc if (AllowBuiltinCreation && LookupBuiltin(*this, R))
1400f4a2713aSLionel Sambuc return true;
1401f4a2713aSLionel Sambuc
1402f4a2713aSLionel Sambuc // If we didn't find a use of this identifier, the ExternalSource
1403f4a2713aSLionel Sambuc // may be able to handle the situation.
1404f4a2713aSLionel Sambuc // Note: some lookup failures are expected!
1405f4a2713aSLionel Sambuc // See e.g. R.isForRedeclaration().
1406f4a2713aSLionel Sambuc return (ExternalSource && ExternalSource->LookupUnqualified(R, S));
1407f4a2713aSLionel Sambuc }
1408f4a2713aSLionel Sambuc
1409f4a2713aSLionel Sambuc /// @brief Perform qualified name lookup in the namespaces nominated by
1410f4a2713aSLionel Sambuc /// using directives by the given context.
1411f4a2713aSLionel Sambuc ///
1412f4a2713aSLionel Sambuc /// C++98 [namespace.qual]p2:
1413f4a2713aSLionel Sambuc /// Given X::m (where X is a user-declared namespace), or given \::m
1414f4a2713aSLionel Sambuc /// (where X is the global namespace), let S be the set of all
1415f4a2713aSLionel Sambuc /// declarations of m in X and in the transitive closure of all
1416f4a2713aSLionel Sambuc /// namespaces nominated by using-directives in X and its used
1417f4a2713aSLionel Sambuc /// namespaces, except that using-directives are ignored in any
1418f4a2713aSLionel Sambuc /// namespace, including X, directly containing one or more
1419f4a2713aSLionel Sambuc /// declarations of m. No namespace is searched more than once in
1420f4a2713aSLionel Sambuc /// the lookup of a name. If S is the empty set, the program is
1421f4a2713aSLionel Sambuc /// ill-formed. Otherwise, if S has exactly one member, or if the
1422f4a2713aSLionel Sambuc /// context of the reference is a using-declaration
1423f4a2713aSLionel Sambuc /// (namespace.udecl), S is the required set of declarations of
1424f4a2713aSLionel Sambuc /// m. Otherwise if the use of m is not one that allows a unique
1425f4a2713aSLionel Sambuc /// declaration to be chosen from S, the program is ill-formed.
1426f4a2713aSLionel Sambuc ///
1427f4a2713aSLionel Sambuc /// C++98 [namespace.qual]p5:
1428f4a2713aSLionel Sambuc /// During the lookup of a qualified namespace member name, if the
1429f4a2713aSLionel Sambuc /// lookup finds more than one declaration of the member, and if one
1430f4a2713aSLionel Sambuc /// declaration introduces a class name or enumeration name and the
1431f4a2713aSLionel Sambuc /// other declarations either introduce the same object, the same
1432f4a2713aSLionel Sambuc /// enumerator or a set of functions, the non-type name hides the
1433f4a2713aSLionel Sambuc /// class or enumeration name if and only if the declarations are
1434f4a2713aSLionel Sambuc /// from the same namespace; otherwise (the declarations are from
1435f4a2713aSLionel Sambuc /// different namespaces), the program is ill-formed.
LookupQualifiedNameInUsingDirectives(Sema & S,LookupResult & R,DeclContext * StartDC)1436f4a2713aSLionel Sambuc static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R,
1437f4a2713aSLionel Sambuc DeclContext *StartDC) {
1438f4a2713aSLionel Sambuc assert(StartDC->isFileContext() && "start context is not a file context");
1439f4a2713aSLionel Sambuc
1440*0a6a1f1dSLionel Sambuc DeclContext::udir_range UsingDirectives = StartDC->using_directives();
1441*0a6a1f1dSLionel Sambuc if (UsingDirectives.begin() == UsingDirectives.end()) return false;
1442f4a2713aSLionel Sambuc
1443f4a2713aSLionel Sambuc // We have at least added all these contexts to the queue.
1444f4a2713aSLionel Sambuc llvm::SmallPtrSet<DeclContext*, 8> Visited;
1445f4a2713aSLionel Sambuc Visited.insert(StartDC);
1446f4a2713aSLionel Sambuc
1447f4a2713aSLionel Sambuc // We have not yet looked into these namespaces, much less added
1448f4a2713aSLionel Sambuc // their "using-children" to the queue.
1449f4a2713aSLionel Sambuc SmallVector<NamespaceDecl*, 8> Queue;
1450f4a2713aSLionel Sambuc
1451f4a2713aSLionel Sambuc // We have already looked into the initial namespace; seed the queue
1452f4a2713aSLionel Sambuc // with its using-children.
1453*0a6a1f1dSLionel Sambuc for (auto *I : UsingDirectives) {
1454*0a6a1f1dSLionel Sambuc NamespaceDecl *ND = I->getNominatedNamespace()->getOriginalNamespace();
1455*0a6a1f1dSLionel Sambuc if (Visited.insert(ND).second)
1456f4a2713aSLionel Sambuc Queue.push_back(ND);
1457f4a2713aSLionel Sambuc }
1458f4a2713aSLionel Sambuc
1459f4a2713aSLionel Sambuc // The easiest way to implement the restriction in [namespace.qual]p5
1460f4a2713aSLionel Sambuc // is to check whether any of the individual results found a tag
1461f4a2713aSLionel Sambuc // and, if so, to declare an ambiguity if the final result is not
1462f4a2713aSLionel Sambuc // a tag.
1463f4a2713aSLionel Sambuc bool FoundTag = false;
1464f4a2713aSLionel Sambuc bool FoundNonTag = false;
1465f4a2713aSLionel Sambuc
1466f4a2713aSLionel Sambuc LookupResult LocalR(LookupResult::Temporary, R);
1467f4a2713aSLionel Sambuc
1468f4a2713aSLionel Sambuc bool Found = false;
1469f4a2713aSLionel Sambuc while (!Queue.empty()) {
1470f4a2713aSLionel Sambuc NamespaceDecl *ND = Queue.pop_back_val();
1471f4a2713aSLionel Sambuc
1472f4a2713aSLionel Sambuc // We go through some convolutions here to avoid copying results
1473f4a2713aSLionel Sambuc // between LookupResults.
1474f4a2713aSLionel Sambuc bool UseLocal = !R.empty();
1475f4a2713aSLionel Sambuc LookupResult &DirectR = UseLocal ? LocalR : R;
1476f4a2713aSLionel Sambuc bool FoundDirect = LookupDirect(S, DirectR, ND);
1477f4a2713aSLionel Sambuc
1478f4a2713aSLionel Sambuc if (FoundDirect) {
1479f4a2713aSLionel Sambuc // First do any local hiding.
1480f4a2713aSLionel Sambuc DirectR.resolveKind();
1481f4a2713aSLionel Sambuc
1482f4a2713aSLionel Sambuc // If the local result is a tag, remember that.
1483f4a2713aSLionel Sambuc if (DirectR.isSingleTagDecl())
1484f4a2713aSLionel Sambuc FoundTag = true;
1485f4a2713aSLionel Sambuc else
1486f4a2713aSLionel Sambuc FoundNonTag = true;
1487f4a2713aSLionel Sambuc
1488f4a2713aSLionel Sambuc // Append the local results to the total results if necessary.
1489f4a2713aSLionel Sambuc if (UseLocal) {
1490f4a2713aSLionel Sambuc R.addAllDecls(LocalR);
1491f4a2713aSLionel Sambuc LocalR.clear();
1492f4a2713aSLionel Sambuc }
1493f4a2713aSLionel Sambuc }
1494f4a2713aSLionel Sambuc
1495f4a2713aSLionel Sambuc // If we find names in this namespace, ignore its using directives.
1496f4a2713aSLionel Sambuc if (FoundDirect) {
1497f4a2713aSLionel Sambuc Found = true;
1498f4a2713aSLionel Sambuc continue;
1499f4a2713aSLionel Sambuc }
1500f4a2713aSLionel Sambuc
1501*0a6a1f1dSLionel Sambuc for (auto I : ND->using_directives()) {
1502*0a6a1f1dSLionel Sambuc NamespaceDecl *Nom = I->getNominatedNamespace();
1503*0a6a1f1dSLionel Sambuc if (Visited.insert(Nom).second)
1504f4a2713aSLionel Sambuc Queue.push_back(Nom);
1505f4a2713aSLionel Sambuc }
1506f4a2713aSLionel Sambuc }
1507f4a2713aSLionel Sambuc
1508f4a2713aSLionel Sambuc if (Found) {
1509f4a2713aSLionel Sambuc if (FoundTag && FoundNonTag)
1510f4a2713aSLionel Sambuc R.setAmbiguousQualifiedTagHiding();
1511f4a2713aSLionel Sambuc else
1512f4a2713aSLionel Sambuc R.resolveKind();
1513f4a2713aSLionel Sambuc }
1514f4a2713aSLionel Sambuc
1515f4a2713aSLionel Sambuc return Found;
1516f4a2713aSLionel Sambuc }
1517f4a2713aSLionel Sambuc
1518f4a2713aSLionel Sambuc /// \brief Callback that looks for any member of a class with the given name.
LookupAnyMember(const CXXBaseSpecifier * Specifier,CXXBasePath & Path,void * Name)1519f4a2713aSLionel Sambuc static bool LookupAnyMember(const CXXBaseSpecifier *Specifier,
1520f4a2713aSLionel Sambuc CXXBasePath &Path,
1521f4a2713aSLionel Sambuc void *Name) {
1522f4a2713aSLionel Sambuc RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
1523f4a2713aSLionel Sambuc
1524f4a2713aSLionel Sambuc DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
1525f4a2713aSLionel Sambuc Path.Decls = BaseRecord->lookup(N);
1526f4a2713aSLionel Sambuc return !Path.Decls.empty();
1527f4a2713aSLionel Sambuc }
1528f4a2713aSLionel Sambuc
1529f4a2713aSLionel Sambuc /// \brief Determine whether the given set of member declarations contains only
1530f4a2713aSLionel Sambuc /// static members, nested types, and enumerators.
1531f4a2713aSLionel Sambuc template<typename InputIterator>
HasOnlyStaticMembers(InputIterator First,InputIterator Last)1532f4a2713aSLionel Sambuc static bool HasOnlyStaticMembers(InputIterator First, InputIterator Last) {
1533f4a2713aSLionel Sambuc Decl *D = (*First)->getUnderlyingDecl();
1534f4a2713aSLionel Sambuc if (isa<VarDecl>(D) || isa<TypeDecl>(D) || isa<EnumConstantDecl>(D))
1535f4a2713aSLionel Sambuc return true;
1536f4a2713aSLionel Sambuc
1537f4a2713aSLionel Sambuc if (isa<CXXMethodDecl>(D)) {
1538f4a2713aSLionel Sambuc // Determine whether all of the methods are static.
1539f4a2713aSLionel Sambuc bool AllMethodsAreStatic = true;
1540f4a2713aSLionel Sambuc for(; First != Last; ++First) {
1541f4a2713aSLionel Sambuc D = (*First)->getUnderlyingDecl();
1542f4a2713aSLionel Sambuc
1543f4a2713aSLionel Sambuc if (!isa<CXXMethodDecl>(D)) {
1544f4a2713aSLionel Sambuc assert(isa<TagDecl>(D) && "Non-function must be a tag decl");
1545f4a2713aSLionel Sambuc break;
1546f4a2713aSLionel Sambuc }
1547f4a2713aSLionel Sambuc
1548f4a2713aSLionel Sambuc if (!cast<CXXMethodDecl>(D)->isStatic()) {
1549f4a2713aSLionel Sambuc AllMethodsAreStatic = false;
1550f4a2713aSLionel Sambuc break;
1551f4a2713aSLionel Sambuc }
1552f4a2713aSLionel Sambuc }
1553f4a2713aSLionel Sambuc
1554f4a2713aSLionel Sambuc if (AllMethodsAreStatic)
1555f4a2713aSLionel Sambuc return true;
1556f4a2713aSLionel Sambuc }
1557f4a2713aSLionel Sambuc
1558f4a2713aSLionel Sambuc return false;
1559f4a2713aSLionel Sambuc }
1560f4a2713aSLionel Sambuc
1561f4a2713aSLionel Sambuc /// \brief Perform qualified name lookup into a given context.
1562f4a2713aSLionel Sambuc ///
1563f4a2713aSLionel Sambuc /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
1564f4a2713aSLionel Sambuc /// names when the context of those names is explicit specified, e.g.,
1565f4a2713aSLionel Sambuc /// "std::vector" or "x->member", or as part of unqualified name lookup.
1566f4a2713aSLionel Sambuc ///
1567f4a2713aSLionel Sambuc /// Different lookup criteria can find different names. For example, a
1568f4a2713aSLionel Sambuc /// particular scope can have both a struct and a function of the same
1569f4a2713aSLionel Sambuc /// name, and each can be found by certain lookup criteria. For more
1570f4a2713aSLionel Sambuc /// information about lookup criteria, see the documentation for the
1571f4a2713aSLionel Sambuc /// class LookupCriteria.
1572f4a2713aSLionel Sambuc ///
1573f4a2713aSLionel Sambuc /// \param R captures both the lookup criteria and any lookup results found.
1574f4a2713aSLionel Sambuc ///
1575f4a2713aSLionel Sambuc /// \param LookupCtx The context in which qualified name lookup will
1576f4a2713aSLionel Sambuc /// search. If the lookup criteria permits, name lookup may also search
1577f4a2713aSLionel Sambuc /// in the parent contexts or (for C++ classes) base classes.
1578f4a2713aSLionel Sambuc ///
1579f4a2713aSLionel Sambuc /// \param InUnqualifiedLookup true if this is qualified name lookup that
1580f4a2713aSLionel Sambuc /// occurs as part of unqualified name lookup.
1581f4a2713aSLionel Sambuc ///
1582f4a2713aSLionel Sambuc /// \returns true if lookup succeeded, false if it failed.
LookupQualifiedName(LookupResult & R,DeclContext * LookupCtx,bool InUnqualifiedLookup)1583f4a2713aSLionel Sambuc bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
1584f4a2713aSLionel Sambuc bool InUnqualifiedLookup) {
1585f4a2713aSLionel Sambuc assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
1586f4a2713aSLionel Sambuc
1587f4a2713aSLionel Sambuc if (!R.getLookupName())
1588f4a2713aSLionel Sambuc return false;
1589f4a2713aSLionel Sambuc
1590f4a2713aSLionel Sambuc // Make sure that the declaration context is complete.
1591f4a2713aSLionel Sambuc assert((!isa<TagDecl>(LookupCtx) ||
1592f4a2713aSLionel Sambuc LookupCtx->isDependentContext() ||
1593f4a2713aSLionel Sambuc cast<TagDecl>(LookupCtx)->isCompleteDefinition() ||
1594f4a2713aSLionel Sambuc cast<TagDecl>(LookupCtx)->isBeingDefined()) &&
1595f4a2713aSLionel Sambuc "Declaration context must already be complete!");
1596f4a2713aSLionel Sambuc
1597f4a2713aSLionel Sambuc // Perform qualified name lookup into the LookupCtx.
1598f4a2713aSLionel Sambuc if (LookupDirect(*this, R, LookupCtx)) {
1599f4a2713aSLionel Sambuc R.resolveKind();
1600f4a2713aSLionel Sambuc if (isa<CXXRecordDecl>(LookupCtx))
1601f4a2713aSLionel Sambuc R.setNamingClass(cast<CXXRecordDecl>(LookupCtx));
1602f4a2713aSLionel Sambuc return true;
1603f4a2713aSLionel Sambuc }
1604f4a2713aSLionel Sambuc
1605f4a2713aSLionel Sambuc // Don't descend into implied contexts for redeclarations.
1606f4a2713aSLionel Sambuc // C++98 [namespace.qual]p6:
1607f4a2713aSLionel Sambuc // In a declaration for a namespace member in which the
1608f4a2713aSLionel Sambuc // declarator-id is a qualified-id, given that the qualified-id
1609f4a2713aSLionel Sambuc // for the namespace member has the form
1610f4a2713aSLionel Sambuc // nested-name-specifier unqualified-id
1611f4a2713aSLionel Sambuc // the unqualified-id shall name a member of the namespace
1612f4a2713aSLionel Sambuc // designated by the nested-name-specifier.
1613f4a2713aSLionel Sambuc // See also [class.mfct]p5 and [class.static.data]p2.
1614f4a2713aSLionel Sambuc if (R.isForRedeclaration())
1615f4a2713aSLionel Sambuc return false;
1616f4a2713aSLionel Sambuc
1617f4a2713aSLionel Sambuc // If this is a namespace, look it up in the implied namespaces.
1618f4a2713aSLionel Sambuc if (LookupCtx->isFileContext())
1619f4a2713aSLionel Sambuc return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx);
1620f4a2713aSLionel Sambuc
1621f4a2713aSLionel Sambuc // If this isn't a C++ class, we aren't allowed to look into base
1622f4a2713aSLionel Sambuc // classes, we're done.
1623f4a2713aSLionel Sambuc CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx);
1624f4a2713aSLionel Sambuc if (!LookupRec || !LookupRec->getDefinition())
1625f4a2713aSLionel Sambuc return false;
1626f4a2713aSLionel Sambuc
1627f4a2713aSLionel Sambuc // If we're performing qualified name lookup into a dependent class,
1628f4a2713aSLionel Sambuc // then we are actually looking into a current instantiation. If we have any
1629f4a2713aSLionel Sambuc // dependent base classes, then we either have to delay lookup until
1630f4a2713aSLionel Sambuc // template instantiation time (at which point all bases will be available)
1631f4a2713aSLionel Sambuc // or we have to fail.
1632f4a2713aSLionel Sambuc if (!InUnqualifiedLookup && LookupRec->isDependentContext() &&
1633f4a2713aSLionel Sambuc LookupRec->hasAnyDependentBases()) {
1634f4a2713aSLionel Sambuc R.setNotFoundInCurrentInstantiation();
1635f4a2713aSLionel Sambuc return false;
1636f4a2713aSLionel Sambuc }
1637f4a2713aSLionel Sambuc
1638f4a2713aSLionel Sambuc // Perform lookup into our base classes.
1639f4a2713aSLionel Sambuc CXXBasePaths Paths;
1640f4a2713aSLionel Sambuc Paths.setOrigin(LookupRec);
1641f4a2713aSLionel Sambuc
1642f4a2713aSLionel Sambuc // Look for this member in our base classes
1643*0a6a1f1dSLionel Sambuc CXXRecordDecl::BaseMatchesCallback *BaseCallback = nullptr;
1644f4a2713aSLionel Sambuc switch (R.getLookupKind()) {
1645f4a2713aSLionel Sambuc case LookupObjCImplicitSelfParam:
1646f4a2713aSLionel Sambuc case LookupOrdinaryName:
1647f4a2713aSLionel Sambuc case LookupMemberName:
1648f4a2713aSLionel Sambuc case LookupRedeclarationWithLinkage:
1649f4a2713aSLionel Sambuc case LookupLocalFriendName:
1650f4a2713aSLionel Sambuc BaseCallback = &CXXRecordDecl::FindOrdinaryMember;
1651f4a2713aSLionel Sambuc break;
1652f4a2713aSLionel Sambuc
1653f4a2713aSLionel Sambuc case LookupTagName:
1654f4a2713aSLionel Sambuc BaseCallback = &CXXRecordDecl::FindTagMember;
1655f4a2713aSLionel Sambuc break;
1656f4a2713aSLionel Sambuc
1657f4a2713aSLionel Sambuc case LookupAnyName:
1658f4a2713aSLionel Sambuc BaseCallback = &LookupAnyMember;
1659f4a2713aSLionel Sambuc break;
1660f4a2713aSLionel Sambuc
1661f4a2713aSLionel Sambuc case LookupUsingDeclName:
1662f4a2713aSLionel Sambuc // This lookup is for redeclarations only.
1663f4a2713aSLionel Sambuc
1664f4a2713aSLionel Sambuc case LookupOperatorName:
1665f4a2713aSLionel Sambuc case LookupNamespaceName:
1666f4a2713aSLionel Sambuc case LookupObjCProtocolName:
1667f4a2713aSLionel Sambuc case LookupLabel:
1668f4a2713aSLionel Sambuc // These lookups will never find a member in a C++ class (or base class).
1669f4a2713aSLionel Sambuc return false;
1670f4a2713aSLionel Sambuc
1671f4a2713aSLionel Sambuc case LookupNestedNameSpecifierName:
1672f4a2713aSLionel Sambuc BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember;
1673f4a2713aSLionel Sambuc break;
1674f4a2713aSLionel Sambuc }
1675f4a2713aSLionel Sambuc
1676f4a2713aSLionel Sambuc if (!LookupRec->lookupInBases(BaseCallback,
1677f4a2713aSLionel Sambuc R.getLookupName().getAsOpaquePtr(), Paths))
1678f4a2713aSLionel Sambuc return false;
1679f4a2713aSLionel Sambuc
1680f4a2713aSLionel Sambuc R.setNamingClass(LookupRec);
1681f4a2713aSLionel Sambuc
1682f4a2713aSLionel Sambuc // C++ [class.member.lookup]p2:
1683f4a2713aSLionel Sambuc // [...] If the resulting set of declarations are not all from
1684f4a2713aSLionel Sambuc // sub-objects of the same type, or the set has a nonstatic member
1685f4a2713aSLionel Sambuc // and includes members from distinct sub-objects, there is an
1686f4a2713aSLionel Sambuc // ambiguity and the program is ill-formed. Otherwise that set is
1687f4a2713aSLionel Sambuc // the result of the lookup.
1688f4a2713aSLionel Sambuc QualType SubobjectType;
1689f4a2713aSLionel Sambuc int SubobjectNumber = 0;
1690f4a2713aSLionel Sambuc AccessSpecifier SubobjectAccess = AS_none;
1691f4a2713aSLionel Sambuc
1692f4a2713aSLionel Sambuc for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
1693f4a2713aSLionel Sambuc Path != PathEnd; ++Path) {
1694f4a2713aSLionel Sambuc const CXXBasePathElement &PathElement = Path->back();
1695f4a2713aSLionel Sambuc
1696f4a2713aSLionel Sambuc // Pick the best (i.e. most permissive i.e. numerically lowest) access
1697f4a2713aSLionel Sambuc // across all paths.
1698f4a2713aSLionel Sambuc SubobjectAccess = std::min(SubobjectAccess, Path->Access);
1699f4a2713aSLionel Sambuc
1700f4a2713aSLionel Sambuc // Determine whether we're looking at a distinct sub-object or not.
1701f4a2713aSLionel Sambuc if (SubobjectType.isNull()) {
1702f4a2713aSLionel Sambuc // This is the first subobject we've looked at. Record its type.
1703f4a2713aSLionel Sambuc SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
1704f4a2713aSLionel Sambuc SubobjectNumber = PathElement.SubobjectNumber;
1705f4a2713aSLionel Sambuc continue;
1706f4a2713aSLionel Sambuc }
1707f4a2713aSLionel Sambuc
1708f4a2713aSLionel Sambuc if (SubobjectType
1709f4a2713aSLionel Sambuc != Context.getCanonicalType(PathElement.Base->getType())) {
1710f4a2713aSLionel Sambuc // We found members of the given name in two subobjects of
1711f4a2713aSLionel Sambuc // different types. If the declaration sets aren't the same, this
1712*0a6a1f1dSLionel Sambuc // lookup is ambiguous.
1713f4a2713aSLionel Sambuc if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) {
1714f4a2713aSLionel Sambuc CXXBasePaths::paths_iterator FirstPath = Paths.begin();
1715f4a2713aSLionel Sambuc DeclContext::lookup_iterator FirstD = FirstPath->Decls.begin();
1716f4a2713aSLionel Sambuc DeclContext::lookup_iterator CurrentD = Path->Decls.begin();
1717f4a2713aSLionel Sambuc
1718f4a2713aSLionel Sambuc while (FirstD != FirstPath->Decls.end() &&
1719f4a2713aSLionel Sambuc CurrentD != Path->Decls.end()) {
1720f4a2713aSLionel Sambuc if ((*FirstD)->getUnderlyingDecl()->getCanonicalDecl() !=
1721f4a2713aSLionel Sambuc (*CurrentD)->getUnderlyingDecl()->getCanonicalDecl())
1722f4a2713aSLionel Sambuc break;
1723f4a2713aSLionel Sambuc
1724f4a2713aSLionel Sambuc ++FirstD;
1725f4a2713aSLionel Sambuc ++CurrentD;
1726f4a2713aSLionel Sambuc }
1727f4a2713aSLionel Sambuc
1728f4a2713aSLionel Sambuc if (FirstD == FirstPath->Decls.end() &&
1729f4a2713aSLionel Sambuc CurrentD == Path->Decls.end())
1730f4a2713aSLionel Sambuc continue;
1731f4a2713aSLionel Sambuc }
1732f4a2713aSLionel Sambuc
1733f4a2713aSLionel Sambuc R.setAmbiguousBaseSubobjectTypes(Paths);
1734f4a2713aSLionel Sambuc return true;
1735f4a2713aSLionel Sambuc }
1736f4a2713aSLionel Sambuc
1737f4a2713aSLionel Sambuc if (SubobjectNumber != PathElement.SubobjectNumber) {
1738f4a2713aSLionel Sambuc // We have a different subobject of the same type.
1739f4a2713aSLionel Sambuc
1740f4a2713aSLionel Sambuc // C++ [class.member.lookup]p5:
1741f4a2713aSLionel Sambuc // A static member, a nested type or an enumerator defined in
1742f4a2713aSLionel Sambuc // a base class T can unambiguously be found even if an object
1743f4a2713aSLionel Sambuc // has more than one base class subobject of type T.
1744f4a2713aSLionel Sambuc if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end()))
1745f4a2713aSLionel Sambuc continue;
1746f4a2713aSLionel Sambuc
1747f4a2713aSLionel Sambuc // We have found a nonstatic member name in multiple, distinct
1748f4a2713aSLionel Sambuc // subobjects. Name lookup is ambiguous.
1749f4a2713aSLionel Sambuc R.setAmbiguousBaseSubobjects(Paths);
1750f4a2713aSLionel Sambuc return true;
1751f4a2713aSLionel Sambuc }
1752f4a2713aSLionel Sambuc }
1753f4a2713aSLionel Sambuc
1754f4a2713aSLionel Sambuc // Lookup in a base class succeeded; return these results.
1755f4a2713aSLionel Sambuc
1756*0a6a1f1dSLionel Sambuc for (auto *D : Paths.front().Decls) {
1757f4a2713aSLionel Sambuc AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess,
1758f4a2713aSLionel Sambuc D->getAccess());
1759f4a2713aSLionel Sambuc R.addDecl(D, AS);
1760f4a2713aSLionel Sambuc }
1761f4a2713aSLionel Sambuc R.resolveKind();
1762f4a2713aSLionel Sambuc return true;
1763f4a2713aSLionel Sambuc }
1764f4a2713aSLionel Sambuc
1765*0a6a1f1dSLionel Sambuc /// \brief Performs qualified name lookup or special type of lookup for
1766*0a6a1f1dSLionel Sambuc /// "__super::" scope specifier.
1767*0a6a1f1dSLionel Sambuc ///
1768*0a6a1f1dSLionel Sambuc /// This routine is a convenience overload meant to be called from contexts
1769*0a6a1f1dSLionel Sambuc /// that need to perform a qualified name lookup with an optional C++ scope
1770*0a6a1f1dSLionel Sambuc /// specifier that might require special kind of lookup.
1771*0a6a1f1dSLionel Sambuc ///
1772*0a6a1f1dSLionel Sambuc /// \param R captures both the lookup criteria and any lookup results found.
1773*0a6a1f1dSLionel Sambuc ///
1774*0a6a1f1dSLionel Sambuc /// \param LookupCtx The context in which qualified name lookup will
1775*0a6a1f1dSLionel Sambuc /// search.
1776*0a6a1f1dSLionel Sambuc ///
1777*0a6a1f1dSLionel Sambuc /// \param SS An optional C++ scope-specifier.
1778*0a6a1f1dSLionel Sambuc ///
1779*0a6a1f1dSLionel Sambuc /// \returns true if lookup succeeded, false if it failed.
LookupQualifiedName(LookupResult & R,DeclContext * LookupCtx,CXXScopeSpec & SS)1780*0a6a1f1dSLionel Sambuc bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
1781*0a6a1f1dSLionel Sambuc CXXScopeSpec &SS) {
1782*0a6a1f1dSLionel Sambuc auto *NNS = SS.getScopeRep();
1783*0a6a1f1dSLionel Sambuc if (NNS && NNS->getKind() == NestedNameSpecifier::Super)
1784*0a6a1f1dSLionel Sambuc return LookupInSuper(R, NNS->getAsRecordDecl());
1785*0a6a1f1dSLionel Sambuc else
1786*0a6a1f1dSLionel Sambuc
1787*0a6a1f1dSLionel Sambuc return LookupQualifiedName(R, LookupCtx);
1788*0a6a1f1dSLionel Sambuc }
1789*0a6a1f1dSLionel Sambuc
1790f4a2713aSLionel Sambuc /// @brief Performs name lookup for a name that was parsed in the
1791f4a2713aSLionel Sambuc /// source code, and may contain a C++ scope specifier.
1792f4a2713aSLionel Sambuc ///
1793f4a2713aSLionel Sambuc /// This routine is a convenience routine meant to be called from
1794f4a2713aSLionel Sambuc /// contexts that receive a name and an optional C++ scope specifier
1795f4a2713aSLionel Sambuc /// (e.g., "N::M::x"). It will then perform either qualified or
1796f4a2713aSLionel Sambuc /// unqualified name lookup (with LookupQualifiedName or LookupName,
1797*0a6a1f1dSLionel Sambuc /// respectively) on the given name and return those results. It will
1798*0a6a1f1dSLionel Sambuc /// perform a special type of lookup for "__super::" scope specifier.
1799f4a2713aSLionel Sambuc ///
1800f4a2713aSLionel Sambuc /// @param S The scope from which unqualified name lookup will
1801f4a2713aSLionel Sambuc /// begin.
1802f4a2713aSLionel Sambuc ///
1803f4a2713aSLionel Sambuc /// @param SS An optional C++ scope-specifier, e.g., "::N::M".
1804f4a2713aSLionel Sambuc ///
1805f4a2713aSLionel Sambuc /// @param EnteringContext Indicates whether we are going to enter the
1806f4a2713aSLionel Sambuc /// context of the scope-specifier SS (if present).
1807f4a2713aSLionel Sambuc ///
1808f4a2713aSLionel Sambuc /// @returns True if any decls were found (but possibly ambiguous)
LookupParsedName(LookupResult & R,Scope * S,CXXScopeSpec * SS,bool AllowBuiltinCreation,bool EnteringContext)1809f4a2713aSLionel Sambuc bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS,
1810f4a2713aSLionel Sambuc bool AllowBuiltinCreation, bool EnteringContext) {
1811f4a2713aSLionel Sambuc if (SS && SS->isInvalid()) {
1812f4a2713aSLionel Sambuc // When the scope specifier is invalid, don't even look for
1813f4a2713aSLionel Sambuc // anything.
1814f4a2713aSLionel Sambuc return false;
1815f4a2713aSLionel Sambuc }
1816f4a2713aSLionel Sambuc
1817f4a2713aSLionel Sambuc if (SS && SS->isSet()) {
1818*0a6a1f1dSLionel Sambuc NestedNameSpecifier *NNS = SS->getScopeRep();
1819*0a6a1f1dSLionel Sambuc if (NNS->getKind() == NestedNameSpecifier::Super)
1820*0a6a1f1dSLionel Sambuc return LookupInSuper(R, NNS->getAsRecordDecl());
1821*0a6a1f1dSLionel Sambuc
1822f4a2713aSLionel Sambuc if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) {
1823f4a2713aSLionel Sambuc // We have resolved the scope specifier to a particular declaration
1824f4a2713aSLionel Sambuc // contex, and will perform name lookup in that context.
1825f4a2713aSLionel Sambuc if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC))
1826f4a2713aSLionel Sambuc return false;
1827f4a2713aSLionel Sambuc
1828f4a2713aSLionel Sambuc R.setContextRange(SS->getRange());
1829f4a2713aSLionel Sambuc return LookupQualifiedName(R, DC);
1830f4a2713aSLionel Sambuc }
1831f4a2713aSLionel Sambuc
1832f4a2713aSLionel Sambuc // We could not resolve the scope specified to a specific declaration
1833f4a2713aSLionel Sambuc // context, which means that SS refers to an unknown specialization.
1834f4a2713aSLionel Sambuc // Name lookup can't find anything in this case.
1835f4a2713aSLionel Sambuc R.setNotFoundInCurrentInstantiation();
1836f4a2713aSLionel Sambuc R.setContextRange(SS->getRange());
1837f4a2713aSLionel Sambuc return false;
1838f4a2713aSLionel Sambuc }
1839f4a2713aSLionel Sambuc
1840f4a2713aSLionel Sambuc // Perform unqualified name lookup starting in the given scope.
1841f4a2713aSLionel Sambuc return LookupName(R, S, AllowBuiltinCreation);
1842f4a2713aSLionel Sambuc }
1843f4a2713aSLionel Sambuc
1844*0a6a1f1dSLionel Sambuc /// \brief Perform qualified name lookup into all base classes of the given
1845*0a6a1f1dSLionel Sambuc /// class.
1846*0a6a1f1dSLionel Sambuc ///
1847*0a6a1f1dSLionel Sambuc /// \param R captures both the lookup criteria and any lookup results found.
1848*0a6a1f1dSLionel Sambuc ///
1849*0a6a1f1dSLionel Sambuc /// \param Class The context in which qualified name lookup will
1850*0a6a1f1dSLionel Sambuc /// search. Name lookup will search in all base classes merging the results.
1851*0a6a1f1dSLionel Sambuc ///
1852*0a6a1f1dSLionel Sambuc /// @returns True if any decls were found (but possibly ambiguous)
LookupInSuper(LookupResult & R,CXXRecordDecl * Class)1853*0a6a1f1dSLionel Sambuc bool Sema::LookupInSuper(LookupResult &R, CXXRecordDecl *Class) {
1854*0a6a1f1dSLionel Sambuc for (const auto &BaseSpec : Class->bases()) {
1855*0a6a1f1dSLionel Sambuc CXXRecordDecl *RD = cast<CXXRecordDecl>(
1856*0a6a1f1dSLionel Sambuc BaseSpec.getType()->castAs<RecordType>()->getDecl());
1857*0a6a1f1dSLionel Sambuc LookupResult Result(*this, R.getLookupNameInfo(), R.getLookupKind());
1858*0a6a1f1dSLionel Sambuc Result.setBaseObjectType(Context.getRecordType(Class));
1859*0a6a1f1dSLionel Sambuc LookupQualifiedName(Result, RD);
1860*0a6a1f1dSLionel Sambuc for (auto *Decl : Result)
1861*0a6a1f1dSLionel Sambuc R.addDecl(Decl);
1862*0a6a1f1dSLionel Sambuc }
1863*0a6a1f1dSLionel Sambuc
1864*0a6a1f1dSLionel Sambuc R.resolveKind();
1865*0a6a1f1dSLionel Sambuc
1866*0a6a1f1dSLionel Sambuc return !R.empty();
1867*0a6a1f1dSLionel Sambuc }
1868f4a2713aSLionel Sambuc
1869f4a2713aSLionel Sambuc /// \brief Produce a diagnostic describing the ambiguity that resulted
1870f4a2713aSLionel Sambuc /// from name lookup.
1871f4a2713aSLionel Sambuc ///
1872f4a2713aSLionel Sambuc /// \param Result The result of the ambiguous lookup to be diagnosed.
DiagnoseAmbiguousLookup(LookupResult & Result)1873f4a2713aSLionel Sambuc void Sema::DiagnoseAmbiguousLookup(LookupResult &Result) {
1874f4a2713aSLionel Sambuc assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
1875f4a2713aSLionel Sambuc
1876f4a2713aSLionel Sambuc DeclarationName Name = Result.getLookupName();
1877f4a2713aSLionel Sambuc SourceLocation NameLoc = Result.getNameLoc();
1878f4a2713aSLionel Sambuc SourceRange LookupRange = Result.getContextRange();
1879f4a2713aSLionel Sambuc
1880f4a2713aSLionel Sambuc switch (Result.getAmbiguityKind()) {
1881f4a2713aSLionel Sambuc case LookupResult::AmbiguousBaseSubobjects: {
1882f4a2713aSLionel Sambuc CXXBasePaths *Paths = Result.getBasePaths();
1883f4a2713aSLionel Sambuc QualType SubobjectType = Paths->front().back().Base->getType();
1884f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
1885f4a2713aSLionel Sambuc << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
1886f4a2713aSLionel Sambuc << LookupRange;
1887f4a2713aSLionel Sambuc
1888f4a2713aSLionel Sambuc DeclContext::lookup_iterator Found = Paths->front().Decls.begin();
1889f4a2713aSLionel Sambuc while (isa<CXXMethodDecl>(*Found) &&
1890f4a2713aSLionel Sambuc cast<CXXMethodDecl>(*Found)->isStatic())
1891f4a2713aSLionel Sambuc ++Found;
1892f4a2713aSLionel Sambuc
1893f4a2713aSLionel Sambuc Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
1894f4a2713aSLionel Sambuc break;
1895f4a2713aSLionel Sambuc }
1896f4a2713aSLionel Sambuc
1897f4a2713aSLionel Sambuc case LookupResult::AmbiguousBaseSubobjectTypes: {
1898f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
1899f4a2713aSLionel Sambuc << Name << LookupRange;
1900f4a2713aSLionel Sambuc
1901f4a2713aSLionel Sambuc CXXBasePaths *Paths = Result.getBasePaths();
1902f4a2713aSLionel Sambuc std::set<Decl *> DeclsPrinted;
1903f4a2713aSLionel Sambuc for (CXXBasePaths::paths_iterator Path = Paths->begin(),
1904f4a2713aSLionel Sambuc PathEnd = Paths->end();
1905f4a2713aSLionel Sambuc Path != PathEnd; ++Path) {
1906f4a2713aSLionel Sambuc Decl *D = Path->Decls.front();
1907f4a2713aSLionel Sambuc if (DeclsPrinted.insert(D).second)
1908f4a2713aSLionel Sambuc Diag(D->getLocation(), diag::note_ambiguous_member_found);
1909f4a2713aSLionel Sambuc }
1910f4a2713aSLionel Sambuc break;
1911f4a2713aSLionel Sambuc }
1912f4a2713aSLionel Sambuc
1913f4a2713aSLionel Sambuc case LookupResult::AmbiguousTagHiding: {
1914f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange;
1915f4a2713aSLionel Sambuc
1916f4a2713aSLionel Sambuc llvm::SmallPtrSet<NamedDecl*,8> TagDecls;
1917f4a2713aSLionel Sambuc
1918*0a6a1f1dSLionel Sambuc for (auto *D : Result)
1919*0a6a1f1dSLionel Sambuc if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
1920f4a2713aSLionel Sambuc TagDecls.insert(TD);
1921f4a2713aSLionel Sambuc Diag(TD->getLocation(), diag::note_hidden_tag);
1922f4a2713aSLionel Sambuc }
1923f4a2713aSLionel Sambuc
1924*0a6a1f1dSLionel Sambuc for (auto *D : Result)
1925*0a6a1f1dSLionel Sambuc if (!isa<TagDecl>(D))
1926*0a6a1f1dSLionel Sambuc Diag(D->getLocation(), diag::note_hiding_object);
1927f4a2713aSLionel Sambuc
1928f4a2713aSLionel Sambuc // For recovery purposes, go ahead and implement the hiding.
1929f4a2713aSLionel Sambuc LookupResult::Filter F = Result.makeFilter();
1930f4a2713aSLionel Sambuc while (F.hasNext()) {
1931f4a2713aSLionel Sambuc if (TagDecls.count(F.next()))
1932f4a2713aSLionel Sambuc F.erase();
1933f4a2713aSLionel Sambuc }
1934f4a2713aSLionel Sambuc F.done();
1935f4a2713aSLionel Sambuc break;
1936f4a2713aSLionel Sambuc }
1937f4a2713aSLionel Sambuc
1938f4a2713aSLionel Sambuc case LookupResult::AmbiguousReference: {
1939f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange;
1940f4a2713aSLionel Sambuc
1941*0a6a1f1dSLionel Sambuc for (auto *D : Result)
1942*0a6a1f1dSLionel Sambuc Diag(D->getLocation(), diag::note_ambiguous_candidate) << D;
1943f4a2713aSLionel Sambuc break;
1944f4a2713aSLionel Sambuc }
1945f4a2713aSLionel Sambuc }
1946f4a2713aSLionel Sambuc }
1947f4a2713aSLionel Sambuc
1948f4a2713aSLionel Sambuc namespace {
1949f4a2713aSLionel Sambuc struct AssociatedLookup {
AssociatedLookup__anonbd765e620311::AssociatedLookup1950f4a2713aSLionel Sambuc AssociatedLookup(Sema &S, SourceLocation InstantiationLoc,
1951f4a2713aSLionel Sambuc Sema::AssociatedNamespaceSet &Namespaces,
1952f4a2713aSLionel Sambuc Sema::AssociatedClassSet &Classes)
1953f4a2713aSLionel Sambuc : S(S), Namespaces(Namespaces), Classes(Classes),
1954f4a2713aSLionel Sambuc InstantiationLoc(InstantiationLoc) {
1955f4a2713aSLionel Sambuc }
1956f4a2713aSLionel Sambuc
1957f4a2713aSLionel Sambuc Sema &S;
1958f4a2713aSLionel Sambuc Sema::AssociatedNamespaceSet &Namespaces;
1959f4a2713aSLionel Sambuc Sema::AssociatedClassSet &Classes;
1960f4a2713aSLionel Sambuc SourceLocation InstantiationLoc;
1961f4a2713aSLionel Sambuc };
1962f4a2713aSLionel Sambuc }
1963f4a2713aSLionel Sambuc
1964f4a2713aSLionel Sambuc static void
1965f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T);
1966f4a2713aSLionel Sambuc
CollectEnclosingNamespace(Sema::AssociatedNamespaceSet & Namespaces,DeclContext * Ctx)1967f4a2713aSLionel Sambuc static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces,
1968f4a2713aSLionel Sambuc DeclContext *Ctx) {
1969f4a2713aSLionel Sambuc // Add the associated namespace for this class.
1970f4a2713aSLionel Sambuc
1971f4a2713aSLionel Sambuc // We don't use DeclContext::getEnclosingNamespaceContext() as this may
1972f4a2713aSLionel Sambuc // be a locally scoped record.
1973f4a2713aSLionel Sambuc
1974f4a2713aSLionel Sambuc // We skip out of inline namespaces. The innermost non-inline namespace
1975f4a2713aSLionel Sambuc // contains all names of all its nested inline namespaces anyway, so we can
1976f4a2713aSLionel Sambuc // replace the entire inline namespace tree with its root.
1977f4a2713aSLionel Sambuc while (Ctx->isRecord() || Ctx->isTransparentContext() ||
1978f4a2713aSLionel Sambuc Ctx->isInlineNamespace())
1979f4a2713aSLionel Sambuc Ctx = Ctx->getParent();
1980f4a2713aSLionel Sambuc
1981f4a2713aSLionel Sambuc if (Ctx->isFileContext())
1982f4a2713aSLionel Sambuc Namespaces.insert(Ctx->getPrimaryContext());
1983f4a2713aSLionel Sambuc }
1984f4a2713aSLionel Sambuc
1985f4a2713aSLionel Sambuc // \brief Add the associated classes and namespaces for argument-dependent
1986f4a2713aSLionel Sambuc // lookup that involves a template argument (C++ [basic.lookup.koenig]p2).
1987f4a2713aSLionel Sambuc static void
addAssociatedClassesAndNamespaces(AssociatedLookup & Result,const TemplateArgument & Arg)1988f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result,
1989f4a2713aSLionel Sambuc const TemplateArgument &Arg) {
1990f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2, last bullet:
1991f4a2713aSLionel Sambuc // -- [...] ;
1992f4a2713aSLionel Sambuc switch (Arg.getKind()) {
1993f4a2713aSLionel Sambuc case TemplateArgument::Null:
1994f4a2713aSLionel Sambuc break;
1995f4a2713aSLionel Sambuc
1996f4a2713aSLionel Sambuc case TemplateArgument::Type:
1997f4a2713aSLionel Sambuc // [...] the namespaces and classes associated with the types of the
1998f4a2713aSLionel Sambuc // template arguments provided for template type parameters (excluding
1999f4a2713aSLionel Sambuc // template template parameters)
2000f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, Arg.getAsType());
2001f4a2713aSLionel Sambuc break;
2002f4a2713aSLionel Sambuc
2003f4a2713aSLionel Sambuc case TemplateArgument::Template:
2004f4a2713aSLionel Sambuc case TemplateArgument::TemplateExpansion: {
2005f4a2713aSLionel Sambuc // [...] the namespaces in which any template template arguments are
2006f4a2713aSLionel Sambuc // defined; and the classes in which any member templates used as
2007f4a2713aSLionel Sambuc // template template arguments are defined.
2008f4a2713aSLionel Sambuc TemplateName Template = Arg.getAsTemplateOrTemplatePattern();
2009f4a2713aSLionel Sambuc if (ClassTemplateDecl *ClassTemplate
2010f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) {
2011f4a2713aSLionel Sambuc DeclContext *Ctx = ClassTemplate->getDeclContext();
2012f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
2013f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass);
2014f4a2713aSLionel Sambuc // Add the associated namespace for this class.
2015f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx);
2016f4a2713aSLionel Sambuc }
2017f4a2713aSLionel Sambuc break;
2018f4a2713aSLionel Sambuc }
2019f4a2713aSLionel Sambuc
2020f4a2713aSLionel Sambuc case TemplateArgument::Declaration:
2021f4a2713aSLionel Sambuc case TemplateArgument::Integral:
2022f4a2713aSLionel Sambuc case TemplateArgument::Expression:
2023f4a2713aSLionel Sambuc case TemplateArgument::NullPtr:
2024f4a2713aSLionel Sambuc // [Note: non-type template arguments do not contribute to the set of
2025f4a2713aSLionel Sambuc // associated namespaces. ]
2026f4a2713aSLionel Sambuc break;
2027f4a2713aSLionel Sambuc
2028f4a2713aSLionel Sambuc case TemplateArgument::Pack:
2029*0a6a1f1dSLionel Sambuc for (const auto &P : Arg.pack_elements())
2030*0a6a1f1dSLionel Sambuc addAssociatedClassesAndNamespaces(Result, P);
2031f4a2713aSLionel Sambuc break;
2032f4a2713aSLionel Sambuc }
2033f4a2713aSLionel Sambuc }
2034f4a2713aSLionel Sambuc
2035f4a2713aSLionel Sambuc // \brief Add the associated classes and namespaces for
2036f4a2713aSLionel Sambuc // argument-dependent lookup with an argument of class type
2037f4a2713aSLionel Sambuc // (C++ [basic.lookup.koenig]p2).
2038f4a2713aSLionel Sambuc static void
addAssociatedClassesAndNamespaces(AssociatedLookup & Result,CXXRecordDecl * Class)2039f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result,
2040f4a2713aSLionel Sambuc CXXRecordDecl *Class) {
2041f4a2713aSLionel Sambuc
2042f4a2713aSLionel Sambuc // Just silently ignore anything whose name is __va_list_tag.
2043f4a2713aSLionel Sambuc if (Class->getDeclName() == Result.S.VAListTagName)
2044f4a2713aSLionel Sambuc return;
2045f4a2713aSLionel Sambuc
2046f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2:
2047f4a2713aSLionel Sambuc // [...]
2048f4a2713aSLionel Sambuc // -- If T is a class type (including unions), its associated
2049f4a2713aSLionel Sambuc // classes are: the class itself; the class of which it is a
2050f4a2713aSLionel Sambuc // member, if any; and its direct and indirect base
2051f4a2713aSLionel Sambuc // classes. Its associated namespaces are the namespaces in
2052f4a2713aSLionel Sambuc // which its associated classes are defined.
2053f4a2713aSLionel Sambuc
2054f4a2713aSLionel Sambuc // Add the class of which it is a member, if any.
2055f4a2713aSLionel Sambuc DeclContext *Ctx = Class->getDeclContext();
2056f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
2057f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass);
2058f4a2713aSLionel Sambuc // Add the associated namespace for this class.
2059f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx);
2060f4a2713aSLionel Sambuc
2061f4a2713aSLionel Sambuc // Add the class itself. If we've already seen this class, we don't
2062f4a2713aSLionel Sambuc // need to visit base classes.
2063*0a6a1f1dSLionel Sambuc //
2064*0a6a1f1dSLionel Sambuc // FIXME: That's not correct, we may have added this class only because it
2065*0a6a1f1dSLionel Sambuc // was the enclosing class of another class, and in that case we won't have
2066*0a6a1f1dSLionel Sambuc // added its base classes yet.
2067*0a6a1f1dSLionel Sambuc if (!Result.Classes.insert(Class).second)
2068f4a2713aSLionel Sambuc return;
2069f4a2713aSLionel Sambuc
2070f4a2713aSLionel Sambuc // -- If T is a template-id, its associated namespaces and classes are
2071f4a2713aSLionel Sambuc // the namespace in which the template is defined; for member
2072f4a2713aSLionel Sambuc // templates, the member template's class; the namespaces and classes
2073f4a2713aSLionel Sambuc // associated with the types of the template arguments provided for
2074f4a2713aSLionel Sambuc // template type parameters (excluding template template parameters); the
2075f4a2713aSLionel Sambuc // namespaces in which any template template arguments are defined; and
2076f4a2713aSLionel Sambuc // the classes in which any member templates used as template template
2077f4a2713aSLionel Sambuc // arguments are defined. [Note: non-type template arguments do not
2078f4a2713aSLionel Sambuc // contribute to the set of associated namespaces. ]
2079f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *Spec
2080f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Class)) {
2081f4a2713aSLionel Sambuc DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext();
2082f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
2083f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass);
2084f4a2713aSLionel Sambuc // Add the associated namespace for this class.
2085f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx);
2086f4a2713aSLionel Sambuc
2087f4a2713aSLionel Sambuc const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
2088f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2089f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]);
2090f4a2713aSLionel Sambuc }
2091f4a2713aSLionel Sambuc
2092f4a2713aSLionel Sambuc // Only recurse into base classes for complete types.
2093*0a6a1f1dSLionel Sambuc if (!Class->hasDefinition())
2094f4a2713aSLionel Sambuc return;
2095f4a2713aSLionel Sambuc
2096f4a2713aSLionel Sambuc // Add direct and indirect base classes along with their associated
2097f4a2713aSLionel Sambuc // namespaces.
2098f4a2713aSLionel Sambuc SmallVector<CXXRecordDecl *, 32> Bases;
2099f4a2713aSLionel Sambuc Bases.push_back(Class);
2100f4a2713aSLionel Sambuc while (!Bases.empty()) {
2101f4a2713aSLionel Sambuc // Pop this class off the stack.
2102f4a2713aSLionel Sambuc Class = Bases.pop_back_val();
2103f4a2713aSLionel Sambuc
2104f4a2713aSLionel Sambuc // Visit the base classes.
2105*0a6a1f1dSLionel Sambuc for (const auto &Base : Class->bases()) {
2106*0a6a1f1dSLionel Sambuc const RecordType *BaseType = Base.getType()->getAs<RecordType>();
2107f4a2713aSLionel Sambuc // In dependent contexts, we do ADL twice, and the first time around,
2108f4a2713aSLionel Sambuc // the base type might be a dependent TemplateSpecializationType, or a
2109f4a2713aSLionel Sambuc // TemplateTypeParmType. If that happens, simply ignore it.
2110f4a2713aSLionel Sambuc // FIXME: If we want to support export, we probably need to add the
2111f4a2713aSLionel Sambuc // namespace of the template in a TemplateSpecializationType, or even
2112f4a2713aSLionel Sambuc // the classes and namespaces of known non-dependent arguments.
2113f4a2713aSLionel Sambuc if (!BaseType)
2114f4a2713aSLionel Sambuc continue;
2115f4a2713aSLionel Sambuc CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl());
2116*0a6a1f1dSLionel Sambuc if (Result.Classes.insert(BaseDecl).second) {
2117f4a2713aSLionel Sambuc // Find the associated namespace for this base class.
2118f4a2713aSLionel Sambuc DeclContext *BaseCtx = BaseDecl->getDeclContext();
2119f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, BaseCtx);
2120f4a2713aSLionel Sambuc
2121f4a2713aSLionel Sambuc // Make sure we visit the bases of this base class.
2122f4a2713aSLionel Sambuc if (BaseDecl->bases_begin() != BaseDecl->bases_end())
2123f4a2713aSLionel Sambuc Bases.push_back(BaseDecl);
2124f4a2713aSLionel Sambuc }
2125f4a2713aSLionel Sambuc }
2126f4a2713aSLionel Sambuc }
2127f4a2713aSLionel Sambuc }
2128f4a2713aSLionel Sambuc
2129f4a2713aSLionel Sambuc // \brief Add the associated classes and namespaces for
2130f4a2713aSLionel Sambuc // argument-dependent lookup with an argument of type T
2131f4a2713aSLionel Sambuc // (C++ [basic.lookup.koenig]p2).
2132f4a2713aSLionel Sambuc static void
addAssociatedClassesAndNamespaces(AssociatedLookup & Result,QualType Ty)2133f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) {
2134f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2:
2135f4a2713aSLionel Sambuc //
2136f4a2713aSLionel Sambuc // For each argument type T in the function call, there is a set
2137f4a2713aSLionel Sambuc // of zero or more associated namespaces and a set of zero or more
2138f4a2713aSLionel Sambuc // associated classes to be considered. The sets of namespaces and
2139f4a2713aSLionel Sambuc // classes is determined entirely by the types of the function
2140f4a2713aSLionel Sambuc // arguments (and the namespace of any template template
2141f4a2713aSLionel Sambuc // argument). Typedef names and using-declarations used to specify
2142f4a2713aSLionel Sambuc // the types do not contribute to this set. The sets of namespaces
2143f4a2713aSLionel Sambuc // and classes are determined in the following way:
2144f4a2713aSLionel Sambuc
2145f4a2713aSLionel Sambuc SmallVector<const Type *, 16> Queue;
2146f4a2713aSLionel Sambuc const Type *T = Ty->getCanonicalTypeInternal().getTypePtr();
2147f4a2713aSLionel Sambuc
2148f4a2713aSLionel Sambuc while (true) {
2149f4a2713aSLionel Sambuc switch (T->getTypeClass()) {
2150f4a2713aSLionel Sambuc
2151f4a2713aSLionel Sambuc #define TYPE(Class, Base)
2152f4a2713aSLionel Sambuc #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2153f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2154f4a2713aSLionel Sambuc #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2155f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base)
2156f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
2157f4a2713aSLionel Sambuc // T is canonical. We can also ignore dependent types because
2158f4a2713aSLionel Sambuc // we don't need to do ADL at the definition point, but if we
2159f4a2713aSLionel Sambuc // wanted to implement template export (or if we find some other
2160f4a2713aSLionel Sambuc // use for associated classes and namespaces...) this would be
2161f4a2713aSLionel Sambuc // wrong.
2162f4a2713aSLionel Sambuc break;
2163f4a2713aSLionel Sambuc
2164f4a2713aSLionel Sambuc // -- If T is a pointer to U or an array of U, its associated
2165f4a2713aSLionel Sambuc // namespaces and classes are those associated with U.
2166f4a2713aSLionel Sambuc case Type::Pointer:
2167f4a2713aSLionel Sambuc T = cast<PointerType>(T)->getPointeeType().getTypePtr();
2168f4a2713aSLionel Sambuc continue;
2169f4a2713aSLionel Sambuc case Type::ConstantArray:
2170f4a2713aSLionel Sambuc case Type::IncompleteArray:
2171f4a2713aSLionel Sambuc case Type::VariableArray:
2172f4a2713aSLionel Sambuc T = cast<ArrayType>(T)->getElementType().getTypePtr();
2173f4a2713aSLionel Sambuc continue;
2174f4a2713aSLionel Sambuc
2175f4a2713aSLionel Sambuc // -- If T is a fundamental type, its associated sets of
2176f4a2713aSLionel Sambuc // namespaces and classes are both empty.
2177f4a2713aSLionel Sambuc case Type::Builtin:
2178f4a2713aSLionel Sambuc break;
2179f4a2713aSLionel Sambuc
2180f4a2713aSLionel Sambuc // -- If T is a class type (including unions), its associated
2181f4a2713aSLionel Sambuc // classes are: the class itself; the class of which it is a
2182f4a2713aSLionel Sambuc // member, if any; and its direct and indirect base
2183f4a2713aSLionel Sambuc // classes. Its associated namespaces are the namespaces in
2184f4a2713aSLionel Sambuc // which its associated classes are defined.
2185f4a2713aSLionel Sambuc case Type::Record: {
2186*0a6a1f1dSLionel Sambuc Result.S.RequireCompleteType(Result.InstantiationLoc, QualType(T, 0),
2187*0a6a1f1dSLionel Sambuc /*no diagnostic*/ 0);
2188f4a2713aSLionel Sambuc CXXRecordDecl *Class
2189f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl());
2190f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, Class);
2191f4a2713aSLionel Sambuc break;
2192f4a2713aSLionel Sambuc }
2193f4a2713aSLionel Sambuc
2194f4a2713aSLionel Sambuc // -- If T is an enumeration type, its associated namespace is
2195f4a2713aSLionel Sambuc // the namespace in which it is defined. If it is class
2196f4a2713aSLionel Sambuc // member, its associated class is the member's class; else
2197f4a2713aSLionel Sambuc // it has no associated class.
2198f4a2713aSLionel Sambuc case Type::Enum: {
2199f4a2713aSLionel Sambuc EnumDecl *Enum = cast<EnumType>(T)->getDecl();
2200f4a2713aSLionel Sambuc
2201f4a2713aSLionel Sambuc DeclContext *Ctx = Enum->getDeclContext();
2202f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
2203f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass);
2204f4a2713aSLionel Sambuc
2205f4a2713aSLionel Sambuc // Add the associated namespace for this class.
2206f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx);
2207f4a2713aSLionel Sambuc
2208f4a2713aSLionel Sambuc break;
2209f4a2713aSLionel Sambuc }
2210f4a2713aSLionel Sambuc
2211f4a2713aSLionel Sambuc // -- If T is a function type, its associated namespaces and
2212f4a2713aSLionel Sambuc // classes are those associated with the function parameter
2213f4a2713aSLionel Sambuc // types and those associated with the return type.
2214f4a2713aSLionel Sambuc case Type::FunctionProto: {
2215f4a2713aSLionel Sambuc const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
2216*0a6a1f1dSLionel Sambuc for (const auto &Arg : Proto->param_types())
2217*0a6a1f1dSLionel Sambuc Queue.push_back(Arg.getTypePtr());
2218f4a2713aSLionel Sambuc // fallthrough
2219f4a2713aSLionel Sambuc }
2220f4a2713aSLionel Sambuc case Type::FunctionNoProto: {
2221f4a2713aSLionel Sambuc const FunctionType *FnType = cast<FunctionType>(T);
2222*0a6a1f1dSLionel Sambuc T = FnType->getReturnType().getTypePtr();
2223f4a2713aSLionel Sambuc continue;
2224f4a2713aSLionel Sambuc }
2225f4a2713aSLionel Sambuc
2226f4a2713aSLionel Sambuc // -- If T is a pointer to a member function of a class X, its
2227f4a2713aSLionel Sambuc // associated namespaces and classes are those associated
2228f4a2713aSLionel Sambuc // with the function parameter types and return type,
2229f4a2713aSLionel Sambuc // together with those associated with X.
2230f4a2713aSLionel Sambuc //
2231f4a2713aSLionel Sambuc // -- If T is a pointer to a data member of class X, its
2232f4a2713aSLionel Sambuc // associated namespaces and classes are those associated
2233f4a2713aSLionel Sambuc // with the member type together with those associated with
2234f4a2713aSLionel Sambuc // X.
2235f4a2713aSLionel Sambuc case Type::MemberPointer: {
2236f4a2713aSLionel Sambuc const MemberPointerType *MemberPtr = cast<MemberPointerType>(T);
2237f4a2713aSLionel Sambuc
2238f4a2713aSLionel Sambuc // Queue up the class type into which this points.
2239f4a2713aSLionel Sambuc Queue.push_back(MemberPtr->getClass());
2240f4a2713aSLionel Sambuc
2241f4a2713aSLionel Sambuc // And directly continue with the pointee type.
2242f4a2713aSLionel Sambuc T = MemberPtr->getPointeeType().getTypePtr();
2243f4a2713aSLionel Sambuc continue;
2244f4a2713aSLionel Sambuc }
2245f4a2713aSLionel Sambuc
2246f4a2713aSLionel Sambuc // As an extension, treat this like a normal pointer.
2247f4a2713aSLionel Sambuc case Type::BlockPointer:
2248f4a2713aSLionel Sambuc T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr();
2249f4a2713aSLionel Sambuc continue;
2250f4a2713aSLionel Sambuc
2251f4a2713aSLionel Sambuc // References aren't covered by the standard, but that's such an
2252f4a2713aSLionel Sambuc // obvious defect that we cover them anyway.
2253f4a2713aSLionel Sambuc case Type::LValueReference:
2254f4a2713aSLionel Sambuc case Type::RValueReference:
2255f4a2713aSLionel Sambuc T = cast<ReferenceType>(T)->getPointeeType().getTypePtr();
2256f4a2713aSLionel Sambuc continue;
2257f4a2713aSLionel Sambuc
2258f4a2713aSLionel Sambuc // These are fundamental types.
2259f4a2713aSLionel Sambuc case Type::Vector:
2260f4a2713aSLionel Sambuc case Type::ExtVector:
2261f4a2713aSLionel Sambuc case Type::Complex:
2262f4a2713aSLionel Sambuc break;
2263f4a2713aSLionel Sambuc
2264f4a2713aSLionel Sambuc // Non-deduced auto types only get here for error cases.
2265f4a2713aSLionel Sambuc case Type::Auto:
2266f4a2713aSLionel Sambuc break;
2267f4a2713aSLionel Sambuc
2268f4a2713aSLionel Sambuc // If T is an Objective-C object or interface type, or a pointer to an
2269f4a2713aSLionel Sambuc // object or interface type, the associated namespace is the global
2270f4a2713aSLionel Sambuc // namespace.
2271f4a2713aSLionel Sambuc case Type::ObjCObject:
2272f4a2713aSLionel Sambuc case Type::ObjCInterface:
2273f4a2713aSLionel Sambuc case Type::ObjCObjectPointer:
2274f4a2713aSLionel Sambuc Result.Namespaces.insert(Result.S.Context.getTranslationUnitDecl());
2275f4a2713aSLionel Sambuc break;
2276f4a2713aSLionel Sambuc
2277f4a2713aSLionel Sambuc // Atomic types are just wrappers; use the associations of the
2278f4a2713aSLionel Sambuc // contained type.
2279f4a2713aSLionel Sambuc case Type::Atomic:
2280f4a2713aSLionel Sambuc T = cast<AtomicType>(T)->getValueType().getTypePtr();
2281f4a2713aSLionel Sambuc continue;
2282f4a2713aSLionel Sambuc }
2283f4a2713aSLionel Sambuc
2284f4a2713aSLionel Sambuc if (Queue.empty())
2285f4a2713aSLionel Sambuc break;
2286f4a2713aSLionel Sambuc T = Queue.pop_back_val();
2287f4a2713aSLionel Sambuc }
2288f4a2713aSLionel Sambuc }
2289f4a2713aSLionel Sambuc
2290f4a2713aSLionel Sambuc /// \brief Find the associated classes and namespaces for
2291f4a2713aSLionel Sambuc /// argument-dependent lookup for a call with the given set of
2292f4a2713aSLionel Sambuc /// arguments.
2293f4a2713aSLionel Sambuc ///
2294f4a2713aSLionel Sambuc /// This routine computes the sets of associated classes and associated
2295f4a2713aSLionel Sambuc /// namespaces searched by argument-dependent lookup
2296f4a2713aSLionel Sambuc /// (C++ [basic.lookup.argdep]) for a given set of arguments.
FindAssociatedClassesAndNamespaces(SourceLocation InstantiationLoc,ArrayRef<Expr * > Args,AssociatedNamespaceSet & AssociatedNamespaces,AssociatedClassSet & AssociatedClasses)2297f4a2713aSLionel Sambuc void Sema::FindAssociatedClassesAndNamespaces(
2298f4a2713aSLionel Sambuc SourceLocation InstantiationLoc, ArrayRef<Expr *> Args,
2299f4a2713aSLionel Sambuc AssociatedNamespaceSet &AssociatedNamespaces,
2300f4a2713aSLionel Sambuc AssociatedClassSet &AssociatedClasses) {
2301f4a2713aSLionel Sambuc AssociatedNamespaces.clear();
2302f4a2713aSLionel Sambuc AssociatedClasses.clear();
2303f4a2713aSLionel Sambuc
2304f4a2713aSLionel Sambuc AssociatedLookup Result(*this, InstantiationLoc,
2305f4a2713aSLionel Sambuc AssociatedNamespaces, AssociatedClasses);
2306f4a2713aSLionel Sambuc
2307f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2:
2308f4a2713aSLionel Sambuc // For each argument type T in the function call, there is a set
2309f4a2713aSLionel Sambuc // of zero or more associated namespaces and a set of zero or more
2310f4a2713aSLionel Sambuc // associated classes to be considered. The sets of namespaces and
2311f4a2713aSLionel Sambuc // classes is determined entirely by the types of the function
2312f4a2713aSLionel Sambuc // arguments (and the namespace of any template template
2313f4a2713aSLionel Sambuc // argument).
2314f4a2713aSLionel Sambuc for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) {
2315f4a2713aSLionel Sambuc Expr *Arg = Args[ArgIdx];
2316f4a2713aSLionel Sambuc
2317f4a2713aSLionel Sambuc if (Arg->getType() != Context.OverloadTy) {
2318f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, Arg->getType());
2319f4a2713aSLionel Sambuc continue;
2320f4a2713aSLionel Sambuc }
2321f4a2713aSLionel Sambuc
2322f4a2713aSLionel Sambuc // [...] In addition, if the argument is the name or address of a
2323f4a2713aSLionel Sambuc // set of overloaded functions and/or function templates, its
2324f4a2713aSLionel Sambuc // associated classes and namespaces are the union of those
2325f4a2713aSLionel Sambuc // associated with each of the members of the set: the namespace
2326f4a2713aSLionel Sambuc // in which the function or function template is defined and the
2327f4a2713aSLionel Sambuc // classes and namespaces associated with its (non-dependent)
2328f4a2713aSLionel Sambuc // parameter types and return type.
2329f4a2713aSLionel Sambuc Arg = Arg->IgnoreParens();
2330f4a2713aSLionel Sambuc if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg))
2331f4a2713aSLionel Sambuc if (unaryOp->getOpcode() == UO_AddrOf)
2332f4a2713aSLionel Sambuc Arg = unaryOp->getSubExpr();
2333f4a2713aSLionel Sambuc
2334f4a2713aSLionel Sambuc UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg);
2335f4a2713aSLionel Sambuc if (!ULE) continue;
2336f4a2713aSLionel Sambuc
2337*0a6a1f1dSLionel Sambuc for (const auto *D : ULE->decls()) {
2338f4a2713aSLionel Sambuc // Look through any using declarations to find the underlying function.
2339*0a6a1f1dSLionel Sambuc const FunctionDecl *FDecl = D->getUnderlyingDecl()->getAsFunction();
2340f4a2713aSLionel Sambuc
2341f4a2713aSLionel Sambuc // Add the classes and namespaces associated with the parameter
2342f4a2713aSLionel Sambuc // types and return type of this function.
2343f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, FDecl->getType());
2344f4a2713aSLionel Sambuc }
2345f4a2713aSLionel Sambuc }
2346f4a2713aSLionel Sambuc }
2347f4a2713aSLionel Sambuc
LookupSingleName(Scope * S,DeclarationName Name,SourceLocation Loc,LookupNameKind NameKind,RedeclarationKind Redecl)2348f4a2713aSLionel Sambuc NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name,
2349f4a2713aSLionel Sambuc SourceLocation Loc,
2350f4a2713aSLionel Sambuc LookupNameKind NameKind,
2351f4a2713aSLionel Sambuc RedeclarationKind Redecl) {
2352f4a2713aSLionel Sambuc LookupResult R(*this, Name, Loc, NameKind, Redecl);
2353f4a2713aSLionel Sambuc LookupName(R, S);
2354f4a2713aSLionel Sambuc return R.getAsSingle<NamedDecl>();
2355f4a2713aSLionel Sambuc }
2356f4a2713aSLionel Sambuc
2357f4a2713aSLionel Sambuc /// \brief Find the protocol with the given name, if any.
LookupProtocol(IdentifierInfo * II,SourceLocation IdLoc,RedeclarationKind Redecl)2358f4a2713aSLionel Sambuc ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II,
2359f4a2713aSLionel Sambuc SourceLocation IdLoc,
2360f4a2713aSLionel Sambuc RedeclarationKind Redecl) {
2361f4a2713aSLionel Sambuc Decl *D = LookupSingleName(TUScope, II, IdLoc,
2362f4a2713aSLionel Sambuc LookupObjCProtocolName, Redecl);
2363f4a2713aSLionel Sambuc return cast_or_null<ObjCProtocolDecl>(D);
2364f4a2713aSLionel Sambuc }
2365f4a2713aSLionel Sambuc
LookupOverloadedOperatorName(OverloadedOperatorKind Op,Scope * S,QualType T1,QualType T2,UnresolvedSetImpl & Functions)2366f4a2713aSLionel Sambuc void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S,
2367f4a2713aSLionel Sambuc QualType T1, QualType T2,
2368f4a2713aSLionel Sambuc UnresolvedSetImpl &Functions) {
2369f4a2713aSLionel Sambuc // C++ [over.match.oper]p3:
2370f4a2713aSLionel Sambuc // -- The set of non-member candidates is the result of the
2371f4a2713aSLionel Sambuc // unqualified lookup of operator@ in the context of the
2372f4a2713aSLionel Sambuc // expression according to the usual rules for name lookup in
2373f4a2713aSLionel Sambuc // unqualified function calls (3.4.2) except that all member
2374*0a6a1f1dSLionel Sambuc // functions are ignored.
2375f4a2713aSLionel Sambuc DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
2376f4a2713aSLionel Sambuc LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName);
2377f4a2713aSLionel Sambuc LookupName(Operators, S);
2378f4a2713aSLionel Sambuc
2379f4a2713aSLionel Sambuc assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
2380*0a6a1f1dSLionel Sambuc Functions.append(Operators.begin(), Operators.end());
2381f4a2713aSLionel Sambuc }
2382f4a2713aSLionel Sambuc
LookupSpecialMember(CXXRecordDecl * RD,CXXSpecialMember SM,bool ConstArg,bool VolatileArg,bool RValueThis,bool ConstThis,bool VolatileThis)2383f4a2713aSLionel Sambuc Sema::SpecialMemberOverloadResult *Sema::LookupSpecialMember(CXXRecordDecl *RD,
2384f4a2713aSLionel Sambuc CXXSpecialMember SM,
2385f4a2713aSLionel Sambuc bool ConstArg,
2386f4a2713aSLionel Sambuc bool VolatileArg,
2387f4a2713aSLionel Sambuc bool RValueThis,
2388f4a2713aSLionel Sambuc bool ConstThis,
2389f4a2713aSLionel Sambuc bool VolatileThis) {
2390f4a2713aSLionel Sambuc assert(CanDeclareSpecialMemberFunction(RD) &&
2391f4a2713aSLionel Sambuc "doing special member lookup into record that isn't fully complete");
2392f4a2713aSLionel Sambuc RD = RD->getDefinition();
2393f4a2713aSLionel Sambuc if (RValueThis || ConstThis || VolatileThis)
2394f4a2713aSLionel Sambuc assert((SM == CXXCopyAssignment || SM == CXXMoveAssignment) &&
2395f4a2713aSLionel Sambuc "constructors and destructors always have unqualified lvalue this");
2396f4a2713aSLionel Sambuc if (ConstArg || VolatileArg)
2397f4a2713aSLionel Sambuc assert((SM != CXXDefaultConstructor && SM != CXXDestructor) &&
2398f4a2713aSLionel Sambuc "parameter-less special members can't have qualified arguments");
2399f4a2713aSLionel Sambuc
2400f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2401f4a2713aSLionel Sambuc ID.AddPointer(RD);
2402f4a2713aSLionel Sambuc ID.AddInteger(SM);
2403f4a2713aSLionel Sambuc ID.AddInteger(ConstArg);
2404f4a2713aSLionel Sambuc ID.AddInteger(VolatileArg);
2405f4a2713aSLionel Sambuc ID.AddInteger(RValueThis);
2406f4a2713aSLionel Sambuc ID.AddInteger(ConstThis);
2407f4a2713aSLionel Sambuc ID.AddInteger(VolatileThis);
2408f4a2713aSLionel Sambuc
2409f4a2713aSLionel Sambuc void *InsertPoint;
2410f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result =
2411f4a2713aSLionel Sambuc SpecialMemberCache.FindNodeOrInsertPos(ID, InsertPoint);
2412f4a2713aSLionel Sambuc
2413f4a2713aSLionel Sambuc // This was already cached
2414f4a2713aSLionel Sambuc if (Result)
2415f4a2713aSLionel Sambuc return Result;
2416f4a2713aSLionel Sambuc
2417f4a2713aSLionel Sambuc Result = BumpAlloc.Allocate<SpecialMemberOverloadResult>();
2418f4a2713aSLionel Sambuc Result = new (Result) SpecialMemberOverloadResult(ID);
2419f4a2713aSLionel Sambuc SpecialMemberCache.InsertNode(Result, InsertPoint);
2420f4a2713aSLionel Sambuc
2421f4a2713aSLionel Sambuc if (SM == CXXDestructor) {
2422f4a2713aSLionel Sambuc if (RD->needsImplicitDestructor())
2423f4a2713aSLionel Sambuc DeclareImplicitDestructor(RD);
2424f4a2713aSLionel Sambuc CXXDestructorDecl *DD = RD->getDestructor();
2425f4a2713aSLionel Sambuc assert(DD && "record without a destructor");
2426f4a2713aSLionel Sambuc Result->setMethod(DD);
2427f4a2713aSLionel Sambuc Result->setKind(DD->isDeleted() ?
2428f4a2713aSLionel Sambuc SpecialMemberOverloadResult::NoMemberOrDeleted :
2429f4a2713aSLionel Sambuc SpecialMemberOverloadResult::Success);
2430f4a2713aSLionel Sambuc return Result;
2431f4a2713aSLionel Sambuc }
2432f4a2713aSLionel Sambuc
2433f4a2713aSLionel Sambuc // Prepare for overload resolution. Here we construct a synthetic argument
2434f4a2713aSLionel Sambuc // if necessary and make sure that implicit functions are declared.
2435f4a2713aSLionel Sambuc CanQualType CanTy = Context.getCanonicalType(Context.getTagDeclType(RD));
2436f4a2713aSLionel Sambuc DeclarationName Name;
2437*0a6a1f1dSLionel Sambuc Expr *Arg = nullptr;
2438f4a2713aSLionel Sambuc unsigned NumArgs;
2439f4a2713aSLionel Sambuc
2440f4a2713aSLionel Sambuc QualType ArgType = CanTy;
2441f4a2713aSLionel Sambuc ExprValueKind VK = VK_LValue;
2442f4a2713aSLionel Sambuc
2443f4a2713aSLionel Sambuc if (SM == CXXDefaultConstructor) {
2444f4a2713aSLionel Sambuc Name = Context.DeclarationNames.getCXXConstructorName(CanTy);
2445f4a2713aSLionel Sambuc NumArgs = 0;
2446f4a2713aSLionel Sambuc if (RD->needsImplicitDefaultConstructor())
2447f4a2713aSLionel Sambuc DeclareImplicitDefaultConstructor(RD);
2448f4a2713aSLionel Sambuc } else {
2449f4a2713aSLionel Sambuc if (SM == CXXCopyConstructor || SM == CXXMoveConstructor) {
2450f4a2713aSLionel Sambuc Name = Context.DeclarationNames.getCXXConstructorName(CanTy);
2451f4a2713aSLionel Sambuc if (RD->needsImplicitCopyConstructor())
2452f4a2713aSLionel Sambuc DeclareImplicitCopyConstructor(RD);
2453f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveConstructor())
2454f4a2713aSLionel Sambuc DeclareImplicitMoveConstructor(RD);
2455f4a2713aSLionel Sambuc } else {
2456f4a2713aSLionel Sambuc Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
2457f4a2713aSLionel Sambuc if (RD->needsImplicitCopyAssignment())
2458f4a2713aSLionel Sambuc DeclareImplicitCopyAssignment(RD);
2459f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveAssignment())
2460f4a2713aSLionel Sambuc DeclareImplicitMoveAssignment(RD);
2461f4a2713aSLionel Sambuc }
2462f4a2713aSLionel Sambuc
2463f4a2713aSLionel Sambuc if (ConstArg)
2464f4a2713aSLionel Sambuc ArgType.addConst();
2465f4a2713aSLionel Sambuc if (VolatileArg)
2466f4a2713aSLionel Sambuc ArgType.addVolatile();
2467f4a2713aSLionel Sambuc
2468f4a2713aSLionel Sambuc // This isn't /really/ specified by the standard, but it's implied
2469f4a2713aSLionel Sambuc // we should be working from an RValue in the case of move to ensure
2470f4a2713aSLionel Sambuc // that we prefer to bind to rvalue references, and an LValue in the
2471f4a2713aSLionel Sambuc // case of copy to ensure we don't bind to rvalue references.
2472f4a2713aSLionel Sambuc // Possibly an XValue is actually correct in the case of move, but
2473f4a2713aSLionel Sambuc // there is no semantic difference for class types in this restricted
2474f4a2713aSLionel Sambuc // case.
2475f4a2713aSLionel Sambuc if (SM == CXXCopyConstructor || SM == CXXCopyAssignment)
2476f4a2713aSLionel Sambuc VK = VK_LValue;
2477f4a2713aSLionel Sambuc else
2478f4a2713aSLionel Sambuc VK = VK_RValue;
2479f4a2713aSLionel Sambuc }
2480f4a2713aSLionel Sambuc
2481f4a2713aSLionel Sambuc OpaqueValueExpr FakeArg(SourceLocation(), ArgType, VK);
2482f4a2713aSLionel Sambuc
2483f4a2713aSLionel Sambuc if (SM != CXXDefaultConstructor) {
2484f4a2713aSLionel Sambuc NumArgs = 1;
2485f4a2713aSLionel Sambuc Arg = &FakeArg;
2486f4a2713aSLionel Sambuc }
2487f4a2713aSLionel Sambuc
2488f4a2713aSLionel Sambuc // Create the object argument
2489f4a2713aSLionel Sambuc QualType ThisTy = CanTy;
2490f4a2713aSLionel Sambuc if (ConstThis)
2491f4a2713aSLionel Sambuc ThisTy.addConst();
2492f4a2713aSLionel Sambuc if (VolatileThis)
2493f4a2713aSLionel Sambuc ThisTy.addVolatile();
2494f4a2713aSLionel Sambuc Expr::Classification Classification =
2495f4a2713aSLionel Sambuc OpaqueValueExpr(SourceLocation(), ThisTy,
2496f4a2713aSLionel Sambuc RValueThis ? VK_RValue : VK_LValue).Classify(Context);
2497f4a2713aSLionel Sambuc
2498f4a2713aSLionel Sambuc // Now we perform lookup on the name we computed earlier and do overload
2499f4a2713aSLionel Sambuc // resolution. Lookup is only performed directly into the class since there
2500f4a2713aSLionel Sambuc // will always be a (possibly implicit) declaration to shadow any others.
2501*0a6a1f1dSLionel Sambuc OverloadCandidateSet OCS(RD->getLocation(), OverloadCandidateSet::CSK_Normal);
2502f4a2713aSLionel Sambuc DeclContext::lookup_result R = RD->lookup(Name);
2503*0a6a1f1dSLionel Sambuc
2504*0a6a1f1dSLionel Sambuc if (R.empty()) {
2505*0a6a1f1dSLionel Sambuc // We might have no default constructor because we have a lambda's closure
2506*0a6a1f1dSLionel Sambuc // type, rather than because there's some other declared constructor.
2507*0a6a1f1dSLionel Sambuc // Every class has a copy/move constructor, copy/move assignment, and
2508*0a6a1f1dSLionel Sambuc // destructor.
2509*0a6a1f1dSLionel Sambuc assert(SM == CXXDefaultConstructor &&
2510f4a2713aSLionel Sambuc "lookup for a constructor or assignment operator was empty");
2511*0a6a1f1dSLionel Sambuc Result->setMethod(nullptr);
2512*0a6a1f1dSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted);
2513*0a6a1f1dSLionel Sambuc return Result;
2514*0a6a1f1dSLionel Sambuc }
2515f4a2713aSLionel Sambuc
2516f4a2713aSLionel Sambuc // Copy the candidates as our processing of them may load new declarations
2517f4a2713aSLionel Sambuc // from an external source and invalidate lookup_result.
2518f4a2713aSLionel Sambuc SmallVector<NamedDecl *, 8> Candidates(R.begin(), R.end());
2519f4a2713aSLionel Sambuc
2520*0a6a1f1dSLionel Sambuc for (auto *Cand : Candidates) {
2521f4a2713aSLionel Sambuc if (Cand->isInvalidDecl())
2522f4a2713aSLionel Sambuc continue;
2523f4a2713aSLionel Sambuc
2524f4a2713aSLionel Sambuc if (UsingShadowDecl *U = dyn_cast<UsingShadowDecl>(Cand)) {
2525f4a2713aSLionel Sambuc // FIXME: [namespace.udecl]p15 says that we should only consider a
2526f4a2713aSLionel Sambuc // using declaration here if it does not match a declaration in the
2527f4a2713aSLionel Sambuc // derived class. We do not implement this correctly in other cases
2528f4a2713aSLionel Sambuc // either.
2529f4a2713aSLionel Sambuc Cand = U->getTargetDecl();
2530f4a2713aSLionel Sambuc
2531f4a2713aSLionel Sambuc if (Cand->isInvalidDecl())
2532f4a2713aSLionel Sambuc continue;
2533f4a2713aSLionel Sambuc }
2534f4a2713aSLionel Sambuc
2535f4a2713aSLionel Sambuc if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(Cand)) {
2536f4a2713aSLionel Sambuc if (SM == CXXCopyAssignment || SM == CXXMoveAssignment)
2537f4a2713aSLionel Sambuc AddMethodCandidate(M, DeclAccessPair::make(M, AS_public), RD, ThisTy,
2538f4a2713aSLionel Sambuc Classification, llvm::makeArrayRef(&Arg, NumArgs),
2539f4a2713aSLionel Sambuc OCS, true);
2540f4a2713aSLionel Sambuc else
2541f4a2713aSLionel Sambuc AddOverloadCandidate(M, DeclAccessPair::make(M, AS_public),
2542f4a2713aSLionel Sambuc llvm::makeArrayRef(&Arg, NumArgs), OCS, true);
2543f4a2713aSLionel Sambuc } else if (FunctionTemplateDecl *Tmpl =
2544f4a2713aSLionel Sambuc dyn_cast<FunctionTemplateDecl>(Cand)) {
2545f4a2713aSLionel Sambuc if (SM == CXXCopyAssignment || SM == CXXMoveAssignment)
2546f4a2713aSLionel Sambuc AddMethodTemplateCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public),
2547*0a6a1f1dSLionel Sambuc RD, nullptr, ThisTy, Classification,
2548f4a2713aSLionel Sambuc llvm::makeArrayRef(&Arg, NumArgs),
2549f4a2713aSLionel Sambuc OCS, true);
2550f4a2713aSLionel Sambuc else
2551f4a2713aSLionel Sambuc AddTemplateOverloadCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public),
2552*0a6a1f1dSLionel Sambuc nullptr, llvm::makeArrayRef(&Arg, NumArgs),
2553f4a2713aSLionel Sambuc OCS, true);
2554f4a2713aSLionel Sambuc } else {
2555f4a2713aSLionel Sambuc assert(isa<UsingDecl>(Cand) && "illegal Kind of operator = Decl");
2556f4a2713aSLionel Sambuc }
2557f4a2713aSLionel Sambuc }
2558f4a2713aSLionel Sambuc
2559f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
2560f4a2713aSLionel Sambuc switch (OCS.BestViableFunction(*this, SourceLocation(), Best)) {
2561f4a2713aSLionel Sambuc case OR_Success:
2562f4a2713aSLionel Sambuc Result->setMethod(cast<CXXMethodDecl>(Best->Function));
2563f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::Success);
2564f4a2713aSLionel Sambuc break;
2565f4a2713aSLionel Sambuc
2566f4a2713aSLionel Sambuc case OR_Deleted:
2567f4a2713aSLionel Sambuc Result->setMethod(cast<CXXMethodDecl>(Best->Function));
2568f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted);
2569f4a2713aSLionel Sambuc break;
2570f4a2713aSLionel Sambuc
2571f4a2713aSLionel Sambuc case OR_Ambiguous:
2572*0a6a1f1dSLionel Sambuc Result->setMethod(nullptr);
2573f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::Ambiguous);
2574f4a2713aSLionel Sambuc break;
2575f4a2713aSLionel Sambuc
2576f4a2713aSLionel Sambuc case OR_No_Viable_Function:
2577*0a6a1f1dSLionel Sambuc Result->setMethod(nullptr);
2578f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted);
2579f4a2713aSLionel Sambuc break;
2580f4a2713aSLionel Sambuc }
2581f4a2713aSLionel Sambuc
2582f4a2713aSLionel Sambuc return Result;
2583f4a2713aSLionel Sambuc }
2584f4a2713aSLionel Sambuc
2585f4a2713aSLionel Sambuc /// \brief Look up the default constructor for the given class.
LookupDefaultConstructor(CXXRecordDecl * Class)2586f4a2713aSLionel Sambuc CXXConstructorDecl *Sema::LookupDefaultConstructor(CXXRecordDecl *Class) {
2587f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result =
2588f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXDefaultConstructor, false, false, false,
2589f4a2713aSLionel Sambuc false, false);
2590f4a2713aSLionel Sambuc
2591f4a2713aSLionel Sambuc return cast_or_null<CXXConstructorDecl>(Result->getMethod());
2592f4a2713aSLionel Sambuc }
2593f4a2713aSLionel Sambuc
2594f4a2713aSLionel Sambuc /// \brief Look up the copying constructor for the given class.
LookupCopyingConstructor(CXXRecordDecl * Class,unsigned Quals)2595f4a2713aSLionel Sambuc CXXConstructorDecl *Sema::LookupCopyingConstructor(CXXRecordDecl *Class,
2596f4a2713aSLionel Sambuc unsigned Quals) {
2597f4a2713aSLionel Sambuc assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
2598f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy ctor arg");
2599f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result =
2600f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXCopyConstructor, Quals & Qualifiers::Const,
2601f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, false, false, false);
2602f4a2713aSLionel Sambuc
2603f4a2713aSLionel Sambuc return cast_or_null<CXXConstructorDecl>(Result->getMethod());
2604f4a2713aSLionel Sambuc }
2605f4a2713aSLionel Sambuc
2606f4a2713aSLionel Sambuc /// \brief Look up the moving constructor for the given class.
LookupMovingConstructor(CXXRecordDecl * Class,unsigned Quals)2607f4a2713aSLionel Sambuc CXXConstructorDecl *Sema::LookupMovingConstructor(CXXRecordDecl *Class,
2608f4a2713aSLionel Sambuc unsigned Quals) {
2609f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result =
2610f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXMoveConstructor, Quals & Qualifiers::Const,
2611f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, false, false, false);
2612f4a2713aSLionel Sambuc
2613f4a2713aSLionel Sambuc return cast_or_null<CXXConstructorDecl>(Result->getMethod());
2614f4a2713aSLionel Sambuc }
2615f4a2713aSLionel Sambuc
2616f4a2713aSLionel Sambuc /// \brief Look up the constructors for the given class.
LookupConstructors(CXXRecordDecl * Class)2617f4a2713aSLionel Sambuc DeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) {
2618f4a2713aSLionel Sambuc // If the implicit constructors have not yet been declared, do so now.
2619f4a2713aSLionel Sambuc if (CanDeclareSpecialMemberFunction(Class)) {
2620f4a2713aSLionel Sambuc if (Class->needsImplicitDefaultConstructor())
2621f4a2713aSLionel Sambuc DeclareImplicitDefaultConstructor(Class);
2622f4a2713aSLionel Sambuc if (Class->needsImplicitCopyConstructor())
2623f4a2713aSLionel Sambuc DeclareImplicitCopyConstructor(Class);
2624f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && Class->needsImplicitMoveConstructor())
2625f4a2713aSLionel Sambuc DeclareImplicitMoveConstructor(Class);
2626f4a2713aSLionel Sambuc }
2627f4a2713aSLionel Sambuc
2628f4a2713aSLionel Sambuc CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class));
2629f4a2713aSLionel Sambuc DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T);
2630f4a2713aSLionel Sambuc return Class->lookup(Name);
2631f4a2713aSLionel Sambuc }
2632f4a2713aSLionel Sambuc
2633f4a2713aSLionel Sambuc /// \brief Look up the copying assignment operator for the given class.
LookupCopyingAssignment(CXXRecordDecl * Class,unsigned Quals,bool RValueThis,unsigned ThisQuals)2634f4a2713aSLionel Sambuc CXXMethodDecl *Sema::LookupCopyingAssignment(CXXRecordDecl *Class,
2635f4a2713aSLionel Sambuc unsigned Quals, bool RValueThis,
2636f4a2713aSLionel Sambuc unsigned ThisQuals) {
2637f4a2713aSLionel Sambuc assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
2638f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy assignment arg");
2639f4a2713aSLionel Sambuc assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
2640f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy assignment this");
2641f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result =
2642f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXCopyAssignment, Quals & Qualifiers::Const,
2643f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, RValueThis,
2644f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Const,
2645f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Volatile);
2646f4a2713aSLionel Sambuc
2647f4a2713aSLionel Sambuc return Result->getMethod();
2648f4a2713aSLionel Sambuc }
2649f4a2713aSLionel Sambuc
2650f4a2713aSLionel Sambuc /// \brief Look up the moving assignment operator for the given class.
LookupMovingAssignment(CXXRecordDecl * Class,unsigned Quals,bool RValueThis,unsigned ThisQuals)2651f4a2713aSLionel Sambuc CXXMethodDecl *Sema::LookupMovingAssignment(CXXRecordDecl *Class,
2652f4a2713aSLionel Sambuc unsigned Quals,
2653f4a2713aSLionel Sambuc bool RValueThis,
2654f4a2713aSLionel Sambuc unsigned ThisQuals) {
2655f4a2713aSLionel Sambuc assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
2656f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy assignment this");
2657f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result =
2658f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXMoveAssignment, Quals & Qualifiers::Const,
2659f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, RValueThis,
2660f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Const,
2661f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Volatile);
2662f4a2713aSLionel Sambuc
2663f4a2713aSLionel Sambuc return Result->getMethod();
2664f4a2713aSLionel Sambuc }
2665f4a2713aSLionel Sambuc
2666f4a2713aSLionel Sambuc /// \brief Look for the destructor of the given class.
2667f4a2713aSLionel Sambuc ///
2668f4a2713aSLionel Sambuc /// During semantic analysis, this routine should be used in lieu of
2669f4a2713aSLionel Sambuc /// CXXRecordDecl::getDestructor().
2670f4a2713aSLionel Sambuc ///
2671f4a2713aSLionel Sambuc /// \returns The destructor for this class.
LookupDestructor(CXXRecordDecl * Class)2672f4a2713aSLionel Sambuc CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) {
2673f4a2713aSLionel Sambuc return cast<CXXDestructorDecl>(LookupSpecialMember(Class, CXXDestructor,
2674f4a2713aSLionel Sambuc false, false, false,
2675f4a2713aSLionel Sambuc false, false)->getMethod());
2676f4a2713aSLionel Sambuc }
2677f4a2713aSLionel Sambuc
2678f4a2713aSLionel Sambuc /// LookupLiteralOperator - Determine which literal operator should be used for
2679f4a2713aSLionel Sambuc /// a user-defined literal, per C++11 [lex.ext].
2680f4a2713aSLionel Sambuc ///
2681f4a2713aSLionel Sambuc /// Normal overload resolution is not used to select which literal operator to
2682f4a2713aSLionel Sambuc /// call for a user-defined literal. Look up the provided literal operator name,
2683f4a2713aSLionel Sambuc /// and filter the results to the appropriate set for the given argument types.
2684f4a2713aSLionel Sambuc Sema::LiteralOperatorLookupResult
LookupLiteralOperator(Scope * S,LookupResult & R,ArrayRef<QualType> ArgTys,bool AllowRaw,bool AllowTemplate,bool AllowStringTemplate)2685f4a2713aSLionel Sambuc Sema::LookupLiteralOperator(Scope *S, LookupResult &R,
2686f4a2713aSLionel Sambuc ArrayRef<QualType> ArgTys,
2687f4a2713aSLionel Sambuc bool AllowRaw, bool AllowTemplate,
2688f4a2713aSLionel Sambuc bool AllowStringTemplate) {
2689f4a2713aSLionel Sambuc LookupName(R, S);
2690f4a2713aSLionel Sambuc assert(R.getResultKind() != LookupResult::Ambiguous &&
2691f4a2713aSLionel Sambuc "literal operator lookup can't be ambiguous");
2692f4a2713aSLionel Sambuc
2693f4a2713aSLionel Sambuc // Filter the lookup results appropriately.
2694f4a2713aSLionel Sambuc LookupResult::Filter F = R.makeFilter();
2695f4a2713aSLionel Sambuc
2696f4a2713aSLionel Sambuc bool FoundRaw = false;
2697f4a2713aSLionel Sambuc bool FoundTemplate = false;
2698f4a2713aSLionel Sambuc bool FoundStringTemplate = false;
2699f4a2713aSLionel Sambuc bool FoundExactMatch = false;
2700f4a2713aSLionel Sambuc
2701f4a2713aSLionel Sambuc while (F.hasNext()) {
2702f4a2713aSLionel Sambuc Decl *D = F.next();
2703f4a2713aSLionel Sambuc if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D))
2704f4a2713aSLionel Sambuc D = USD->getTargetDecl();
2705f4a2713aSLionel Sambuc
2706f4a2713aSLionel Sambuc // If the declaration we found is invalid, skip it.
2707f4a2713aSLionel Sambuc if (D->isInvalidDecl()) {
2708f4a2713aSLionel Sambuc F.erase();
2709f4a2713aSLionel Sambuc continue;
2710f4a2713aSLionel Sambuc }
2711f4a2713aSLionel Sambuc
2712f4a2713aSLionel Sambuc bool IsRaw = false;
2713f4a2713aSLionel Sambuc bool IsTemplate = false;
2714f4a2713aSLionel Sambuc bool IsStringTemplate = false;
2715f4a2713aSLionel Sambuc bool IsExactMatch = false;
2716f4a2713aSLionel Sambuc
2717f4a2713aSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2718f4a2713aSLionel Sambuc if (FD->getNumParams() == 1 &&
2719f4a2713aSLionel Sambuc FD->getParamDecl(0)->getType()->getAs<PointerType>())
2720f4a2713aSLionel Sambuc IsRaw = true;
2721f4a2713aSLionel Sambuc else if (FD->getNumParams() == ArgTys.size()) {
2722f4a2713aSLionel Sambuc IsExactMatch = true;
2723f4a2713aSLionel Sambuc for (unsigned ArgIdx = 0; ArgIdx != ArgTys.size(); ++ArgIdx) {
2724f4a2713aSLionel Sambuc QualType ParamTy = FD->getParamDecl(ArgIdx)->getType();
2725f4a2713aSLionel Sambuc if (!Context.hasSameUnqualifiedType(ArgTys[ArgIdx], ParamTy)) {
2726f4a2713aSLionel Sambuc IsExactMatch = false;
2727f4a2713aSLionel Sambuc break;
2728f4a2713aSLionel Sambuc }
2729f4a2713aSLionel Sambuc }
2730f4a2713aSLionel Sambuc }
2731f4a2713aSLionel Sambuc }
2732f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FD = dyn_cast<FunctionTemplateDecl>(D)) {
2733f4a2713aSLionel Sambuc TemplateParameterList *Params = FD->getTemplateParameters();
2734f4a2713aSLionel Sambuc if (Params->size() == 1)
2735f4a2713aSLionel Sambuc IsTemplate = true;
2736f4a2713aSLionel Sambuc else
2737f4a2713aSLionel Sambuc IsStringTemplate = true;
2738f4a2713aSLionel Sambuc }
2739f4a2713aSLionel Sambuc
2740f4a2713aSLionel Sambuc if (IsExactMatch) {
2741f4a2713aSLionel Sambuc FoundExactMatch = true;
2742f4a2713aSLionel Sambuc AllowRaw = false;
2743f4a2713aSLionel Sambuc AllowTemplate = false;
2744f4a2713aSLionel Sambuc AllowStringTemplate = false;
2745f4a2713aSLionel Sambuc if (FoundRaw || FoundTemplate || FoundStringTemplate) {
2746f4a2713aSLionel Sambuc // Go through again and remove the raw and template decls we've
2747f4a2713aSLionel Sambuc // already found.
2748f4a2713aSLionel Sambuc F.restart();
2749f4a2713aSLionel Sambuc FoundRaw = FoundTemplate = FoundStringTemplate = false;
2750f4a2713aSLionel Sambuc }
2751f4a2713aSLionel Sambuc } else if (AllowRaw && IsRaw) {
2752f4a2713aSLionel Sambuc FoundRaw = true;
2753f4a2713aSLionel Sambuc } else if (AllowTemplate && IsTemplate) {
2754f4a2713aSLionel Sambuc FoundTemplate = true;
2755f4a2713aSLionel Sambuc } else if (AllowStringTemplate && IsStringTemplate) {
2756f4a2713aSLionel Sambuc FoundStringTemplate = true;
2757f4a2713aSLionel Sambuc } else {
2758f4a2713aSLionel Sambuc F.erase();
2759f4a2713aSLionel Sambuc }
2760f4a2713aSLionel Sambuc }
2761f4a2713aSLionel Sambuc
2762f4a2713aSLionel Sambuc F.done();
2763f4a2713aSLionel Sambuc
2764f4a2713aSLionel Sambuc // C++11 [lex.ext]p3, p4: If S contains a literal operator with a matching
2765f4a2713aSLionel Sambuc // parameter type, that is used in preference to a raw literal operator
2766f4a2713aSLionel Sambuc // or literal operator template.
2767f4a2713aSLionel Sambuc if (FoundExactMatch)
2768f4a2713aSLionel Sambuc return LOLR_Cooked;
2769f4a2713aSLionel Sambuc
2770f4a2713aSLionel Sambuc // C++11 [lex.ext]p3, p4: S shall contain a raw literal operator or a literal
2771f4a2713aSLionel Sambuc // operator template, but not both.
2772f4a2713aSLionel Sambuc if (FoundRaw && FoundTemplate) {
2773f4a2713aSLionel Sambuc Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName();
2774*0a6a1f1dSLionel Sambuc for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I)
2775*0a6a1f1dSLionel Sambuc NoteOverloadCandidate((*I)->getUnderlyingDecl()->getAsFunction());
2776f4a2713aSLionel Sambuc return LOLR_Error;
2777f4a2713aSLionel Sambuc }
2778f4a2713aSLionel Sambuc
2779f4a2713aSLionel Sambuc if (FoundRaw)
2780f4a2713aSLionel Sambuc return LOLR_Raw;
2781f4a2713aSLionel Sambuc
2782f4a2713aSLionel Sambuc if (FoundTemplate)
2783f4a2713aSLionel Sambuc return LOLR_Template;
2784f4a2713aSLionel Sambuc
2785f4a2713aSLionel Sambuc if (FoundStringTemplate)
2786f4a2713aSLionel Sambuc return LOLR_StringTemplate;
2787f4a2713aSLionel Sambuc
2788f4a2713aSLionel Sambuc // Didn't find anything we could use.
2789f4a2713aSLionel Sambuc Diag(R.getNameLoc(), diag::err_ovl_no_viable_literal_operator)
2790f4a2713aSLionel Sambuc << R.getLookupName() << (int)ArgTys.size() << ArgTys[0]
2791f4a2713aSLionel Sambuc << (ArgTys.size() == 2 ? ArgTys[1] : QualType()) << AllowRaw
2792f4a2713aSLionel Sambuc << (AllowTemplate || AllowStringTemplate);
2793f4a2713aSLionel Sambuc return LOLR_Error;
2794f4a2713aSLionel Sambuc }
2795f4a2713aSLionel Sambuc
insert(NamedDecl * New)2796f4a2713aSLionel Sambuc void ADLResult::insert(NamedDecl *New) {
2797f4a2713aSLionel Sambuc NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())];
2798f4a2713aSLionel Sambuc
2799f4a2713aSLionel Sambuc // If we haven't yet seen a decl for this key, or the last decl
2800f4a2713aSLionel Sambuc // was exactly this one, we're done.
2801*0a6a1f1dSLionel Sambuc if (Old == nullptr || Old == New) {
2802f4a2713aSLionel Sambuc Old = New;
2803f4a2713aSLionel Sambuc return;
2804f4a2713aSLionel Sambuc }
2805f4a2713aSLionel Sambuc
2806f4a2713aSLionel Sambuc // Otherwise, decide which is a more recent redeclaration.
2807*0a6a1f1dSLionel Sambuc FunctionDecl *OldFD = Old->getAsFunction();
2808*0a6a1f1dSLionel Sambuc FunctionDecl *NewFD = New->getAsFunction();
2809f4a2713aSLionel Sambuc
2810f4a2713aSLionel Sambuc FunctionDecl *Cursor = NewFD;
2811f4a2713aSLionel Sambuc while (true) {
2812f4a2713aSLionel Sambuc Cursor = Cursor->getPreviousDecl();
2813f4a2713aSLionel Sambuc
2814f4a2713aSLionel Sambuc // If we got to the end without finding OldFD, OldFD is the newer
2815f4a2713aSLionel Sambuc // declaration; leave things as they are.
2816f4a2713aSLionel Sambuc if (!Cursor) return;
2817f4a2713aSLionel Sambuc
2818f4a2713aSLionel Sambuc // If we do find OldFD, then NewFD is newer.
2819f4a2713aSLionel Sambuc if (Cursor == OldFD) break;
2820f4a2713aSLionel Sambuc
2821f4a2713aSLionel Sambuc // Otherwise, keep looking.
2822f4a2713aSLionel Sambuc }
2823f4a2713aSLionel Sambuc
2824f4a2713aSLionel Sambuc Old = New;
2825f4a2713aSLionel Sambuc }
2826f4a2713aSLionel Sambuc
ArgumentDependentLookup(DeclarationName Name,SourceLocation Loc,ArrayRef<Expr * > Args,ADLResult & Result)2827*0a6a1f1dSLionel Sambuc void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc,
2828*0a6a1f1dSLionel Sambuc ArrayRef<Expr *> Args, ADLResult &Result) {
2829f4a2713aSLionel Sambuc // Find all of the associated namespaces and classes based on the
2830f4a2713aSLionel Sambuc // arguments we have.
2831f4a2713aSLionel Sambuc AssociatedNamespaceSet AssociatedNamespaces;
2832f4a2713aSLionel Sambuc AssociatedClassSet AssociatedClasses;
2833f4a2713aSLionel Sambuc FindAssociatedClassesAndNamespaces(Loc, Args,
2834f4a2713aSLionel Sambuc AssociatedNamespaces,
2835f4a2713aSLionel Sambuc AssociatedClasses);
2836f4a2713aSLionel Sambuc
2837f4a2713aSLionel Sambuc // C++ [basic.lookup.argdep]p3:
2838f4a2713aSLionel Sambuc // Let X be the lookup set produced by unqualified lookup (3.4.1)
2839f4a2713aSLionel Sambuc // and let Y be the lookup set produced by argument dependent
2840f4a2713aSLionel Sambuc // lookup (defined as follows). If X contains [...] then Y is
2841f4a2713aSLionel Sambuc // empty. Otherwise Y is the set of declarations found in the
2842f4a2713aSLionel Sambuc // namespaces associated with the argument types as described
2843f4a2713aSLionel Sambuc // below. The set of declarations found by the lookup of the name
2844f4a2713aSLionel Sambuc // is the union of X and Y.
2845f4a2713aSLionel Sambuc //
2846f4a2713aSLionel Sambuc // Here, we compute Y and add its members to the overloaded
2847f4a2713aSLionel Sambuc // candidate set.
2848*0a6a1f1dSLionel Sambuc for (auto *NS : AssociatedNamespaces) {
2849f4a2713aSLionel Sambuc // When considering an associated namespace, the lookup is the
2850f4a2713aSLionel Sambuc // same as the lookup performed when the associated namespace is
2851f4a2713aSLionel Sambuc // used as a qualifier (3.4.3.2) except that:
2852f4a2713aSLionel Sambuc //
2853f4a2713aSLionel Sambuc // -- Any using-directives in the associated namespace are
2854f4a2713aSLionel Sambuc // ignored.
2855f4a2713aSLionel Sambuc //
2856f4a2713aSLionel Sambuc // -- Any namespace-scope friend functions declared in
2857f4a2713aSLionel Sambuc // associated classes are visible within their respective
2858f4a2713aSLionel Sambuc // namespaces even if they are not visible during an ordinary
2859f4a2713aSLionel Sambuc // lookup (11.4).
2860*0a6a1f1dSLionel Sambuc DeclContext::lookup_result R = NS->lookup(Name);
2861*0a6a1f1dSLionel Sambuc for (auto *D : R) {
2862f4a2713aSLionel Sambuc // If the only declaration here is an ordinary friend, consider
2863f4a2713aSLionel Sambuc // it only if it was declared in an associated classes.
2864f4a2713aSLionel Sambuc if ((D->getIdentifierNamespace() & Decl::IDNS_Ordinary) == 0) {
2865f4a2713aSLionel Sambuc // If it's neither ordinarily visible nor a friend, we can't find it.
2866f4a2713aSLionel Sambuc if ((D->getIdentifierNamespace() & Decl::IDNS_OrdinaryFriend) == 0)
2867f4a2713aSLionel Sambuc continue;
2868f4a2713aSLionel Sambuc
2869f4a2713aSLionel Sambuc bool DeclaredInAssociatedClass = false;
2870f4a2713aSLionel Sambuc for (Decl *DI = D; DI; DI = DI->getPreviousDecl()) {
2871f4a2713aSLionel Sambuc DeclContext *LexDC = DI->getLexicalDeclContext();
2872f4a2713aSLionel Sambuc if (isa<CXXRecordDecl>(LexDC) &&
2873f4a2713aSLionel Sambuc AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) {
2874f4a2713aSLionel Sambuc DeclaredInAssociatedClass = true;
2875f4a2713aSLionel Sambuc break;
2876f4a2713aSLionel Sambuc }
2877f4a2713aSLionel Sambuc }
2878f4a2713aSLionel Sambuc if (!DeclaredInAssociatedClass)
2879f4a2713aSLionel Sambuc continue;
2880f4a2713aSLionel Sambuc }
2881f4a2713aSLionel Sambuc
2882f4a2713aSLionel Sambuc if (isa<UsingShadowDecl>(D))
2883f4a2713aSLionel Sambuc D = cast<UsingShadowDecl>(D)->getTargetDecl();
2884f4a2713aSLionel Sambuc
2885*0a6a1f1dSLionel Sambuc if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D))
2886f4a2713aSLionel Sambuc continue;
2887f4a2713aSLionel Sambuc
2888f4a2713aSLionel Sambuc Result.insert(D);
2889f4a2713aSLionel Sambuc }
2890f4a2713aSLionel Sambuc }
2891f4a2713aSLionel Sambuc }
2892f4a2713aSLionel Sambuc
2893f4a2713aSLionel Sambuc //----------------------------------------------------------------------------
2894f4a2713aSLionel Sambuc // Search for all visible declarations.
2895f4a2713aSLionel Sambuc //----------------------------------------------------------------------------
~VisibleDeclConsumer()2896f4a2713aSLionel Sambuc VisibleDeclConsumer::~VisibleDeclConsumer() { }
2897f4a2713aSLionel Sambuc
includeHiddenDecls() const2898f4a2713aSLionel Sambuc bool VisibleDeclConsumer::includeHiddenDecls() const { return false; }
2899f4a2713aSLionel Sambuc
2900f4a2713aSLionel Sambuc namespace {
2901f4a2713aSLionel Sambuc
2902f4a2713aSLionel Sambuc class ShadowContextRAII;
2903f4a2713aSLionel Sambuc
2904f4a2713aSLionel Sambuc class VisibleDeclsRecord {
2905f4a2713aSLionel Sambuc public:
2906f4a2713aSLionel Sambuc /// \brief An entry in the shadow map, which is optimized to store a
2907f4a2713aSLionel Sambuc /// single declaration (the common case) but can also store a list
2908f4a2713aSLionel Sambuc /// of declarations.
2909f4a2713aSLionel Sambuc typedef llvm::TinyPtrVector<NamedDecl*> ShadowMapEntry;
2910f4a2713aSLionel Sambuc
2911f4a2713aSLionel Sambuc private:
2912f4a2713aSLionel Sambuc /// \brief A mapping from declaration names to the declarations that have
2913f4a2713aSLionel Sambuc /// this name within a particular scope.
2914f4a2713aSLionel Sambuc typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
2915f4a2713aSLionel Sambuc
2916f4a2713aSLionel Sambuc /// \brief A list of shadow maps, which is used to model name hiding.
2917f4a2713aSLionel Sambuc std::list<ShadowMap> ShadowMaps;
2918f4a2713aSLionel Sambuc
2919f4a2713aSLionel Sambuc /// \brief The declaration contexts we have already visited.
2920f4a2713aSLionel Sambuc llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts;
2921f4a2713aSLionel Sambuc
2922f4a2713aSLionel Sambuc friend class ShadowContextRAII;
2923f4a2713aSLionel Sambuc
2924f4a2713aSLionel Sambuc public:
2925f4a2713aSLionel Sambuc /// \brief Determine whether we have already visited this context
2926f4a2713aSLionel Sambuc /// (and, if not, note that we are going to visit that context now).
visitedContext(DeclContext * Ctx)2927f4a2713aSLionel Sambuc bool visitedContext(DeclContext *Ctx) {
2928*0a6a1f1dSLionel Sambuc return !VisitedContexts.insert(Ctx).second;
2929f4a2713aSLionel Sambuc }
2930f4a2713aSLionel Sambuc
alreadyVisitedContext(DeclContext * Ctx)2931f4a2713aSLionel Sambuc bool alreadyVisitedContext(DeclContext *Ctx) {
2932f4a2713aSLionel Sambuc return VisitedContexts.count(Ctx);
2933f4a2713aSLionel Sambuc }
2934f4a2713aSLionel Sambuc
2935f4a2713aSLionel Sambuc /// \brief Determine whether the given declaration is hidden in the
2936f4a2713aSLionel Sambuc /// current scope.
2937f4a2713aSLionel Sambuc ///
2938f4a2713aSLionel Sambuc /// \returns the declaration that hides the given declaration, or
2939f4a2713aSLionel Sambuc /// NULL if no such declaration exists.
2940f4a2713aSLionel Sambuc NamedDecl *checkHidden(NamedDecl *ND);
2941f4a2713aSLionel Sambuc
2942f4a2713aSLionel Sambuc /// \brief Add a declaration to the current shadow map.
add(NamedDecl * ND)2943f4a2713aSLionel Sambuc void add(NamedDecl *ND) {
2944f4a2713aSLionel Sambuc ShadowMaps.back()[ND->getDeclName()].push_back(ND);
2945f4a2713aSLionel Sambuc }
2946f4a2713aSLionel Sambuc };
2947f4a2713aSLionel Sambuc
2948f4a2713aSLionel Sambuc /// \brief RAII object that records when we've entered a shadow context.
2949f4a2713aSLionel Sambuc class ShadowContextRAII {
2950f4a2713aSLionel Sambuc VisibleDeclsRecord &Visible;
2951f4a2713aSLionel Sambuc
2952f4a2713aSLionel Sambuc typedef VisibleDeclsRecord::ShadowMap ShadowMap;
2953f4a2713aSLionel Sambuc
2954f4a2713aSLionel Sambuc public:
ShadowContextRAII(VisibleDeclsRecord & Visible)2955f4a2713aSLionel Sambuc ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) {
2956f4a2713aSLionel Sambuc Visible.ShadowMaps.push_back(ShadowMap());
2957f4a2713aSLionel Sambuc }
2958f4a2713aSLionel Sambuc
~ShadowContextRAII()2959f4a2713aSLionel Sambuc ~ShadowContextRAII() {
2960f4a2713aSLionel Sambuc Visible.ShadowMaps.pop_back();
2961f4a2713aSLionel Sambuc }
2962f4a2713aSLionel Sambuc };
2963f4a2713aSLionel Sambuc
2964f4a2713aSLionel Sambuc } // end anonymous namespace
2965f4a2713aSLionel Sambuc
checkHidden(NamedDecl * ND)2966f4a2713aSLionel Sambuc NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) {
2967f4a2713aSLionel Sambuc // Look through using declarations.
2968f4a2713aSLionel Sambuc ND = ND->getUnderlyingDecl();
2969f4a2713aSLionel Sambuc
2970f4a2713aSLionel Sambuc unsigned IDNS = ND->getIdentifierNamespace();
2971f4a2713aSLionel Sambuc std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin();
2972f4a2713aSLionel Sambuc for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend();
2973f4a2713aSLionel Sambuc SM != SMEnd; ++SM) {
2974f4a2713aSLionel Sambuc ShadowMap::iterator Pos = SM->find(ND->getDeclName());
2975f4a2713aSLionel Sambuc if (Pos == SM->end())
2976f4a2713aSLionel Sambuc continue;
2977f4a2713aSLionel Sambuc
2978*0a6a1f1dSLionel Sambuc for (auto *D : Pos->second) {
2979f4a2713aSLionel Sambuc // A tag declaration does not hide a non-tag declaration.
2980*0a6a1f1dSLionel Sambuc if (D->hasTagIdentifierNamespace() &&
2981f4a2713aSLionel Sambuc (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
2982f4a2713aSLionel Sambuc Decl::IDNS_ObjCProtocol)))
2983f4a2713aSLionel Sambuc continue;
2984f4a2713aSLionel Sambuc
2985f4a2713aSLionel Sambuc // Protocols are in distinct namespaces from everything else.
2986*0a6a1f1dSLionel Sambuc if (((D->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
2987f4a2713aSLionel Sambuc || (IDNS & Decl::IDNS_ObjCProtocol)) &&
2988*0a6a1f1dSLionel Sambuc D->getIdentifierNamespace() != IDNS)
2989f4a2713aSLionel Sambuc continue;
2990f4a2713aSLionel Sambuc
2991f4a2713aSLionel Sambuc // Functions and function templates in the same scope overload
2992f4a2713aSLionel Sambuc // rather than hide. FIXME: Look for hiding based on function
2993f4a2713aSLionel Sambuc // signatures!
2994*0a6a1f1dSLionel Sambuc if (D->getUnderlyingDecl()->isFunctionOrFunctionTemplate() &&
2995*0a6a1f1dSLionel Sambuc ND->getUnderlyingDecl()->isFunctionOrFunctionTemplate() &&
2996f4a2713aSLionel Sambuc SM == ShadowMaps.rbegin())
2997f4a2713aSLionel Sambuc continue;
2998f4a2713aSLionel Sambuc
2999f4a2713aSLionel Sambuc // We've found a declaration that hides this one.
3000*0a6a1f1dSLionel Sambuc return D;
3001f4a2713aSLionel Sambuc }
3002f4a2713aSLionel Sambuc }
3003f4a2713aSLionel Sambuc
3004*0a6a1f1dSLionel Sambuc return nullptr;
3005f4a2713aSLionel Sambuc }
3006f4a2713aSLionel Sambuc
LookupVisibleDecls(DeclContext * Ctx,LookupResult & Result,bool QualifiedNameLookup,bool InBaseClass,VisibleDeclConsumer & Consumer,VisibleDeclsRecord & Visited)3007f4a2713aSLionel Sambuc static void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result,
3008f4a2713aSLionel Sambuc bool QualifiedNameLookup,
3009f4a2713aSLionel Sambuc bool InBaseClass,
3010f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer,
3011f4a2713aSLionel Sambuc VisibleDeclsRecord &Visited) {
3012f4a2713aSLionel Sambuc if (!Ctx)
3013f4a2713aSLionel Sambuc return;
3014f4a2713aSLionel Sambuc
3015f4a2713aSLionel Sambuc // Make sure we don't visit the same context twice.
3016f4a2713aSLionel Sambuc if (Visited.visitedContext(Ctx->getPrimaryContext()))
3017f4a2713aSLionel Sambuc return;
3018f4a2713aSLionel Sambuc
3019f4a2713aSLionel Sambuc if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx))
3020f4a2713aSLionel Sambuc Result.getSema().ForceDeclarationOfImplicitMembers(Class);
3021f4a2713aSLionel Sambuc
3022f4a2713aSLionel Sambuc // Enumerate all of the results in this context.
3023*0a6a1f1dSLionel Sambuc for (const auto &R : Ctx->lookups()) {
3024*0a6a1f1dSLionel Sambuc for (auto *I : R) {
3025*0a6a1f1dSLionel Sambuc if (NamedDecl *ND = dyn_cast<NamedDecl>(I)) {
3026f4a2713aSLionel Sambuc if ((ND = Result.getAcceptableDecl(ND))) {
3027f4a2713aSLionel Sambuc Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass);
3028f4a2713aSLionel Sambuc Visited.add(ND);
3029f4a2713aSLionel Sambuc }
3030f4a2713aSLionel Sambuc }
3031f4a2713aSLionel Sambuc }
3032f4a2713aSLionel Sambuc }
3033f4a2713aSLionel Sambuc
3034f4a2713aSLionel Sambuc // Traverse using directives for qualified name lookup.
3035f4a2713aSLionel Sambuc if (QualifiedNameLookup) {
3036f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3037*0a6a1f1dSLionel Sambuc for (auto I : Ctx->using_directives()) {
3038*0a6a1f1dSLionel Sambuc LookupVisibleDecls(I->getNominatedNamespace(), Result,
3039f4a2713aSLionel Sambuc QualifiedNameLookup, InBaseClass, Consumer, Visited);
3040f4a2713aSLionel Sambuc }
3041f4a2713aSLionel Sambuc }
3042f4a2713aSLionel Sambuc
3043f4a2713aSLionel Sambuc // Traverse the contexts of inherited C++ classes.
3044f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) {
3045f4a2713aSLionel Sambuc if (!Record->hasDefinition())
3046f4a2713aSLionel Sambuc return;
3047f4a2713aSLionel Sambuc
3048*0a6a1f1dSLionel Sambuc for (const auto &B : Record->bases()) {
3049*0a6a1f1dSLionel Sambuc QualType BaseType = B.getType();
3050f4a2713aSLionel Sambuc
3051f4a2713aSLionel Sambuc // Don't look into dependent bases, because name lookup can't look
3052f4a2713aSLionel Sambuc // there anyway.
3053f4a2713aSLionel Sambuc if (BaseType->isDependentType())
3054f4a2713aSLionel Sambuc continue;
3055f4a2713aSLionel Sambuc
3056f4a2713aSLionel Sambuc const RecordType *Record = BaseType->getAs<RecordType>();
3057f4a2713aSLionel Sambuc if (!Record)
3058f4a2713aSLionel Sambuc continue;
3059f4a2713aSLionel Sambuc
3060f4a2713aSLionel Sambuc // FIXME: It would be nice to be able to determine whether referencing
3061f4a2713aSLionel Sambuc // a particular member would be ambiguous. For example, given
3062f4a2713aSLionel Sambuc //
3063f4a2713aSLionel Sambuc // struct A { int member; };
3064f4a2713aSLionel Sambuc // struct B { int member; };
3065f4a2713aSLionel Sambuc // struct C : A, B { };
3066f4a2713aSLionel Sambuc //
3067f4a2713aSLionel Sambuc // void f(C *c) { c->### }
3068f4a2713aSLionel Sambuc //
3069f4a2713aSLionel Sambuc // accessing 'member' would result in an ambiguity. However, we
3070f4a2713aSLionel Sambuc // could be smart enough to qualify the member with the base
3071f4a2713aSLionel Sambuc // class, e.g.,
3072f4a2713aSLionel Sambuc //
3073f4a2713aSLionel Sambuc // c->B::member
3074f4a2713aSLionel Sambuc //
3075f4a2713aSLionel Sambuc // or
3076f4a2713aSLionel Sambuc //
3077f4a2713aSLionel Sambuc // c->A::member
3078f4a2713aSLionel Sambuc
3079f4a2713aSLionel Sambuc // Find results in this base class (and its bases).
3080f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3081f4a2713aSLionel Sambuc LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup,
3082f4a2713aSLionel Sambuc true, Consumer, Visited);
3083f4a2713aSLionel Sambuc }
3084f4a2713aSLionel Sambuc }
3085f4a2713aSLionel Sambuc
3086f4a2713aSLionel Sambuc // Traverse the contexts of Objective-C classes.
3087f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) {
3088f4a2713aSLionel Sambuc // Traverse categories.
3089*0a6a1f1dSLionel Sambuc for (auto *Cat : IFace->visible_categories()) {
3090f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3091*0a6a1f1dSLionel Sambuc LookupVisibleDecls(Cat, Result, QualifiedNameLookup, false,
3092f4a2713aSLionel Sambuc Consumer, Visited);
3093f4a2713aSLionel Sambuc }
3094f4a2713aSLionel Sambuc
3095f4a2713aSLionel Sambuc // Traverse protocols.
3096*0a6a1f1dSLionel Sambuc for (auto *I : IFace->all_referenced_protocols()) {
3097f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3098*0a6a1f1dSLionel Sambuc LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer,
3099f4a2713aSLionel Sambuc Visited);
3100f4a2713aSLionel Sambuc }
3101f4a2713aSLionel Sambuc
3102f4a2713aSLionel Sambuc // Traverse the superclass.
3103f4a2713aSLionel Sambuc if (IFace->getSuperClass()) {
3104f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3105f4a2713aSLionel Sambuc LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup,
3106f4a2713aSLionel Sambuc true, Consumer, Visited);
3107f4a2713aSLionel Sambuc }
3108f4a2713aSLionel Sambuc
3109f4a2713aSLionel Sambuc // If there is an implementation, traverse it. We do this to find
3110f4a2713aSLionel Sambuc // synthesized ivars.
3111f4a2713aSLionel Sambuc if (IFace->getImplementation()) {
3112f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3113f4a2713aSLionel Sambuc LookupVisibleDecls(IFace->getImplementation(), Result,
3114f4a2713aSLionel Sambuc QualifiedNameLookup, InBaseClass, Consumer, Visited);
3115f4a2713aSLionel Sambuc }
3116f4a2713aSLionel Sambuc } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) {
3117*0a6a1f1dSLionel Sambuc for (auto *I : Protocol->protocols()) {
3118f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3119*0a6a1f1dSLionel Sambuc LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer,
3120f4a2713aSLionel Sambuc Visited);
3121f4a2713aSLionel Sambuc }
3122f4a2713aSLionel Sambuc } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) {
3123*0a6a1f1dSLionel Sambuc for (auto *I : Category->protocols()) {
3124f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3125*0a6a1f1dSLionel Sambuc LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer,
3126f4a2713aSLionel Sambuc Visited);
3127f4a2713aSLionel Sambuc }
3128f4a2713aSLionel Sambuc
3129f4a2713aSLionel Sambuc // If there is an implementation, traverse it.
3130f4a2713aSLionel Sambuc if (Category->getImplementation()) {
3131f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3132f4a2713aSLionel Sambuc LookupVisibleDecls(Category->getImplementation(), Result,
3133f4a2713aSLionel Sambuc QualifiedNameLookup, true, Consumer, Visited);
3134f4a2713aSLionel Sambuc }
3135f4a2713aSLionel Sambuc }
3136f4a2713aSLionel Sambuc }
3137f4a2713aSLionel Sambuc
LookupVisibleDecls(Scope * S,LookupResult & Result,UnqualUsingDirectiveSet & UDirs,VisibleDeclConsumer & Consumer,VisibleDeclsRecord & Visited)3138f4a2713aSLionel Sambuc static void LookupVisibleDecls(Scope *S, LookupResult &Result,
3139f4a2713aSLionel Sambuc UnqualUsingDirectiveSet &UDirs,
3140f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer,
3141f4a2713aSLionel Sambuc VisibleDeclsRecord &Visited) {
3142f4a2713aSLionel Sambuc if (!S)
3143f4a2713aSLionel Sambuc return;
3144f4a2713aSLionel Sambuc
3145f4a2713aSLionel Sambuc if (!S->getEntity() ||
3146f4a2713aSLionel Sambuc (!S->getParent() &&
3147f4a2713aSLionel Sambuc !Visited.alreadyVisitedContext(S->getEntity())) ||
3148f4a2713aSLionel Sambuc (S->getEntity())->isFunctionOrMethod()) {
3149f4a2713aSLionel Sambuc FindLocalExternScope FindLocals(Result);
3150f4a2713aSLionel Sambuc // Walk through the declarations in this Scope.
3151*0a6a1f1dSLionel Sambuc for (auto *D : S->decls()) {
3152*0a6a1f1dSLionel Sambuc if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
3153f4a2713aSLionel Sambuc if ((ND = Result.getAcceptableDecl(ND))) {
3154*0a6a1f1dSLionel Sambuc Consumer.FoundDecl(ND, Visited.checkHidden(ND), nullptr, false);
3155f4a2713aSLionel Sambuc Visited.add(ND);
3156f4a2713aSLionel Sambuc }
3157f4a2713aSLionel Sambuc }
3158f4a2713aSLionel Sambuc }
3159f4a2713aSLionel Sambuc
3160f4a2713aSLionel Sambuc // FIXME: C++ [temp.local]p8
3161*0a6a1f1dSLionel Sambuc DeclContext *Entity = nullptr;
3162f4a2713aSLionel Sambuc if (S->getEntity()) {
3163f4a2713aSLionel Sambuc // Look into this scope's declaration context, along with any of its
3164f4a2713aSLionel Sambuc // parent lookup contexts (e.g., enclosing classes), up to the point
3165f4a2713aSLionel Sambuc // where we hit the context stored in the next outer scope.
3166f4a2713aSLionel Sambuc Entity = S->getEntity();
3167f4a2713aSLionel Sambuc DeclContext *OuterCtx = findOuterContext(S).first; // FIXME
3168f4a2713aSLionel Sambuc
3169f4a2713aSLionel Sambuc for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx);
3170f4a2713aSLionel Sambuc Ctx = Ctx->getLookupParent()) {
3171f4a2713aSLionel Sambuc if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) {
3172f4a2713aSLionel Sambuc if (Method->isInstanceMethod()) {
3173f4a2713aSLionel Sambuc // For instance methods, look for ivars in the method's interface.
3174f4a2713aSLionel Sambuc LookupResult IvarResult(Result.getSema(), Result.getLookupName(),
3175f4a2713aSLionel Sambuc Result.getNameLoc(), Sema::LookupMemberName);
3176f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) {
3177f4a2713aSLionel Sambuc LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false,
3178f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited);
3179f4a2713aSLionel Sambuc }
3180f4a2713aSLionel Sambuc }
3181f4a2713aSLionel Sambuc
3182f4a2713aSLionel Sambuc // We've already performed all of the name lookup that we need
3183f4a2713aSLionel Sambuc // to for Objective-C methods; the next context will be the
3184f4a2713aSLionel Sambuc // outer scope.
3185f4a2713aSLionel Sambuc break;
3186f4a2713aSLionel Sambuc }
3187f4a2713aSLionel Sambuc
3188f4a2713aSLionel Sambuc if (Ctx->isFunctionOrMethod())
3189f4a2713aSLionel Sambuc continue;
3190f4a2713aSLionel Sambuc
3191f4a2713aSLionel Sambuc LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false,
3192f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited);
3193f4a2713aSLionel Sambuc }
3194f4a2713aSLionel Sambuc } else if (!S->getParent()) {
3195f4a2713aSLionel Sambuc // Look into the translation unit scope. We walk through the translation
3196f4a2713aSLionel Sambuc // unit's declaration context, because the Scope itself won't have all of
3197f4a2713aSLionel Sambuc // the declarations if we loaded a precompiled header.
3198f4a2713aSLionel Sambuc // FIXME: We would like the translation unit's Scope object to point to the
3199f4a2713aSLionel Sambuc // translation unit, so we don't need this special "if" branch. However,
3200f4a2713aSLionel Sambuc // doing so would force the normal C++ name-lookup code to look into the
3201f4a2713aSLionel Sambuc // translation unit decl when the IdentifierInfo chains would suffice.
3202f4a2713aSLionel Sambuc // Once we fix that problem (which is part of a more general "don't look
3203f4a2713aSLionel Sambuc // in DeclContexts unless we have to" optimization), we can eliminate this.
3204f4a2713aSLionel Sambuc Entity = Result.getSema().Context.getTranslationUnitDecl();
3205f4a2713aSLionel Sambuc LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false,
3206f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited);
3207f4a2713aSLionel Sambuc }
3208f4a2713aSLionel Sambuc
3209f4a2713aSLionel Sambuc if (Entity) {
3210f4a2713aSLionel Sambuc // Lookup visible declarations in any namespaces found by using
3211f4a2713aSLionel Sambuc // directives.
3212f4a2713aSLionel Sambuc UnqualUsingDirectiveSet::const_iterator UI, UEnd;
3213*0a6a1f1dSLionel Sambuc std::tie(UI, UEnd) = UDirs.getNamespacesFor(Entity);
3214f4a2713aSLionel Sambuc for (; UI != UEnd; ++UI)
3215f4a2713aSLionel Sambuc LookupVisibleDecls(const_cast<DeclContext *>(UI->getNominatedNamespace()),
3216f4a2713aSLionel Sambuc Result, /*QualifiedNameLookup=*/false,
3217f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited);
3218f4a2713aSLionel Sambuc }
3219f4a2713aSLionel Sambuc
3220f4a2713aSLionel Sambuc // Lookup names in the parent scope.
3221f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3222f4a2713aSLionel Sambuc LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited);
3223f4a2713aSLionel Sambuc }
3224f4a2713aSLionel Sambuc
LookupVisibleDecls(Scope * S,LookupNameKind Kind,VisibleDeclConsumer & Consumer,bool IncludeGlobalScope)3225f4a2713aSLionel Sambuc void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind,
3226f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer,
3227f4a2713aSLionel Sambuc bool IncludeGlobalScope) {
3228f4a2713aSLionel Sambuc // Determine the set of using directives available during
3229f4a2713aSLionel Sambuc // unqualified name lookup.
3230f4a2713aSLionel Sambuc Scope *Initial = S;
3231f4a2713aSLionel Sambuc UnqualUsingDirectiveSet UDirs;
3232f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus) {
3233f4a2713aSLionel Sambuc // Find the first namespace or translation-unit scope.
3234f4a2713aSLionel Sambuc while (S && !isNamespaceOrTranslationUnitScope(S))
3235f4a2713aSLionel Sambuc S = S->getParent();
3236f4a2713aSLionel Sambuc
3237f4a2713aSLionel Sambuc UDirs.visitScopeChain(Initial, S);
3238f4a2713aSLionel Sambuc }
3239f4a2713aSLionel Sambuc UDirs.done();
3240f4a2713aSLionel Sambuc
3241f4a2713aSLionel Sambuc // Look for visible declarations.
3242f4a2713aSLionel Sambuc LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind);
3243f4a2713aSLionel Sambuc Result.setAllowHidden(Consumer.includeHiddenDecls());
3244f4a2713aSLionel Sambuc VisibleDeclsRecord Visited;
3245f4a2713aSLionel Sambuc if (!IncludeGlobalScope)
3246f4a2713aSLionel Sambuc Visited.visitedContext(Context.getTranslationUnitDecl());
3247f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3248f4a2713aSLionel Sambuc ::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited);
3249f4a2713aSLionel Sambuc }
3250f4a2713aSLionel Sambuc
LookupVisibleDecls(DeclContext * Ctx,LookupNameKind Kind,VisibleDeclConsumer & Consumer,bool IncludeGlobalScope)3251f4a2713aSLionel Sambuc void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind,
3252f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer,
3253f4a2713aSLionel Sambuc bool IncludeGlobalScope) {
3254f4a2713aSLionel Sambuc LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind);
3255f4a2713aSLionel Sambuc Result.setAllowHidden(Consumer.includeHiddenDecls());
3256f4a2713aSLionel Sambuc VisibleDeclsRecord Visited;
3257f4a2713aSLionel Sambuc if (!IncludeGlobalScope)
3258f4a2713aSLionel Sambuc Visited.visitedContext(Context.getTranslationUnitDecl());
3259f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited);
3260f4a2713aSLionel Sambuc ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true,
3261f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited);
3262f4a2713aSLionel Sambuc }
3263f4a2713aSLionel Sambuc
3264f4a2713aSLionel Sambuc /// LookupOrCreateLabel - Do a name lookup of a label with the specified name.
3265f4a2713aSLionel Sambuc /// If GnuLabelLoc is a valid source location, then this is a definition
3266f4a2713aSLionel Sambuc /// of an __label__ label name, otherwise it is a normal label definition
3267f4a2713aSLionel Sambuc /// or use.
LookupOrCreateLabel(IdentifierInfo * II,SourceLocation Loc,SourceLocation GnuLabelLoc)3268f4a2713aSLionel Sambuc LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc,
3269f4a2713aSLionel Sambuc SourceLocation GnuLabelLoc) {
3270f4a2713aSLionel Sambuc // Do a lookup to see if we have a label with this name already.
3271*0a6a1f1dSLionel Sambuc NamedDecl *Res = nullptr;
3272f4a2713aSLionel Sambuc
3273f4a2713aSLionel Sambuc if (GnuLabelLoc.isValid()) {
3274f4a2713aSLionel Sambuc // Local label definitions always shadow existing labels.
3275f4a2713aSLionel Sambuc Res = LabelDecl::Create(Context, CurContext, Loc, II, GnuLabelLoc);
3276f4a2713aSLionel Sambuc Scope *S = CurScope;
3277f4a2713aSLionel Sambuc PushOnScopeChains(Res, S, true);
3278f4a2713aSLionel Sambuc return cast<LabelDecl>(Res);
3279f4a2713aSLionel Sambuc }
3280f4a2713aSLionel Sambuc
3281f4a2713aSLionel Sambuc // Not a GNU local label.
3282f4a2713aSLionel Sambuc Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration);
3283f4a2713aSLionel Sambuc // If we found a label, check to see if it is in the same context as us.
3284f4a2713aSLionel Sambuc // When in a Block, we don't want to reuse a label in an enclosing function.
3285f4a2713aSLionel Sambuc if (Res && Res->getDeclContext() != CurContext)
3286*0a6a1f1dSLionel Sambuc Res = nullptr;
3287*0a6a1f1dSLionel Sambuc if (!Res) {
3288f4a2713aSLionel Sambuc // If not forward referenced or defined already, create the backing decl.
3289f4a2713aSLionel Sambuc Res = LabelDecl::Create(Context, CurContext, Loc, II);
3290f4a2713aSLionel Sambuc Scope *S = CurScope->getFnParent();
3291f4a2713aSLionel Sambuc assert(S && "Not in a function?");
3292f4a2713aSLionel Sambuc PushOnScopeChains(Res, S, true);
3293f4a2713aSLionel Sambuc }
3294f4a2713aSLionel Sambuc return cast<LabelDecl>(Res);
3295f4a2713aSLionel Sambuc }
3296f4a2713aSLionel Sambuc
3297f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3298f4a2713aSLionel Sambuc // Typo correction
3299f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3300f4a2713aSLionel Sambuc
isCandidateViable(CorrectionCandidateCallback & CCC,TypoCorrection & Candidate)3301*0a6a1f1dSLionel Sambuc static bool isCandidateViable(CorrectionCandidateCallback &CCC,
3302*0a6a1f1dSLionel Sambuc TypoCorrection &Candidate) {
3303*0a6a1f1dSLionel Sambuc Candidate.setCallbackDistance(CCC.RankCandidate(Candidate));
3304*0a6a1f1dSLionel Sambuc return Candidate.getEditDistance(false) != TypoCorrection::InvalidDistance;
3305f4a2713aSLionel Sambuc }
3306f4a2713aSLionel Sambuc
3307*0a6a1f1dSLionel Sambuc static void LookupPotentialTypoResult(Sema &SemaRef,
3308*0a6a1f1dSLionel Sambuc LookupResult &Res,
3309*0a6a1f1dSLionel Sambuc IdentifierInfo *Name,
3310*0a6a1f1dSLionel Sambuc Scope *S, CXXScopeSpec *SS,
3311*0a6a1f1dSLionel Sambuc DeclContext *MemberContext,
3312*0a6a1f1dSLionel Sambuc bool EnteringContext,
3313*0a6a1f1dSLionel Sambuc bool isObjCIvarLookup,
3314*0a6a1f1dSLionel Sambuc bool FindHidden);
3315f4a2713aSLionel Sambuc
3316*0a6a1f1dSLionel Sambuc /// \brief Check whether the declarations found for a typo correction are
3317*0a6a1f1dSLionel Sambuc /// visible, and if none of them are, convert the correction to an 'import
3318*0a6a1f1dSLionel Sambuc /// a module' correction.
checkCorrectionVisibility(Sema & SemaRef,TypoCorrection & TC)3319*0a6a1f1dSLionel Sambuc static void checkCorrectionVisibility(Sema &SemaRef, TypoCorrection &TC) {
3320*0a6a1f1dSLionel Sambuc if (TC.begin() == TC.end())
3321*0a6a1f1dSLionel Sambuc return;
3322*0a6a1f1dSLionel Sambuc
3323*0a6a1f1dSLionel Sambuc TypoCorrection::decl_iterator DI = TC.begin(), DE = TC.end();
3324*0a6a1f1dSLionel Sambuc
3325*0a6a1f1dSLionel Sambuc for (/**/; DI != DE; ++DI)
3326*0a6a1f1dSLionel Sambuc if (!LookupResult::isVisible(SemaRef, *DI))
3327*0a6a1f1dSLionel Sambuc break;
3328*0a6a1f1dSLionel Sambuc // Nothing to do if all decls are visible.
3329*0a6a1f1dSLionel Sambuc if (DI == DE)
3330*0a6a1f1dSLionel Sambuc return;
3331*0a6a1f1dSLionel Sambuc
3332*0a6a1f1dSLionel Sambuc llvm::SmallVector<NamedDecl*, 4> NewDecls(TC.begin(), DI);
3333*0a6a1f1dSLionel Sambuc bool AnyVisibleDecls = !NewDecls.empty();
3334*0a6a1f1dSLionel Sambuc
3335*0a6a1f1dSLionel Sambuc for (/**/; DI != DE; ++DI) {
3336*0a6a1f1dSLionel Sambuc NamedDecl *VisibleDecl = *DI;
3337*0a6a1f1dSLionel Sambuc if (!LookupResult::isVisible(SemaRef, *DI))
3338*0a6a1f1dSLionel Sambuc VisibleDecl = findAcceptableDecl(SemaRef, *DI);
3339*0a6a1f1dSLionel Sambuc
3340*0a6a1f1dSLionel Sambuc if (VisibleDecl) {
3341*0a6a1f1dSLionel Sambuc if (!AnyVisibleDecls) {
3342*0a6a1f1dSLionel Sambuc // Found a visible decl, discard all hidden ones.
3343*0a6a1f1dSLionel Sambuc AnyVisibleDecls = true;
3344*0a6a1f1dSLionel Sambuc NewDecls.clear();
3345*0a6a1f1dSLionel Sambuc }
3346*0a6a1f1dSLionel Sambuc NewDecls.push_back(VisibleDecl);
3347*0a6a1f1dSLionel Sambuc } else if (!AnyVisibleDecls && !(*DI)->isModulePrivate())
3348*0a6a1f1dSLionel Sambuc NewDecls.push_back(*DI);
3349f4a2713aSLionel Sambuc }
3350f4a2713aSLionel Sambuc
3351*0a6a1f1dSLionel Sambuc if (NewDecls.empty())
3352*0a6a1f1dSLionel Sambuc TC = TypoCorrection();
3353*0a6a1f1dSLionel Sambuc else {
3354*0a6a1f1dSLionel Sambuc TC.setCorrectionDecls(NewDecls);
3355*0a6a1f1dSLionel Sambuc TC.setRequiresImport(!AnyVisibleDecls);
3356*0a6a1f1dSLionel Sambuc }
3357f4a2713aSLionel Sambuc }
3358f4a2713aSLionel Sambuc
3359*0a6a1f1dSLionel Sambuc // Fill the supplied vector with the IdentifierInfo pointers for each piece of
3360*0a6a1f1dSLionel Sambuc // the given NestedNameSpecifier (i.e. given a NestedNameSpecifier "foo::bar::",
3361*0a6a1f1dSLionel Sambuc // fill the vector with the IdentifierInfo pointers for "foo" and "bar").
getNestedNameSpecifierIdentifiers(NestedNameSpecifier * NNS,SmallVectorImpl<const IdentifierInfo * > & Identifiers)3362*0a6a1f1dSLionel Sambuc static void getNestedNameSpecifierIdentifiers(
3363*0a6a1f1dSLionel Sambuc NestedNameSpecifier *NNS,
3364*0a6a1f1dSLionel Sambuc SmallVectorImpl<const IdentifierInfo*> &Identifiers) {
3365*0a6a1f1dSLionel Sambuc if (NestedNameSpecifier *Prefix = NNS->getPrefix())
3366*0a6a1f1dSLionel Sambuc getNestedNameSpecifierIdentifiers(Prefix, Identifiers);
3367*0a6a1f1dSLionel Sambuc else
3368*0a6a1f1dSLionel Sambuc Identifiers.clear();
3369f4a2713aSLionel Sambuc
3370*0a6a1f1dSLionel Sambuc const IdentifierInfo *II = nullptr;
3371*0a6a1f1dSLionel Sambuc
3372*0a6a1f1dSLionel Sambuc switch (NNS->getKind()) {
3373*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Identifier:
3374*0a6a1f1dSLionel Sambuc II = NNS->getAsIdentifier();
3375*0a6a1f1dSLionel Sambuc break;
3376*0a6a1f1dSLionel Sambuc
3377*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Namespace:
3378*0a6a1f1dSLionel Sambuc if (NNS->getAsNamespace()->isAnonymousNamespace())
3379*0a6a1f1dSLionel Sambuc return;
3380*0a6a1f1dSLionel Sambuc II = NNS->getAsNamespace()->getIdentifier();
3381*0a6a1f1dSLionel Sambuc break;
3382*0a6a1f1dSLionel Sambuc
3383*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::NamespaceAlias:
3384*0a6a1f1dSLionel Sambuc II = NNS->getAsNamespaceAlias()->getIdentifier();
3385*0a6a1f1dSLionel Sambuc break;
3386*0a6a1f1dSLionel Sambuc
3387*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate:
3388*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::TypeSpec:
3389*0a6a1f1dSLionel Sambuc II = QualType(NNS->getAsType(), 0).getBaseTypeIdentifier();
3390*0a6a1f1dSLionel Sambuc break;
3391*0a6a1f1dSLionel Sambuc
3392*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Global:
3393*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Super:
3394*0a6a1f1dSLionel Sambuc return;
3395*0a6a1f1dSLionel Sambuc }
3396*0a6a1f1dSLionel Sambuc
3397*0a6a1f1dSLionel Sambuc if (II)
3398*0a6a1f1dSLionel Sambuc Identifiers.push_back(II);
3399f4a2713aSLionel Sambuc }
3400f4a2713aSLionel Sambuc
FoundDecl(NamedDecl * ND,NamedDecl * Hiding,DeclContext * Ctx,bool InBaseClass)3401f4a2713aSLionel Sambuc void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding,
3402f4a2713aSLionel Sambuc DeclContext *Ctx, bool InBaseClass) {
3403f4a2713aSLionel Sambuc // Don't consider hidden names for typo correction.
3404f4a2713aSLionel Sambuc if (Hiding)
3405f4a2713aSLionel Sambuc return;
3406f4a2713aSLionel Sambuc
3407f4a2713aSLionel Sambuc // Only consider entities with identifiers for names, ignoring
3408f4a2713aSLionel Sambuc // special names (constructors, overloaded operators, selectors,
3409f4a2713aSLionel Sambuc // etc.).
3410f4a2713aSLionel Sambuc IdentifierInfo *Name = ND->getIdentifier();
3411f4a2713aSLionel Sambuc if (!Name)
3412f4a2713aSLionel Sambuc return;
3413f4a2713aSLionel Sambuc
3414f4a2713aSLionel Sambuc // Only consider visible declarations and declarations from modules with
3415f4a2713aSLionel Sambuc // names that exactly match.
3416*0a6a1f1dSLionel Sambuc if (!LookupResult::isVisible(SemaRef, ND) && Name != Typo &&
3417f4a2713aSLionel Sambuc !findAcceptableDecl(SemaRef, ND))
3418f4a2713aSLionel Sambuc return;
3419f4a2713aSLionel Sambuc
3420f4a2713aSLionel Sambuc FoundName(Name->getName());
3421f4a2713aSLionel Sambuc }
3422f4a2713aSLionel Sambuc
FoundName(StringRef Name)3423f4a2713aSLionel Sambuc void TypoCorrectionConsumer::FoundName(StringRef Name) {
3424f4a2713aSLionel Sambuc // Compute the edit distance between the typo and the name of this
3425f4a2713aSLionel Sambuc // entity, and add the identifier to the list of results.
3426*0a6a1f1dSLionel Sambuc addName(Name, nullptr);
3427f4a2713aSLionel Sambuc }
3428f4a2713aSLionel Sambuc
addKeywordResult(StringRef Keyword)3429f4a2713aSLionel Sambuc void TypoCorrectionConsumer::addKeywordResult(StringRef Keyword) {
3430f4a2713aSLionel Sambuc // Compute the edit distance between the typo and this keyword,
3431f4a2713aSLionel Sambuc // and add the keyword to the list of results.
3432*0a6a1f1dSLionel Sambuc addName(Keyword, nullptr, nullptr, true);
3433f4a2713aSLionel Sambuc }
3434f4a2713aSLionel Sambuc
addName(StringRef Name,NamedDecl * ND,NestedNameSpecifier * NNS,bool isKeyword)3435f4a2713aSLionel Sambuc void TypoCorrectionConsumer::addName(StringRef Name, NamedDecl *ND,
3436f4a2713aSLionel Sambuc NestedNameSpecifier *NNS, bool isKeyword) {
3437f4a2713aSLionel Sambuc // Use a simple length-based heuristic to determine the minimum possible
3438f4a2713aSLionel Sambuc // edit distance. If the minimum isn't good enough, bail out early.
3439*0a6a1f1dSLionel Sambuc StringRef TypoStr = Typo->getName();
3440*0a6a1f1dSLionel Sambuc unsigned MinED = abs((int)Name.size() - (int)TypoStr.size());
3441*0a6a1f1dSLionel Sambuc if (MinED && TypoStr.size() / MinED < 3)
3442f4a2713aSLionel Sambuc return;
3443f4a2713aSLionel Sambuc
3444f4a2713aSLionel Sambuc // Compute an upper bound on the allowable edit distance, so that the
3445f4a2713aSLionel Sambuc // edit-distance algorithm can short-circuit.
3446*0a6a1f1dSLionel Sambuc unsigned UpperBound = (TypoStr.size() + 2) / 3 + 1;
3447*0a6a1f1dSLionel Sambuc unsigned ED = TypoStr.edit_distance(Name, true, UpperBound);
3448f4a2713aSLionel Sambuc if (ED >= UpperBound) return;
3449f4a2713aSLionel Sambuc
3450f4a2713aSLionel Sambuc TypoCorrection TC(&SemaRef.Context.Idents.get(Name), ND, NNS, ED);
3451f4a2713aSLionel Sambuc if (isKeyword) TC.makeKeyword();
3452*0a6a1f1dSLionel Sambuc TC.setCorrectionRange(nullptr, Result.getLookupNameInfo());
3453f4a2713aSLionel Sambuc addCorrection(TC);
3454f4a2713aSLionel Sambuc }
3455f4a2713aSLionel Sambuc
3456*0a6a1f1dSLionel Sambuc static const unsigned MaxTypoDistanceResultSets = 5;
3457*0a6a1f1dSLionel Sambuc
addCorrection(TypoCorrection Correction)3458f4a2713aSLionel Sambuc void TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) {
3459*0a6a1f1dSLionel Sambuc StringRef TypoStr = Typo->getName();
3460f4a2713aSLionel Sambuc StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName();
3461*0a6a1f1dSLionel Sambuc
3462*0a6a1f1dSLionel Sambuc // For very short typos, ignore potential corrections that have a different
3463*0a6a1f1dSLionel Sambuc // base identifier from the typo or which have a normalized edit distance
3464*0a6a1f1dSLionel Sambuc // longer than the typo itself.
3465*0a6a1f1dSLionel Sambuc if (TypoStr.size() < 3 &&
3466*0a6a1f1dSLionel Sambuc (Name != TypoStr || Correction.getEditDistance(true) > TypoStr.size()))
3467*0a6a1f1dSLionel Sambuc return;
3468*0a6a1f1dSLionel Sambuc
3469*0a6a1f1dSLionel Sambuc // If the correction is resolved but is not viable, ignore it.
3470*0a6a1f1dSLionel Sambuc if (Correction.isResolved()) {
3471*0a6a1f1dSLionel Sambuc checkCorrectionVisibility(SemaRef, Correction);
3472*0a6a1f1dSLionel Sambuc if (!Correction || !isCandidateViable(*CorrectionValidator, Correction))
3473*0a6a1f1dSLionel Sambuc return;
3474*0a6a1f1dSLionel Sambuc }
3475*0a6a1f1dSLionel Sambuc
3476f4a2713aSLionel Sambuc TypoResultList &CList =
3477f4a2713aSLionel Sambuc CorrectionResults[Correction.getEditDistance(false)][Name];
3478f4a2713aSLionel Sambuc
3479f4a2713aSLionel Sambuc if (!CList.empty() && !CList.back().isResolved())
3480f4a2713aSLionel Sambuc CList.pop_back();
3481f4a2713aSLionel Sambuc if (NamedDecl *NewND = Correction.getCorrectionDecl()) {
3482f4a2713aSLionel Sambuc std::string CorrectionStr = Correction.getAsString(SemaRef.getLangOpts());
3483f4a2713aSLionel Sambuc for (TypoResultList::iterator RI = CList.begin(), RIEnd = CList.end();
3484f4a2713aSLionel Sambuc RI != RIEnd; ++RI) {
3485f4a2713aSLionel Sambuc // If the Correction refers to a decl already in the result list,
3486f4a2713aSLionel Sambuc // replace the existing result if the string representation of Correction
3487f4a2713aSLionel Sambuc // comes before the current result alphabetically, then stop as there is
3488f4a2713aSLionel Sambuc // nothing more to be done to add Correction to the candidate set.
3489f4a2713aSLionel Sambuc if (RI->getCorrectionDecl() == NewND) {
3490f4a2713aSLionel Sambuc if (CorrectionStr < RI->getAsString(SemaRef.getLangOpts()))
3491f4a2713aSLionel Sambuc *RI = Correction;
3492f4a2713aSLionel Sambuc return;
3493f4a2713aSLionel Sambuc }
3494f4a2713aSLionel Sambuc }
3495f4a2713aSLionel Sambuc }
3496f4a2713aSLionel Sambuc if (CList.empty() || Correction.isResolved())
3497f4a2713aSLionel Sambuc CList.push_back(Correction);
3498f4a2713aSLionel Sambuc
3499f4a2713aSLionel Sambuc while (CorrectionResults.size() > MaxTypoDistanceResultSets)
3500*0a6a1f1dSLionel Sambuc CorrectionResults.erase(std::prev(CorrectionResults.end()));
3501f4a2713aSLionel Sambuc }
3502f4a2713aSLionel Sambuc
addNamespaces(const llvm::MapVector<NamespaceDecl *,bool> & KnownNamespaces)3503*0a6a1f1dSLionel Sambuc void TypoCorrectionConsumer::addNamespaces(
3504*0a6a1f1dSLionel Sambuc const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces) {
3505*0a6a1f1dSLionel Sambuc SearchNamespaces = true;
3506f4a2713aSLionel Sambuc
3507*0a6a1f1dSLionel Sambuc for (auto KNPair : KnownNamespaces)
3508*0a6a1f1dSLionel Sambuc Namespaces.addNameSpecifier(KNPair.first);
3509f4a2713aSLionel Sambuc
3510*0a6a1f1dSLionel Sambuc bool SSIsTemplate = false;
3511*0a6a1f1dSLionel Sambuc if (NestedNameSpecifier *NNS =
3512*0a6a1f1dSLionel Sambuc (SS && SS->isValid()) ? SS->getScopeRep() : nullptr) {
3513*0a6a1f1dSLionel Sambuc if (const Type *T = NNS->getAsType())
3514*0a6a1f1dSLionel Sambuc SSIsTemplate = T->getTypeClass() == Type::TemplateSpecialization;
3515*0a6a1f1dSLionel Sambuc }
3516*0a6a1f1dSLionel Sambuc for (const auto *TI : SemaRef.getASTContext().types()) {
3517*0a6a1f1dSLionel Sambuc if (CXXRecordDecl *CD = TI->getAsCXXRecordDecl()) {
3518*0a6a1f1dSLionel Sambuc CD = CD->getCanonicalDecl();
3519*0a6a1f1dSLionel Sambuc if (!CD->isDependentType() && !CD->isAnonymousStructOrUnion() &&
3520*0a6a1f1dSLionel Sambuc !CD->isUnion() && CD->getIdentifier() &&
3521*0a6a1f1dSLionel Sambuc (SSIsTemplate || !isa<ClassTemplateSpecializationDecl>(CD)) &&
3522*0a6a1f1dSLionel Sambuc (CD->isBeingDefined() || CD->isCompleteDefinition()))
3523*0a6a1f1dSLionel Sambuc Namespaces.addNameSpecifier(CD);
3524*0a6a1f1dSLionel Sambuc }
3525*0a6a1f1dSLionel Sambuc }
3526f4a2713aSLionel Sambuc }
3527f4a2713aSLionel Sambuc
getNextCorrection()3528*0a6a1f1dSLionel Sambuc const TypoCorrection &TypoCorrectionConsumer::getNextCorrection() {
3529*0a6a1f1dSLionel Sambuc if (++CurrentTCIndex < ValidatedCorrections.size())
3530*0a6a1f1dSLionel Sambuc return ValidatedCorrections[CurrentTCIndex];
3531*0a6a1f1dSLionel Sambuc
3532*0a6a1f1dSLionel Sambuc CurrentTCIndex = ValidatedCorrections.size();
3533*0a6a1f1dSLionel Sambuc while (!CorrectionResults.empty()) {
3534*0a6a1f1dSLionel Sambuc auto DI = CorrectionResults.begin();
3535*0a6a1f1dSLionel Sambuc if (DI->second.empty()) {
3536*0a6a1f1dSLionel Sambuc CorrectionResults.erase(DI);
3537*0a6a1f1dSLionel Sambuc continue;
3538f4a2713aSLionel Sambuc }
3539f4a2713aSLionel Sambuc
3540*0a6a1f1dSLionel Sambuc auto RI = DI->second.begin();
3541*0a6a1f1dSLionel Sambuc if (RI->second.empty()) {
3542*0a6a1f1dSLionel Sambuc DI->second.erase(RI);
3543*0a6a1f1dSLionel Sambuc performQualifiedLookups();
3544*0a6a1f1dSLionel Sambuc continue;
3545*0a6a1f1dSLionel Sambuc }
3546f4a2713aSLionel Sambuc
3547*0a6a1f1dSLionel Sambuc TypoCorrection TC = RI->second.pop_back_val();
3548*0a6a1f1dSLionel Sambuc if (TC.isResolved() || TC.requiresImport() || resolveCorrection(TC)) {
3549*0a6a1f1dSLionel Sambuc ValidatedCorrections.push_back(TC);
3550*0a6a1f1dSLionel Sambuc return ValidatedCorrections[CurrentTCIndex];
3551*0a6a1f1dSLionel Sambuc }
3552*0a6a1f1dSLionel Sambuc }
3553*0a6a1f1dSLionel Sambuc return ValidatedCorrections[0]; // The empty correction.
3554*0a6a1f1dSLionel Sambuc }
3555f4a2713aSLionel Sambuc
resolveCorrection(TypoCorrection & Candidate)3556*0a6a1f1dSLionel Sambuc bool TypoCorrectionConsumer::resolveCorrection(TypoCorrection &Candidate) {
3557*0a6a1f1dSLionel Sambuc IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo();
3558*0a6a1f1dSLionel Sambuc DeclContext *TempMemberContext = MemberContext;
3559*0a6a1f1dSLionel Sambuc CXXScopeSpec *TempSS = SS.get();
3560*0a6a1f1dSLionel Sambuc retry_lookup:
3561*0a6a1f1dSLionel Sambuc LookupPotentialTypoResult(SemaRef, Result, Name, S, TempSS, TempMemberContext,
3562*0a6a1f1dSLionel Sambuc EnteringContext,
3563*0a6a1f1dSLionel Sambuc CorrectionValidator->IsObjCIvarLookup,
3564*0a6a1f1dSLionel Sambuc Name == Typo && !Candidate.WillReplaceSpecifier());
3565*0a6a1f1dSLionel Sambuc switch (Result.getResultKind()) {
3566*0a6a1f1dSLionel Sambuc case LookupResult::NotFound:
3567*0a6a1f1dSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation:
3568*0a6a1f1dSLionel Sambuc case LookupResult::FoundUnresolvedValue:
3569*0a6a1f1dSLionel Sambuc if (TempSS) {
3570*0a6a1f1dSLionel Sambuc // Immediately retry the lookup without the given CXXScopeSpec
3571*0a6a1f1dSLionel Sambuc TempSS = nullptr;
3572*0a6a1f1dSLionel Sambuc Candidate.WillReplaceSpecifier(true);
3573*0a6a1f1dSLionel Sambuc goto retry_lookup;
3574*0a6a1f1dSLionel Sambuc }
3575*0a6a1f1dSLionel Sambuc if (TempMemberContext) {
3576*0a6a1f1dSLionel Sambuc if (SS && !TempSS)
3577*0a6a1f1dSLionel Sambuc TempSS = SS.get();
3578*0a6a1f1dSLionel Sambuc TempMemberContext = nullptr;
3579*0a6a1f1dSLionel Sambuc goto retry_lookup;
3580*0a6a1f1dSLionel Sambuc }
3581*0a6a1f1dSLionel Sambuc if (SearchNamespaces)
3582*0a6a1f1dSLionel Sambuc QualifiedResults.push_back(Candidate);
3583*0a6a1f1dSLionel Sambuc break;
3584f4a2713aSLionel Sambuc
3585*0a6a1f1dSLionel Sambuc case LookupResult::Ambiguous:
3586*0a6a1f1dSLionel Sambuc // We don't deal with ambiguities.
3587*0a6a1f1dSLionel Sambuc break;
3588f4a2713aSLionel Sambuc
3589*0a6a1f1dSLionel Sambuc case LookupResult::Found:
3590*0a6a1f1dSLionel Sambuc case LookupResult::FoundOverloaded:
3591*0a6a1f1dSLionel Sambuc // Store all of the Decls for overloaded symbols
3592*0a6a1f1dSLionel Sambuc for (auto *TRD : Result)
3593*0a6a1f1dSLionel Sambuc Candidate.addCorrectionDecl(TRD);
3594*0a6a1f1dSLionel Sambuc checkCorrectionVisibility(SemaRef, Candidate);
3595*0a6a1f1dSLionel Sambuc if (!isCandidateViable(*CorrectionValidator, Candidate)) {
3596*0a6a1f1dSLionel Sambuc if (SearchNamespaces)
3597*0a6a1f1dSLionel Sambuc QualifiedResults.push_back(Candidate);
3598*0a6a1f1dSLionel Sambuc break;
3599*0a6a1f1dSLionel Sambuc }
3600*0a6a1f1dSLionel Sambuc Candidate.setCorrectionRange(SS.get(), Result.getLookupNameInfo());
3601*0a6a1f1dSLionel Sambuc return true;
3602*0a6a1f1dSLionel Sambuc }
3603*0a6a1f1dSLionel Sambuc return false;
3604*0a6a1f1dSLionel Sambuc }
3605f4a2713aSLionel Sambuc
performQualifiedLookups()3606*0a6a1f1dSLionel Sambuc void TypoCorrectionConsumer::performQualifiedLookups() {
3607*0a6a1f1dSLionel Sambuc unsigned TypoLen = Typo->getName().size();
3608*0a6a1f1dSLionel Sambuc for (auto QR : QualifiedResults) {
3609*0a6a1f1dSLionel Sambuc for (auto NSI : Namespaces) {
3610*0a6a1f1dSLionel Sambuc DeclContext *Ctx = NSI.DeclCtx;
3611*0a6a1f1dSLionel Sambuc const Type *NSType = NSI.NameSpecifier->getAsType();
3612f4a2713aSLionel Sambuc
3613*0a6a1f1dSLionel Sambuc // If the current NestedNameSpecifier refers to a class and the
3614*0a6a1f1dSLionel Sambuc // current correction candidate is the name of that class, then skip
3615*0a6a1f1dSLionel Sambuc // it as it is unlikely a qualified version of the class' constructor
3616*0a6a1f1dSLionel Sambuc // is an appropriate correction.
3617*0a6a1f1dSLionel Sambuc if (CXXRecordDecl *NSDecl = NSType ? NSType->getAsCXXRecordDecl() : 0) {
3618*0a6a1f1dSLionel Sambuc if (NSDecl->getIdentifier() == QR.getCorrectionAsIdentifierInfo())
3619*0a6a1f1dSLionel Sambuc continue;
3620*0a6a1f1dSLionel Sambuc }
3621f4a2713aSLionel Sambuc
3622*0a6a1f1dSLionel Sambuc TypoCorrection TC(QR);
3623*0a6a1f1dSLionel Sambuc TC.ClearCorrectionDecls();
3624*0a6a1f1dSLionel Sambuc TC.setCorrectionSpecifier(NSI.NameSpecifier);
3625*0a6a1f1dSLionel Sambuc TC.setQualifierDistance(NSI.EditDistance);
3626*0a6a1f1dSLionel Sambuc TC.setCallbackDistance(0); // Reset the callback distance
3627f4a2713aSLionel Sambuc
3628*0a6a1f1dSLionel Sambuc // If the current correction candidate and namespace combination are
3629*0a6a1f1dSLionel Sambuc // too far away from the original typo based on the normalized edit
3630*0a6a1f1dSLionel Sambuc // distance, then skip performing a qualified name lookup.
3631*0a6a1f1dSLionel Sambuc unsigned TmpED = TC.getEditDistance(true);
3632*0a6a1f1dSLionel Sambuc if (QR.getCorrectionAsIdentifierInfo() != Typo && TmpED &&
3633*0a6a1f1dSLionel Sambuc TypoLen / TmpED < 3)
3634*0a6a1f1dSLionel Sambuc continue;
3635*0a6a1f1dSLionel Sambuc
3636*0a6a1f1dSLionel Sambuc Result.clear();
3637*0a6a1f1dSLionel Sambuc Result.setLookupName(QR.getCorrectionAsIdentifierInfo());
3638*0a6a1f1dSLionel Sambuc if (!SemaRef.LookupQualifiedName(Result, Ctx))
3639*0a6a1f1dSLionel Sambuc continue;
3640*0a6a1f1dSLionel Sambuc
3641*0a6a1f1dSLionel Sambuc // Any corrections added below will be validated in subsequent
3642*0a6a1f1dSLionel Sambuc // iterations of the main while() loop over the Consumer's contents.
3643*0a6a1f1dSLionel Sambuc switch (Result.getResultKind()) {
3644*0a6a1f1dSLionel Sambuc case LookupResult::Found:
3645*0a6a1f1dSLionel Sambuc case LookupResult::FoundOverloaded: {
3646*0a6a1f1dSLionel Sambuc if (SS && SS->isValid()) {
3647*0a6a1f1dSLionel Sambuc std::string NewQualified = TC.getAsString(SemaRef.getLangOpts());
3648*0a6a1f1dSLionel Sambuc std::string OldQualified;
3649*0a6a1f1dSLionel Sambuc llvm::raw_string_ostream OldOStream(OldQualified);
3650*0a6a1f1dSLionel Sambuc SS->getScopeRep()->print(OldOStream, SemaRef.getPrintingPolicy());
3651*0a6a1f1dSLionel Sambuc OldOStream << Typo->getName();
3652*0a6a1f1dSLionel Sambuc // If correction candidate would be an identical written qualified
3653*0a6a1f1dSLionel Sambuc // identifer, then the existing CXXScopeSpec probably included a
3654*0a6a1f1dSLionel Sambuc // typedef that didn't get accounted for properly.
3655*0a6a1f1dSLionel Sambuc if (OldOStream.str() == NewQualified)
3656*0a6a1f1dSLionel Sambuc break;
3657*0a6a1f1dSLionel Sambuc }
3658*0a6a1f1dSLionel Sambuc for (LookupResult::iterator TRD = Result.begin(), TRDEnd = Result.end();
3659*0a6a1f1dSLionel Sambuc TRD != TRDEnd; ++TRD) {
3660*0a6a1f1dSLionel Sambuc if (SemaRef.CheckMemberAccess(TC.getCorrectionRange().getBegin(),
3661*0a6a1f1dSLionel Sambuc NSType ? NSType->getAsCXXRecordDecl()
3662*0a6a1f1dSLionel Sambuc : nullptr,
3663*0a6a1f1dSLionel Sambuc TRD.getPair()) == Sema::AR_accessible)
3664*0a6a1f1dSLionel Sambuc TC.addCorrectionDecl(*TRD);
3665*0a6a1f1dSLionel Sambuc }
3666*0a6a1f1dSLionel Sambuc if (TC.isResolved()) {
3667*0a6a1f1dSLionel Sambuc TC.setCorrectionRange(SS.get(), Result.getLookupNameInfo());
3668*0a6a1f1dSLionel Sambuc addCorrection(TC);
3669*0a6a1f1dSLionel Sambuc }
3670*0a6a1f1dSLionel Sambuc break;
3671*0a6a1f1dSLionel Sambuc }
3672*0a6a1f1dSLionel Sambuc case LookupResult::NotFound:
3673*0a6a1f1dSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation:
3674*0a6a1f1dSLionel Sambuc case LookupResult::Ambiguous:
3675*0a6a1f1dSLionel Sambuc case LookupResult::FoundUnresolvedValue:
3676*0a6a1f1dSLionel Sambuc break;
3677*0a6a1f1dSLionel Sambuc }
3678*0a6a1f1dSLionel Sambuc }
3679*0a6a1f1dSLionel Sambuc }
3680*0a6a1f1dSLionel Sambuc QualifiedResults.clear();
3681*0a6a1f1dSLionel Sambuc }
3682*0a6a1f1dSLionel Sambuc
NamespaceSpecifierSet(ASTContext & Context,DeclContext * CurContext,CXXScopeSpec * CurScopeSpec)3683*0a6a1f1dSLionel Sambuc TypoCorrectionConsumer::NamespaceSpecifierSet::NamespaceSpecifierSet(
3684*0a6a1f1dSLionel Sambuc ASTContext &Context, DeclContext *CurContext, CXXScopeSpec *CurScopeSpec)
3685*0a6a1f1dSLionel Sambuc : Context(Context), CurContextChain(buildContextChain(CurContext)),
3686f4a2713aSLionel Sambuc isSorted(false) {
3687f4a2713aSLionel Sambuc if (NestedNameSpecifier *NNS =
3688*0a6a1f1dSLionel Sambuc CurScopeSpec ? CurScopeSpec->getScopeRep() : nullptr) {
3689f4a2713aSLionel Sambuc llvm::raw_string_ostream SpecifierOStream(CurNameSpecifier);
3690f4a2713aSLionel Sambuc NNS->print(SpecifierOStream, Context.getPrintingPolicy());
3691f4a2713aSLionel Sambuc
3692f4a2713aSLionel Sambuc getNestedNameSpecifierIdentifiers(NNS, CurNameSpecifierIdentifiers);
3693f4a2713aSLionel Sambuc }
3694f4a2713aSLionel Sambuc // Build the list of identifiers that would be used for an absolute
3695f4a2713aSLionel Sambuc // (from the global context) NestedNameSpecifier referring to the current
3696f4a2713aSLionel Sambuc // context.
3697f4a2713aSLionel Sambuc for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(),
3698f4a2713aSLionel Sambuc CEnd = CurContextChain.rend();
3699f4a2713aSLionel Sambuc C != CEnd; ++C) {
3700f4a2713aSLionel Sambuc if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C))
3701f4a2713aSLionel Sambuc CurContextIdentifiers.push_back(ND->getIdentifier());
3702f4a2713aSLionel Sambuc }
3703f4a2713aSLionel Sambuc
3704f4a2713aSLionel Sambuc // Add the global context as a NestedNameSpecifier
3705f4a2713aSLionel Sambuc Distances.insert(1);
3706*0a6a1f1dSLionel Sambuc SpecifierInfo SI = {cast<DeclContext>(Context.getTranslationUnitDecl()),
3707*0a6a1f1dSLionel Sambuc NestedNameSpecifier::GlobalSpecifier(Context), 1};
3708*0a6a1f1dSLionel Sambuc DistanceMap[1].push_back(SI);
3709f4a2713aSLionel Sambuc }
3710f4a2713aSLionel Sambuc
buildContextChain(DeclContext * Start)3711*0a6a1f1dSLionel Sambuc auto TypoCorrectionConsumer::NamespaceSpecifierSet::buildContextChain(
3712*0a6a1f1dSLionel Sambuc DeclContext *Start) -> DeclContextList {
3713f4a2713aSLionel Sambuc assert(Start && "Building a context chain from a null context");
3714f4a2713aSLionel Sambuc DeclContextList Chain;
3715*0a6a1f1dSLionel Sambuc for (DeclContext *DC = Start->getPrimaryContext(); DC != nullptr;
3716f4a2713aSLionel Sambuc DC = DC->getLookupParent()) {
3717f4a2713aSLionel Sambuc NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(DC);
3718f4a2713aSLionel Sambuc if (!DC->isInlineNamespace() && !DC->isTransparentContext() &&
3719f4a2713aSLionel Sambuc !(ND && ND->isAnonymousNamespace()))
3720f4a2713aSLionel Sambuc Chain.push_back(DC->getPrimaryContext());
3721f4a2713aSLionel Sambuc }
3722f4a2713aSLionel Sambuc return Chain;
3723f4a2713aSLionel Sambuc }
3724f4a2713aSLionel Sambuc
sortNamespaces()3725*0a6a1f1dSLionel Sambuc void TypoCorrectionConsumer::NamespaceSpecifierSet::sortNamespaces() {
3726f4a2713aSLionel Sambuc SmallVector<unsigned, 4> sortedDistances;
3727f4a2713aSLionel Sambuc sortedDistances.append(Distances.begin(), Distances.end());
3728f4a2713aSLionel Sambuc
3729f4a2713aSLionel Sambuc if (sortedDistances.size() > 1)
3730f4a2713aSLionel Sambuc std::sort(sortedDistances.begin(), sortedDistances.end());
3731f4a2713aSLionel Sambuc
3732f4a2713aSLionel Sambuc Specifiers.clear();
3733*0a6a1f1dSLionel Sambuc for (auto D : sortedDistances) {
3734*0a6a1f1dSLionel Sambuc SpecifierInfoList &SpecList = DistanceMap[D];
3735f4a2713aSLionel Sambuc Specifiers.append(SpecList.begin(), SpecList.end());
3736f4a2713aSLionel Sambuc }
3737f4a2713aSLionel Sambuc
3738f4a2713aSLionel Sambuc isSorted = true;
3739f4a2713aSLionel Sambuc }
3740f4a2713aSLionel Sambuc
3741*0a6a1f1dSLionel Sambuc unsigned
buildNestedNameSpecifier(DeclContextList & DeclChain,NestedNameSpecifier * & NNS)3742*0a6a1f1dSLionel Sambuc TypoCorrectionConsumer::NamespaceSpecifierSet::buildNestedNameSpecifier(
3743*0a6a1f1dSLionel Sambuc DeclContextList &DeclChain, NestedNameSpecifier *&NNS) {
3744f4a2713aSLionel Sambuc unsigned NumSpecifiers = 0;
3745f4a2713aSLionel Sambuc for (DeclContextList::reverse_iterator C = DeclChain.rbegin(),
3746f4a2713aSLionel Sambuc CEnd = DeclChain.rend();
3747f4a2713aSLionel Sambuc C != CEnd; ++C) {
3748f4a2713aSLionel Sambuc if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) {
3749f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::Create(Context, NNS, ND);
3750f4a2713aSLionel Sambuc ++NumSpecifiers;
3751f4a2713aSLionel Sambuc } else if (RecordDecl *RD = dyn_cast_or_null<RecordDecl>(*C)) {
3752f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::Create(Context, NNS, RD->isTemplateDecl(),
3753f4a2713aSLionel Sambuc RD->getTypeForDecl());
3754f4a2713aSLionel Sambuc ++NumSpecifiers;
3755f4a2713aSLionel Sambuc }
3756f4a2713aSLionel Sambuc }
3757f4a2713aSLionel Sambuc return NumSpecifiers;
3758f4a2713aSLionel Sambuc }
3759f4a2713aSLionel Sambuc
addNameSpecifier(DeclContext * Ctx)3760*0a6a1f1dSLionel Sambuc void TypoCorrectionConsumer::NamespaceSpecifierSet::addNameSpecifier(
3761*0a6a1f1dSLionel Sambuc DeclContext *Ctx) {
3762*0a6a1f1dSLionel Sambuc NestedNameSpecifier *NNS = nullptr;
3763f4a2713aSLionel Sambuc unsigned NumSpecifiers = 0;
3764*0a6a1f1dSLionel Sambuc DeclContextList NamespaceDeclChain(buildContextChain(Ctx));
3765f4a2713aSLionel Sambuc DeclContextList FullNamespaceDeclChain(NamespaceDeclChain);
3766f4a2713aSLionel Sambuc
3767f4a2713aSLionel Sambuc // Eliminate common elements from the two DeclContext chains.
3768f4a2713aSLionel Sambuc for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(),
3769f4a2713aSLionel Sambuc CEnd = CurContextChain.rend();
3770f4a2713aSLionel Sambuc C != CEnd && !NamespaceDeclChain.empty() &&
3771f4a2713aSLionel Sambuc NamespaceDeclChain.back() == *C; ++C) {
3772f4a2713aSLionel Sambuc NamespaceDeclChain.pop_back();
3773f4a2713aSLionel Sambuc }
3774f4a2713aSLionel Sambuc
3775f4a2713aSLionel Sambuc // Build the NestedNameSpecifier from what is left of the NamespaceDeclChain
3776*0a6a1f1dSLionel Sambuc NumSpecifiers = buildNestedNameSpecifier(NamespaceDeclChain, NNS);
3777f4a2713aSLionel Sambuc
3778f4a2713aSLionel Sambuc // Add an explicit leading '::' specifier if needed.
3779f4a2713aSLionel Sambuc if (NamespaceDeclChain.empty()) {
3780f4a2713aSLionel Sambuc // Rebuild the NestedNameSpecifier as a globally-qualified specifier.
3781f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::GlobalSpecifier(Context);
3782f4a2713aSLionel Sambuc NumSpecifiers =
3783*0a6a1f1dSLionel Sambuc buildNestedNameSpecifier(FullNamespaceDeclChain, NNS);
3784f4a2713aSLionel Sambuc } else if (NamedDecl *ND =
3785f4a2713aSLionel Sambuc dyn_cast_or_null<NamedDecl>(NamespaceDeclChain.back())) {
3786f4a2713aSLionel Sambuc IdentifierInfo *Name = ND->getIdentifier();
3787f4a2713aSLionel Sambuc bool SameNameSpecifier = false;
3788f4a2713aSLionel Sambuc if (std::find(CurNameSpecifierIdentifiers.begin(),
3789f4a2713aSLionel Sambuc CurNameSpecifierIdentifiers.end(),
3790f4a2713aSLionel Sambuc Name) != CurNameSpecifierIdentifiers.end()) {
3791f4a2713aSLionel Sambuc std::string NewNameSpecifier;
3792f4a2713aSLionel Sambuc llvm::raw_string_ostream SpecifierOStream(NewNameSpecifier);
3793f4a2713aSLionel Sambuc SmallVector<const IdentifierInfo *, 4> NewNameSpecifierIdentifiers;
3794f4a2713aSLionel Sambuc getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers);
3795f4a2713aSLionel Sambuc NNS->print(SpecifierOStream, Context.getPrintingPolicy());
3796f4a2713aSLionel Sambuc SpecifierOStream.flush();
3797f4a2713aSLionel Sambuc SameNameSpecifier = NewNameSpecifier == CurNameSpecifier;
3798f4a2713aSLionel Sambuc }
3799f4a2713aSLionel Sambuc if (SameNameSpecifier ||
3800f4a2713aSLionel Sambuc std::find(CurContextIdentifiers.begin(), CurContextIdentifiers.end(),
3801f4a2713aSLionel Sambuc Name) != CurContextIdentifiers.end()) {
3802f4a2713aSLionel Sambuc // Rebuild the NestedNameSpecifier as a globally-qualified specifier.
3803f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::GlobalSpecifier(Context);
3804f4a2713aSLionel Sambuc NumSpecifiers =
3805*0a6a1f1dSLionel Sambuc buildNestedNameSpecifier(FullNamespaceDeclChain, NNS);
3806f4a2713aSLionel Sambuc }
3807f4a2713aSLionel Sambuc }
3808f4a2713aSLionel Sambuc
3809f4a2713aSLionel Sambuc // If the built NestedNameSpecifier would be replacing an existing
3810f4a2713aSLionel Sambuc // NestedNameSpecifier, use the number of component identifiers that
3811f4a2713aSLionel Sambuc // would need to be changed as the edit distance instead of the number
3812f4a2713aSLionel Sambuc // of components in the built NestedNameSpecifier.
3813f4a2713aSLionel Sambuc if (NNS && !CurNameSpecifierIdentifiers.empty()) {
3814f4a2713aSLionel Sambuc SmallVector<const IdentifierInfo*, 4> NewNameSpecifierIdentifiers;
3815f4a2713aSLionel Sambuc getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers);
3816f4a2713aSLionel Sambuc NumSpecifiers = llvm::ComputeEditDistance(
3817*0a6a1f1dSLionel Sambuc llvm::makeArrayRef(CurNameSpecifierIdentifiers),
3818*0a6a1f1dSLionel Sambuc llvm::makeArrayRef(NewNameSpecifierIdentifiers));
3819f4a2713aSLionel Sambuc }
3820f4a2713aSLionel Sambuc
3821f4a2713aSLionel Sambuc isSorted = false;
3822f4a2713aSLionel Sambuc Distances.insert(NumSpecifiers);
3823*0a6a1f1dSLionel Sambuc SpecifierInfo SI = {Ctx, NNS, NumSpecifiers};
3824*0a6a1f1dSLionel Sambuc DistanceMap[NumSpecifiers].push_back(SI);
3825f4a2713aSLionel Sambuc }
3826f4a2713aSLionel Sambuc
3827f4a2713aSLionel Sambuc /// \brief Perform name lookup for a possible result for typo correction.
LookupPotentialTypoResult(Sema & SemaRef,LookupResult & Res,IdentifierInfo * Name,Scope * S,CXXScopeSpec * SS,DeclContext * MemberContext,bool EnteringContext,bool isObjCIvarLookup,bool FindHidden)3828f4a2713aSLionel Sambuc static void LookupPotentialTypoResult(Sema &SemaRef,
3829f4a2713aSLionel Sambuc LookupResult &Res,
3830f4a2713aSLionel Sambuc IdentifierInfo *Name,
3831f4a2713aSLionel Sambuc Scope *S, CXXScopeSpec *SS,
3832f4a2713aSLionel Sambuc DeclContext *MemberContext,
3833f4a2713aSLionel Sambuc bool EnteringContext,
3834f4a2713aSLionel Sambuc bool isObjCIvarLookup,
3835f4a2713aSLionel Sambuc bool FindHidden) {
3836f4a2713aSLionel Sambuc Res.suppressDiagnostics();
3837f4a2713aSLionel Sambuc Res.clear();
3838f4a2713aSLionel Sambuc Res.setLookupName(Name);
3839f4a2713aSLionel Sambuc Res.setAllowHidden(FindHidden);
3840f4a2713aSLionel Sambuc if (MemberContext) {
3841f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) {
3842f4a2713aSLionel Sambuc if (isObjCIvarLookup) {
3843f4a2713aSLionel Sambuc if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) {
3844f4a2713aSLionel Sambuc Res.addDecl(Ivar);
3845f4a2713aSLionel Sambuc Res.resolveKind();
3846f4a2713aSLionel Sambuc return;
3847f4a2713aSLionel Sambuc }
3848f4a2713aSLionel Sambuc }
3849f4a2713aSLionel Sambuc
3850f4a2713aSLionel Sambuc if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration(Name)) {
3851f4a2713aSLionel Sambuc Res.addDecl(Prop);
3852f4a2713aSLionel Sambuc Res.resolveKind();
3853f4a2713aSLionel Sambuc return;
3854f4a2713aSLionel Sambuc }
3855f4a2713aSLionel Sambuc }
3856f4a2713aSLionel Sambuc
3857f4a2713aSLionel Sambuc SemaRef.LookupQualifiedName(Res, MemberContext);
3858f4a2713aSLionel Sambuc return;
3859f4a2713aSLionel Sambuc }
3860f4a2713aSLionel Sambuc
3861f4a2713aSLionel Sambuc SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false,
3862f4a2713aSLionel Sambuc EnteringContext);
3863f4a2713aSLionel Sambuc
3864f4a2713aSLionel Sambuc // Fake ivar lookup; this should really be part of
3865f4a2713aSLionel Sambuc // LookupParsedName.
3866f4a2713aSLionel Sambuc if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) {
3867f4a2713aSLionel Sambuc if (Method->isInstanceMethod() && Method->getClassInterface() &&
3868f4a2713aSLionel Sambuc (Res.empty() ||
3869f4a2713aSLionel Sambuc (Res.isSingleResult() &&
3870f4a2713aSLionel Sambuc Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()))) {
3871f4a2713aSLionel Sambuc if (ObjCIvarDecl *IV
3872f4a2713aSLionel Sambuc = Method->getClassInterface()->lookupInstanceVariable(Name)) {
3873f4a2713aSLionel Sambuc Res.addDecl(IV);
3874f4a2713aSLionel Sambuc Res.resolveKind();
3875f4a2713aSLionel Sambuc }
3876f4a2713aSLionel Sambuc }
3877f4a2713aSLionel Sambuc }
3878f4a2713aSLionel Sambuc }
3879f4a2713aSLionel Sambuc
3880f4a2713aSLionel Sambuc /// \brief Add keywords to the consumer as possible typo corrections.
AddKeywordsToConsumer(Sema & SemaRef,TypoCorrectionConsumer & Consumer,Scope * S,CorrectionCandidateCallback & CCC,bool AfterNestedNameSpecifier)3881f4a2713aSLionel Sambuc static void AddKeywordsToConsumer(Sema &SemaRef,
3882f4a2713aSLionel Sambuc TypoCorrectionConsumer &Consumer,
3883f4a2713aSLionel Sambuc Scope *S, CorrectionCandidateCallback &CCC,
3884f4a2713aSLionel Sambuc bool AfterNestedNameSpecifier) {
3885f4a2713aSLionel Sambuc if (AfterNestedNameSpecifier) {
3886f4a2713aSLionel Sambuc // For 'X::', we know exactly which keywords can appear next.
3887f4a2713aSLionel Sambuc Consumer.addKeywordResult("template");
3888f4a2713aSLionel Sambuc if (CCC.WantExpressionKeywords)
3889f4a2713aSLionel Sambuc Consumer.addKeywordResult("operator");
3890f4a2713aSLionel Sambuc return;
3891f4a2713aSLionel Sambuc }
3892f4a2713aSLionel Sambuc
3893f4a2713aSLionel Sambuc if (CCC.WantObjCSuper)
3894f4a2713aSLionel Sambuc Consumer.addKeywordResult("super");
3895f4a2713aSLionel Sambuc
3896f4a2713aSLionel Sambuc if (CCC.WantTypeSpecifiers) {
3897f4a2713aSLionel Sambuc // Add type-specifier keywords to the set of results.
3898f4a2713aSLionel Sambuc static const char *const CTypeSpecs[] = {
3899f4a2713aSLionel Sambuc "char", "const", "double", "enum", "float", "int", "long", "short",
3900f4a2713aSLionel Sambuc "signed", "struct", "union", "unsigned", "void", "volatile",
3901f4a2713aSLionel Sambuc "_Complex", "_Imaginary",
3902f4a2713aSLionel Sambuc // storage-specifiers as well
3903f4a2713aSLionel Sambuc "extern", "inline", "static", "typedef"
3904f4a2713aSLionel Sambuc };
3905f4a2713aSLionel Sambuc
3906f4a2713aSLionel Sambuc const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs);
3907f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumCTypeSpecs; ++I)
3908f4a2713aSLionel Sambuc Consumer.addKeywordResult(CTypeSpecs[I]);
3909f4a2713aSLionel Sambuc
3910f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().C99)
3911f4a2713aSLionel Sambuc Consumer.addKeywordResult("restrict");
3912f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus)
3913f4a2713aSLionel Sambuc Consumer.addKeywordResult("bool");
3914f4a2713aSLionel Sambuc else if (SemaRef.getLangOpts().C99)
3915f4a2713aSLionel Sambuc Consumer.addKeywordResult("_Bool");
3916f4a2713aSLionel Sambuc
3917f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) {
3918f4a2713aSLionel Sambuc Consumer.addKeywordResult("class");
3919f4a2713aSLionel Sambuc Consumer.addKeywordResult("typename");
3920f4a2713aSLionel Sambuc Consumer.addKeywordResult("wchar_t");
3921f4a2713aSLionel Sambuc
3922f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus11) {
3923f4a2713aSLionel Sambuc Consumer.addKeywordResult("char16_t");
3924f4a2713aSLionel Sambuc Consumer.addKeywordResult("char32_t");
3925f4a2713aSLionel Sambuc Consumer.addKeywordResult("constexpr");
3926f4a2713aSLionel Sambuc Consumer.addKeywordResult("decltype");
3927f4a2713aSLionel Sambuc Consumer.addKeywordResult("thread_local");
3928f4a2713aSLionel Sambuc }
3929f4a2713aSLionel Sambuc }
3930f4a2713aSLionel Sambuc
3931f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().GNUMode)
3932f4a2713aSLionel Sambuc Consumer.addKeywordResult("typeof");
3933*0a6a1f1dSLionel Sambuc } else if (CCC.WantFunctionLikeCasts) {
3934*0a6a1f1dSLionel Sambuc static const char *const CastableTypeSpecs[] = {
3935*0a6a1f1dSLionel Sambuc "char", "double", "float", "int", "long", "short",
3936*0a6a1f1dSLionel Sambuc "signed", "unsigned", "void"
3937*0a6a1f1dSLionel Sambuc };
3938*0a6a1f1dSLionel Sambuc for (auto *kw : CastableTypeSpecs)
3939*0a6a1f1dSLionel Sambuc Consumer.addKeywordResult(kw);
3940f4a2713aSLionel Sambuc }
3941f4a2713aSLionel Sambuc
3942f4a2713aSLionel Sambuc if (CCC.WantCXXNamedCasts && SemaRef.getLangOpts().CPlusPlus) {
3943f4a2713aSLionel Sambuc Consumer.addKeywordResult("const_cast");
3944f4a2713aSLionel Sambuc Consumer.addKeywordResult("dynamic_cast");
3945f4a2713aSLionel Sambuc Consumer.addKeywordResult("reinterpret_cast");
3946f4a2713aSLionel Sambuc Consumer.addKeywordResult("static_cast");
3947f4a2713aSLionel Sambuc }
3948f4a2713aSLionel Sambuc
3949f4a2713aSLionel Sambuc if (CCC.WantExpressionKeywords) {
3950f4a2713aSLionel Sambuc Consumer.addKeywordResult("sizeof");
3951f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) {
3952f4a2713aSLionel Sambuc Consumer.addKeywordResult("false");
3953f4a2713aSLionel Sambuc Consumer.addKeywordResult("true");
3954f4a2713aSLionel Sambuc }
3955f4a2713aSLionel Sambuc
3956f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) {
3957f4a2713aSLionel Sambuc static const char *const CXXExprs[] = {
3958f4a2713aSLionel Sambuc "delete", "new", "operator", "throw", "typeid"
3959f4a2713aSLionel Sambuc };
3960f4a2713aSLionel Sambuc const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs);
3961f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumCXXExprs; ++I)
3962f4a2713aSLionel Sambuc Consumer.addKeywordResult(CXXExprs[I]);
3963f4a2713aSLionel Sambuc
3964f4a2713aSLionel Sambuc if (isa<CXXMethodDecl>(SemaRef.CurContext) &&
3965f4a2713aSLionel Sambuc cast<CXXMethodDecl>(SemaRef.CurContext)->isInstance())
3966f4a2713aSLionel Sambuc Consumer.addKeywordResult("this");
3967f4a2713aSLionel Sambuc
3968f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus11) {
3969f4a2713aSLionel Sambuc Consumer.addKeywordResult("alignof");
3970f4a2713aSLionel Sambuc Consumer.addKeywordResult("nullptr");
3971f4a2713aSLionel Sambuc }
3972f4a2713aSLionel Sambuc }
3973f4a2713aSLionel Sambuc
3974f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().C11) {
3975f4a2713aSLionel Sambuc // FIXME: We should not suggest _Alignof if the alignof macro
3976f4a2713aSLionel Sambuc // is present.
3977f4a2713aSLionel Sambuc Consumer.addKeywordResult("_Alignof");
3978f4a2713aSLionel Sambuc }
3979f4a2713aSLionel Sambuc }
3980f4a2713aSLionel Sambuc
3981f4a2713aSLionel Sambuc if (CCC.WantRemainingKeywords) {
3982f4a2713aSLionel Sambuc if (SemaRef.getCurFunctionOrMethodDecl() || SemaRef.getCurBlock()) {
3983f4a2713aSLionel Sambuc // Statements.
3984f4a2713aSLionel Sambuc static const char *const CStmts[] = {
3985f4a2713aSLionel Sambuc "do", "else", "for", "goto", "if", "return", "switch", "while" };
3986f4a2713aSLionel Sambuc const unsigned NumCStmts = llvm::array_lengthof(CStmts);
3987f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumCStmts; ++I)
3988f4a2713aSLionel Sambuc Consumer.addKeywordResult(CStmts[I]);
3989f4a2713aSLionel Sambuc
3990f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) {
3991f4a2713aSLionel Sambuc Consumer.addKeywordResult("catch");
3992f4a2713aSLionel Sambuc Consumer.addKeywordResult("try");
3993f4a2713aSLionel Sambuc }
3994f4a2713aSLionel Sambuc
3995f4a2713aSLionel Sambuc if (S && S->getBreakParent())
3996f4a2713aSLionel Sambuc Consumer.addKeywordResult("break");
3997f4a2713aSLionel Sambuc
3998f4a2713aSLionel Sambuc if (S && S->getContinueParent())
3999f4a2713aSLionel Sambuc Consumer.addKeywordResult("continue");
4000f4a2713aSLionel Sambuc
4001f4a2713aSLionel Sambuc if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
4002f4a2713aSLionel Sambuc Consumer.addKeywordResult("case");
4003f4a2713aSLionel Sambuc Consumer.addKeywordResult("default");
4004f4a2713aSLionel Sambuc }
4005f4a2713aSLionel Sambuc } else {
4006f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) {
4007f4a2713aSLionel Sambuc Consumer.addKeywordResult("namespace");
4008f4a2713aSLionel Sambuc Consumer.addKeywordResult("template");
4009f4a2713aSLionel Sambuc }
4010f4a2713aSLionel Sambuc
4011f4a2713aSLionel Sambuc if (S && S->isClassScope()) {
4012f4a2713aSLionel Sambuc Consumer.addKeywordResult("explicit");
4013f4a2713aSLionel Sambuc Consumer.addKeywordResult("friend");
4014f4a2713aSLionel Sambuc Consumer.addKeywordResult("mutable");
4015f4a2713aSLionel Sambuc Consumer.addKeywordResult("private");
4016f4a2713aSLionel Sambuc Consumer.addKeywordResult("protected");
4017f4a2713aSLionel Sambuc Consumer.addKeywordResult("public");
4018f4a2713aSLionel Sambuc Consumer.addKeywordResult("virtual");
4019f4a2713aSLionel Sambuc }
4020f4a2713aSLionel Sambuc }
4021f4a2713aSLionel Sambuc
4022f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) {
4023f4a2713aSLionel Sambuc Consumer.addKeywordResult("using");
4024f4a2713aSLionel Sambuc
4025f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus11)
4026f4a2713aSLionel Sambuc Consumer.addKeywordResult("static_assert");
4027f4a2713aSLionel Sambuc }
4028f4a2713aSLionel Sambuc }
4029f4a2713aSLionel Sambuc }
4030f4a2713aSLionel Sambuc
makeTypoCorrectionConsumer(const DeclarationNameInfo & TypoName,Sema::LookupNameKind LookupKind,Scope * S,CXXScopeSpec * SS,std::unique_ptr<CorrectionCandidateCallback> CCC,DeclContext * MemberContext,bool EnteringContext,const ObjCObjectPointerType * OPT,bool ErrorRecovery)4031*0a6a1f1dSLionel Sambuc std::unique_ptr<TypoCorrectionConsumer> Sema::makeTypoCorrectionConsumer(
4032*0a6a1f1dSLionel Sambuc const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind,
4033*0a6a1f1dSLionel Sambuc Scope *S, CXXScopeSpec *SS,
4034*0a6a1f1dSLionel Sambuc std::unique_ptr<CorrectionCandidateCallback> CCC,
4035*0a6a1f1dSLionel Sambuc DeclContext *MemberContext, bool EnteringContext,
4036*0a6a1f1dSLionel Sambuc const ObjCObjectPointerType *OPT, bool ErrorRecovery) {
4037*0a6a1f1dSLionel Sambuc
4038*0a6a1f1dSLionel Sambuc if (Diags.hasFatalErrorOccurred() || !getLangOpts().SpellChecking ||
4039*0a6a1f1dSLionel Sambuc DisableTypoCorrection)
4040*0a6a1f1dSLionel Sambuc return nullptr;
4041*0a6a1f1dSLionel Sambuc
4042*0a6a1f1dSLionel Sambuc // In Microsoft mode, don't perform typo correction in a template member
4043*0a6a1f1dSLionel Sambuc // function dependent context because it interferes with the "lookup into
4044*0a6a1f1dSLionel Sambuc // dependent bases of class templates" feature.
4045*0a6a1f1dSLionel Sambuc if (getLangOpts().MSVCCompat && CurContext->isDependentContext() &&
4046*0a6a1f1dSLionel Sambuc isa<CXXMethodDecl>(CurContext))
4047*0a6a1f1dSLionel Sambuc return nullptr;
4048*0a6a1f1dSLionel Sambuc
4049*0a6a1f1dSLionel Sambuc // We only attempt to correct typos for identifiers.
4050*0a6a1f1dSLionel Sambuc IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo();
4051*0a6a1f1dSLionel Sambuc if (!Typo)
4052*0a6a1f1dSLionel Sambuc return nullptr;
4053*0a6a1f1dSLionel Sambuc
4054*0a6a1f1dSLionel Sambuc // If the scope specifier itself was invalid, don't try to correct
4055*0a6a1f1dSLionel Sambuc // typos.
4056*0a6a1f1dSLionel Sambuc if (SS && SS->isInvalid())
4057*0a6a1f1dSLionel Sambuc return nullptr;
4058*0a6a1f1dSLionel Sambuc
4059*0a6a1f1dSLionel Sambuc // Never try to correct typos during template deduction or
4060*0a6a1f1dSLionel Sambuc // instantiation.
4061*0a6a1f1dSLionel Sambuc if (!ActiveTemplateInstantiations.empty())
4062*0a6a1f1dSLionel Sambuc return nullptr;
4063*0a6a1f1dSLionel Sambuc
4064*0a6a1f1dSLionel Sambuc // Don't try to correct 'super'.
4065*0a6a1f1dSLionel Sambuc if (S && S->isInObjcMethodScope() && Typo == getSuperIdentifier())
4066*0a6a1f1dSLionel Sambuc return nullptr;
4067*0a6a1f1dSLionel Sambuc
4068*0a6a1f1dSLionel Sambuc // Abort if typo correction already failed for this specific typo.
4069*0a6a1f1dSLionel Sambuc IdentifierSourceLocations::iterator locs = TypoCorrectionFailures.find(Typo);
4070*0a6a1f1dSLionel Sambuc if (locs != TypoCorrectionFailures.end() &&
4071*0a6a1f1dSLionel Sambuc locs->second.count(TypoName.getLoc()))
4072*0a6a1f1dSLionel Sambuc return nullptr;
4073*0a6a1f1dSLionel Sambuc
4074*0a6a1f1dSLionel Sambuc // Don't try to correct the identifier "vector" when in AltiVec mode.
4075*0a6a1f1dSLionel Sambuc // TODO: Figure out why typo correction misbehaves in this case, fix it, and
4076*0a6a1f1dSLionel Sambuc // remove this workaround.
4077*0a6a1f1dSLionel Sambuc if (getLangOpts().AltiVec && Typo->isStr("vector"))
4078*0a6a1f1dSLionel Sambuc return nullptr;
4079*0a6a1f1dSLionel Sambuc
4080*0a6a1f1dSLionel Sambuc // Provide a stop gap for files that are just seriously broken. Trying
4081*0a6a1f1dSLionel Sambuc // to correct all typos can turn into a HUGE performance penalty, causing
4082*0a6a1f1dSLionel Sambuc // some files to take minutes to get rejected by the parser.
4083*0a6a1f1dSLionel Sambuc unsigned Limit = getDiagnostics().getDiagnosticOptions().SpellCheckingLimit;
4084*0a6a1f1dSLionel Sambuc if (Limit && TyposCorrected >= Limit)
4085*0a6a1f1dSLionel Sambuc return nullptr;
4086*0a6a1f1dSLionel Sambuc ++TyposCorrected;
4087*0a6a1f1dSLionel Sambuc
4088*0a6a1f1dSLionel Sambuc // If we're handling a missing symbol error, using modules, and the
4089*0a6a1f1dSLionel Sambuc // special search all modules option is used, look for a missing import.
4090*0a6a1f1dSLionel Sambuc if (ErrorRecovery && getLangOpts().Modules &&
4091*0a6a1f1dSLionel Sambuc getLangOpts().ModulesSearchAll) {
4092*0a6a1f1dSLionel Sambuc // The following has the side effect of loading the missing module.
4093*0a6a1f1dSLionel Sambuc getModuleLoader().lookupMissingImports(Typo->getName(),
4094*0a6a1f1dSLionel Sambuc TypoName.getLocStart());
4095f4a2713aSLionel Sambuc }
4096f4a2713aSLionel Sambuc
4097*0a6a1f1dSLionel Sambuc CorrectionCandidateCallback &CCCRef = *CCC;
4098*0a6a1f1dSLionel Sambuc auto Consumer = llvm::make_unique<TypoCorrectionConsumer>(
4099*0a6a1f1dSLionel Sambuc *this, TypoName, LookupKind, S, SS, std::move(CCC), MemberContext,
4100*0a6a1f1dSLionel Sambuc EnteringContext);
4101f4a2713aSLionel Sambuc
4102*0a6a1f1dSLionel Sambuc // Perform name lookup to find visible, similarly-named entities.
4103*0a6a1f1dSLionel Sambuc bool IsUnqualifiedLookup = false;
4104*0a6a1f1dSLionel Sambuc DeclContext *QualifiedDC = MemberContext;
4105*0a6a1f1dSLionel Sambuc if (MemberContext) {
4106*0a6a1f1dSLionel Sambuc LookupVisibleDecls(MemberContext, LookupKind, *Consumer);
4107f4a2713aSLionel Sambuc
4108*0a6a1f1dSLionel Sambuc // Look in qualified interfaces.
4109*0a6a1f1dSLionel Sambuc if (OPT) {
4110*0a6a1f1dSLionel Sambuc for (auto *I : OPT->quals())
4111*0a6a1f1dSLionel Sambuc LookupVisibleDecls(I, LookupKind, *Consumer);
4112*0a6a1f1dSLionel Sambuc }
4113*0a6a1f1dSLionel Sambuc } else if (SS && SS->isSet()) {
4114*0a6a1f1dSLionel Sambuc QualifiedDC = computeDeclContext(*SS, EnteringContext);
4115*0a6a1f1dSLionel Sambuc if (!QualifiedDC)
4116*0a6a1f1dSLionel Sambuc return nullptr;
4117*0a6a1f1dSLionel Sambuc
4118*0a6a1f1dSLionel Sambuc LookupVisibleDecls(QualifiedDC, LookupKind, *Consumer);
4119*0a6a1f1dSLionel Sambuc } else {
4120*0a6a1f1dSLionel Sambuc IsUnqualifiedLookup = true;
4121*0a6a1f1dSLionel Sambuc }
4122*0a6a1f1dSLionel Sambuc
4123*0a6a1f1dSLionel Sambuc // Determine whether we are going to search in the various namespaces for
4124*0a6a1f1dSLionel Sambuc // corrections.
4125*0a6a1f1dSLionel Sambuc bool SearchNamespaces
4126*0a6a1f1dSLionel Sambuc = getLangOpts().CPlusPlus &&
4127*0a6a1f1dSLionel Sambuc (IsUnqualifiedLookup || (SS && SS->isSet()));
4128*0a6a1f1dSLionel Sambuc
4129*0a6a1f1dSLionel Sambuc if (IsUnqualifiedLookup || SearchNamespaces) {
4130*0a6a1f1dSLionel Sambuc // For unqualified lookup, look through all of the names that we have
4131*0a6a1f1dSLionel Sambuc // seen in this translation unit.
4132*0a6a1f1dSLionel Sambuc // FIXME: Re-add the ability to skip very unlikely potential corrections.
4133*0a6a1f1dSLionel Sambuc for (const auto &I : Context.Idents)
4134*0a6a1f1dSLionel Sambuc Consumer->FoundName(I.getKey());
4135*0a6a1f1dSLionel Sambuc
4136*0a6a1f1dSLionel Sambuc // Walk through identifiers in external identifier sources.
4137*0a6a1f1dSLionel Sambuc // FIXME: Re-add the ability to skip very unlikely potential corrections.
4138*0a6a1f1dSLionel Sambuc if (IdentifierInfoLookup *External
4139*0a6a1f1dSLionel Sambuc = Context.Idents.getExternalIdentifierLookup()) {
4140*0a6a1f1dSLionel Sambuc std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers());
4141*0a6a1f1dSLionel Sambuc do {
4142*0a6a1f1dSLionel Sambuc StringRef Name = Iter->Next();
4143*0a6a1f1dSLionel Sambuc if (Name.empty())
4144f4a2713aSLionel Sambuc break;
4145f4a2713aSLionel Sambuc
4146*0a6a1f1dSLionel Sambuc Consumer->FoundName(Name);
4147*0a6a1f1dSLionel Sambuc } while (true);
4148f4a2713aSLionel Sambuc }
4149f4a2713aSLionel Sambuc }
4150f4a2713aSLionel Sambuc
4151*0a6a1f1dSLionel Sambuc AddKeywordsToConsumer(*this, *Consumer, S, CCCRef, SS && SS->isNotEmpty());
4152*0a6a1f1dSLionel Sambuc
4153*0a6a1f1dSLionel Sambuc // Build the NestedNameSpecifiers for the KnownNamespaces, if we're going
4154*0a6a1f1dSLionel Sambuc // to search those namespaces.
4155*0a6a1f1dSLionel Sambuc if (SearchNamespaces) {
4156*0a6a1f1dSLionel Sambuc // Load any externally-known namespaces.
4157*0a6a1f1dSLionel Sambuc if (ExternalSource && !LoadedExternalKnownNamespaces) {
4158*0a6a1f1dSLionel Sambuc SmallVector<NamespaceDecl *, 4> ExternalKnownNamespaces;
4159*0a6a1f1dSLionel Sambuc LoadedExternalKnownNamespaces = true;
4160*0a6a1f1dSLionel Sambuc ExternalSource->ReadKnownNamespaces(ExternalKnownNamespaces);
4161*0a6a1f1dSLionel Sambuc for (auto *N : ExternalKnownNamespaces)
4162*0a6a1f1dSLionel Sambuc KnownNamespaces[N] = true;
4163f4a2713aSLionel Sambuc }
4164*0a6a1f1dSLionel Sambuc
4165*0a6a1f1dSLionel Sambuc Consumer->addNamespaces(KnownNamespaces);
4166*0a6a1f1dSLionel Sambuc }
4167*0a6a1f1dSLionel Sambuc
4168*0a6a1f1dSLionel Sambuc return Consumer;
4169f4a2713aSLionel Sambuc }
4170f4a2713aSLionel Sambuc
4171f4a2713aSLionel Sambuc /// \brief Try to "correct" a typo in the source code by finding
4172f4a2713aSLionel Sambuc /// visible declarations whose names are similar to the name that was
4173f4a2713aSLionel Sambuc /// present in the source code.
4174f4a2713aSLionel Sambuc ///
4175f4a2713aSLionel Sambuc /// \param TypoName the \c DeclarationNameInfo structure that contains
4176f4a2713aSLionel Sambuc /// the name that was present in the source code along with its location.
4177f4a2713aSLionel Sambuc ///
4178f4a2713aSLionel Sambuc /// \param LookupKind the name-lookup criteria used to search for the name.
4179f4a2713aSLionel Sambuc ///
4180f4a2713aSLionel Sambuc /// \param S the scope in which name lookup occurs.
4181f4a2713aSLionel Sambuc ///
4182f4a2713aSLionel Sambuc /// \param SS the nested-name-specifier that precedes the name we're
4183f4a2713aSLionel Sambuc /// looking for, if present.
4184f4a2713aSLionel Sambuc ///
4185f4a2713aSLionel Sambuc /// \param CCC A CorrectionCandidateCallback object that provides further
4186f4a2713aSLionel Sambuc /// validation of typo correction candidates. It also provides flags for
4187f4a2713aSLionel Sambuc /// determining the set of keywords permitted.
4188f4a2713aSLionel Sambuc ///
4189f4a2713aSLionel Sambuc /// \param MemberContext if non-NULL, the context in which to look for
4190f4a2713aSLionel Sambuc /// a member access expression.
4191f4a2713aSLionel Sambuc ///
4192f4a2713aSLionel Sambuc /// \param EnteringContext whether we're entering the context described by
4193f4a2713aSLionel Sambuc /// the nested-name-specifier SS.
4194f4a2713aSLionel Sambuc ///
4195f4a2713aSLionel Sambuc /// \param OPT when non-NULL, the search for visible declarations will
4196f4a2713aSLionel Sambuc /// also walk the protocols in the qualified interfaces of \p OPT.
4197f4a2713aSLionel Sambuc ///
4198f4a2713aSLionel Sambuc /// \returns a \c TypoCorrection containing the corrected name if the typo
4199f4a2713aSLionel Sambuc /// along with information such as the \c NamedDecl where the corrected name
4200f4a2713aSLionel Sambuc /// was declared, and any additional \c NestedNameSpecifier needed to access
4201f4a2713aSLionel Sambuc /// it (C++ only). The \c TypoCorrection is empty if there is no correction.
CorrectTypo(const DeclarationNameInfo & TypoName,Sema::LookupNameKind LookupKind,Scope * S,CXXScopeSpec * SS,std::unique_ptr<CorrectionCandidateCallback> CCC,CorrectTypoKind Mode,DeclContext * MemberContext,bool EnteringContext,const ObjCObjectPointerType * OPT,bool RecordFailure)4202f4a2713aSLionel Sambuc TypoCorrection Sema::CorrectTypo(const DeclarationNameInfo &TypoName,
4203f4a2713aSLionel Sambuc Sema::LookupNameKind LookupKind,
4204f4a2713aSLionel Sambuc Scope *S, CXXScopeSpec *SS,
4205*0a6a1f1dSLionel Sambuc std::unique_ptr<CorrectionCandidateCallback> CCC,
4206*0a6a1f1dSLionel Sambuc CorrectTypoKind Mode,
4207f4a2713aSLionel Sambuc DeclContext *MemberContext,
4208f4a2713aSLionel Sambuc bool EnteringContext,
4209f4a2713aSLionel Sambuc const ObjCObjectPointerType *OPT,
4210f4a2713aSLionel Sambuc bool RecordFailure) {
4211*0a6a1f1dSLionel Sambuc assert(CCC && "CorrectTypo requires a CorrectionCandidateCallback");
4212*0a6a1f1dSLionel Sambuc
4213f4a2713aSLionel Sambuc // Always let the ExternalSource have the first chance at correction, even
4214f4a2713aSLionel Sambuc // if we would otherwise have given up.
4215f4a2713aSLionel Sambuc if (ExternalSource) {
4216f4a2713aSLionel Sambuc if (TypoCorrection Correction = ExternalSource->CorrectTypo(
4217*0a6a1f1dSLionel Sambuc TypoName, LookupKind, S, SS, *CCC, MemberContext, EnteringContext, OPT))
4218f4a2713aSLionel Sambuc return Correction;
4219f4a2713aSLionel Sambuc }
4220f4a2713aSLionel Sambuc
4221*0a6a1f1dSLionel Sambuc // Ugly hack equivalent to CTC == CTC_ObjCMessageReceiver;
4222*0a6a1f1dSLionel Sambuc // WantObjCSuper is only true for CTC_ObjCMessageReceiver and for
4223*0a6a1f1dSLionel Sambuc // some instances of CTC_Unknown, while WantRemainingKeywords is true
4224*0a6a1f1dSLionel Sambuc // for CTC_Unknown but not for CTC_ObjCMessageReceiver.
4225*0a6a1f1dSLionel Sambuc bool ObjCMessageReceiver = CCC->WantObjCSuper && !CCC->WantRemainingKeywords;
4226f4a2713aSLionel Sambuc
4227f4a2713aSLionel Sambuc IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo();
4228*0a6a1f1dSLionel Sambuc auto Consumer = makeTypoCorrectionConsumer(
4229*0a6a1f1dSLionel Sambuc TypoName, LookupKind, S, SS, std::move(CCC), MemberContext,
4230*0a6a1f1dSLionel Sambuc EnteringContext, OPT, Mode == CTK_ErrorRecovery);
4231*0a6a1f1dSLionel Sambuc
4232*0a6a1f1dSLionel Sambuc if (!Consumer)
4233f4a2713aSLionel Sambuc return TypoCorrection();
4234f4a2713aSLionel Sambuc
4235f4a2713aSLionel Sambuc // If we haven't found anything, we're done.
4236*0a6a1f1dSLionel Sambuc if (Consumer->empty())
4237*0a6a1f1dSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
4238f4a2713aSLionel Sambuc
4239f4a2713aSLionel Sambuc // Make sure the best edit distance (prior to adding any namespace qualifiers)
4240f4a2713aSLionel Sambuc // is not more that about a third of the length of the typo's identifier.
4241*0a6a1f1dSLionel Sambuc unsigned ED = Consumer->getBestEditDistance(true);
4242*0a6a1f1dSLionel Sambuc unsigned TypoLen = Typo->getName().size();
4243f4a2713aSLionel Sambuc if (ED > 0 && TypoLen / ED < 3)
4244f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
4245f4a2713aSLionel Sambuc
4246*0a6a1f1dSLionel Sambuc TypoCorrection BestTC = Consumer->getNextCorrection();
4247*0a6a1f1dSLionel Sambuc TypoCorrection SecondBestTC = Consumer->getNextCorrection();
4248*0a6a1f1dSLionel Sambuc if (!BestTC)
4249f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
4250f4a2713aSLionel Sambuc
4251*0a6a1f1dSLionel Sambuc ED = BestTC.getEditDistance();
4252f4a2713aSLionel Sambuc
4253*0a6a1f1dSLionel Sambuc if (TypoLen >= 3 && ED > 0 && TypoLen / ED < 3) {
4254f4a2713aSLionel Sambuc // If this was an unqualified lookup and we believe the callback
4255f4a2713aSLionel Sambuc // object wouldn't have filtered out possible corrections, note
4256f4a2713aSLionel Sambuc // that no correction was found.
4257*0a6a1f1dSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
4258f4a2713aSLionel Sambuc }
4259f4a2713aSLionel Sambuc
4260f4a2713aSLionel Sambuc // If only a single name remains, return that result.
4261*0a6a1f1dSLionel Sambuc if (!SecondBestTC ||
4262*0a6a1f1dSLionel Sambuc SecondBestTC.getEditDistance(false) > BestTC.getEditDistance(false)) {
4263*0a6a1f1dSLionel Sambuc const TypoCorrection &Result = BestTC;
4264f4a2713aSLionel Sambuc
4265f4a2713aSLionel Sambuc // Don't correct to a keyword that's the same as the typo; the keyword
4266f4a2713aSLionel Sambuc // wasn't actually in scope.
4267f4a2713aSLionel Sambuc if (ED == 0 && Result.isKeyword())
4268f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
4269f4a2713aSLionel Sambuc
4270f4a2713aSLionel Sambuc TypoCorrection TC = Result;
4271f4a2713aSLionel Sambuc TC.setCorrectionRange(SS, TypoName);
4272*0a6a1f1dSLionel Sambuc checkCorrectionVisibility(*this, TC);
4273f4a2713aSLionel Sambuc return TC;
4274*0a6a1f1dSLionel Sambuc } else if (SecondBestTC && ObjCMessageReceiver) {
4275f4a2713aSLionel Sambuc // Prefer 'super' when we're completing in a message-receiver
4276f4a2713aSLionel Sambuc // context.
4277f4a2713aSLionel Sambuc
4278*0a6a1f1dSLionel Sambuc if (BestTC.getCorrection().getAsString() != "super") {
4279*0a6a1f1dSLionel Sambuc if (SecondBestTC.getCorrection().getAsString() == "super")
4280*0a6a1f1dSLionel Sambuc BestTC = SecondBestTC;
4281*0a6a1f1dSLionel Sambuc else if ((*Consumer)["super"].front().isKeyword())
4282*0a6a1f1dSLionel Sambuc BestTC = (*Consumer)["super"].front();
4283*0a6a1f1dSLionel Sambuc }
4284f4a2713aSLionel Sambuc // Don't correct to a keyword that's the same as the typo; the keyword
4285f4a2713aSLionel Sambuc // wasn't actually in scope.
4286*0a6a1f1dSLionel Sambuc if (BestTC.getEditDistance() == 0 ||
4287*0a6a1f1dSLionel Sambuc BestTC.getCorrection().getAsString() != "super")
4288f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
4289f4a2713aSLionel Sambuc
4290*0a6a1f1dSLionel Sambuc BestTC.setCorrectionRange(SS, TypoName);
4291*0a6a1f1dSLionel Sambuc return BestTC;
4292f4a2713aSLionel Sambuc }
4293f4a2713aSLionel Sambuc
4294*0a6a1f1dSLionel Sambuc // Record the failure's location if needed and return an empty correction. If
4295*0a6a1f1dSLionel Sambuc // this was an unqualified lookup and we believe the callback object did not
4296*0a6a1f1dSLionel Sambuc // filter out possible corrections, also cache the failure for the typo.
4297f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
4298f4a2713aSLionel Sambuc }
4299f4a2713aSLionel Sambuc
4300*0a6a1f1dSLionel Sambuc /// \brief Try to "correct" a typo in the source code by finding
4301*0a6a1f1dSLionel Sambuc /// visible declarations whose names are similar to the name that was
4302*0a6a1f1dSLionel Sambuc /// present in the source code.
4303*0a6a1f1dSLionel Sambuc ///
4304*0a6a1f1dSLionel Sambuc /// \param TypoName the \c DeclarationNameInfo structure that contains
4305*0a6a1f1dSLionel Sambuc /// the name that was present in the source code along with its location.
4306*0a6a1f1dSLionel Sambuc ///
4307*0a6a1f1dSLionel Sambuc /// \param LookupKind the name-lookup criteria used to search for the name.
4308*0a6a1f1dSLionel Sambuc ///
4309*0a6a1f1dSLionel Sambuc /// \param S the scope in which name lookup occurs.
4310*0a6a1f1dSLionel Sambuc ///
4311*0a6a1f1dSLionel Sambuc /// \param SS the nested-name-specifier that precedes the name we're
4312*0a6a1f1dSLionel Sambuc /// looking for, if present.
4313*0a6a1f1dSLionel Sambuc ///
4314*0a6a1f1dSLionel Sambuc /// \param CCC A CorrectionCandidateCallback object that provides further
4315*0a6a1f1dSLionel Sambuc /// validation of typo correction candidates. It also provides flags for
4316*0a6a1f1dSLionel Sambuc /// determining the set of keywords permitted.
4317*0a6a1f1dSLionel Sambuc ///
4318*0a6a1f1dSLionel Sambuc /// \param TDG A TypoDiagnosticGenerator functor that will be used to print
4319*0a6a1f1dSLionel Sambuc /// diagnostics when the actual typo correction is attempted.
4320*0a6a1f1dSLionel Sambuc ///
4321*0a6a1f1dSLionel Sambuc /// \param TRC A TypoRecoveryCallback functor that will be used to build an
4322*0a6a1f1dSLionel Sambuc /// Expr from a typo correction candidate.
4323*0a6a1f1dSLionel Sambuc ///
4324*0a6a1f1dSLionel Sambuc /// \param MemberContext if non-NULL, the context in which to look for
4325*0a6a1f1dSLionel Sambuc /// a member access expression.
4326*0a6a1f1dSLionel Sambuc ///
4327*0a6a1f1dSLionel Sambuc /// \param EnteringContext whether we're entering the context described by
4328*0a6a1f1dSLionel Sambuc /// the nested-name-specifier SS.
4329*0a6a1f1dSLionel Sambuc ///
4330*0a6a1f1dSLionel Sambuc /// \param OPT when non-NULL, the search for visible declarations will
4331*0a6a1f1dSLionel Sambuc /// also walk the protocols in the qualified interfaces of \p OPT.
4332*0a6a1f1dSLionel Sambuc ///
4333*0a6a1f1dSLionel Sambuc /// \returns a new \c TypoExpr that will later be replaced in the AST with an
4334*0a6a1f1dSLionel Sambuc /// Expr representing the result of performing typo correction, or nullptr if
4335*0a6a1f1dSLionel Sambuc /// typo correction is not possible. If nullptr is returned, no diagnostics will
4336*0a6a1f1dSLionel Sambuc /// be emitted and it is the responsibility of the caller to emit any that are
4337*0a6a1f1dSLionel Sambuc /// needed.
CorrectTypoDelayed(const DeclarationNameInfo & TypoName,Sema::LookupNameKind LookupKind,Scope * S,CXXScopeSpec * SS,std::unique_ptr<CorrectionCandidateCallback> CCC,TypoDiagnosticGenerator TDG,TypoRecoveryCallback TRC,CorrectTypoKind Mode,DeclContext * MemberContext,bool EnteringContext,const ObjCObjectPointerType * OPT)4338*0a6a1f1dSLionel Sambuc TypoExpr *Sema::CorrectTypoDelayed(
4339*0a6a1f1dSLionel Sambuc const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind,
4340*0a6a1f1dSLionel Sambuc Scope *S, CXXScopeSpec *SS,
4341*0a6a1f1dSLionel Sambuc std::unique_ptr<CorrectionCandidateCallback> CCC,
4342*0a6a1f1dSLionel Sambuc TypoDiagnosticGenerator TDG, TypoRecoveryCallback TRC, CorrectTypoKind Mode,
4343*0a6a1f1dSLionel Sambuc DeclContext *MemberContext, bool EnteringContext,
4344*0a6a1f1dSLionel Sambuc const ObjCObjectPointerType *OPT) {
4345*0a6a1f1dSLionel Sambuc assert(CCC && "CorrectTypoDelayed requires a CorrectionCandidateCallback");
4346*0a6a1f1dSLionel Sambuc
4347*0a6a1f1dSLionel Sambuc TypoCorrection Empty;
4348*0a6a1f1dSLionel Sambuc auto Consumer = makeTypoCorrectionConsumer(
4349*0a6a1f1dSLionel Sambuc TypoName, LookupKind, S, SS, std::move(CCC), MemberContext,
4350*0a6a1f1dSLionel Sambuc EnteringContext, OPT,
4351*0a6a1f1dSLionel Sambuc /*SearchModules=*/(Mode == CTK_ErrorRecovery) && getLangOpts().Modules &&
4352*0a6a1f1dSLionel Sambuc getLangOpts().ModulesSearchAll);
4353*0a6a1f1dSLionel Sambuc
4354*0a6a1f1dSLionel Sambuc if (!Consumer || Consumer->empty())
4355*0a6a1f1dSLionel Sambuc return nullptr;
4356*0a6a1f1dSLionel Sambuc
4357*0a6a1f1dSLionel Sambuc // Make sure the best edit distance (prior to adding any namespace qualifiers)
4358*0a6a1f1dSLionel Sambuc // is not more that about a third of the length of the typo's identifier.
4359*0a6a1f1dSLionel Sambuc unsigned ED = Consumer->getBestEditDistance(true);
4360*0a6a1f1dSLionel Sambuc IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo();
4361*0a6a1f1dSLionel Sambuc if (ED > 0 && Typo->getName().size() / ED < 3)
4362*0a6a1f1dSLionel Sambuc return nullptr;
4363*0a6a1f1dSLionel Sambuc
4364*0a6a1f1dSLionel Sambuc ExprEvalContexts.back().NumTypos++;
4365*0a6a1f1dSLionel Sambuc return createDelayedTypo(std::move(Consumer), std::move(TDG), std::move(TRC));
4366*0a6a1f1dSLionel Sambuc }
4367*0a6a1f1dSLionel Sambuc
addCorrectionDecl(NamedDecl * CDecl)4368f4a2713aSLionel Sambuc void TypoCorrection::addCorrectionDecl(NamedDecl *CDecl) {
4369f4a2713aSLionel Sambuc if (!CDecl) return;
4370f4a2713aSLionel Sambuc
4371f4a2713aSLionel Sambuc if (isKeyword())
4372f4a2713aSLionel Sambuc CorrectionDecls.clear();
4373f4a2713aSLionel Sambuc
4374f4a2713aSLionel Sambuc CorrectionDecls.push_back(CDecl->getUnderlyingDecl());
4375f4a2713aSLionel Sambuc
4376f4a2713aSLionel Sambuc if (!CorrectionName)
4377f4a2713aSLionel Sambuc CorrectionName = CDecl->getDeclName();
4378f4a2713aSLionel Sambuc }
4379f4a2713aSLionel Sambuc
getAsString(const LangOptions & LO) const4380f4a2713aSLionel Sambuc std::string TypoCorrection::getAsString(const LangOptions &LO) const {
4381f4a2713aSLionel Sambuc if (CorrectionNameSpec) {
4382f4a2713aSLionel Sambuc std::string tmpBuffer;
4383f4a2713aSLionel Sambuc llvm::raw_string_ostream PrefixOStream(tmpBuffer);
4384f4a2713aSLionel Sambuc CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO));
4385f4a2713aSLionel Sambuc PrefixOStream << CorrectionName;
4386f4a2713aSLionel Sambuc return PrefixOStream.str();
4387f4a2713aSLionel Sambuc }
4388f4a2713aSLionel Sambuc
4389f4a2713aSLionel Sambuc return CorrectionName.getAsString();
4390f4a2713aSLionel Sambuc }
4391f4a2713aSLionel Sambuc
ValidateCandidate(const TypoCorrection & candidate)4392*0a6a1f1dSLionel Sambuc bool CorrectionCandidateCallback::ValidateCandidate(
4393*0a6a1f1dSLionel Sambuc const TypoCorrection &candidate) {
4394f4a2713aSLionel Sambuc if (!candidate.isResolved())
4395f4a2713aSLionel Sambuc return true;
4396f4a2713aSLionel Sambuc
4397f4a2713aSLionel Sambuc if (candidate.isKeyword())
4398f4a2713aSLionel Sambuc return WantTypeSpecifiers || WantExpressionKeywords || WantCXXNamedCasts ||
4399f4a2713aSLionel Sambuc WantRemainingKeywords || WantObjCSuper;
4400f4a2713aSLionel Sambuc
4401*0a6a1f1dSLionel Sambuc bool HasNonType = false;
4402*0a6a1f1dSLionel Sambuc bool HasStaticMethod = false;
4403*0a6a1f1dSLionel Sambuc bool HasNonStaticMethod = false;
4404*0a6a1f1dSLionel Sambuc for (Decl *D : candidate) {
4405*0a6a1f1dSLionel Sambuc if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
4406*0a6a1f1dSLionel Sambuc D = FTD->getTemplatedDecl();
4407*0a6a1f1dSLionel Sambuc if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
4408*0a6a1f1dSLionel Sambuc if (Method->isStatic())
4409*0a6a1f1dSLionel Sambuc HasStaticMethod = true;
4410*0a6a1f1dSLionel Sambuc else
4411*0a6a1f1dSLionel Sambuc HasNonStaticMethod = true;
4412*0a6a1f1dSLionel Sambuc }
4413*0a6a1f1dSLionel Sambuc if (!isa<TypeDecl>(D))
4414*0a6a1f1dSLionel Sambuc HasNonType = true;
4415f4a2713aSLionel Sambuc }
4416f4a2713aSLionel Sambuc
4417*0a6a1f1dSLionel Sambuc if (IsAddressOfOperand && HasNonStaticMethod && !HasStaticMethod &&
4418*0a6a1f1dSLionel Sambuc !candidate.getCorrectionSpecifier())
4419*0a6a1f1dSLionel Sambuc return false;
4420*0a6a1f1dSLionel Sambuc
4421*0a6a1f1dSLionel Sambuc return WantTypeSpecifiers || HasNonType;
4422f4a2713aSLionel Sambuc }
4423f4a2713aSLionel Sambuc
FunctionCallFilterCCC(Sema & SemaRef,unsigned NumArgs,bool HasExplicitTemplateArgs,MemberExpr * ME)4424f4a2713aSLionel Sambuc FunctionCallFilterCCC::FunctionCallFilterCCC(Sema &SemaRef, unsigned NumArgs,
4425*0a6a1f1dSLionel Sambuc bool HasExplicitTemplateArgs,
4426*0a6a1f1dSLionel Sambuc MemberExpr *ME)
4427*0a6a1f1dSLionel Sambuc : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs),
4428*0a6a1f1dSLionel Sambuc CurContext(SemaRef.CurContext), MemberFn(ME) {
4429*0a6a1f1dSLionel Sambuc WantTypeSpecifiers = false;
4430*0a6a1f1dSLionel Sambuc WantFunctionLikeCasts = SemaRef.getLangOpts().CPlusPlus && NumArgs == 1;
4431f4a2713aSLionel Sambuc WantRemainingKeywords = false;
4432f4a2713aSLionel Sambuc }
4433f4a2713aSLionel Sambuc
ValidateCandidate(const TypoCorrection & candidate)4434f4a2713aSLionel Sambuc bool FunctionCallFilterCCC::ValidateCandidate(const TypoCorrection &candidate) {
4435f4a2713aSLionel Sambuc if (!candidate.getCorrectionDecl())
4436f4a2713aSLionel Sambuc return candidate.isKeyword();
4437f4a2713aSLionel Sambuc
4438*0a6a1f1dSLionel Sambuc for (auto *C : candidate) {
4439*0a6a1f1dSLionel Sambuc FunctionDecl *FD = nullptr;
4440*0a6a1f1dSLionel Sambuc NamedDecl *ND = C->getUnderlyingDecl();
4441f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4442f4a2713aSLionel Sambuc FD = FTD->getTemplatedDecl();
4443f4a2713aSLionel Sambuc if (!HasExplicitTemplateArgs && !FD) {
4444f4a2713aSLionel Sambuc if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) {
4445f4a2713aSLionel Sambuc // If the Decl is neither a function nor a template function,
4446f4a2713aSLionel Sambuc // determine if it is a pointer or reference to a function. If so,
4447f4a2713aSLionel Sambuc // check against the number of arguments expected for the pointee.
4448f4a2713aSLionel Sambuc QualType ValType = cast<ValueDecl>(ND)->getType();
4449f4a2713aSLionel Sambuc if (ValType->isAnyPointerType() || ValType->isReferenceType())
4450f4a2713aSLionel Sambuc ValType = ValType->getPointeeType();
4451f4a2713aSLionel Sambuc if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>())
4452*0a6a1f1dSLionel Sambuc if (FPT->getNumParams() == NumArgs)
4453f4a2713aSLionel Sambuc return true;
4454f4a2713aSLionel Sambuc }
4455f4a2713aSLionel Sambuc }
4456*0a6a1f1dSLionel Sambuc
4457*0a6a1f1dSLionel Sambuc // Skip the current candidate if it is not a FunctionDecl or does not accept
4458*0a6a1f1dSLionel Sambuc // the current number of arguments.
4459*0a6a1f1dSLionel Sambuc if (!FD || !(FD->getNumParams() >= NumArgs &&
4460*0a6a1f1dSLionel Sambuc FD->getMinRequiredArguments() <= NumArgs))
4461*0a6a1f1dSLionel Sambuc continue;
4462*0a6a1f1dSLionel Sambuc
4463*0a6a1f1dSLionel Sambuc // If the current candidate is a non-static C++ method, skip the candidate
4464*0a6a1f1dSLionel Sambuc // unless the method being corrected--or the current DeclContext, if the
4465*0a6a1f1dSLionel Sambuc // function being corrected is not a method--is a method in the same class
4466*0a6a1f1dSLionel Sambuc // or a descendent class of the candidate's parent class.
4467*0a6a1f1dSLionel Sambuc if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
4468*0a6a1f1dSLionel Sambuc if (MemberFn || !MD->isStatic()) {
4469*0a6a1f1dSLionel Sambuc CXXMethodDecl *CurMD =
4470*0a6a1f1dSLionel Sambuc MemberFn
4471*0a6a1f1dSLionel Sambuc ? dyn_cast_or_null<CXXMethodDecl>(MemberFn->getMemberDecl())
4472*0a6a1f1dSLionel Sambuc : dyn_cast_or_null<CXXMethodDecl>(CurContext);
4473*0a6a1f1dSLionel Sambuc CXXRecordDecl *CurRD =
4474*0a6a1f1dSLionel Sambuc CurMD ? CurMD->getParent()->getCanonicalDecl() : nullptr;
4475*0a6a1f1dSLionel Sambuc CXXRecordDecl *RD = MD->getParent()->getCanonicalDecl();
4476*0a6a1f1dSLionel Sambuc if (!CurRD || (CurRD != RD && !CurRD->isDerivedFrom(RD)))
4477*0a6a1f1dSLionel Sambuc continue;
4478*0a6a1f1dSLionel Sambuc }
4479*0a6a1f1dSLionel Sambuc }
4480f4a2713aSLionel Sambuc return true;
4481f4a2713aSLionel Sambuc }
4482f4a2713aSLionel Sambuc return false;
4483f4a2713aSLionel Sambuc }
4484f4a2713aSLionel Sambuc
diagnoseTypo(const TypoCorrection & Correction,const PartialDiagnostic & TypoDiag,bool ErrorRecovery)4485f4a2713aSLionel Sambuc void Sema::diagnoseTypo(const TypoCorrection &Correction,
4486f4a2713aSLionel Sambuc const PartialDiagnostic &TypoDiag,
4487f4a2713aSLionel Sambuc bool ErrorRecovery) {
4488f4a2713aSLionel Sambuc diagnoseTypo(Correction, TypoDiag, PDiag(diag::note_previous_decl),
4489f4a2713aSLionel Sambuc ErrorRecovery);
4490f4a2713aSLionel Sambuc }
4491f4a2713aSLionel Sambuc
4492f4a2713aSLionel Sambuc /// Find which declaration we should import to provide the definition of
4493f4a2713aSLionel Sambuc /// the given declaration.
getDefinitionToImport(const NamedDecl * D)4494f4a2713aSLionel Sambuc static const NamedDecl *getDefinitionToImport(const NamedDecl *D) {
4495f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D))
4496f4a2713aSLionel Sambuc return VD->getDefinition();
4497f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4498*0a6a1f1dSLionel Sambuc return FD->isDefined(FD) ? FD : nullptr;
4499f4a2713aSLionel Sambuc if (const TagDecl *TD = dyn_cast<TagDecl>(D))
4500f4a2713aSLionel Sambuc return TD->getDefinition();
4501f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
4502f4a2713aSLionel Sambuc return ID->getDefinition();
4503f4a2713aSLionel Sambuc if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
4504f4a2713aSLionel Sambuc return PD->getDefinition();
4505f4a2713aSLionel Sambuc if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
4506f4a2713aSLionel Sambuc return getDefinitionToImport(TD->getTemplatedDecl());
4507*0a6a1f1dSLionel Sambuc return nullptr;
4508f4a2713aSLionel Sambuc }
4509f4a2713aSLionel Sambuc
4510f4a2713aSLionel Sambuc /// \brief Diagnose a successfully-corrected typo. Separated from the correction
4511f4a2713aSLionel Sambuc /// itself to allow external validation of the result, etc.
4512f4a2713aSLionel Sambuc ///
4513f4a2713aSLionel Sambuc /// \param Correction The result of performing typo correction.
4514f4a2713aSLionel Sambuc /// \param TypoDiag The diagnostic to produce. This will have the corrected
4515f4a2713aSLionel Sambuc /// string added to it (and usually also a fixit).
4516f4a2713aSLionel Sambuc /// \param PrevNote A note to use when indicating the location of the entity to
4517f4a2713aSLionel Sambuc /// which we are correcting. Will have the correction string added to it.
4518f4a2713aSLionel Sambuc /// \param ErrorRecovery If \c true (the default), the caller is going to
4519f4a2713aSLionel Sambuc /// recover from the typo as if the corrected string had been typed.
4520f4a2713aSLionel Sambuc /// In this case, \c PDiag must be an error, and we will attach a fixit
4521f4a2713aSLionel Sambuc /// to it.
diagnoseTypo(const TypoCorrection & Correction,const PartialDiagnostic & TypoDiag,const PartialDiagnostic & PrevNote,bool ErrorRecovery)4522f4a2713aSLionel Sambuc void Sema::diagnoseTypo(const TypoCorrection &Correction,
4523f4a2713aSLionel Sambuc const PartialDiagnostic &TypoDiag,
4524f4a2713aSLionel Sambuc const PartialDiagnostic &PrevNote,
4525f4a2713aSLionel Sambuc bool ErrorRecovery) {
4526f4a2713aSLionel Sambuc std::string CorrectedStr = Correction.getAsString(getLangOpts());
4527f4a2713aSLionel Sambuc std::string CorrectedQuotedStr = Correction.getQuoted(getLangOpts());
4528f4a2713aSLionel Sambuc FixItHint FixTypo = FixItHint::CreateReplacement(
4529f4a2713aSLionel Sambuc Correction.getCorrectionRange(), CorrectedStr);
4530f4a2713aSLionel Sambuc
4531f4a2713aSLionel Sambuc // Maybe we're just missing a module import.
4532f4a2713aSLionel Sambuc if (Correction.requiresImport()) {
4533f4a2713aSLionel Sambuc NamedDecl *Decl = Correction.getCorrectionDecl();
4534f4a2713aSLionel Sambuc assert(Decl && "import required but no declaration to import");
4535f4a2713aSLionel Sambuc
4536f4a2713aSLionel Sambuc // Suggest importing a module providing the definition of this entity, if
4537f4a2713aSLionel Sambuc // possible.
4538f4a2713aSLionel Sambuc const NamedDecl *Def = getDefinitionToImport(Decl);
4539f4a2713aSLionel Sambuc if (!Def)
4540f4a2713aSLionel Sambuc Def = Decl;
4541f4a2713aSLionel Sambuc Module *Owner = Def->getOwningModule();
4542f4a2713aSLionel Sambuc assert(Owner && "definition of hidden declaration is not in a module");
4543f4a2713aSLionel Sambuc
4544f4a2713aSLionel Sambuc Diag(Correction.getCorrectionRange().getBegin(),
4545f4a2713aSLionel Sambuc diag::err_module_private_declaration)
4546f4a2713aSLionel Sambuc << Def << Owner->getFullModuleName();
4547f4a2713aSLionel Sambuc Diag(Def->getLocation(), diag::note_previous_declaration);
4548f4a2713aSLionel Sambuc
4549f4a2713aSLionel Sambuc // Recover by implicitly importing this module.
4550*0a6a1f1dSLionel Sambuc if (ErrorRecovery)
4551*0a6a1f1dSLionel Sambuc createImplicitModuleImportForErrorRecovery(
4552*0a6a1f1dSLionel Sambuc Correction.getCorrectionRange().getBegin(), Owner);
4553f4a2713aSLionel Sambuc return;
4554f4a2713aSLionel Sambuc }
4555f4a2713aSLionel Sambuc
4556f4a2713aSLionel Sambuc Diag(Correction.getCorrectionRange().getBegin(), TypoDiag)
4557f4a2713aSLionel Sambuc << CorrectedQuotedStr << (ErrorRecovery ? FixTypo : FixItHint());
4558f4a2713aSLionel Sambuc
4559f4a2713aSLionel Sambuc NamedDecl *ChosenDecl =
4560*0a6a1f1dSLionel Sambuc Correction.isKeyword() ? nullptr : Correction.getCorrectionDecl();
4561f4a2713aSLionel Sambuc if (PrevNote.getDiagID() && ChosenDecl)
4562f4a2713aSLionel Sambuc Diag(ChosenDecl->getLocation(), PrevNote)
4563f4a2713aSLionel Sambuc << CorrectedQuotedStr << (ErrorRecovery ? FixItHint() : FixTypo);
4564f4a2713aSLionel Sambuc }
4565*0a6a1f1dSLionel Sambuc
createDelayedTypo(std::unique_ptr<TypoCorrectionConsumer> TCC,TypoDiagnosticGenerator TDG,TypoRecoveryCallback TRC)4566*0a6a1f1dSLionel Sambuc TypoExpr *Sema::createDelayedTypo(std::unique_ptr<TypoCorrectionConsumer> TCC,
4567*0a6a1f1dSLionel Sambuc TypoDiagnosticGenerator TDG,
4568*0a6a1f1dSLionel Sambuc TypoRecoveryCallback TRC) {
4569*0a6a1f1dSLionel Sambuc assert(TCC && "createDelayedTypo requires a valid TypoCorrectionConsumer");
4570*0a6a1f1dSLionel Sambuc auto TE = new (Context) TypoExpr(Context.DependentTy);
4571*0a6a1f1dSLionel Sambuc auto &State = DelayedTypos[TE];
4572*0a6a1f1dSLionel Sambuc State.Consumer = std::move(TCC);
4573*0a6a1f1dSLionel Sambuc State.DiagHandler = std::move(TDG);
4574*0a6a1f1dSLionel Sambuc State.RecoveryHandler = std::move(TRC);
4575*0a6a1f1dSLionel Sambuc return TE;
4576*0a6a1f1dSLionel Sambuc }
4577*0a6a1f1dSLionel Sambuc
getTypoExprState(TypoExpr * TE) const4578*0a6a1f1dSLionel Sambuc const Sema::TypoExprState &Sema::getTypoExprState(TypoExpr *TE) const {
4579*0a6a1f1dSLionel Sambuc auto Entry = DelayedTypos.find(TE);
4580*0a6a1f1dSLionel Sambuc assert(Entry != DelayedTypos.end() &&
4581*0a6a1f1dSLionel Sambuc "Failed to get the state for a TypoExpr!");
4582*0a6a1f1dSLionel Sambuc return Entry->second;
4583*0a6a1f1dSLionel Sambuc }
4584*0a6a1f1dSLionel Sambuc
clearDelayedTypo(TypoExpr * TE)4585*0a6a1f1dSLionel Sambuc void Sema::clearDelayedTypo(TypoExpr *TE) {
4586*0a6a1f1dSLionel Sambuc DelayedTypos.erase(TE);
4587*0a6a1f1dSLionel Sambuc }
4588