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