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