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