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