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 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Sema.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 20 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 23 | #include "clang/AST/Decl.h" |
| 24 | #include "clang/AST/DeclCXX.h" |
| 25 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 27 | #include "clang/AST/Expr.h" |
Douglas Gregor | daa439a | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Builtins.h" |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 30 | #include "clang/Basic/LangOptions.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringMap.h" |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 36 | #include <limits> |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 37 | #include <list> |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 38 | #include <set> |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 39 | #include <vector> |
| 40 | #include <iterator> |
| 41 | #include <utility> |
| 42 | #include <algorithm> |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 45 | using namespace sema; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 46 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | class UnqualUsingEntry { |
| 49 | const DeclContext *Nominated; |
| 50 | const DeclContext *CommonAncestor; |
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 | public: |
| 53 | UnqualUsingEntry(const DeclContext *Nominated, |
| 54 | const DeclContext *CommonAncestor) |
| 55 | : Nominated(Nominated), CommonAncestor(CommonAncestor) { |
| 56 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 57 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 58 | const DeclContext *getCommonAncestor() const { |
| 59 | return CommonAncestor; |
| 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 | const DeclContext *getNominatedNamespace() const { |
| 63 | return Nominated; |
| 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 | // Sort by the pointer value of the common ancestor. |
| 67 | struct Comparator { |
| 68 | bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { |
| 69 | return L.getCommonAncestor() < R.getCommonAncestor(); |
| 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 | bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { |
| 73 | return E.getCommonAncestor() < DC; |
| 74 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 75 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 76 | bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { |
| 77 | return DC < E.getCommonAncestor(); |
| 78 | } |
| 79 | }; |
| 80 | }; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 81 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 82 | /// A collection of using directives, as used by C++ unqualified |
| 83 | /// lookup. |
| 84 | class UnqualUsingDirectiveSet { |
| 85 | typedef llvm::SmallVector<UnqualUsingEntry, 8> ListTy; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 86 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 87 | ListTy list; |
| 88 | llvm::SmallPtrSet<DeclContext*, 8> visited; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 89 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 90 | public: |
| 91 | UnqualUsingDirectiveSet() {} |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 92 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 93 | void visitScopeChain(Scope *S, Scope *InnermostFileScope) { |
| 94 | // C++ [namespace.udir]p1: |
| 95 | // During unqualified name lookup, the names appear as if they |
| 96 | // were declared in the nearest enclosing namespace which contains |
| 97 | // both the using-directive and the nominated namespace. |
| 98 | DeclContext *InnermostFileDC |
| 99 | = static_cast<DeclContext*>(InnermostFileScope->getEntity()); |
| 100 | assert(InnermostFileDC && InnermostFileDC->isFileContext()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 101 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 102 | for (; S; S = S->getParent()) { |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 103 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 104 | DeclContext *EffectiveDC = (Ctx->isFileContext() ? Ctx : InnermostFileDC); |
| 105 | visit(Ctx, EffectiveDC); |
| 106 | } else { |
| 107 | Scope::udir_iterator I = S->using_directives_begin(), |
| 108 | End = S->using_directives_end(); |
| 109 | |
| 110 | for (; I != End; ++I) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 111 | visit(*I, InnermostFileDC); |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 112 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 115 | |
| 116 | // Visits a context and collect all of its using directives |
| 117 | // recursively. Treats all using directives as if they were |
| 118 | // declared in the context. |
| 119 | // |
| 120 | // A given context is only every visited once, so it is important |
| 121 | // that contexts be visited from the inside out in order to get |
| 122 | // the effective DCs right. |
| 123 | void visit(DeclContext *DC, DeclContext *EffectiveDC) { |
| 124 | if (!visited.insert(DC)) |
| 125 | return; |
| 126 | |
| 127 | addUsingDirectives(DC, EffectiveDC); |
| 128 | } |
| 129 | |
| 130 | // Visits a using directive and collects all of its using |
| 131 | // directives recursively. Treats all using directives as if they |
| 132 | // were declared in the effective DC. |
| 133 | void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 134 | DeclContext *NS = UD->getNominatedNamespace(); |
| 135 | if (!visited.insert(NS)) |
| 136 | return; |
| 137 | |
| 138 | addUsingDirective(UD, EffectiveDC); |
| 139 | addUsingDirectives(NS, EffectiveDC); |
| 140 | } |
| 141 | |
| 142 | // Adds all the using directives in a context (and those nominated |
| 143 | // by its using directives, transitively) as if they appeared in |
| 144 | // the given effective context. |
| 145 | void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { |
| 146 | llvm::SmallVector<DeclContext*,4> queue; |
| 147 | while (true) { |
| 148 | DeclContext::udir_iterator I, End; |
| 149 | for (llvm::tie(I, End) = DC->getUsingDirectives(); I != End; ++I) { |
| 150 | UsingDirectiveDecl *UD = *I; |
| 151 | DeclContext *NS = UD->getNominatedNamespace(); |
| 152 | if (visited.insert(NS)) { |
| 153 | addUsingDirective(UD, EffectiveDC); |
| 154 | queue.push_back(NS); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (queue.empty()) |
| 159 | return; |
| 160 | |
| 161 | DC = queue.back(); |
| 162 | queue.pop_back(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Add a using directive as if it had been declared in the given |
| 167 | // context. This helps implement C++ [namespace.udir]p3: |
| 168 | // The using-directive is transitive: if a scope contains a |
| 169 | // using-directive that nominates a second namespace that itself |
| 170 | // contains using-directives, the effect is as if the |
| 171 | // using-directives from the second namespace also appeared in |
| 172 | // the first. |
| 173 | void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 174 | // Find the common ancestor between the effective context and |
| 175 | // the nominated namespace. |
| 176 | DeclContext *Common = UD->getNominatedNamespace(); |
| 177 | while (!Common->Encloses(EffectiveDC)) |
| 178 | Common = Common->getParent(); |
John McCall | 12ea578 | 2009-11-10 09:20:04 +0000 | [diff] [blame] | 179 | Common = Common->getPrimaryContext(); |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 180 | |
| 181 | list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); |
| 182 | } |
| 183 | |
| 184 | void done() { |
| 185 | std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator()); |
| 186 | } |
| 187 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 188 | typedef ListTy::const_iterator const_iterator; |
| 189 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 190 | const_iterator begin() const { return list.begin(); } |
| 191 | const_iterator end() const { return list.end(); } |
| 192 | |
| 193 | std::pair<const_iterator,const_iterator> |
| 194 | getNamespacesFor(DeclContext *DC) const { |
John McCall | 12ea578 | 2009-11-10 09:20:04 +0000 | [diff] [blame] | 195 | return std::equal_range(begin(), end(), DC->getPrimaryContext(), |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 196 | UnqualUsingEntry::Comparator()); |
| 197 | } |
| 198 | }; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 201 | // Retrieve the set of identifier namespaces that correspond to a |
| 202 | // specific kind of name lookup. |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 203 | static inline unsigned getIDNS(Sema::LookupNameKind NameKind, |
| 204 | bool CPlusPlus, |
| 205 | bool Redeclaration) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 206 | unsigned IDNS = 0; |
| 207 | switch (NameKind) { |
| 208 | case Sema::LookupOrdinaryName: |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 209 | case Sema::LookupRedeclarationWithLinkage: |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 210 | IDNS = Decl::IDNS_Ordinary; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 211 | if (CPlusPlus) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 212 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 213 | if (Redeclaration) IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; |
| 214 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 215 | break; |
| 216 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 217 | case Sema::LookupOperatorName: |
| 218 | // Operator lookup is its own crazy thing; it is not the same |
| 219 | // as (e.g.) looking up an operator name for redeclaration. |
| 220 | assert(!Redeclaration && "cannot do redeclaration operator lookup"); |
| 221 | IDNS = Decl::IDNS_NonMemberOperator; |
| 222 | break; |
| 223 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 224 | case Sema::LookupTagName: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 225 | if (CPlusPlus) { |
| 226 | IDNS = Decl::IDNS_Type; |
| 227 | |
| 228 | // When looking for a redeclaration of a tag name, we add: |
| 229 | // 1) TagFriend to find undeclared friend decls |
| 230 | // 2) Namespace because they can't "overload" with tag decls. |
| 231 | // 3) Tag because it includes class templates, which can't |
| 232 | // "overload" with tag decls. |
| 233 | if (Redeclaration) |
| 234 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; |
| 235 | } else { |
| 236 | IDNS = Decl::IDNS_Tag; |
| 237 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 238 | break; |
| 239 | |
| 240 | case Sema::LookupMemberName: |
| 241 | IDNS = Decl::IDNS_Member; |
| 242 | if (CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case Sema::LookupNestedNameSpecifierName: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 247 | IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; |
| 248 | break; |
| 249 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 250 | case Sema::LookupNamespaceName: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 251 | IDNS = Decl::IDNS_Namespace; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 252 | break; |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 253 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 254 | case Sema::LookupUsingDeclName: |
| 255 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag |
| 256 | | Decl::IDNS_Member | Decl::IDNS_Using; |
| 257 | break; |
| 258 | |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 259 | case Sema::LookupObjCProtocolName: |
| 260 | IDNS = Decl::IDNS_ObjCProtocol; |
| 261 | break; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 262 | |
| 263 | case Sema::LookupAnyName: |
| 264 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member |
| 265 | | Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol |
| 266 | | Decl::IDNS_Type; |
| 267 | break; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 268 | } |
| 269 | return IDNS; |
| 270 | } |
| 271 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 272 | void LookupResult::configure() { |
| 273 | IDNS = getIDNS(LookupKind, |
| 274 | SemaRef.getLangOptions().CPlusPlus, |
| 275 | isForRedeclaration()); |
Douglas Gregor | b5b2ccb | 2010-03-24 05:07:21 +0000 | [diff] [blame] | 276 | |
| 277 | // If we're looking for one of the allocation or deallocation |
| 278 | // operators, make sure that the implicitly-declared new and delete |
| 279 | // operators can be found. |
| 280 | if (!isForRedeclaration()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 281 | switch (NameInfo.getName().getCXXOverloadedOperator()) { |
Douglas Gregor | b5b2ccb | 2010-03-24 05:07:21 +0000 | [diff] [blame] | 282 | case OO_New: |
| 283 | case OO_Delete: |
| 284 | case OO_Array_New: |
| 285 | case OO_Array_Delete: |
| 286 | SemaRef.DeclareGlobalNewDelete(); |
| 287 | break; |
| 288 | |
| 289 | default: |
| 290 | break; |
| 291 | } |
| 292 | } |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 293 | } |
| 294 | |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 295 | #ifndef NDEBUG |
| 296 | void LookupResult::sanity() const { |
| 297 | assert(ResultKind != NotFound || Decls.size() == 0); |
| 298 | assert(ResultKind != Found || Decls.size() == 1); |
| 299 | assert(ResultKind != FoundOverloaded || Decls.size() > 1 || |
| 300 | (Decls.size() == 1 && |
| 301 | isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))); |
| 302 | assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved()); |
| 303 | assert(ResultKind != Ambiguous || Decls.size() > 1 || |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 304 | (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || |
| 305 | Ambiguity == AmbiguousBaseSubobjectTypes))); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 306 | assert((Paths != NULL) == (ResultKind == Ambiguous && |
| 307 | (Ambiguity == AmbiguousBaseSubobjectTypes || |
| 308 | Ambiguity == AmbiguousBaseSubobjects))); |
| 309 | } |
| 310 | #endif |
| 311 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 312 | // Necessary because CXXBasePaths is not complete in Sema.h |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 313 | void LookupResult::deletePaths(CXXBasePaths *Paths) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 314 | delete Paths; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 315 | } |
| 316 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 317 | /// Resolves the result kind of this lookup. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 318 | void LookupResult::resolveKind() { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 319 | unsigned N = Decls.size(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 320 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 321 | // Fast case: no possible ambiguity. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 322 | if (N == 0) { |
John McCall | dc5c786 | 2010-01-15 21:27:01 +0000 | [diff] [blame] | 323 | assert(ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 327 | // If there's a single decl, we need to examine it to decide what |
| 328 | // kind of lookup this is. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 329 | if (N == 1) { |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 330 | NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); |
| 331 | if (isa<FunctionTemplateDecl>(D)) |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 332 | ResultKind = FoundOverloaded; |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 333 | else if (isa<UnresolvedUsingValueDecl>(D)) |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 334 | ResultKind = FoundUnresolvedValue; |
| 335 | return; |
| 336 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 337 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 338 | // 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] | 339 | if (ResultKind == Ambiguous) return; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 340 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 341 | llvm::SmallPtrSet<NamedDecl*, 16> Unique; |
Douglas Gregor | 7f1c547 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 342 | llvm::SmallPtrSet<QualType, 16> UniqueTypes; |
| 343 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 344 | bool Ambiguous = false; |
| 345 | bool HasTag = false, HasFunction = false, HasNonFunction = false; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 346 | bool HasFunctionTemplate = false, HasUnresolved = false; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 347 | |
| 348 | unsigned UniqueTagIndex = 0; |
| 349 | |
| 350 | unsigned I = 0; |
| 351 | while (I < N) { |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 352 | NamedDecl *D = Decls[I]->getUnderlyingDecl(); |
| 353 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | 7f1c547 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 355 | // Redeclarations of types via typedef can occur both within a scope |
| 356 | // and, through using declarations and directives, across scopes. There is |
| 357 | // no ambiguity if they all refer to the same type, so unique based on the |
| 358 | // canonical type. |
| 359 | if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { |
| 360 | if (!TD->getDeclContext()->isRecord()) { |
| 361 | QualType T = SemaRef.Context.getTypeDeclType(TD); |
| 362 | if (!UniqueTypes.insert(SemaRef.Context.getCanonicalType(T))) { |
| 363 | // The type is not unique; pull something off the back and continue |
| 364 | // at this index. |
| 365 | Decls[I] = Decls[--N]; |
| 366 | continue; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 371 | if (!Unique.insert(D)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 372 | // If it's not unique, pull something off the back (and |
| 373 | // continue at this index). |
| 374 | Decls[I] = Decls[--N]; |
Douglas Gregor | 7f1c547 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 375 | continue; |
| 376 | } |
| 377 | |
| 378 | // Otherwise, do some decl type analysis and then continue. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | 7f1c547 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 380 | if (isa<UnresolvedUsingValueDecl>(D)) { |
| 381 | HasUnresolved = true; |
| 382 | } else if (isa<TagDecl>(D)) { |
| 383 | if (HasTag) |
| 384 | Ambiguous = true; |
| 385 | UniqueTagIndex = I; |
| 386 | HasTag = true; |
| 387 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 388 | HasFunction = true; |
| 389 | HasFunctionTemplate = true; |
| 390 | } else if (isa<FunctionDecl>(D)) { |
| 391 | HasFunction = true; |
| 392 | } else { |
| 393 | if (HasNonFunction) |
| 394 | Ambiguous = true; |
| 395 | HasNonFunction = true; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 396 | } |
Douglas Gregor | 7f1c547 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 397 | I++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | } |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 399 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 400 | // C++ [basic.scope.hiding]p2: |
| 401 | // A class name or enumeration name can be hidden by the name of |
| 402 | // an object, function, or enumerator declared in the same |
| 403 | // scope. If a class or enumeration name and an object, function, |
| 404 | // or enumerator are declared in the same scope (in any order) |
| 405 | // with the same name, the class or enumeration name is hidden |
| 406 | // wherever the object, function, or enumerator name is visible. |
| 407 | // But it's still an error if there are distinct tag types found, |
| 408 | // even if they're not visible. (ref?) |
John McCall | fda8e12 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 409 | if (HideTags && HasTag && !Ambiguous && |
Douglas Gregor | 77a1a88 | 2010-10-23 16:06:17 +0000 | [diff] [blame] | 410 | (HasFunction || HasNonFunction || HasUnresolved)) { |
| 411 | if (Decls[UniqueTagIndex]->getDeclContext()->getRedeclContext()->Equals( |
| 412 | Decls[UniqueTagIndex? 0 : N-1]->getDeclContext()->getRedeclContext())) |
| 413 | Decls[UniqueTagIndex] = Decls[--N]; |
| 414 | else |
| 415 | Ambiguous = true; |
| 416 | } |
Anders Carlsson | 8b50d01 | 2009-06-26 03:37:05 +0000 | [diff] [blame] | 417 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 418 | Decls.set_size(N); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 419 | |
John McCall | fda8e12 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 420 | if (HasNonFunction && (HasFunction || HasUnresolved)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 421 | Ambiguous = true; |
Douglas Gregor | 69d993a | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 422 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 423 | if (Ambiguous) |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 424 | setAmbiguous(LookupResult::AmbiguousReference); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 425 | else if (HasUnresolved) |
| 426 | ResultKind = LookupResult::FoundUnresolvedValue; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 427 | else if (N > 1 || HasFunctionTemplate) |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 428 | ResultKind = LookupResult::FoundOverloaded; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 429 | else |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 430 | ResultKind = LookupResult::Found; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 431 | } |
| 432 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 433 | void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 434 | CXXBasePaths::const_paths_iterator I, E; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 435 | DeclContext::lookup_iterator DI, DE; |
| 436 | for (I = P.begin(), E = P.end(); I != E; ++I) |
| 437 | for (llvm::tie(DI,DE) = I->Decls; DI != DE; ++DI) |
| 438 | addDecl(*DI); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 439 | } |
| 440 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 441 | void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 442 | Paths = new CXXBasePaths; |
| 443 | Paths->swap(P); |
| 444 | addDeclsFromBasePaths(*Paths); |
| 445 | resolveKind(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 446 | setAmbiguous(AmbiguousBaseSubobjects); |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 447 | } |
| 448 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 449 | void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 450 | Paths = new CXXBasePaths; |
| 451 | Paths->swap(P); |
| 452 | addDeclsFromBasePaths(*Paths); |
| 453 | resolveKind(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 454 | setAmbiguous(AmbiguousBaseSubobjectTypes); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 455 | } |
| 456 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 457 | void LookupResult::print(llvm::raw_ostream &Out) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 458 | Out << Decls.size() << " result(s)"; |
| 459 | if (isAmbiguous()) Out << ", ambiguous"; |
| 460 | if (Paths) Out << ", base paths present"; |
| 461 | |
| 462 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 463 | Out << "\n"; |
| 464 | (*I)->print(Out, 2); |
| 465 | } |
| 466 | } |
| 467 | |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 468 | /// \brief Lookup a builtin function, when name lookup would otherwise |
| 469 | /// fail. |
| 470 | static bool LookupBuiltin(Sema &S, LookupResult &R) { |
| 471 | Sema::LookupNameKind NameKind = R.getLookupKind(); |
| 472 | |
| 473 | // If we didn't find a use of this identifier, and if the identifier |
| 474 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 475 | // now, injecting it into translation unit scope, and return it. |
| 476 | if (NameKind == Sema::LookupOrdinaryName || |
| 477 | NameKind == Sema::LookupRedeclarationWithLinkage) { |
| 478 | IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); |
| 479 | if (II) { |
| 480 | // If this is a builtin on this (or all) targets, create the decl. |
| 481 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 482 | // In C++, we don't have any predefined library functions like |
| 483 | // 'malloc'. Instead, we'll just error. |
| 484 | if (S.getLangOptions().CPlusPlus && |
| 485 | S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 486 | return false; |
| 487 | |
| 488 | NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 489 | S.TUScope, R.isForRedeclaration(), |
| 490 | R.getNameLoc()); |
| 491 | if (D) |
| 492 | R.addDecl(D); |
| 493 | return (D != NULL); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return false; |
| 499 | } |
| 500 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 501 | /// \brief Determine whether we can declare a special member function within |
| 502 | /// the class at this point. |
| 503 | static bool CanDeclareSpecialMemberFunction(ASTContext &Context, |
| 504 | const CXXRecordDecl *Class) { |
John McCall | b3b50a8 | 2010-08-11 23:52:36 +0000 | [diff] [blame] | 505 | // Don't do it if the class is invalid. |
| 506 | if (Class->isInvalidDecl()) |
| 507 | return false; |
| 508 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 509 | // We need to have a definition for the class. |
| 510 | if (!Class->getDefinition() || Class->isDependentContext()) |
| 511 | return false; |
| 512 | |
| 513 | // We can't be in the middle of defining the class. |
| 514 | if (const RecordType *RecordTy |
| 515 | = Context.getTypeDeclType(Class)->getAs<RecordType>()) |
| 516 | return !RecordTy->isBeingDefined(); |
| 517 | |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | void Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 522 | if (!CanDeclareSpecialMemberFunction(Context, Class)) |
| 523 | return; |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 524 | |
| 525 | // If the default constructor has not yet been declared, do so now. |
| 526 | if (!Class->hasDeclaredDefaultConstructor()) |
| 527 | DeclareImplicitDefaultConstructor(Class); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 528 | |
| 529 | // If the copy constructor has not yet been declared, do so now. |
| 530 | if (!Class->hasDeclaredCopyConstructor()) |
| 531 | DeclareImplicitCopyConstructor(Class); |
| 532 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 533 | // If the copy assignment operator has not yet been declared, do so now. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 534 | if (!Class->hasDeclaredCopyAssignment()) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 535 | DeclareImplicitCopyAssignment(Class); |
| 536 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 537 | // If the destructor has not yet been declared, do so now. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 538 | if (!Class->hasDeclaredDestructor()) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 539 | DeclareImplicitDestructor(Class); |
| 540 | } |
| 541 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 542 | /// \brief Determine whether this is the name of an implicitly-declared |
| 543 | /// special member function. |
| 544 | static bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) { |
| 545 | switch (Name.getNameKind()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 546 | case DeclarationName::CXXConstructorName: |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 547 | case DeclarationName::CXXDestructorName: |
| 548 | return true; |
| 549 | |
| 550 | case DeclarationName::CXXOperatorName: |
| 551 | return Name.getCXXOverloadedOperator() == OO_Equal; |
| 552 | |
| 553 | default: |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | /// \brief If there are any implicit member functions with the given name |
| 561 | /// that need to be declared in the given declaration context, do so. |
| 562 | static void DeclareImplicitMemberFunctionsWithName(Sema &S, |
| 563 | DeclarationName Name, |
| 564 | const DeclContext *DC) { |
| 565 | if (!DC) |
| 566 | return; |
| 567 | |
| 568 | switch (Name.getNameKind()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 569 | case DeclarationName::CXXConstructorName: |
| 570 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 571 | if (Record->getDefinition() && |
| 572 | CanDeclareSpecialMemberFunction(S.Context, Record)) { |
| 573 | if (!Record->hasDeclaredDefaultConstructor()) |
| 574 | S.DeclareImplicitDefaultConstructor( |
| 575 | const_cast<CXXRecordDecl *>(Record)); |
| 576 | if (!Record->hasDeclaredCopyConstructor()) |
| 577 | S.DeclareImplicitCopyConstructor(const_cast<CXXRecordDecl *>(Record)); |
| 578 | } |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 579 | break; |
| 580 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 581 | case DeclarationName::CXXDestructorName: |
| 582 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
| 583 | if (Record->getDefinition() && !Record->hasDeclaredDestructor() && |
| 584 | CanDeclareSpecialMemberFunction(S.Context, Record)) |
| 585 | S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record)); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 586 | break; |
| 587 | |
| 588 | case DeclarationName::CXXOperatorName: |
| 589 | if (Name.getCXXOverloadedOperator() != OO_Equal) |
| 590 | break; |
| 591 | |
| 592 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
| 593 | if (Record->getDefinition() && !Record->hasDeclaredCopyAssignment() && |
| 594 | CanDeclareSpecialMemberFunction(S.Context, Record)) |
| 595 | S.DeclareImplicitCopyAssignment(const_cast<CXXRecordDecl *>(Record)); |
| 596 | break; |
| 597 | |
| 598 | default: |
| 599 | break; |
| 600 | } |
| 601 | } |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 602 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 603 | // Adds all qualifying matches for a name within a decl context to the |
| 604 | // given lookup result. Returns true if any matches were found. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 605 | static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 606 | bool Found = false; |
| 607 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 608 | // Lazily declare C++ special member functions. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 609 | if (S.getLangOptions().CPlusPlus) |
| 610 | DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), DC); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 611 | |
| 612 | // Perform lookup into this declaration context. |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 613 | DeclContext::lookup_const_iterator I, E; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 614 | for (llvm::tie(I, E) = DC->lookup(R.getLookupName()); I != E; ++I) { |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 615 | NamedDecl *D = *I; |
| 616 | if (R.isAcceptableDecl(D)) { |
| 617 | R.addDecl(D); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 618 | Found = true; |
| 619 | } |
| 620 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 621 | |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 622 | if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R)) |
| 623 | return true; |
| 624 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 625 | if (R.getLookupName().getNameKind() |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 626 | != DeclarationName::CXXConversionFunctionName || |
| 627 | R.getLookupName().getCXXNameType()->isDependentType() || |
| 628 | !isa<CXXRecordDecl>(DC)) |
| 629 | return Found; |
| 630 | |
| 631 | // C++ [temp.mem]p6: |
| 632 | // A specialization of a conversion function template is not found by |
| 633 | // name lookup. Instead, any conversion function templates visible in the |
| 634 | // context of the use are considered. [...] |
| 635 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 636 | if (!Record->isDefinition()) |
| 637 | return Found; |
| 638 | |
| 639 | const UnresolvedSetImpl *Unresolved = Record->getConversionFunctions(); |
| 640 | for (UnresolvedSetImpl::iterator U = Unresolved->begin(), |
| 641 | UEnd = Unresolved->end(); U != UEnd; ++U) { |
| 642 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); |
| 643 | if (!ConvTemplate) |
| 644 | continue; |
| 645 | |
| 646 | // When we're performing lookup for the purposes of redeclaration, just |
| 647 | // add the conversion function template. When we deduce template |
| 648 | // arguments for specializations, we'll end up unifying the return |
| 649 | // type of the new declaration with the type of the function template. |
| 650 | if (R.isForRedeclaration()) { |
| 651 | R.addDecl(ConvTemplate); |
| 652 | Found = true; |
| 653 | continue; |
| 654 | } |
| 655 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 656 | // C++ [temp.mem]p6: |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 657 | // [...] For each such operator, if argument deduction succeeds |
| 658 | // (14.9.2.3), the resulting specialization is used as if found by |
| 659 | // name lookup. |
| 660 | // |
| 661 | // When referencing a conversion function for any purpose other than |
| 662 | // a redeclaration (such that we'll be building an expression with the |
| 663 | // result), perform template argument deduction and place the |
| 664 | // specialization into the result set. We do this to avoid forcing all |
| 665 | // callers to perform special deduction for conversion functions. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 666 | TemplateDeductionInfo Info(R.getSema().Context, R.getNameLoc()); |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 667 | FunctionDecl *Specialization = 0; |
| 668 | |
| 669 | const FunctionProtoType *ConvProto |
| 670 | = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); |
| 671 | assert(ConvProto && "Nonsensical conversion function template type"); |
Douglas Gregor | 3f477a1 | 2010-01-12 01:17:50 +0000 | [diff] [blame] | 672 | |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 673 | // Compute the type of the function that we would expect the conversion |
| 674 | // function to have, if it were to match the name given. |
| 675 | // FIXME: Calling convention! |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 676 | FunctionType::ExtInfo ConvProtoInfo = ConvProto->getExtInfo(); |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 677 | QualType ExpectedType |
| 678 | = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(), |
| 679 | 0, 0, ConvProto->isVariadic(), |
| 680 | ConvProto->getTypeQuals(), |
| 681 | false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 682 | ConvProtoInfo.withCallingConv(CC_Default)); |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 683 | |
| 684 | // Perform template argument deduction against the type that we would |
| 685 | // expect the function to have. |
| 686 | if (R.getSema().DeduceTemplateArguments(ConvTemplate, 0, ExpectedType, |
| 687 | Specialization, Info) |
| 688 | == Sema::TDK_Success) { |
| 689 | R.addDecl(Specialization); |
| 690 | Found = true; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 691 | } |
| 692 | } |
Chandler Carruth | aaa1a89 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 693 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 694 | return Found; |
| 695 | } |
| 696 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 697 | // Performs C++ unqualified lookup into the given file context. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 698 | static bool |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 699 | CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, |
| 700 | DeclContext *NS, UnqualUsingDirectiveSet &UDirs) { |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 701 | |
| 702 | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); |
| 703 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 704 | // Perform direct name lookup into the LookupCtx. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 705 | bool Found = LookupDirect(S, R, NS); |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 706 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 707 | // Perform direct name lookup into the namespaces nominated by the |
| 708 | // using directives whose common ancestor is this namespace. |
| 709 | UnqualUsingDirectiveSet::const_iterator UI, UEnd; |
| 710 | llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(NS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 712 | for (; UI != UEnd; ++UI) |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 713 | if (LookupDirect(S, R, UI->getNominatedNamespace())) |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 714 | Found = true; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 715 | |
| 716 | R.resolveKind(); |
| 717 | |
| 718 | return Found; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static bool isNamespaceOrTranslationUnitScope(Scope *S) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 722 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 723 | return Ctx->isFileContext(); |
| 724 | return false; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 725 | } |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 727 | // Find the next outer declaration context from this scope. This |
| 728 | // routine actually returns the semantic outer context, which may |
| 729 | // differ from the lexical context (encoded directly in the Scope |
| 730 | // stack) when we are parsing a member of a class template. In this |
| 731 | // case, the second element of the pair will be true, to indicate that |
| 732 | // name lookup should continue searching in this semantic context when |
| 733 | // it leaves the current template parameter scope. |
| 734 | static std::pair<DeclContext *, bool> findOuterContext(Scope *S) { |
| 735 | DeclContext *DC = static_cast<DeclContext *>(S->getEntity()); |
| 736 | DeclContext *Lexical = 0; |
| 737 | for (Scope *OuterS = S->getParent(); OuterS; |
| 738 | OuterS = OuterS->getParent()) { |
| 739 | if (OuterS->getEntity()) { |
Douglas Gregor | dbdf5e7 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 740 | Lexical = static_cast<DeclContext *>(OuterS->getEntity()); |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // C++ [temp.local]p8: |
| 746 | // In the definition of a member of a class template that appears |
| 747 | // outside of the namespace containing the class template |
| 748 | // definition, the name of a template-parameter hides the name of |
| 749 | // a member of this namespace. |
| 750 | // |
| 751 | // Example: |
| 752 | // |
| 753 | // namespace N { |
| 754 | // class C { }; |
| 755 | // |
| 756 | // template<class T> class B { |
| 757 | // void f(T); |
| 758 | // }; |
| 759 | // } |
| 760 | // |
| 761 | // template<class C> void N::B<C>::f(C) { |
| 762 | // C b; // C is the template parameter, not N::C |
| 763 | // } |
| 764 | // |
| 765 | // In this example, the lexical context we return is the |
| 766 | // TranslationUnit, while the semantic context is the namespace N. |
| 767 | if (!Lexical || !DC || !S->getParent() || |
| 768 | !S->getParent()->isTemplateParamScope()) |
| 769 | return std::make_pair(Lexical, false); |
| 770 | |
| 771 | // Find the outermost template parameter scope. |
| 772 | // For the example, this is the scope for the template parameters of |
| 773 | // template<class C>. |
| 774 | Scope *OutermostTemplateScope = S->getParent(); |
| 775 | while (OutermostTemplateScope->getParent() && |
| 776 | OutermostTemplateScope->getParent()->isTemplateParamScope()) |
| 777 | OutermostTemplateScope = OutermostTemplateScope->getParent(); |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 779 | // Find the namespace context in which the original scope occurs. In |
| 780 | // the example, this is namespace N. |
| 781 | DeclContext *Semantic = DC; |
| 782 | while (!Semantic->isFileContext()) |
| 783 | Semantic = Semantic->getParent(); |
| 784 | |
| 785 | // Find the declaration context just outside of the template |
| 786 | // parameter scope. This is the context in which the template is |
| 787 | // being lexically declaration (a namespace context). In the |
| 788 | // example, this is the global scope. |
| 789 | if (Lexical->isFileContext() && !Lexical->Equals(Semantic) && |
| 790 | Lexical->Encloses(Semantic)) |
| 791 | return std::make_pair(Semantic, true); |
| 792 | |
| 793 | return std::make_pair(Lexical, false); |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 794 | } |
| 795 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 796 | bool Sema::CppLookupName(LookupResult &R, Scope *S) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 797 | assert(getLangOptions().CPlusPlus && "Can perform only C++ lookup"); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 798 | |
| 799 | DeclarationName Name = R.getLookupName(); |
| 800 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 801 | // If this is the name of an implicitly-declared special member function, |
| 802 | // go through the scope stack to implicitly declare |
| 803 | if (isImplicitlyDeclaredMemberFunctionName(Name)) { |
| 804 | for (Scope *PreS = S; PreS; PreS = PreS->getParent()) |
| 805 | if (DeclContext *DC = static_cast<DeclContext *>(PreS->getEntity())) |
| 806 | DeclareImplicitMemberFunctionsWithName(*this, Name, DC); |
| 807 | } |
| 808 | |
| 809 | // Implicitly declare member functions with the name we're looking for, if in |
| 810 | // fact we are in a scope where it matters. |
| 811 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 812 | Scope *Initial = S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | IdentifierResolver::iterator |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 814 | I = IdResolver.begin(Name), |
| 815 | IEnd = IdResolver.end(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 816 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 817 | // First we lookup local scope. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 818 | // 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] | 819 | // ...During unqualified name lookup (3.4.1), the names appear as if |
| 820 | // they were declared in the nearest enclosing namespace which contains |
| 821 | // both the using-directive and the nominated namespace. |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 822 | // [Note: in this context, "contains" means "contains directly or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | // indirectly". |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 824 | // |
| 825 | // For example: |
| 826 | // namespace A { int i; } |
| 827 | // void foo() { |
| 828 | // int i; |
| 829 | // { |
| 830 | // using namespace A; |
| 831 | // ++i; // finds local 'i', A::i appears at global scope |
| 832 | // } |
| 833 | // } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 834 | // |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 835 | DeclContext *OutsideOfTemplateParamDC = 0; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 836 | for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 837 | DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()); |
| 838 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 839 | // Check whether the IdResolver has anything in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 840 | bool Found = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 841 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 842 | if (R.isAcceptableDecl(*I)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 843 | Found = true; |
| 844 | R.addDecl(*I); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 847 | if (Found) { |
| 848 | R.resolveKind(); |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 849 | if (S->isClassScope()) |
| 850 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
| 851 | R.setNamingClass(Record); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 852 | return true; |
| 853 | } |
| 854 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 855 | if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && |
| 856 | S->getParent() && !S->getParent()->isTemplateParamScope()) { |
| 857 | // We've just searched the last template parameter scope and |
| 858 | // found nothing, so look into the the contexts between the |
| 859 | // lexical and semantic declaration contexts returned by |
| 860 | // findOuterContext(). This implements the name lookup behavior |
| 861 | // of C++ [temp.local]p8. |
| 862 | Ctx = OutsideOfTemplateParamDC; |
| 863 | OutsideOfTemplateParamDC = 0; |
| 864 | } |
| 865 | |
| 866 | if (Ctx) { |
| 867 | DeclContext *OuterCtx; |
| 868 | bool SearchAfterTemplateScope; |
| 869 | llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); |
| 870 | if (SearchAfterTemplateScope) |
| 871 | OutsideOfTemplateParamDC = OuterCtx; |
| 872 | |
Douglas Gregor | dbdf5e7 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 873 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { |
Douglas Gregor | 36262b8 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 874 | // We do not directly look into transparent contexts, since |
| 875 | // those entities will be found in the nearest enclosing |
| 876 | // non-transparent context. |
| 877 | if (Ctx->isTransparentContext()) |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 878 | continue; |
Douglas Gregor | 36262b8 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 879 | |
| 880 | // We do not look directly into function or method contexts, |
| 881 | // since all of the local variables and parameters of the |
| 882 | // function/method are present within the Scope. |
| 883 | if (Ctx->isFunctionOrMethod()) { |
| 884 | // If we have an Objective-C instance method, look for ivars |
| 885 | // in the corresponding interface. |
| 886 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
| 887 | if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()) |
| 888 | if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { |
| 889 | ObjCInterfaceDecl *ClassDeclared; |
| 890 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( |
| 891 | Name.getAsIdentifierInfo(), |
| 892 | ClassDeclared)) { |
| 893 | if (R.isAcceptableDecl(Ivar)) { |
| 894 | R.addDecl(Ivar); |
| 895 | R.resolveKind(); |
| 896 | return true; |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | continue; |
| 903 | } |
| 904 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 905 | // Perform qualified name lookup into this context. |
| 906 | // FIXME: In some cases, we know that every name that could be found by |
| 907 | // this qualified name lookup will also be on the identifier chain. For |
| 908 | // example, inside a class without any base classes, we never need to |
| 909 | // perform qualified lookup because all of the members are on top of the |
| 910 | // identifier chain. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 911 | if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 912 | return true; |
Douglas Gregor | 551f48c | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 913 | } |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 914 | } |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 915 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 916 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 917 | // Stop if we ran out of scopes. |
| 918 | // FIXME: This really, really shouldn't be happening. |
| 919 | if (!S) return false; |
| 920 | |
Argyrios Kyrtzidis | 78f5911 | 2010-10-29 16:12:50 +0000 | [diff] [blame] | 921 | // If we are looking for members, no need to look into global/namespace scope. |
| 922 | if (R.getLookupKind() == LookupMemberName) |
| 923 | return false; |
| 924 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 925 | // Collect UsingDirectiveDecls in all scopes, and recursively all |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 926 | // nominated namespaces by those using-directives. |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 927 | // |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 928 | // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we |
| 929 | // don't build it for each lookup! |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 930 | |
John McCall | d7be78a | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 931 | UnqualUsingDirectiveSet UDirs; |
| 932 | UDirs.visitScopeChain(Initial, S); |
| 933 | UDirs.done(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 935 | // Lookup namespace scope, and global scope. |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 936 | // Unqualified name lookup in C++ requires looking into scopes |
| 937 | // that aren't strictly lexical, and therefore we walk through the |
| 938 | // context as well as walking through the scopes. |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 940 | for (; S; S = S->getParent()) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 941 | // Check whether the IdResolver has anything in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 942 | bool Found = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 943 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 944 | if (R.isAcceptableDecl(*I)) { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 945 | // We found something. Look for anything else in our scope |
| 946 | // with this same name and in an acceptable identifier |
| 947 | // namespace, so that we can construct an overload set if we |
| 948 | // need to. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 949 | Found = true; |
| 950 | R.addDecl(*I); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
Douglas Gregor | 00b4b03 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 954 | if (Found && S->isTemplateParamScope()) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 955 | R.resolveKind(); |
| 956 | return true; |
| 957 | } |
| 958 | |
Douglas Gregor | 00b4b03 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 959 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 960 | if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && |
| 961 | S->getParent() && !S->getParent()->isTemplateParamScope()) { |
| 962 | // We've just searched the last template parameter scope and |
| 963 | // found nothing, so look into the the contexts between the |
| 964 | // lexical and semantic declaration contexts returned by |
| 965 | // findOuterContext(). This implements the name lookup behavior |
| 966 | // of C++ [temp.local]p8. |
| 967 | Ctx = OutsideOfTemplateParamDC; |
| 968 | OutsideOfTemplateParamDC = 0; |
| 969 | } |
| 970 | |
| 971 | if (Ctx) { |
| 972 | DeclContext *OuterCtx; |
| 973 | bool SearchAfterTemplateScope; |
| 974 | llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); |
| 975 | if (SearchAfterTemplateScope) |
| 976 | OutsideOfTemplateParamDC = OuterCtx; |
| 977 | |
| 978 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { |
| 979 | // We do not directly look into transparent contexts, since |
| 980 | // those entities will be found in the nearest enclosing |
| 981 | // non-transparent context. |
| 982 | if (Ctx->isTransparentContext()) |
| 983 | continue; |
| 984 | |
| 985 | // If we have a context, and it's not a context stashed in the |
| 986 | // template parameter scope for an out-of-line definition, also |
| 987 | // look into that context. |
| 988 | if (!(Found && S && S->isTemplateParamScope())) { |
| 989 | assert(Ctx->isFileContext() && |
| 990 | "We should have been looking only at file context here already."); |
| 991 | |
| 992 | // Look into context considering using-directives. |
| 993 | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) |
| 994 | Found = true; |
| 995 | } |
| 996 | |
| 997 | if (Found) { |
| 998 | R.resolveKind(); |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | if (R.isForRedeclaration() && !Ctx->isTransparentContext()) |
| 1003 | return false; |
| 1004 | } |
| 1005 | } |
| 1006 | |
Douglas Gregor | 1df0ee9 | 2010-02-05 07:07:10 +0000 | [diff] [blame] | 1007 | if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1008 | return false; |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 1009 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1010 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1011 | return !R.empty(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1014 | /// @brief Perform unqualified name lookup starting from a given |
| 1015 | /// scope. |
| 1016 | /// |
| 1017 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 1018 | /// used to find names within the current scope. For example, 'x' in |
| 1019 | /// @code |
| 1020 | /// int x; |
| 1021 | /// int f() { |
| 1022 | /// return x; // unqualified name look finds 'x' in the global scope |
| 1023 | /// } |
| 1024 | /// @endcode |
| 1025 | /// |
| 1026 | /// Different lookup criteria can find different names. For example, a |
| 1027 | /// particular scope can have both a struct and a function of the same |
| 1028 | /// name, and each can be found by certain lookup criteria. For more |
| 1029 | /// information about lookup criteria, see the documentation for the |
| 1030 | /// class LookupCriteria. |
| 1031 | /// |
| 1032 | /// @param S The scope from which unqualified name lookup will |
| 1033 | /// begin. If the lookup criteria permits, name lookup may also search |
| 1034 | /// in the parent scopes. |
| 1035 | /// |
| 1036 | /// @param Name The name of the entity that we are searching for. |
| 1037 | /// |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1038 | /// @param Loc If provided, the source location where we're performing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | /// name lookup. At present, this is only used to produce diagnostics when |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1040 | /// C library functions (like "malloc") are implicitly declared. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1041 | /// |
| 1042 | /// @returns The result of name lookup, which includes zero or more |
| 1043 | /// declarations and possibly additional information used to diagnose |
| 1044 | /// ambiguities. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1045 | bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) { |
| 1046 | DeclarationName Name = R.getLookupName(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1047 | if (!Name) return false; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1048 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1049 | LookupNameKind NameKind = R.getLookupKind(); |
| 1050 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1051 | if (!getLangOptions().CPlusPlus) { |
| 1052 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 1053 | // search in the declarations attached to the name. |
| 1054 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 1055 | if (NameKind == Sema::LookupRedeclarationWithLinkage) { |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1056 | // Find the nearest non-transparent declaration scope. |
| 1057 | while (!(S->getFlags() & Scope::DeclScope) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | (S->getEntity() && |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1059 | static_cast<DeclContext *>(S->getEntity()) |
| 1060 | ->isTransparentContext())) |
| 1061 | S = S->getParent(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 1064 | unsigned IDNS = R.getIdentifierNamespace(); |
| 1065 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1066 | // Scan up the scope chain looking for a decl that matches this |
| 1067 | // identifier that is in the appropriate namespace. This search |
| 1068 | // should not take long, as shadowing of names is uncommon, and |
| 1069 | // deep shadowing is extremely uncommon. |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1070 | bool LeftStartingScope = false; |
| 1071 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1072 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | IEnd = IdResolver.end(); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1074 | I != IEnd; ++I) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1075 | if ((*I)->isInIdentifierNamespace(IDNS)) { |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1076 | if (NameKind == LookupRedeclarationWithLinkage) { |
| 1077 | // Determine whether this (or a previous) declaration is |
| 1078 | // out-of-scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1079 | if (!LeftStartingScope && !S->isDeclScope(*I)) |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1080 | LeftStartingScope = true; |
| 1081 | |
| 1082 | // If we found something outside of our starting scope that |
| 1083 | // does not have linkage, skip it. |
| 1084 | if (LeftStartingScope && !((*I)->hasLinkage())) |
| 1085 | continue; |
| 1086 | } |
| 1087 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1088 | R.addDecl(*I); |
| 1089 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1090 | if ((*I)->getAttr<OverloadableAttr>()) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1091 | // If this declaration has the "overloadable" attribute, we |
| 1092 | // might have a set of overloaded functions. |
| 1093 | |
| 1094 | // Figure out what scope the identifier is in. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1095 | while (!(S->getFlags() & Scope::DeclScope) || |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1096 | !S->isDeclScope(*I)) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1097 | S = S->getParent(); |
| 1098 | |
| 1099 | // Find the last declaration in this scope (with the same |
| 1100 | // name, naturally). |
| 1101 | IdentifierResolver::iterator LastI = I; |
| 1102 | for (++LastI; LastI != IEnd; ++LastI) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1103 | if (!S->isDeclScope(*LastI)) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1104 | break; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1105 | R.addDecl(*LastI); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1106 | } |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1109 | R.resolveKind(); |
| 1110 | |
| 1111 | return true; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1112 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1113 | } else { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1114 | // Perform C++ unqualified name lookup. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1115 | if (CppLookupName(R, S)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1116 | return true; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | // If we didn't find a use of this identifier, and if the identifier |
| 1120 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 1121 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1122 | if (AllowBuiltinCreation) |
| 1123 | return LookupBuiltin(*this, R); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1124 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1125 | return false; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1128 | /// @brief Perform qualified name lookup in the namespaces nominated by |
| 1129 | /// using directives by the given context. |
| 1130 | /// |
| 1131 | /// C++98 [namespace.qual]p2: |
| 1132 | /// Given X::m (where X is a user-declared namespace), or given ::m |
| 1133 | /// (where X is the global namespace), let S be the set of all |
| 1134 | /// declarations of m in X and in the transitive closure of all |
| 1135 | /// namespaces nominated by using-directives in X and its used |
| 1136 | /// namespaces, except that using-directives are ignored in any |
| 1137 | /// namespace, including X, directly containing one or more |
| 1138 | /// declarations of m. No namespace is searched more than once in |
| 1139 | /// the lookup of a name. If S is the empty set, the program is |
| 1140 | /// ill-formed. Otherwise, if S has exactly one member, or if the |
| 1141 | /// context of the reference is a using-declaration |
| 1142 | /// (namespace.udecl), S is the required set of declarations of |
| 1143 | /// m. Otherwise if the use of m is not one that allows a unique |
| 1144 | /// declaration to be chosen from S, the program is ill-formed. |
| 1145 | /// C++98 [namespace.qual]p5: |
| 1146 | /// During the lookup of a qualified namespace member name, if the |
| 1147 | /// lookup finds more than one declaration of the member, and if one |
| 1148 | /// declaration introduces a class name or enumeration name and the |
| 1149 | /// other declarations either introduce the same object, the same |
| 1150 | /// enumerator or a set of functions, the non-type name hides the |
| 1151 | /// class or enumeration name if and only if the declarations are |
| 1152 | /// from the same namespace; otherwise (the declarations are from |
| 1153 | /// different namespaces), the program is ill-formed. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1154 | static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1155 | DeclContext *StartDC) { |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1156 | assert(StartDC->isFileContext() && "start context is not a file context"); |
| 1157 | |
| 1158 | DeclContext::udir_iterator I = StartDC->using_directives_begin(); |
| 1159 | DeclContext::udir_iterator E = StartDC->using_directives_end(); |
| 1160 | |
| 1161 | if (I == E) return false; |
| 1162 | |
| 1163 | // We have at least added all these contexts to the queue. |
| 1164 | llvm::DenseSet<DeclContext*> Visited; |
| 1165 | Visited.insert(StartDC); |
| 1166 | |
| 1167 | // We have not yet looked into these namespaces, much less added |
| 1168 | // their "using-children" to the queue. |
| 1169 | llvm::SmallVector<NamespaceDecl*, 8> Queue; |
| 1170 | |
| 1171 | // We have already looked into the initial namespace; seed the queue |
| 1172 | // with its using-children. |
| 1173 | for (; I != E; ++I) { |
John McCall | d9f01d4 | 2009-11-10 09:25:37 +0000 | [diff] [blame] | 1174 | NamespaceDecl *ND = (*I)->getNominatedNamespace()->getOriginalNamespace(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1175 | if (Visited.insert(ND).second) |
| 1176 | Queue.push_back(ND); |
| 1177 | } |
| 1178 | |
| 1179 | // The easiest way to implement the restriction in [namespace.qual]p5 |
| 1180 | // is to check whether any of the individual results found a tag |
| 1181 | // and, if so, to declare an ambiguity if the final result is not |
| 1182 | // a tag. |
| 1183 | bool FoundTag = false; |
| 1184 | bool FoundNonTag = false; |
| 1185 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1186 | LookupResult LocalR(LookupResult::Temporary, R); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1187 | |
| 1188 | bool Found = false; |
| 1189 | while (!Queue.empty()) { |
| 1190 | NamespaceDecl *ND = Queue.back(); |
| 1191 | Queue.pop_back(); |
| 1192 | |
| 1193 | // We go through some convolutions here to avoid copying results |
| 1194 | // between LookupResults. |
| 1195 | bool UseLocal = !R.empty(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1196 | LookupResult &DirectR = UseLocal ? LocalR : R; |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1197 | bool FoundDirect = LookupDirect(S, DirectR, ND); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (FoundDirect) { |
| 1200 | // First do any local hiding. |
| 1201 | DirectR.resolveKind(); |
| 1202 | |
| 1203 | // If the local result is a tag, remember that. |
| 1204 | if (DirectR.isSingleTagDecl()) |
| 1205 | FoundTag = true; |
| 1206 | else |
| 1207 | FoundNonTag = true; |
| 1208 | |
| 1209 | // Append the local results to the total results if necessary. |
| 1210 | if (UseLocal) { |
| 1211 | R.addAllDecls(LocalR); |
| 1212 | LocalR.clear(); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // If we find names in this namespace, ignore its using directives. |
| 1217 | if (FoundDirect) { |
| 1218 | Found = true; |
| 1219 | continue; |
| 1220 | } |
| 1221 | |
| 1222 | for (llvm::tie(I,E) = ND->getUsingDirectives(); I != E; ++I) { |
| 1223 | NamespaceDecl *Nom = (*I)->getNominatedNamespace(); |
| 1224 | if (Visited.insert(Nom).second) |
| 1225 | Queue.push_back(Nom); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | if (Found) { |
| 1230 | if (FoundTag && FoundNonTag) |
| 1231 | R.setAmbiguousQualifiedTagHiding(); |
| 1232 | else |
| 1233 | R.resolveKind(); |
| 1234 | } |
| 1235 | |
| 1236 | return Found; |
| 1237 | } |
| 1238 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1239 | /// \brief Callback that looks for any member of a class with the given name. |
| 1240 | static bool LookupAnyMember(const CXXBaseSpecifier *Specifier, |
| 1241 | CXXBasePath &Path, |
| 1242 | void *Name) { |
| 1243 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 1244 | |
| 1245 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 1246 | Path.Decls = BaseRecord->lookup(N); |
| 1247 | return Path.Decls.first != Path.Decls.second; |
| 1248 | } |
| 1249 | |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1250 | /// \brief Determine whether the given set of member declarations contains only |
| 1251 | /// static members, nested types, and enumerators. |
| 1252 | template<typename InputIterator> |
| 1253 | static bool HasOnlyStaticMembers(InputIterator First, InputIterator Last) { |
| 1254 | Decl *D = (*First)->getUnderlyingDecl(); |
| 1255 | if (isa<VarDecl>(D) || isa<TypeDecl>(D) || isa<EnumConstantDecl>(D)) |
| 1256 | return true; |
| 1257 | |
| 1258 | if (isa<CXXMethodDecl>(D)) { |
| 1259 | // Determine whether all of the methods are static. |
| 1260 | bool AllMethodsAreStatic = true; |
| 1261 | for(; First != Last; ++First) { |
| 1262 | D = (*First)->getUnderlyingDecl(); |
| 1263 | |
| 1264 | if (!isa<CXXMethodDecl>(D)) { |
| 1265 | assert(isa<TagDecl>(D) && "Non-function must be a tag decl"); |
| 1266 | break; |
| 1267 | } |
| 1268 | |
| 1269 | if (!cast<CXXMethodDecl>(D)->isStatic()) { |
| 1270 | AllMethodsAreStatic = false; |
| 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | if (AllMethodsAreStatic) |
| 1276 | return true; |
| 1277 | } |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1282 | /// \brief Perform qualified name lookup into a given context. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1283 | /// |
| 1284 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 1285 | /// names when the context of those names is explicit specified, e.g., |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1286 | /// "std::vector" or "x->member", or as part of unqualified name lookup. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1287 | /// |
| 1288 | /// Different lookup criteria can find different names. For example, a |
| 1289 | /// particular scope can have both a struct and a function of the same |
| 1290 | /// name, and each can be found by certain lookup criteria. For more |
| 1291 | /// information about lookup criteria, see the documentation for the |
| 1292 | /// class LookupCriteria. |
| 1293 | /// |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1294 | /// \param R captures both the lookup criteria and any lookup results found. |
| 1295 | /// |
| 1296 | /// \param LookupCtx The context in which qualified name lookup will |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1297 | /// search. If the lookup criteria permits, name lookup may also search |
| 1298 | /// in the parent contexts or (for C++ classes) base classes. |
| 1299 | /// |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1300 | /// \param InUnqualifiedLookup true if this is qualified name lookup that |
| 1301 | /// occurs as part of unqualified name lookup. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1302 | /// |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1303 | /// \returns true if lookup succeeded, false if it failed. |
| 1304 | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, |
| 1305 | bool InUnqualifiedLookup) { |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1306 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1308 | if (!R.getLookupName()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1309 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1311 | // Make sure that the declaration context is complete. |
| 1312 | assert((!isa<TagDecl>(LookupCtx) || |
| 1313 | LookupCtx->isDependentContext() || |
| 1314 | cast<TagDecl>(LookupCtx)->isDefinition() || |
| 1315 | Context.getTypeDeclType(cast<TagDecl>(LookupCtx))->getAs<TagType>() |
| 1316 | ->isBeingDefined()) && |
| 1317 | "Declaration context must already be complete!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1319 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1320 | if (LookupDirect(*this, R, LookupCtx)) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1321 | R.resolveKind(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1322 | if (isa<CXXRecordDecl>(LookupCtx)) |
| 1323 | R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1324 | return true; |
| 1325 | } |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1326 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1327 | // Don't descend into implied contexts for redeclarations. |
| 1328 | // C++98 [namespace.qual]p6: |
| 1329 | // In a declaration for a namespace member in which the |
| 1330 | // declarator-id is a qualified-id, given that the qualified-id |
| 1331 | // for the namespace member has the form |
| 1332 | // nested-name-specifier unqualified-id |
| 1333 | // the unqualified-id shall name a member of the namespace |
| 1334 | // designated by the nested-name-specifier. |
| 1335 | // See also [class.mfct]p5 and [class.static.data]p2. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1336 | if (R.isForRedeclaration()) |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1337 | return false; |
| 1338 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1339 | // If this is a namespace, look it up in the implied namespaces. |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1340 | if (LookupCtx->isFileContext()) |
Douglas Gregor | 8591098 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1341 | return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1342 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1343 | // 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] | 1344 | // classes, we're done. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1345 | CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); |
Douglas Gregor | 025291b | 2010-07-01 00:21:21 +0000 | [diff] [blame] | 1346 | if (!LookupRec || !LookupRec->getDefinition()) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1347 | return false; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1349 | // If we're performing qualified name lookup into a dependent class, |
| 1350 | // then we are actually looking into a current instantiation. If we have any |
| 1351 | // dependent base classes, then we either have to delay lookup until |
| 1352 | // template instantiation time (at which point all bases will be available) |
| 1353 | // or we have to fail. |
| 1354 | if (!InUnqualifiedLookup && LookupRec->isDependentContext() && |
| 1355 | LookupRec->hasAnyDependentBases()) { |
| 1356 | R.setNotFoundInCurrentInstantiation(); |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1360 | // Perform lookup into our base classes. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1361 | CXXBasePaths Paths; |
| 1362 | Paths.setOrigin(LookupRec); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Look for this member in our base classes |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1365 | CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1366 | switch (R.getLookupKind()) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1367 | case LookupOrdinaryName: |
| 1368 | case LookupMemberName: |
| 1369 | case LookupRedeclarationWithLinkage: |
| 1370 | BaseCallback = &CXXRecordDecl::FindOrdinaryMember; |
| 1371 | break; |
| 1372 | |
| 1373 | case LookupTagName: |
| 1374 | BaseCallback = &CXXRecordDecl::FindTagMember; |
| 1375 | break; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1377 | case LookupAnyName: |
| 1378 | BaseCallback = &LookupAnyMember; |
| 1379 | break; |
| 1380 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1381 | case LookupUsingDeclName: |
| 1382 | // This lookup is for redeclarations only. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1383 | |
| 1384 | case LookupOperatorName: |
| 1385 | case LookupNamespaceName: |
| 1386 | case LookupObjCProtocolName: |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1387 | // 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] | 1388 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1389 | |
| 1390 | case LookupNestedNameSpecifierName: |
| 1391 | BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember; |
| 1392 | break; |
| 1393 | } |
| 1394 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1395 | if (!LookupRec->lookupInBases(BaseCallback, |
| 1396 | R.getLookupName().getAsOpaquePtr(), Paths)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1397 | return false; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1398 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1399 | R.setNamingClass(LookupRec); |
| 1400 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1401 | // C++ [class.member.lookup]p2: |
| 1402 | // [...] If the resulting set of declarations are not all from |
| 1403 | // sub-objects of the same type, or the set has a nonstatic member |
| 1404 | // and includes members from distinct sub-objects, there is an |
| 1405 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 1406 | // the result of the lookup. |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1407 | QualType SubobjectType; |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1408 | int SubobjectNumber = 0; |
John McCall | 7aceaf8 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 1409 | AccessSpecifier SubobjectAccess = AS_none; |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1411 | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1412 | Path != PathEnd; ++Path) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1413 | const CXXBasePathElement &PathElement = Path->back(); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1414 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1415 | // Pick the best (i.e. most permissive i.e. numerically lowest) access |
| 1416 | // across all paths. |
| 1417 | SubobjectAccess = std::min(SubobjectAccess, Path->Access); |
| 1418 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1419 | // Determine whether we're looking at a distinct sub-object or not. |
| 1420 | if (SubobjectType.isNull()) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1421 | // This is the first subobject we've looked at. Record its type. |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1422 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 1423 | SubobjectNumber = PathElement.SubobjectNumber; |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1424 | continue; |
| 1425 | } |
| 1426 | |
| 1427 | if (SubobjectType |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1428 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 1429 | // We found members of the given name in two subobjects of |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1430 | // different types. If the declaration sets aren't the same, this |
| 1431 | // this lookup is ambiguous. |
| 1432 | if (HasOnlyStaticMembers(Path->Decls.first, Path->Decls.second)) { |
| 1433 | CXXBasePaths::paths_iterator FirstPath = Paths.begin(); |
| 1434 | DeclContext::lookup_iterator FirstD = FirstPath->Decls.first; |
| 1435 | DeclContext::lookup_iterator CurrentD = Path->Decls.first; |
| 1436 | |
| 1437 | while (FirstD != FirstPath->Decls.second && |
| 1438 | CurrentD != Path->Decls.second) { |
| 1439 | if ((*FirstD)->getUnderlyingDecl()->getCanonicalDecl() != |
| 1440 | (*CurrentD)->getUnderlyingDecl()->getCanonicalDecl()) |
| 1441 | break; |
| 1442 | |
| 1443 | ++FirstD; |
| 1444 | ++CurrentD; |
| 1445 | } |
| 1446 | |
| 1447 | if (FirstD == FirstPath->Decls.second && |
| 1448 | CurrentD == Path->Decls.second) |
| 1449 | continue; |
| 1450 | } |
| 1451 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1452 | R.setAmbiguousBaseSubobjectTypes(Paths); |
| 1453 | return true; |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | if (SubobjectNumber != PathElement.SubobjectNumber) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1457 | // We have a different subobject of the same type. |
| 1458 | |
| 1459 | // C++ [class.member.lookup]p5: |
| 1460 | // A static member, a nested type or an enumerator defined in |
| 1461 | // a base class T can unambiguously be found even if an object |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | // has more than one base class subobject of type T. |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1463 | if (HasOnlyStaticMembers(Path->Decls.first, Path->Decls.second)) |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1464 | continue; |
Douglas Gregor | f17b58c | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1465 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1466 | // We have found a nonstatic member name in multiple, distinct |
| 1467 | // subobjects. Name lookup is ambiguous. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1468 | R.setAmbiguousBaseSubobjects(Paths); |
| 1469 | return true; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | // Lookup in a base class succeeded; return these results. |
| 1474 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1475 | DeclContext::lookup_iterator I, E; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1476 | for (llvm::tie(I,E) = Paths.front().Decls; I != E; ++I) { |
| 1477 | NamedDecl *D = *I; |
| 1478 | AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, |
| 1479 | D->getAccess()); |
| 1480 | R.addDecl(D, AS); |
| 1481 | } |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1482 | R.resolveKind(); |
| 1483 | return true; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | /// @brief Performs name lookup for a name that was parsed in the |
| 1487 | /// source code, and may contain a C++ scope specifier. |
| 1488 | /// |
| 1489 | /// This routine is a convenience routine meant to be called from |
| 1490 | /// contexts that receive a name and an optional C++ scope specifier |
| 1491 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 1492 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 1493 | /// respectively) on the given name and return those results. |
| 1494 | /// |
| 1495 | /// @param S The scope from which unqualified name lookup will |
| 1496 | /// begin. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | /// |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1498 | /// @param SS An optional C++ scope-specifier, e.g., "::N::M". |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1499 | /// |
| 1500 | /// @param Name The name of the entity that name lookup will |
| 1501 | /// search for. |
| 1502 | /// |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1503 | /// @param Loc If provided, the source location where we're performing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | /// name lookup. At present, this is only used to produce diagnostics when |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1505 | /// C library functions (like "malloc") are implicitly declared. |
| 1506 | /// |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1507 | /// @param EnteringContext Indicates whether we are going to enter the |
| 1508 | /// context of the scope-specifier SS (if present). |
| 1509 | /// |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1510 | /// @returns True if any decls were found (but possibly ambiguous) |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1511 | bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1512 | bool AllowBuiltinCreation, bool EnteringContext) { |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1513 | if (SS && SS->isInvalid()) { |
| 1514 | // When the scope specifier is invalid, don't even look for |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1515 | // anything. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1516 | return false; |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1519 | if (SS && SS->isSet()) { |
| 1520 | if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | // We have resolved the scope specifier to a particular declaration |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1522 | // contex, and will perform name lookup in that context. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1523 | if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1524 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1526 | R.setContextRange(SS->getRange()); |
| 1527 | |
| 1528 | return LookupQualifiedName(R, DC); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1529 | } |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1530 | |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1531 | // We could not resolve the scope specified to a specific declaration |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | // context, which means that SS refers to an unknown specialization. |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1533 | // Name lookup can't find anything in this case. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1534 | return false; |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | // Perform unqualified name lookup starting in the given scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1538 | return LookupName(R, S, AllowBuiltinCreation); |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1541 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1542 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 1543 | /// from name lookup. |
| 1544 | /// |
| 1545 | /// @param Result The ambiguous name lookup result. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | /// |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1547 | /// @param Name The name of the entity that name lookup was |
| 1548 | /// searching for. |
| 1549 | /// |
| 1550 | /// @param NameLoc The location of the name within the source code. |
| 1551 | /// |
| 1552 | /// @param LookupRange A source range that provides more |
| 1553 | /// source-location information concerning the lookup itself. For |
| 1554 | /// example, this range might highlight a nested-name-specifier that |
| 1555 | /// precedes the name. |
| 1556 | /// |
| 1557 | /// @returns true |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1558 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1559 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 1560 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1561 | DeclarationName Name = Result.getLookupName(); |
| 1562 | SourceLocation NameLoc = Result.getNameLoc(); |
| 1563 | SourceRange LookupRange = Result.getContextRange(); |
| 1564 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1565 | switch (Result.getAmbiguityKind()) { |
| 1566 | case LookupResult::AmbiguousBaseSubobjects: { |
| 1567 | CXXBasePaths *Paths = Result.getBasePaths(); |
| 1568 | QualType SubobjectType = Paths->front().back().Base->getType(); |
| 1569 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 1570 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 1571 | << LookupRange; |
| 1572 | |
| 1573 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
| 1574 | while (isa<CXXMethodDecl>(*Found) && |
| 1575 | cast<CXXMethodDecl>(*Found)->isStatic()) |
| 1576 | ++Found; |
| 1577 | |
| 1578 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
| 1579 | |
| 1580 | return true; |
| 1581 | } |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1582 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1583 | case LookupResult::AmbiguousBaseSubobjectTypes: { |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1584 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 1585 | << Name << LookupRange; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1586 | |
| 1587 | CXXBasePaths *Paths = Result.getBasePaths(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1588 | std::set<Decl *> DeclsPrinted; |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1589 | for (CXXBasePaths::paths_iterator Path = Paths->begin(), |
| 1590 | PathEnd = Paths->end(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1591 | Path != PathEnd; ++Path) { |
| 1592 | Decl *D = *Path->Decls.first; |
| 1593 | if (DeclsPrinted.insert(D).second) |
| 1594 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 1595 | } |
| 1596 | |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1597 | return true; |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1600 | case LookupResult::AmbiguousTagHiding: { |
| 1601 | Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; |
Douglas Gregor | 69d993a | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1602 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1603 | llvm::SmallPtrSet<NamedDecl*,8> TagDecls; |
| 1604 | |
| 1605 | LookupResult::iterator DI, DE = Result.end(); |
| 1606 | for (DI = Result.begin(); DI != DE; ++DI) |
| 1607 | if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) { |
| 1608 | TagDecls.insert(TD); |
| 1609 | Diag(TD->getLocation(), diag::note_hidden_tag); |
| 1610 | } |
| 1611 | |
| 1612 | for (DI = Result.begin(); DI != DE; ++DI) |
| 1613 | if (!isa<TagDecl>(*DI)) |
| 1614 | Diag((*DI)->getLocation(), diag::note_hiding_object); |
| 1615 | |
| 1616 | // For recovery purposes, go ahead and implement the hiding. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1617 | LookupResult::Filter F = Result.makeFilter(); |
| 1618 | while (F.hasNext()) { |
| 1619 | if (TagDecls.count(F.next())) |
| 1620 | F.erase(); |
| 1621 | } |
| 1622 | F.done(); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1623 | |
| 1624 | return true; |
| 1625 | } |
| 1626 | |
| 1627 | case LookupResult::AmbiguousReference: { |
| 1628 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1629 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1630 | LookupResult::iterator DI = Result.begin(), DE = Result.end(); |
| 1631 | for (; DI != DE; ++DI) |
| 1632 | Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1633 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1634 | return true; |
| 1635 | } |
| 1636 | } |
| 1637 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1638 | llvm_unreachable("unknown ambiguity kind"); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1639 | return true; |
| 1640 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1641 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1642 | namespace { |
| 1643 | struct AssociatedLookup { |
| 1644 | AssociatedLookup(Sema &S, |
| 1645 | Sema::AssociatedNamespaceSet &Namespaces, |
| 1646 | Sema::AssociatedClassSet &Classes) |
| 1647 | : S(S), Namespaces(Namespaces), Classes(Classes) { |
| 1648 | } |
| 1649 | |
| 1650 | Sema &S; |
| 1651 | Sema::AssociatedNamespaceSet &Namespaces; |
| 1652 | Sema::AssociatedClassSet &Classes; |
| 1653 | }; |
| 1654 | } |
| 1655 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1657 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | 5402295 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 1659 | static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, |
| 1660 | DeclContext *Ctx) { |
| 1661 | // Add the associated namespace for this class. |
| 1662 | |
| 1663 | // We don't use DeclContext::getEnclosingNamespaceContext() as this may |
| 1664 | // be a locally scoped record. |
| 1665 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1666 | // We skip out of inline namespaces. The innermost non-inline namespace |
| 1667 | // contains all names of all its nested inline namespaces anyway, so we can |
| 1668 | // replace the entire inline namespace tree with its root. |
| 1669 | while (Ctx->isRecord() || Ctx->isTransparentContext() || |
| 1670 | Ctx->isInlineNamespace()) |
Douglas Gregor | 5402295 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 1671 | Ctx = Ctx->getParent(); |
| 1672 | |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1673 | if (Ctx->isFileContext()) |
Douglas Gregor | 5402295 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 1674 | Namespaces.insert(Ctx->getPrimaryContext()); |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1675 | } |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1676 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | // \brief Add the associated classes and namespaces for argument-dependent |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1678 | // lookup that involves a template argument (C++ [basic.lookup.koenig]p2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1680 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
| 1681 | const TemplateArgument &Arg) { |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1682 | // C++ [basic.lookup.koenig]p2, last bullet: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | // -- [...] ; |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1684 | switch (Arg.getKind()) { |
| 1685 | case TemplateArgument::Null: |
| 1686 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1688 | case TemplateArgument::Type: |
| 1689 | // [...] the namespaces and classes associated with the types of the |
| 1690 | // template arguments provided for template type parameters (excluding |
| 1691 | // template template parameters) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1692 | addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1693 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1695 | case TemplateArgument::Template: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | // [...] the namespaces in which any template template arguments are |
| 1697 | // defined; and the classes in which any member templates used as |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1698 | // template template arguments are defined. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1699 | TemplateName Template = Arg.getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1701 | = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1702 | DeclContext *Ctx = ClassTemplate->getDeclContext(); |
| 1703 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1704 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1705 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1706 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1707 | } |
| 1708 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | case TemplateArgument::Declaration: |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1712 | case TemplateArgument::Integral: |
| 1713 | case TemplateArgument::Expression: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | // [Note: non-type template arguments do not contribute to the set of |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1715 | // associated namespaces. ] |
| 1716 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1718 | case TemplateArgument::Pack: |
| 1719 | for (TemplateArgument::pack_iterator P = Arg.pack_begin(), |
| 1720 | PEnd = Arg.pack_end(); |
| 1721 | P != PEnd; ++P) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1722 | addAssociatedClassesAndNamespaces(Result, *P); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1723 | break; |
| 1724 | } |
| 1725 | } |
| 1726 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1727 | // \brief Add the associated classes and namespaces for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | // argument-dependent lookup with an argument of class type |
| 1729 | // (C++ [basic.lookup.koenig]p2). |
| 1730 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1731 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
| 1732 | CXXRecordDecl *Class) { |
| 1733 | |
| 1734 | // Just silently ignore anything whose name is __va_list_tag. |
| 1735 | if (Class->getDeclName() == Result.S.VAListTagName) |
| 1736 | return; |
| 1737 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1738 | // C++ [basic.lookup.koenig]p2: |
| 1739 | // [...] |
| 1740 | // -- If T is a class type (including unions), its associated |
| 1741 | // classes are: the class itself; the class of which it is a |
| 1742 | // member, if any; and its direct and indirect base |
| 1743 | // classes. Its associated namespaces are the namespaces in |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | // which its associated classes are defined. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1745 | |
| 1746 | // Add the class of which it is a member, if any. |
| 1747 | DeclContext *Ctx = Class->getDeclContext(); |
| 1748 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1749 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1750 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1751 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1753 | // Add the class itself. If we've already seen this class, we don't |
| 1754 | // need to visit base classes. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1755 | if (!Result.Classes.insert(Class)) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1756 | return; |
| 1757 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | // -- If T is a template-id, its associated namespaces and classes are |
| 1759 | // the namespace in which the template is defined; for member |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1760 | // templates, the member template’s class; the namespaces and classes |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | // associated with the types of the template arguments provided for |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1762 | // template type parameters (excluding template template parameters); the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | // namespaces in which any template template arguments are defined; and |
| 1764 | // the classes in which any member templates used as template template |
| 1765 | // arguments are defined. [Note: non-type template arguments do not |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1766 | // contribute to the set of associated namespaces. ] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1768 | = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { |
| 1769 | DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); |
| 1770 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1771 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1772 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1773 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1775 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 1776 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1777 | addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); |
Douglas Gregor | 69be8d6 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 1778 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1780 | // Only recurse into base classes for complete types. |
| 1781 | if (!Class->hasDefinition()) { |
| 1782 | // FIXME: we might need to instantiate templates here |
| 1783 | return; |
| 1784 | } |
| 1785 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1786 | // Add direct and indirect base classes along with their associated |
| 1787 | // namespaces. |
| 1788 | llvm::SmallVector<CXXRecordDecl *, 32> Bases; |
| 1789 | Bases.push_back(Class); |
| 1790 | while (!Bases.empty()) { |
| 1791 | // Pop this class off the stack. |
| 1792 | Class = Bases.back(); |
| 1793 | Bases.pop_back(); |
| 1794 | |
| 1795 | // Visit the base classes. |
| 1796 | for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), |
| 1797 | BaseEnd = Class->bases_end(); |
| 1798 | Base != BaseEnd; ++Base) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1799 | const RecordType *BaseType = Base->getType()->getAs<RecordType>(); |
Sebastian Redl | bbc1cc5 | 2009-10-25 09:35:33 +0000 | [diff] [blame] | 1800 | // In dependent contexts, we do ADL twice, and the first time around, |
| 1801 | // the base type might be a dependent TemplateSpecializationType, or a |
| 1802 | // TemplateTypeParmType. If that happens, simply ignore it. |
| 1803 | // FIXME: If we want to support export, we probably need to add the |
| 1804 | // namespace of the template in a TemplateSpecializationType, or even |
| 1805 | // the classes and namespaces of known non-dependent arguments. |
| 1806 | if (!BaseType) |
| 1807 | continue; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1808 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1809 | if (Result.Classes.insert(BaseDecl)) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1810 | // Find the associated namespace for this base class. |
| 1811 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1812 | CollectEnclosingNamespace(Result.Namespaces, BaseCtx); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Make sure we visit the bases of this base class. |
| 1815 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
| 1816 | Bases.push_back(BaseDecl); |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | // \brief Add the associated classes and namespaces for |
| 1823 | // argument-dependent lookup with an argument of type T |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | // (C++ [basic.lookup.koenig]p2). |
| 1825 | static void |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1826 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1827 | // C++ [basic.lookup.koenig]p2: |
| 1828 | // |
| 1829 | // For each argument type T in the function call, there is a set |
| 1830 | // of zero or more associated namespaces and a set of zero or more |
| 1831 | // associated classes to be considered. The sets of namespaces and |
| 1832 | // classes is determined entirely by the types of the function |
| 1833 | // arguments (and the namespace of any template template |
| 1834 | // argument). Typedef names and using-declarations used to specify |
| 1835 | // the types do not contribute to this set. The sets of namespaces |
| 1836 | // and classes are determined in the following way: |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1837 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1838 | llvm::SmallVector<const Type *, 16> Queue; |
| 1839 | const Type *T = Ty->getCanonicalTypeInternal().getTypePtr(); |
| 1840 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1841 | while (true) { |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1842 | switch (T->getTypeClass()) { |
| 1843 | |
| 1844 | #define TYPE(Class, Base) |
| 1845 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1846 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1847 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1848 | #define ABSTRACT_TYPE(Class, Base) |
| 1849 | #include "clang/AST/TypeNodes.def" |
| 1850 | // T is canonical. We can also ignore dependent types because |
| 1851 | // we don't need to do ADL at the definition point, but if we |
| 1852 | // wanted to implement template export (or if we find some other |
| 1853 | // use for associated classes and namespaces...) this would be |
| 1854 | // wrong. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1855 | break; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1856 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1857 | // -- If T is a pointer to U or an array of U, its associated |
| 1858 | // namespaces and classes are those associated with U. |
| 1859 | case Type::Pointer: |
| 1860 | T = cast<PointerType>(T)->getPointeeType().getTypePtr(); |
| 1861 | continue; |
| 1862 | case Type::ConstantArray: |
| 1863 | case Type::IncompleteArray: |
| 1864 | case Type::VariableArray: |
| 1865 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 1866 | continue; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1867 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1868 | // -- If T is a fundamental type, its associated sets of |
| 1869 | // namespaces and classes are both empty. |
| 1870 | case Type::Builtin: |
| 1871 | break; |
| 1872 | |
| 1873 | // -- If T is a class type (including unions), its associated |
| 1874 | // classes are: the class itself; the class of which it is a |
| 1875 | // member, if any; and its direct and indirect base |
| 1876 | // classes. Its associated namespaces are the namespaces in |
| 1877 | // which its associated classes are defined. |
| 1878 | case Type::Record: { |
| 1879 | CXXRecordDecl *Class |
| 1880 | = cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()); |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1881 | addAssociatedClassesAndNamespaces(Result, Class); |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1882 | break; |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1883 | } |
Douglas Gregor | 4e58c25 | 2010-05-20 02:26:51 +0000 | [diff] [blame] | 1884 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1885 | // -- If T is an enumeration type, its associated namespace is |
| 1886 | // the namespace in which it is defined. If it is class |
| 1887 | // member, its associated class is the member’s class; else |
| 1888 | // it has no associated class. |
| 1889 | case Type::Enum: { |
| 1890 | EnumDecl *Enum = cast<EnumType>(T)->getDecl(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1891 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1892 | DeclContext *Ctx = Enum->getDeclContext(); |
| 1893 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1894 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1895 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1896 | // Add the associated namespace for this class. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1897 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1898 | |
John McCall | fa4edcf | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 1899 | break; |
| 1900 | } |
| 1901 | |
| 1902 | // -- If T is a function type, its associated namespaces and |
| 1903 | // classes are those associated with the function parameter |
| 1904 | // types and those associated with the return type. |
| 1905 | case Type::FunctionProto: { |
| 1906 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 1907 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
| 1908 | ArgEnd = Proto->arg_type_end(); |
| 1909 | Arg != ArgEnd; ++Arg) |
| 1910 | Queue.push_back(Arg->getTypePtr()); |
| 1911 | // fallthrough |
| 1912 | } |
| 1913 | case Type::FunctionNoProto: { |
| 1914 | const FunctionType *FnType = cast<FunctionType>(T); |
| 1915 | T = FnType->getResultType().getTypePtr(); |
| 1916 | continue; |
| 1917 | } |
| 1918 | |
| 1919 | // -- If T is a pointer to a member function of a class X, its |
| 1920 | // associated namespaces and classes are those associated |
| 1921 | // with the function parameter types and return type, |
| 1922 | // together with those associated with X. |
| 1923 | // |
| 1924 | // -- If T is a pointer to a data member of class X, its |
| 1925 | // associated namespaces and classes are those associated |
| 1926 | // with the member type together with those associated with |
| 1927 | // X. |
| 1928 | case Type::MemberPointer: { |
| 1929 | const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); |
| 1930 | |
| 1931 | // Queue up the class type into which this points. |
| 1932 | Queue.push_back(MemberPtr->getClass()); |
| 1933 | |
| 1934 | // And directly continue with the pointee type. |
| 1935 | T = MemberPtr->getPointeeType().getTypePtr(); |
| 1936 | continue; |
| 1937 | } |
| 1938 | |
| 1939 | // As an extension, treat this like a normal pointer. |
| 1940 | case Type::BlockPointer: |
| 1941 | T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr(); |
| 1942 | continue; |
| 1943 | |
| 1944 | // References aren't covered by the standard, but that's such an |
| 1945 | // obvious defect that we cover them anyway. |
| 1946 | case Type::LValueReference: |
| 1947 | case Type::RValueReference: |
| 1948 | T = cast<ReferenceType>(T)->getPointeeType().getTypePtr(); |
| 1949 | continue; |
| 1950 | |
| 1951 | // These are fundamental types. |
| 1952 | case Type::Vector: |
| 1953 | case Type::ExtVector: |
| 1954 | case Type::Complex: |
| 1955 | break; |
| 1956 | |
| 1957 | // These are ignored by ADL. |
| 1958 | case Type::ObjCObject: |
| 1959 | case Type::ObjCInterface: |
| 1960 | case Type::ObjCObjectPointer: |
| 1961 | break; |
| 1962 | } |
| 1963 | |
| 1964 | if (Queue.empty()) break; |
| 1965 | T = Queue.back(); |
| 1966 | Queue.pop_back(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1967 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /// \brief Find the associated classes and namespaces for |
| 1971 | /// argument-dependent lookup for a call with the given set of |
| 1972 | /// arguments. |
| 1973 | /// |
| 1974 | /// This routine computes the sets of associated classes and associated |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | /// namespaces searched by argument-dependent lookup |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1976 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1978 | Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, |
| 1979 | AssociatedNamespaceSet &AssociatedNamespaces, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 1980 | AssociatedClassSet &AssociatedClasses) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1981 | AssociatedNamespaces.clear(); |
| 1982 | AssociatedClasses.clear(); |
| 1983 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1984 | AssociatedLookup Result(*this, AssociatedNamespaces, AssociatedClasses); |
| 1985 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1986 | // C++ [basic.lookup.koenig]p2: |
| 1987 | // For each argument type T in the function call, there is a set |
| 1988 | // of zero or more associated namespaces and a set of zero or more |
| 1989 | // associated classes to be considered. The sets of namespaces and |
| 1990 | // classes is determined entirely by the types of the function |
| 1991 | // arguments (and the namespace of any template template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | // argument). |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1993 | for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) { |
| 1994 | Expr *Arg = Args[ArgIdx]; |
| 1995 | |
| 1996 | if (Arg->getType() != Context.OverloadTy) { |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 1997 | addAssociatedClassesAndNamespaces(Result, Arg->getType()); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1998 | continue; |
| 1999 | } |
| 2000 | |
| 2001 | // [...] In addition, if the argument is the name or address of a |
| 2002 | // set of overloaded functions and/or function templates, its |
| 2003 | // associated classes and namespaces are the union of those |
| 2004 | // associated with each of the members of the set: the namespace |
| 2005 | // in which the function or function template is defined and the |
| 2006 | // classes and namespaces associated with its (non-dependent) |
| 2007 | // parameter types and return type. |
Douglas Gregor | daa439a | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 2008 | Arg = Arg->IgnoreParens(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2009 | if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2010 | if (unaryOp->getOpcode() == UO_AddrOf) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2011 | Arg = unaryOp->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2013 | UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg); |
| 2014 | if (!ULE) continue; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2015 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2016 | for (UnresolvedSetIterator I = ULE->decls_begin(), E = ULE->decls_end(); |
| 2017 | I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 2018 | // Look through any using declarations to find the underlying function. |
| 2019 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2020 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 2021 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(Fn); |
| 2022 | if (!FDecl) |
| 2023 | FDecl = cast<FunctionTemplateDecl>(Fn)->getTemplatedDecl(); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Add the classes and namespaces associated with the parameter |
| 2026 | // types and return type of this function. |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2027 | addAssociatedClassesAndNamespaces(Result, FDecl->getType()); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2031 | |
| 2032 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 2033 | /// an acceptable non-member overloaded operator for a call whose |
| 2034 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 2035 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 2036 | /// enumeration types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | static bool |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2038 | IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, |
| 2039 | QualType T1, QualType T2, |
| 2040 | ASTContext &Context) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2041 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
| 2042 | return true; |
| 2043 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2044 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 2045 | return true; |
| 2046 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2047 | const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2048 | if (Proto->getNumArgs() < 1) |
| 2049 | return false; |
| 2050 | |
| 2051 | if (T1->isEnumeralType()) { |
| 2052 | QualType ArgType = Proto->getArgType(0).getNonReferenceType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2053 | if (Context.hasSameUnqualifiedType(T1, ArgType)) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2054 | return true; |
| 2055 | } |
| 2056 | |
| 2057 | if (Proto->getNumArgs() < 2) |
| 2058 | return false; |
| 2059 | |
| 2060 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 2061 | QualType ArgType = Proto->getArgType(1).getNonReferenceType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2062 | if (Context.hasSameUnqualifiedType(T2, ArgType)) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2063 | return true; |
| 2064 | } |
| 2065 | |
| 2066 | return false; |
| 2067 | } |
| 2068 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2069 | NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2070 | SourceLocation Loc, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2071 | LookupNameKind NameKind, |
| 2072 | RedeclarationKind Redecl) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2073 | LookupResult R(*this, Name, Loc, NameKind, Redecl); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2074 | LookupName(R, S); |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 2075 | return R.getAsSingle<NamedDecl>(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 2078 | /// \brief Find the protocol with the given name, if any. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2079 | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, |
| 2080 | SourceLocation IdLoc) { |
| 2081 | Decl *D = LookupSingleName(TUScope, II, IdLoc, |
| 2082 | LookupObjCProtocolName); |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 2083 | return cast_or_null<ObjCProtocolDecl>(D); |
| 2084 | } |
| 2085 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2086 | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | QualType T1, QualType T2, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2088 | UnresolvedSetImpl &Functions) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2089 | // C++ [over.match.oper]p3: |
| 2090 | // -- The set of non-member candidates is the result of the |
| 2091 | // unqualified lookup of operator@ in the context of the |
| 2092 | // expression according to the usual rules for name lookup in |
| 2093 | // unqualified function calls (3.4.2) except that all member |
| 2094 | // functions are ignored. However, if no operand has a class |
| 2095 | // type, only those non-member functions in the lookup set |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2096 | // that have a first parameter of type T1 or "reference to |
| 2097 | // (possibly cv-qualified) T1", when T1 is an enumeration |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2098 | // type, or (if there is a right operand) a second parameter |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2099 | // of type T2 or "reference to (possibly cv-qualified) T2", |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2100 | // when T2 is an enumeration type, are candidate functions. |
| 2101 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2102 | LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); |
| 2103 | LookupName(Operators, S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2105 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
| 2106 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2107 | if (Operators.empty()) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2108 | return; |
| 2109 | |
| 2110 | for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end(); |
| 2111 | Op != OpEnd; ++Op) { |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 2112 | NamedDecl *Found = (*Op)->getUnderlyingDecl(); |
| 2113 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Found)) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2114 | if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 2115 | Functions.addDecl(*Op, Op.getAccess()); // FIXME: canonical FD |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | } else if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 2117 | = dyn_cast<FunctionTemplateDecl>(Found)) { |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2118 | // FIXME: friend operators? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2120 | // later? |
| 2121 | if (!FunTmpl->getDeclContext()->isRecord()) |
Douglas Gregor | 6bf356f | 2010-04-25 20:25:43 +0000 | [diff] [blame] | 2122 | Functions.addDecl(*Op, Op.getAccess()); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2123 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2127 | /// \brief Look up the constructors for the given class. |
| 2128 | DeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 2129 | // If the copy constructor has not yet been declared, do so now. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2130 | if (CanDeclareSpecialMemberFunction(Context, Class)) { |
| 2131 | if (!Class->hasDeclaredDefaultConstructor()) |
| 2132 | DeclareImplicitDefaultConstructor(Class); |
| 2133 | if (!Class->hasDeclaredCopyConstructor()) |
| 2134 | DeclareImplicitCopyConstructor(Class); |
| 2135 | } |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2137 | CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class)); |
| 2138 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T); |
| 2139 | return Class->lookup(Name); |
| 2140 | } |
| 2141 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2142 | /// \brief Look for the destructor of the given class. |
| 2143 | /// |
| 2144 | /// During semantic analysis, this routine should be used in lieu of |
| 2145 | /// CXXRecordDecl::getDestructor(). |
| 2146 | /// |
| 2147 | /// \returns The destructor for this class. |
| 2148 | CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) { |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 2149 | // If the destructor has not yet been declared, do so now. |
| 2150 | if (CanDeclareSpecialMemberFunction(Context, Class) && |
| 2151 | !Class->hasDeclaredDestructor()) |
| 2152 | DeclareImplicitDestructor(Class); |
| 2153 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2154 | return Class->getDestructor(); |
| 2155 | } |
| 2156 | |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2157 | void ADLResult::insert(NamedDecl *New) { |
| 2158 | NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; |
| 2159 | |
| 2160 | // If we haven't yet seen a decl for this key, or the last decl |
| 2161 | // was exactly this one, we're done. |
| 2162 | if (Old == 0 || Old == New) { |
| 2163 | Old = New; |
| 2164 | return; |
| 2165 | } |
| 2166 | |
| 2167 | // Otherwise, decide which is a more recent redeclaration. |
| 2168 | FunctionDecl *OldFD, *NewFD; |
| 2169 | if (isa<FunctionTemplateDecl>(New)) { |
| 2170 | OldFD = cast<FunctionTemplateDecl>(Old)->getTemplatedDecl(); |
| 2171 | NewFD = cast<FunctionTemplateDecl>(New)->getTemplatedDecl(); |
| 2172 | } else { |
| 2173 | OldFD = cast<FunctionDecl>(Old); |
| 2174 | NewFD = cast<FunctionDecl>(New); |
| 2175 | } |
| 2176 | |
| 2177 | FunctionDecl *Cursor = NewFD; |
| 2178 | while (true) { |
| 2179 | Cursor = Cursor->getPreviousDeclaration(); |
| 2180 | |
| 2181 | // If we got to the end without finding OldFD, OldFD is the newer |
| 2182 | // declaration; leave things as they are. |
| 2183 | if (!Cursor) return; |
| 2184 | |
| 2185 | // If we do find OldFD, then NewFD is newer. |
| 2186 | if (Cursor == OldFD) break; |
| 2187 | |
| 2188 | // Otherwise, keep looking. |
| 2189 | } |
| 2190 | |
| 2191 | Old = New; |
| 2192 | } |
| 2193 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2194 | void Sema::ArgumentDependentLookup(DeclarationName Name, bool Operator, |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2195 | Expr **Args, unsigned NumArgs, |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2196 | ADLResult &Result) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2197 | // Find all of the associated namespaces and classes based on the |
| 2198 | // arguments we have. |
| 2199 | AssociatedNamespaceSet AssociatedNamespaces; |
| 2200 | AssociatedClassSet AssociatedClasses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | FindAssociatedClassesAndNamespaces(Args, NumArgs, |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 2202 | AssociatedNamespaces, |
| 2203 | AssociatedClasses); |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2204 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2205 | QualType T1, T2; |
| 2206 | if (Operator) { |
| 2207 | T1 = Args[0]->getType(); |
| 2208 | if (NumArgs >= 2) |
| 2209 | T2 = Args[1]->getType(); |
| 2210 | } |
| 2211 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2212 | // C++ [basic.lookup.argdep]p3: |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2213 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 2214 | // and let Y be the lookup set produced by argument dependent |
| 2215 | // lookup (defined as follows). If X contains [...] then Y is |
| 2216 | // empty. Otherwise Y is the set of declarations found in the |
| 2217 | // namespaces associated with the argument types as described |
| 2218 | // below. The set of declarations found by the lookup of the name |
| 2219 | // is the union of X and Y. |
| 2220 | // |
| 2221 | // Here, we compute Y and add its members to the overloaded |
| 2222 | // candidate set. |
| 2223 | for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | NSEnd = AssociatedNamespaces.end(); |
| 2225 | NS != NSEnd; ++NS) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2226 | // When considering an associated namespace, the lookup is the |
| 2227 | // same as the lookup performed when the associated namespace is |
| 2228 | // used as a qualifier (3.4.3.2) except that: |
| 2229 | // |
| 2230 | // -- Any using-directives in the associated namespace are |
| 2231 | // ignored. |
| 2232 | // |
John McCall | 6ff0785 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 2233 | // -- Any namespace-scope friend functions declared in |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2234 | // associated classes are visible within their respective |
| 2235 | // namespaces even if they are not visible during an ordinary |
| 2236 | // lookup (11.4). |
| 2237 | DeclContext::lookup_iterator I, E; |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 2238 | for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2239 | NamedDecl *D = *I; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2240 | // If the only declaration here is an ordinary friend, consider |
| 2241 | // it only if it was declared in an associated classes. |
| 2242 | if (D->getIdentifierNamespace() == Decl::IDNS_OrdinaryFriend) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 2243 | DeclContext *LexDC = D->getLexicalDeclContext(); |
| 2244 | if (!AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) |
| 2245 | continue; |
| 2246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 2248 | if (isa<UsingShadowDecl>(D)) |
| 2249 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2250 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 2251 | if (isa<FunctionDecl>(D)) { |
| 2252 | if (Operator && |
| 2253 | !IsAcceptableNonMemberOperatorCandidate(cast<FunctionDecl>(D), |
| 2254 | T1, T2, Context)) |
| 2255 | continue; |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2256 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2257 | continue; |
| 2258 | |
| 2259 | Result.insert(D); |
Douglas Gregor | 44bc2d5 | 2009-06-23 20:14:09 +0000 | [diff] [blame] | 2260 | } |
| 2261 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2262 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2263 | |
| 2264 | //---------------------------------------------------------------------------- |
| 2265 | // Search for all visible declarations. |
| 2266 | //---------------------------------------------------------------------------- |
| 2267 | VisibleDeclConsumer::~VisibleDeclConsumer() { } |
| 2268 | |
| 2269 | namespace { |
| 2270 | |
| 2271 | class ShadowContextRAII; |
| 2272 | |
| 2273 | class VisibleDeclsRecord { |
| 2274 | public: |
| 2275 | /// \brief An entry in the shadow map, which is optimized to store a |
| 2276 | /// single declaration (the common case) but can also store a list |
| 2277 | /// of declarations. |
| 2278 | class ShadowMapEntry { |
| 2279 | typedef llvm::SmallVector<NamedDecl *, 4> DeclVector; |
| 2280 | |
| 2281 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 2282 | /// of declarations. |
| 2283 | llvm::PointerUnion<NamedDecl *, DeclVector*> DeclOrVector; |
| 2284 | |
| 2285 | public: |
| 2286 | ShadowMapEntry() : DeclOrVector() { } |
| 2287 | |
| 2288 | void Add(NamedDecl *ND); |
| 2289 | void Destroy(); |
| 2290 | |
| 2291 | // Iteration. |
| 2292 | typedef NamedDecl **iterator; |
| 2293 | iterator begin(); |
| 2294 | iterator end(); |
| 2295 | }; |
| 2296 | |
| 2297 | private: |
| 2298 | /// \brief A mapping from declaration names to the declarations that have |
| 2299 | /// this name within a particular scope. |
| 2300 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
| 2301 | |
| 2302 | /// \brief A list of shadow maps, which is used to model name hiding. |
| 2303 | std::list<ShadowMap> ShadowMaps; |
| 2304 | |
| 2305 | /// \brief The declaration contexts we have already visited. |
| 2306 | llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; |
| 2307 | |
| 2308 | friend class ShadowContextRAII; |
| 2309 | |
| 2310 | public: |
| 2311 | /// \brief Determine whether we have already visited this context |
| 2312 | /// (and, if not, note that we are going to visit that context now). |
| 2313 | bool visitedContext(DeclContext *Ctx) { |
| 2314 | return !VisitedContexts.insert(Ctx); |
| 2315 | } |
| 2316 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2317 | bool alreadyVisitedContext(DeclContext *Ctx) { |
| 2318 | return VisitedContexts.count(Ctx); |
| 2319 | } |
| 2320 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2321 | /// \brief Determine whether the given declaration is hidden in the |
| 2322 | /// current scope. |
| 2323 | /// |
| 2324 | /// \returns the declaration that hides the given declaration, or |
| 2325 | /// NULL if no such declaration exists. |
| 2326 | NamedDecl *checkHidden(NamedDecl *ND); |
| 2327 | |
| 2328 | /// \brief Add a declaration to the current shadow map. |
| 2329 | void add(NamedDecl *ND) { ShadowMaps.back()[ND->getDeclName()].Add(ND); } |
| 2330 | }; |
| 2331 | |
| 2332 | /// \brief RAII object that records when we've entered a shadow context. |
| 2333 | class ShadowContextRAII { |
| 2334 | VisibleDeclsRecord &Visible; |
| 2335 | |
| 2336 | typedef VisibleDeclsRecord::ShadowMap ShadowMap; |
| 2337 | |
| 2338 | public: |
| 2339 | ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { |
| 2340 | Visible.ShadowMaps.push_back(ShadowMap()); |
| 2341 | } |
| 2342 | |
| 2343 | ~ShadowContextRAII() { |
| 2344 | for (ShadowMap::iterator E = Visible.ShadowMaps.back().begin(), |
| 2345 | EEnd = Visible.ShadowMaps.back().end(); |
| 2346 | E != EEnd; |
| 2347 | ++E) |
| 2348 | E->second.Destroy(); |
| 2349 | |
| 2350 | Visible.ShadowMaps.pop_back(); |
| 2351 | } |
| 2352 | }; |
| 2353 | |
| 2354 | } // end anonymous namespace |
| 2355 | |
| 2356 | void VisibleDeclsRecord::ShadowMapEntry::Add(NamedDecl *ND) { |
| 2357 | if (DeclOrVector.isNull()) { |
| 2358 | // 0 - > 1 elements: just set the single element information. |
| 2359 | DeclOrVector = ND; |
| 2360 | return; |
| 2361 | } |
| 2362 | |
| 2363 | if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { |
| 2364 | // 1 -> 2 elements: create the vector of results and push in the |
| 2365 | // existing declaration. |
| 2366 | DeclVector *Vec = new DeclVector; |
| 2367 | Vec->push_back(PrevND); |
| 2368 | DeclOrVector = Vec; |
| 2369 | } |
| 2370 | |
| 2371 | // Add the new element to the end of the vector. |
| 2372 | DeclOrVector.get<DeclVector*>()->push_back(ND); |
| 2373 | } |
| 2374 | |
| 2375 | void VisibleDeclsRecord::ShadowMapEntry::Destroy() { |
| 2376 | if (DeclVector *Vec = DeclOrVector.dyn_cast<DeclVector *>()) { |
| 2377 | delete Vec; |
| 2378 | DeclOrVector = ((NamedDecl *)0); |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | VisibleDeclsRecord::ShadowMapEntry::iterator |
| 2383 | VisibleDeclsRecord::ShadowMapEntry::begin() { |
| 2384 | if (DeclOrVector.isNull()) |
| 2385 | return 0; |
| 2386 | |
| 2387 | if (DeclOrVector.dyn_cast<NamedDecl *>()) |
| 2388 | return &reinterpret_cast<NamedDecl*&>(DeclOrVector); |
| 2389 | |
| 2390 | return DeclOrVector.get<DeclVector *>()->begin(); |
| 2391 | } |
| 2392 | |
| 2393 | VisibleDeclsRecord::ShadowMapEntry::iterator |
| 2394 | VisibleDeclsRecord::ShadowMapEntry::end() { |
| 2395 | if (DeclOrVector.isNull()) |
| 2396 | return 0; |
| 2397 | |
| 2398 | if (DeclOrVector.dyn_cast<NamedDecl *>()) |
| 2399 | return &reinterpret_cast<NamedDecl*&>(DeclOrVector) + 1; |
| 2400 | |
| 2401 | return DeclOrVector.get<DeclVector *>()->end(); |
| 2402 | } |
| 2403 | |
| 2404 | NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { |
Douglas Gregor | efcf16d | 2010-01-14 00:06:47 +0000 | [diff] [blame] | 2405 | // Look through using declarations. |
| 2406 | ND = ND->getUnderlyingDecl(); |
| 2407 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2408 | unsigned IDNS = ND->getIdentifierNamespace(); |
| 2409 | std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); |
| 2410 | for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); |
| 2411 | SM != SMEnd; ++SM) { |
| 2412 | ShadowMap::iterator Pos = SM->find(ND->getDeclName()); |
| 2413 | if (Pos == SM->end()) |
| 2414 | continue; |
| 2415 | |
| 2416 | for (ShadowMapEntry::iterator I = Pos->second.begin(), |
| 2417 | IEnd = Pos->second.end(); |
| 2418 | I != IEnd; ++I) { |
| 2419 | // A tag declaration does not hide a non-tag declaration. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2420 | if ((*I)->hasTagIdentifierNamespace() && |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2421 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 2422 | Decl::IDNS_ObjCProtocol))) |
| 2423 | continue; |
| 2424 | |
| 2425 | // Protocols are in distinct namespaces from everything else. |
| 2426 | if ((((*I)->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
| 2427 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
| 2428 | (*I)->getIdentifierNamespace() != IDNS) |
| 2429 | continue; |
| 2430 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2431 | // Functions and function templates in the same scope overload |
| 2432 | // rather than hide. FIXME: Look for hiding based on function |
| 2433 | // signatures! |
Douglas Gregor | def9107 | 2010-01-14 03:35:48 +0000 | [diff] [blame] | 2434 | if ((*I)->isFunctionOrFunctionTemplate() && |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2435 | ND->isFunctionOrFunctionTemplate() && |
| 2436 | SM == ShadowMaps.rbegin()) |
Douglas Gregor | def9107 | 2010-01-14 03:35:48 +0000 | [diff] [blame] | 2437 | continue; |
| 2438 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2439 | // We've found a declaration that hides this one. |
| 2440 | return *I; |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | return 0; |
| 2445 | } |
| 2446 | |
| 2447 | static void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result, |
| 2448 | bool QualifiedNameLookup, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2449 | bool InBaseClass, |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2450 | VisibleDeclConsumer &Consumer, |
| 2451 | VisibleDeclsRecord &Visited) { |
Douglas Gregor | 6202119 | 2010-02-04 23:42:48 +0000 | [diff] [blame] | 2452 | if (!Ctx) |
| 2453 | return; |
| 2454 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2455 | // Make sure we don't visit the same context twice. |
| 2456 | if (Visited.visitedContext(Ctx->getPrimaryContext())) |
| 2457 | return; |
| 2458 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 2459 | if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) |
| 2460 | Result.getSema().ForceDeclarationOfImplicitMembers(Class); |
| 2461 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2462 | // Enumerate all of the results in this context. |
| 2463 | for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx; |
| 2464 | CurCtx = CurCtx->getNextContext()) { |
| 2465 | for (DeclContext::decl_iterator D = CurCtx->decls_begin(), |
| 2466 | DEnd = CurCtx->decls_end(); |
| 2467 | D != DEnd; ++D) { |
| 2468 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
| 2469 | if (Result.isAcceptableDecl(ND)) { |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2470 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), InBaseClass); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2471 | Visited.add(ND); |
| 2472 | } |
| 2473 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 2474 | // Visit transparent contexts and inline namespaces inside this context. |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2475 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) { |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 2476 | if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2477 | LookupVisibleDecls(InnerCtx, Result, QualifiedNameLookup, InBaseClass, |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2478 | Consumer, Visited); |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | // Traverse using directives for qualified name lookup. |
| 2484 | if (QualifiedNameLookup) { |
| 2485 | ShadowContextRAII Shadow(Visited); |
| 2486 | DeclContext::udir_iterator I, E; |
| 2487 | for (llvm::tie(I, E) = Ctx->getUsingDirectives(); I != E; ++I) { |
| 2488 | LookupVisibleDecls((*I)->getNominatedNamespace(), Result, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2489 | QualifiedNameLookup, InBaseClass, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2493 | // Traverse the contexts of inherited C++ classes. |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2494 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2495 | if (!Record->hasDefinition()) |
| 2496 | return; |
| 2497 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2498 | for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), |
| 2499 | BEnd = Record->bases_end(); |
| 2500 | B != BEnd; ++B) { |
| 2501 | QualType BaseType = B->getType(); |
| 2502 | |
| 2503 | // Don't look into dependent bases, because name lookup can't look |
| 2504 | // there anyway. |
| 2505 | if (BaseType->isDependentType()) |
| 2506 | continue; |
| 2507 | |
| 2508 | const RecordType *Record = BaseType->getAs<RecordType>(); |
| 2509 | if (!Record) |
| 2510 | continue; |
| 2511 | |
| 2512 | // FIXME: It would be nice to be able to determine whether referencing |
| 2513 | // a particular member would be ambiguous. For example, given |
| 2514 | // |
| 2515 | // struct A { int member; }; |
| 2516 | // struct B { int member; }; |
| 2517 | // struct C : A, B { }; |
| 2518 | // |
| 2519 | // void f(C *c) { c->### } |
| 2520 | // |
| 2521 | // accessing 'member' would result in an ambiguity. However, we |
| 2522 | // could be smart enough to qualify the member with the base |
| 2523 | // class, e.g., |
| 2524 | // |
| 2525 | // c->B::member |
| 2526 | // |
| 2527 | // or |
| 2528 | // |
| 2529 | // c->A::member |
| 2530 | |
| 2531 | // Find results in this base class (and its bases). |
| 2532 | ShadowContextRAII Shadow(Visited); |
| 2533 | LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2534 | true, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2535 | } |
| 2536 | } |
| 2537 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2538 | // Traverse the contexts of Objective-C classes. |
| 2539 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { |
| 2540 | // Traverse categories. |
| 2541 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 2542 | Category; Category = Category->getNextClassCategory()) { |
| 2543 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2544 | LookupVisibleDecls(Category, Result, QualifiedNameLookup, false, |
| 2545 | Consumer, Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | // Traverse protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2549 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2550 | I = IFace->all_referenced_protocol_begin(), |
| 2551 | E = IFace->all_referenced_protocol_end(); I != E; ++I) { |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2552 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2553 | LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, |
| 2554 | Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | // Traverse the superclass. |
| 2558 | if (IFace->getSuperClass()) { |
| 2559 | ShadowContextRAII Shadow(Visited); |
| 2560 | LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2561 | true, Consumer, Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2562 | } |
Douglas Gregor | c220a18 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 2563 | |
| 2564 | // If there is an implementation, traverse it. We do this to find |
| 2565 | // synthesized ivars. |
| 2566 | if (IFace->getImplementation()) { |
| 2567 | ShadowContextRAII Shadow(Visited); |
| 2568 | LookupVisibleDecls(IFace->getImplementation(), Result, |
| 2569 | QualifiedNameLookup, true, Consumer, Visited); |
| 2570 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2571 | } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { |
| 2572 | for (ObjCProtocolDecl::protocol_iterator I = Protocol->protocol_begin(), |
| 2573 | E = Protocol->protocol_end(); I != E; ++I) { |
| 2574 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2575 | LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, |
| 2576 | Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2577 | } |
| 2578 | } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { |
| 2579 | for (ObjCCategoryDecl::protocol_iterator I = Category->protocol_begin(), |
| 2580 | E = Category->protocol_end(); I != E; ++I) { |
| 2581 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2582 | LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, |
| 2583 | Visited); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2584 | } |
Douglas Gregor | c220a18 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 2585 | |
| 2586 | // If there is an implementation, traverse it. |
| 2587 | if (Category->getImplementation()) { |
| 2588 | ShadowContextRAII Shadow(Visited); |
| 2589 | LookupVisibleDecls(Category->getImplementation(), Result, |
| 2590 | QualifiedNameLookup, true, Consumer, Visited); |
| 2591 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2592 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | static void LookupVisibleDecls(Scope *S, LookupResult &Result, |
| 2596 | UnqualUsingDirectiveSet &UDirs, |
| 2597 | VisibleDeclConsumer &Consumer, |
| 2598 | VisibleDeclsRecord &Visited) { |
| 2599 | if (!S) |
| 2600 | return; |
| 2601 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2602 | if (!S->getEntity() || |
| 2603 | (!S->getParent() && |
| 2604 | !Visited.alreadyVisitedContext((DeclContext *)S->getEntity())) || |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2605 | ((DeclContext *)S->getEntity())->isFunctionOrMethod()) { |
| 2606 | // Walk through the declarations in this Scope. |
| 2607 | for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 2608 | D != DEnd; ++D) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2609 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2610 | if (Result.isAcceptableDecl(ND)) { |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2611 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), false); |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2612 | Visited.add(ND); |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 2617 | // FIXME: C++ [temp.local]p8 |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2618 | DeclContext *Entity = 0; |
Douglas Gregor | e358201 | 2010-01-01 17:44:25 +0000 | [diff] [blame] | 2619 | if (S->getEntity()) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2620 | // Look into this scope's declaration context, along with any of its |
| 2621 | // parent lookup contexts (e.g., enclosing classes), up to the point |
| 2622 | // where we hit the context stored in the next outer scope. |
| 2623 | Entity = (DeclContext *)S->getEntity(); |
Douglas Gregor | 711be1e | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 2624 | DeclContext *OuterCtx = findOuterContext(S).first; // FIXME |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2625 | |
Douglas Gregor | dbdf5e7 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 2626 | for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2627 | Ctx = Ctx->getLookupParent()) { |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2628 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
| 2629 | if (Method->isInstanceMethod()) { |
| 2630 | // For instance methods, look for ivars in the method's interface. |
| 2631 | LookupResult IvarResult(Result.getSema(), Result.getLookupName(), |
| 2632 | Result.getNameLoc(), Sema::LookupMemberName); |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 2633 | if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) { |
Douglas Gregor | 6202119 | 2010-02-04 23:42:48 +0000 | [diff] [blame] | 2634 | LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false, |
| 2635 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 2636 | |
| 2637 | // Look for properties from which we can synthesize ivars, if |
| 2638 | // permitted. |
| 2639 | if (Result.getSema().getLangOptions().ObjCNonFragileABI2 && |
| 2640 | IFace->getImplementation() && |
| 2641 | Result.getLookupKind() == Sema::LookupOrdinaryName) { |
| 2642 | for (ObjCInterfaceDecl::prop_iterator |
| 2643 | P = IFace->prop_begin(), |
| 2644 | PEnd = IFace->prop_end(); |
| 2645 | P != PEnd; ++P) { |
| 2646 | if (Result.getSema().canSynthesizeProvisionalIvar(*P) && |
| 2647 | !IFace->lookupInstanceVariable((*P)->getIdentifier())) { |
| 2648 | Consumer.FoundDecl(*P, Visited.checkHidden(*P), false); |
| 2649 | Visited.add(*P); |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | // We've already performed all of the name lookup that we need |
| 2657 | // to for Objective-C methods; the next context will be the |
| 2658 | // outer scope. |
| 2659 | break; |
| 2660 | } |
| 2661 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2662 | if (Ctx->isFunctionOrMethod()) |
| 2663 | continue; |
| 2664 | |
| 2665 | LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2666 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2667 | } |
| 2668 | } else if (!S->getParent()) { |
| 2669 | // Look into the translation unit scope. We walk through the translation |
| 2670 | // unit's declaration context, because the Scope itself won't have all of |
| 2671 | // the declarations if we loaded a precompiled header. |
| 2672 | // FIXME: We would like the translation unit's Scope object to point to the |
| 2673 | // translation unit, so we don't need this special "if" branch. However, |
| 2674 | // doing so would force the normal C++ name-lookup code to look into the |
| 2675 | // translation unit decl when the IdentifierInfo chains would suffice. |
| 2676 | // Once we fix that problem (which is part of a more general "don't look |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2677 | // in DeclContexts unless we have to" optimization), we can eliminate this. |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2678 | Entity = Result.getSema().Context.getTranslationUnitDecl(); |
| 2679 | LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2680 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 539c5c3 | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 2681 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2682 | |
| 2683 | if (Entity) { |
| 2684 | // Lookup visible declarations in any namespaces found by using |
| 2685 | // directives. |
| 2686 | UnqualUsingDirectiveSet::const_iterator UI, UEnd; |
| 2687 | llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(Entity); |
| 2688 | for (; UI != UEnd; ++UI) |
| 2689 | LookupVisibleDecls(const_cast<DeclContext *>(UI->getNominatedNamespace()), |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2690 | Result, /*QualifiedNameLookup=*/false, |
| 2691 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | // Lookup names in the parent scope. |
| 2695 | ShadowContextRAII Shadow(Visited); |
| 2696 | LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited); |
| 2697 | } |
| 2698 | |
| 2699 | void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2700 | VisibleDeclConsumer &Consumer, |
| 2701 | bool IncludeGlobalScope) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2702 | // Determine the set of using directives available during |
| 2703 | // unqualified name lookup. |
| 2704 | Scope *Initial = S; |
| 2705 | UnqualUsingDirectiveSet UDirs; |
| 2706 | if (getLangOptions().CPlusPlus) { |
| 2707 | // Find the first namespace or translation-unit scope. |
| 2708 | while (S && !isNamespaceOrTranslationUnitScope(S)) |
| 2709 | S = S->getParent(); |
| 2710 | |
| 2711 | UDirs.visitScopeChain(Initial, S); |
| 2712 | } |
| 2713 | UDirs.done(); |
| 2714 | |
| 2715 | // Look for visible declarations. |
| 2716 | LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); |
| 2717 | VisibleDeclsRecord Visited; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2718 | if (!IncludeGlobalScope) |
| 2719 | Visited.visitedContext(Context.getTranslationUnitDecl()); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2720 | ShadowContextRAII Shadow(Visited); |
| 2721 | ::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited); |
| 2722 | } |
| 2723 | |
| 2724 | void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2725 | VisibleDeclConsumer &Consumer, |
| 2726 | bool IncludeGlobalScope) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2727 | LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); |
| 2728 | VisibleDeclsRecord Visited; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2729 | if (!IncludeGlobalScope) |
| 2730 | Visited.visitedContext(Context.getTranslationUnitDecl()); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2731 | ShadowContextRAII Shadow(Visited); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2732 | ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true, |
| 2733 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | //---------------------------------------------------------------------------- |
| 2737 | // Typo correction |
| 2738 | //---------------------------------------------------------------------------- |
| 2739 | |
| 2740 | namespace { |
| 2741 | class TypoCorrectionConsumer : public VisibleDeclConsumer { |
| 2742 | /// \brief The name written that is a typo in the source. |
| 2743 | llvm::StringRef Typo; |
| 2744 | |
| 2745 | /// \brief The results found that have the smallest edit distance |
| 2746 | /// found (so far) with the typo name. |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2747 | /// |
| 2748 | /// The boolean value indicates whether there is a keyword with this name. |
| 2749 | llvm::StringMap<bool, llvm::BumpPtrAllocator> BestResults; |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2750 | |
| 2751 | /// \brief The best edit distance found so far. |
| 2752 | unsigned BestEditDistance; |
| 2753 | |
| 2754 | public: |
| 2755 | explicit TypoCorrectionConsumer(IdentifierInfo *Typo) |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2756 | : Typo(Typo->getName()), |
| 2757 | BestEditDistance((std::numeric_limits<unsigned>::max)()) { } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2759 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass); |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 2760 | void FoundName(llvm::StringRef Name); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2761 | void addKeywordResult(ASTContext &Context, llvm::StringRef Keyword); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2762 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2763 | typedef llvm::StringMap<bool, llvm::BumpPtrAllocator>::iterator iterator; |
| 2764 | iterator begin() { return BestResults.begin(); } |
| 2765 | iterator end() { return BestResults.end(); } |
| 2766 | void erase(iterator I) { BestResults.erase(I); } |
| 2767 | unsigned size() const { return BestResults.size(); } |
| 2768 | bool empty() const { return BestResults.empty(); } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 7b824e8 | 2010-10-15 13:35:25 +0000 | [diff] [blame] | 2770 | bool &operator[](llvm::StringRef Name) { |
| 2771 | return BestResults[Name]; |
| 2772 | } |
| 2773 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2774 | unsigned getBestEditDistance() const { return BestEditDistance; } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2775 | }; |
| 2776 | |
| 2777 | } |
| 2778 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2779 | void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, |
| 2780 | bool InBaseClass) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2781 | // Don't consider hidden names for typo correction. |
| 2782 | if (Hiding) |
| 2783 | return; |
| 2784 | |
| 2785 | // Only consider entities with identifiers for names, ignoring |
| 2786 | // special names (constructors, overloaded operators, selectors, |
| 2787 | // etc.). |
| 2788 | IdentifierInfo *Name = ND->getIdentifier(); |
| 2789 | if (!Name) |
| 2790 | return; |
| 2791 | |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 2792 | FoundName(Name->getName()); |
| 2793 | } |
| 2794 | |
| 2795 | void TypoCorrectionConsumer::FoundName(llvm::StringRef Name) { |
Douglas Gregor | a119477 | 2010-10-19 22:14:33 +0000 | [diff] [blame] | 2796 | using namespace std; |
| 2797 | |
Douglas Gregor | 362a8f2 | 2010-10-19 19:39:10 +0000 | [diff] [blame] | 2798 | // Use a simple length-based heuristic to determine the minimum possible |
| 2799 | // edit distance. If the minimum isn't good enough, bail out early. |
| 2800 | unsigned MinED = abs((int)Name.size() - (int)Typo.size()); |
| 2801 | if (MinED > BestEditDistance || (MinED && Typo.size() / MinED < 3)) |
| 2802 | return; |
| 2803 | |
Douglas Gregor | a119477 | 2010-10-19 22:14:33 +0000 | [diff] [blame] | 2804 | // Compute an upper bound on the allowable edit distance, so that the |
| 2805 | // edit-distance algorithm can short-circuit. |
| 2806 | unsigned UpperBound = min(unsigned((Typo.size() + 2) / 3), BestEditDistance); |
| 2807 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2808 | // Compute the edit distance between the typo and the name of this |
| 2809 | // entity. If this edit distance is not worse than the best edit |
| 2810 | // distance we've seen so far, add it to the list of results. |
Douglas Gregor | a119477 | 2010-10-19 22:14:33 +0000 | [diff] [blame] | 2811 | unsigned ED = Typo.edit_distance(Name, true, UpperBound); |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 2812 | if (ED == 0) |
| 2813 | return; |
| 2814 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2815 | if (ED < BestEditDistance) { |
| 2816 | // This result is better than any we've seen before; clear out |
| 2817 | // the previous results. |
| 2818 | BestResults.clear(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2819 | BestEditDistance = ED; |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2820 | } else if (ED > BestEditDistance) { |
| 2821 | // This result is worse than the best results we've seen so far; |
| 2822 | // ignore it. |
| 2823 | return; |
| 2824 | } |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 2825 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2826 | // Add this name to the list of results. By not assigning a value, we |
| 2827 | // keep the current value if we've seen this name before (either as a |
| 2828 | // keyword or as a declaration), or get the default value (not a keyword) |
| 2829 | // if we haven't seen it before. |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 2830 | (void)BestResults[Name]; |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2833 | void TypoCorrectionConsumer::addKeywordResult(ASTContext &Context, |
| 2834 | llvm::StringRef Keyword) { |
| 2835 | // Compute the edit distance between the typo and this keyword. |
| 2836 | // If this edit distance is not worse than the best edit |
| 2837 | // distance we've seen so far, add it to the list of results. |
| 2838 | unsigned ED = Typo.edit_distance(Keyword); |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2839 | if (ED < BestEditDistance) { |
| 2840 | BestResults.clear(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2841 | BestEditDistance = ED; |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2842 | } else if (ED > BestEditDistance) { |
| 2843 | // This result is worse than the best results we've seen so far; |
| 2844 | // ignore it. |
| 2845 | return; |
| 2846 | } |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 2848 | BestResults[Keyword] = true; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 2851 | /// \brief Perform name lookup for a possible result for typo correction. |
| 2852 | static void LookupPotentialTypoResult(Sema &SemaRef, |
| 2853 | LookupResult &Res, |
| 2854 | IdentifierInfo *Name, |
| 2855 | Scope *S, CXXScopeSpec *SS, |
| 2856 | DeclContext *MemberContext, |
| 2857 | bool EnteringContext, |
| 2858 | Sema::CorrectTypoContext CTC) { |
| 2859 | Res.suppressDiagnostics(); |
| 2860 | Res.clear(); |
| 2861 | Res.setLookupName(Name); |
| 2862 | if (MemberContext) { |
| 2863 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) { |
| 2864 | if (CTC == Sema::CTC_ObjCIvarLookup) { |
| 2865 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) { |
| 2866 | Res.addDecl(Ivar); |
| 2867 | Res.resolveKind(); |
| 2868 | return; |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration(Name)) { |
| 2873 | Res.addDecl(Prop); |
| 2874 | Res.resolveKind(); |
| 2875 | return; |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | SemaRef.LookupQualifiedName(Res, MemberContext); |
| 2880 | return; |
| 2881 | } |
| 2882 | |
| 2883 | SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, |
| 2884 | EnteringContext); |
| 2885 | |
| 2886 | // Fake ivar lookup; this should really be part of |
| 2887 | // LookupParsedName. |
| 2888 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2889 | if (Method->isInstanceMethod() && Method->getClassInterface() && |
| 2890 | (Res.empty() || |
| 2891 | (Res.isSingleResult() && |
| 2892 | Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()))) { |
| 2893 | if (ObjCIvarDecl *IV |
| 2894 | = Method->getClassInterface()->lookupInstanceVariable(Name)) { |
| 2895 | Res.addDecl(IV); |
| 2896 | Res.resolveKind(); |
| 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | } |
| 2901 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2902 | /// \brief Try to "correct" a typo in the source code by finding |
| 2903 | /// visible declarations whose names are similar to the name that was |
| 2904 | /// present in the source code. |
| 2905 | /// |
| 2906 | /// \param Res the \c LookupResult structure that contains the name |
| 2907 | /// that was present in the source code along with the name-lookup |
| 2908 | /// criteria used to search for the name. On success, this structure |
| 2909 | /// will contain the results of name lookup. |
| 2910 | /// |
| 2911 | /// \param S the scope in which name lookup occurs. |
| 2912 | /// |
| 2913 | /// \param SS the nested-name-specifier that precedes the name we're |
| 2914 | /// looking for, if present. |
| 2915 | /// |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2916 | /// \param MemberContext if non-NULL, the context in which to look for |
| 2917 | /// a member access expression. |
| 2918 | /// |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 2919 | /// \param EnteringContext whether we're entering the context described by |
| 2920 | /// the nested-name-specifier SS. |
| 2921 | /// |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2922 | /// \param CTC The context in which typo correction occurs, which impacts the |
| 2923 | /// set of keywords permitted. |
| 2924 | /// |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2925 | /// \param OPT when non-NULL, the search for visible declarations will |
| 2926 | /// also walk the protocols in the qualified interfaces of \p OPT. |
| 2927 | /// |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2928 | /// \returns the corrected name if the typo was corrected, otherwise returns an |
| 2929 | /// empty \c DeclarationName. When a typo was corrected, the result structure |
| 2930 | /// may contain the results of name lookup for the correct name or it may be |
| 2931 | /// empty. |
| 2932 | DeclarationName Sema::CorrectTypo(LookupResult &Res, Scope *S, CXXScopeSpec *SS, |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2933 | DeclContext *MemberContext, |
| 2934 | bool EnteringContext, |
| 2935 | CorrectTypoContext CTC, |
| 2936 | const ObjCObjectPointerType *OPT) { |
Douglas Gregor | a0068fc | 2010-07-09 17:35:33 +0000 | [diff] [blame] | 2937 | if (Diags.hasFatalErrorOccurred() || !getLangOptions().SpellChecking) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2938 | return DeclarationName(); |
Ted Kremenek | 1dac341 | 2010-01-06 00:23:04 +0000 | [diff] [blame] | 2939 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2940 | // We only attempt to correct typos for identifiers. |
| 2941 | IdentifierInfo *Typo = Res.getLookupName().getAsIdentifierInfo(); |
| 2942 | if (!Typo) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2943 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2944 | |
| 2945 | // If the scope specifier itself was invalid, don't try to correct |
| 2946 | // typos. |
| 2947 | if (SS && SS->isInvalid()) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2948 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2949 | |
| 2950 | // Never try to correct typos during template deduction or |
| 2951 | // instantiation. |
| 2952 | if (!ActiveTemplateInstantiations.empty()) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2953 | return DeclarationName(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2955 | TypoCorrectionConsumer Consumer(Typo); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2956 | |
| 2957 | // Perform name lookup to find visible, similarly-named entities. |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 2958 | bool IsUnqualifiedLookup = false; |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2959 | if (MemberContext) { |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2960 | LookupVisibleDecls(MemberContext, Res.getLookupKind(), Consumer); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2961 | |
| 2962 | // Look in qualified interfaces. |
| 2963 | if (OPT) { |
| 2964 | for (ObjCObjectPointerType::qual_iterator |
| 2965 | I = OPT->qual_begin(), E = OPT->qual_end(); |
| 2966 | I != E; ++I) |
| 2967 | LookupVisibleDecls(*I, Res.getLookupKind(), Consumer); |
| 2968 | } |
| 2969 | } else if (SS && SS->isSet()) { |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2970 | DeclContext *DC = computeDeclContext(*SS, EnteringContext); |
| 2971 | if (!DC) |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 2972 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 2974 | // Provide a stop gap for files that are just seriously broken. Trying |
| 2975 | // to correct all typos can turn into a HUGE performance penalty, causing |
| 2976 | // some files to take minutes to get rejected by the parser. |
| 2977 | if (TyposCorrected + UnqualifiedTyposCorrected.size() >= 20) |
| 2978 | return DeclarationName(); |
| 2979 | ++TyposCorrected; |
| 2980 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 2981 | LookupVisibleDecls(DC, Res.getLookupKind(), Consumer); |
| 2982 | } else { |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 2983 | IsUnqualifiedLookup = true; |
| 2984 | UnqualifiedTyposCorrectedMap::iterator Cached |
| 2985 | = UnqualifiedTyposCorrected.find(Typo); |
| 2986 | if (Cached == UnqualifiedTyposCorrected.end()) { |
| 2987 | // Provide a stop gap for files that are just seriously broken. Trying |
| 2988 | // to correct all typos can turn into a HUGE performance penalty, causing |
| 2989 | // some files to take minutes to get rejected by the parser. |
| 2990 | if (TyposCorrected + UnqualifiedTyposCorrected.size() >= 20) |
| 2991 | return DeclarationName(); |
| 2992 | |
| 2993 | // For unqualified lookup, look through all of the names that we have |
| 2994 | // seen in this translation unit. |
| 2995 | for (IdentifierTable::iterator I = Context.Idents.begin(), |
| 2996 | IEnd = Context.Idents.end(); |
| 2997 | I != IEnd; ++I) |
| 2998 | Consumer.FoundName(I->getKey()); |
| 2999 | |
| 3000 | // Walk through identifiers in external identifier sources. |
| 3001 | if (IdentifierInfoLookup *External |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 3002 | = Context.Idents.getExternalIdentifierLookup()) { |
Ted Kremenek | 7a054b1 | 2010-11-07 06:11:33 +0000 | [diff] [blame^] | 3003 | llvm::OwningPtr<IdentifierIterator> Iter(External->getIdentifiers()); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3004 | do { |
| 3005 | llvm::StringRef Name = Iter->Next(); |
| 3006 | if (Name.empty()) |
| 3007 | break; |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 3008 | |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3009 | Consumer.FoundName(Name); |
| 3010 | } while (true); |
| 3011 | } |
| 3012 | } else { |
| 3013 | // Use the cached value, unless it's a keyword. In the keyword case, we'll |
| 3014 | // end up adding the keyword below. |
| 3015 | if (Cached->second.first.empty()) |
| 3016 | return DeclarationName(); |
| 3017 | |
| 3018 | if (!Cached->second.second) |
| 3019 | Consumer.FoundName(Cached->second.first); |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 3020 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3023 | // Add context-dependent keywords. |
| 3024 | bool WantTypeSpecifiers = false; |
| 3025 | bool WantExpressionKeywords = false; |
| 3026 | bool WantCXXNamedCasts = false; |
| 3027 | bool WantRemainingKeywords = false; |
| 3028 | switch (CTC) { |
| 3029 | case CTC_Unknown: |
| 3030 | WantTypeSpecifiers = true; |
| 3031 | WantExpressionKeywords = true; |
| 3032 | WantCXXNamedCasts = true; |
| 3033 | WantRemainingKeywords = true; |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 3034 | |
| 3035 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 3036 | if (Method->getClassInterface() && |
| 3037 | Method->getClassInterface()->getSuperClass()) |
| 3038 | Consumer.addKeywordResult(Context, "super"); |
| 3039 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3040 | break; |
| 3041 | |
| 3042 | case CTC_NoKeywords: |
| 3043 | break; |
| 3044 | |
| 3045 | case CTC_Type: |
| 3046 | WantTypeSpecifiers = true; |
| 3047 | break; |
| 3048 | |
| 3049 | case CTC_ObjCMessageReceiver: |
| 3050 | Consumer.addKeywordResult(Context, "super"); |
| 3051 | // Fall through to handle message receivers like expressions. |
| 3052 | |
| 3053 | case CTC_Expression: |
| 3054 | if (getLangOptions().CPlusPlus) |
| 3055 | WantTypeSpecifiers = true; |
| 3056 | WantExpressionKeywords = true; |
| 3057 | // Fall through to get C++ named casts. |
| 3058 | |
| 3059 | case CTC_CXXCasts: |
| 3060 | WantCXXNamedCasts = true; |
| 3061 | break; |
| 3062 | |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3063 | case CTC_ObjCPropertyLookup: |
| 3064 | // FIXME: Add "isa"? |
| 3065 | break; |
| 3066 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3067 | case CTC_MemberLookup: |
| 3068 | if (getLangOptions().CPlusPlus) |
| 3069 | Consumer.addKeywordResult(Context, "template"); |
| 3070 | break; |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3071 | |
| 3072 | case CTC_ObjCIvarLookup: |
| 3073 | break; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | if (WantTypeSpecifiers) { |
| 3077 | // Add type-specifier keywords to the set of results. |
| 3078 | const char *CTypeSpecs[] = { |
| 3079 | "char", "const", "double", "enum", "float", "int", "long", "short", |
| 3080 | "signed", "struct", "union", "unsigned", "void", "volatile", "_Bool", |
| 3081 | "_Complex", "_Imaginary", |
| 3082 | // storage-specifiers as well |
| 3083 | "extern", "inline", "static", "typedef" |
| 3084 | }; |
| 3085 | |
| 3086 | const unsigned NumCTypeSpecs = sizeof(CTypeSpecs) / sizeof(CTypeSpecs[0]); |
| 3087 | for (unsigned I = 0; I != NumCTypeSpecs; ++I) |
| 3088 | Consumer.addKeywordResult(Context, CTypeSpecs[I]); |
| 3089 | |
| 3090 | if (getLangOptions().C99) |
| 3091 | Consumer.addKeywordResult(Context, "restrict"); |
| 3092 | if (getLangOptions().Bool || getLangOptions().CPlusPlus) |
| 3093 | Consumer.addKeywordResult(Context, "bool"); |
| 3094 | |
| 3095 | if (getLangOptions().CPlusPlus) { |
| 3096 | Consumer.addKeywordResult(Context, "class"); |
| 3097 | Consumer.addKeywordResult(Context, "typename"); |
| 3098 | Consumer.addKeywordResult(Context, "wchar_t"); |
| 3099 | |
| 3100 | if (getLangOptions().CPlusPlus0x) { |
| 3101 | Consumer.addKeywordResult(Context, "char16_t"); |
| 3102 | Consumer.addKeywordResult(Context, "char32_t"); |
| 3103 | Consumer.addKeywordResult(Context, "constexpr"); |
| 3104 | Consumer.addKeywordResult(Context, "decltype"); |
| 3105 | Consumer.addKeywordResult(Context, "thread_local"); |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | if (getLangOptions().GNUMode) |
| 3110 | Consumer.addKeywordResult(Context, "typeof"); |
| 3111 | } |
| 3112 | |
Douglas Gregor | d0785ea | 2010-05-18 16:30:22 +0000 | [diff] [blame] | 3113 | if (WantCXXNamedCasts && getLangOptions().CPlusPlus) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3114 | Consumer.addKeywordResult(Context, "const_cast"); |
| 3115 | Consumer.addKeywordResult(Context, "dynamic_cast"); |
| 3116 | Consumer.addKeywordResult(Context, "reinterpret_cast"); |
| 3117 | Consumer.addKeywordResult(Context, "static_cast"); |
| 3118 | } |
| 3119 | |
| 3120 | if (WantExpressionKeywords) { |
| 3121 | Consumer.addKeywordResult(Context, "sizeof"); |
| 3122 | if (getLangOptions().Bool || getLangOptions().CPlusPlus) { |
| 3123 | Consumer.addKeywordResult(Context, "false"); |
| 3124 | Consumer.addKeywordResult(Context, "true"); |
| 3125 | } |
| 3126 | |
| 3127 | if (getLangOptions().CPlusPlus) { |
| 3128 | const char *CXXExprs[] = { |
| 3129 | "delete", "new", "operator", "throw", "typeid" |
| 3130 | }; |
| 3131 | const unsigned NumCXXExprs = sizeof(CXXExprs) / sizeof(CXXExprs[0]); |
| 3132 | for (unsigned I = 0; I != NumCXXExprs; ++I) |
| 3133 | Consumer.addKeywordResult(Context, CXXExprs[I]); |
| 3134 | |
| 3135 | if (isa<CXXMethodDecl>(CurContext) && |
| 3136 | cast<CXXMethodDecl>(CurContext)->isInstance()) |
| 3137 | Consumer.addKeywordResult(Context, "this"); |
| 3138 | |
| 3139 | if (getLangOptions().CPlusPlus0x) { |
| 3140 | Consumer.addKeywordResult(Context, "alignof"); |
| 3141 | Consumer.addKeywordResult(Context, "nullptr"); |
| 3142 | } |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | if (WantRemainingKeywords) { |
| 3147 | if (getCurFunctionOrMethodDecl() || getCurBlock()) { |
| 3148 | // Statements. |
| 3149 | const char *CStmts[] = { |
| 3150 | "do", "else", "for", "goto", "if", "return", "switch", "while" }; |
| 3151 | const unsigned NumCStmts = sizeof(CStmts) / sizeof(CStmts[0]); |
| 3152 | for (unsigned I = 0; I != NumCStmts; ++I) |
| 3153 | Consumer.addKeywordResult(Context, CStmts[I]); |
| 3154 | |
| 3155 | if (getLangOptions().CPlusPlus) { |
| 3156 | Consumer.addKeywordResult(Context, "catch"); |
| 3157 | Consumer.addKeywordResult(Context, "try"); |
| 3158 | } |
| 3159 | |
| 3160 | if (S && S->getBreakParent()) |
| 3161 | Consumer.addKeywordResult(Context, "break"); |
| 3162 | |
| 3163 | if (S && S->getContinueParent()) |
| 3164 | Consumer.addKeywordResult(Context, "continue"); |
| 3165 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3166 | if (!getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3167 | Consumer.addKeywordResult(Context, "case"); |
| 3168 | Consumer.addKeywordResult(Context, "default"); |
| 3169 | } |
| 3170 | } else { |
| 3171 | if (getLangOptions().CPlusPlus) { |
| 3172 | Consumer.addKeywordResult(Context, "namespace"); |
| 3173 | Consumer.addKeywordResult(Context, "template"); |
| 3174 | } |
| 3175 | |
| 3176 | if (S && S->isClassScope()) { |
| 3177 | Consumer.addKeywordResult(Context, "explicit"); |
| 3178 | Consumer.addKeywordResult(Context, "friend"); |
| 3179 | Consumer.addKeywordResult(Context, "mutable"); |
| 3180 | Consumer.addKeywordResult(Context, "private"); |
| 3181 | Consumer.addKeywordResult(Context, "protected"); |
| 3182 | Consumer.addKeywordResult(Context, "public"); |
| 3183 | Consumer.addKeywordResult(Context, "virtual"); |
| 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | if (getLangOptions().CPlusPlus) { |
| 3188 | Consumer.addKeywordResult(Context, "using"); |
| 3189 | |
| 3190 | if (getLangOptions().CPlusPlus0x) |
| 3191 | Consumer.addKeywordResult(Context, "static_assert"); |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | // If we haven't found anything, we're done. |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3196 | if (Consumer.empty()) { |
| 3197 | // If this was an unqualified lookup, note that no correction was found. |
| 3198 | if (IsUnqualifiedLookup) |
| 3199 | (void)UnqualifiedTyposCorrected[Typo]; |
| 3200 | |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 3201 | return DeclarationName(); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3202 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3203 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3204 | // Make sure that the user typed at least 3 characters for each correction |
| 3205 | // made. Otherwise, we don't even both looking at the results. |
Douglas Gregor | 53e4b55 | 2010-10-26 17:18:00 +0000 | [diff] [blame] | 3206 | |
| 3207 | // We also suppress exact matches; those should be handled by a |
| 3208 | // different mechanism (e.g., one that introduces qualification in |
| 3209 | // C++). |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3210 | unsigned ED = Consumer.getBestEditDistance(); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3211 | if (ED > 0 && Typo->getName().size() / ED < 3) { |
| 3212 | // If this was an unqualified lookup, note that no correction was found. |
Douglas Gregor | 157a3ff | 2010-10-27 14:20:34 +0000 | [diff] [blame] | 3213 | if (IsUnqualifiedLookup) |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3214 | (void)UnqualifiedTyposCorrected[Typo]; |
| 3215 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3216 | return DeclarationName(); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3217 | } |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3218 | |
| 3219 | // Weed out any names that could not be found by name lookup. |
Douglas Gregor | 6eaac8b | 2010-10-15 16:49:56 +0000 | [diff] [blame] | 3220 | bool LastLookupWasAccepted = false; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3221 | for (TypoCorrectionConsumer::iterator I = Consumer.begin(), |
| 3222 | IEnd = Consumer.end(); |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3223 | I != IEnd; /* Increment in loop. */) { |
| 3224 | // Keywords are always found. |
| 3225 | if (I->second) { |
| 3226 | ++I; |
| 3227 | continue; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3228 | } |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3229 | |
| 3230 | // Perform name lookup on this name. |
| 3231 | IdentifierInfo *Name = &Context.Idents.get(I->getKey()); |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3232 | LookupPotentialTypoResult(*this, Res, Name, S, SS, MemberContext, |
| 3233 | EnteringContext, CTC); |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3234 | |
| 3235 | switch (Res.getResultKind()) { |
| 3236 | case LookupResult::NotFound: |
| 3237 | case LookupResult::NotFoundInCurrentInstantiation: |
| 3238 | case LookupResult::Ambiguous: |
| 3239 | // We didn't find this name in our scope, or didn't like what we found; |
| 3240 | // ignore it. |
| 3241 | Res.suppressDiagnostics(); |
| 3242 | { |
| 3243 | TypoCorrectionConsumer::iterator Next = I; |
| 3244 | ++Next; |
| 3245 | Consumer.erase(I); |
| 3246 | I = Next; |
| 3247 | } |
Douglas Gregor | 6eaac8b | 2010-10-15 16:49:56 +0000 | [diff] [blame] | 3248 | LastLookupWasAccepted = false; |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3249 | break; |
| 3250 | |
| 3251 | case LookupResult::Found: |
| 3252 | case LookupResult::FoundOverloaded: |
| 3253 | case LookupResult::FoundUnresolvedValue: |
| 3254 | ++I; |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3255 | LastLookupWasAccepted = true; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3256 | break; |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | if (Res.isAmbiguous()) { |
| 3260 | // We don't deal with ambiguities. |
| 3261 | Res.suppressDiagnostics(); |
| 3262 | Res.clear(); |
| 3263 | return DeclarationName(); |
| 3264 | } |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3265 | } |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3266 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3267 | // If only a single name remains, return that result. |
Douglas Gregor | 6eaac8b | 2010-10-15 16:49:56 +0000 | [diff] [blame] | 3268 | if (Consumer.size() == 1) { |
| 3269 | IdentifierInfo *Name = &Context.Idents.get(Consumer.begin()->getKey()); |
Douglas Gregor | f98402d | 2010-10-20 01:01:57 +0000 | [diff] [blame] | 3270 | if (Consumer.begin()->second) { |
| 3271 | Res.suppressDiagnostics(); |
| 3272 | Res.clear(); |
Douglas Gregor | 53e4b55 | 2010-10-26 17:18:00 +0000 | [diff] [blame] | 3273 | |
| 3274 | // Don't correct to a keyword that's the same as the typo; the keyword |
| 3275 | // wasn't actually in scope. |
| 3276 | if (ED == 0) { |
| 3277 | Res.setLookupName(Typo); |
| 3278 | return DeclarationName(); |
| 3279 | } |
| 3280 | |
Douglas Gregor | f98402d | 2010-10-20 01:01:57 +0000 | [diff] [blame] | 3281 | } else if (!LastLookupWasAccepted) { |
Douglas Gregor | 6eaac8b | 2010-10-15 16:49:56 +0000 | [diff] [blame] | 3282 | // Perform name lookup on this name. |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3283 | LookupPotentialTypoResult(*this, Res, Name, S, SS, MemberContext, |
| 3284 | EnteringContext, CTC); |
Douglas Gregor | 6eaac8b | 2010-10-15 16:49:56 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3287 | // Record the correction for unqualified lookup. |
| 3288 | if (IsUnqualifiedLookup) |
| 3289 | UnqualifiedTyposCorrected[Typo] |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3290 | = std::make_pair(Name->getName(), Consumer.begin()->second); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3291 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3292 | return &Context.Idents.get(Consumer.begin()->getKey()); |
Douglas Gregor | 6eaac8b | 2010-10-15 16:49:56 +0000 | [diff] [blame] | 3293 | } |
Douglas Gregor | 7b824e8 | 2010-10-15 13:35:25 +0000 | [diff] [blame] | 3294 | else if (Consumer.size() > 1 && CTC == CTC_ObjCMessageReceiver |
| 3295 | && Consumer["super"]) { |
| 3296 | // Prefix 'super' when we're completing in a message-receiver |
| 3297 | // context. |
| 3298 | Res.suppressDiagnostics(); |
| 3299 | Res.clear(); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | 53e4b55 | 2010-10-26 17:18:00 +0000 | [diff] [blame] | 3301 | // Don't correct to a keyword that's the same as the typo; the keyword |
| 3302 | // wasn't actually in scope. |
| 3303 | if (ED == 0) { |
| 3304 | Res.setLookupName(Typo); |
| 3305 | return DeclarationName(); |
| 3306 | } |
| 3307 | |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3308 | // Record the correction for unqualified lookup. |
| 3309 | if (IsUnqualifiedLookup) |
| 3310 | UnqualifiedTyposCorrected[Typo] |
Douglas Gregor | 9a632ea | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3311 | = std::make_pair("super", Consumer.begin()->second); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3312 | |
Douglas Gregor | 7b824e8 | 2010-10-15 13:35:25 +0000 | [diff] [blame] | 3313 | return &Context.Idents.get("super"); |
| 3314 | } |
| 3315 | |
Douglas Gregor | e24b575 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 3316 | Res.suppressDiagnostics(); |
| 3317 | Res.setLookupName(Typo); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3318 | Res.clear(); |
Douglas Gregor | 3eedbb0 | 2010-10-20 01:32:02 +0000 | [diff] [blame] | 3319 | // Record the correction for unqualified lookup. |
| 3320 | if (IsUnqualifiedLookup) |
| 3321 | (void)UnqualifiedTyposCorrected[Typo]; |
| 3322 | |
Douglas Gregor | 931f98a | 2010-04-14 17:09:22 +0000 | [diff] [blame] | 3323 | return DeclarationName(); |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3324 | } |