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