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