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