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