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