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