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