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 | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 30 | #include <set> |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 31 | #include <vector> |
| 32 | #include <iterator> |
| 33 | #include <utility> |
| 34 | #include <algorithm> |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace clang; |
| 37 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | class UnqualUsingEntry { |
| 40 | const DeclContext *Nominated; |
| 41 | const DeclContext *CommonAncestor; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 42 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 43 | public: |
| 44 | UnqualUsingEntry(const DeclContext *Nominated, |
| 45 | const DeclContext *CommonAncestor) |
| 46 | : Nominated(Nominated), CommonAncestor(CommonAncestor) { |
| 47 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 48 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 49 | const DeclContext *getCommonAncestor() const { |
| 50 | return CommonAncestor; |
| 51 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 52 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 53 | const DeclContext *getNominatedNamespace() const { |
| 54 | return Nominated; |
| 55 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 56 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 57 | // Sort by the pointer value of the common ancestor. |
| 58 | struct Comparator { |
| 59 | bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { |
| 60 | return L.getCommonAncestor() < R.getCommonAncestor(); |
| 61 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 62 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 63 | bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { |
| 64 | return E.getCommonAncestor() < DC; |
| 65 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 66 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 67 | bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { |
| 68 | return DC < E.getCommonAncestor(); |
| 69 | } |
| 70 | }; |
| 71 | }; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 72 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 73 | /// A collection of using directives, as used by C++ unqualified |
| 74 | /// lookup. |
| 75 | class UnqualUsingDirectiveSet { |
| 76 | typedef llvm::SmallVector<UnqualUsingEntry, 8> ListTy; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 77 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 78 | ListTy list; |
| 79 | llvm::SmallPtrSet<DeclContext*, 8> visited; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 80 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 81 | public: |
| 82 | UnqualUsingDirectiveSet() {} |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 83 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 84 | void visitScopeChain(Scope *S, Scope *InnermostFileScope) { |
| 85 | // C++ [namespace.udir]p1: |
| 86 | // During unqualified name lookup, the names appear as if they |
| 87 | // were declared in the nearest enclosing namespace which contains |
| 88 | // both the using-directive and the nominated namespace. |
| 89 | DeclContext *InnermostFileDC |
| 90 | = static_cast<DeclContext*>(InnermostFileScope->getEntity()); |
| 91 | assert(InnermostFileDC && InnermostFileDC->isFileContext()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 92 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 93 | for (; S; S = S->getParent()) { |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 94 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 95 | DeclContext *EffectiveDC = (Ctx->isFileContext() ? Ctx : InnermostFileDC); |
| 96 | visit(Ctx, EffectiveDC); |
| 97 | } else { |
| 98 | Scope::udir_iterator I = S->using_directives_begin(), |
| 99 | End = S->using_directives_end(); |
| 100 | |
| 101 | for (; I != End; ++I) |
| 102 | visit(I->getAs<UsingDirectiveDecl>(), InnermostFileDC); |
| 103 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 106 | |
| 107 | // Visits a context and collect all of its using directives |
| 108 | // recursively. Treats all using directives as if they were |
| 109 | // declared in the context. |
| 110 | // |
| 111 | // A given context is only every visited once, so it is important |
| 112 | // that contexts be visited from the inside out in order to get |
| 113 | // the effective DCs right. |
| 114 | void visit(DeclContext *DC, DeclContext *EffectiveDC) { |
| 115 | if (!visited.insert(DC)) |
| 116 | return; |
| 117 | |
| 118 | addUsingDirectives(DC, EffectiveDC); |
| 119 | } |
| 120 | |
| 121 | // Visits a using directive and collects all of its using |
| 122 | // directives recursively. Treats all using directives as if they |
| 123 | // were declared in the effective DC. |
| 124 | void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 125 | DeclContext *NS = UD->getNominatedNamespace(); |
| 126 | if (!visited.insert(NS)) |
| 127 | return; |
| 128 | |
| 129 | addUsingDirective(UD, EffectiveDC); |
| 130 | addUsingDirectives(NS, EffectiveDC); |
| 131 | } |
| 132 | |
| 133 | // Adds all the using directives in a context (and those nominated |
| 134 | // by its using directives, transitively) as if they appeared in |
| 135 | // the given effective context. |
| 136 | void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { |
| 137 | llvm::SmallVector<DeclContext*,4> queue; |
| 138 | while (true) { |
| 139 | DeclContext::udir_iterator I, End; |
| 140 | for (llvm::tie(I, End) = DC->getUsingDirectives(); I != End; ++I) { |
| 141 | UsingDirectiveDecl *UD = *I; |
| 142 | DeclContext *NS = UD->getNominatedNamespace(); |
| 143 | if (visited.insert(NS)) { |
| 144 | addUsingDirective(UD, EffectiveDC); |
| 145 | queue.push_back(NS); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (queue.empty()) |
| 150 | return; |
| 151 | |
| 152 | DC = queue.back(); |
| 153 | queue.pop_back(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Add a using directive as if it had been declared in the given |
| 158 | // context. This helps implement C++ [namespace.udir]p3: |
| 159 | // The using-directive is transitive: if a scope contains a |
| 160 | // using-directive that nominates a second namespace that itself |
| 161 | // contains using-directives, the effect is as if the |
| 162 | // using-directives from the second namespace also appeared in |
| 163 | // the first. |
| 164 | void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 165 | // Find the common ancestor between the effective context and |
| 166 | // the nominated namespace. |
| 167 | DeclContext *Common = UD->getNominatedNamespace(); |
| 168 | while (!Common->Encloses(EffectiveDC)) |
| 169 | Common = Common->getParent(); |
John McCall | 12ea578 | 2009-11-10 09:20:04 +0000 | [diff] [blame] | 170 | Common = Common->getPrimaryContext(); |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 171 | |
| 172 | list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); |
| 173 | } |
| 174 | |
| 175 | void done() { |
| 176 | std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator()); |
| 177 | } |
| 178 | |
| 179 | typedef ListTy::iterator iterator; |
| 180 | typedef ListTy::const_iterator const_iterator; |
| 181 | |
| 182 | iterator begin() { return list.begin(); } |
| 183 | iterator end() { return list.end(); } |
| 184 | const_iterator begin() const { return list.begin(); } |
| 185 | const_iterator end() const { return list.end(); } |
| 186 | |
| 187 | std::pair<const_iterator,const_iterator> |
| 188 | getNamespacesFor(DeclContext *DC) const { |
John McCall | 12ea578 | 2009-11-10 09:20:04 +0000 | [diff] [blame] | 189 | return std::equal_range(begin(), end(), DC->getPrimaryContext(), |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 190 | UnqualUsingEntry::Comparator()); |
| 191 | } |
| 192 | }; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 193 | } |
| 194 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 195 | static bool IsAcceptableIDNS(NamedDecl *D, unsigned IDNS) { |
| 196 | return D->isInIdentifierNamespace(IDNS); |
| 197 | } |
| 198 | |
| 199 | static bool IsAcceptableOperatorName(NamedDecl *D, unsigned IDNS) { |
| 200 | return D->isInIdentifierNamespace(IDNS) && |
| 201 | !D->getDeclContext()->isRecord(); |
| 202 | } |
| 203 | |
| 204 | static bool IsAcceptableNestedNameSpecifierName(NamedDecl *D, unsigned IDNS) { |
John McCall | 4e0d81f | 2009-12-18 10:48:10 +0000 | [diff] [blame] | 205 | // This lookup ignores everything that isn't a type. |
| 206 | |
| 207 | // This is a fast check for the far most common case. |
| 208 | if (D->isInIdentifierNamespace(Decl::IDNS_Tag)) |
| 209 | return true; |
| 210 | |
| 211 | if (isa<UsingShadowDecl>(D)) |
| 212 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 213 | |
| 214 | return isa<TypeDecl>(D); |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static bool IsAcceptableNamespaceName(NamedDecl *D, unsigned IDNS) { |
John McCall | 4e0d81f | 2009-12-18 10:48:10 +0000 | [diff] [blame] | 218 | // We don't need to look through using decls here because |
| 219 | // using decls aren't allowed to name namespaces. |
| 220 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 221 | return isa<NamespaceDecl>(D) || isa<NamespaceAliasDecl>(D); |
| 222 | } |
| 223 | |
| 224 | /// Gets the default result filter for the given lookup. |
| 225 | static inline |
| 226 | LookupResult::ResultFilter getResultFilter(Sema::LookupNameKind NameKind) { |
| 227 | switch (NameKind) { |
| 228 | case Sema::LookupOrdinaryName: |
| 229 | case Sema::LookupTagName: |
| 230 | case Sema::LookupMemberName: |
| 231 | case Sema::LookupRedeclarationWithLinkage: // FIXME: check linkage, scoping |
| 232 | case Sema::LookupUsingDeclName: |
| 233 | case Sema::LookupObjCProtocolName: |
| 234 | case Sema::LookupObjCImplementationName: |
| 235 | return &IsAcceptableIDNS; |
| 236 | |
| 237 | case Sema::LookupOperatorName: |
| 238 | return &IsAcceptableOperatorName; |
| 239 | |
| 240 | case Sema::LookupNestedNameSpecifierName: |
| 241 | return &IsAcceptableNestedNameSpecifierName; |
| 242 | |
| 243 | case Sema::LookupNamespaceName: |
| 244 | return &IsAcceptableNamespaceName; |
| 245 | } |
| 246 | |
| 247 | llvm_unreachable("unkknown lookup kind"); |
| 248 | return 0; |
| 249 | } |
| 250 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 251 | // Retrieve the set of identifier namespaces that correspond to a |
| 252 | // specific kind of name lookup. |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 253 | static inline unsigned getIDNS(Sema::LookupNameKind NameKind, |
| 254 | bool CPlusPlus, |
| 255 | bool Redeclaration) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 256 | unsigned IDNS = 0; |
| 257 | switch (NameKind) { |
| 258 | case Sema::LookupOrdinaryName: |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 259 | case Sema::LookupOperatorName: |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 260 | case Sema::LookupRedeclarationWithLinkage: |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 261 | IDNS = Decl::IDNS_Ordinary; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 262 | if (CPlusPlus) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 263 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 264 | if (Redeclaration) IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; |
| 265 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 266 | break; |
| 267 | |
| 268 | case Sema::LookupTagName: |
| 269 | IDNS = Decl::IDNS_Tag; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 270 | if (CPlusPlus && Redeclaration) |
| 271 | IDNS |= Decl::IDNS_TagFriend; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 272 | break; |
| 273 | |
| 274 | case Sema::LookupMemberName: |
| 275 | IDNS = Decl::IDNS_Member; |
| 276 | if (CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 278 | break; |
| 279 | |
| 280 | case Sema::LookupNestedNameSpecifierName: |
| 281 | case Sema::LookupNamespaceName: |
| 282 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member; |
| 283 | break; |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 284 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 285 | case Sema::LookupUsingDeclName: |
| 286 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag |
| 287 | | Decl::IDNS_Member | Decl::IDNS_Using; |
| 288 | break; |
| 289 | |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 290 | case Sema::LookupObjCProtocolName: |
| 291 | IDNS = Decl::IDNS_ObjCProtocol; |
| 292 | break; |
| 293 | |
| 294 | case Sema::LookupObjCImplementationName: |
| 295 | IDNS = Decl::IDNS_ObjCImplementation; |
| 296 | break; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 297 | } |
| 298 | return IDNS; |
| 299 | } |
| 300 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 301 | void LookupResult::configure() { |
| 302 | IDNS = getIDNS(LookupKind, |
| 303 | SemaRef.getLangOptions().CPlusPlus, |
| 304 | isForRedeclaration()); |
| 305 | IsAcceptableFn = getResultFilter(LookupKind); |
| 306 | } |
| 307 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 308 | // Necessary because CXXBasePaths is not complete in Sema.h |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 309 | void LookupResult::deletePaths(CXXBasePaths *Paths) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 310 | delete Paths; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 311 | } |
| 312 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 313 | /// Resolves the result kind of this lookup. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 314 | void LookupResult::resolveKind() { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 315 | unsigned N = Decls.size(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 316 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 317 | // Fast case: no possible ambiguity. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 318 | if (N == 0) { |
| 319 | assert(ResultKind == NotFound); |
| 320 | return; |
| 321 | } |
| 322 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 323 | // If there's a single decl, we need to examine it to decide what |
| 324 | // kind of lookup this is. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 325 | if (N == 1) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 326 | if (isa<FunctionTemplateDecl>(Decls[0])) |
| 327 | ResultKind = FoundOverloaded; |
| 328 | else if (isa<UnresolvedUsingValueDecl>(Decls[0])) |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 329 | ResultKind = FoundUnresolvedValue; |
| 330 | return; |
| 331 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 332 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 333 | // 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] | 334 | if (ResultKind == Ambiguous) return; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 335 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 336 | llvm::SmallPtrSet<NamedDecl*, 16> Unique; |
| 337 | |
| 338 | bool Ambiguous = false; |
| 339 | bool HasTag = false, HasFunction = false, HasNonFunction = false; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 340 | bool HasFunctionTemplate = false, HasUnresolved = false; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 341 | |
| 342 | unsigned UniqueTagIndex = 0; |
| 343 | |
| 344 | unsigned I = 0; |
| 345 | while (I < N) { |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 346 | NamedDecl *D = Decls[I]->getUnderlyingDecl(); |
| 347 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 348 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 349 | if (!Unique.insert(D)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 350 | // If it's not unique, pull something off the back (and |
| 351 | // continue at this index). |
| 352 | Decls[I] = Decls[--N]; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 353 | } else { |
| 354 | // Otherwise, do some decl type analysis and then continue. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 355 | |
| 356 | if (isa<UnresolvedUsingValueDecl>(D)) { |
| 357 | HasUnresolved = true; |
| 358 | } else if (isa<TagDecl>(D)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 359 | if (HasTag) |
| 360 | Ambiguous = true; |
| 361 | UniqueTagIndex = I; |
| 362 | HasTag = true; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 363 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 364 | HasFunction = true; |
| 365 | HasFunctionTemplate = true; |
| 366 | } else if (isa<FunctionDecl>(D)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 367 | HasFunction = true; |
| 368 | } else { |
| 369 | if (HasNonFunction) |
| 370 | Ambiguous = true; |
| 371 | HasNonFunction = true; |
| 372 | } |
| 373 | I++; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 374 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | } |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 376 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 377 | // C++ [basic.scope.hiding]p2: |
| 378 | // A class name or enumeration name can be hidden by the name of |
| 379 | // an object, function, or enumerator declared in the same |
| 380 | // scope. If a class or enumeration name and an object, function, |
| 381 | // or enumerator are declared in the same scope (in any order) |
| 382 | // with the same name, the class or enumeration name is hidden |
| 383 | // wherever the object, function, or enumerator name is visible. |
| 384 | // But it's still an error if there are distinct tag types found, |
| 385 | // even if they're not visible. (ref?) |
John McCall | fda8e12 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 386 | if (HideTags && HasTag && !Ambiguous && |
| 387 | (HasFunction || HasNonFunction || HasUnresolved)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 388 | Decls[UniqueTagIndex] = Decls[--N]; |
Anders Carlsson | 8b50d01 | 2009-06-26 03:37:05 +0000 | [diff] [blame] | 389 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 390 | Decls.set_size(N); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 391 | |
John McCall | fda8e12 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 392 | if (HasNonFunction && (HasFunction || HasUnresolved)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 393 | Ambiguous = true; |
Douglas Gregor | 69d993a | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 394 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 395 | if (Ambiguous) |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 396 | setAmbiguous(LookupResult::AmbiguousReference); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 397 | else if (HasUnresolved) |
| 398 | ResultKind = LookupResult::FoundUnresolvedValue; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 399 | else if (N > 1 || HasFunctionTemplate) |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 400 | ResultKind = LookupResult::FoundOverloaded; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 401 | else |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 402 | ResultKind = LookupResult::Found; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 403 | } |
| 404 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 405 | void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 406 | CXXBasePaths::paths_iterator I, E; |
| 407 | DeclContext::lookup_iterator DI, DE; |
| 408 | for (I = P.begin(), E = P.end(); I != E; ++I) |
| 409 | for (llvm::tie(DI,DE) = I->Decls; DI != DE; ++DI) |
| 410 | addDecl(*DI); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 411 | } |
| 412 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 413 | void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 414 | Paths = new CXXBasePaths; |
| 415 | Paths->swap(P); |
| 416 | addDeclsFromBasePaths(*Paths); |
| 417 | resolveKind(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 418 | setAmbiguous(AmbiguousBaseSubobjects); |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 419 | } |
| 420 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 421 | void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 422 | Paths = new CXXBasePaths; |
| 423 | Paths->swap(P); |
| 424 | addDeclsFromBasePaths(*Paths); |
| 425 | resolveKind(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 426 | setAmbiguous(AmbiguousBaseSubobjectTypes); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 427 | } |
| 428 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 429 | void LookupResult::print(llvm::raw_ostream &Out) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 430 | Out << Decls.size() << " result(s)"; |
| 431 | if (isAmbiguous()) Out << ", ambiguous"; |
| 432 | if (Paths) Out << ", base paths present"; |
| 433 | |
| 434 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 435 | Out << "\n"; |
| 436 | (*I)->print(Out, 2); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Adds all qualifying matches for a name within a decl context to the |
| 441 | // given lookup result. Returns true if any matches were found. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 442 | static bool LookupDirect(LookupResult &R, const DeclContext *DC) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 443 | bool Found = false; |
| 444 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 445 | DeclContext::lookup_const_iterator I, E; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 446 | for (llvm::tie(I, E) = DC->lookup(R.getLookupName()); I != E; ++I) |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 447 | if (R.isAcceptableDecl(*I)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 448 | R.addDecl(*I), Found = true; |
| 449 | |
| 450 | return Found; |
| 451 | } |
| 452 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 453 | // Performs C++ unqualified lookup into the given file context. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 454 | static bool |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 455 | CppNamespaceLookup(LookupResult &R, ASTContext &Context, DeclContext *NS, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 456 | UnqualUsingDirectiveSet &UDirs) { |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 457 | |
| 458 | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); |
| 459 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 460 | // Perform direct name lookup into the LookupCtx. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 461 | bool Found = LookupDirect(R, NS); |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 462 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 463 | // Perform direct name lookup into the namespaces nominated by the |
| 464 | // using directives whose common ancestor is this namespace. |
| 465 | UnqualUsingDirectiveSet::const_iterator UI, UEnd; |
| 466 | llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(NS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 468 | for (; UI != UEnd; ++UI) |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 469 | if (LookupDirect(R, UI->getNominatedNamespace())) |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 470 | Found = true; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 471 | |
| 472 | R.resolveKind(); |
| 473 | |
| 474 | return Found; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static bool isNamespaceOrTranslationUnitScope(Scope *S) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 478 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 479 | return Ctx->isFileContext(); |
| 480 | return false; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 481 | } |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 483 | // Find the next outer declaration context corresponding to this scope. |
| 484 | static DeclContext *findOuterContext(Scope *S) { |
| 485 | for (S = S->getParent(); S; S = S->getParent()) |
| 486 | if (S->getEntity()) |
| 487 | return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext(); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 492 | bool Sema::CppLookupName(LookupResult &R, Scope *S) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 493 | assert(getLangOptions().CPlusPlus && "Can perform only C++ lookup"); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 494 | |
| 495 | DeclarationName Name = R.getLookupName(); |
| 496 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 497 | Scope *Initial = S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | IdentifierResolver::iterator |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 499 | I = IdResolver.begin(Name), |
| 500 | IEnd = IdResolver.end(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 502 | // First we lookup local scope. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 503 | // 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] | 504 | // ...During unqualified name lookup (3.4.1), the names appear as if |
| 505 | // they were declared in the nearest enclosing namespace which contains |
| 506 | // both the using-directive and the nominated namespace. |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 507 | // [Note: in this context, "contains" means "contains directly or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | // indirectly". |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 509 | // |
| 510 | // For example: |
| 511 | // namespace A { int i; } |
| 512 | // void foo() { |
| 513 | // int i; |
| 514 | // { |
| 515 | // using namespace A; |
| 516 | // ++i; // finds local 'i', A::i appears at global scope |
| 517 | // } |
| 518 | // } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 519 | // |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 520 | for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 521 | // Check whether the IdResolver has anything in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 522 | bool Found = false; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 523 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 524 | if (R.isAcceptableDecl(*I)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 525 | Found = true; |
| 526 | R.addDecl(*I); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 529 | if (Found) { |
| 530 | R.resolveKind(); |
| 531 | return true; |
| 532 | } |
| 533 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 534 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 535 | DeclContext *OuterCtx = findOuterContext(S); |
| 536 | for (; Ctx && Ctx->getPrimaryContext() != OuterCtx; |
| 537 | Ctx = Ctx->getLookupParent()) { |
Douglas Gregor | 1237259 | 2009-12-08 15:38:36 +0000 | [diff] [blame] | 538 | // We do not directly look into function or method contexts |
| 539 | // (since all local variables are found via the identifier |
| 540 | // changes) or in transparent contexts (since those entities |
| 541 | // will be found in the nearest enclosing non-transparent |
| 542 | // context). |
| 543 | if (Ctx->isFunctionOrMethod() || Ctx->isTransparentContext()) |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 544 | continue; |
| 545 | |
| 546 | // Perform qualified name lookup into this context. |
| 547 | // FIXME: In some cases, we know that every name that could be found by |
| 548 | // this qualified name lookup will also be on the identifier chain. For |
| 549 | // example, inside a class without any base classes, we never need to |
| 550 | // perform qualified lookup because all of the members are on top of the |
| 551 | // identifier chain. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 552 | if (LookupQualifiedName(R, Ctx)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 553 | return true; |
Douglas Gregor | 551f48c | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 554 | } |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 555 | } |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 556 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 557 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 558 | // Stop if we ran out of scopes. |
| 559 | // FIXME: This really, really shouldn't be happening. |
| 560 | if (!S) return false; |
| 561 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 562 | // Collect UsingDirectiveDecls in all scopes, and recursively all |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 563 | // nominated namespaces by those using-directives. |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 564 | // |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 565 | // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we |
| 566 | // don't build it for each lookup! |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 567 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 568 | UnqualUsingDirectiveSet UDirs; |
| 569 | UDirs.visitScopeChain(Initial, S); |
| 570 | UDirs.done(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 571 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 572 | // Lookup namespace scope, and global scope. |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 573 | // Unqualified name lookup in C++ requires looking into scopes |
| 574 | // that aren't strictly lexical, and therefore we walk through the |
| 575 | // context as well as walking through the scopes. |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 577 | for (; S; S = S->getParent()) { |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 578 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Douglas Gregor | a24eb4e | 2009-08-24 18:55:03 +0000 | [diff] [blame] | 579 | if (Ctx->isTransparentContext()) |
| 580 | continue; |
| 581 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 582 | assert(Ctx && Ctx->isFileContext() && |
| 583 | "We should have been looking only at file context here already."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 584 | |
| 585 | // Check whether the IdResolver has anything in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 586 | bool Found = false; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 587 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 588 | if (R.isAcceptableDecl(*I)) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 589 | // We found something. Look for anything else in our scope |
| 590 | // with this same name and in an acceptable identifier |
| 591 | // namespace, so that we can construct an overload set if we |
| 592 | // need to. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 593 | Found = true; |
| 594 | R.addDecl(*I); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 598 | // Look into context considering using-directives. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 599 | if (CppNamespaceLookup(R, Context, Ctx, UDirs)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 600 | Found = true; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 601 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 602 | if (Found) { |
| 603 | R.resolveKind(); |
| 604 | return true; |
| 605 | } |
| 606 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 607 | if (R.isForRedeclaration() && !Ctx->isTransparentContext()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 608 | return false; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 609 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 610 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 611 | return !R.empty(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 614 | /// @brief Perform unqualified name lookup starting from a given |
| 615 | /// scope. |
| 616 | /// |
| 617 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 618 | /// used to find names within the current scope. For example, 'x' in |
| 619 | /// @code |
| 620 | /// int x; |
| 621 | /// int f() { |
| 622 | /// return x; // unqualified name look finds 'x' in the global scope |
| 623 | /// } |
| 624 | /// @endcode |
| 625 | /// |
| 626 | /// Different lookup criteria can find different names. For example, a |
| 627 | /// particular scope can have both a struct and a function of the same |
| 628 | /// name, and each can be found by certain lookup criteria. For more |
| 629 | /// information about lookup criteria, see the documentation for the |
| 630 | /// class LookupCriteria. |
| 631 | /// |
| 632 | /// @param S The scope from which unqualified name lookup will |
| 633 | /// begin. If the lookup criteria permits, name lookup may also search |
| 634 | /// in the parent scopes. |
| 635 | /// |
| 636 | /// @param Name The name of the entity that we are searching for. |
| 637 | /// |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 638 | /// @param Loc If provided, the source location where we're performing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | /// name lookup. At present, this is only used to produce diagnostics when |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 640 | /// C library functions (like "malloc") are implicitly declared. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 641 | /// |
| 642 | /// @returns The result of name lookup, which includes zero or more |
| 643 | /// declarations and possibly additional information used to diagnose |
| 644 | /// ambiguities. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 645 | bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) { |
| 646 | DeclarationName Name = R.getLookupName(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 647 | if (!Name) return false; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 648 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 649 | LookupNameKind NameKind = R.getLookupKind(); |
| 650 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 651 | if (!getLangOptions().CPlusPlus) { |
| 652 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 653 | // search in the declarations attached to the name. |
| 654 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 655 | if (NameKind == Sema::LookupRedeclarationWithLinkage) { |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 656 | // Find the nearest non-transparent declaration scope. |
| 657 | while (!(S->getFlags() & Scope::DeclScope) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | (S->getEntity() && |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 659 | static_cast<DeclContext *>(S->getEntity()) |
| 660 | ->isTransparentContext())) |
| 661 | S = S->getParent(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 662 | } |
| 663 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 664 | unsigned IDNS = R.getIdentifierNamespace(); |
| 665 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 666 | // Scan up the scope chain looking for a decl that matches this |
| 667 | // identifier that is in the appropriate namespace. This search |
| 668 | // should not take long, as shadowing of names is uncommon, and |
| 669 | // deep shadowing is extremely uncommon. |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 670 | bool LeftStartingScope = false; |
| 671 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 672 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | IEnd = IdResolver.end(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 674 | I != IEnd; ++I) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 675 | if ((*I)->isInIdentifierNamespace(IDNS)) { |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 676 | if (NameKind == LookupRedeclarationWithLinkage) { |
| 677 | // Determine whether this (or a previous) declaration is |
| 678 | // out-of-scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 679 | if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 680 | LeftStartingScope = true; |
| 681 | |
| 682 | // If we found something outside of our starting scope that |
| 683 | // does not have linkage, skip it. |
| 684 | if (LeftStartingScope && !((*I)->hasLinkage())) |
| 685 | continue; |
| 686 | } |
| 687 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 688 | R.addDecl(*I); |
| 689 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 690 | if ((*I)->getAttr<OverloadableAttr>()) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 691 | // If this declaration has the "overloadable" attribute, we |
| 692 | // might have a set of overloaded functions. |
| 693 | |
| 694 | // Figure out what scope the identifier is in. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 695 | while (!(S->getFlags() & Scope::DeclScope) || |
| 696 | !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 697 | S = S->getParent(); |
| 698 | |
| 699 | // Find the last declaration in this scope (with the same |
| 700 | // name, naturally). |
| 701 | IdentifierResolver::iterator LastI = I; |
| 702 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 703 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 704 | break; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 705 | R.addDecl(*LastI); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 706 | } |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 707 | } |
| 708 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 709 | R.resolveKind(); |
| 710 | |
| 711 | return true; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 712 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 713 | } else { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 714 | // Perform C++ unqualified name lookup. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 715 | if (CppLookupName(R, S)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 716 | return true; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | // If we didn't find a use of this identifier, and if the identifier |
| 720 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 721 | // now, injecting it into translation unit scope, and return it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | if (NameKind == LookupOrdinaryName || |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 723 | NameKind == LookupRedeclarationWithLinkage) { |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 724 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 725 | if (II && AllowBuiltinCreation) { |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 726 | // If this is a builtin on this (or all) targets, create the decl. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 727 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 728 | // In C++, we don't have any predefined library functions like |
| 729 | // 'malloc'. Instead, we'll just error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 731 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 732 | return false; |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 733 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 734 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 735 | S, R.isForRedeclaration(), |
| 736 | R.getNameLoc()); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 737 | if (D) R.addDecl(D); |
| 738 | return (D != NULL); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 739 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 740 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 741 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 742 | return false; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 743 | } |
| 744 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 745 | /// @brief Perform qualified name lookup in the namespaces nominated by |
| 746 | /// using directives by the given context. |
| 747 | /// |
| 748 | /// C++98 [namespace.qual]p2: |
| 749 | /// Given X::m (where X is a user-declared namespace), or given ::m |
| 750 | /// (where X is the global namespace), let S be the set of all |
| 751 | /// declarations of m in X and in the transitive closure of all |
| 752 | /// namespaces nominated by using-directives in X and its used |
| 753 | /// namespaces, except that using-directives are ignored in any |
| 754 | /// namespace, including X, directly containing one or more |
| 755 | /// declarations of m. No namespace is searched more than once in |
| 756 | /// the lookup of a name. If S is the empty set, the program is |
| 757 | /// ill-formed. Otherwise, if S has exactly one member, or if the |
| 758 | /// context of the reference is a using-declaration |
| 759 | /// (namespace.udecl), S is the required set of declarations of |
| 760 | /// m. Otherwise if the use of m is not one that allows a unique |
| 761 | /// declaration to be chosen from S, the program is ill-formed. |
| 762 | /// C++98 [namespace.qual]p5: |
| 763 | /// During the lookup of a qualified namespace member name, if the |
| 764 | /// lookup finds more than one declaration of the member, and if one |
| 765 | /// declaration introduces a class name or enumeration name and the |
| 766 | /// other declarations either introduce the same object, the same |
| 767 | /// enumerator or a set of functions, the non-type name hides the |
| 768 | /// class or enumeration name if and only if the declarations are |
| 769 | /// from the same namespace; otherwise (the declarations are from |
| 770 | /// different namespaces), the program is ill-formed. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 771 | static bool LookupQualifiedNameInUsingDirectives(LookupResult &R, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 772 | DeclContext *StartDC) { |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 773 | assert(StartDC->isFileContext() && "start context is not a file context"); |
| 774 | |
| 775 | DeclContext::udir_iterator I = StartDC->using_directives_begin(); |
| 776 | DeclContext::udir_iterator E = StartDC->using_directives_end(); |
| 777 | |
| 778 | if (I == E) return false; |
| 779 | |
| 780 | // We have at least added all these contexts to the queue. |
| 781 | llvm::DenseSet<DeclContext*> Visited; |
| 782 | Visited.insert(StartDC); |
| 783 | |
| 784 | // We have not yet looked into these namespaces, much less added |
| 785 | // their "using-children" to the queue. |
| 786 | llvm::SmallVector<NamespaceDecl*, 8> Queue; |
| 787 | |
| 788 | // We have already looked into the initial namespace; seed the queue |
| 789 | // with its using-children. |
| 790 | for (; I != E; ++I) { |
John McCall | d9f01d4 | 2009-11-10 09:25:37 +0000 | [diff] [blame] | 791 | NamespaceDecl *ND = (*I)->getNominatedNamespace()->getOriginalNamespace(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 792 | if (Visited.insert(ND).second) |
| 793 | Queue.push_back(ND); |
| 794 | } |
| 795 | |
| 796 | // The easiest way to implement the restriction in [namespace.qual]p5 |
| 797 | // is to check whether any of the individual results found a tag |
| 798 | // and, if so, to declare an ambiguity if the final result is not |
| 799 | // a tag. |
| 800 | bool FoundTag = false; |
| 801 | bool FoundNonTag = false; |
| 802 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 803 | LookupResult LocalR(LookupResult::Temporary, R); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 804 | |
| 805 | bool Found = false; |
| 806 | while (!Queue.empty()) { |
| 807 | NamespaceDecl *ND = Queue.back(); |
| 808 | Queue.pop_back(); |
| 809 | |
| 810 | // We go through some convolutions here to avoid copying results |
| 811 | // between LookupResults. |
| 812 | bool UseLocal = !R.empty(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 813 | LookupResult &DirectR = UseLocal ? LocalR : R; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 814 | bool FoundDirect = LookupDirect(DirectR, ND); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 815 | |
| 816 | if (FoundDirect) { |
| 817 | // First do any local hiding. |
| 818 | DirectR.resolveKind(); |
| 819 | |
| 820 | // If the local result is a tag, remember that. |
| 821 | if (DirectR.isSingleTagDecl()) |
| 822 | FoundTag = true; |
| 823 | else |
| 824 | FoundNonTag = true; |
| 825 | |
| 826 | // Append the local results to the total results if necessary. |
| 827 | if (UseLocal) { |
| 828 | R.addAllDecls(LocalR); |
| 829 | LocalR.clear(); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // If we find names in this namespace, ignore its using directives. |
| 834 | if (FoundDirect) { |
| 835 | Found = true; |
| 836 | continue; |
| 837 | } |
| 838 | |
| 839 | for (llvm::tie(I,E) = ND->getUsingDirectives(); I != E; ++I) { |
| 840 | NamespaceDecl *Nom = (*I)->getNominatedNamespace(); |
| 841 | if (Visited.insert(Nom).second) |
| 842 | Queue.push_back(Nom); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | if (Found) { |
| 847 | if (FoundTag && FoundNonTag) |
| 848 | R.setAmbiguousQualifiedTagHiding(); |
| 849 | else |
| 850 | R.resolveKind(); |
| 851 | } |
| 852 | |
| 853 | return Found; |
| 854 | } |
| 855 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 856 | /// @brief Perform qualified name lookup into a given context. |
| 857 | /// |
| 858 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 859 | /// names when the context of those names is explicit specified, e.g., |
| 860 | /// "std::vector" or "x->member". |
| 861 | /// |
| 862 | /// Different lookup criteria can find different names. For example, a |
| 863 | /// particular scope can have both a struct and a function of the same |
| 864 | /// name, and each can be found by certain lookup criteria. For more |
| 865 | /// information about lookup criteria, see the documentation for the |
| 866 | /// class LookupCriteria. |
| 867 | /// |
| 868 | /// @param LookupCtx The context in which qualified name lookup will |
| 869 | /// search. If the lookup criteria permits, name lookup may also search |
| 870 | /// in the parent contexts or (for C++ classes) base classes. |
| 871 | /// |
| 872 | /// @param Name The name of the entity that we are searching for. |
| 873 | /// |
| 874 | /// @param Criteria The criteria that this routine will use to |
| 875 | /// determine which names are visible and which names will be |
| 876 | /// found. Note that name lookup will find a name that is visible by |
| 877 | /// the given criteria, but the entity itself may not be semantically |
| 878 | /// correct or even the kind of entity expected based on the |
| 879 | /// lookup. For example, searching for a nested-name-specifier name |
| 880 | /// might result in an EnumDecl, which is visible but is not permitted |
| 881 | /// as a nested-name-specifier in C++03. |
| 882 | /// |
| 883 | /// @returns The result of name lookup, which includes zero or more |
| 884 | /// declarations and possibly additional information used to diagnose |
| 885 | /// ambiguities. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 886 | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx) { |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 887 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 889 | if (!R.getLookupName()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 890 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 892 | // Make sure that the declaration context is complete. |
| 893 | assert((!isa<TagDecl>(LookupCtx) || |
| 894 | LookupCtx->isDependentContext() || |
| 895 | cast<TagDecl>(LookupCtx)->isDefinition() || |
| 896 | Context.getTypeDeclType(cast<TagDecl>(LookupCtx))->getAs<TagType>() |
| 897 | ->isBeingDefined()) && |
| 898 | "Declaration context must already be complete!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 900 | // Perform qualified name lookup into the LookupCtx. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 901 | if (LookupDirect(R, LookupCtx)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 902 | R.resolveKind(); |
| 903 | return true; |
| 904 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 905 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 906 | // Don't descend into implied contexts for redeclarations. |
| 907 | // C++98 [namespace.qual]p6: |
| 908 | // In a declaration for a namespace member in which the |
| 909 | // declarator-id is a qualified-id, given that the qualified-id |
| 910 | // for the namespace member has the form |
| 911 | // nested-name-specifier unqualified-id |
| 912 | // the unqualified-id shall name a member of the namespace |
| 913 | // designated by the nested-name-specifier. |
| 914 | // See also [class.mfct]p5 and [class.static.data]p2. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 915 | if (R.isForRedeclaration()) |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 916 | return false; |
| 917 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 918 | // If this is a namespace, look it up in the implied namespaces. |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 919 | if (LookupCtx->isFileContext()) |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 920 | return LookupQualifiedNameInUsingDirectives(R, LookupCtx); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 922 | // 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] | 923 | // classes, we're done. |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 924 | if (!isa<CXXRecordDecl>(LookupCtx)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 925 | return false; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 926 | |
| 927 | // Perform lookup into our base classes. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 928 | CXXRecordDecl *LookupRec = cast<CXXRecordDecl>(LookupCtx); |
| 929 | CXXBasePaths Paths; |
| 930 | Paths.setOrigin(LookupRec); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 931 | |
| 932 | // Look for this member in our base classes |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 933 | CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 934 | switch (R.getLookupKind()) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 935 | case LookupOrdinaryName: |
| 936 | case LookupMemberName: |
| 937 | case LookupRedeclarationWithLinkage: |
| 938 | BaseCallback = &CXXRecordDecl::FindOrdinaryMember; |
| 939 | break; |
| 940 | |
| 941 | case LookupTagName: |
| 942 | BaseCallback = &CXXRecordDecl::FindTagMember; |
| 943 | break; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 944 | |
| 945 | case LookupUsingDeclName: |
| 946 | // This lookup is for redeclarations only. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 947 | |
| 948 | case LookupOperatorName: |
| 949 | case LookupNamespaceName: |
| 950 | case LookupObjCProtocolName: |
| 951 | case LookupObjCImplementationName: |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 952 | // 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] | 953 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 954 | |
| 955 | case LookupNestedNameSpecifierName: |
| 956 | BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember; |
| 957 | break; |
| 958 | } |
| 959 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 960 | if (!LookupRec->lookupInBases(BaseCallback, |
| 961 | R.getLookupName().getAsOpaquePtr(), Paths)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 962 | return false; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 963 | |
| 964 | // C++ [class.member.lookup]p2: |
| 965 | // [...] If the resulting set of declarations are not all from |
| 966 | // sub-objects of the same type, or the set has a nonstatic member |
| 967 | // and includes members from distinct sub-objects, there is an |
| 968 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 969 | // the result of the lookup. |
| 970 | // FIXME: support using declarations! |
| 971 | QualType SubobjectType; |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 972 | int SubobjectNumber = 0; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 973 | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 974 | Path != PathEnd; ++Path) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 975 | const CXXBasePathElement &PathElement = Path->back(); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 976 | |
| 977 | // Determine whether we're looking at a distinct sub-object or not. |
| 978 | if (SubobjectType.isNull()) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 979 | // This is the first subobject we've looked at. Record its type. |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 980 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 981 | SubobjectNumber = PathElement.SubobjectNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | } else if (SubobjectType |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 983 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 984 | // We found members of the given name in two subobjects of |
| 985 | // different types. This lookup is ambiguous. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 986 | R.setAmbiguousBaseSubobjectTypes(Paths); |
| 987 | return true; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 988 | } else if (SubobjectNumber != PathElement.SubobjectNumber) { |
| 989 | // We have a different subobject of the same type. |
| 990 | |
| 991 | // C++ [class.member.lookup]p5: |
| 992 | // A static member, a nested type or an enumerator defined in |
| 993 | // a base class T can unambiguously be found even if an object |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | // has more than one base class subobject of type T. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 995 | Decl *FirstDecl = *Path->Decls.first; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 996 | if (isa<VarDecl>(FirstDecl) || |
| 997 | isa<TypeDecl>(FirstDecl) || |
| 998 | isa<EnumConstantDecl>(FirstDecl)) |
| 999 | continue; |
| 1000 | |
| 1001 | if (isa<CXXMethodDecl>(FirstDecl)) { |
| 1002 | // Determine whether all of the methods are static. |
| 1003 | bool AllMethodsAreStatic = true; |
| 1004 | for (DeclContext::lookup_iterator Func = Path->Decls.first; |
| 1005 | Func != Path->Decls.second; ++Func) { |
| 1006 | if (!isa<CXXMethodDecl>(*Func)) { |
| 1007 | assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | if (!cast<CXXMethodDecl>(*Func)->isStatic()) { |
| 1012 | AllMethodsAreStatic = false; |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if (AllMethodsAreStatic) |
| 1018 | continue; |
| 1019 | } |
| 1020 | |
| 1021 | // We have found a nonstatic member name in multiple, distinct |
| 1022 | // subobjects. Name lookup is ambiguous. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1023 | R.setAmbiguousBaseSubobjects(Paths); |
| 1024 | return true; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | // Lookup in a base class succeeded; return these results. |
| 1029 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1030 | DeclContext::lookup_iterator I, E; |
| 1031 | for (llvm::tie(I,E) = Paths.front().Decls; I != E; ++I) |
| 1032 | R.addDecl(*I); |
| 1033 | R.resolveKind(); |
| 1034 | return true; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /// @brief Performs name lookup for a name that was parsed in the |
| 1038 | /// source code, and may contain a C++ scope specifier. |
| 1039 | /// |
| 1040 | /// This routine is a convenience routine meant to be called from |
| 1041 | /// contexts that receive a name and an optional C++ scope specifier |
| 1042 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 1043 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 1044 | /// respectively) on the given name and return those results. |
| 1045 | /// |
| 1046 | /// @param S The scope from which unqualified name lookup will |
| 1047 | /// begin. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | /// |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1049 | /// @param SS An optional C++ scope-specifier, e.g., "::N::M". |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1050 | /// |
| 1051 | /// @param Name The name of the entity that name lookup will |
| 1052 | /// search for. |
| 1053 | /// |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1054 | /// @param Loc If provided, the source location where we're performing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | /// name lookup. At present, this is only used to produce diagnostics when |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1056 | /// C library functions (like "malloc") are implicitly declared. |
| 1057 | /// |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1058 | /// @param EnteringContext Indicates whether we are going to enter the |
| 1059 | /// context of the scope-specifier SS (if present). |
| 1060 | /// |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1061 | /// @returns True if any decls were found (but possibly ambiguous) |
| 1062 | bool Sema::LookupParsedName(LookupResult &R, Scope *S, const CXXScopeSpec *SS, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1063 | bool AllowBuiltinCreation, bool EnteringContext) { |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1064 | if (SS && SS->isInvalid()) { |
| 1065 | // When the scope specifier is invalid, don't even look for |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1066 | // anything. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1067 | return false; |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1068 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1070 | if (SS && SS->isSet()) { |
| 1071 | if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | // We have resolved the scope specifier to a particular declaration |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1073 | // contex, and will perform name lookup in that context. |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1074 | if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1075 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1077 | R.setContextRange(SS->getRange()); |
| 1078 | |
| 1079 | return LookupQualifiedName(R, DC); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1080 | } |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1082 | // We could not resolve the scope specified to a specific declaration |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | // context, which means that SS refers to an unknown specialization. |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1084 | // Name lookup can't find anything in this case. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1085 | return false; |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | // Perform unqualified name lookup starting in the given scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1089 | return LookupName(R, S, AllowBuiltinCreation); |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1092 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1093 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 1094 | /// from name lookup. |
| 1095 | /// |
| 1096 | /// @param Result The ambiguous name lookup result. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | /// |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1098 | /// @param Name The name of the entity that name lookup was |
| 1099 | /// searching for. |
| 1100 | /// |
| 1101 | /// @param NameLoc The location of the name within the source code. |
| 1102 | /// |
| 1103 | /// @param LookupRange A source range that provides more |
| 1104 | /// source-location information concerning the lookup itself. For |
| 1105 | /// example, this range might highlight a nested-name-specifier that |
| 1106 | /// precedes the name. |
| 1107 | /// |
| 1108 | /// @returns true |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1109 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1110 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 1111 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1112 | DeclarationName Name = Result.getLookupName(); |
| 1113 | SourceLocation NameLoc = Result.getNameLoc(); |
| 1114 | SourceRange LookupRange = Result.getContextRange(); |
| 1115 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1116 | switch (Result.getAmbiguityKind()) { |
| 1117 | case LookupResult::AmbiguousBaseSubobjects: { |
| 1118 | CXXBasePaths *Paths = Result.getBasePaths(); |
| 1119 | QualType SubobjectType = Paths->front().back().Base->getType(); |
| 1120 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 1121 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 1122 | << LookupRange; |
| 1123 | |
| 1124 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
| 1125 | while (isa<CXXMethodDecl>(*Found) && |
| 1126 | cast<CXXMethodDecl>(*Found)->isStatic()) |
| 1127 | ++Found; |
| 1128 | |
| 1129 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
| 1130 | |
| 1131 | return true; |
| 1132 | } |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1133 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1134 | case LookupResult::AmbiguousBaseSubobjectTypes: { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1135 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 1136 | << Name << LookupRange; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1137 | |
| 1138 | CXXBasePaths *Paths = Result.getBasePaths(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1139 | std::set<Decl *> DeclsPrinted; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1140 | for (CXXBasePaths::paths_iterator Path = Paths->begin(), |
| 1141 | PathEnd = Paths->end(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1142 | Path != PathEnd; ++Path) { |
| 1143 | Decl *D = *Path->Decls.first; |
| 1144 | if (DeclsPrinted.insert(D).second) |
| 1145 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 1146 | } |
| 1147 | |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1148 | return true; |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1151 | case LookupResult::AmbiguousTagHiding: { |
| 1152 | Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; |
Douglas Gregor | 69d993a | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1153 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1154 | llvm::SmallPtrSet<NamedDecl*,8> TagDecls; |
| 1155 | |
| 1156 | LookupResult::iterator DI, DE = Result.end(); |
| 1157 | for (DI = Result.begin(); DI != DE; ++DI) |
| 1158 | if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) { |
| 1159 | TagDecls.insert(TD); |
| 1160 | Diag(TD->getLocation(), diag::note_hidden_tag); |
| 1161 | } |
| 1162 | |
| 1163 | for (DI = Result.begin(); DI != DE; ++DI) |
| 1164 | if (!isa<TagDecl>(*DI)) |
| 1165 | Diag((*DI)->getLocation(), diag::note_hiding_object); |
| 1166 | |
| 1167 | // For recovery purposes, go ahead and implement the hiding. |
| 1168 | Result.hideDecls(TagDecls); |
| 1169 | |
| 1170 | return true; |
| 1171 | } |
| 1172 | |
| 1173 | case LookupResult::AmbiguousReference: { |
| 1174 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1175 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1176 | LookupResult::iterator DI = Result.begin(), DE = Result.end(); |
| 1177 | for (; DI != DE; ++DI) |
| 1178 | Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1179 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1180 | return true; |
| 1181 | } |
| 1182 | } |
| 1183 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1184 | llvm_unreachable("unknown ambiguity kind"); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1185 | return true; |
| 1186 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1187 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | static void |
| 1189 | addAssociatedClassesAndNamespaces(QualType T, |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1190 | ASTContext &Context, |
| 1191 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1192 | Sema::AssociatedClassSet &AssociatedClasses); |
| 1193 | |
| 1194 | static void CollectNamespace(Sema::AssociatedNamespaceSet &Namespaces, |
| 1195 | DeclContext *Ctx) { |
| 1196 | if (Ctx->isFileContext()) |
| 1197 | Namespaces.insert(Ctx); |
| 1198 | } |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1199 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | // \brief Add the associated classes and namespaces for argument-dependent |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1201 | // lookup that involves a template argument (C++ [basic.lookup.koenig]p2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | static void |
| 1203 | addAssociatedClassesAndNamespaces(const TemplateArgument &Arg, |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1204 | ASTContext &Context, |
| 1205 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1206 | Sema::AssociatedClassSet &AssociatedClasses) { |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1207 | // C++ [basic.lookup.koenig]p2, last bullet: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | // -- [...] ; |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1209 | switch (Arg.getKind()) { |
| 1210 | case TemplateArgument::Null: |
| 1211 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1213 | case TemplateArgument::Type: |
| 1214 | // [...] the namespaces and classes associated with the types of the |
| 1215 | // template arguments provided for template type parameters (excluding |
| 1216 | // template template parameters) |
| 1217 | addAssociatedClassesAndNamespaces(Arg.getAsType(), Context, |
| 1218 | AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1219 | AssociatedClasses); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1220 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1222 | case TemplateArgument::Template: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | // [...] the namespaces in which any template template arguments are |
| 1224 | // defined; and the classes in which any member templates used as |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1225 | // template template arguments are defined. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1226 | TemplateName Template = Arg.getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1228 | = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1229 | DeclContext *Ctx = ClassTemplate->getDeclContext(); |
| 1230 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1231 | AssociatedClasses.insert(EnclosingClass); |
| 1232 | // Add the associated namespace for this class. |
| 1233 | while (Ctx->isRecord()) |
| 1234 | Ctx = Ctx->getParent(); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1235 | CollectNamespace(AssociatedNamespaces, Ctx); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1236 | } |
| 1237 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | case TemplateArgument::Declaration: |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1241 | case TemplateArgument::Integral: |
| 1242 | case TemplateArgument::Expression: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | // [Note: non-type template arguments do not contribute to the set of |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1244 | // associated namespaces. ] |
| 1245 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1247 | case TemplateArgument::Pack: |
| 1248 | for (TemplateArgument::pack_iterator P = Arg.pack_begin(), |
| 1249 | PEnd = Arg.pack_end(); |
| 1250 | P != PEnd; ++P) |
| 1251 | addAssociatedClassesAndNamespaces(*P, Context, |
| 1252 | AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1253 | AssociatedClasses); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1258 | // \brief Add the associated classes and namespaces for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | // argument-dependent lookup with an argument of class type |
| 1260 | // (C++ [basic.lookup.koenig]p2). |
| 1261 | static void |
| 1262 | addAssociatedClassesAndNamespaces(CXXRecordDecl *Class, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1263 | ASTContext &Context, |
| 1264 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1265 | Sema::AssociatedClassSet &AssociatedClasses) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1266 | // C++ [basic.lookup.koenig]p2: |
| 1267 | // [...] |
| 1268 | // -- If T is a class type (including unions), its associated |
| 1269 | // classes are: the class itself; the class of which it is a |
| 1270 | // member, if any; and its direct and indirect base |
| 1271 | // classes. Its associated namespaces are the namespaces in |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | // which its associated classes are defined. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1273 | |
| 1274 | // Add the class of which it is a member, if any. |
| 1275 | DeclContext *Ctx = Class->getDeclContext(); |
| 1276 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1277 | AssociatedClasses.insert(EnclosingClass); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1278 | // Add the associated namespace for this class. |
| 1279 | while (Ctx->isRecord()) |
| 1280 | Ctx = Ctx->getParent(); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1281 | CollectNamespace(AssociatedNamespaces, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1282 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1283 | // Add the class itself. If we've already seen this class, we don't |
| 1284 | // need to visit base classes. |
| 1285 | if (!AssociatedClasses.insert(Class)) |
| 1286 | return; |
| 1287 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | // -- If T is a template-id, its associated namespaces and classes are |
| 1289 | // the namespace in which the template is defined; for member |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1290 | // templates, the member template’s class; the namespaces and classes |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | // associated with the types of the template arguments provided for |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1292 | // template type parameters (excluding template template parameters); the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | // namespaces in which any template template arguments are defined; and |
| 1294 | // the classes in which any member templates used as template template |
| 1295 | // arguments are defined. [Note: non-type template arguments do not |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1296 | // contribute to the set of associated namespaces. ] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1298 | = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { |
| 1299 | DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); |
| 1300 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1301 | AssociatedClasses.insert(EnclosingClass); |
| 1302 | // Add the associated namespace for this class. |
| 1303 | while (Ctx->isRecord()) |
| 1304 | Ctx = Ctx->getParent(); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1305 | CollectNamespace(AssociatedNamespaces, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1307 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 1308 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 1309 | addAssociatedClassesAndNamespaces(TemplateArgs[I], Context, |
| 1310 | AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1311 | AssociatedClasses); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1314 | // Add direct and indirect base classes along with their associated |
| 1315 | // namespaces. |
| 1316 | llvm::SmallVector<CXXRecordDecl *, 32> Bases; |
| 1317 | Bases.push_back(Class); |
| 1318 | while (!Bases.empty()) { |
| 1319 | // Pop this class off the stack. |
| 1320 | Class = Bases.back(); |
| 1321 | Bases.pop_back(); |
| 1322 | |
| 1323 | // Visit the base classes. |
| 1324 | for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), |
| 1325 | BaseEnd = Class->bases_end(); |
| 1326 | Base != BaseEnd; ++Base) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1327 | const RecordType *BaseType = Base->getType()->getAs<RecordType>(); |
Sebastian Redl | bbc1cc5 | 2009-10-25 09:35:33 +0000 | [diff] [blame] | 1328 | // In dependent contexts, we do ADL twice, and the first time around, |
| 1329 | // the base type might be a dependent TemplateSpecializationType, or a |
| 1330 | // TemplateTypeParmType. If that happens, simply ignore it. |
| 1331 | // FIXME: If we want to support export, we probably need to add the |
| 1332 | // namespace of the template in a TemplateSpecializationType, or even |
| 1333 | // the classes and namespaces of known non-dependent arguments. |
| 1334 | if (!BaseType) |
| 1335 | continue; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1336 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 1337 | if (AssociatedClasses.insert(BaseDecl)) { |
| 1338 | // Find the associated namespace for this base class. |
| 1339 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
| 1340 | while (BaseCtx->isRecord()) |
| 1341 | BaseCtx = BaseCtx->getParent(); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1342 | CollectNamespace(AssociatedNamespaces, BaseCtx); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Make sure we visit the bases of this base class. |
| 1345 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
| 1346 | Bases.push_back(BaseDecl); |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | // \brief Add the associated classes and namespaces for |
| 1353 | // argument-dependent lookup with an argument of type T |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | // (C++ [basic.lookup.koenig]p2). |
| 1355 | static void |
| 1356 | addAssociatedClassesAndNamespaces(QualType T, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1357 | ASTContext &Context, |
| 1358 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1359 | Sema::AssociatedClassSet &AssociatedClasses) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1360 | // C++ [basic.lookup.koenig]p2: |
| 1361 | // |
| 1362 | // For each argument type T in the function call, there is a set |
| 1363 | // of zero or more associated namespaces and a set of zero or more |
| 1364 | // associated classes to be considered. The sets of namespaces and |
| 1365 | // classes is determined entirely by the types of the function |
| 1366 | // arguments (and the namespace of any template template |
| 1367 | // argument). Typedef names and using-declarations used to specify |
| 1368 | // the types do not contribute to this set. The sets of namespaces |
| 1369 | // and classes are determined in the following way: |
| 1370 | T = Context.getCanonicalType(T).getUnqualifiedType(); |
| 1371 | |
| 1372 | // -- 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] | 1373 | // namespaces and classes are those associated with U. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1374 | // |
| 1375 | // We handle this by unwrapping pointer and array types immediately, |
| 1376 | // to avoid unnecessary recursion. |
| 1377 | while (true) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1378 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1379 | T = Ptr->getPointeeType(); |
| 1380 | else if (const ArrayType *Ptr = Context.getAsArrayType(T)) |
| 1381 | T = Ptr->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | else |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1383 | break; |
| 1384 | } |
| 1385 | |
| 1386 | // -- If T is a fundamental type, its associated sets of |
| 1387 | // namespaces and classes are both empty. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1388 | if (T->getAs<BuiltinType>()) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1389 | return; |
| 1390 | |
| 1391 | // -- If T is a class type (including unions), its associated |
| 1392 | // classes are: the class itself; the class of which it is a |
| 1393 | // member, if any; and its direct and indirect base |
| 1394 | // classes. Its associated namespaces are the namespaces in |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | // which its associated classes are defined. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1396 | if (const RecordType *ClassType = T->getAs<RecordType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | if (CXXRecordDecl *ClassDecl |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1398 | = dyn_cast<CXXRecordDecl>(ClassType->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | addAssociatedClassesAndNamespaces(ClassDecl, Context, |
| 1400 | AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1401 | AssociatedClasses); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1402 | return; |
| 1403 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1404 | |
| 1405 | // -- If T is an enumeration type, its associated namespace is |
| 1406 | // the namespace in which it is defined. If it is class |
| 1407 | // member, its associated class is the member’s class; else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | // it has no associated class. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1409 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1410 | EnumDecl *Enum = EnumT->getDecl(); |
| 1411 | |
| 1412 | DeclContext *Ctx = Enum->getDeclContext(); |
| 1413 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1414 | AssociatedClasses.insert(EnclosingClass); |
| 1415 | |
| 1416 | // Add the associated namespace for this class. |
| 1417 | while (Ctx->isRecord()) |
| 1418 | Ctx = Ctx->getParent(); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1419 | CollectNamespace(AssociatedNamespaces, Ctx); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1420 | |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | // -- If T is a function type, its associated namespaces and |
| 1425 | // classes are those associated with the function parameter |
| 1426 | // types and those associated with the return type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1427 | if (const FunctionType *FnType = T->getAs<FunctionType>()) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1428 | // Return type |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1429 | addAssociatedClassesAndNamespaces(FnType->getResultType(), |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1430 | Context, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1431 | AssociatedNamespaces, AssociatedClasses); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1432 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1433 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1434 | if (!Proto) |
| 1435 | return; |
| 1436 | |
| 1437 | // Argument types |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1438 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | ArgEnd = Proto->arg_type_end(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1440 | Arg != ArgEnd; ++Arg) |
| 1441 | addAssociatedClassesAndNamespaces(*Arg, Context, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1442 | AssociatedNamespaces, AssociatedClasses); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | // -- If T is a pointer to a member function of a class X, its |
| 1448 | // associated namespaces and classes are those associated |
| 1449 | // with the function parameter types and return type, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | // together with those associated with X. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1451 | // |
| 1452 | // -- If T is a pointer to a data member of class X, its |
| 1453 | // associated namespaces and classes are those associated |
| 1454 | // with the member type together with those associated with |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | // X. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1456 | if (const MemberPointerType *MemberPtr = T->getAs<MemberPointerType>()) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1457 | // Handle the type that the pointer to member points to. |
| 1458 | addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(), |
| 1459 | Context, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1460 | AssociatedNamespaces, |
| 1461 | AssociatedClasses); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Handle the class type into which this points. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1464 | if (const RecordType *Class = MemberPtr->getClass()->getAs<RecordType>()) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1465 | addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()), |
| 1466 | Context, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1467 | AssociatedNamespaces, |
| 1468 | AssociatedClasses); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1469 | |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | // FIXME: What about block pointers? |
| 1474 | // FIXME: What about Objective-C message sends? |
| 1475 | } |
| 1476 | |
| 1477 | /// \brief Find the associated classes and namespaces for |
| 1478 | /// argument-dependent lookup for a call with the given set of |
| 1479 | /// arguments. |
| 1480 | /// |
| 1481 | /// This routine computes the sets of associated classes and associated |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | /// namespaces searched by argument-dependent lookup |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1483 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1485 | Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, |
| 1486 | AssociatedNamespaceSet &AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1487 | AssociatedClassSet &AssociatedClasses) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1488 | AssociatedNamespaces.clear(); |
| 1489 | AssociatedClasses.clear(); |
| 1490 | |
| 1491 | // C++ [basic.lookup.koenig]p2: |
| 1492 | // For each argument type T in the function call, there is a set |
| 1493 | // of zero or more associated namespaces and a set of zero or more |
| 1494 | // associated classes to be considered. The sets of namespaces and |
| 1495 | // classes is determined entirely by the types of the function |
| 1496 | // arguments (and the namespace of any template template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | // argument). |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1498 | for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) { |
| 1499 | Expr *Arg = Args[ArgIdx]; |
| 1500 | |
| 1501 | if (Arg->getType() != Context.OverloadTy) { |
| 1502 | addAssociatedClassesAndNamespaces(Arg->getType(), Context, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1503 | AssociatedNamespaces, |
| 1504 | AssociatedClasses); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1505 | continue; |
| 1506 | } |
| 1507 | |
| 1508 | // [...] In addition, if the argument is the name or address of a |
| 1509 | // set of overloaded functions and/or function templates, its |
| 1510 | // associated classes and namespaces are the union of those |
| 1511 | // associated with each of the members of the set: the namespace |
| 1512 | // in which the function or function template is defined and the |
| 1513 | // classes and namespaces associated with its (non-dependent) |
| 1514 | // parameter types and return type. |
Douglas Gregor | daa439a | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 1515 | Arg = Arg->IgnoreParens(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1516 | if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) |
| 1517 | if (unaryOp->getOpcode() == UnaryOperator::AddrOf) |
| 1518 | Arg = unaryOp->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1520 | // TODO: avoid the copies. This should be easy when the cases |
| 1521 | // share a storage implementation. |
| 1522 | llvm::SmallVector<NamedDecl*, 8> Functions; |
| 1523 | |
| 1524 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg)) |
| 1525 | Functions.append(ULE->decls_begin(), ULE->decls_end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1526 | else |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1527 | continue; |
| 1528 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1529 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Functions.begin(), |
| 1530 | E = Functions.end(); I != E; ++I) { |
| 1531 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*I); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1532 | if (!FDecl) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1533 | FDecl = cast<FunctionTemplateDecl>(*I)->getTemplatedDecl(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1534 | |
| 1535 | // Add the namespace in which this function was defined. Note |
| 1536 | // that, if this is a member function, we do *not* consider the |
| 1537 | // enclosing namespace of its class. |
| 1538 | DeclContext *Ctx = FDecl->getDeclContext(); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1539 | CollectNamespace(AssociatedNamespaces, Ctx); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1540 | |
| 1541 | // Add the classes and namespaces associated with the parameter |
| 1542 | // types and return type of this function. |
| 1543 | addAssociatedClassesAndNamespaces(FDecl->getType(), Context, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1544 | AssociatedNamespaces, |
| 1545 | AssociatedClasses); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1549 | |
| 1550 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 1551 | /// an acceptable non-member overloaded operator for a call whose |
| 1552 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 1553 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 1554 | /// enumeration types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | static bool |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1556 | IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, |
| 1557 | QualType T1, QualType T2, |
| 1558 | ASTContext &Context) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1559 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
| 1560 | return true; |
| 1561 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1562 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 1563 | return true; |
| 1564 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1565 | const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1566 | if (Proto->getNumArgs() < 1) |
| 1567 | return false; |
| 1568 | |
| 1569 | if (T1->isEnumeralType()) { |
| 1570 | QualType ArgType = Proto->getArgType(0).getNonReferenceType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1571 | if (Context.hasSameUnqualifiedType(T1, ArgType)) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1572 | return true; |
| 1573 | } |
| 1574 | |
| 1575 | if (Proto->getNumArgs() < 2) |
| 1576 | return false; |
| 1577 | |
| 1578 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 1579 | QualType ArgType = Proto->getArgType(1).getNonReferenceType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1580 | if (Context.hasSameUnqualifiedType(T2, ArgType)) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1581 | return true; |
| 1582 | } |
| 1583 | |
| 1584 | return false; |
| 1585 | } |
| 1586 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1587 | NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, |
| 1588 | LookupNameKind NameKind, |
| 1589 | RedeclarationKind Redecl) { |
| 1590 | LookupResult R(*this, Name, SourceLocation(), NameKind, Redecl); |
| 1591 | LookupName(R, S); |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 1592 | return R.getAsSingle<NamedDecl>(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1595 | /// \brief Find the protocol with the given name, if any. |
| 1596 | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1597 | Decl *D = LookupSingleName(TUScope, II, LookupObjCProtocolName); |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1598 | return cast_or_null<ObjCProtocolDecl>(D); |
| 1599 | } |
| 1600 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1601 | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | QualType T1, QualType T2, |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1603 | FunctionSet &Functions) { |
| 1604 | // C++ [over.match.oper]p3: |
| 1605 | // -- The set of non-member candidates is the result of the |
| 1606 | // unqualified lookup of operator@ in the context of the |
| 1607 | // expression according to the usual rules for name lookup in |
| 1608 | // unqualified function calls (3.4.2) except that all member |
| 1609 | // functions are ignored. However, if no operand has a class |
| 1610 | // type, only those non-member functions in the lookup set |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1611 | // that have a first parameter of type T1 or "reference to |
| 1612 | // (possibly cv-qualified) T1", when T1 is an enumeration |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1613 | // type, or (if there is a right operand) a second parameter |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1614 | // of type T2 or "reference to (possibly cv-qualified) T2", |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1615 | // when T2 is an enumeration type, are candidate functions. |
| 1616 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1617 | LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); |
| 1618 | LookupName(Operators, S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1620 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
| 1621 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1622 | if (Operators.empty()) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1623 | return; |
| 1624 | |
| 1625 | for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end(); |
| 1626 | Op != OpEnd; ++Op) { |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 1627 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Op)) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1628 | if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) |
| 1629 | Functions.insert(FD); // FIXME: canonical FD |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | } else if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 1631 | = dyn_cast<FunctionTemplateDecl>(*Op)) { |
| 1632 | // FIXME: friend operators? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 1634 | // later? |
| 1635 | if (!FunTmpl->getDeclContext()->isRecord()) |
| 1636 | Functions.insert(FunTmpl); |
| 1637 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1641 | static void CollectFunctionDecl(Sema::FunctionSet &Functions, |
| 1642 | Decl *D) { |
| 1643 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) |
| 1644 | Functions.insert(Func); |
| 1645 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 1646 | Functions.insert(FunTmpl); |
| 1647 | } |
| 1648 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 1649 | void Sema::ArgumentDependentLookup(DeclarationName Name, bool Operator, |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1650 | Expr **Args, unsigned NumArgs, |
| 1651 | FunctionSet &Functions) { |
| 1652 | // Find all of the associated namespaces and classes based on the |
| 1653 | // arguments we have. |
| 1654 | AssociatedNamespaceSet AssociatedNamespaces; |
| 1655 | AssociatedClassSet AssociatedClasses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | FindAssociatedClassesAndNamespaces(Args, NumArgs, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1657 | AssociatedNamespaces, |
| 1658 | AssociatedClasses); |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1659 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 1660 | QualType T1, T2; |
| 1661 | if (Operator) { |
| 1662 | T1 = Args[0]->getType(); |
| 1663 | if (NumArgs >= 2) |
| 1664 | T2 = Args[1]->getType(); |
| 1665 | } |
| 1666 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1667 | // C++ [basic.lookup.argdep]p3: |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1668 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 1669 | // and let Y be the lookup set produced by argument dependent |
| 1670 | // lookup (defined as follows). If X contains [...] then Y is |
| 1671 | // empty. Otherwise Y is the set of declarations found in the |
| 1672 | // namespaces associated with the argument types as described |
| 1673 | // below. The set of declarations found by the lookup of the name |
| 1674 | // is the union of X and Y. |
| 1675 | // |
| 1676 | // Here, we compute Y and add its members to the overloaded |
| 1677 | // candidate set. |
| 1678 | for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | NSEnd = AssociatedNamespaces.end(); |
| 1680 | NS != NSEnd; ++NS) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1681 | // When considering an associated namespace, the lookup is the |
| 1682 | // same as the lookup performed when the associated namespace is |
| 1683 | // used as a qualifier (3.4.3.2) except that: |
| 1684 | // |
| 1685 | // -- Any using-directives in the associated namespace are |
| 1686 | // ignored. |
| 1687 | // |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1688 | // -- Any namespace-scope friend functions declared in |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1689 | // associated classes are visible within their respective |
| 1690 | // namespaces even if they are not visible during an ordinary |
| 1691 | // lookup (11.4). |
| 1692 | DeclContext::lookup_iterator I, E; |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1693 | for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) { |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1694 | Decl *D = *I; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1695 | // If the only declaration here is an ordinary friend, consider |
| 1696 | // it only if it was declared in an associated classes. |
| 1697 | if (D->getIdentifierNamespace() == Decl::IDNS_OrdinaryFriend) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1698 | DeclContext *LexDC = D->getLexicalDeclContext(); |
| 1699 | if (!AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) |
| 1700 | continue; |
| 1701 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 1703 | FunctionDecl *Fn; |
| 1704 | if (!Operator || !(Fn = dyn_cast<FunctionDecl>(D)) || |
| 1705 | IsAcceptableNonMemberOperatorCandidate(Fn, T1, T2, Context)) |
| 1706 | CollectFunctionDecl(Functions, D); |
Douglas Gregor | 44bc2d5 | 2009-06-23 20:14:09 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1709 | } |