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