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