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