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