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 | //===----------------------------------------------------------------------===// |
| 14 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclCXX.h" |
| 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" |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Builtins.h" |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 26 | #include "clang/Basic/LangOptions.h" |
| 27 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 30 | #include <list> |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 31 | #include <set> |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 32 | #include <vector> |
| 33 | #include <iterator> |
| 34 | #include <utility> |
| 35 | #include <algorithm> |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
| 38 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | class UnqualUsingEntry { |
| 41 | const DeclContext *Nominated; |
| 42 | const DeclContext *CommonAncestor; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 43 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 44 | public: |
| 45 | UnqualUsingEntry(const DeclContext *Nominated, |
| 46 | const DeclContext *CommonAncestor) |
| 47 | : Nominated(Nominated), CommonAncestor(CommonAncestor) { |
| 48 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 49 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 50 | const DeclContext *getCommonAncestor() const { |
| 51 | return CommonAncestor; |
| 52 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 53 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 54 | const DeclContext *getNominatedNamespace() const { |
| 55 | return Nominated; |
| 56 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 57 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 58 | // Sort by the pointer value of the common ancestor. |
| 59 | struct Comparator { |
| 60 | bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { |
| 61 | return L.getCommonAncestor() < R.getCommonAncestor(); |
| 62 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 63 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 64 | bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { |
| 65 | return E.getCommonAncestor() < DC; |
| 66 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 67 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 68 | bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { |
| 69 | return DC < E.getCommonAncestor(); |
| 70 | } |
| 71 | }; |
| 72 | }; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 73 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 74 | /// A collection of using directives, as used by C++ unqualified |
| 75 | /// lookup. |
| 76 | class UnqualUsingDirectiveSet { |
| 77 | typedef llvm::SmallVector<UnqualUsingEntry, 8> ListTy; |
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 | ListTy list; |
| 80 | llvm::SmallPtrSet<DeclContext*, 8> visited; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 81 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 82 | public: |
| 83 | UnqualUsingDirectiveSet() {} |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 84 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 85 | void visitScopeChain(Scope *S, Scope *InnermostFileScope) { |
| 86 | // C++ [namespace.udir]p1: |
| 87 | // During unqualified name lookup, the names appear as if they |
| 88 | // were declared in the nearest enclosing namespace which contains |
| 89 | // both the using-directive and the nominated namespace. |
| 90 | DeclContext *InnermostFileDC |
| 91 | = static_cast<DeclContext*>(InnermostFileScope->getEntity()); |
| 92 | assert(InnermostFileDC && InnermostFileDC->isFileContext()); |
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 | for (; S; S = S->getParent()) { |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 95 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 96 | DeclContext *EffectiveDC = (Ctx->isFileContext() ? Ctx : InnermostFileDC); |
| 97 | visit(Ctx, EffectiveDC); |
| 98 | } else { |
| 99 | Scope::udir_iterator I = S->using_directives_begin(), |
| 100 | End = S->using_directives_end(); |
| 101 | |
| 102 | for (; I != End; ++I) |
| 103 | visit(I->getAs<UsingDirectiveDecl>(), InnermostFileDC); |
| 104 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 107 | |
| 108 | // Visits a context and collect all of its using directives |
| 109 | // recursively. Treats all using directives as if they were |
| 110 | // declared in the context. |
| 111 | // |
| 112 | // A given context is only every visited once, so it is important |
| 113 | // that contexts be visited from the inside out in order to get |
| 114 | // the effective DCs right. |
| 115 | void visit(DeclContext *DC, DeclContext *EffectiveDC) { |
| 116 | if (!visited.insert(DC)) |
| 117 | return; |
| 118 | |
| 119 | addUsingDirectives(DC, EffectiveDC); |
| 120 | } |
| 121 | |
| 122 | // Visits a using directive and collects all of its using |
| 123 | // directives recursively. Treats all using directives as if they |
| 124 | // were declared in the effective DC. |
| 125 | void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 126 | DeclContext *NS = UD->getNominatedNamespace(); |
| 127 | if (!visited.insert(NS)) |
| 128 | return; |
| 129 | |
| 130 | addUsingDirective(UD, EffectiveDC); |
| 131 | addUsingDirectives(NS, EffectiveDC); |
| 132 | } |
| 133 | |
| 134 | // Adds all the using directives in a context (and those nominated |
| 135 | // by its using directives, transitively) as if they appeared in |
| 136 | // the given effective context. |
| 137 | void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { |
| 138 | llvm::SmallVector<DeclContext*,4> queue; |
| 139 | while (true) { |
| 140 | DeclContext::udir_iterator I, End; |
| 141 | for (llvm::tie(I, End) = DC->getUsingDirectives(); I != End; ++I) { |
| 142 | UsingDirectiveDecl *UD = *I; |
| 143 | DeclContext *NS = UD->getNominatedNamespace(); |
| 144 | if (visited.insert(NS)) { |
| 145 | addUsingDirective(UD, EffectiveDC); |
| 146 | queue.push_back(NS); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (queue.empty()) |
| 151 | return; |
| 152 | |
| 153 | DC = queue.back(); |
| 154 | queue.pop_back(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Add a using directive as if it had been declared in the given |
| 159 | // context. This helps implement C++ [namespace.udir]p3: |
| 160 | // The using-directive is transitive: if a scope contains a |
| 161 | // using-directive that nominates a second namespace that itself |
| 162 | // contains using-directives, the effect is as if the |
| 163 | // using-directives from the second namespace also appeared in |
| 164 | // the first. |
| 165 | void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 166 | // Find the common ancestor between the effective context and |
| 167 | // the nominated namespace. |
| 168 | DeclContext *Common = UD->getNominatedNamespace(); |
| 169 | while (!Common->Encloses(EffectiveDC)) |
| 170 | Common = Common->getParent(); |
John McCall | 12ea578 | 2009-11-10 09:20:04 +0000 | [diff] [blame] | 171 | Common = Common->getPrimaryContext(); |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 172 | |
| 173 | list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); |
| 174 | } |
| 175 | |
| 176 | void done() { |
| 177 | std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator()); |
| 178 | } |
| 179 | |
| 180 | typedef ListTy::iterator iterator; |
| 181 | typedef ListTy::const_iterator const_iterator; |
| 182 | |
| 183 | iterator begin() { return list.begin(); } |
| 184 | iterator end() { return list.end(); } |
| 185 | const_iterator begin() const { return list.begin(); } |
| 186 | const_iterator end() const { return list.end(); } |
| 187 | |
| 188 | std::pair<const_iterator,const_iterator> |
| 189 | getNamespacesFor(DeclContext *DC) const { |
John McCall | 12ea578 | 2009-11-10 09:20:04 +0000 | [diff] [blame] | 190 | return std::equal_range(begin(), end(), DC->getPrimaryContext(), |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 191 | UnqualUsingEntry::Comparator()); |
| 192 | } |
| 193 | }; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 196 | // Retrieve the set of identifier namespaces that correspond to a |
| 197 | // specific kind of name lookup. |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 198 | static inline unsigned getIDNS(Sema::LookupNameKind NameKind, |
| 199 | bool CPlusPlus, |
| 200 | bool Redeclaration) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 201 | unsigned IDNS = 0; |
| 202 | switch (NameKind) { |
| 203 | case Sema::LookupOrdinaryName: |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 204 | case Sema::LookupRedeclarationWithLinkage: |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 205 | IDNS = Decl::IDNS_Ordinary; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 206 | if (CPlusPlus) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 207 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 208 | if (Redeclaration) IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; |
| 209 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 210 | break; |
| 211 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 212 | case Sema::LookupOperatorName: |
| 213 | // Operator lookup is its own crazy thing; it is not the same |
| 214 | // as (e.g.) looking up an operator name for redeclaration. |
| 215 | assert(!Redeclaration && "cannot do redeclaration operator lookup"); |
| 216 | IDNS = Decl::IDNS_NonMemberOperator; |
| 217 | break; |
| 218 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 219 | case Sema::LookupTagName: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 220 | if (CPlusPlus) { |
| 221 | IDNS = Decl::IDNS_Type; |
| 222 | |
| 223 | // When looking for a redeclaration of a tag name, we add: |
| 224 | // 1) TagFriend to find undeclared friend decls |
| 225 | // 2) Namespace because they can't "overload" with tag decls. |
| 226 | // 3) Tag because it includes class templates, which can't |
| 227 | // "overload" with tag decls. |
| 228 | if (Redeclaration) |
| 229 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; |
| 230 | } else { |
| 231 | IDNS = Decl::IDNS_Tag; |
| 232 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 233 | break; |
| 234 | |
| 235 | case Sema::LookupMemberName: |
| 236 | IDNS = Decl::IDNS_Member; |
| 237 | if (CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 239 | break; |
| 240 | |
| 241 | case Sema::LookupNestedNameSpecifierName: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 242 | IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; |
| 243 | break; |
| 244 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 245 | case Sema::LookupNamespaceName: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 246 | IDNS = Decl::IDNS_Namespace; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 247 | break; |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 248 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 249 | case Sema::LookupUsingDeclName: |
| 250 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag |
| 251 | | Decl::IDNS_Member | Decl::IDNS_Using; |
| 252 | break; |
| 253 | |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 254 | case Sema::LookupObjCProtocolName: |
| 255 | IDNS = Decl::IDNS_ObjCProtocol; |
| 256 | break; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 257 | } |
| 258 | return IDNS; |
| 259 | } |
| 260 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 261 | void LookupResult::configure() { |
| 262 | IDNS = getIDNS(LookupKind, |
| 263 | SemaRef.getLangOptions().CPlusPlus, |
| 264 | isForRedeclaration()); |
Douglas Gregor | b5b2ccb | 2010-03-24 05:07:21 +0000 | [diff] [blame] | 265 | |
| 266 | // If we're looking for one of the allocation or deallocation |
| 267 | // operators, make sure that the implicitly-declared new and delete |
| 268 | // operators can be found. |
| 269 | if (!isForRedeclaration()) { |
| 270 | switch (Name.getCXXOverloadedOperator()) { |
| 271 | case OO_New: |
| 272 | case OO_Delete: |
| 273 | case OO_Array_New: |
| 274 | case OO_Array_Delete: |
| 275 | SemaRef.DeclareGlobalNewDelete(); |
| 276 | break; |
| 277 | |
| 278 | default: |
| 279 | break; |
| 280 | } |
| 281 | } |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 282 | } |
| 283 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 284 | // Necessary because CXXBasePaths is not complete in Sema.h |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 285 | void LookupResult::deletePaths(CXXBasePaths *Paths) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 286 | delete Paths; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 287 | } |
| 288 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 289 | /// Resolves the result kind of this lookup. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 290 | void LookupResult::resolveKind() { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 291 | unsigned N = Decls.size(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 292 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 293 | // Fast case: no possible ambiguity. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 294 | if (N == 0) { |
John McCall | dc5c786 | 2010-01-15 21:27:01 +0000 | [diff] [blame] | 295 | assert(ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 299 | // If there's a single decl, we need to examine it to decide what |
| 300 | // kind of lookup this is. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 301 | if (N == 1) { |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 302 | NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); |
| 303 | if (isa<FunctionTemplateDecl>(D)) |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 304 | ResultKind = FoundOverloaded; |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 305 | else if (isa<UnresolvedUsingValueDecl>(D)) |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 306 | ResultKind = FoundUnresolvedValue; |
| 307 | return; |
| 308 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 309 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 310 | // 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] | 311 | if (ResultKind == Ambiguous) return; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 312 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 313 | llvm::SmallPtrSet<NamedDecl*, 16> Unique; |
| 314 | |
| 315 | bool Ambiguous = false; |
| 316 | bool HasTag = false, HasFunction = false, HasNonFunction = false; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 317 | bool HasFunctionTemplate = false, HasUnresolved = false; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 318 | |
| 319 | unsigned UniqueTagIndex = 0; |
| 320 | |
| 321 | unsigned I = 0; |
| 322 | while (I < N) { |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 323 | NamedDecl *D = Decls[I]->getUnderlyingDecl(); |
| 324 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 325 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 326 | if (!Unique.insert(D)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 327 | // If it's not unique, pull something off the back (and |
| 328 | // continue at this index). |
| 329 | Decls[I] = Decls[--N]; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 330 | } else { |
| 331 | // Otherwise, do some decl type analysis and then continue. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 332 | |
| 333 | if (isa<UnresolvedUsingValueDecl>(D)) { |
| 334 | HasUnresolved = true; |
| 335 | } else if (isa<TagDecl>(D)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 336 | if (HasTag) |
| 337 | Ambiguous = true; |
| 338 | UniqueTagIndex = I; |
| 339 | HasTag = true; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 340 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 341 | HasFunction = true; |
| 342 | HasFunctionTemplate = true; |
| 343 | } else if (isa<FunctionDecl>(D)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 344 | HasFunction = true; |
| 345 | } else { |
| 346 | if (HasNonFunction) |
| 347 | Ambiguous = true; |
| 348 | HasNonFunction = true; |
| 349 | } |
| 350 | I++; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 351 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | } |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 353 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 354 | // C++ [basic.scope.hiding]p2: |
| 355 | // A class name or enumeration name can be hidden by the name of |
| 356 | // an object, function, or enumerator declared in the same |
| 357 | // scope. If a class or enumeration name and an object, function, |
| 358 | // or enumerator are declared in the same scope (in any order) |
| 359 | // with the same name, the class or enumeration name is hidden |
| 360 | // wherever the object, function, or enumerator name is visible. |
| 361 | // But it's still an error if there are distinct tag types found, |
| 362 | // even if they're not visible. (ref?) |
John McCall | fda8e12 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 363 | if (HideTags && HasTag && !Ambiguous && |
| 364 | (HasFunction || HasNonFunction || HasUnresolved)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 365 | Decls[UniqueTagIndex] = Decls[--N]; |
Anders Carlsson | 8b50d01 | 2009-06-26 03:37:05 +0000 | [diff] [blame] | 366 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 367 | Decls.set_size(N); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 368 | |
John McCall | fda8e12 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 369 | if (HasNonFunction && (HasFunction || HasUnresolved)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 370 | Ambiguous = true; |
Douglas Gregor | 69d993a | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 371 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 372 | if (Ambiguous) |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 373 | setAmbiguous(LookupResult::AmbiguousReference); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 374 | else if (HasUnresolved) |
| 375 | ResultKind = LookupResult::FoundUnresolvedValue; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 376 | else if (N > 1 || HasFunctionTemplate) |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 377 | ResultKind = LookupResult::FoundOverloaded; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 378 | else |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 379 | ResultKind = LookupResult::Found; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 380 | } |
| 381 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 382 | void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 383 | CXXBasePaths::const_paths_iterator I, E; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 384 | DeclContext::lookup_iterator DI, DE; |
| 385 | for (I = P.begin(), E = P.end(); I != E; ++I) |
| 386 | for (llvm::tie(DI,DE) = I->Decls; DI != DE; ++DI) |
| 387 | addDecl(*DI); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 388 | } |
| 389 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 390 | void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 391 | Paths = new CXXBasePaths; |
| 392 | Paths->swap(P); |
| 393 | addDeclsFromBasePaths(*Paths); |
| 394 | resolveKind(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 395 | setAmbiguous(AmbiguousBaseSubobjects); |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 396 | } |
| 397 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 398 | void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 399 | Paths = new CXXBasePaths; |
| 400 | Paths->swap(P); |
| 401 | addDeclsFromBasePaths(*Paths); |
| 402 | resolveKind(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 403 | setAmbiguous(AmbiguousBaseSubobjectTypes); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 404 | } |
| 405 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 406 | void LookupResult::print(llvm::raw_ostream &Out) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 407 | Out << Decls.size() << " result(s)"; |
| 408 | if (isAmbiguous()) Out << ", ambiguous"; |
| 409 | if (Paths) Out << ", base paths present"; |
| 410 | |
| 411 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 412 | Out << "\n"; |
| 413 | (*I)->print(Out, 2); |
| 414 | } |
| 415 | } |
| 416 | |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 417 | /// \brief Lookup a builtin function, when name lookup would otherwise |
| 418 | /// fail. |
| 419 | static bool LookupBuiltin(Sema &S, LookupResult &R) { |
| 420 | Sema::LookupNameKind NameKind = R.getLookupKind(); |
| 421 | |
| 422 | // If we didn't find a use of this identifier, and if the identifier |
| 423 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 424 | // now, injecting it into translation unit scope, and return it. |
| 425 | if (NameKind == Sema::LookupOrdinaryName || |
| 426 | NameKind == Sema::LookupRedeclarationWithLinkage) { |
| 427 | IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); |
| 428 | if (II) { |
| 429 | // If this is a builtin on this (or all) targets, create the decl. |
| 430 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 431 | // In C++, we don't have any predefined library functions like |
| 432 | // 'malloc'. Instead, we'll just error. |
| 433 | if (S.getLangOptions().CPlusPlus && |
| 434 | S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 435 | return false; |
| 436 | |
| 437 | NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 438 | S.TUScope, R.isForRedeclaration(), |
| 439 | R.getNameLoc()); |
| 440 | if (D) |
| 441 | R.addDecl(D); |
| 442 | return (D != NULL); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | return false; |
| 448 | } |
| 449 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 450 | // Adds all qualifying matches for a name within a decl context to the |
| 451 | // given lookup result. Returns true if any matches were found. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 452 | static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 453 | bool Found = false; |
| 454 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 455 | DeclContext::lookup_const_iterator I, E; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 456 | for (llvm::tie(I, E) = DC->lookup(R.getLookupName()); I != E; ++I) { |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 457 | NamedDecl *D = *I; |
| 458 | if (R.isAcceptableDecl(D)) { |
| 459 | R.addDecl(D); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 460 | Found = true; |
| 461 | } |
| 462 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 464 | if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R)) |
| 465 | return true; |
| 466 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 467 | if (R.getLookupName().getNameKind() |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 468 | != DeclarationName::CXXConversionFunctionName || |
| 469 | R.getLookupName().getCXXNameType()->isDependentType() || |
| 470 | !isa<CXXRecordDecl>(DC)) |
| 471 | return Found; |
| 472 | |
| 473 | // C++ [temp.mem]p6: |
| 474 | // A specialization of a conversion function template is not found by |
| 475 | // name lookup. Instead, any conversion function templates visible in the |
| 476 | // context of the use are considered. [...] |
| 477 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 478 | if (!Record->isDefinition()) |
| 479 | return Found; |
| 480 | |
| 481 | const UnresolvedSetImpl *Unresolved = Record->getConversionFunctions(); |
| 482 | for (UnresolvedSetImpl::iterator U = Unresolved->begin(), |
| 483 | UEnd = Unresolved->end(); U != UEnd; ++U) { |
| 484 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); |
| 485 | if (!ConvTemplate) |
| 486 | continue; |
| 487 | |
| 488 | // When we're performing lookup for the purposes of redeclaration, just |
| 489 | // add the conversion function template. When we deduce template |
| 490 | // arguments for specializations, we'll end up unifying the return |
| 491 | // type of the new declaration with the type of the function template. |
| 492 | if (R.isForRedeclaration()) { |
| 493 | R.addDecl(ConvTemplate); |
| 494 | Found = true; |
| 495 | continue; |
| 496 | } |
| 497 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 498 | // C++ [temp.mem]p6: |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 499 | // [...] For each such operator, if argument deduction succeeds |
| 500 | // (14.9.2.3), the resulting specialization is used as if found by |
| 501 | // name lookup. |
| 502 | // |
| 503 | // When referencing a conversion function for any purpose other than |
| 504 | // a redeclaration (such that we'll be building an expression with the |
| 505 | // result), perform template argument deduction and place the |
| 506 | // specialization into the result set. We do this to avoid forcing all |
| 507 | // callers to perform special deduction for conversion functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 508 | Sema::TemplateDeductionInfo Info(R.getSema().Context, R.getNameLoc()); |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 509 | FunctionDecl *Specialization = 0; |
| 510 | |
| 511 | const FunctionProtoType *ConvProto |
| 512 | = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); |
| 513 | assert(ConvProto && "Nonsensical conversion function template type"); |
Douglas Gregor | 3f477a1 | 2010-01-12 01:17:50 +0000 | [diff] [blame] | 514 | |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 515 | // Compute the type of the function that we would expect the conversion |
| 516 | // function to have, if it were to match the name given. |
| 517 | // FIXME: Calling convention! |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 518 | FunctionType::ExtInfo ConvProtoInfo = ConvProto->getExtInfo(); |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 519 | QualType ExpectedType |
| 520 | = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(), |
| 521 | 0, 0, ConvProto->isVariadic(), |
| 522 | ConvProto->getTypeQuals(), |
| 523 | false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 524 | ConvProtoInfo.withCallingConv(CC_Default)); |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 525 | |
| 526 | // Perform template argument deduction against the type that we would |
| 527 | // expect the function to have. |
| 528 | if (R.getSema().DeduceTemplateArguments(ConvTemplate, 0, ExpectedType, |
| 529 | Specialization, Info) |
| 530 | == Sema::TDK_Success) { |
| 531 | R.addDecl(Specialization); |
| 532 | Found = true; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 535 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 536 | return Found; |
| 537 | } |
| 538 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 539 | // Performs C++ unqualified lookup into the given file context. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 540 | static bool |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 541 | CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, |
| 542 | DeclContext *NS, UnqualUsingDirectiveSet &UDirs) { |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 543 | |
| 544 | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); |
| 545 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 546 | // Perform direct name lookup into the LookupCtx. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 547 | bool Found = LookupDirect(S, R, NS); |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 548 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 549 | // Perform direct name lookup into the namespaces nominated by the |
| 550 | // using directives whose common ancestor is this namespace. |
| 551 | UnqualUsingDirectiveSet::const_iterator UI, UEnd; |
| 552 | llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(NS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 554 | for (; UI != UEnd; ++UI) |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 555 | if (LookupDirect(S, R, UI->getNominatedNamespace())) |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 556 | Found = true; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 557 | |
| 558 | R.resolveKind(); |
| 559 | |
| 560 | return Found; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static bool isNamespaceOrTranslationUnitScope(Scope *S) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 564 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 565 | return Ctx->isFileContext(); |
| 566 | return false; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 567 | } |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 569 | // Find the next outer declaration context from this scope. This |
| 570 | // routine actually returns the semantic outer context, which may |
| 571 | // differ from the lexical context (encoded directly in the Scope |
| 572 | // stack) when we are parsing a member of a class template. In this |
| 573 | // case, the second element of the pair will be true, to indicate that |
| 574 | // name lookup should continue searching in this semantic context when |
| 575 | // it leaves the current template parameter scope. |
| 576 | static std::pair<DeclContext *, bool> findOuterContext(Scope *S) { |
| 577 | DeclContext *DC = static_cast<DeclContext *>(S->getEntity()); |
| 578 | DeclContext *Lexical = 0; |
| 579 | for (Scope *OuterS = S->getParent(); OuterS; |
| 580 | OuterS = OuterS->getParent()) { |
| 581 | if (OuterS->getEntity()) { |
Douglas Gregor | dbdf5e7 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 582 | Lexical = static_cast<DeclContext *>(OuterS->getEntity()); |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 583 | break; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // C++ [temp.local]p8: |
| 588 | // In the definition of a member of a class template that appears |
| 589 | // outside of the namespace containing the class template |
| 590 | // definition, the name of a template-parameter hides the name of |
| 591 | // a member of this namespace. |
| 592 | // |
| 593 | // Example: |
| 594 | // |
| 595 | // namespace N { |
| 596 | // class C { }; |
| 597 | // |
| 598 | // template<class T> class B { |
| 599 | // void f(T); |
| 600 | // }; |
| 601 | // } |
| 602 | // |
| 603 | // template<class C> void N::B<C>::f(C) { |
| 604 | // C b; // C is the template parameter, not N::C |
| 605 | // } |
| 606 | // |
| 607 | // In this example, the lexical context we return is the |
| 608 | // TranslationUnit, while the semantic context is the namespace N. |
| 609 | if (!Lexical || !DC || !S->getParent() || |
| 610 | !S->getParent()->isTemplateParamScope()) |
| 611 | return std::make_pair(Lexical, false); |
| 612 | |
| 613 | // Find the outermost template parameter scope. |
| 614 | // For the example, this is the scope for the template parameters of |
| 615 | // template<class C>. |
| 616 | Scope *OutermostTemplateScope = S->getParent(); |
| 617 | while (OutermostTemplateScope->getParent() && |
| 618 | OutermostTemplateScope->getParent()->isTemplateParamScope()) |
| 619 | OutermostTemplateScope = OutermostTemplateScope->getParent(); |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 620 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 621 | // Find the namespace context in which the original scope occurs. In |
| 622 | // the example, this is namespace N. |
| 623 | DeclContext *Semantic = DC; |
| 624 | while (!Semantic->isFileContext()) |
| 625 | Semantic = Semantic->getParent(); |
| 626 | |
| 627 | // Find the declaration context just outside of the template |
| 628 | // parameter scope. This is the context in which the template is |
| 629 | // being lexically declaration (a namespace context). In the |
| 630 | // example, this is the global scope. |
| 631 | if (Lexical->isFileContext() && !Lexical->Equals(Semantic) && |
| 632 | Lexical->Encloses(Semantic)) |
| 633 | return std::make_pair(Semantic, true); |
| 634 | |
| 635 | return std::make_pair(Lexical, false); |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 636 | } |
| 637 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 638 | bool Sema::CppLookupName(LookupResult &R, Scope *S) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 639 | assert(getLangOptions().CPlusPlus && "Can perform only C++ lookup"); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 640 | |
| 641 | DeclarationName Name = R.getLookupName(); |
| 642 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 643 | Scope *Initial = S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | IdentifierResolver::iterator |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 645 | I = IdResolver.begin(Name), |
| 646 | IEnd = IdResolver.end(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 647 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 648 | // First we lookup local scope. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 649 | // 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] | 650 | // ...During unqualified name lookup (3.4.1), the names appear as if |
| 651 | // they were declared in the nearest enclosing namespace which contains |
| 652 | // both the using-directive and the nominated namespace. |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 653 | // [Note: in this context, "contains" means "contains directly or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | // indirectly". |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 655 | // |
| 656 | // For example: |
| 657 | // namespace A { int i; } |
| 658 | // void foo() { |
| 659 | // int i; |
| 660 | // { |
| 661 | // using namespace A; |
| 662 | // ++i; // finds local 'i', A::i appears at global scope |
| 663 | // } |
| 664 | // } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 665 | // |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 666 | DeclContext *OutsideOfTemplateParamDC = 0; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 667 | for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 668 | DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()); |
| 669 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 670 | // Check whether the IdResolver has anything in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 671 | bool Found = false; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 672 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 673 | if (R.isAcceptableDecl(*I)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 674 | Found = true; |
| 675 | R.addDecl(*I); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 678 | if (Found) { |
| 679 | R.resolveKind(); |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 680 | if (S->isClassScope()) |
| 681 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
| 682 | R.setNamingClass(Record); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 683 | return true; |
| 684 | } |
| 685 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 686 | if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && |
| 687 | S->getParent() && !S->getParent()->isTemplateParamScope()) { |
| 688 | // We've just searched the last template parameter scope and |
| 689 | // found nothing, so look into the the contexts between the |
| 690 | // lexical and semantic declaration contexts returned by |
| 691 | // findOuterContext(). This implements the name lookup behavior |
| 692 | // of C++ [temp.local]p8. |
| 693 | Ctx = OutsideOfTemplateParamDC; |
| 694 | OutsideOfTemplateParamDC = 0; |
| 695 | } |
| 696 | |
| 697 | if (Ctx) { |
| 698 | DeclContext *OuterCtx; |
| 699 | bool SearchAfterTemplateScope; |
| 700 | llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); |
| 701 | if (SearchAfterTemplateScope) |
| 702 | OutsideOfTemplateParamDC = OuterCtx; |
| 703 | |
Douglas Gregor | dbdf5e7 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 704 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { |
Douglas Gregor | 36262b8 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 705 | // We do not directly look into transparent contexts, since |
| 706 | // those entities will be found in the nearest enclosing |
| 707 | // non-transparent context. |
| 708 | if (Ctx->isTransparentContext()) |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 709 | continue; |
Douglas Gregor | 36262b8 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 710 | |
| 711 | // We do not look directly into function or method contexts, |
| 712 | // since all of the local variables and parameters of the |
| 713 | // function/method are present within the Scope. |
| 714 | if (Ctx->isFunctionOrMethod()) { |
| 715 | // If we have an Objective-C instance method, look for ivars |
| 716 | // in the corresponding interface. |
| 717 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
| 718 | if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()) |
| 719 | if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { |
| 720 | ObjCInterfaceDecl *ClassDeclared; |
| 721 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( |
| 722 | Name.getAsIdentifierInfo(), |
| 723 | ClassDeclared)) { |
| 724 | if (R.isAcceptableDecl(Ivar)) { |
| 725 | R.addDecl(Ivar); |
| 726 | R.resolveKind(); |
| 727 | return true; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | continue; |
| 734 | } |
| 735 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 736 | // Perform qualified name lookup into this context. |
| 737 | // FIXME: In some cases, we know that every name that could be found by |
| 738 | // this qualified name lookup will also be on the identifier chain. For |
| 739 | // example, inside a class without any base classes, we never need to |
| 740 | // perform qualified lookup because all of the members are on top of the |
| 741 | // identifier chain. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 742 | if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 743 | return true; |
Douglas Gregor | 551f48c | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 744 | } |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 745 | } |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 746 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 747 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 748 | // Stop if we ran out of scopes. |
| 749 | // FIXME: This really, really shouldn't be happening. |
| 750 | if (!S) return false; |
| 751 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 752 | // Collect UsingDirectiveDecls in all scopes, and recursively all |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 753 | // nominated namespaces by those using-directives. |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 754 | // |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 755 | // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we |
| 756 | // don't build it for each lookup! |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 757 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 758 | UnqualUsingDirectiveSet UDirs; |
| 759 | UDirs.visitScopeChain(Initial, S); |
| 760 | UDirs.done(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 762 | // Lookup namespace scope, and global scope. |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 763 | // Unqualified name lookup in C++ requires looking into scopes |
| 764 | // that aren't strictly lexical, and therefore we walk through the |
| 765 | // context as well as walking through the scopes. |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 767 | for (; S; S = S->getParent()) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 768 | // Check whether the IdResolver has anything in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 769 | bool Found = false; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 770 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 771 | if (R.isAcceptableDecl(*I)) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 772 | // We found something. Look for anything else in our scope |
| 773 | // with this same name and in an acceptable identifier |
| 774 | // namespace, so that we can construct an overload set if we |
| 775 | // need to. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 776 | Found = true; |
| 777 | R.addDecl(*I); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Douglas Gregor | 00b4b03 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 781 | if (Found && S->isTemplateParamScope()) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 782 | R.resolveKind(); |
| 783 | return true; |
| 784 | } |
| 785 | |
Douglas Gregor | 00b4b03 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 786 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 787 | if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && |
| 788 | S->getParent() && !S->getParent()->isTemplateParamScope()) { |
| 789 | // We've just searched the last template parameter scope and |
| 790 | // found nothing, so look into the the contexts between the |
| 791 | // lexical and semantic declaration contexts returned by |
| 792 | // findOuterContext(). This implements the name lookup behavior |
| 793 | // of C++ [temp.local]p8. |
| 794 | Ctx = OutsideOfTemplateParamDC; |
| 795 | OutsideOfTemplateParamDC = 0; |
| 796 | } |
| 797 | |
| 798 | if (Ctx) { |
| 799 | DeclContext *OuterCtx; |
| 800 | bool SearchAfterTemplateScope; |
| 801 | llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); |
| 802 | if (SearchAfterTemplateScope) |
| 803 | OutsideOfTemplateParamDC = OuterCtx; |
| 804 | |
| 805 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { |
| 806 | // We do not directly look into transparent contexts, since |
| 807 | // those entities will be found in the nearest enclosing |
| 808 | // non-transparent context. |
| 809 | if (Ctx->isTransparentContext()) |
| 810 | continue; |
| 811 | |
| 812 | // If we have a context, and it's not a context stashed in the |
| 813 | // template parameter scope for an out-of-line definition, also |
| 814 | // look into that context. |
| 815 | if (!(Found && S && S->isTemplateParamScope())) { |
| 816 | assert(Ctx->isFileContext() && |
| 817 | "We should have been looking only at file context here already."); |
| 818 | |
| 819 | // Look into context considering using-directives. |
| 820 | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) |
| 821 | Found = true; |
| 822 | } |
| 823 | |
| 824 | if (Found) { |
| 825 | R.resolveKind(); |
| 826 | return true; |
| 827 | } |
| 828 | |
| 829 | if (R.isForRedeclaration() && !Ctx->isTransparentContext()) |
| 830 | return false; |
| 831 | } |
| 832 | } |
| 833 | |
Douglas Gregor | 1df0ee9 | 2010-02-05 07:07:10 +0000 | [diff] [blame] | 834 | if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 835 | return false; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 836 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 837 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 838 | return !R.empty(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 841 | /// @brief Perform unqualified name lookup starting from a given |
| 842 | /// scope. |
| 843 | /// |
| 844 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 845 | /// used to find names within the current scope. For example, 'x' in |
| 846 | /// @code |
| 847 | /// int x; |
| 848 | /// int f() { |
| 849 | /// return x; // unqualified name look finds 'x' in the global scope |
| 850 | /// } |
| 851 | /// @endcode |
| 852 | /// |
| 853 | /// Different lookup criteria can find different names. For example, a |
| 854 | /// particular scope can have both a struct and a function of the same |
| 855 | /// name, and each can be found by certain lookup criteria. For more |
| 856 | /// information about lookup criteria, see the documentation for the |
| 857 | /// class LookupCriteria. |
| 858 | /// |
| 859 | /// @param S The scope from which unqualified name lookup will |
| 860 | /// begin. If the lookup criteria permits, name lookup may also search |
| 861 | /// in the parent scopes. |
| 862 | /// |
| 863 | /// @param Name The name of the entity that we are searching for. |
| 864 | /// |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 865 | /// @param Loc If provided, the source location where we're performing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | /// name lookup. At present, this is only used to produce diagnostics when |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 867 | /// C library functions (like "malloc") are implicitly declared. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 868 | /// |
| 869 | /// @returns The result of name lookup, which includes zero or more |
| 870 | /// declarations and possibly additional information used to diagnose |
| 871 | /// ambiguities. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 872 | bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) { |
| 873 | DeclarationName Name = R.getLookupName(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 874 | if (!Name) return false; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 875 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 876 | LookupNameKind NameKind = R.getLookupKind(); |
| 877 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 878 | if (!getLangOptions().CPlusPlus) { |
| 879 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 880 | // search in the declarations attached to the name. |
| 881 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 882 | if (NameKind == Sema::LookupRedeclarationWithLinkage) { |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 883 | // Find the nearest non-transparent declaration scope. |
| 884 | while (!(S->getFlags() & Scope::DeclScope) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | (S->getEntity() && |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 886 | static_cast<DeclContext *>(S->getEntity()) |
| 887 | ->isTransparentContext())) |
| 888 | S = S->getParent(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 889 | } |
| 890 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 891 | unsigned IDNS = R.getIdentifierNamespace(); |
| 892 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 893 | // Scan up the scope chain looking for a decl that matches this |
| 894 | // identifier that is in the appropriate namespace. This search |
| 895 | // should not take long, as shadowing of names is uncommon, and |
| 896 | // deep shadowing is extremely uncommon. |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 897 | bool LeftStartingScope = false; |
| 898 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 899 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | IEnd = IdResolver.end(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 901 | I != IEnd; ++I) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 902 | if ((*I)->isInIdentifierNamespace(IDNS)) { |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 903 | if (NameKind == LookupRedeclarationWithLinkage) { |
| 904 | // Determine whether this (or a previous) declaration is |
| 905 | // out-of-scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 906 | if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 907 | LeftStartingScope = true; |
| 908 | |
| 909 | // If we found something outside of our starting scope that |
| 910 | // does not have linkage, skip it. |
| 911 | if (LeftStartingScope && !((*I)->hasLinkage())) |
| 912 | continue; |
| 913 | } |
| 914 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 915 | R.addDecl(*I); |
| 916 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 917 | if ((*I)->getAttr<OverloadableAttr>()) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 918 | // If this declaration has the "overloadable" attribute, we |
| 919 | // might have a set of overloaded functions. |
| 920 | |
| 921 | // Figure out what scope the identifier is in. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 922 | while (!(S->getFlags() & Scope::DeclScope) || |
| 923 | !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 924 | S = S->getParent(); |
| 925 | |
| 926 | // Find the last declaration in this scope (with the same |
| 927 | // name, naturally). |
| 928 | IdentifierResolver::iterator LastI = I; |
| 929 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 930 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 931 | break; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 932 | R.addDecl(*LastI); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 933 | } |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 934 | } |
| 935 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 936 | R.resolveKind(); |
| 937 | |
| 938 | return true; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 939 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 940 | } else { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 941 | // Perform C++ unqualified name lookup. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 942 | if (CppLookupName(R, S)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 943 | return true; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | // If we didn't find a use of this identifier, and if the identifier |
| 947 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 948 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 949 | if (AllowBuiltinCreation) |
| 950 | return LookupBuiltin(*this, R); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 951 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 952 | return false; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 953 | } |
| 954 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 955 | /// @brief Perform qualified name lookup in the namespaces nominated by |
| 956 | /// using directives by the given context. |
| 957 | /// |
| 958 | /// C++98 [namespace.qual]p2: |
| 959 | /// Given X::m (where X is a user-declared namespace), or given ::m |
| 960 | /// (where X is the global namespace), let S be the set of all |
| 961 | /// declarations of m in X and in the transitive closure of all |
| 962 | /// namespaces nominated by using-directives in X and its used |
| 963 | /// namespaces, except that using-directives are ignored in any |
| 964 | /// namespace, including X, directly containing one or more |
| 965 | /// declarations of m. No namespace is searched more than once in |
| 966 | /// the lookup of a name. If S is the empty set, the program is |
| 967 | /// ill-formed. Otherwise, if S has exactly one member, or if the |
| 968 | /// context of the reference is a using-declaration |
| 969 | /// (namespace.udecl), S is the required set of declarations of |
| 970 | /// m. Otherwise if the use of m is not one that allows a unique |
| 971 | /// declaration to be chosen from S, the program is ill-formed. |
| 972 | /// C++98 [namespace.qual]p5: |
| 973 | /// During the lookup of a qualified namespace member name, if the |
| 974 | /// lookup finds more than one declaration of the member, and if one |
| 975 | /// declaration introduces a class name or enumeration name and the |
| 976 | /// other declarations either introduce the same object, the same |
| 977 | /// enumerator or a set of functions, the non-type name hides the |
| 978 | /// class or enumeration name if and only if the declarations are |
| 979 | /// from the same namespace; otherwise (the declarations are from |
| 980 | /// different namespaces), the program is ill-formed. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 981 | static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 982 | DeclContext *StartDC) { |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 983 | assert(StartDC->isFileContext() && "start context is not a file context"); |
| 984 | |
| 985 | DeclContext::udir_iterator I = StartDC->using_directives_begin(); |
| 986 | DeclContext::udir_iterator E = StartDC->using_directives_end(); |
| 987 | |
| 988 | if (I == E) return false; |
| 989 | |
| 990 | // We have at least added all these contexts to the queue. |
| 991 | llvm::DenseSet<DeclContext*> Visited; |
| 992 | Visited.insert(StartDC); |
| 993 | |
| 994 | // We have not yet looked into these namespaces, much less added |
| 995 | // their "using-children" to the queue. |
| 996 | llvm::SmallVector<NamespaceDecl*, 8> Queue; |
| 997 | |
| 998 | // We have already looked into the initial namespace; seed the queue |
| 999 | // with its using-children. |
| 1000 | for (; I != E; ++I) { |
John McCall | d9f01d4 | 2009-11-10 09:25:37 +0000 | [diff] [blame] | 1001 | NamespaceDecl *ND = (*I)->getNominatedNamespace()->getOriginalNamespace(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1002 | if (Visited.insert(ND).second) |
| 1003 | Queue.push_back(ND); |
| 1004 | } |
| 1005 | |
| 1006 | // The easiest way to implement the restriction in [namespace.qual]p5 |
| 1007 | // is to check whether any of the individual results found a tag |
| 1008 | // and, if so, to declare an ambiguity if the final result is not |
| 1009 | // a tag. |
| 1010 | bool FoundTag = false; |
| 1011 | bool FoundNonTag = false; |
| 1012 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1013 | LookupResult LocalR(LookupResult::Temporary, R); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1014 | |
| 1015 | bool Found = false; |
| 1016 | while (!Queue.empty()) { |
| 1017 | NamespaceDecl *ND = Queue.back(); |
| 1018 | Queue.pop_back(); |
| 1019 | |
| 1020 | // We go through some convolutions here to avoid copying results |
| 1021 | // between LookupResults. |
| 1022 | bool UseLocal = !R.empty(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1023 | LookupResult &DirectR = UseLocal ? LocalR : R; |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1024 | bool FoundDirect = LookupDirect(S, DirectR, ND); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (FoundDirect) { |
| 1027 | // First do any local hiding. |
| 1028 | DirectR.resolveKind(); |
| 1029 | |
| 1030 | // If the local result is a tag, remember that. |
| 1031 | if (DirectR.isSingleTagDecl()) |
| 1032 | FoundTag = true; |
| 1033 | else |
| 1034 | FoundNonTag = true; |
| 1035 | |
| 1036 | // Append the local results to the total results if necessary. |
| 1037 | if (UseLocal) { |
| 1038 | R.addAllDecls(LocalR); |
| 1039 | LocalR.clear(); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | // If we find names in this namespace, ignore its using directives. |
| 1044 | if (FoundDirect) { |
| 1045 | Found = true; |
| 1046 | continue; |
| 1047 | } |
| 1048 | |
| 1049 | for (llvm::tie(I,E) = ND->getUsingDirectives(); I != E; ++I) { |
| 1050 | NamespaceDecl *Nom = (*I)->getNominatedNamespace(); |
| 1051 | if (Visited.insert(Nom).second) |
| 1052 | Queue.push_back(Nom); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | if (Found) { |
| 1057 | if (FoundTag && FoundNonTag) |
| 1058 | R.setAmbiguousQualifiedTagHiding(); |
| 1059 | else |
| 1060 | R.resolveKind(); |
| 1061 | } |
| 1062 | |
| 1063 | return Found; |
| 1064 | } |
| 1065 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1066 | /// \brief Perform qualified name lookup into a given context. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1067 | /// |
| 1068 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 1069 | /// names when the context of those names is explicit specified, e.g., |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1070 | /// "std::vector" or "x->member", or as part of unqualified name lookup. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1071 | /// |
| 1072 | /// Different lookup criteria can find different names. For example, a |
| 1073 | /// particular scope can have both a struct and a function of the same |
| 1074 | /// name, and each can be found by certain lookup criteria. For more |
| 1075 | /// information about lookup criteria, see the documentation for the |
| 1076 | /// class LookupCriteria. |
| 1077 | /// |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1078 | /// \param R captures both the lookup criteria and any lookup results found. |
| 1079 | /// |
| 1080 | /// \param LookupCtx The context in which qualified name lookup will |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1081 | /// search. If the lookup criteria permits, name lookup may also search |
| 1082 | /// in the parent contexts or (for C++ classes) base classes. |
| 1083 | /// |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1084 | /// \param InUnqualifiedLookup true if this is qualified name lookup that |
| 1085 | /// occurs as part of unqualified name lookup. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1086 | /// |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1087 | /// \returns true if lookup succeeded, false if it failed. |
| 1088 | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, |
| 1089 | bool InUnqualifiedLookup) { |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1090 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1092 | if (!R.getLookupName()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1093 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1095 | // Make sure that the declaration context is complete. |
| 1096 | assert((!isa<TagDecl>(LookupCtx) || |
| 1097 | LookupCtx->isDependentContext() || |
| 1098 | cast<TagDecl>(LookupCtx)->isDefinition() || |
| 1099 | Context.getTypeDeclType(cast<TagDecl>(LookupCtx))->getAs<TagType>() |
| 1100 | ->isBeingDefined()) && |
| 1101 | "Declaration context must already be complete!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1103 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1104 | if (LookupDirect(*this, R, LookupCtx)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1105 | R.resolveKind(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1106 | if (isa<CXXRecordDecl>(LookupCtx)) |
| 1107 | R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1108 | return true; |
| 1109 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1110 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1111 | // Don't descend into implied contexts for redeclarations. |
| 1112 | // C++98 [namespace.qual]p6: |
| 1113 | // In a declaration for a namespace member in which the |
| 1114 | // declarator-id is a qualified-id, given that the qualified-id |
| 1115 | // for the namespace member has the form |
| 1116 | // nested-name-specifier unqualified-id |
| 1117 | // the unqualified-id shall name a member of the namespace |
| 1118 | // designated by the nested-name-specifier. |
| 1119 | // See also [class.mfct]p5 and [class.static.data]p2. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1120 | if (R.isForRedeclaration()) |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1121 | return false; |
| 1122 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1123 | // If this is a namespace, look it up in the implied namespaces. |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1124 | if (LookupCtx->isFileContext()) |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1125 | return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1126 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1127 | // 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] | 1128 | // classes, we're done. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1129 | CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); |
Douglas Gregor | 025291b | 2010-07-01 00:21:21 +0000 | [diff] [blame] | 1130 | if (!LookupRec || !LookupRec->getDefinition()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1131 | return false; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1133 | // If we're performing qualified name lookup into a dependent class, |
| 1134 | // then we are actually looking into a current instantiation. If we have any |
| 1135 | // dependent base classes, then we either have to delay lookup until |
| 1136 | // template instantiation time (at which point all bases will be available) |
| 1137 | // or we have to fail. |
| 1138 | if (!InUnqualifiedLookup && LookupRec->isDependentContext() && |
| 1139 | LookupRec->hasAnyDependentBases()) { |
| 1140 | R.setNotFoundInCurrentInstantiation(); |
| 1141 | return false; |
| 1142 | } |
| 1143 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1144 | // Perform lookup into our base classes. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1145 | CXXBasePaths Paths; |
| 1146 | Paths.setOrigin(LookupRec); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Look for this member in our base classes |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1149 | CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1150 | switch (R.getLookupKind()) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1151 | case LookupOrdinaryName: |
| 1152 | case LookupMemberName: |
| 1153 | case LookupRedeclarationWithLinkage: |
| 1154 | BaseCallback = &CXXRecordDecl::FindOrdinaryMember; |
| 1155 | break; |
| 1156 | |
| 1157 | case LookupTagName: |
| 1158 | BaseCallback = &CXXRecordDecl::FindTagMember; |
| 1159 | break; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1160 | |
| 1161 | case LookupUsingDeclName: |
| 1162 | // This lookup is for redeclarations only. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1163 | |
| 1164 | case LookupOperatorName: |
| 1165 | case LookupNamespaceName: |
| 1166 | case LookupObjCProtocolName: |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1167 | // 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] | 1168 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1169 | |
| 1170 | case LookupNestedNameSpecifierName: |
| 1171 | BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember; |
| 1172 | break; |
| 1173 | } |
| 1174 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1175 | if (!LookupRec->lookupInBases(BaseCallback, |
| 1176 | R.getLookupName().getAsOpaquePtr(), Paths)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1177 | return false; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1178 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1179 | R.setNamingClass(LookupRec); |
| 1180 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1181 | // C++ [class.member.lookup]p2: |
| 1182 | // [...] If the resulting set of declarations are not all from |
| 1183 | // sub-objects of the same type, or the set has a nonstatic member |
| 1184 | // and includes members from distinct sub-objects, there is an |
| 1185 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 1186 | // the result of the lookup. |
| 1187 | // FIXME: support using declarations! |
| 1188 | QualType SubobjectType; |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1189 | int SubobjectNumber = 0; |
John McCall | 7aceaf8 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 1190 | AccessSpecifier SubobjectAccess = AS_none; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1191 | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1192 | Path != PathEnd; ++Path) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1193 | const CXXBasePathElement &PathElement = Path->back(); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1194 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1195 | // Pick the best (i.e. most permissive i.e. numerically lowest) access |
| 1196 | // across all paths. |
| 1197 | SubobjectAccess = std::min(SubobjectAccess, Path->Access); |
| 1198 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1199 | // Determine whether we're looking at a distinct sub-object or not. |
| 1200 | if (SubobjectType.isNull()) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1201 | // This is the first subobject we've looked at. Record its type. |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1202 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 1203 | SubobjectNumber = PathElement.SubobjectNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | } else if (SubobjectType |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1205 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 1206 | // We found members of the given name in two subobjects of |
| 1207 | // different types. This lookup is ambiguous. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1208 | R.setAmbiguousBaseSubobjectTypes(Paths); |
| 1209 | return true; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1210 | } else if (SubobjectNumber != PathElement.SubobjectNumber) { |
| 1211 | // We have a different subobject of the same type. |
| 1212 | |
| 1213 | // C++ [class.member.lookup]p5: |
| 1214 | // A static member, a nested type or an enumerator defined in |
| 1215 | // a base class T can unambiguously be found even if an object |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | // has more than one base class subobject of type T. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1217 | Decl *FirstDecl = *Path->Decls.first; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1218 | if (isa<VarDecl>(FirstDecl) || |
| 1219 | isa<TypeDecl>(FirstDecl) || |
| 1220 | isa<EnumConstantDecl>(FirstDecl)) |
| 1221 | continue; |
| 1222 | |
| 1223 | if (isa<CXXMethodDecl>(FirstDecl)) { |
| 1224 | // Determine whether all of the methods are static. |
| 1225 | bool AllMethodsAreStatic = true; |
| 1226 | for (DeclContext::lookup_iterator Func = Path->Decls.first; |
| 1227 | Func != Path->Decls.second; ++Func) { |
| 1228 | if (!isa<CXXMethodDecl>(*Func)) { |
| 1229 | assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); |
| 1230 | break; |
| 1231 | } |
| 1232 | |
| 1233 | if (!cast<CXXMethodDecl>(*Func)->isStatic()) { |
| 1234 | AllMethodsAreStatic = false; |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | if (AllMethodsAreStatic) |
| 1240 | continue; |
| 1241 | } |
| 1242 | |
| 1243 | // We have found a nonstatic member name in multiple, distinct |
| 1244 | // subobjects. Name lookup is ambiguous. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1245 | R.setAmbiguousBaseSubobjects(Paths); |
| 1246 | return true; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | // Lookup in a base class succeeded; return these results. |
| 1251 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1252 | DeclContext::lookup_iterator I, E; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1253 | for (llvm::tie(I,E) = Paths.front().Decls; I != E; ++I) { |
| 1254 | NamedDecl *D = *I; |
| 1255 | AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, |
| 1256 | D->getAccess()); |
| 1257 | R.addDecl(D, AS); |
| 1258 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1259 | R.resolveKind(); |
| 1260 | return true; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | /// @brief Performs name lookup for a name that was parsed in the |
| 1264 | /// source code, and may contain a C++ scope specifier. |
| 1265 | /// |
| 1266 | /// This routine is a convenience routine meant to be called from |
| 1267 | /// contexts that receive a name and an optional C++ scope specifier |
| 1268 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 1269 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 1270 | /// respectively) on the given name and return those results. |
| 1271 | /// |
| 1272 | /// @param S The scope from which unqualified name lookup will |
| 1273 | /// begin. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | /// |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1275 | /// @param SS An optional C++ scope-specifier, e.g., "::N::M". |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1276 | /// |
| 1277 | /// @param Name The name of the entity that name lookup will |
| 1278 | /// search for. |
| 1279 | /// |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1280 | /// @param Loc If provided, the source location where we're performing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | /// name lookup. At present, this is only used to produce diagnostics when |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1282 | /// C library functions (like "malloc") are implicitly declared. |
| 1283 | /// |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1284 | /// @param EnteringContext Indicates whether we are going to enter the |
| 1285 | /// context of the scope-specifier SS (if present). |
| 1286 | /// |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1287 | /// @returns True if any decls were found (but possibly ambiguous) |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1288 | bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1289 | bool AllowBuiltinCreation, bool EnteringContext) { |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1290 | if (SS && SS->isInvalid()) { |
| 1291 | // When the scope specifier is invalid, don't even look for |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1292 | // anything. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1293 | return false; |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1296 | if (SS && SS->isSet()) { |
| 1297 | if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | // We have resolved the scope specifier to a particular declaration |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1299 | // contex, and will perform name lookup in that context. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1300 | if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1301 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1303 | R.setContextRange(SS->getRange()); |
| 1304 | |
| 1305 | return LookupQualifiedName(R, DC); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1306 | } |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1308 | // We could not resolve the scope specified to a specific declaration |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | // context, which means that SS refers to an unknown specialization. |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1310 | // Name lookup can't find anything in this case. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1311 | return false; |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | // Perform unqualified name lookup starting in the given scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1315 | return LookupName(R, S, AllowBuiltinCreation); |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1318 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1319 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 1320 | /// from name lookup. |
| 1321 | /// |
| 1322 | /// @param Result The ambiguous name lookup result. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | /// |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1324 | /// @param Name The name of the entity that name lookup was |
| 1325 | /// searching for. |
| 1326 | /// |
| 1327 | /// @param NameLoc The location of the name within the source code. |
| 1328 | /// |
| 1329 | /// @param LookupRange A source range that provides more |
| 1330 | /// source-location information concerning the lookup itself. For |
| 1331 | /// example, this range might highlight a nested-name-specifier that |
| 1332 | /// precedes the name. |
| 1333 | /// |
| 1334 | /// @returns true |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1335 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1336 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 1337 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1338 | DeclarationName Name = Result.getLookupName(); |
| 1339 | SourceLocation NameLoc = Result.getNameLoc(); |
| 1340 | SourceRange LookupRange = Result.getContextRange(); |
| 1341 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1342 | switch (Result.getAmbiguityKind()) { |
| 1343 | case LookupResult::AmbiguousBaseSubobjects: { |
| 1344 | CXXBasePaths *Paths = Result.getBasePaths(); |
| 1345 | QualType SubobjectType = Paths->front().back().Base->getType(); |
| 1346 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 1347 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 1348 | << LookupRange; |
| 1349 | |
| 1350 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
| 1351 | while (isa<CXXMethodDecl>(*Found) && |
| 1352 | cast<CXXMethodDecl>(*Found)->isStatic()) |
| 1353 | ++Found; |
| 1354 | |
| 1355 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
| 1356 | |
| 1357 | return true; |
| 1358 | } |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1359 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1360 | case LookupResult::AmbiguousBaseSubobjectTypes: { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1361 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 1362 | << Name << LookupRange; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1363 | |
| 1364 | CXXBasePaths *Paths = Result.getBasePaths(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1365 | std::set<Decl *> DeclsPrinted; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1366 | for (CXXBasePaths::paths_iterator Path = Paths->begin(), |
| 1367 | PathEnd = Paths->end(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1368 | Path != PathEnd; ++Path) { |
| 1369 | Decl *D = *Path->Decls.first; |
| 1370 | if (DeclsPrinted.insert(D).second) |
| 1371 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 1372 | } |
| 1373 | |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1374 | return true; |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1377 | case LookupResult::AmbiguousTagHiding: { |
| 1378 | Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; |
Douglas Gregor | 69d993a | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1379 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1380 | llvm::SmallPtrSet<NamedDecl*,8> TagDecls; |
| 1381 | |
| 1382 | LookupResult::iterator DI, DE = Result.end(); |
| 1383 | for (DI = Result.begin(); DI != DE; ++DI) |
| 1384 | if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) { |
| 1385 | TagDecls.insert(TD); |
| 1386 | Diag(TD->getLocation(), diag::note_hidden_tag); |
| 1387 | } |
| 1388 | |
| 1389 | for (DI = Result.begin(); DI != DE; ++DI) |
| 1390 | if (!isa<TagDecl>(*DI)) |
| 1391 | Diag((*DI)->getLocation(), diag::note_hiding_object); |
| 1392 | |
| 1393 | // For recovery purposes, go ahead and implement the hiding. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1394 | LookupResult::Filter F = Result.makeFilter(); |
| 1395 | while (F.hasNext()) { |
| 1396 | if (TagDecls.count(F.next())) |
| 1397 | F.erase(); |
| 1398 | } |
| 1399 | F.done(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1400 | |
| 1401 | return true; |
| 1402 | } |
| 1403 | |
| 1404 | case LookupResult::AmbiguousReference: { |
| 1405 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1406 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1407 | LookupResult::iterator DI = Result.begin(), DE = Result.end(); |
| 1408 | for (; DI != DE; ++DI) |
| 1409 | Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1410 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1411 | return true; |
| 1412 | } |
| 1413 | } |
| 1414 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1415 | llvm_unreachable("unknown ambiguity kind"); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1416 | return true; |
| 1417 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1418 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1419 | namespace { |
| 1420 | struct AssociatedLookup { |
| 1421 | AssociatedLookup(Sema &S, |
| 1422 | Sema::AssociatedNamespaceSet &Namespaces, |
| 1423 | Sema::AssociatedClassSet &Classes) |
| 1424 | : S(S), Namespaces(Namespaces), Classes(Classes) { |
| 1425 | } |
| 1426 | |
| 1427 | Sema &S; |
| 1428 | Sema::AssociatedNamespaceSet &Namespaces; |
| 1429 | Sema::AssociatedClassSet &Classes; |
| 1430 | }; |
| 1431 | } |
| 1432 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1434 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1435 | |
Douglas Gregor | 5402295 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 1436 | static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, |
| 1437 | DeclContext *Ctx) { |
| 1438 | // Add the associated namespace for this class. |
| 1439 | |
| 1440 | // We don't use DeclContext::getEnclosingNamespaceContext() as this may |
| 1441 | // be a locally scoped record. |
| 1442 | |
| 1443 | while (Ctx->isRecord() || Ctx->isTransparentContext()) |
| 1444 | Ctx = Ctx->getParent(); |
| 1445 | |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1446 | if (Ctx->isFileContext()) |
Douglas Gregor | 5402295 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 1447 | Namespaces.insert(Ctx->getPrimaryContext()); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1448 | } |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1449 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | // \brief Add the associated classes and namespaces for argument-dependent |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1451 | // lookup that involves a template argument (C++ [basic.lookup.koenig]p2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1453 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
| 1454 | const TemplateArgument &Arg) { |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1455 | // C++ [basic.lookup.koenig]p2, last bullet: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | // -- [...] ; |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1457 | switch (Arg.getKind()) { |
| 1458 | case TemplateArgument::Null: |
| 1459 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1461 | case TemplateArgument::Type: |
| 1462 | // [...] the namespaces and classes associated with the types of the |
| 1463 | // template arguments provided for template type parameters (excluding |
| 1464 | // template template parameters) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1465 | addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1466 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1468 | case TemplateArgument::Template: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | // [...] the namespaces in which any template template arguments are |
| 1470 | // defined; and the classes in which any member templates used as |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1471 | // template template arguments are defined. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1472 | TemplateName Template = Arg.getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1474 | = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1475 | DeclContext *Ctx = ClassTemplate->getDeclContext(); |
| 1476 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1477 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1478 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1479 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1480 | } |
| 1481 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | case TemplateArgument::Declaration: |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1485 | case TemplateArgument::Integral: |
| 1486 | case TemplateArgument::Expression: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | // [Note: non-type template arguments do not contribute to the set of |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1488 | // associated namespaces. ] |
| 1489 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1491 | case TemplateArgument::Pack: |
| 1492 | for (TemplateArgument::pack_iterator P = Arg.pack_begin(), |
| 1493 | PEnd = Arg.pack_end(); |
| 1494 | P != PEnd; ++P) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1495 | addAssociatedClassesAndNamespaces(Result, *P); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | } |
| 1499 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1500 | // \brief Add the associated classes and namespaces for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | // argument-dependent lookup with an argument of class type |
| 1502 | // (C++ [basic.lookup.koenig]p2). |
| 1503 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1504 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
| 1505 | CXXRecordDecl *Class) { |
| 1506 | |
| 1507 | // Just silently ignore anything whose name is __va_list_tag. |
| 1508 | if (Class->getDeclName() == Result.S.VAListTagName) |
| 1509 | return; |
| 1510 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1511 | // C++ [basic.lookup.koenig]p2: |
| 1512 | // [...] |
| 1513 | // -- If T is a class type (including unions), its associated |
| 1514 | // classes are: the class itself; the class of which it is a |
| 1515 | // member, if any; and its direct and indirect base |
| 1516 | // classes. Its associated namespaces are the namespaces in |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | // which its associated classes are defined. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Add the class of which it is a member, if any. |
| 1520 | DeclContext *Ctx = Class->getDeclContext(); |
| 1521 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1522 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1523 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1524 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1526 | // Add the class itself. If we've already seen this class, we don't |
| 1527 | // need to visit base classes. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1528 | if (!Result.Classes.insert(Class)) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1529 | return; |
| 1530 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | // -- If T is a template-id, its associated namespaces and classes are |
| 1532 | // the namespace in which the template is defined; for member |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1533 | // templates, the member template’s class; the namespaces and classes |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | // associated with the types of the template arguments provided for |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1535 | // template type parameters (excluding template template parameters); the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | // namespaces in which any template template arguments are defined; and |
| 1537 | // the classes in which any member templates used as template template |
| 1538 | // arguments are defined. [Note: non-type template arguments do not |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1539 | // contribute to the set of associated namespaces. ] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1541 | = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { |
| 1542 | DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); |
| 1543 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1544 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1545 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1546 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1548 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 1549 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1550 | addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1551 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1553 | // Only recurse into base classes for complete types. |
| 1554 | if (!Class->hasDefinition()) { |
| 1555 | // FIXME: we might need to instantiate templates here |
| 1556 | return; |
| 1557 | } |
| 1558 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1559 | // Add direct and indirect base classes along with their associated |
| 1560 | // namespaces. |
| 1561 | llvm::SmallVector<CXXRecordDecl *, 32> Bases; |
| 1562 | Bases.push_back(Class); |
| 1563 | while (!Bases.empty()) { |
| 1564 | // Pop this class off the stack. |
| 1565 | Class = Bases.back(); |
| 1566 | Bases.pop_back(); |
| 1567 | |
| 1568 | // Visit the base classes. |
| 1569 | for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), |
| 1570 | BaseEnd = Class->bases_end(); |
| 1571 | Base != BaseEnd; ++Base) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1572 | const RecordType *BaseType = Base->getType()->getAs<RecordType>(); |
Sebastian Redl | bbc1cc5 | 2009-10-25 09:35:33 +0000 | [diff] [blame] | 1573 | // In dependent contexts, we do ADL twice, and the first time around, |
| 1574 | // the base type might be a dependent TemplateSpecializationType, or a |
| 1575 | // TemplateTypeParmType. If that happens, simply ignore it. |
| 1576 | // FIXME: If we want to support export, we probably need to add the |
| 1577 | // namespace of the template in a TemplateSpecializationType, or even |
| 1578 | // the classes and namespaces of known non-dependent arguments. |
| 1579 | if (!BaseType) |
| 1580 | continue; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1581 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1582 | if (Result.Classes.insert(BaseDecl)) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1583 | // Find the associated namespace for this base class. |
| 1584 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1585 | CollectEnclosingNamespace(Result.Namespaces, BaseCtx); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1586 | |
| 1587 | // Make sure we visit the bases of this base class. |
| 1588 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
| 1589 | Bases.push_back(BaseDecl); |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | // \brief Add the associated classes and namespaces for |
| 1596 | // argument-dependent lookup with an argument of type T |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | // (C++ [basic.lookup.koenig]p2). |
| 1598 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1599 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1600 | // C++ [basic.lookup.koenig]p2: |
| 1601 | // |
| 1602 | // For each argument type T in the function call, there is a set |
| 1603 | // of zero or more associated namespaces and a set of zero or more |
| 1604 | // associated classes to be considered. The sets of namespaces and |
| 1605 | // classes is determined entirely by the types of the function |
| 1606 | // arguments (and the namespace of any template template |
| 1607 | // argument). Typedef names and using-declarations used to specify |
| 1608 | // the types do not contribute to this set. The sets of namespaces |
| 1609 | // and classes are determined in the following way: |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1610 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1611 | llvm::SmallVector<const Type *, 16> Queue; |
| 1612 | const Type *T = Ty->getCanonicalTypeInternal().getTypePtr(); |
| 1613 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1614 | while (true) { |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1615 | switch (T->getTypeClass()) { |
| 1616 | |
| 1617 | #define TYPE(Class, Base) |
| 1618 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1619 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1620 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1621 | #define ABSTRACT_TYPE(Class, Base) |
| 1622 | #include "clang/AST/TypeNodes.def" |
| 1623 | // T is canonical. We can also ignore dependent types because |
| 1624 | // we don't need to do ADL at the definition point, but if we |
| 1625 | // wanted to implement template export (or if we find some other |
| 1626 | // use for associated classes and namespaces...) this would be |
| 1627 | // wrong. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1628 | break; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1629 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1630 | // -- If T is a pointer to U or an array of U, its associated |
| 1631 | // namespaces and classes are those associated with U. |
| 1632 | case Type::Pointer: |
| 1633 | T = cast<PointerType>(T)->getPointeeType().getTypePtr(); |
| 1634 | continue; |
| 1635 | case Type::ConstantArray: |
| 1636 | case Type::IncompleteArray: |
| 1637 | case Type::VariableArray: |
| 1638 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 1639 | continue; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1640 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1641 | // -- If T is a fundamental type, its associated sets of |
| 1642 | // namespaces and classes are both empty. |
| 1643 | case Type::Builtin: |
| 1644 | break; |
| 1645 | |
| 1646 | // -- If T is a class type (including unions), its associated |
| 1647 | // classes are: the class itself; the class of which it is a |
| 1648 | // member, if any; and its direct and indirect base |
| 1649 | // classes. Its associated namespaces are the namespaces in |
| 1650 | // which its associated classes are defined. |
| 1651 | case Type::Record: { |
| 1652 | CXXRecordDecl *Class |
| 1653 | = cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()); |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1654 | addAssociatedClassesAndNamespaces(Result, Class); |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1655 | break; |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1656 | } |
Douglas Gregor | 4e58c25 | 2010-05-20 02:26:51 +0000 | [diff] [blame] | 1657 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1658 | // -- If T is an enumeration type, its associated namespace is |
| 1659 | // the namespace in which it is defined. If it is class |
| 1660 | // member, its associated class is the member’s class; else |
| 1661 | // it has no associated class. |
| 1662 | case Type::Enum: { |
| 1663 | EnumDecl *Enum = cast<EnumType>(T)->getDecl(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1664 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1665 | DeclContext *Ctx = Enum->getDeclContext(); |
| 1666 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1667 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1668 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1669 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1670 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1671 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1672 | break; |
| 1673 | } |
| 1674 | |
| 1675 | // -- If T is a function type, its associated namespaces and |
| 1676 | // classes are those associated with the function parameter |
| 1677 | // types and those associated with the return type. |
| 1678 | case Type::FunctionProto: { |
| 1679 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 1680 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
| 1681 | ArgEnd = Proto->arg_type_end(); |
| 1682 | Arg != ArgEnd; ++Arg) |
| 1683 | Queue.push_back(Arg->getTypePtr()); |
| 1684 | // fallthrough |
| 1685 | } |
| 1686 | case Type::FunctionNoProto: { |
| 1687 | const FunctionType *FnType = cast<FunctionType>(T); |
| 1688 | T = FnType->getResultType().getTypePtr(); |
| 1689 | continue; |
| 1690 | } |
| 1691 | |
| 1692 | // -- If T is a pointer to a member function of a class X, its |
| 1693 | // associated namespaces and classes are those associated |
| 1694 | // with the function parameter types and return type, |
| 1695 | // together with those associated with X. |
| 1696 | // |
| 1697 | // -- If T is a pointer to a data member of class X, its |
| 1698 | // associated namespaces and classes are those associated |
| 1699 | // with the member type together with those associated with |
| 1700 | // X. |
| 1701 | case Type::MemberPointer: { |
| 1702 | const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); |
| 1703 | |
| 1704 | // Queue up the class type into which this points. |
| 1705 | Queue.push_back(MemberPtr->getClass()); |
| 1706 | |
| 1707 | // And directly continue with the pointee type. |
| 1708 | T = MemberPtr->getPointeeType().getTypePtr(); |
| 1709 | continue; |
| 1710 | } |
| 1711 | |
| 1712 | // As an extension, treat this like a normal pointer. |
| 1713 | case Type::BlockPointer: |
| 1714 | T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr(); |
| 1715 | continue; |
| 1716 | |
| 1717 | // References aren't covered by the standard, but that's such an |
| 1718 | // obvious defect that we cover them anyway. |
| 1719 | case Type::LValueReference: |
| 1720 | case Type::RValueReference: |
| 1721 | T = cast<ReferenceType>(T)->getPointeeType().getTypePtr(); |
| 1722 | continue; |
| 1723 | |
| 1724 | // These are fundamental types. |
| 1725 | case Type::Vector: |
| 1726 | case Type::ExtVector: |
| 1727 | case Type::Complex: |
| 1728 | break; |
| 1729 | |
| 1730 | // These are ignored by ADL. |
| 1731 | case Type::ObjCObject: |
| 1732 | case Type::ObjCInterface: |
| 1733 | case Type::ObjCObjectPointer: |
| 1734 | break; |
| 1735 | } |
| 1736 | |
| 1737 | if (Queue.empty()) break; |
| 1738 | T = Queue.back(); |
| 1739 | Queue.pop_back(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1740 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /// \brief Find the associated classes and namespaces for |
| 1744 | /// argument-dependent lookup for a call with the given set of |
| 1745 | /// arguments. |
| 1746 | /// |
| 1747 | /// This routine computes the sets of associated classes and associated |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | /// namespaces searched by argument-dependent lookup |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1749 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1751 | Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, |
| 1752 | AssociatedNamespaceSet &AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1753 | AssociatedClassSet &AssociatedClasses) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1754 | AssociatedNamespaces.clear(); |
| 1755 | AssociatedClasses.clear(); |
| 1756 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1757 | AssociatedLookup Result(*this, AssociatedNamespaces, AssociatedClasses); |
| 1758 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1759 | // C++ [basic.lookup.koenig]p2: |
| 1760 | // For each argument type T in the function call, there is a set |
| 1761 | // of zero or more associated namespaces and a set of zero or more |
| 1762 | // associated classes to be considered. The sets of namespaces and |
| 1763 | // classes is determined entirely by the types of the function |
| 1764 | // arguments (and the namespace of any template template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | // argument). |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1766 | for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) { |
| 1767 | Expr *Arg = Args[ArgIdx]; |
| 1768 | |
| 1769 | if (Arg->getType() != Context.OverloadTy) { |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1770 | addAssociatedClassesAndNamespaces(Result, Arg->getType()); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1771 | continue; |
| 1772 | } |
| 1773 | |
| 1774 | // [...] In addition, if the argument is the name or address of a |
| 1775 | // set of overloaded functions and/or function templates, its |
| 1776 | // associated classes and namespaces are the union of those |
| 1777 | // associated with each of the members of the set: the namespace |
| 1778 | // in which the function or function template is defined and the |
| 1779 | // classes and namespaces associated with its (non-dependent) |
| 1780 | // parameter types and return type. |
Douglas Gregor | daa439a | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 1781 | Arg = Arg->IgnoreParens(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1782 | if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) |
| 1783 | if (unaryOp->getOpcode() == UnaryOperator::AddrOf) |
| 1784 | Arg = unaryOp->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1786 | UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg); |
| 1787 | if (!ULE) continue; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1788 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1789 | for (UnresolvedSetIterator I = ULE->decls_begin(), E = ULE->decls_end(); |
| 1790 | I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 1791 | // Look through any using declarations to find the underlying function. |
| 1792 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1793 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 1794 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(Fn); |
| 1795 | if (!FDecl) |
| 1796 | FDecl = cast<FunctionTemplateDecl>(Fn)->getTemplatedDecl(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1797 | |
| 1798 | // Add the classes and namespaces associated with the parameter |
| 1799 | // types and return type of this function. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1800 | addAssociatedClassesAndNamespaces(Result, FDecl->getType()); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1804 | |
| 1805 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 1806 | /// an acceptable non-member overloaded operator for a call whose |
| 1807 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 1808 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 1809 | /// enumeration types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | static bool |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1811 | IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, |
| 1812 | QualType T1, QualType T2, |
| 1813 | ASTContext &Context) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1814 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
| 1815 | return true; |
| 1816 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1817 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 1818 | return true; |
| 1819 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1820 | const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1821 | if (Proto->getNumArgs() < 1) |
| 1822 | return false; |
| 1823 | |
| 1824 | if (T1->isEnumeralType()) { |
| 1825 | QualType ArgType = Proto->getArgType(0).getNonReferenceType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1826 | if (Context.hasSameUnqualifiedType(T1, ArgType)) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1827 | return true; |
| 1828 | } |
| 1829 | |
| 1830 | if (Proto->getNumArgs() < 2) |
| 1831 | return false; |
| 1832 | |
| 1833 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 1834 | QualType ArgType = Proto->getArgType(1).getNonReferenceType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1835 | if (Context.hasSameUnqualifiedType(T2, ArgType)) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1836 | return true; |
| 1837 | } |
| 1838 | |
| 1839 | return false; |
| 1840 | } |
| 1841 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1842 | NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1843 | SourceLocation Loc, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1844 | LookupNameKind NameKind, |
| 1845 | RedeclarationKind Redecl) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1846 | LookupResult R(*this, Name, Loc, NameKind, Redecl); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1847 | LookupName(R, S); |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 1848 | return R.getAsSingle<NamedDecl>(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1851 | /// \brief Find the protocol with the given name, if any. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1852 | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, |
| 1853 | SourceLocation IdLoc) { |
| 1854 | Decl *D = LookupSingleName(TUScope, II, IdLoc, |
| 1855 | LookupObjCProtocolName); |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1856 | return cast_or_null<ObjCProtocolDecl>(D); |
| 1857 | } |
| 1858 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1859 | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | QualType T1, QualType T2, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 1861 | UnresolvedSetImpl &Functions) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1862 | // C++ [over.match.oper]p3: |
| 1863 | // -- The set of non-member candidates is the result of the |
| 1864 | // unqualified lookup of operator@ in the context of the |
| 1865 | // expression according to the usual rules for name lookup in |
| 1866 | // unqualified function calls (3.4.2) except that all member |
| 1867 | // functions are ignored. However, if no operand has a class |
| 1868 | // type, only those non-member functions in the lookup set |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1869 | // that have a first parameter of type T1 or "reference to |
| 1870 | // (possibly cv-qualified) T1", when T1 is an enumeration |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1871 | // type, or (if there is a right operand) a second parameter |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1872 | // of type T2 or "reference to (possibly cv-qualified) T2", |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1873 | // when T2 is an enumeration type, are candidate functions. |
| 1874 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1875 | LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); |
| 1876 | LookupName(Operators, S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1878 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
| 1879 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1880 | if (Operators.empty()) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1881 | return; |
| 1882 | |
| 1883 | for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end(); |
| 1884 | Op != OpEnd; ++Op) { |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 1885 | NamedDecl *Found = (*Op)->getUnderlyingDecl(); |
| 1886 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Found)) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1887 | if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 1888 | Functions.addDecl(*Op, Op.getAccess()); // FIXME: canonical FD |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | } else if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 1890 | = dyn_cast<FunctionTemplateDecl>(Found)) { |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 1891 | // FIXME: friend operators? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 1893 | // later? |
| 1894 | if (!FunTmpl->getDeclContext()->isRecord()) |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 1895 | Functions.addDecl(*Op, Op.getAccess()); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 1896 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 1900 | void ADLResult::insert(NamedDecl *New) { |
| 1901 | NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; |
| 1902 | |
| 1903 | // If we haven't yet seen a decl for this key, or the last decl |
| 1904 | // was exactly this one, we're done. |
| 1905 | if (Old == 0 || Old == New) { |
| 1906 | Old = New; |
| 1907 | return; |
| 1908 | } |
| 1909 | |
| 1910 | // Otherwise, decide which is a more recent redeclaration. |
| 1911 | FunctionDecl *OldFD, *NewFD; |
| 1912 | if (isa<FunctionTemplateDecl>(New)) { |
| 1913 | OldFD = cast<FunctionTemplateDecl>(Old)->getTemplatedDecl(); |
| 1914 | NewFD = cast<FunctionTemplateDecl>(New)->getTemplatedDecl(); |
| 1915 | } else { |
| 1916 | OldFD = cast<FunctionDecl>(Old); |
| 1917 | NewFD = cast<FunctionDecl>(New); |
| 1918 | } |
| 1919 | |
| 1920 | FunctionDecl *Cursor = NewFD; |
| 1921 | while (true) { |
| 1922 | Cursor = Cursor->getPreviousDeclaration(); |
| 1923 | |
| 1924 | // If we got to the end without finding OldFD, OldFD is the newer |
| 1925 | // declaration; leave things as they are. |
| 1926 | if (!Cursor) return; |
| 1927 | |
| 1928 | // If we do find OldFD, then NewFD is newer. |
| 1929 | if (Cursor == OldFD) break; |
| 1930 | |
| 1931 | // Otherwise, keep looking. |
| 1932 | } |
| 1933 | |
| 1934 | Old = New; |
| 1935 | } |
| 1936 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 1937 | void Sema::ArgumentDependentLookup(DeclarationName Name, bool Operator, |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1938 | Expr **Args, unsigned NumArgs, |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 1939 | ADLResult &Result) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1940 | // Find all of the associated namespaces and classes based on the |
| 1941 | // arguments we have. |
| 1942 | AssociatedNamespaceSet AssociatedNamespaces; |
| 1943 | AssociatedClassSet AssociatedClasses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | FindAssociatedClassesAndNamespaces(Args, NumArgs, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1945 | AssociatedNamespaces, |
| 1946 | AssociatedClasses); |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1947 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 1948 | QualType T1, T2; |
| 1949 | if (Operator) { |
| 1950 | T1 = Args[0]->getType(); |
| 1951 | if (NumArgs >= 2) |
| 1952 | T2 = Args[1]->getType(); |
| 1953 | } |
| 1954 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1955 | // C++ [basic.lookup.argdep]p3: |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1956 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 1957 | // and let Y be the lookup set produced by argument dependent |
| 1958 | // lookup (defined as follows). If X contains [...] then Y is |
| 1959 | // empty. Otherwise Y is the set of declarations found in the |
| 1960 | // namespaces associated with the argument types as described |
| 1961 | // below. The set of declarations found by the lookup of the name |
| 1962 | // is the union of X and Y. |
| 1963 | // |
| 1964 | // Here, we compute Y and add its members to the overloaded |
| 1965 | // candidate set. |
| 1966 | for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | NSEnd = AssociatedNamespaces.end(); |
| 1968 | NS != NSEnd; ++NS) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1969 | // When considering an associated namespace, the lookup is the |
| 1970 | // same as the lookup performed when the associated namespace is |
| 1971 | // used as a qualifier (3.4.3.2) except that: |
| 1972 | // |
| 1973 | // -- Any using-directives in the associated namespace are |
| 1974 | // ignored. |
| 1975 | // |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1976 | // -- Any namespace-scope friend functions declared in |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1977 | // associated classes are visible within their respective |
| 1978 | // namespaces even if they are not visible during an ordinary |
| 1979 | // lookup (11.4). |
| 1980 | DeclContext::lookup_iterator I, E; |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1981 | for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 1982 | NamedDecl *D = *I; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1983 | // If the only declaration here is an ordinary friend, consider |
| 1984 | // it only if it was declared in an associated classes. |
| 1985 | if (D->getIdentifierNamespace() == Decl::IDNS_OrdinaryFriend) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1986 | DeclContext *LexDC = D->getLexicalDeclContext(); |
| 1987 | if (!AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) |
| 1988 | continue; |
| 1989 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 1991 | if (isa<UsingShadowDecl>(D)) |
| 1992 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 1993 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 1994 | if (isa<FunctionDecl>(D)) { |
| 1995 | if (Operator && |
| 1996 | !IsAcceptableNonMemberOperatorCandidate(cast<FunctionDecl>(D), |
| 1997 | T1, T2, Context)) |
| 1998 | continue; |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 1999 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2000 | continue; |
| 2001 | |
| 2002 | Result.insert(D); |
Douglas Gregor | 44bc2d5 | 2009-06-23 20:14:09 +0000 | [diff] [blame] | 2003 | } |
| 2004 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2005 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2006 | |
| 2007 | //---------------------------------------------------------------------------- |
| 2008 | // Search for all visible declarations. |
| 2009 | //---------------------------------------------------------------------------- |
| 2010 | VisibleDeclConsumer::~VisibleDeclConsumer() { } |
| 2011 | |
| 2012 | namespace { |
| 2013 | |
| 2014 | class ShadowContextRAII; |
| 2015 | |
| 2016 | class VisibleDeclsRecord { |
| 2017 | public: |
| 2018 | /// \brief An entry in the shadow map, which is optimized to store a |
| 2019 | /// single declaration (the common case) but can also store a list |
| 2020 | /// of declarations. |
| 2021 | class ShadowMapEntry { |
| 2022 | typedef llvm::SmallVector<NamedDecl *, 4> DeclVector; |
| 2023 | |
| 2024 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 2025 | /// of declarations. |
| 2026 | llvm::PointerUnion<NamedDecl *, DeclVector*> DeclOrVector; |
| 2027 | |
| 2028 | public: |
| 2029 | ShadowMapEntry() : DeclOrVector() { } |
| 2030 | |
| 2031 | void Add(NamedDecl *ND); |
| 2032 | void Destroy(); |
| 2033 | |
| 2034 | // Iteration. |
| 2035 | typedef NamedDecl **iterator; |
| 2036 | iterator begin(); |
| 2037 | iterator end(); |
| 2038 | }; |
| 2039 | |
| 2040 | private: |
| 2041 | /// \brief A mapping from declaration names to the declarations that have |
| 2042 | /// this name within a particular scope. |
| 2043 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
| 2044 | |
| 2045 | /// \brief A list of shadow maps, which is used to model name hiding. |
| 2046 | std::list<ShadowMap> ShadowMaps; |
| 2047 | |
| 2048 | /// \brief The declaration contexts we have already visited. |
| 2049 | llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; |
| 2050 | |
| 2051 | friend class ShadowContextRAII; |
| 2052 | |
| 2053 | public: |
| 2054 | /// \brief Determine whether we have already visited this context |
| 2055 | /// (and, if not, note that we are going to visit that context now). |
| 2056 | bool visitedContext(DeclContext *Ctx) { |
| 2057 | return !VisitedContexts.insert(Ctx); |
| 2058 | } |
| 2059 | |
| 2060 | /// \brief Determine whether the given declaration is hidden in the |
| 2061 | /// current scope. |
| 2062 | /// |
| 2063 | /// \returns the declaration that hides the given declaration, or |
| 2064 | /// NULL if no such declaration exists. |
| 2065 | NamedDecl *checkHidden(NamedDecl *ND); |
| 2066 | |
| 2067 | /// \brief Add a declaration to the current shadow map. |
| 2068 | void add(NamedDecl *ND) { ShadowMaps.back()[ND->getDeclName()].Add(ND); } |
| 2069 | }; |
| 2070 | |
| 2071 | /// \brief RAII object that records when we've entered a shadow context. |
| 2072 | class ShadowContextRAII { |
| 2073 | VisibleDeclsRecord &Visible; |
| 2074 | |
| 2075 | typedef VisibleDeclsRecord::ShadowMap ShadowMap; |
| 2076 | |
| 2077 | public: |
| 2078 | ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { |
| 2079 | Visible.ShadowMaps.push_back(ShadowMap()); |
| 2080 | } |
| 2081 | |
| 2082 | ~ShadowContextRAII() { |
| 2083 | for (ShadowMap::iterator E = Visible.ShadowMaps.back().begin(), |
| 2084 | EEnd = Visible.ShadowMaps.back().end(); |
| 2085 | E != EEnd; |
| 2086 | ++E) |
| 2087 | E->second.Destroy(); |
| 2088 | |
| 2089 | Visible.ShadowMaps.pop_back(); |
| 2090 | } |
| 2091 | }; |
| 2092 | |
| 2093 | } // end anonymous namespace |
| 2094 | |
| 2095 | void VisibleDeclsRecord::ShadowMapEntry::Add(NamedDecl *ND) { |
| 2096 | if (DeclOrVector.isNull()) { |
| 2097 | // 0 - > 1 elements: just set the single element information. |
| 2098 | DeclOrVector = ND; |
| 2099 | return; |
| 2100 | } |
| 2101 | |
| 2102 | if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { |
| 2103 | // 1 -> 2 elements: create the vector of results and push in the |
| 2104 | // existing declaration. |
| 2105 | DeclVector *Vec = new DeclVector; |
| 2106 | Vec->push_back(PrevND); |
| 2107 | DeclOrVector = Vec; |
| 2108 | } |
| 2109 | |
| 2110 | // Add the new element to the end of the vector. |
| 2111 | DeclOrVector.get<DeclVector*>()->push_back(ND); |
| 2112 | } |
| 2113 | |
| 2114 | void VisibleDeclsRecord::ShadowMapEntry::Destroy() { |
| 2115 | if (DeclVector *Vec = DeclOrVector.dyn_cast<DeclVector *>()) { |
| 2116 | delete Vec; |
| 2117 | DeclOrVector = ((NamedDecl *)0); |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | VisibleDeclsRecord::ShadowMapEntry::iterator |
| 2122 | VisibleDeclsRecord::ShadowMapEntry::begin() { |
| 2123 | if (DeclOrVector.isNull()) |
| 2124 | return 0; |
| 2125 | |
| 2126 | if (DeclOrVector.dyn_cast<NamedDecl *>()) |
| 2127 | return &reinterpret_cast<NamedDecl*&>(DeclOrVector); |
| 2128 | |
| 2129 | return DeclOrVector.get<DeclVector *>()->begin(); |
| 2130 | } |
| 2131 | |
| 2132 | VisibleDeclsRecord::ShadowMapEntry::iterator |
| 2133 | VisibleDeclsRecord::ShadowMapEntry::end() { |
| 2134 | if (DeclOrVector.isNull()) |
| 2135 | return 0; |
| 2136 | |
| 2137 | if (DeclOrVector.dyn_cast<NamedDecl *>()) |
| 2138 | return &reinterpret_cast<NamedDecl*&>(DeclOrVector) + 1; |
| 2139 | |
| 2140 | return DeclOrVector.get<DeclVector *>()->end(); |
| 2141 | } |
| 2142 | |
| 2143 | NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { |
Douglas Gregor | efcf16d | 2010-01-14 00:06:47 +0000 | [diff] [blame] | 2144 | // Look through using declarations. |
| 2145 | ND = ND->getUnderlyingDecl(); |
| 2146 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2147 | unsigned IDNS = ND->getIdentifierNamespace(); |
| 2148 | std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); |
| 2149 | for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); |
| 2150 | SM != SMEnd; ++SM) { |
| 2151 | ShadowMap::iterator Pos = SM->find(ND->getDeclName()); |
| 2152 | if (Pos == SM->end()) |
| 2153 | continue; |
| 2154 | |
| 2155 | for (ShadowMapEntry::iterator I = Pos->second.begin(), |
| 2156 | IEnd = Pos->second.end(); |
| 2157 | I != IEnd; ++I) { |
| 2158 | // A tag declaration does not hide a non-tag declaration. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2159 | if ((*I)->hasTagIdentifierNamespace() && |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2160 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 2161 | Decl::IDNS_ObjCProtocol))) |
| 2162 | continue; |
| 2163 | |
| 2164 | // Protocols are in distinct namespaces from everything else. |
| 2165 | if ((((*I)->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
| 2166 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
| 2167 | (*I)->getIdentifierNamespace() != IDNS) |
| 2168 | continue; |
| 2169 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2170 | // Functions and function templates in the same scope overload |
| 2171 | // rather than hide. FIXME: Look for hiding based on function |
| 2172 | // signatures! |
Douglas Gregor | def9107 | 2010-01-14 03:35:48 +0000 | [diff] [blame] | 2173 | if ((*I)->isFunctionOrFunctionTemplate() && |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2174 | ND->isFunctionOrFunctionTemplate() && |
| 2175 | SM == ShadowMaps.rbegin()) |
Douglas Gregor | def9107 | 2010-01-14 03:35:48 +0000 | [diff] [blame] | 2176 | continue; |
| 2177 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2178 | // We've found a declaration that hides this one. |
| 2179 | return *I; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | static void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result, |
| 2187 | bool QualifiedNameLookup, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2188 | bool InBaseClass, |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2189 | VisibleDeclConsumer &Consumer, |
| 2190 | VisibleDeclsRecord &Visited) { |
Douglas Gregor | 6202119 | 2010-02-04 23:42:48 +0000 | [diff] [blame] | 2191 | if (!Ctx) |
| 2192 | return; |
| 2193 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2194 | // Make sure we don't visit the same context twice. |
| 2195 | if (Visited.visitedContext(Ctx->getPrimaryContext())) |
| 2196 | return; |
| 2197 | |
| 2198 | // Enumerate all of the results in this context. |
| 2199 | for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx; |
| 2200 | CurCtx = CurCtx->getNextContext()) { |
| 2201 | for (DeclContext::decl_iterator D = CurCtx->decls_begin(), |
| 2202 | DEnd = CurCtx->decls_end(); |
| 2203 | D != DEnd; ++D) { |
| 2204 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
| 2205 | if (Result.isAcceptableDecl(ND)) { |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2206 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), InBaseClass); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2207 | Visited.add(ND); |
| 2208 | } |
| 2209 | |
| 2210 | // Visit transparent contexts inside this context. |
| 2211 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) { |
| 2212 | if (InnerCtx->isTransparentContext()) |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2213 | LookupVisibleDecls(InnerCtx, Result, QualifiedNameLookup, InBaseClass, |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2214 | Consumer, Visited); |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | // Traverse using directives for qualified name lookup. |
| 2220 | if (QualifiedNameLookup) { |
| 2221 | ShadowContextRAII Shadow(Visited); |
| 2222 | DeclContext::udir_iterator I, E; |
| 2223 | for (llvm::tie(I, E) = Ctx->getUsingDirectives(); I != E; ++I) { |
| 2224 | LookupVisibleDecls((*I)->getNominatedNamespace(), Result, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2225 | QualifiedNameLookup, InBaseClass, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2229 | // Traverse the contexts of inherited C++ classes. |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2230 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2231 | if (!Record->hasDefinition()) |
| 2232 | return; |
| 2233 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2234 | for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), |
| 2235 | BEnd = Record->bases_end(); |
| 2236 | B != BEnd; ++B) { |
| 2237 | QualType BaseType = B->getType(); |
| 2238 | |
| 2239 | // Don't look into dependent bases, because name lookup can't look |
| 2240 | // there anyway. |
| 2241 | if (BaseType->isDependentType()) |
| 2242 | continue; |
| 2243 | |
| 2244 | const RecordType *Record = BaseType->getAs<RecordType>(); |
| 2245 | if (!Record) |
| 2246 | continue; |
| 2247 | |
| 2248 | // FIXME: It would be nice to be able to determine whether referencing |
| 2249 | // a particular member would be ambiguous. For example, given |
| 2250 | // |
| 2251 | // struct A { int member; }; |
| 2252 | // struct B { int member; }; |
| 2253 | // struct C : A, B { }; |
| 2254 | // |
| 2255 | // void f(C *c) { c->### } |
| 2256 | // |
| 2257 | // accessing 'member' would result in an ambiguity. However, we |
| 2258 | // could be smart enough to qualify the member with the base |
| 2259 | // class, e.g., |
| 2260 | // |
| 2261 | // c->B::member |
| 2262 | // |
| 2263 | // or |
| 2264 | // |
| 2265 | // c->A::member |
| 2266 | |
| 2267 | // Find results in this base class (and its bases). |
| 2268 | ShadowContextRAII Shadow(Visited); |
| 2269 | LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2270 | true, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2274 | // Traverse the contexts of Objective-C classes. |
| 2275 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { |
| 2276 | // Traverse categories. |
| 2277 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 2278 | Category; Category = Category->getNextClassCategory()) { |
| 2279 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2280 | LookupVisibleDecls(Category, Result, QualifiedNameLookup, false, |
| 2281 | Consumer, Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | // Traverse protocols. |
| 2285 | for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(), |
| 2286 | E = IFace->protocol_end(); I != E; ++I) { |
| 2287 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2288 | LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, |
| 2289 | Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | // Traverse the superclass. |
| 2293 | if (IFace->getSuperClass()) { |
| 2294 | ShadowContextRAII Shadow(Visited); |
| 2295 | LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2296 | true, Consumer, Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2297 | } |
Douglas Gregor | c220a18 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 2298 | |
| 2299 | // If there is an implementation, traverse it. We do this to find |
| 2300 | // synthesized ivars. |
| 2301 | if (IFace->getImplementation()) { |
| 2302 | ShadowContextRAII Shadow(Visited); |
| 2303 | LookupVisibleDecls(IFace->getImplementation(), Result, |
| 2304 | QualifiedNameLookup, true, Consumer, Visited); |
| 2305 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2306 | } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { |
| 2307 | for (ObjCProtocolDecl::protocol_iterator I = Protocol->protocol_begin(), |
| 2308 | E = Protocol->protocol_end(); I != E; ++I) { |
| 2309 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2310 | LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, |
| 2311 | Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { |
| 2314 | for (ObjCCategoryDecl::protocol_iterator I = Category->protocol_begin(), |
| 2315 | E = Category->protocol_end(); I != E; ++I) { |
| 2316 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2317 | LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, |
| 2318 | Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2319 | } |
Douglas Gregor | c220a18 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 2320 | |
| 2321 | // If there is an implementation, traverse it. |
| 2322 | if (Category->getImplementation()) { |
| 2323 | ShadowContextRAII Shadow(Visited); |
| 2324 | LookupVisibleDecls(Category->getImplementation(), Result, |
| 2325 | QualifiedNameLookup, true, Consumer, Visited); |
| 2326 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2327 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | static void LookupVisibleDecls(Scope *S, LookupResult &Result, |
| 2331 | UnqualUsingDirectiveSet &UDirs, |
| 2332 | VisibleDeclConsumer &Consumer, |
| 2333 | VisibleDeclsRecord &Visited) { |
| 2334 | if (!S) |
| 2335 | return; |
| 2336 | |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2337 | if (!S->getEntity() || !S->getParent() || |
| 2338 | ((DeclContext *)S->getEntity())->isFunctionOrMethod()) { |
| 2339 | // Walk through the declarations in this Scope. |
| 2340 | for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 2341 | D != DEnd; ++D) { |
| 2342 | if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get()))) |
| 2343 | if (Result.isAcceptableDecl(ND)) { |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2344 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), false); |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2345 | Visited.add(ND); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 2350 | // FIXME: C++ [temp.local]p8 |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2351 | DeclContext *Entity = 0; |
Douglas Gregor | e358201 | 2010-01-01 17:44:25 +0000 | [diff] [blame] | 2352 | if (S->getEntity()) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2353 | // Look into this scope's declaration context, along with any of its |
| 2354 | // parent lookup contexts (e.g., enclosing classes), up to the point |
| 2355 | // where we hit the context stored in the next outer scope. |
| 2356 | Entity = (DeclContext *)S->getEntity(); |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 2357 | DeclContext *OuterCtx = findOuterContext(S).first; // FIXME |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2358 | |
Douglas Gregor | dbdf5e7 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 2359 | for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2360 | Ctx = Ctx->getLookupParent()) { |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2361 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
| 2362 | if (Method->isInstanceMethod()) { |
| 2363 | // For instance methods, look for ivars in the method's interface. |
| 2364 | LookupResult IvarResult(Result.getSema(), Result.getLookupName(), |
| 2365 | Result.getNameLoc(), Sema::LookupMemberName); |
Douglas Gregor | 6202119 | 2010-02-04 23:42:48 +0000 | [diff] [blame] | 2366 | if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) |
| 2367 | LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false, |
| 2368 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | // We've already performed all of the name lookup that we need |
| 2372 | // to for Objective-C methods; the next context will be the |
| 2373 | // outer scope. |
| 2374 | break; |
| 2375 | } |
| 2376 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2377 | if (Ctx->isFunctionOrMethod()) |
| 2378 | continue; |
| 2379 | |
| 2380 | LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2381 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } else if (!S->getParent()) { |
| 2384 | // Look into the translation unit scope. We walk through the translation |
| 2385 | // unit's declaration context, because the Scope itself won't have all of |
| 2386 | // the declarations if we loaded a precompiled header. |
| 2387 | // FIXME: We would like the translation unit's Scope object to point to the |
| 2388 | // translation unit, so we don't need this special "if" branch. However, |
| 2389 | // doing so would force the normal C++ name-lookup code to look into the |
| 2390 | // translation unit decl when the IdentifierInfo chains would suffice. |
| 2391 | // 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] | 2392 | // in DeclContexts unless we have to" optimization), we can eliminate this. |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2393 | Entity = Result.getSema().Context.getTranslationUnitDecl(); |
| 2394 | LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2395 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2396 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2397 | |
| 2398 | if (Entity) { |
| 2399 | // Lookup visible declarations in any namespaces found by using |
| 2400 | // directives. |
| 2401 | UnqualUsingDirectiveSet::const_iterator UI, UEnd; |
| 2402 | llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(Entity); |
| 2403 | for (; UI != UEnd; ++UI) |
| 2404 | LookupVisibleDecls(const_cast<DeclContext *>(UI->getNominatedNamespace()), |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2405 | Result, /*QualifiedNameLookup=*/false, |
| 2406 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | // Lookup names in the parent scope. |
| 2410 | ShadowContextRAII Shadow(Visited); |
| 2411 | LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited); |
| 2412 | } |
| 2413 | |
| 2414 | void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, |
| 2415 | VisibleDeclConsumer &Consumer) { |
| 2416 | // Determine the set of using directives available during |
| 2417 | // unqualified name lookup. |
| 2418 | Scope *Initial = S; |
| 2419 | UnqualUsingDirectiveSet UDirs; |
| 2420 | if (getLangOptions().CPlusPlus) { |
| 2421 | // Find the first namespace or translation-unit scope. |
| 2422 | while (S && !isNamespaceOrTranslationUnitScope(S)) |
| 2423 | S = S->getParent(); |
| 2424 | |
| 2425 | UDirs.visitScopeChain(Initial, S); |
| 2426 | } |
| 2427 | UDirs.done(); |
| 2428 | |
| 2429 | // Look for visible declarations. |
| 2430 | LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); |
| 2431 | VisibleDeclsRecord Visited; |
| 2432 | ShadowContextRAII Shadow(Visited); |
| 2433 | ::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited); |
| 2434 | } |
| 2435 | |
| 2436 | void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, |
| 2437 | VisibleDeclConsumer &Consumer) { |
| 2438 | LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); |
| 2439 | VisibleDeclsRecord Visited; |
| 2440 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2441 | ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true, |
| 2442 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | //---------------------------------------------------------------------------- |
| 2446 | // Typo correction |
| 2447 | //---------------------------------------------------------------------------- |
| 2448 | |
| 2449 | namespace { |
| 2450 | class TypoCorrectionConsumer : public VisibleDeclConsumer { |
| 2451 | /// \brief The name written that is a typo in the source. |
| 2452 | llvm::StringRef Typo; |
| 2453 | |
| 2454 | /// \brief The results found that have the smallest edit distance |
| 2455 | /// found (so far) with the typo name. |
| 2456 | llvm::SmallVector<NamedDecl *, 4> BestResults; |
| 2457 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2458 | /// \brief The keywords that have the smallest edit distance. |
| 2459 | llvm::SmallVector<IdentifierInfo *, 4> BestKeywords; |
| 2460 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2461 | /// \brief The best edit distance found so far. |
| 2462 | unsigned BestEditDistance; |
| 2463 | |
| 2464 | public: |
| 2465 | explicit TypoCorrectionConsumer(IdentifierInfo *Typo) |
| 2466 | : Typo(Typo->getName()) { } |
| 2467 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2468 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2469 | void addKeywordResult(ASTContext &Context, llvm::StringRef Keyword); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2470 | |
| 2471 | typedef llvm::SmallVector<NamedDecl *, 4>::const_iterator iterator; |
| 2472 | iterator begin() const { return BestResults.begin(); } |
| 2473 | iterator end() const { return BestResults.end(); } |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2474 | void clear_decls() { BestResults.clear(); } |
| 2475 | |
| 2476 | bool empty() const { return BestResults.empty() && BestKeywords.empty(); } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2477 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2478 | typedef llvm::SmallVector<IdentifierInfo *, 4>::const_iterator |
| 2479 | keyword_iterator; |
| 2480 | keyword_iterator keyword_begin() const { return BestKeywords.begin(); } |
| 2481 | keyword_iterator keyword_end() const { return BestKeywords.end(); } |
| 2482 | bool keyword_empty() const { return BestKeywords.empty(); } |
| 2483 | unsigned keyword_size() const { return BestKeywords.size(); } |
| 2484 | |
| 2485 | unsigned getBestEditDistance() const { return BestEditDistance; } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2486 | }; |
| 2487 | |
| 2488 | } |
| 2489 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2490 | void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, |
| 2491 | bool InBaseClass) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2492 | // Don't consider hidden names for typo correction. |
| 2493 | if (Hiding) |
| 2494 | return; |
| 2495 | |
| 2496 | // Only consider entities with identifiers for names, ignoring |
| 2497 | // special names (constructors, overloaded operators, selectors, |
| 2498 | // etc.). |
| 2499 | IdentifierInfo *Name = ND->getIdentifier(); |
| 2500 | if (!Name) |
| 2501 | return; |
| 2502 | |
| 2503 | // Compute the edit distance between the typo and the name of this |
| 2504 | // entity. If this edit distance is not worse than the best edit |
| 2505 | // distance we've seen so far, add it to the list of results. |
| 2506 | unsigned ED = Typo.edit_distance(Name->getName()); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2507 | if (!BestResults.empty() || !BestKeywords.empty()) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2508 | if (ED < BestEditDistance) { |
| 2509 | // This result is better than any we've seen before; clear out |
| 2510 | // the previous results. |
| 2511 | BestResults.clear(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2512 | BestKeywords.clear(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2513 | BestEditDistance = ED; |
| 2514 | } else if (ED > BestEditDistance) { |
| 2515 | // This result is worse than the best results we've seen so far; |
| 2516 | // ignore it. |
| 2517 | return; |
| 2518 | } |
| 2519 | } else |
| 2520 | BestEditDistance = ED; |
| 2521 | |
| 2522 | BestResults.push_back(ND); |
| 2523 | } |
| 2524 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2525 | void TypoCorrectionConsumer::addKeywordResult(ASTContext &Context, |
| 2526 | llvm::StringRef Keyword) { |
| 2527 | // Compute the edit distance between the typo and this keyword. |
| 2528 | // If this edit distance is not worse than the best edit |
| 2529 | // distance we've seen so far, add it to the list of results. |
| 2530 | unsigned ED = Typo.edit_distance(Keyword); |
| 2531 | if (!BestResults.empty() || !BestKeywords.empty()) { |
| 2532 | if (ED < BestEditDistance) { |
| 2533 | BestResults.clear(); |
| 2534 | BestKeywords.clear(); |
| 2535 | BestEditDistance = ED; |
| 2536 | } else if (ED > BestEditDistance) { |
| 2537 | // This result is worse than the best results we've seen so far; |
| 2538 | // ignore it. |
| 2539 | return; |
| 2540 | } |
| 2541 | } else |
| 2542 | BestEditDistance = ED; |
| 2543 | |
| 2544 | BestKeywords.push_back(&Context.Idents.get(Keyword)); |
| 2545 | } |
| 2546 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2547 | /// \brief Try to "correct" a typo in the source code by finding |
| 2548 | /// visible declarations whose names are similar to the name that was |
| 2549 | /// present in the source code. |
| 2550 | /// |
| 2551 | /// \param Res the \c LookupResult structure that contains the name |
| 2552 | /// that was present in the source code along with the name-lookup |
| 2553 | /// criteria used to search for the name. On success, this structure |
| 2554 | /// will contain the results of name lookup. |
| 2555 | /// |
| 2556 | /// \param S the scope in which name lookup occurs. |
| 2557 | /// |
| 2558 | /// \param SS the nested-name-specifier that precedes the name we're |
| 2559 | /// looking for, if present. |
| 2560 | /// |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2561 | /// \param MemberContext if non-NULL, the context in which to look for |
| 2562 | /// a member access expression. |
| 2563 | /// |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 2564 | /// \param EnteringContext whether we're entering the context described by |
| 2565 | /// the nested-name-specifier SS. |
| 2566 | /// |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2567 | /// \param CTC The context in which typo correction occurs, which impacts the |
| 2568 | /// set of keywords permitted. |
| 2569 | /// |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2570 | /// \param OPT when non-NULL, the search for visible declarations will |
| 2571 | /// also walk the protocols in the qualified interfaces of \p OPT. |
| 2572 | /// |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2573 | /// \returns the corrected name if the typo was corrected, otherwise returns an |
| 2574 | /// empty \c DeclarationName. When a typo was corrected, the result structure |
| 2575 | /// may contain the results of name lookup for the correct name or it may be |
| 2576 | /// empty. |
| 2577 | DeclarationName Sema::CorrectTypo(LookupResult &Res, Scope *S, CXXScopeSpec *SS, |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2578 | DeclContext *MemberContext, |
| 2579 | bool EnteringContext, |
| 2580 | CorrectTypoContext CTC, |
| 2581 | const ObjCObjectPointerType *OPT) { |
Ted Kremenek | 1dac341 | 2010-01-06 00:23:04 +0000 | [diff] [blame] | 2582 | if (Diags.hasFatalErrorOccurred()) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2583 | return DeclarationName(); |
Ted Kremenek | d0ed448 | 2010-02-02 02:07:01 +0000 | [diff] [blame] | 2584 | |
| 2585 | // Provide a stop gap for files that are just seriously broken. Trying |
| 2586 | // to correct all typos can turn into a HUGE performance penalty, causing |
| 2587 | // some files to take minutes to get rejected by the parser. |
| 2588 | // FIXME: Is this the right solution? |
| 2589 | if (TyposCorrected == 20) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2590 | return DeclarationName(); |
Ted Kremenek | d0ed448 | 2010-02-02 02:07:01 +0000 | [diff] [blame] | 2591 | ++TyposCorrected; |
Ted Kremenek | 1dac341 | 2010-01-06 00:23:04 +0000 | [diff] [blame] | 2592 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2593 | // We only attempt to correct typos for identifiers. |
| 2594 | IdentifierInfo *Typo = Res.getLookupName().getAsIdentifierInfo(); |
| 2595 | if (!Typo) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2596 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2597 | |
| 2598 | // If the scope specifier itself was invalid, don't try to correct |
| 2599 | // typos. |
| 2600 | if (SS && SS->isInvalid()) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2601 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2602 | |
| 2603 | // Never try to correct typos during template deduction or |
| 2604 | // instantiation. |
| 2605 | if (!ActiveTemplateInstantiations.empty()) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2606 | return DeclarationName(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2607 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2608 | TypoCorrectionConsumer Consumer(Typo); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2609 | |
| 2610 | // Perform name lookup to find visible, similarly-named entities. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2611 | if (MemberContext) { |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2612 | LookupVisibleDecls(MemberContext, Res.getLookupKind(), Consumer); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2613 | |
| 2614 | // Look in qualified interfaces. |
| 2615 | if (OPT) { |
| 2616 | for (ObjCObjectPointerType::qual_iterator |
| 2617 | I = OPT->qual_begin(), E = OPT->qual_end(); |
| 2618 | I != E; ++I) |
| 2619 | LookupVisibleDecls(*I, Res.getLookupKind(), Consumer); |
| 2620 | } |
| 2621 | } else if (SS && SS->isSet()) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2622 | DeclContext *DC = computeDeclContext(*SS, EnteringContext); |
| 2623 | if (!DC) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2624 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2625 | |
| 2626 | LookupVisibleDecls(DC, Res.getLookupKind(), Consumer); |
| 2627 | } else { |
| 2628 | LookupVisibleDecls(S, Res.getLookupKind(), Consumer); |
| 2629 | } |
| 2630 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2631 | // Add context-dependent keywords. |
| 2632 | bool WantTypeSpecifiers = false; |
| 2633 | bool WantExpressionKeywords = false; |
| 2634 | bool WantCXXNamedCasts = false; |
| 2635 | bool WantRemainingKeywords = false; |
| 2636 | switch (CTC) { |
| 2637 | case CTC_Unknown: |
| 2638 | WantTypeSpecifiers = true; |
| 2639 | WantExpressionKeywords = true; |
| 2640 | WantCXXNamedCasts = true; |
| 2641 | WantRemainingKeywords = true; |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 2642 | |
| 2643 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 2644 | if (Method->getClassInterface() && |
| 2645 | Method->getClassInterface()->getSuperClass()) |
| 2646 | Consumer.addKeywordResult(Context, "super"); |
| 2647 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2648 | break; |
| 2649 | |
| 2650 | case CTC_NoKeywords: |
| 2651 | break; |
| 2652 | |
| 2653 | case CTC_Type: |
| 2654 | WantTypeSpecifiers = true; |
| 2655 | break; |
| 2656 | |
| 2657 | case CTC_ObjCMessageReceiver: |
| 2658 | Consumer.addKeywordResult(Context, "super"); |
| 2659 | // Fall through to handle message receivers like expressions. |
| 2660 | |
| 2661 | case CTC_Expression: |
| 2662 | if (getLangOptions().CPlusPlus) |
| 2663 | WantTypeSpecifiers = true; |
| 2664 | WantExpressionKeywords = true; |
| 2665 | // Fall through to get C++ named casts. |
| 2666 | |
| 2667 | case CTC_CXXCasts: |
| 2668 | WantCXXNamedCasts = true; |
| 2669 | break; |
| 2670 | |
| 2671 | case CTC_MemberLookup: |
| 2672 | if (getLangOptions().CPlusPlus) |
| 2673 | Consumer.addKeywordResult(Context, "template"); |
| 2674 | break; |
| 2675 | } |
| 2676 | |
| 2677 | if (WantTypeSpecifiers) { |
| 2678 | // Add type-specifier keywords to the set of results. |
| 2679 | const char *CTypeSpecs[] = { |
| 2680 | "char", "const", "double", "enum", "float", "int", "long", "short", |
| 2681 | "signed", "struct", "union", "unsigned", "void", "volatile", "_Bool", |
| 2682 | "_Complex", "_Imaginary", |
| 2683 | // storage-specifiers as well |
| 2684 | "extern", "inline", "static", "typedef" |
| 2685 | }; |
| 2686 | |
| 2687 | const unsigned NumCTypeSpecs = sizeof(CTypeSpecs) / sizeof(CTypeSpecs[0]); |
| 2688 | for (unsigned I = 0; I != NumCTypeSpecs; ++I) |
| 2689 | Consumer.addKeywordResult(Context, CTypeSpecs[I]); |
| 2690 | |
| 2691 | if (getLangOptions().C99) |
| 2692 | Consumer.addKeywordResult(Context, "restrict"); |
| 2693 | if (getLangOptions().Bool || getLangOptions().CPlusPlus) |
| 2694 | Consumer.addKeywordResult(Context, "bool"); |
| 2695 | |
| 2696 | if (getLangOptions().CPlusPlus) { |
| 2697 | Consumer.addKeywordResult(Context, "class"); |
| 2698 | Consumer.addKeywordResult(Context, "typename"); |
| 2699 | Consumer.addKeywordResult(Context, "wchar_t"); |
| 2700 | |
| 2701 | if (getLangOptions().CPlusPlus0x) { |
| 2702 | Consumer.addKeywordResult(Context, "char16_t"); |
| 2703 | Consumer.addKeywordResult(Context, "char32_t"); |
| 2704 | Consumer.addKeywordResult(Context, "constexpr"); |
| 2705 | Consumer.addKeywordResult(Context, "decltype"); |
| 2706 | Consumer.addKeywordResult(Context, "thread_local"); |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | if (getLangOptions().GNUMode) |
| 2711 | Consumer.addKeywordResult(Context, "typeof"); |
| 2712 | } |
| 2713 | |
Douglas Gregor | d0785ea | 2010-05-18 16:30:22 +0000 | [diff] [blame] | 2714 | if (WantCXXNamedCasts && getLangOptions().CPlusPlus) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2715 | Consumer.addKeywordResult(Context, "const_cast"); |
| 2716 | Consumer.addKeywordResult(Context, "dynamic_cast"); |
| 2717 | Consumer.addKeywordResult(Context, "reinterpret_cast"); |
| 2718 | Consumer.addKeywordResult(Context, "static_cast"); |
| 2719 | } |
| 2720 | |
| 2721 | if (WantExpressionKeywords) { |
| 2722 | Consumer.addKeywordResult(Context, "sizeof"); |
| 2723 | if (getLangOptions().Bool || getLangOptions().CPlusPlus) { |
| 2724 | Consumer.addKeywordResult(Context, "false"); |
| 2725 | Consumer.addKeywordResult(Context, "true"); |
| 2726 | } |
| 2727 | |
| 2728 | if (getLangOptions().CPlusPlus) { |
| 2729 | const char *CXXExprs[] = { |
| 2730 | "delete", "new", "operator", "throw", "typeid" |
| 2731 | }; |
| 2732 | const unsigned NumCXXExprs = sizeof(CXXExprs) / sizeof(CXXExprs[0]); |
| 2733 | for (unsigned I = 0; I != NumCXXExprs; ++I) |
| 2734 | Consumer.addKeywordResult(Context, CXXExprs[I]); |
| 2735 | |
| 2736 | if (isa<CXXMethodDecl>(CurContext) && |
| 2737 | cast<CXXMethodDecl>(CurContext)->isInstance()) |
| 2738 | Consumer.addKeywordResult(Context, "this"); |
| 2739 | |
| 2740 | if (getLangOptions().CPlusPlus0x) { |
| 2741 | Consumer.addKeywordResult(Context, "alignof"); |
| 2742 | Consumer.addKeywordResult(Context, "nullptr"); |
| 2743 | } |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | if (WantRemainingKeywords) { |
| 2748 | if (getCurFunctionOrMethodDecl() || getCurBlock()) { |
| 2749 | // Statements. |
| 2750 | const char *CStmts[] = { |
| 2751 | "do", "else", "for", "goto", "if", "return", "switch", "while" }; |
| 2752 | const unsigned NumCStmts = sizeof(CStmts) / sizeof(CStmts[0]); |
| 2753 | for (unsigned I = 0; I != NumCStmts; ++I) |
| 2754 | Consumer.addKeywordResult(Context, CStmts[I]); |
| 2755 | |
| 2756 | if (getLangOptions().CPlusPlus) { |
| 2757 | Consumer.addKeywordResult(Context, "catch"); |
| 2758 | Consumer.addKeywordResult(Context, "try"); |
| 2759 | } |
| 2760 | |
| 2761 | if (S && S->getBreakParent()) |
| 2762 | Consumer.addKeywordResult(Context, "break"); |
| 2763 | |
| 2764 | if (S && S->getContinueParent()) |
| 2765 | Consumer.addKeywordResult(Context, "continue"); |
| 2766 | |
| 2767 | if (!getSwitchStack().empty()) { |
| 2768 | Consumer.addKeywordResult(Context, "case"); |
| 2769 | Consumer.addKeywordResult(Context, "default"); |
| 2770 | } |
| 2771 | } else { |
| 2772 | if (getLangOptions().CPlusPlus) { |
| 2773 | Consumer.addKeywordResult(Context, "namespace"); |
| 2774 | Consumer.addKeywordResult(Context, "template"); |
| 2775 | } |
| 2776 | |
| 2777 | if (S && S->isClassScope()) { |
| 2778 | Consumer.addKeywordResult(Context, "explicit"); |
| 2779 | Consumer.addKeywordResult(Context, "friend"); |
| 2780 | Consumer.addKeywordResult(Context, "mutable"); |
| 2781 | Consumer.addKeywordResult(Context, "private"); |
| 2782 | Consumer.addKeywordResult(Context, "protected"); |
| 2783 | Consumer.addKeywordResult(Context, "public"); |
| 2784 | Consumer.addKeywordResult(Context, "virtual"); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | if (getLangOptions().CPlusPlus) { |
| 2789 | Consumer.addKeywordResult(Context, "using"); |
| 2790 | |
| 2791 | if (getLangOptions().CPlusPlus0x) |
| 2792 | Consumer.addKeywordResult(Context, "static_assert"); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | // If we haven't found anything, we're done. |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2797 | if (Consumer.empty()) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2798 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2799 | |
| 2800 | // Only allow a single, closest name in the result set (it's okay to |
| 2801 | // have overloads of that name, though). |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2802 | DeclarationName BestName; |
| 2803 | NamedDecl *BestIvarOrPropertyDecl = 0; |
| 2804 | bool FoundIvarOrPropertyDecl = false; |
| 2805 | |
| 2806 | // Check all of the declaration results to find the best name so far. |
| 2807 | for (TypoCorrectionConsumer::iterator I = Consumer.begin(), |
| 2808 | IEnd = Consumer.end(); |
| 2809 | I != IEnd; ++I) { |
| 2810 | if (!BestName) |
| 2811 | BestName = (*I)->getDeclName(); |
| 2812 | else if (BestName != (*I)->getDeclName()) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2813 | return DeclarationName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2815 | // \brief Keep track of either an Objective-C ivar or a property, but not |
| 2816 | // both. |
| 2817 | if (isa<ObjCIvarDecl>(*I) || isa<ObjCPropertyDecl>(*I)) { |
| 2818 | if (FoundIvarOrPropertyDecl) |
| 2819 | BestIvarOrPropertyDecl = 0; |
| 2820 | else { |
| 2821 | BestIvarOrPropertyDecl = *I; |
| 2822 | FoundIvarOrPropertyDecl = true; |
| 2823 | } |
| 2824 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2827 | // Now check all of the keyword results to find the best name. |
| 2828 | switch (Consumer.keyword_size()) { |
| 2829 | case 0: |
| 2830 | // No keywords matched. |
| 2831 | break; |
| 2832 | |
| 2833 | case 1: |
| 2834 | // If we already have a name |
| 2835 | if (!BestName) { |
| 2836 | // We did not have anything previously, |
| 2837 | BestName = *Consumer.keyword_begin(); |
| 2838 | } else if (BestName.getAsIdentifierInfo() == *Consumer.keyword_begin()) { |
| 2839 | // We have a declaration with the same name as a context-sensitive |
| 2840 | // keyword. The keyword takes precedence. |
| 2841 | BestIvarOrPropertyDecl = 0; |
| 2842 | FoundIvarOrPropertyDecl = false; |
| 2843 | Consumer.clear_decls(); |
Douglas Gregor | d0785ea | 2010-05-18 16:30:22 +0000 | [diff] [blame] | 2844 | } else if (CTC == CTC_ObjCMessageReceiver && |
| 2845 | (*Consumer.keyword_begin())->isStr("super")) { |
| 2846 | // In an Objective-C message send, give the "super" keyword a slight |
| 2847 | // edge over entities not in function or method scope. |
| 2848 | for (TypoCorrectionConsumer::iterator I = Consumer.begin(), |
| 2849 | IEnd = Consumer.end(); |
| 2850 | I != IEnd; ++I) { |
| 2851 | if ((*I)->getDeclName() == BestName) { |
| 2852 | if ((*I)->getDeclContext()->isFunctionOrMethod()) |
| 2853 | return DeclarationName(); |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | // Everything found was outside a function or method; the 'super' |
| 2858 | // keyword takes precedence. |
| 2859 | BestIvarOrPropertyDecl = 0; |
| 2860 | FoundIvarOrPropertyDecl = false; |
| 2861 | Consumer.clear_decls(); |
| 2862 | BestName = *Consumer.keyword_begin(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2863 | } else { |
| 2864 | // Name collision; we will not correct typos. |
| 2865 | return DeclarationName(); |
| 2866 | } |
| 2867 | break; |
| 2868 | |
| 2869 | default: |
| 2870 | // Name collision; we will not correct typos. |
| 2871 | return DeclarationName(); |
| 2872 | } |
| 2873 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2874 | // BestName is the closest viable name to what the user |
| 2875 | // typed. However, to make sure that we don't pick something that's |
| 2876 | // way off, make sure that the user typed at least 3 characters for |
| 2877 | // each correction. |
| 2878 | unsigned ED = Consumer.getBestEditDistance(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2879 | if (ED == 0 || !BestName.getAsIdentifierInfo() || |
| 2880 | (BestName.getAsIdentifierInfo()->getName().size() / ED) < 3) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2881 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2882 | |
| 2883 | // Perform name lookup again with the name we chose, and declare |
| 2884 | // success if we found something that was not ambiguous. |
| 2885 | Res.clear(); |
| 2886 | Res.setLookupName(BestName); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2887 | |
| 2888 | // If we found an ivar or property, add that result; no further |
| 2889 | // lookup is required. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2890 | if (BestIvarOrPropertyDecl) |
| 2891 | Res.addDecl(BestIvarOrPropertyDecl); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2892 | // If we're looking into the context of a member, perform qualified |
| 2893 | // name lookup on the best name. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2894 | else if (!Consumer.keyword_empty()) { |
| 2895 | // The best match was a keyword. Return it. |
| 2896 | return BestName; |
| 2897 | } else if (MemberContext) |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2898 | LookupQualifiedName(Res, MemberContext); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2899 | // Perform lookup as if we had just parsed the best name. |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2900 | else |
| 2901 | LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, |
| 2902 | EnteringContext); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 2903 | |
| 2904 | if (Res.isAmbiguous()) { |
| 2905 | Res.suppressDiagnostics(); |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2906 | return DeclarationName(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2909 | if (Res.getResultKind() != LookupResult::NotFound) |
| 2910 | return BestName; |
| 2911 | |
| 2912 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2913 | } |