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