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