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