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