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