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