Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1 | //===--------------------- SemaLookup.cpp - Name Lookup ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements name lookup for C, C++, Objective-C, and |
| 11 | // Objective-C++. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Lookup.h" |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclCXX.h" |
Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclLookups.h" |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Douglas Gregor | be75925 | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Builtins.h" |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 26 | #include "clang/Basic/LangOptions.h" |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 27 | #include "clang/Lex/HeaderSearch.h" |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 28 | #include "clang/Lex/ModuleLoader.h" |
Richard Smith | 4caa449 | 2015-05-15 02:34:32 +0000 | [diff] [blame] | 29 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 30 | #include "clang/Sema/DeclSpec.h" |
| 31 | #include "clang/Sema/ExternalSemaSource.h" |
| 32 | #include "clang/Sema/Overload.h" |
| 33 | #include "clang/Sema/Scope.h" |
| 34 | #include "clang/Sema/ScopeInfo.h" |
| 35 | #include "clang/Sema/Sema.h" |
| 36 | #include "clang/Sema/SemaInternal.h" |
| 37 | #include "clang/Sema/TemplateDeduction.h" |
| 38 | #include "clang/Sema/TypoCorrection.h" |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/SetVector.h" |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 0afa7f6 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 83cfc7c | 2011-07-18 01:54:02 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/TinyPtrVector.h" |
Kaelyn Uhrain | 5986c3e | 2012-02-15 22:14:18 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/edit_distance.h" |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ErrorHandling.h" |
Nick Lewycky | 13668f2 | 2012-04-03 20:26:45 +0000 | [diff] [blame] | 46 | #include <algorithm> |
| 47 | #include <iterator> |
Douglas Gregor | 0afa7f6 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 48 | #include <limits> |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 49 | #include <list> |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 50 | #include <map> |
Nick Lewycky | 13668f2 | 2012-04-03 20:26:45 +0000 | [diff] [blame] | 51 | #include <set> |
| 52 | #include <utility> |
| 53 | #include <vector> |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 54 | |
| 55 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 56 | using namespace sema; |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 57 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | class UnqualUsingEntry { |
| 60 | const DeclContext *Nominated; |
| 61 | const DeclContext *CommonAncestor; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 62 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 63 | public: |
| 64 | UnqualUsingEntry(const DeclContext *Nominated, |
| 65 | const DeclContext *CommonAncestor) |
| 66 | : Nominated(Nominated), CommonAncestor(CommonAncestor) { |
| 67 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 68 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 69 | const DeclContext *getCommonAncestor() const { |
| 70 | return CommonAncestor; |
| 71 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 72 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 73 | const DeclContext *getNominatedNamespace() const { |
| 74 | return Nominated; |
| 75 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 76 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 77 | // Sort by the pointer value of the common ancestor. |
| 78 | struct Comparator { |
| 79 | bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { |
| 80 | return L.getCommonAncestor() < R.getCommonAncestor(); |
| 81 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 82 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 83 | bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { |
| 84 | return E.getCommonAncestor() < DC; |
| 85 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 86 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 87 | bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { |
| 88 | return DC < E.getCommonAncestor(); |
| 89 | } |
| 90 | }; |
| 91 | }; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 92 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 93 | /// A collection of using directives, as used by C++ unqualified |
| 94 | /// lookup. |
| 95 | class UnqualUsingDirectiveSet { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 96 | typedef SmallVector<UnqualUsingEntry, 8> ListTy; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 97 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 98 | ListTy list; |
| 99 | llvm::SmallPtrSet<DeclContext*, 8> visited; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 100 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 101 | public: |
| 102 | UnqualUsingDirectiveSet() {} |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 103 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 104 | void visitScopeChain(Scope *S, Scope *InnermostFileScope) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 105 | // C++ [namespace.udir]p1: |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 106 | // During unqualified name lookup, the names appear as if they |
| 107 | // were declared in the nearest enclosing namespace which contains |
| 108 | // both the using-directive and the nominated namespace. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 109 | DeclContext *InnermostFileDC = InnermostFileScope->getEntity(); |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 110 | assert(InnermostFileDC && InnermostFileDC->isFileContext()); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 111 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 112 | for (; S; S = S->getParent()) { |
Nick Lewycky | 2bd636f | 2012-03-13 04:12:34 +0000 | [diff] [blame] | 113 | // C++ [namespace.udir]p1: |
| 114 | // A using-directive shall not appear in class scope, but may |
| 115 | // appear in namespace scope or in block scope. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 116 | DeclContext *Ctx = S->getEntity(); |
Nick Lewycky | 2bd636f | 2012-03-13 04:12:34 +0000 | [diff] [blame] | 117 | if (Ctx && Ctx->isFileContext()) { |
| 118 | visit(Ctx, Ctx); |
| 119 | } else if (!Ctx || Ctx->isFunctionOrMethod()) { |
Aaron Ballman | 5df6aa4 | 2014-03-17 17:03:37 +0000 | [diff] [blame] | 120 | for (auto *I : S->using_directives()) |
| 121 | visit(I, InnermostFileDC); |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 122 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 125 | |
| 126 | // Visits a context and collect all of its using directives |
| 127 | // recursively. Treats all using directives as if they were |
| 128 | // declared in the context. |
| 129 | // |
| 130 | // A given context is only every visited once, so it is important |
| 131 | // that contexts be visited from the inside out in order to get |
| 132 | // the effective DCs right. |
| 133 | void visit(DeclContext *DC, DeclContext *EffectiveDC) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 134 | if (!visited.insert(DC).second) |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | addUsingDirectives(DC, EffectiveDC); |
| 138 | } |
| 139 | |
| 140 | // Visits a using directive and collects all of its using |
| 141 | // directives recursively. Treats all using directives as if they |
| 142 | // were declared in the effective DC. |
| 143 | void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 144 | DeclContext *NS = UD->getNominatedNamespace(); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 145 | if (!visited.insert(NS).second) |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 146 | return; |
| 147 | |
| 148 | addUsingDirective(UD, EffectiveDC); |
| 149 | addUsingDirectives(NS, EffectiveDC); |
| 150 | } |
| 151 | |
| 152 | // Adds all the using directives in a context (and those nominated |
| 153 | // by its using directives, transitively) as if they appeared in |
| 154 | // the given effective context. |
| 155 | void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 156 | SmallVector<DeclContext*,4> queue; |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 157 | while (true) { |
Aaron Ballman | 804a7fb | 2014-03-17 17:14:12 +0000 | [diff] [blame] | 158 | for (auto UD : DC->using_directives()) { |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 159 | DeclContext *NS = UD->getNominatedNamespace(); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 160 | if (visited.insert(NS).second) { |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 161 | addUsingDirective(UD, EffectiveDC); |
| 162 | queue.push_back(NS); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (queue.empty()) |
| 167 | return; |
| 168 | |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 169 | DC = queue.pop_back_val(); |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // Add a using directive as if it had been declared in the given |
| 174 | // context. This helps implement C++ [namespace.udir]p3: |
| 175 | // The using-directive is transitive: if a scope contains a |
| 176 | // using-directive that nominates a second namespace that itself |
| 177 | // contains using-directives, the effect is as if the |
| 178 | // using-directives from the second namespace also appeared in |
| 179 | // the first. |
| 180 | void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { |
| 181 | // Find the common ancestor between the effective context and |
| 182 | // the nominated namespace. |
| 183 | DeclContext *Common = UD->getNominatedNamespace(); |
| 184 | while (!Common->Encloses(EffectiveDC)) |
| 185 | Common = Common->getParent(); |
John McCall | 9757d03 | 2009-11-10 09:20:04 +0000 | [diff] [blame] | 186 | Common = Common->getPrimaryContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 187 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 188 | list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); |
| 189 | } |
| 190 | |
| 191 | void done() { |
| 192 | std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator()); |
| 193 | } |
| 194 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 195 | typedef ListTy::const_iterator const_iterator; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 196 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 197 | const_iterator begin() const { return list.begin(); } |
| 198 | const_iterator end() const { return list.end(); } |
| 199 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 200 | llvm::iterator_range<const_iterator> |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 201 | getNamespacesFor(DeclContext *DC) const { |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 202 | return llvm::make_range(std::equal_range(begin(), end(), |
| 203 | DC->getPrimaryContext(), |
| 204 | UnqualUsingEntry::Comparator())); |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 205 | } |
| 206 | }; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 209 | // Retrieve the set of identifier namespaces that correspond to a |
| 210 | // specific kind of name lookup. |
John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 211 | static inline unsigned getIDNS(Sema::LookupNameKind NameKind, |
| 212 | bool CPlusPlus, |
| 213 | bool Redeclaration) { |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 214 | unsigned IDNS = 0; |
| 215 | switch (NameKind) { |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 216 | case Sema::LookupObjCImplicitSelfParam: |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 217 | case Sema::LookupOrdinaryName: |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 218 | case Sema::LookupRedeclarationWithLinkage: |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 219 | case Sema::LookupLocalFriendName: |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 220 | IDNS = Decl::IDNS_Ordinary; |
John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 221 | if (CPlusPlus) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 222 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 223 | if (Redeclaration) |
| 224 | IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; |
John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 225 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 226 | if (Redeclaration) |
| 227 | IDNS |= Decl::IDNS_LocalExtern; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 228 | break; |
| 229 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 230 | case Sema::LookupOperatorName: |
| 231 | // Operator lookup is its own crazy thing; it is not the same |
| 232 | // as (e.g.) looking up an operator name for redeclaration. |
| 233 | assert(!Redeclaration && "cannot do redeclaration operator lookup"); |
| 234 | IDNS = Decl::IDNS_NonMemberOperator; |
| 235 | break; |
| 236 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 237 | case Sema::LookupTagName: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 238 | if (CPlusPlus) { |
| 239 | IDNS = Decl::IDNS_Type; |
| 240 | |
| 241 | // When looking for a redeclaration of a tag name, we add: |
| 242 | // 1) TagFriend to find undeclared friend decls |
| 243 | // 2) Namespace because they can't "overload" with tag decls. |
| 244 | // 3) Tag because it includes class templates, which can't |
| 245 | // "overload" with tag decls. |
| 246 | if (Redeclaration) |
| 247 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; |
| 248 | } else { |
| 249 | IDNS = Decl::IDNS_Tag; |
| 250 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 251 | break; |
Richard Smith | 83e78f5 | 2014-04-11 01:03:38 +0000 | [diff] [blame] | 252 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 253 | case Sema::LookupLabel: |
| 254 | IDNS = Decl::IDNS_Label; |
| 255 | break; |
Richard Smith | 83e78f5 | 2014-04-11 01:03:38 +0000 | [diff] [blame] | 256 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 257 | case Sema::LookupMemberName: |
| 258 | IDNS = Decl::IDNS_Member; |
| 259 | if (CPlusPlus) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 261 | break; |
| 262 | |
| 263 | case Sema::LookupNestedNameSpecifierName: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 264 | IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; |
| 265 | break; |
| 266 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 267 | case Sema::LookupNamespaceName: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 268 | IDNS = Decl::IDNS_Namespace; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 269 | break; |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 270 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 271 | case Sema::LookupUsingDeclName: |
Richard Smith | 83e78f5 | 2014-04-11 01:03:38 +0000 | [diff] [blame] | 272 | assert(Redeclaration && "should only be used for redecl lookup"); |
| 273 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member | |
| 274 | Decl::IDNS_Using | Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend | |
| 275 | Decl::IDNS_LocalExtern; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 276 | break; |
| 277 | |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 278 | case Sema::LookupObjCProtocolName: |
| 279 | IDNS = Decl::IDNS_ObjCProtocol; |
| 280 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 282 | case Sema::LookupAnyName: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 283 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 284 | | Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol |
| 285 | | Decl::IDNS_Type; |
| 286 | break; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 287 | } |
| 288 | return IDNS; |
| 289 | } |
| 290 | |
John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 291 | void LookupResult::configure() { |
Kaelyn Takata | fc8c61a | 2014-11-11 23:00:42 +0000 | [diff] [blame] | 292 | IDNS = getIDNS(LookupKind, getSema().getLangOpts().CPlusPlus, |
John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 293 | isForRedeclaration()); |
Douglas Gregor | bcf0a47 | 2010-03-24 05:07:21 +0000 | [diff] [blame] | 294 | |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 295 | // If we're looking for one of the allocation or deallocation |
| 296 | // operators, make sure that the implicitly-declared new and delete |
| 297 | // operators can be found. |
| 298 | switch (NameInfo.getName().getCXXOverloadedOperator()) { |
| 299 | case OO_New: |
| 300 | case OO_Delete: |
| 301 | case OO_Array_New: |
| 302 | case OO_Array_Delete: |
Kaelyn Takata | fc8c61a | 2014-11-11 23:00:42 +0000 | [diff] [blame] | 303 | getSema().DeclareGlobalNewDelete(); |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 304 | break; |
Douglas Gregor | bcf0a47 | 2010-03-24 05:07:21 +0000 | [diff] [blame] | 305 | |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 306 | default: |
| 307 | break; |
| 308 | } |
Douglas Gregor | 1519766 | 2013-04-03 23:06:26 +0000 | [diff] [blame] | 309 | |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 310 | // Compiler builtins are always visible, regardless of where they end |
| 311 | // up being declared. |
| 312 | if (IdentifierInfo *Id = NameInfo.getName().getAsIdentifierInfo()) { |
| 313 | if (unsigned BuiltinID = Id->getBuiltinID()) { |
Kaelyn Takata | fc8c61a | 2014-11-11 23:00:42 +0000 | [diff] [blame] | 314 | if (!getSema().Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 315 | AllowHidden = true; |
Douglas Gregor | 1519766 | 2013-04-03 23:06:26 +0000 | [diff] [blame] | 316 | } |
Douglas Gregor | bcf0a47 | 2010-03-24 05:07:21 +0000 | [diff] [blame] | 317 | } |
John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Alp Toker | c108676 | 2013-12-07 13:51:35 +0000 | [diff] [blame] | 320 | bool LookupResult::sanity() const { |
Richard Smith | f97ad22 | 2014-04-01 18:33:50 +0000 | [diff] [blame] | 321 | // This function is never called by NDEBUG builds. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 322 | assert(ResultKind != NotFound || Decls.size() == 0); |
| 323 | assert(ResultKind != Found || Decls.size() == 1); |
| 324 | assert(ResultKind != FoundOverloaded || Decls.size() > 1 || |
| 325 | (Decls.size() == 1 && |
| 326 | isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))); |
| 327 | assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved()); |
| 328 | assert(ResultKind != Ambiguous || Decls.size() > 1 || |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 329 | (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || |
| 330 | Ambiguity == AmbiguousBaseSubobjectTypes))); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 331 | assert((Paths != nullptr) == (ResultKind == Ambiguous && |
| 332 | (Ambiguity == AmbiguousBaseSubobjectTypes || |
| 333 | Ambiguity == AmbiguousBaseSubobjects))); |
Alp Toker | c108676 | 2013-12-07 13:51:35 +0000 | [diff] [blame] | 334 | return true; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 335 | } |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 336 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 337 | // Necessary because CXXBasePaths is not complete in Sema.h |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 338 | void LookupResult::deletePaths(CXXBasePaths *Paths) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 339 | delete Paths; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Richard Smith | 3876cc8 | 2013-10-30 01:02:04 +0000 | [diff] [blame] | 342 | /// Get a representative context for a declaration such that two declarations |
| 343 | /// will have the same context if they were found within the same scope. |
Benjamin Kramer | fc58b04 | 2013-11-01 11:50:55 +0000 | [diff] [blame] | 344 | static DeclContext *getContextForScopeMatching(Decl *D) { |
Richard Smith | 3876cc8 | 2013-10-30 01:02:04 +0000 | [diff] [blame] | 345 | // For function-local declarations, use that function as the context. This |
| 346 | // doesn't account for scopes within the function; the caller must deal with |
| 347 | // those. |
| 348 | DeclContext *DC = D->getLexicalDeclContext(); |
| 349 | if (DC->isFunctionOrMethod()) |
| 350 | return DC; |
| 351 | |
| 352 | // Otherwise, look at the semantic context of the declaration. The |
| 353 | // declaration must have been found there. |
| 354 | return D->getDeclContext()->getRedeclContext(); |
| 355 | } |
| 356 | |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 357 | /// Resolves the result kind of this lookup. |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 358 | void LookupResult::resolveKind() { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 359 | unsigned N = Decls.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 360 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 361 | // Fast case: no possible ambiguity. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 362 | if (N == 0) { |
John McCall | 7fe6e9c | 2010-01-15 21:27:01 +0000 | [diff] [blame] | 363 | assert(ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 367 | // If there's a single decl, we need to examine it to decide what |
| 368 | // kind of lookup this is. |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 369 | if (N == 1) { |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 370 | NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); |
| 371 | if (isa<FunctionTemplateDecl>(D)) |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 372 | ResultKind = FoundOverloaded; |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 373 | else if (isa<UnresolvedUsingValueDecl>(D)) |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 374 | ResultKind = FoundUnresolvedValue; |
| 375 | return; |
| 376 | } |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 377 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 378 | // Don't do any extra resolution if we've already resolved as ambiguous. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 379 | if (ResultKind == Ambiguous) return; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 380 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 381 | llvm::SmallPtrSet<NamedDecl*, 16> Unique; |
Douglas Gregor | 13e6587 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 382 | llvm::SmallPtrSet<QualType, 16> UniqueTypes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 383 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 384 | bool Ambiguous = false; |
| 385 | bool HasTag = false, HasFunction = false, HasNonFunction = false; |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 386 | bool HasFunctionTemplate = false, HasUnresolved = false; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 387 | |
| 388 | unsigned UniqueTagIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 390 | unsigned I = 0; |
| 391 | while (I < N) { |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 392 | NamedDecl *D = Decls[I]->getUnderlyingDecl(); |
| 393 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 394 | |
Argyrios Kyrtzidis | 1fcd7fd | 2013-02-22 06:58:37 +0000 | [diff] [blame] | 395 | // Ignore an invalid declaration unless it's the only one left. |
| 396 | if (D->isInvalidDecl() && I < N-1) { |
| 397 | Decls[I] = Decls[--N]; |
| 398 | continue; |
| 399 | } |
| 400 | |
Douglas Gregor | 13e6587 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 401 | // Redeclarations of types via typedef can occur both within a scope |
| 402 | // and, through using declarations and directives, across scopes. There is |
| 403 | // no ambiguity if they all refer to the same type, so unique based on the |
| 404 | // canonical type. |
| 405 | if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { |
| 406 | if (!TD->getDeclContext()->isRecord()) { |
Kaelyn Takata | fc8c61a | 2014-11-11 23:00:42 +0000 | [diff] [blame] | 407 | QualType T = getSema().Context.getTypeDeclType(TD); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 408 | if (!UniqueTypes.insert(getSema().Context.getCanonicalType(T)).second) { |
Douglas Gregor | 13e6587 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 409 | // The type is not unique; pull something off the back and continue |
| 410 | // at this index. |
| 411 | Decls[I] = Decls[--N]; |
| 412 | continue; |
| 413 | } |
| 414 | } |
| 415 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 416 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 417 | if (!Unique.insert(D).second) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 418 | // If it's not unique, pull something off the back (and |
| 419 | // continue at this index). |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 420 | // FIXME: This is wrong. We need to take the more recent declaration in |
Richard Smith | fe620d2 | 2015-03-05 23:24:12 +0000 | [diff] [blame] | 421 | // order to get the right type, default arguments, etc. We also need to |
| 422 | // prefer visible declarations to hidden ones (for redeclaration lookup |
| 423 | // in modules builds). |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 424 | Decls[I] = Decls[--N]; |
Douglas Gregor | 13e6587 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 425 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Douglas Gregor | 13e6587 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 428 | // Otherwise, do some decl type analysis and then continue. |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 429 | |
Douglas Gregor | 13e6587 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 430 | if (isa<UnresolvedUsingValueDecl>(D)) { |
| 431 | HasUnresolved = true; |
| 432 | } else if (isa<TagDecl>(D)) { |
| 433 | if (HasTag) |
| 434 | Ambiguous = true; |
| 435 | UniqueTagIndex = I; |
| 436 | HasTag = true; |
| 437 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 438 | HasFunction = true; |
| 439 | HasFunctionTemplate = true; |
| 440 | } else if (isa<FunctionDecl>(D)) { |
| 441 | HasFunction = true; |
| 442 | } else { |
| 443 | if (HasNonFunction) |
| 444 | Ambiguous = true; |
| 445 | HasNonFunction = true; |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 446 | } |
Douglas Gregor | 13e6587 | 2010-08-11 14:45:53 +0000 | [diff] [blame] | 447 | I++; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | } |
Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 449 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 450 | // C++ [basic.scope.hiding]p2: |
| 451 | // A class name or enumeration name can be hidden by the name of |
| 452 | // an object, function, or enumerator declared in the same |
| 453 | // scope. If a class or enumeration name and an object, function, |
| 454 | // or enumerator are declared in the same scope (in any order) |
| 455 | // with the same name, the class or enumeration name is hidden |
| 456 | // wherever the object, function, or enumerator name is visible. |
| 457 | // But it's still an error if there are distinct tag types found, |
| 458 | // even if they're not visible. (ref?) |
John McCall | 8005382 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 459 | if (HideTags && HasTag && !Ambiguous && |
Douglas Gregor | e63d087 | 2010-10-23 16:06:17 +0000 | [diff] [blame] | 460 | (HasFunction || HasNonFunction || HasUnresolved)) { |
Richard Smith | 3876cc8 | 2013-10-30 01:02:04 +0000 | [diff] [blame] | 461 | if (getContextForScopeMatching(Decls[UniqueTagIndex])->Equals( |
| 462 | getContextForScopeMatching(Decls[UniqueTagIndex ? 0 : N - 1]))) |
Douglas Gregor | e63d087 | 2010-10-23 16:06:17 +0000 | [diff] [blame] | 463 | Decls[UniqueTagIndex] = Decls[--N]; |
| 464 | else |
| 465 | Ambiguous = true; |
| 466 | } |
Anders Carlsson | 8d0f6b7 | 2009-06-26 03:37:05 +0000 | [diff] [blame] | 467 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 468 | Decls.set_size(N); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 469 | |
John McCall | 8005382 | 2009-12-03 00:58:24 +0000 | [diff] [blame] | 470 | if (HasNonFunction && (HasFunction || HasUnresolved)) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 471 | Ambiguous = true; |
Douglas Gregor | f23311d | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 472 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 473 | if (Ambiguous) |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 474 | setAmbiguous(LookupResult::AmbiguousReference); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 475 | else if (HasUnresolved) |
| 476 | ResultKind = LookupResult::FoundUnresolvedValue; |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 477 | else if (N > 1 || HasFunctionTemplate) |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 478 | ResultKind = LookupResult::FoundOverloaded; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 479 | else |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 480 | ResultKind = LookupResult::Found; |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 481 | } |
| 482 | |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 483 | void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 484 | CXXBasePaths::const_paths_iterator I, E; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 485 | for (I = P.begin(), E = P.end(); I != E; ++I) |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 486 | for (DeclContext::lookup_iterator DI = I->Decls.begin(), |
| 487 | DE = I->Decls.end(); DI != DE; ++DI) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 488 | addDecl(*DI); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 489 | } |
| 490 | |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 491 | void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 492 | Paths = new CXXBasePaths; |
| 493 | Paths->swap(P); |
| 494 | addDeclsFromBasePaths(*Paths); |
| 495 | resolveKind(); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 496 | setAmbiguous(AmbiguousBaseSubobjects); |
Douglas Gregor | 0e8fc3c | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 497 | } |
| 498 | |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 499 | void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 500 | Paths = new CXXBasePaths; |
| 501 | Paths->swap(P); |
| 502 | addDeclsFromBasePaths(*Paths); |
| 503 | resolveKind(); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 504 | setAmbiguous(AmbiguousBaseSubobjectTypes); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 507 | void LookupResult::print(raw_ostream &Out) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 508 | Out << Decls.size() << " result(s)"; |
| 509 | if (isAmbiguous()) Out << ", ambiguous"; |
| 510 | if (Paths) Out << ", base paths present"; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 511 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 512 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 513 | Out << "\n"; |
| 514 | (*I)->print(Out, 2); |
| 515 | } |
| 516 | } |
| 517 | |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 518 | /// \brief Lookup a builtin function, when name lookup would otherwise |
| 519 | /// fail. |
| 520 | static bool LookupBuiltin(Sema &S, LookupResult &R) { |
| 521 | Sema::LookupNameKind NameKind = R.getLookupKind(); |
| 522 | |
| 523 | // If we didn't find a use of this identifier, and if the identifier |
| 524 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 525 | // now, injecting it into translation unit scope, and return it. |
| 526 | if (NameKind == Sema::LookupOrdinaryName || |
| 527 | NameKind == Sema::LookupRedeclarationWithLinkage) { |
| 528 | IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); |
| 529 | if (II) { |
Nico Weber | e1687c5 | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 530 | if (S.getLangOpts().CPlusPlus11 && S.getLangOpts().GNUMode && |
| 531 | II == S.getFloat128Identifier()) { |
| 532 | // libstdc++4.7's type_traits expects type __float128 to exist, so |
| 533 | // insert a dummy type to make that header build in gnu++11 mode. |
| 534 | R.addDecl(S.getASTContext().getFloat128StubType()); |
| 535 | return true; |
| 536 | } |
| 537 | |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 538 | // If this is a builtin on this (or all) targets, create the decl. |
| 539 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 540 | // In C++, we don't have any predefined library functions like |
| 541 | // 'malloc'. Instead, we'll just error. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 542 | if (S.getLangOpts().CPlusPlus && |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 543 | S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 544 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 545 | |
| 546 | if (NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, |
| 547 | BuiltinID, S.TUScope, |
Douglas Gregor | bfe022c | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 548 | R.isForRedeclaration(), |
| 549 | R.getNameLoc())) { |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 550 | R.addDecl(D); |
Douglas Gregor | bfe022c | 2011-01-03 09:37:44 +0000 | [diff] [blame] | 551 | return true; |
| 552 | } |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return false; |
| 558 | } |
| 559 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 560 | /// \brief Determine whether we can declare a special member function within |
| 561 | /// the class at this point. |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 562 | static bool CanDeclareSpecialMemberFunction(const CXXRecordDecl *Class) { |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 563 | // We need to have a definition for the class. |
| 564 | if (!Class->getDefinition() || Class->isDependentContext()) |
| 565 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 567 | // We can't be in the middle of defining the class. |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 568 | return !Class->isBeingDefined(); |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | void Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) { |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 572 | if (!CanDeclareSpecialMemberFunction(Class)) |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 573 | return; |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 574 | |
| 575 | // If the default constructor has not yet been declared, do so now. |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 576 | if (Class->needsImplicitDefaultConstructor()) |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 577 | DeclareImplicitDefaultConstructor(Class); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 578 | |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 579 | // If the copy constructor has not yet been declared, do so now. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 580 | if (Class->needsImplicitCopyConstructor()) |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 581 | DeclareImplicitCopyConstructor(Class); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 582 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 583 | // If the copy assignment operator has not yet been declared, do so now. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 584 | if (Class->needsImplicitCopyAssignment()) |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 585 | DeclareImplicitCopyAssignment(Class); |
| 586 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 587 | if (getLangOpts().CPlusPlus11) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 588 | // If the move constructor has not yet been declared, do so now. |
| 589 | if (Class->needsImplicitMoveConstructor()) |
| 590 | DeclareImplicitMoveConstructor(Class); // might not actually do it |
| 591 | |
| 592 | // If the move assignment operator has not yet been declared, do so now. |
| 593 | if (Class->needsImplicitMoveAssignment()) |
| 594 | DeclareImplicitMoveAssignment(Class); // might not actually do it |
| 595 | } |
| 596 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 597 | // If the destructor has not yet been declared, do so now. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 598 | if (Class->needsImplicitDestructor()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | DeclareImplicitDestructor(Class); |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 600 | } |
| 601 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 602 | /// \brief Determine whether this is the name of an implicitly-declared |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 603 | /// special member function. |
| 604 | static bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) { |
| 605 | switch (Name.getNameKind()) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 606 | case DeclarationName::CXXConstructorName: |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 607 | case DeclarationName::CXXDestructorName: |
| 608 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 610 | case DeclarationName::CXXOperatorName: |
| 611 | return Name.getCXXOverloadedOperator() == OO_Equal; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 612 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 613 | default: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 614 | break; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 615 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 616 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 617 | return false; |
| 618 | } |
| 619 | |
| 620 | /// \brief If there are any implicit member functions with the given name |
| 621 | /// that need to be declared in the given declaration context, do so. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 622 | static void DeclareImplicitMemberFunctionsWithName(Sema &S, |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 623 | DeclarationName Name, |
| 624 | const DeclContext *DC) { |
| 625 | if (!DC) |
| 626 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 628 | switch (Name.getNameKind()) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 629 | case DeclarationName::CXXConstructorName: |
| 630 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 631 | if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 632 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 633 | if (Record->needsImplicitDefaultConstructor()) |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 634 | S.DeclareImplicitDefaultConstructor(Class); |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 635 | if (Record->needsImplicitCopyConstructor()) |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 636 | S.DeclareImplicitCopyConstructor(Class); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 637 | if (S.getLangOpts().CPlusPlus11 && |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 638 | Record->needsImplicitMoveConstructor()) |
| 639 | S.DeclareImplicitMoveConstructor(Class); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 640 | } |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 641 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 643 | case DeclarationName::CXXDestructorName: |
| 644 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 645 | if (Record->getDefinition() && Record->needsImplicitDestructor() && |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 646 | CanDeclareSpecialMemberFunction(Record)) |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 647 | S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record)); |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 648 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 650 | case DeclarationName::CXXOperatorName: |
| 651 | if (Name.getCXXOverloadedOperator() != OO_Equal) |
| 652 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 653 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 654 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) { |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 655 | if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 656 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 657 | if (Record->needsImplicitCopyAssignment()) |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 658 | S.DeclareImplicitCopyAssignment(Class); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 659 | if (S.getLangOpts().CPlusPlus11 && |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 660 | Record->needsImplicitMoveAssignment()) |
| 661 | S.DeclareImplicitMoveAssignment(Class); |
| 662 | } |
| 663 | } |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 664 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 666 | default: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 667 | break; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 670 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 671 | // Adds all qualifying matches for a name within a decl context to the |
| 672 | // given lookup result. Returns true if any matches were found. |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 673 | static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 674 | bool Found = false; |
| 675 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 676 | // Lazily declare C++ special member functions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 677 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 678 | DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), DC); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 680 | // Perform lookup into this declaration context. |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 681 | DeclContext::lookup_result DR = DC->lookup(R.getLookupName()); |
| 682 | for (DeclContext::lookup_iterator I = DR.begin(), E = DR.end(); I != E; |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 683 | ++I) { |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 684 | NamedDecl *D = *I; |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 685 | if ((D = R.getAcceptableDecl(D))) { |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 686 | R.addDecl(D); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 687 | Found = true; |
| 688 | } |
| 689 | } |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 691 | if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R)) |
| 692 | return true; |
| 693 | |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 694 | if (R.getLookupName().getNameKind() |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 695 | != DeclarationName::CXXConversionFunctionName || |
| 696 | R.getLookupName().getCXXNameType()->isDependentType() || |
| 697 | !isa<CXXRecordDecl>(DC)) |
| 698 | return Found; |
| 699 | |
| 700 | // C++ [temp.mem]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 701 | // A specialization of a conversion function template is not found by |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 702 | // name lookup. Instead, any conversion function templates visible in the |
| 703 | // context of the use are considered. [...] |
| 704 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 705 | if (!Record->isCompleteDefinition()) |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 706 | return Found; |
| 707 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 708 | for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(), |
| 709 | UEnd = Record->conversion_end(); U != UEnd; ++U) { |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 710 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); |
| 711 | if (!ConvTemplate) |
| 712 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 713 | |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 714 | // When we're performing lookup for the purposes of redeclaration, just |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 715 | // add the conversion function template. When we deduce template |
| 716 | // arguments for specializations, we'll end up unifying the return |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 717 | // type of the new declaration with the type of the function template. |
| 718 | if (R.isForRedeclaration()) { |
| 719 | R.addDecl(ConvTemplate); |
| 720 | Found = true; |
| 721 | continue; |
| 722 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 724 | // C++ [temp.mem]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 725 | // [...] For each such operator, if argument deduction succeeds |
| 726 | // (14.9.2.3), the resulting specialization is used as if found by |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 727 | // name lookup. |
| 728 | // |
| 729 | // When referencing a conversion function for any purpose other than |
| 730 | // a redeclaration (such that we'll be building an expression with the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 731 | // result), perform template argument deduction and place the |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 732 | // specialization into the result set. We do this to avoid forcing all |
| 733 | // callers to perform special deduction for conversion functions. |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 734 | TemplateDeductionInfo Info(R.getNameLoc()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 735 | FunctionDecl *Specialization = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 736 | |
| 737 | const FunctionProtoType *ConvProto |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 738 | = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); |
| 739 | assert(ConvProto && "Nonsensical conversion function template type"); |
Douglas Gregor | 3c96a46 | 2010-01-12 01:17:50 +0000 | [diff] [blame] | 740 | |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 741 | // Compute the type of the function that we would expect the conversion |
| 742 | // function to have, if it were to match the name given. |
| 743 | // FIXME: Calling convention! |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 744 | FunctionProtoType::ExtProtoInfo EPI = ConvProto->getExtProtoInfo(); |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 745 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC_C); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 746 | EPI.ExceptionSpec = EST_None; |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 747 | QualType ExpectedType |
| 748 | = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(), |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 749 | None, EPI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 750 | |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 751 | // Perform template argument deduction against the type that we would |
| 752 | // expect the function to have. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 753 | if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType, |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 754 | Specialization, Info) |
| 755 | == Sema::TDK_Success) { |
| 756 | R.addDecl(Specialization); |
| 757 | Found = true; |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
Chandler Carruth | 3a693b7 | 2010-01-31 11:44:02 +0000 | [diff] [blame] | 760 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 761 | return Found; |
| 762 | } |
| 763 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 764 | // Performs C++ unqualified lookup into the given file context. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 765 | static bool |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 766 | CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 767 | DeclContext *NS, UnqualUsingDirectiveSet &UDirs) { |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 768 | |
| 769 | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); |
| 770 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 771 | // Perform direct name lookup into the LookupCtx. |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 772 | bool Found = LookupDirect(S, R, NS); |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 773 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 774 | // Perform direct name lookup into the namespaces nominated by the |
| 775 | // using directives whose common ancestor is this namespace. |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 776 | for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(NS)) |
| 777 | if (LookupDirect(S, R, UUE.getNominatedNamespace())) |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 778 | Found = true; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 779 | |
| 780 | R.resolveKind(); |
| 781 | |
| 782 | return Found; |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | static bool isNamespaceOrTranslationUnitScope(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 786 | if (DeclContext *Ctx = S->getEntity()) |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 787 | return Ctx->isFileContext(); |
| 788 | return false; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 789 | } |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 791 | // Find the next outer declaration context from this scope. This |
| 792 | // routine actually returns the semantic outer context, which may |
| 793 | // differ from the lexical context (encoded directly in the Scope |
| 794 | // stack) when we are parsing a member of a class template. In this |
| 795 | // case, the second element of the pair will be true, to indicate that |
| 796 | // name lookup should continue searching in this semantic context when |
| 797 | // it leaves the current template parameter scope. |
| 798 | static std::pair<DeclContext *, bool> findOuterContext(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 799 | DeclContext *DC = S->getEntity(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 800 | DeclContext *Lexical = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | for (Scope *OuterS = S->getParent(); OuterS; |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 802 | OuterS = OuterS->getParent()) { |
| 803 | if (OuterS->getEntity()) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 804 | Lexical = OuterS->getEntity(); |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 805 | break; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | // C++ [temp.local]p8: |
| 810 | // In the definition of a member of a class template that appears |
| 811 | // outside of the namespace containing the class template |
| 812 | // definition, the name of a template-parameter hides the name of |
| 813 | // a member of this namespace. |
| 814 | // |
| 815 | // Example: |
| 816 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | // namespace N { |
| 818 | // class C { }; |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 819 | // |
| 820 | // template<class T> class B { |
| 821 | // void f(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 822 | // }; |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 823 | // } |
| 824 | // |
| 825 | // template<class C> void N::B<C>::f(C) { |
| 826 | // C b; // C is the template parameter, not N::C |
| 827 | // } |
| 828 | // |
| 829 | // In this example, the lexical context we return is the |
| 830 | // TranslationUnit, while the semantic context is the namespace N. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 831 | if (!Lexical || !DC || !S->getParent() || |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 832 | !S->getParent()->isTemplateParamScope()) |
| 833 | return std::make_pair(Lexical, false); |
| 834 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 835 | // Find the outermost template parameter scope. |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 836 | // For the example, this is the scope for the template parameters of |
| 837 | // template<class C>. |
| 838 | Scope *OutermostTemplateScope = S->getParent(); |
| 839 | while (OutermostTemplateScope->getParent() && |
| 840 | OutermostTemplateScope->getParent()->isTemplateParamScope()) |
| 841 | OutermostTemplateScope = OutermostTemplateScope->getParent(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 842 | |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 843 | // Find the namespace context in which the original scope occurs. In |
| 844 | // the example, this is namespace N. |
| 845 | DeclContext *Semantic = DC; |
| 846 | while (!Semantic->isFileContext()) |
| 847 | Semantic = Semantic->getParent(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 849 | // Find the declaration context just outside of the template |
| 850 | // parameter scope. This is the context in which the template is |
| 851 | // being lexically declaration (a namespace context). In the |
| 852 | // example, this is the global scope. |
| 853 | if (Lexical->isFileContext() && !Lexical->Equals(Semantic) && |
| 854 | Lexical->Encloses(Semantic)) |
| 855 | return std::make_pair(Semantic, true); |
| 856 | |
| 857 | return std::make_pair(Lexical, false); |
Douglas Gregor | 7f737c0 | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 860 | namespace { |
| 861 | /// An RAII object to specify that we want to find block scope extern |
| 862 | /// declarations. |
| 863 | struct FindLocalExternScope { |
| 864 | FindLocalExternScope(LookupResult &R) |
| 865 | : R(R), OldFindLocalExtern(R.getIdentifierNamespace() & |
| 866 | Decl::IDNS_LocalExtern) { |
| 867 | R.setFindLocalExtern(R.getIdentifierNamespace() & Decl::IDNS_Ordinary); |
| 868 | } |
| 869 | void restore() { |
| 870 | R.setFindLocalExtern(OldFindLocalExtern); |
| 871 | } |
| 872 | ~FindLocalExternScope() { |
| 873 | restore(); |
| 874 | } |
| 875 | LookupResult &R; |
| 876 | bool OldFindLocalExtern; |
| 877 | }; |
| 878 | } |
| 879 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 880 | bool Sema::CppLookupName(LookupResult &R, Scope *S) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 881 | assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup"); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 882 | |
| 883 | DeclarationName Name = R.getLookupName(); |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 884 | Sema::LookupNameKind NameKind = R.getLookupKind(); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 886 | // If this is the name of an implicitly-declared special member function, |
| 887 | // go through the scope stack to implicitly declare |
| 888 | if (isImplicitlyDeclaredMemberFunctionName(Name)) { |
| 889 | for (Scope *PreS = S; PreS; PreS = PreS->getParent()) |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 890 | if (DeclContext *DC = PreS->getEntity()) |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 891 | DeclareImplicitMemberFunctionsWithName(*this, Name, DC); |
| 892 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 894 | // Implicitly declare member functions with the name we're looking for, if in |
| 895 | // fact we are in a scope where it matters. |
| 896 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 897 | Scope *Initial = S; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | IdentifierResolver::iterator |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 899 | I = IdResolver.begin(Name), |
| 900 | IEnd = IdResolver.end(); |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 902 | // First we lookup local scope. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 903 | // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 904 | // ...During unqualified name lookup (3.4.1), the names appear as if |
| 905 | // they were declared in the nearest enclosing namespace which contains |
| 906 | // both the using-directive and the nominated namespace. |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 907 | // [Note: in this context, "contains" means "contains directly or |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | // indirectly". |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 909 | // |
| 910 | // For example: |
| 911 | // namespace A { int i; } |
| 912 | // void foo() { |
| 913 | // int i; |
| 914 | // { |
| 915 | // using namespace A; |
| 916 | // ++i; // finds local 'i', A::i appears at global scope |
| 917 | // } |
| 918 | // } |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 919 | // |
Douglas Gregor | cc9406c | 2013-04-08 23:11:25 +0000 | [diff] [blame] | 920 | UnqualUsingDirectiveSet UDirs; |
| 921 | bool VisitedUsingDirectives = false; |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 922 | bool LeftStartingScope = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 923 | DeclContext *OutsideOfTemplateParamDC = nullptr; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 924 | |
| 925 | // When performing a scope lookup, we want to find local extern decls. |
| 926 | FindLocalExternScope FindLocals(R); |
| 927 | |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 928 | for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 929 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 931 | // Check whether the IdResolver has anything in this scope. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 932 | bool Found = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 933 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 934 | if (NamedDecl *ND = R.getAcceptableDecl(*I)) { |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 935 | if (NameKind == LookupRedeclarationWithLinkage) { |
| 936 | // Determine whether this (or a previous) declaration is |
| 937 | // out-of-scope. |
| 938 | if (!LeftStartingScope && !Initial->isDeclScope(*I)) |
| 939 | LeftStartingScope = true; |
| 940 | |
| 941 | // If we found something outside of our starting scope that |
Richard Smith | 9a00bbf | 2013-10-16 21:12:00 +0000 | [diff] [blame] | 942 | // does not have linkage, skip it. If it's a template parameter, |
| 943 | // we still find it, so we can diagnose the invalid redeclaration. |
| 944 | if (LeftStartingScope && !((*I)->hasLinkage()) && |
| 945 | !(*I)->isTemplateParameter()) { |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 946 | R.setShadowed(); |
| 947 | continue; |
| 948 | } |
| 949 | } |
| 950 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 951 | Found = true; |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 952 | R.addDecl(ND); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 955 | if (Found) { |
| 956 | R.resolveKind(); |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 957 | if (S->isClassScope()) |
| 958 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
| 959 | R.setNamingClass(Record); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 960 | return true; |
| 961 | } |
| 962 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 963 | if (NameKind == LookupLocalFriendName && !S->isClassScope()) { |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 964 | // C++11 [class.friend]p11: |
| 965 | // If a friend declaration appears in a local class and the name |
| 966 | // specified is an unqualified name, a prior declaration is |
| 967 | // looked up without considering scopes that are outside the |
| 968 | // innermost enclosing non-class scope. |
| 969 | return false; |
| 970 | } |
| 971 | |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 972 | if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && |
| 973 | S->getParent() && !S->getParent()->isTemplateParamScope()) { |
| 974 | // We've just searched the last template parameter scope and |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 975 | // found nothing, so look into the contexts between the |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 976 | // lexical and semantic declaration contexts returned by |
| 977 | // findOuterContext(). This implements the name lookup behavior |
| 978 | // of C++ [temp.local]p8. |
| 979 | Ctx = OutsideOfTemplateParamDC; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 980 | OutsideOfTemplateParamDC = nullptr; |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | if (Ctx) { |
| 984 | DeclContext *OuterCtx; |
| 985 | bool SearchAfterTemplateScope; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 986 | std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 987 | if (SearchAfterTemplateScope) |
| 988 | OutsideOfTemplateParamDC = OuterCtx; |
| 989 | |
Douglas Gregor | ea16606 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 990 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { |
Douglas Gregor | 337caf9 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 991 | // We do not directly look into transparent contexts, since |
| 992 | // those entities will be found in the nearest enclosing |
| 993 | // non-transparent context. |
| 994 | if (Ctx->isTransparentContext()) |
Douglas Gregor | 7f737c0 | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 995 | continue; |
Douglas Gregor | 337caf9 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 996 | |
| 997 | // We do not look directly into function or method contexts, |
| 998 | // since all of the local variables and parameters of the |
| 999 | // function/method are present within the Scope. |
| 1000 | if (Ctx->isFunctionOrMethod()) { |
| 1001 | // If we have an Objective-C instance method, look for ivars |
| 1002 | // in the corresponding interface. |
| 1003 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
| 1004 | if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()) |
| 1005 | if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { |
| 1006 | ObjCInterfaceDecl *ClassDeclared; |
| 1007 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1008 | Name.getAsIdentifierInfo(), |
Douglas Gregor | 337caf9 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 1009 | ClassDeclared)) { |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1010 | if (NamedDecl *ND = R.getAcceptableDecl(Ivar)) { |
| 1011 | R.addDecl(ND); |
Douglas Gregor | 337caf9 | 2010-02-19 16:08:35 +0000 | [diff] [blame] | 1012 | R.resolveKind(); |
| 1013 | return true; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
Douglas Gregor | b0d0aa5 | 2013-03-27 12:51:49 +0000 | [diff] [blame] | 1022 | // If this is a file context, we need to perform unqualified name |
| 1023 | // lookup considering using directives. |
| 1024 | if (Ctx->isFileContext()) { |
Douglas Gregor | cc9406c | 2013-04-08 23:11:25 +0000 | [diff] [blame] | 1025 | // If we haven't handled using directives yet, do so now. |
| 1026 | if (!VisitedUsingDirectives) { |
| 1027 | // Add using directives from this context up to the top level. |
Douglas Gregor | 8ccbc18 | 2013-04-09 01:49:26 +0000 | [diff] [blame] | 1028 | for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent()) { |
| 1029 | if (UCtx->isTransparentContext()) |
| 1030 | continue; |
| 1031 | |
Douglas Gregor | cc9406c | 2013-04-08 23:11:25 +0000 | [diff] [blame] | 1032 | UDirs.visit(UCtx, UCtx); |
Douglas Gregor | 8ccbc18 | 2013-04-09 01:49:26 +0000 | [diff] [blame] | 1033 | } |
Douglas Gregor | cc9406c | 2013-04-08 23:11:25 +0000 | [diff] [blame] | 1034 | |
| 1035 | // Find the innermost file scope, so we can add using directives |
| 1036 | // from local scopes. |
| 1037 | Scope *InnermostFileScope = S; |
| 1038 | while (InnermostFileScope && |
| 1039 | !isNamespaceOrTranslationUnitScope(InnermostFileScope)) |
| 1040 | InnermostFileScope = InnermostFileScope->getParent(); |
| 1041 | UDirs.visitScopeChain(Initial, InnermostFileScope); |
| 1042 | |
| 1043 | UDirs.done(); |
| 1044 | |
| 1045 | VisitedUsingDirectives = true; |
| 1046 | } |
Douglas Gregor | b0d0aa5 | 2013-03-27 12:51:49 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) { |
| 1049 | R.resolveKind(); |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
Douglas Gregor | 7f737c0 | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 1056 | // Perform qualified name lookup into this context. |
| 1057 | // FIXME: In some cases, we know that every name that could be found by |
| 1058 | // this qualified name lookup will also be on the identifier chain. For |
| 1059 | // example, inside a class without any base classes, we never need to |
| 1060 | // perform qualified lookup because all of the members are on top of the |
| 1061 | // identifier chain. |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1062 | if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1063 | return true; |
Douglas Gregor | fdca4a7 | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 1064 | } |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 1065 | } |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1066 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1067 | |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 1068 | // Stop if we ran out of scopes. |
| 1069 | // FIXME: This really, really shouldn't be happening. |
| 1070 | if (!S) return false; |
| 1071 | |
Argyrios Kyrtzidis | 706bbf8 | 2010-10-29 16:12:50 +0000 | [diff] [blame] | 1072 | // If we are looking for members, no need to look into global/namespace scope. |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1073 | if (NameKind == LookupMemberName) |
Argyrios Kyrtzidis | 706bbf8 | 2010-10-29 16:12:50 +0000 | [diff] [blame] | 1074 | return false; |
| 1075 | |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 1076 | // Collect UsingDirectiveDecls in all scopes, and recursively all |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1077 | // nominated namespaces by those using-directives. |
John McCall | f6c8a4e | 2009-11-10 07:01:13 +0000 | [diff] [blame] | 1078 | // |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1079 | // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we |
| 1080 | // don't build it for each lookup! |
Douglas Gregor | cc9406c | 2013-04-08 23:11:25 +0000 | [diff] [blame] | 1081 | if (!VisitedUsingDirectives) { |
| 1082 | UDirs.visitScopeChain(Initial, S); |
| 1083 | UDirs.done(); |
| 1084 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1085 | |
| 1086 | // If we're not performing redeclaration lookup, do not look for local |
| 1087 | // extern declarations outside of a function scope. |
| 1088 | if (!R.isForRedeclaration()) |
| 1089 | FindLocals.restore(); |
| 1090 | |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 1091 | // Lookup namespace scope, and global scope. |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1092 | // Unqualified name lookup in C++ requires looking into scopes |
| 1093 | // that aren't strictly lexical, and therefore we walk through the |
| 1094 | // context as well as walking through the scopes. |
| 1095 | for (; S; S = S->getParent()) { |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1096 | // Check whether the IdResolver has anything in this scope. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1097 | bool Found = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1098 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1099 | if (NamedDecl *ND = R.getAcceptableDecl(*I)) { |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1100 | // We found something. Look for anything else in our scope |
| 1101 | // with this same name and in an acceptable identifier |
| 1102 | // namespace, so that we can construct an overload set if we |
| 1103 | // need to. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1104 | Found = true; |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1105 | R.addDecl(ND); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1109 | if (Found && S->isTemplateParamScope()) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1110 | R.resolveKind(); |
| 1111 | return true; |
| 1112 | } |
| 1113 | |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1114 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1115 | if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && |
| 1116 | S->getParent() && !S->getParent()->isTemplateParamScope()) { |
| 1117 | // We've just searched the last template parameter scope and |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 1118 | // found nothing, so look into the contexts between the |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1119 | // lexical and semantic declaration contexts returned by |
| 1120 | // findOuterContext(). This implements the name lookup behavior |
| 1121 | // of C++ [temp.local]p8. |
| 1122 | Ctx = OutsideOfTemplateParamDC; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1123 | OutsideOfTemplateParamDC = nullptr; |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1124 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1126 | if (Ctx) { |
| 1127 | DeclContext *OuterCtx; |
| 1128 | bool SearchAfterTemplateScope; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1129 | std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1130 | if (SearchAfterTemplateScope) |
| 1131 | OutsideOfTemplateParamDC = OuterCtx; |
| 1132 | |
| 1133 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { |
| 1134 | // We do not directly look into transparent contexts, since |
| 1135 | // those entities will be found in the nearest enclosing |
| 1136 | // non-transparent context. |
| 1137 | if (Ctx->isTransparentContext()) |
| 1138 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1140 | // If we have a context, and it's not a context stashed in the |
| 1141 | // template parameter scope for an out-of-line definition, also |
| 1142 | // look into that context. |
| 1143 | if (!(Found && S && S->isTemplateParamScope())) { |
| 1144 | assert(Ctx->isFileContext() && |
| 1145 | "We should have been looking only at file context here already."); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1147 | // Look into context considering using-directives. |
| 1148 | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) |
| 1149 | Found = true; |
| 1150 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1152 | if (Found) { |
| 1153 | R.resolveKind(); |
| 1154 | return true; |
| 1155 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1156 | |
Douglas Gregor | f3d3ae6 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 1157 | if (R.isForRedeclaration() && !Ctx->isTransparentContext()) |
| 1158 | return false; |
| 1159 | } |
| 1160 | } |
| 1161 | |
Douglas Gregor | 3ce7493 | 2010-02-05 07:07:10 +0000 | [diff] [blame] | 1162 | if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext()) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1163 | return false; |
Douglas Gregor | 700792c | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 1164 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1165 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1166 | return !R.empty(); |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1169 | /// \brief Find the declaration that a class temploid member specialization was |
| 1170 | /// instantiated from, or the member itself if it is an explicit specialization. |
| 1171 | static Decl *getInstantiatedFrom(Decl *D, MemberSpecializationInfo *MSInfo) { |
| 1172 | return MSInfo->isExplicitSpecialization() ? D : MSInfo->getInstantiatedFrom(); |
| 1173 | } |
| 1174 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1175 | Module *Sema::getOwningModule(Decl *Entity) { |
| 1176 | // If it's imported, grab its owning module. |
| 1177 | Module *M = Entity->getImportedOwningModule(); |
| 1178 | if (M || !isa<NamedDecl>(Entity) || !cast<NamedDecl>(Entity)->isHidden()) |
| 1179 | return M; |
| 1180 | assert(!Entity->isFromASTFile() && |
| 1181 | "hidden entity from AST file has no owning module"); |
| 1182 | |
| 1183 | // It's local and hidden; grab or compute its owning module. |
| 1184 | M = Entity->getLocalOwningModule(); |
| 1185 | if (M) |
| 1186 | return M; |
| 1187 | |
| 1188 | if (auto *Containing = |
| 1189 | PP.getModuleContainingLocation(Entity->getLocation())) { |
| 1190 | M = Containing; |
| 1191 | } else if (Entity->isInvalidDecl() || Entity->getLocation().isInvalid()) { |
| 1192 | // Don't bother tracking visibility for invalid declarations with broken |
| 1193 | // locations. |
| 1194 | cast<NamedDecl>(Entity)->setHidden(false); |
| 1195 | } else { |
| 1196 | // We need to assign a module to an entity that exists outside of any |
| 1197 | // module, so that we can hide it from modules that we textually enter. |
| 1198 | // Invent a fake module for all such entities. |
| 1199 | if (!CachedFakeTopLevelModule) { |
| 1200 | CachedFakeTopLevelModule = |
| 1201 | PP.getHeaderSearchInfo().getModuleMap().findOrCreateModule( |
| 1202 | "<top-level>", nullptr, false, false).first; |
| 1203 | |
| 1204 | auto &SrcMgr = PP.getSourceManager(); |
| 1205 | SourceLocation StartLoc = |
| 1206 | SrcMgr.getLocForStartOfFile(SrcMgr.getMainFileID()); |
| 1207 | auto &TopLevel = |
| 1208 | VisibleModulesStack.empty() ? VisibleModules : VisibleModulesStack[0]; |
| 1209 | TopLevel.setVisible(CachedFakeTopLevelModule, StartLoc); |
| 1210 | } |
| 1211 | |
| 1212 | M = CachedFakeTopLevelModule; |
| 1213 | } |
| 1214 | |
| 1215 | if (M) |
| 1216 | Entity->setLocalOwningModule(M); |
| 1217 | return M; |
| 1218 | } |
| 1219 | |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1220 | void Sema::makeMergedDefinitionVisible(NamedDecl *ND, SourceLocation Loc) { |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1221 | auto *M = PP.getModuleContainingLocation(Loc); |
| 1222 | assert(M && "hidden definition not in any module"); |
| 1223 | Context.mergeDefinitionIntoModule(ND, M); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1226 | /// \brief Find the module in which the given declaration was defined. |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1227 | static Module *getDefiningModule(Sema &S, Decl *Entity) { |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1228 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Entity)) { |
| 1229 | // If this function was instantiated from a template, the defining module is |
| 1230 | // the module containing the pattern. |
| 1231 | if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern()) |
| 1232 | Entity = Pattern; |
| 1233 | } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Entity)) { |
Reid Kleckner | e7367d6 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 1234 | if (CXXRecordDecl *Pattern = RD->getTemplateInstantiationPattern()) |
| 1235 | Entity = Pattern; |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1236 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(Entity)) { |
| 1237 | if (MemberSpecializationInfo *MSInfo = ED->getMemberSpecializationInfo()) |
| 1238 | Entity = getInstantiatedFrom(ED, MSInfo); |
| 1239 | } else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) { |
| 1240 | // FIXME: Map from variable template specializations back to the template. |
| 1241 | if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) |
| 1242 | Entity = getInstantiatedFrom(VD, MSInfo); |
| 1243 | } |
| 1244 | |
| 1245 | // Walk up to the containing context. That might also have been instantiated |
| 1246 | // from a template. |
| 1247 | DeclContext *Context = Entity->getDeclContext(); |
| 1248 | if (Context->isFileContext()) |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1249 | return S.getOwningModule(Entity); |
| 1250 | return getDefiningModule(S, cast<Decl>(Context)); |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | llvm::DenseSet<Module*> &Sema::getLookupModules() { |
| 1254 | unsigned N = ActiveTemplateInstantiations.size(); |
| 1255 | for (unsigned I = ActiveTemplateInstantiationLookupModules.size(); |
| 1256 | I != N; ++I) { |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1257 | Module *M = |
| 1258 | getDefiningModule(*this, ActiveTemplateInstantiations[I].Entity); |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1259 | if (M && !LookupModulesCache.insert(M).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1260 | M = nullptr; |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1261 | ActiveTemplateInstantiationLookupModules.push_back(M); |
| 1262 | } |
| 1263 | return LookupModulesCache; |
| 1264 | } |
| 1265 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1266 | bool Sema::hasVisibleMergedDefinition(NamedDecl *Def) { |
| 1267 | for (Module *Merged : Context.getModulesWithMergedDefinition(Def)) |
| 1268 | if (isModuleVisible(Merged)) |
| 1269 | return true; |
| 1270 | return false; |
| 1271 | } |
| 1272 | |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1273 | /// \brief Determine whether a declaration is visible to name lookup. |
| 1274 | /// |
| 1275 | /// This routine determines whether the declaration D is visible in the current |
| 1276 | /// lookup context, taking into account the current template instantiation |
| 1277 | /// stack. During template instantiation, a declaration is visible if it is |
| 1278 | /// visible from a module containing any entity on the template instantiation |
| 1279 | /// path (by instantiating a template, you allow it to see the declarations that |
| 1280 | /// your module can see, including those later on in your module). |
| 1281 | bool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1282 | assert(D->isHidden() && "should not call this: not in slow case"); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1283 | Module *DeclModule = SemaRef.getOwningModule(D); |
| 1284 | if (!DeclModule) { |
| 1285 | // getOwningModule() may have decided the declaration should not be hidden. |
| 1286 | assert(!D->isHidden() && "hidden decl not from a module"); |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
| 1290 | // If the owning module is visible, and the decl is not module private, |
| 1291 | // then the decl is visible too. (Module private is ignored within the same |
| 1292 | // top-level module.) |
| 1293 | if (!D->isFromASTFile() || !D->isModulePrivate()) { |
| 1294 | if (SemaRef.isModuleVisible(DeclModule)) |
| 1295 | return true; |
| 1296 | // Also check merged definitions. |
| 1297 | if (SemaRef.getLangOpts().ModulesLocalVisibility && |
| 1298 | SemaRef.hasVisibleMergedDefinition(D)) |
| 1299 | return true; |
| 1300 | } |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1301 | |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1302 | // If this declaration is not at namespace scope nor module-private, |
| 1303 | // then it is visible if its lexical parent has a visible definition. |
| 1304 | DeclContext *DC = D->getLexicalDeclContext(); |
| 1305 | if (!D->isModulePrivate() && |
| 1306 | DC && !DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) { |
Richard Smith | c7d48d1 | 2015-05-20 17:50:35 +0000 | [diff] [blame] | 1307 | // For a parameter, check whether our current template declaration's |
| 1308 | // lexical context is visible, not whether there's some other visible |
| 1309 | // definition of it, because parameters aren't "within" the definition. |
| 1310 | if ((D->isTemplateParameter() || isa<ParmVarDecl>(D)) |
| 1311 | ? isVisible(SemaRef, cast<NamedDecl>(DC)) |
| 1312 | : SemaRef.hasVisibleDefinition(cast<NamedDecl>(DC))) { |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1313 | if (SemaRef.ActiveTemplateInstantiations.empty() && |
| 1314 | // FIXME: Do something better in this case. |
| 1315 | !SemaRef.getLangOpts().ModulesLocalVisibility) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1316 | // Cache the fact that this declaration is implicitly visible because |
| 1317 | // its parent has a visible definition. |
| 1318 | D->setHidden(false); |
| 1319 | } |
| 1320 | return true; |
| 1321 | } |
| 1322 | return false; |
| 1323 | } |
| 1324 | |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1325 | // Find the extra places where we need to look. |
| 1326 | llvm::DenseSet<Module*> &LookupModules = SemaRef.getLookupModules(); |
| 1327 | if (LookupModules.empty()) |
| 1328 | return false; |
| 1329 | |
| 1330 | // If our lookup set contains the decl's module, it's visible. |
| 1331 | if (LookupModules.count(DeclModule)) |
| 1332 | return true; |
| 1333 | |
| 1334 | // If the declaration isn't exported, it's not visible in any other module. |
| 1335 | if (D->isModulePrivate()) |
| 1336 | return false; |
| 1337 | |
| 1338 | // Check whether DeclModule is transitively exported to an import of |
| 1339 | // the lookup set. |
| 1340 | for (llvm::DenseSet<Module *>::iterator I = LookupModules.begin(), |
| 1341 | E = LookupModules.end(); |
| 1342 | I != E; ++I) |
| 1343 | if ((*I)->isModuleVisible(DeclModule)) |
| 1344 | return true; |
| 1345 | return false; |
| 1346 | } |
| 1347 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1348 | bool Sema::isVisibleSlow(const NamedDecl *D) { |
| 1349 | return LookupResult::isVisible(*this, const_cast<NamedDecl*>(D)); |
| 1350 | } |
| 1351 | |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1352 | /// \brief Retrieve the visible declaration corresponding to D, if any. |
| 1353 | /// |
| 1354 | /// This routine determines whether the declaration D is visible in the current |
| 1355 | /// module, with the current imports. If not, it checks whether any |
| 1356 | /// redeclaration of D is visible, and if so, returns that declaration. |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1357 | /// |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1358 | /// \returns D, or a visible previous declaration of D, whichever is more recent |
| 1359 | /// and visible. If no declaration of D is visible, returns null. |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 1360 | static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { |
| 1361 | assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1362 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 1363 | for (auto RD : D->redecls()) { |
| 1364 | if (auto ND = dyn_cast<NamedDecl>(RD)) { |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1365 | // FIXME: This is wrong in the case where the previous declaration is not |
| 1366 | // visible in the same scope as D. This needs to be done much more |
| 1367 | // carefully. |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 1368 | if (LookupResult::isVisible(SemaRef, ND)) |
Douglas Gregor | 5407920 | 2012-01-06 22:05:37 +0000 | [diff] [blame] | 1369 | return ND; |
| 1370 | } |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1371 | } |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1372 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1373 | return nullptr; |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 1376 | NamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl *D) const { |
Kaelyn Takata | fc8c61a | 2014-11-11 23:00:42 +0000 | [diff] [blame] | 1377 | return findAcceptableDecl(getSema(), D); |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1380 | /// @brief Perform unqualified name lookup starting from a given |
| 1381 | /// scope. |
| 1382 | /// |
| 1383 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 1384 | /// used to find names within the current scope. For example, 'x' in |
| 1385 | /// @code |
| 1386 | /// int x; |
| 1387 | /// int f() { |
| 1388 | /// return x; // unqualified name look finds 'x' in the global scope |
| 1389 | /// } |
| 1390 | /// @endcode |
| 1391 | /// |
| 1392 | /// Different lookup criteria can find different names. For example, a |
| 1393 | /// particular scope can have both a struct and a function of the same |
| 1394 | /// name, and each can be found by certain lookup criteria. For more |
| 1395 | /// information about lookup criteria, see the documentation for the |
| 1396 | /// class LookupCriteria. |
| 1397 | /// |
| 1398 | /// @param S The scope from which unqualified name lookup will |
| 1399 | /// begin. If the lookup criteria permits, name lookup may also search |
| 1400 | /// in the parent scopes. |
| 1401 | /// |
James Dennett | 91738ff | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 1402 | /// @param [in,out] R Specifies the lookup to perform (e.g., the name to |
| 1403 | /// look up and the lookup kind), and is updated with the results of lookup |
| 1404 | /// including zero or more declarations and possibly additional information |
| 1405 | /// used to diagnose ambiguities. |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1406 | /// |
James Dennett | 91738ff | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 1407 | /// @returns \c true if lookup succeeded and false otherwise. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1408 | bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) { |
| 1409 | DeclarationName Name = R.getLookupName(); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1410 | if (!Name) return false; |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1411 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1412 | LookupNameKind NameKind = R.getLookupKind(); |
| 1413 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1414 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1415 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 1416 | // search in the declarations attached to the name. |
John McCall | ea305ed | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 1417 | if (NameKind == Sema::LookupRedeclarationWithLinkage) { |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1418 | // Find the nearest non-transparent declaration scope. |
| 1419 | while (!(S->getFlags() & Scope::DeclScope) || |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1420 | (S->getEntity() && S->getEntity()->isTransparentContext())) |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1421 | S = S->getParent(); |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1424 | // When performing a scope lookup, we want to find local extern decls. |
| 1425 | FindLocalExternScope FindLocals(R); |
| 1426 | |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1427 | // Scan up the scope chain looking for a decl that matches this |
| 1428 | // identifier that is in the appropriate namespace. This search |
| 1429 | // should not take long, as shadowing of names is uncommon, and |
| 1430 | // deep shadowing is extremely uncommon. |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1431 | bool LeftStartingScope = false; |
| 1432 | |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1433 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | IEnd = IdResolver.end(); |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1435 | I != IEnd; ++I) |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1436 | if (NamedDecl *D = R.getAcceptableDecl(*I)) { |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1437 | if (NameKind == LookupRedeclarationWithLinkage) { |
| 1438 | // Determine whether this (or a previous) declaration is |
| 1439 | // out-of-scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1440 | if (!LeftStartingScope && !S->isDeclScope(*I)) |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1441 | LeftStartingScope = true; |
| 1442 | |
| 1443 | // If we found something outside of our starting scope that |
| 1444 | // does not have linkage, skip it. |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1445 | if (LeftStartingScope && !((*I)->hasLinkage())) { |
| 1446 | R.setShadowed(); |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1447 | continue; |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1448 | } |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1449 | } |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1450 | else if (NameKind == LookupObjCImplicitSelfParam && |
| 1451 | !isa<ImplicitParamDecl>(*I)) |
| 1452 | continue; |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1453 | |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 1454 | R.addDecl(D); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1455 | |
Douglas Gregor | b59643b | 2012-01-03 23:26:26 +0000 | [diff] [blame] | 1456 | // Check whether there are any other declarations with the same name |
| 1457 | // and in the same scope. |
Douglas Gregor | 9b7b391 | 2012-01-04 16:44:10 +0000 | [diff] [blame] | 1458 | if (I != IEnd) { |
Douglas Gregor | 81bd038 | 2012-01-13 23:06:53 +0000 | [diff] [blame] | 1459 | // Find the scope in which this declaration was declared (if it |
| 1460 | // actually exists in a Scope). |
| 1461 | while (S && !S->isDeclScope(D)) |
| 1462 | S = S->getParent(); |
| 1463 | |
| 1464 | // If the scope containing the declaration is the translation unit, |
| 1465 | // then we'll need to perform our checks based on the matching |
| 1466 | // DeclContexts rather than matching scopes. |
| 1467 | if (S && isNamespaceOrTranslationUnitScope(S)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1468 | S = nullptr; |
Douglas Gregor | 81bd038 | 2012-01-13 23:06:53 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Compute the DeclContext, if we need it. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1471 | DeclContext *DC = nullptr; |
Douglas Gregor | 81bd038 | 2012-01-13 23:06:53 +0000 | [diff] [blame] | 1472 | if (!S) |
| 1473 | DC = (*I)->getDeclContext()->getRedeclContext(); |
| 1474 | |
Douglas Gregor | 9b7b391 | 2012-01-04 16:44:10 +0000 | [diff] [blame] | 1475 | IdentifierResolver::iterator LastI = I; |
| 1476 | for (++LastI; LastI != IEnd; ++LastI) { |
Douglas Gregor | 81bd038 | 2012-01-13 23:06:53 +0000 | [diff] [blame] | 1477 | if (S) { |
| 1478 | // Match based on scope. |
| 1479 | if (!S->isDeclScope(*LastI)) |
| 1480 | break; |
| 1481 | } else { |
| 1482 | // Match based on DeclContext. |
| 1483 | DeclContext *LastDC |
| 1484 | = (*LastI)->getDeclContext()->getRedeclContext(); |
| 1485 | if (!LastDC->Equals(DC)) |
| 1486 | break; |
| 1487 | } |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 1488 | |
| 1489 | // If the declaration is in the right namespace and visible, add it. |
| 1490 | if (NamedDecl *LastD = R.getAcceptableDecl(*LastI)) |
| 1491 | R.addDecl(LastD); |
Douglas Gregor | 9b7b391 | 2012-01-04 16:44:10 +0000 | [diff] [blame] | 1492 | } |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1493 | |
Douglas Gregor | 9b7b391 | 2012-01-04 16:44:10 +0000 | [diff] [blame] | 1494 | R.resolveKind(); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1495 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1496 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1497 | return true; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1498 | } |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1499 | } else { |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1500 | // Perform C++ unqualified name lookup. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1501 | if (CppLookupName(R, S)) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1502 | return true; |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | // If we didn't find a use of this identifier, and if the identifier |
| 1506 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 1507 | // now, injecting it into translation unit scope, and return it. |
Axel Naumann | 43dec14 | 2011-04-13 13:19:46 +0000 | [diff] [blame] | 1508 | if (AllowBuiltinCreation && LookupBuiltin(*this, R)) |
| 1509 | return true; |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1510 | |
Axel Naumann | 016538a | 2011-02-24 16:47:47 +0000 | [diff] [blame] | 1511 | // If we didn't find a use of this identifier, the ExternalSource |
| 1512 | // may be able to handle the situation. |
| 1513 | // Note: some lookup failures are expected! |
| 1514 | // See e.g. R.isForRedeclaration(). |
| 1515 | return (ExternalSource && ExternalSource->LookupUnqualified(R, S)); |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1518 | /// @brief Perform qualified name lookup in the namespaces nominated by |
| 1519 | /// using directives by the given context. |
| 1520 | /// |
| 1521 | /// C++98 [namespace.qual]p2: |
James Dennett | 51a8d8b | 2012-06-19 21:05:49 +0000 | [diff] [blame] | 1522 | /// Given X::m (where X is a user-declared namespace), or given \::m |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1523 | /// (where X is the global namespace), let S be the set of all |
| 1524 | /// declarations of m in X and in the transitive closure of all |
| 1525 | /// namespaces nominated by using-directives in X and its used |
| 1526 | /// namespaces, except that using-directives are ignored in any |
| 1527 | /// namespace, including X, directly containing one or more |
| 1528 | /// declarations of m. No namespace is searched more than once in |
| 1529 | /// the lookup of a name. If S is the empty set, the program is |
| 1530 | /// ill-formed. Otherwise, if S has exactly one member, or if the |
| 1531 | /// context of the reference is a using-declaration |
| 1532 | /// (namespace.udecl), S is the required set of declarations of |
| 1533 | /// m. Otherwise if the use of m is not one that allows a unique |
| 1534 | /// declaration to be chosen from S, the program is ill-formed. |
James Dennett | 51a8d8b | 2012-06-19 21:05:49 +0000 | [diff] [blame] | 1535 | /// |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1536 | /// C++98 [namespace.qual]p5: |
| 1537 | /// During the lookup of a qualified namespace member name, if the |
| 1538 | /// lookup finds more than one declaration of the member, and if one |
| 1539 | /// declaration introduces a class name or enumeration name and the |
| 1540 | /// other declarations either introduce the same object, the same |
| 1541 | /// enumerator or a set of functions, the non-type name hides the |
| 1542 | /// class or enumeration name if and only if the declarations are |
| 1543 | /// from the same namespace; otherwise (the declarations are from |
| 1544 | /// different namespaces), the program is ill-formed. |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1545 | static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1546 | DeclContext *StartDC) { |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1547 | assert(StartDC->isFileContext() && "start context is not a file context"); |
| 1548 | |
Aaron Ballman | 804a7fb | 2014-03-17 17:14:12 +0000 | [diff] [blame] | 1549 | DeclContext::udir_range UsingDirectives = StartDC->using_directives(); |
| 1550 | if (UsingDirectives.begin() == UsingDirectives.end()) return false; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1551 | |
| 1552 | // We have at least added all these contexts to the queue. |
Benjamin Kramer | 3adbe1c | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 1553 | llvm::SmallPtrSet<DeclContext*, 8> Visited; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1554 | Visited.insert(StartDC); |
| 1555 | |
| 1556 | // We have not yet looked into these namespaces, much less added |
| 1557 | // their "using-children" to the queue. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1558 | SmallVector<NamespaceDecl*, 8> Queue; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1559 | |
| 1560 | // We have already looked into the initial namespace; seed the queue |
| 1561 | // with its using-children. |
Aaron Ballman | 804a7fb | 2014-03-17 17:14:12 +0000 | [diff] [blame] | 1562 | for (auto *I : UsingDirectives) { |
| 1563 | NamespaceDecl *ND = I->getNominatedNamespace()->getOriginalNamespace(); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1564 | if (Visited.insert(ND).second) |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1565 | Queue.push_back(ND); |
| 1566 | } |
| 1567 | |
| 1568 | // The easiest way to implement the restriction in [namespace.qual]p5 |
| 1569 | // is to check whether any of the individual results found a tag |
| 1570 | // and, if so, to declare an ambiguity if the final result is not |
| 1571 | // a tag. |
| 1572 | bool FoundTag = false; |
| 1573 | bool FoundNonTag = false; |
| 1574 | |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1575 | LookupResult LocalR(LookupResult::Temporary, R); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1576 | |
| 1577 | bool Found = false; |
| 1578 | while (!Queue.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1579 | NamespaceDecl *ND = Queue.pop_back_val(); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1580 | |
| 1581 | // We go through some convolutions here to avoid copying results |
| 1582 | // between LookupResults. |
| 1583 | bool UseLocal = !R.empty(); |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1584 | LookupResult &DirectR = UseLocal ? LocalR : R; |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1585 | bool FoundDirect = LookupDirect(S, DirectR, ND); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (FoundDirect) { |
| 1588 | // First do any local hiding. |
| 1589 | DirectR.resolveKind(); |
| 1590 | |
| 1591 | // If the local result is a tag, remember that. |
| 1592 | if (DirectR.isSingleTagDecl()) |
| 1593 | FoundTag = true; |
| 1594 | else |
| 1595 | FoundNonTag = true; |
| 1596 | |
| 1597 | // Append the local results to the total results if necessary. |
| 1598 | if (UseLocal) { |
| 1599 | R.addAllDecls(LocalR); |
| 1600 | LocalR.clear(); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | // If we find names in this namespace, ignore its using directives. |
| 1605 | if (FoundDirect) { |
| 1606 | Found = true; |
| 1607 | continue; |
| 1608 | } |
| 1609 | |
Aaron Ballman | 804a7fb | 2014-03-17 17:14:12 +0000 | [diff] [blame] | 1610 | for (auto I : ND->using_directives()) { |
Aaron Ballman | 63ab760 | 2014-03-07 13:44:44 +0000 | [diff] [blame] | 1611 | NamespaceDecl *Nom = I->getNominatedNamespace(); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1612 | if (Visited.insert(Nom).second) |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1613 | Queue.push_back(Nom); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | if (Found) { |
| 1618 | if (FoundTag && FoundNonTag) |
| 1619 | R.setAmbiguousQualifiedTagHiding(); |
| 1620 | else |
| 1621 | R.resolveKind(); |
| 1622 | } |
| 1623 | |
| 1624 | return Found; |
| 1625 | } |
| 1626 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1627 | /// \brief Callback that looks for any member of a class with the given name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1628 | static bool LookupAnyMember(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1629 | CXXBasePath &Path, |
| 1630 | void *Name) { |
| 1631 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1632 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1633 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 1634 | Path.Decls = BaseRecord->lookup(N); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1635 | return !Path.Decls.empty(); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1638 | /// \brief Determine whether the given set of member declarations contains only |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1639 | /// static members, nested types, and enumerators. |
| 1640 | template<typename InputIterator> |
| 1641 | static bool HasOnlyStaticMembers(InputIterator First, InputIterator Last) { |
| 1642 | Decl *D = (*First)->getUnderlyingDecl(); |
| 1643 | if (isa<VarDecl>(D) || isa<TypeDecl>(D) || isa<EnumConstantDecl>(D)) |
| 1644 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1645 | |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1646 | if (isa<CXXMethodDecl>(D)) { |
| 1647 | // Determine whether all of the methods are static. |
| 1648 | bool AllMethodsAreStatic = true; |
| 1649 | for(; First != Last; ++First) { |
| 1650 | D = (*First)->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1651 | |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1652 | if (!isa<CXXMethodDecl>(D)) { |
| 1653 | assert(isa<TagDecl>(D) && "Non-function must be a tag decl"); |
| 1654 | break; |
| 1655 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1657 | if (!cast<CXXMethodDecl>(D)->isStatic()) { |
| 1658 | AllMethodsAreStatic = false; |
| 1659 | break; |
| 1660 | } |
| 1661 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1663 | if (AllMethodsAreStatic) |
| 1664 | return true; |
| 1665 | } |
| 1666 | |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1670 | /// \brief Perform qualified name lookup into a given context. |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1671 | /// |
| 1672 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 1673 | /// names when the context of those names is explicit specified, e.g., |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1674 | /// "std::vector" or "x->member", or as part of unqualified name lookup. |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1675 | /// |
| 1676 | /// Different lookup criteria can find different names. For example, a |
| 1677 | /// particular scope can have both a struct and a function of the same |
| 1678 | /// name, and each can be found by certain lookup criteria. For more |
| 1679 | /// information about lookup criteria, see the documentation for the |
| 1680 | /// class LookupCriteria. |
| 1681 | /// |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1682 | /// \param R captures both the lookup criteria and any lookup results found. |
| 1683 | /// |
| 1684 | /// \param LookupCtx The context in which qualified name lookup will |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1685 | /// search. If the lookup criteria permits, name lookup may also search |
| 1686 | /// in the parent contexts or (for C++ classes) base classes. |
| 1687 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1688 | /// \param InUnqualifiedLookup true if this is qualified name lookup that |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1689 | /// occurs as part of unqualified name lookup. |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1690 | /// |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1691 | /// \returns true if lookup succeeded, false if it failed. |
| 1692 | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, |
| 1693 | bool InUnqualifiedLookup) { |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1694 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1696 | if (!R.getLookupName()) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1697 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1699 | // Make sure that the declaration context is complete. |
| 1700 | assert((!isa<TagDecl>(LookupCtx) || |
| 1701 | LookupCtx->isDependentContext() || |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1702 | cast<TagDecl>(LookupCtx)->isCompleteDefinition() || |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 1703 | cast<TagDecl>(LookupCtx)->isBeingDefined()) && |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1704 | "Declaration context must already be complete!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1706 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1707 | if (LookupDirect(*this, R, LookupCtx)) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1708 | R.resolveKind(); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1709 | if (isa<CXXRecordDecl>(LookupCtx)) |
| 1710 | R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1711 | return true; |
| 1712 | } |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1713 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1714 | // Don't descend into implied contexts for redeclarations. |
| 1715 | // C++98 [namespace.qual]p6: |
| 1716 | // In a declaration for a namespace member in which the |
| 1717 | // declarator-id is a qualified-id, given that the qualified-id |
| 1718 | // for the namespace member has the form |
| 1719 | // nested-name-specifier unqualified-id |
| 1720 | // the unqualified-id shall name a member of the namespace |
| 1721 | // designated by the nested-name-specifier. |
| 1722 | // See also [class.mfct]p5 and [class.static.data]p2. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1723 | if (R.isForRedeclaration()) |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1724 | return false; |
| 1725 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1726 | // If this is a namespace, look it up in the implied namespaces. |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1727 | if (LookupCtx->isFileContext()) |
Douglas Gregor | d3a5918 | 2010-02-12 05:48:04 +0000 | [diff] [blame] | 1728 | return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1729 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1730 | // If this isn't a C++ class, we aren't allowed to look into base |
Douglas Gregor | cc2427c | 2009-09-11 22:57:37 +0000 | [diff] [blame] | 1731 | // classes, we're done. |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1732 | CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); |
Douglas Gregor | 5a5fcd8 | 2010-07-01 00:21:21 +0000 | [diff] [blame] | 1733 | if (!LookupRec || !LookupRec->getDefinition()) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1734 | return false; |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1735 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1736 | // If we're performing qualified name lookup into a dependent class, |
| 1737 | // then we are actually looking into a current instantiation. If we have any |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1738 | // dependent base classes, then we either have to delay lookup until |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 1739 | // template instantiation time (at which point all bases will be available) |
| 1740 | // or we have to fail. |
| 1741 | if (!InUnqualifiedLookup && LookupRec->isDependentContext() && |
| 1742 | LookupRec->hasAnyDependentBases()) { |
| 1743 | R.setNotFoundInCurrentInstantiation(); |
| 1744 | return false; |
| 1745 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1746 | |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1747 | // Perform lookup into our base classes. |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1748 | CXXBasePaths Paths; |
| 1749 | Paths.setOrigin(LookupRec); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1750 | |
| 1751 | // Look for this member in our base classes |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1752 | CXXRecordDecl::BaseMatchesCallback *BaseCallback = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1753 | switch (R.getLookupKind()) { |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1754 | case LookupObjCImplicitSelfParam: |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1755 | case LookupOrdinaryName: |
| 1756 | case LookupMemberName: |
| 1757 | case LookupRedeclarationWithLinkage: |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1758 | case LookupLocalFriendName: |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1759 | BaseCallback = &CXXRecordDecl::FindOrdinaryMember; |
| 1760 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1761 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1762 | case LookupTagName: |
| 1763 | BaseCallback = &CXXRecordDecl::FindTagMember; |
| 1764 | break; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1766 | case LookupAnyName: |
| 1767 | BaseCallback = &LookupAnyMember; |
| 1768 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1769 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1770 | case LookupUsingDeclName: |
| 1771 | // This lookup is for redeclarations only. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1772 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1773 | case LookupOperatorName: |
| 1774 | case LookupNamespaceName: |
| 1775 | case LookupObjCProtocolName: |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1776 | case LookupLabel: |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1777 | // These lookups will never find a member in a C++ class (or base class). |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1778 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1779 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1780 | case LookupNestedNameSpecifierName: |
| 1781 | BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember; |
| 1782 | break; |
| 1783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1785 | if (!LookupRec->lookupInBases(BaseCallback, |
| 1786 | R.getLookupName().getAsOpaquePtr(), Paths)) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1787 | return false; |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1788 | |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1789 | R.setNamingClass(LookupRec); |
| 1790 | |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1791 | // C++ [class.member.lookup]p2: |
| 1792 | // [...] If the resulting set of declarations are not all from |
| 1793 | // sub-objects of the same type, or the set has a nonstatic member |
| 1794 | // and includes members from distinct sub-objects, there is an |
| 1795 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 1796 | // the result of the lookup. |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1797 | QualType SubobjectType; |
Daniel Dunbar | 435bbe0 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1798 | int SubobjectNumber = 0; |
John McCall | a332b95 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 1799 | AccessSpecifier SubobjectAccess = AS_none; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1801 | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1802 | Path != PathEnd; ++Path) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1803 | const CXXBasePathElement &PathElement = Path->back(); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1804 | |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1805 | // Pick the best (i.e. most permissive i.e. numerically lowest) access |
| 1806 | // across all paths. |
| 1807 | SubobjectAccess = std::min(SubobjectAccess, Path->Access); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1809 | // Determine whether we're looking at a distinct sub-object or not. |
| 1810 | if (SubobjectType.isNull()) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1811 | // This is the first subobject we've looked at. Record its type. |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1812 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 1813 | SubobjectNumber = PathElement.SubobjectNumber; |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1814 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1817 | if (SubobjectType |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1818 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 1819 | // We found members of the given name in two subobjects of |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1820 | // different types. If the declaration sets aren't the same, this |
Nikola Smiljanic | 1c12568 | 2014-07-09 05:42:35 +0000 | [diff] [blame] | 1821 | // lookup is ambiguous. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1822 | if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) { |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1823 | CXXBasePaths::paths_iterator FirstPath = Paths.begin(); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1824 | DeclContext::lookup_iterator FirstD = FirstPath->Decls.begin(); |
| 1825 | DeclContext::lookup_iterator CurrentD = Path->Decls.begin(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1826 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1827 | while (FirstD != FirstPath->Decls.end() && |
| 1828 | CurrentD != Path->Decls.end()) { |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1829 | if ((*FirstD)->getUnderlyingDecl()->getCanonicalDecl() != |
| 1830 | (*CurrentD)->getUnderlyingDecl()->getCanonicalDecl()) |
| 1831 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1833 | ++FirstD; |
| 1834 | ++CurrentD; |
| 1835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1836 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1837 | if (FirstD == FirstPath->Decls.end() && |
| 1838 | CurrentD == Path->Decls.end()) |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1839 | continue; |
| 1840 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1841 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1842 | R.setAmbiguousBaseSubobjectTypes(Paths); |
| 1843 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Douglas Gregor | c0d2490 | 2010-10-22 22:08:47 +0000 | [diff] [blame] | 1846 | if (SubobjectNumber != PathElement.SubobjectNumber) { |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1847 | // We have a different subobject of the same type. |
| 1848 | |
| 1849 | // C++ [class.member.lookup]p5: |
| 1850 | // A static member, a nested type or an enumerator defined in |
| 1851 | // a base class T can unambiguously be found even if an object |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | // has more than one base class subobject of type T. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1853 | if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1854 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1856 | // We have found a nonstatic member name in multiple, distinct |
| 1857 | // subobjects. Name lookup is ambiguous. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1858 | R.setAmbiguousBaseSubobjects(Paths); |
| 1859 | return true; |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | // Lookup in a base class succeeded; return these results. |
| 1864 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 1865 | for (auto *D : Paths.front().Decls) { |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1866 | AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, |
| 1867 | D->getAccess()); |
| 1868 | R.addDecl(D, AS); |
| 1869 | } |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1870 | R.resolveKind(); |
| 1871 | return true; |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 1874 | /// \brief Performs qualified name lookup or special type of lookup for |
| 1875 | /// "__super::" scope specifier. |
| 1876 | /// |
| 1877 | /// This routine is a convenience overload meant to be called from contexts |
| 1878 | /// that need to perform a qualified name lookup with an optional C++ scope |
| 1879 | /// specifier that might require special kind of lookup. |
| 1880 | /// |
| 1881 | /// \param R captures both the lookup criteria and any lookup results found. |
| 1882 | /// |
| 1883 | /// \param LookupCtx The context in which qualified name lookup will |
| 1884 | /// search. |
| 1885 | /// |
| 1886 | /// \param SS An optional C++ scope-specifier. |
| 1887 | /// |
| 1888 | /// \returns true if lookup succeeded, false if it failed. |
| 1889 | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, |
| 1890 | CXXScopeSpec &SS) { |
| 1891 | auto *NNS = SS.getScopeRep(); |
| 1892 | if (NNS && NNS->getKind() == NestedNameSpecifier::Super) |
| 1893 | return LookupInSuper(R, NNS->getAsRecordDecl()); |
| 1894 | else |
| 1895 | |
| 1896 | return LookupQualifiedName(R, LookupCtx); |
| 1897 | } |
| 1898 | |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1899 | /// @brief Performs name lookup for a name that was parsed in the |
| 1900 | /// source code, and may contain a C++ scope specifier. |
| 1901 | /// |
| 1902 | /// This routine is a convenience routine meant to be called from |
| 1903 | /// contexts that receive a name and an optional C++ scope specifier |
| 1904 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 1905 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
Nikola Smiljanic | 905bfda | 2014-10-04 10:17:57 +0000 | [diff] [blame] | 1906 | /// respectively) on the given name and return those results. It will |
| 1907 | /// perform a special type of lookup for "__super::" scope specifier. |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1908 | /// |
| 1909 | /// @param S The scope from which unqualified name lookup will |
| 1910 | /// begin. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | /// |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1912 | /// @param SS An optional C++ scope-specifier, e.g., "::N::M". |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1913 | /// |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1914 | /// @param EnteringContext Indicates whether we are going to enter the |
| 1915 | /// context of the scope-specifier SS (if present). |
| 1916 | /// |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1917 | /// @returns True if any decls were found (but possibly ambiguous) |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1918 | bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1919 | bool AllowBuiltinCreation, bool EnteringContext) { |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1920 | if (SS && SS->isInvalid()) { |
| 1921 | // When the scope specifier is invalid, don't even look for |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1922 | // anything. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1923 | return false; |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1924 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1926 | if (SS && SS->isSet()) { |
Nikola Smiljanic | 905bfda | 2014-10-04 10:17:57 +0000 | [diff] [blame] | 1927 | NestedNameSpecifier *NNS = SS->getScopeRep(); |
| 1928 | if (NNS->getKind() == NestedNameSpecifier::Super) |
| 1929 | return LookupInSuper(R, NNS->getAsRecordDecl()); |
| 1930 | |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1931 | if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | // We have resolved the scope specifier to a particular declaration |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1933 | // contex, and will perform name lookup in that context. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1934 | if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1935 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1937 | R.setContextRange(SS->getRange()); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1938 | return LookupQualifiedName(R, DC); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1939 | } |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1940 | |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1941 | // We could not resolve the scope specified to a specific declaration |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | // context, which means that SS refers to an unknown specialization. |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 1943 | // Name lookup can't find anything in this case. |
Douglas Gregor | 89ab56d | 2011-10-24 22:24:50 +0000 | [diff] [blame] | 1944 | R.setNotFoundInCurrentInstantiation(); |
| 1945 | R.setContextRange(SS->getRange()); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1946 | return false; |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | // Perform unqualified name lookup starting in the given scope. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1950 | return LookupName(R, S, AllowBuiltinCreation); |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1953 | /// \brief Perform qualified name lookup into all base classes of the given |
| 1954 | /// class. |
| 1955 | /// |
| 1956 | /// \param R captures both the lookup criteria and any lookup results found. |
| 1957 | /// |
| 1958 | /// \param Class The context in which qualified name lookup will |
| 1959 | /// search. Name lookup will search in all base classes merging the results. |
Nikola Smiljanic | 905bfda | 2014-10-04 10:17:57 +0000 | [diff] [blame] | 1960 | /// |
| 1961 | /// @returns True if any decls were found (but possibly ambiguous) |
| 1962 | bool Sema::LookupInSuper(LookupResult &R, CXXRecordDecl *Class) { |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1963 | for (const auto &BaseSpec : Class->bases()) { |
| 1964 | CXXRecordDecl *RD = cast<CXXRecordDecl>( |
| 1965 | BaseSpec.getType()->castAs<RecordType>()->getDecl()); |
| 1966 | LookupResult Result(*this, R.getLookupNameInfo(), R.getLookupKind()); |
| 1967 | Result.setBaseObjectType(Context.getRecordType(Class)); |
| 1968 | LookupQualifiedName(Result, RD); |
| 1969 | for (auto *Decl : Result) |
| 1970 | R.addDecl(Decl); |
| 1971 | } |
| 1972 | |
| 1973 | R.resolveKind(); |
Nikola Smiljanic | 905bfda | 2014-10-04 10:17:57 +0000 | [diff] [blame] | 1974 | |
| 1975 | return !R.empty(); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1976 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1977 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 1978 | /// \brief Produce a diagnostic describing the ambiguity that resulted |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1979 | /// from name lookup. |
| 1980 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 1981 | /// \param Result The result of the ambiguous lookup to be diagnosed. |
Serge Pavlov | 9929209 | 2013-08-29 07:23:24 +0000 | [diff] [blame] | 1982 | void Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1983 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 1984 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1985 | DeclarationName Name = Result.getLookupName(); |
| 1986 | SourceLocation NameLoc = Result.getNameLoc(); |
| 1987 | SourceRange LookupRange = Result.getContextRange(); |
| 1988 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1989 | switch (Result.getAmbiguityKind()) { |
| 1990 | case LookupResult::AmbiguousBaseSubobjects: { |
| 1991 | CXXBasePaths *Paths = Result.getBasePaths(); |
| 1992 | QualType SubobjectType = Paths->front().back().Base->getType(); |
| 1993 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 1994 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 1995 | << LookupRange; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1996 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1997 | DeclContext::lookup_iterator Found = Paths->front().Decls.begin(); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 1998 | while (isa<CXXMethodDecl>(*Found) && |
| 1999 | cast<CXXMethodDecl>(*Found)->isStatic()) |
| 2000 | ++Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2001 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2002 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
Serge Pavlov | 9929209 | 2013-08-29 07:23:24 +0000 | [diff] [blame] | 2003 | break; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2004 | } |
Douglas Gregor | 1c846b0 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 2005 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2006 | case LookupResult::AmbiguousBaseSubobjectTypes: { |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2007 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 2008 | << Name << LookupRange; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2009 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2010 | CXXBasePaths *Paths = Result.getBasePaths(); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2011 | std::set<Decl *> DeclsPrinted; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2012 | for (CXXBasePaths::paths_iterator Path = Paths->begin(), |
| 2013 | PathEnd = Paths->end(); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2014 | Path != PathEnd; ++Path) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2015 | Decl *D = Path->Decls.front(); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2016 | if (DeclsPrinted.insert(D).second) |
| 2017 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 2018 | } |
Serge Pavlov | 9929209 | 2013-08-29 07:23:24 +0000 | [diff] [blame] | 2019 | break; |
Douglas Gregor | 1c846b0 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2022 | case LookupResult::AmbiguousTagHiding: { |
| 2023 | Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; |
Douglas Gregor | f23311d | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 2024 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2025 | llvm::SmallPtrSet<NamedDecl*,8> TagDecls; |
| 2026 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2027 | for (auto *D : Result) |
| 2028 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2029 | TagDecls.insert(TD); |
| 2030 | Diag(TD->getLocation(), diag::note_hidden_tag); |
| 2031 | } |
| 2032 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2033 | for (auto *D : Result) |
| 2034 | if (!isa<TagDecl>(D)) |
| 2035 | Diag(D->getLocation(), diag::note_hiding_object); |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2036 | |
| 2037 | // For recovery purposes, go ahead and implement the hiding. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2038 | LookupResult::Filter F = Result.makeFilter(); |
| 2039 | while (F.hasNext()) { |
| 2040 | if (TagDecls.count(F.next())) |
| 2041 | F.erase(); |
| 2042 | } |
| 2043 | F.done(); |
Serge Pavlov | 9929209 | 2013-08-29 07:23:24 +0000 | [diff] [blame] | 2044 | break; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | case LookupResult::AmbiguousReference: { |
| 2048 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2049 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2050 | for (auto *D : Result) |
| 2051 | Diag(D->getLocation(), diag::note_ambiguous_candidate) << D; |
Serge Pavlov | 9929209 | 2013-08-29 07:23:24 +0000 | [diff] [blame] | 2052 | break; |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 2053 | } |
Serge Pavlov | 9929209 | 2013-08-29 07:23:24 +0000 | [diff] [blame] | 2054 | } |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2055 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2056 | |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2057 | namespace { |
| 2058 | struct AssociatedLookup { |
John McCall | 7d8b041 | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 2059 | AssociatedLookup(Sema &S, SourceLocation InstantiationLoc, |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2060 | Sema::AssociatedNamespaceSet &Namespaces, |
| 2061 | Sema::AssociatedClassSet &Classes) |
John McCall | 7d8b041 | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 2062 | : S(S), Namespaces(Namespaces), Classes(Classes), |
| 2063 | InstantiationLoc(InstantiationLoc) { |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | Sema &S; |
| 2067 | Sema::AssociatedNamespaceSet &Namespaces; |
| 2068 | Sema::AssociatedClassSet &Classes; |
John McCall | 7d8b041 | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 2069 | SourceLocation InstantiationLoc; |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2070 | }; |
| 2071 | } |
| 2072 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | static void |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2074 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T); |
John McCall | c7e8e79 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 2075 | |
Douglas Gregor | 8b89522 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 2076 | static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, |
| 2077 | DeclContext *Ctx) { |
| 2078 | // Add the associated namespace for this class. |
| 2079 | |
| 2080 | // We don't use DeclContext::getEnclosingNamespaceContext() as this may |
| 2081 | // be a locally scoped record. |
| 2082 | |
Sebastian Redl | bd59576 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 2083 | // We skip out of inline namespaces. The innermost non-inline namespace |
| 2084 | // contains all names of all its nested inline namespaces anyway, so we can |
| 2085 | // replace the entire inline namespace tree with its root. |
| 2086 | while (Ctx->isRecord() || Ctx->isTransparentContext() || |
| 2087 | Ctx->isInlineNamespace()) |
Douglas Gregor | 8b89522 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 2088 | Ctx = Ctx->getParent(); |
| 2089 | |
John McCall | c7e8e79 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 2090 | if (Ctx->isFileContext()) |
Douglas Gregor | 8b89522 | 2010-04-30 07:08:38 +0000 | [diff] [blame] | 2091 | Namespaces.insert(Ctx->getPrimaryContext()); |
John McCall | c7e8e79 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 2092 | } |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2093 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | // \brief Add the associated classes and namespaces for argument-dependent |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2095 | // lookup that involves a template argument (C++ [basic.lookup.koenig]p2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | static void |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2097 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
| 2098 | const TemplateArgument &Arg) { |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2099 | // C++ [basic.lookup.koenig]p2, last bullet: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | // -- [...] ; |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2101 | switch (Arg.getKind()) { |
| 2102 | case TemplateArgument::Null: |
| 2103 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2105 | case TemplateArgument::Type: |
| 2106 | // [...] the namespaces and classes associated with the types of the |
| 2107 | // template arguments provided for template type parameters (excluding |
| 2108 | // template template parameters) |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2109 | addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2110 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2112 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2113 | case TemplateArgument::TemplateExpansion: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | // [...] the namespaces in which any template template arguments are |
| 2115 | // defined; and the classes in which any member templates used as |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2116 | // template template arguments are defined. |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2117 | TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2118 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2119 | = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2120 | DeclContext *Ctx = ClassTemplate->getDeclContext(); |
| 2121 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2122 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2123 | // Add the associated namespace for this class. |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2124 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2125 | } |
| 2126 | break; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2127 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2129 | case TemplateArgument::Declaration: |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2130 | case TemplateArgument::Integral: |
| 2131 | case TemplateArgument::Expression: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2132 | case TemplateArgument::NullPtr: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | // [Note: non-type template arguments do not contribute to the set of |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2134 | // associated namespaces. ] |
| 2135 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2137 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2138 | for (const auto &P : Arg.pack_elements()) |
| 2139 | addAssociatedClassesAndNamespaces(Result, P); |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2140 | break; |
| 2141 | } |
| 2142 | } |
| 2143 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2144 | // \brief Add the associated classes and namespaces for |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | // argument-dependent lookup with an argument of class type |
| 2146 | // (C++ [basic.lookup.koenig]p2). |
| 2147 | static void |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2148 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, |
| 2149 | CXXRecordDecl *Class) { |
| 2150 | |
| 2151 | // Just silently ignore anything whose name is __va_list_tag. |
| 2152 | if (Class->getDeclName() == Result.S.VAListTagName) |
| 2153 | return; |
| 2154 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2155 | // C++ [basic.lookup.koenig]p2: |
| 2156 | // [...] |
| 2157 | // -- If T is a class type (including unions), its associated |
| 2158 | // classes are: the class itself; the class of which it is a |
| 2159 | // member, if any; and its direct and indirect base |
| 2160 | // classes. Its associated namespaces are the namespaces in |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | // which its associated classes are defined. |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Add the class of which it is a member, if any. |
| 2164 | DeclContext *Ctx = Class->getDeclContext(); |
| 2165 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2166 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2167 | // Add the associated namespace for this class. |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2168 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2170 | // Add the class itself. If we've already seen this class, we don't |
| 2171 | // need to visit base classes. |
Richard Smith | 594461f | 2014-03-14 22:07:27 +0000 | [diff] [blame] | 2172 | // |
| 2173 | // FIXME: That's not correct, we may have added this class only because it |
| 2174 | // was the enclosing class of another class, and in that case we won't have |
| 2175 | // added its base classes yet. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2176 | if (!Result.Classes.insert(Class).second) |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2177 | return; |
| 2178 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | // -- If T is a template-id, its associated namespaces and classes are |
| 2180 | // the namespace in which the template is defined; for member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2181 | // templates, the member template's class; the namespaces and classes |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | // associated with the types of the template arguments provided for |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2183 | // template type parameters (excluding template template parameters); the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | // namespaces in which any template template arguments are defined; and |
| 2185 | // the classes in which any member templates used as template template |
| 2186 | // arguments are defined. [Note: non-type template arguments do not |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2187 | // contribute to the set of associated namespaces. ] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2189 | = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { |
| 2190 | DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); |
| 2191 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2192 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2193 | // Add the associated namespace for this class. |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2194 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2196 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 2197 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2198 | addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); |
Douglas Gregor | 197e5f7 | 2009-07-08 07:51:57 +0000 | [diff] [blame] | 2199 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2201 | // Only recurse into base classes for complete types. |
Richard Smith | 594461f | 2014-03-14 22:07:27 +0000 | [diff] [blame] | 2202 | if (!Class->hasDefinition()) |
| 2203 | return; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2205 | // Add direct and indirect base classes along with their associated |
| 2206 | // namespaces. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2207 | SmallVector<CXXRecordDecl *, 32> Bases; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2208 | Bases.push_back(Class); |
| 2209 | while (!Bases.empty()) { |
| 2210 | // Pop this class off the stack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2211 | Class = Bases.pop_back_val(); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2212 | |
| 2213 | // Visit the base classes. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2214 | for (const auto &Base : Class->bases()) { |
| 2215 | const RecordType *BaseType = Base.getType()->getAs<RecordType>(); |
Sebastian Redl | c45c03c | 2009-10-25 09:35:33 +0000 | [diff] [blame] | 2216 | // In dependent contexts, we do ADL twice, and the first time around, |
| 2217 | // the base type might be a dependent TemplateSpecializationType, or a |
| 2218 | // TemplateTypeParmType. If that happens, simply ignore it. |
| 2219 | // FIXME: If we want to support export, we probably need to add the |
| 2220 | // namespace of the template in a TemplateSpecializationType, or even |
| 2221 | // the classes and namespaces of known non-dependent arguments. |
| 2222 | if (!BaseType) |
| 2223 | continue; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2224 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2225 | if (Result.Classes.insert(BaseDecl).second) { |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2226 | // Find the associated namespace for this base class. |
| 2227 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2228 | CollectEnclosingNamespace(Result.Namespaces, BaseCtx); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2229 | |
| 2230 | // Make sure we visit the bases of this base class. |
| 2231 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
| 2232 | Bases.push_back(BaseDecl); |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | // \brief Add the associated classes and namespaces for |
| 2239 | // argument-dependent lookup with an argument of type T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | // (C++ [basic.lookup.koenig]p2). |
| 2241 | static void |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2242 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2243 | // C++ [basic.lookup.koenig]p2: |
| 2244 | // |
| 2245 | // For each argument type T in the function call, there is a set |
| 2246 | // of zero or more associated namespaces and a set of zero or more |
| 2247 | // associated classes to be considered. The sets of namespaces and |
| 2248 | // classes is determined entirely by the types of the function |
| 2249 | // arguments (and the namespace of any template template |
| 2250 | // argument). Typedef names and using-declarations used to specify |
| 2251 | // the types do not contribute to this set. The sets of namespaces |
| 2252 | // and classes are determined in the following way: |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2253 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2254 | SmallVector<const Type *, 16> Queue; |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2255 | const Type *T = Ty->getCanonicalTypeInternal().getTypePtr(); |
| 2256 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2257 | while (true) { |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2258 | switch (T->getTypeClass()) { |
| 2259 | |
| 2260 | #define TYPE(Class, Base) |
| 2261 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2262 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2263 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2264 | #define ABSTRACT_TYPE(Class, Base) |
| 2265 | #include "clang/AST/TypeNodes.def" |
| 2266 | // T is canonical. We can also ignore dependent types because |
| 2267 | // we don't need to do ADL at the definition point, but if we |
| 2268 | // wanted to implement template export (or if we find some other |
| 2269 | // use for associated classes and namespaces...) this would be |
| 2270 | // wrong. |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2271 | break; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2272 | |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2273 | // -- If T is a pointer to U or an array of U, its associated |
| 2274 | // namespaces and classes are those associated with U. |
| 2275 | case Type::Pointer: |
| 2276 | T = cast<PointerType>(T)->getPointeeType().getTypePtr(); |
| 2277 | continue; |
| 2278 | case Type::ConstantArray: |
| 2279 | case Type::IncompleteArray: |
| 2280 | case Type::VariableArray: |
| 2281 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 2282 | continue; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2283 | |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2284 | // -- If T is a fundamental type, its associated sets of |
| 2285 | // namespaces and classes are both empty. |
| 2286 | case Type::Builtin: |
| 2287 | break; |
| 2288 | |
| 2289 | // -- If T is a class type (including unions), its associated |
| 2290 | // classes are: the class itself; the class of which it is a |
| 2291 | // member, if any; and its direct and indirect base |
| 2292 | // classes. Its associated namespaces are the namespaces in |
| 2293 | // which its associated classes are defined. |
| 2294 | case Type::Record: { |
Richard Smith | 594461f | 2014-03-14 22:07:27 +0000 | [diff] [blame] | 2295 | Result.S.RequireCompleteType(Result.InstantiationLoc, QualType(T, 0), |
| 2296 | /*no diagnostic*/ 0); |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2297 | CXXRecordDecl *Class |
| 2298 | = cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()); |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2299 | addAssociatedClassesAndNamespaces(Result, Class); |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2300 | break; |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2301 | } |
Douglas Gregor | fe60c14 | 2010-05-20 02:26:51 +0000 | [diff] [blame] | 2302 | |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2303 | // -- If T is an enumeration type, its associated namespace is |
| 2304 | // the namespace in which it is defined. If it is class |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2305 | // member, its associated class is the member's class; else |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2306 | // it has no associated class. |
| 2307 | case Type::Enum: { |
| 2308 | EnumDecl *Enum = cast<EnumType>(T)->getDecl(); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2309 | |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2310 | DeclContext *Ctx = Enum->getDeclContext(); |
| 2311 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2312 | Result.Classes.insert(EnclosingClass); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2313 | |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2314 | // Add the associated namespace for this class. |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2315 | CollectEnclosingNamespace(Result.Namespaces, Ctx); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2316 | |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2317 | break; |
| 2318 | } |
| 2319 | |
| 2320 | // -- If T is a function type, its associated namespaces and |
| 2321 | // classes are those associated with the function parameter |
| 2322 | // types and those associated with the return type. |
| 2323 | case Type::FunctionProto: { |
| 2324 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 2325 | for (const auto &Arg : Proto->param_types()) |
| 2326 | Queue.push_back(Arg.getTypePtr()); |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2327 | // fallthrough |
| 2328 | } |
| 2329 | case Type::FunctionNoProto: { |
| 2330 | const FunctionType *FnType = cast<FunctionType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2331 | T = FnType->getReturnType().getTypePtr(); |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2332 | continue; |
| 2333 | } |
| 2334 | |
| 2335 | // -- If T is a pointer to a member function of a class X, its |
| 2336 | // associated namespaces and classes are those associated |
| 2337 | // with the function parameter types and return type, |
| 2338 | // together with those associated with X. |
| 2339 | // |
| 2340 | // -- If T is a pointer to a data member of class X, its |
| 2341 | // associated namespaces and classes are those associated |
| 2342 | // with the member type together with those associated with |
| 2343 | // X. |
| 2344 | case Type::MemberPointer: { |
| 2345 | const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); |
| 2346 | |
| 2347 | // Queue up the class type into which this points. |
| 2348 | Queue.push_back(MemberPtr->getClass()); |
| 2349 | |
| 2350 | // And directly continue with the pointee type. |
| 2351 | T = MemberPtr->getPointeeType().getTypePtr(); |
| 2352 | continue; |
| 2353 | } |
| 2354 | |
| 2355 | // As an extension, treat this like a normal pointer. |
| 2356 | case Type::BlockPointer: |
| 2357 | T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr(); |
| 2358 | continue; |
| 2359 | |
| 2360 | // References aren't covered by the standard, but that's such an |
| 2361 | // obvious defect that we cover them anyway. |
| 2362 | case Type::LValueReference: |
| 2363 | case Type::RValueReference: |
| 2364 | T = cast<ReferenceType>(T)->getPointeeType().getTypePtr(); |
| 2365 | continue; |
| 2366 | |
| 2367 | // These are fundamental types. |
| 2368 | case Type::Vector: |
| 2369 | case Type::ExtVector: |
| 2370 | case Type::Complex: |
| 2371 | break; |
| 2372 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2373 | // Non-deduced auto types only get here for error cases. |
| 2374 | case Type::Auto: |
| 2375 | break; |
| 2376 | |
Douglas Gregor | 8e93666 | 2011-04-12 01:02:45 +0000 | [diff] [blame] | 2377 | // If T is an Objective-C object or interface type, or a pointer to an |
| 2378 | // object or interface type, the associated namespace is the global |
| 2379 | // namespace. |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2380 | case Type::ObjCObject: |
| 2381 | case Type::ObjCInterface: |
| 2382 | case Type::ObjCObjectPointer: |
Douglas Gregor | 8e93666 | 2011-04-12 01:02:45 +0000 | [diff] [blame] | 2383 | Result.Namespaces.insert(Result.S.Context.getTranslationUnitDecl()); |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2384 | break; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2385 | |
| 2386 | // Atomic types are just wrappers; use the associations of the |
| 2387 | // contained type. |
| 2388 | case Type::Atomic: |
| 2389 | T = cast<AtomicType>(T)->getValueType().getTypePtr(); |
| 2390 | continue; |
John McCall | 0af3d3b | 2010-05-28 06:08:54 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2393 | if (Queue.empty()) |
| 2394 | break; |
| 2395 | T = Queue.pop_back_val(); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2396 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | /// \brief Find the associated classes and namespaces for |
| 2400 | /// argument-dependent lookup for a call with the given set of |
| 2401 | /// arguments. |
| 2402 | /// |
| 2403 | /// This routine computes the sets of associated classes and associated |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | /// namespaces searched by argument-dependent lookup |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2405 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 2406 | void Sema::FindAssociatedClassesAndNamespaces( |
| 2407 | SourceLocation InstantiationLoc, ArrayRef<Expr *> Args, |
| 2408 | AssociatedNamespaceSet &AssociatedNamespaces, |
| 2409 | AssociatedClassSet &AssociatedClasses) { |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2410 | AssociatedNamespaces.clear(); |
| 2411 | AssociatedClasses.clear(); |
| 2412 | |
John McCall | 7d8b041 | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 2413 | AssociatedLookup Result(*this, InstantiationLoc, |
| 2414 | AssociatedNamespaces, AssociatedClasses); |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2415 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2416 | // C++ [basic.lookup.koenig]p2: |
| 2417 | // For each argument type T in the function call, there is a set |
| 2418 | // of zero or more associated namespaces and a set of zero or more |
| 2419 | // associated classes to be considered. The sets of namespaces and |
| 2420 | // classes is determined entirely by the types of the function |
| 2421 | // arguments (and the namespace of any template template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2422 | // argument). |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2423 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2424 | Expr *Arg = Args[ArgIdx]; |
| 2425 | |
| 2426 | if (Arg->getType() != Context.OverloadTy) { |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2427 | addAssociatedClassesAndNamespaces(Result, Arg->getType()); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2428 | continue; |
| 2429 | } |
| 2430 | |
| 2431 | // [...] In addition, if the argument is the name or address of a |
| 2432 | // set of overloaded functions and/or function templates, its |
| 2433 | // associated classes and namespaces are the union of those |
| 2434 | // associated with each of the members of the set: the namespace |
| 2435 | // in which the function or function template is defined and the |
| 2436 | // classes and namespaces associated with its (non-dependent) |
| 2437 | // parameter types and return type. |
Douglas Gregor | be75925 | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 2438 | Arg = Arg->IgnoreParens(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2439 | if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2440 | if (unaryOp->getOpcode() == UO_AddrOf) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2441 | Arg = unaryOp->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2442 | |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2443 | UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg); |
| 2444 | if (!ULE) continue; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2445 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2446 | for (const auto *D : ULE->decls()) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 2447 | // Look through any using declarations to find the underlying function. |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2448 | const FunctionDecl *FDecl = D->getUnderlyingDecl()->getAsFunction(); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2449 | |
| 2450 | // Add the classes and namespaces associated with the parameter |
| 2451 | // types and return type of this function. |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 2452 | addAssociatedClassesAndNamespaces(Result, FDecl->getType()); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2456 | |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2457 | NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2458 | SourceLocation Loc, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2459 | LookupNameKind NameKind, |
| 2460 | RedeclarationKind Redecl) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2461 | LookupResult R(*this, Name, Loc, NameKind, Redecl); |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2462 | LookupName(R, S); |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 2463 | return R.getAsSingle<NamedDecl>(); |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 2466 | /// \brief Find the protocol with the given name, if any. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2468 | SourceLocation IdLoc, |
| 2469 | RedeclarationKind Redecl) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2470 | Decl *D = LookupSingleName(TUScope, II, IdLoc, |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 2471 | LookupObjCProtocolName, Redecl); |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 2472 | return cast_or_null<ObjCProtocolDecl>(D); |
| 2473 | } |
| 2474 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2475 | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2476 | QualType T1, QualType T2, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2477 | UnresolvedSetImpl &Functions) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2478 | // C++ [over.match.oper]p3: |
| 2479 | // -- The set of non-member candidates is the result of the |
| 2480 | // unqualified lookup of operator@ in the context of the |
| 2481 | // expression according to the usual rules for name lookup in |
| 2482 | // unqualified function calls (3.4.2) except that all member |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 2483 | // functions are ignored. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2484 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2485 | LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); |
| 2486 | LookupName(Operators, S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2487 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2488 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 2489 | Functions.append(Operators.begin(), Operators.end()); |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2492 | Sema::SpecialMemberOverloadResult *Sema::LookupSpecialMember(CXXRecordDecl *RD, |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2493 | CXXSpecialMember SM, |
| 2494 | bool ConstArg, |
| 2495 | bool VolatileArg, |
| 2496 | bool RValueThis, |
| 2497 | bool ConstThis, |
| 2498 | bool VolatileThis) { |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 2499 | assert(CanDeclareSpecialMemberFunction(RD) && |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2500 | "doing special member lookup into record that isn't fully complete"); |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 2501 | RD = RD->getDefinition(); |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2502 | if (RValueThis || ConstThis || VolatileThis) |
| 2503 | assert((SM == CXXCopyAssignment || SM == CXXMoveAssignment) && |
| 2504 | "constructors and destructors always have unqualified lvalue this"); |
| 2505 | if (ConstArg || VolatileArg) |
| 2506 | assert((SM != CXXDefaultConstructor && SM != CXXDestructor) && |
| 2507 | "parameter-less special members can't have qualified arguments"); |
| 2508 | |
| 2509 | llvm::FoldingSetNodeID ID; |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2510 | ID.AddPointer(RD); |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2511 | ID.AddInteger(SM); |
| 2512 | ID.AddInteger(ConstArg); |
| 2513 | ID.AddInteger(VolatileArg); |
| 2514 | ID.AddInteger(RValueThis); |
| 2515 | ID.AddInteger(ConstThis); |
| 2516 | ID.AddInteger(VolatileThis); |
| 2517 | |
| 2518 | void *InsertPoint; |
| 2519 | SpecialMemberOverloadResult *Result = |
| 2520 | SpecialMemberCache.FindNodeOrInsertPos(ID, InsertPoint); |
| 2521 | |
| 2522 | // This was already cached |
| 2523 | if (Result) |
| 2524 | return Result; |
| 2525 | |
Alexis Hunt | ba8e18d | 2011-06-07 00:11:58 +0000 | [diff] [blame] | 2526 | Result = BumpAlloc.Allocate<SpecialMemberOverloadResult>(); |
| 2527 | Result = new (Result) SpecialMemberOverloadResult(ID); |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2528 | SpecialMemberCache.InsertNode(Result, InsertPoint); |
| 2529 | |
| 2530 | if (SM == CXXDestructor) { |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 2531 | if (RD->needsImplicitDestructor()) |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2532 | DeclareImplicitDestructor(RD); |
| 2533 | CXXDestructorDecl *DD = RD->getDestructor(); |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2534 | assert(DD && "record without a destructor"); |
| 2535 | Result->setMethod(DD); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2536 | Result->setKind(DD->isDeleted() ? |
| 2537 | SpecialMemberOverloadResult::NoMemberOrDeleted : |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2538 | SpecialMemberOverloadResult::Success); |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2539 | return Result; |
| 2540 | } |
| 2541 | |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2542 | // Prepare for overload resolution. Here we construct a synthetic argument |
| 2543 | // if necessary and make sure that implicit functions are declared. |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2544 | CanQualType CanTy = Context.getCanonicalType(Context.getTagDeclType(RD)); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2545 | DeclarationName Name; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2546 | Expr *Arg = nullptr; |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2547 | unsigned NumArgs; |
| 2548 | |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2549 | QualType ArgType = CanTy; |
| 2550 | ExprValueKind VK = VK_LValue; |
| 2551 | |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2552 | if (SM == CXXDefaultConstructor) { |
| 2553 | Name = Context.DeclarationNames.getCXXConstructorName(CanTy); |
| 2554 | NumArgs = 0; |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2555 | if (RD->needsImplicitDefaultConstructor()) |
| 2556 | DeclareImplicitDefaultConstructor(RD); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2557 | } else { |
| 2558 | if (SM == CXXCopyConstructor || SM == CXXMoveConstructor) { |
| 2559 | Name = Context.DeclarationNames.getCXXConstructorName(CanTy); |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 2560 | if (RD->needsImplicitCopyConstructor()) |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2561 | DeclareImplicitCopyConstructor(RD); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2562 | if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveConstructor()) |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2563 | DeclareImplicitMoveConstructor(RD); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2564 | } else { |
| 2565 | Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 2566 | if (RD->needsImplicitCopyAssignment()) |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2567 | DeclareImplicitCopyAssignment(RD); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2568 | if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveAssignment()) |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2569 | DeclareImplicitMoveAssignment(RD); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2572 | if (ConstArg) |
| 2573 | ArgType.addConst(); |
| 2574 | if (VolatileArg) |
| 2575 | ArgType.addVolatile(); |
| 2576 | |
| 2577 | // This isn't /really/ specified by the standard, but it's implied |
| 2578 | // we should be working from an RValue in the case of move to ensure |
| 2579 | // that we prefer to bind to rvalue references, and an LValue in the |
| 2580 | // case of copy to ensure we don't bind to rvalue references. |
| 2581 | // Possibly an XValue is actually correct in the case of move, but |
| 2582 | // there is no semantic difference for class types in this restricted |
| 2583 | // case. |
Alexis Hunt | 46d1ce2 | 2011-06-22 22:13:13 +0000 | [diff] [blame] | 2584 | if (SM == CXXCopyConstructor || SM == CXXCopyAssignment) |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2585 | VK = VK_LValue; |
| 2586 | else |
| 2587 | VK = VK_RValue; |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2588 | } |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2589 | |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2590 | OpaqueValueExpr FakeArg(SourceLocation(), ArgType, VK); |
| 2591 | |
| 2592 | if (SM != CXXDefaultConstructor) { |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2593 | NumArgs = 1; |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2594 | Arg = &FakeArg; |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | // Create the object argument |
| 2598 | QualType ThisTy = CanTy; |
| 2599 | if (ConstThis) |
| 2600 | ThisTy.addConst(); |
| 2601 | if (VolatileThis) |
| 2602 | ThisTy.addVolatile(); |
Alexis Hunt | 080709f | 2011-06-23 00:26:20 +0000 | [diff] [blame] | 2603 | Expr::Classification Classification = |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2604 | OpaqueValueExpr(SourceLocation(), ThisTy, |
| 2605 | RValueThis ? VK_RValue : VK_LValue).Classify(Context); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2606 | |
| 2607 | // Now we perform lookup on the name we computed earlier and do overload |
| 2608 | // resolution. Lookup is only performed directly into the class since there |
| 2609 | // will always be a (possibly implicit) declaration to shadow any others. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 2610 | OverloadCandidateSet OCS(RD->getLocation(), OverloadCandidateSet::CSK_Normal); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2611 | DeclContext::lookup_result R = RD->lookup(Name); |
Richard Smith | 8c37ab5 | 2015-02-11 01:48:47 +0000 | [diff] [blame] | 2612 | |
| 2613 | if (R.empty()) { |
| 2614 | // We might have no default constructor because we have a lambda's closure |
| 2615 | // type, rather than because there's some other declared constructor. |
| 2616 | // Every class has a copy/move constructor, copy/move assignment, and |
| 2617 | // destructor. |
| 2618 | assert(SM == CXXDefaultConstructor && |
| 2619 | "lookup for a constructor or assignment operator was empty"); |
| 2620 | Result->setMethod(nullptr); |
| 2621 | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 2622 | return Result; |
| 2623 | } |
Chandler Carruth | 7deaae7 | 2013-08-18 07:20:52 +0000 | [diff] [blame] | 2624 | |
| 2625 | // Copy the candidates as our processing of them may load new declarations |
| 2626 | // from an external source and invalidate lookup_result. |
| 2627 | SmallVector<NamedDecl *, 8> Candidates(R.begin(), R.end()); |
| 2628 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2629 | for (auto *Cand : Candidates) { |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2630 | if (Cand->isInvalidDecl()) |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2631 | continue; |
| 2632 | |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2633 | if (UsingShadowDecl *U = dyn_cast<UsingShadowDecl>(Cand)) { |
| 2634 | // FIXME: [namespace.udecl]p15 says that we should only consider a |
| 2635 | // using declaration here if it does not match a declaration in the |
| 2636 | // derived class. We do not implement this correctly in other cases |
| 2637 | // either. |
| 2638 | Cand = U->getTargetDecl(); |
| 2639 | |
| 2640 | if (Cand->isInvalidDecl()) |
| 2641 | continue; |
| 2642 | } |
| 2643 | |
| 2644 | if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(Cand)) { |
Alexis Hunt | 080709f | 2011-06-23 00:26:20 +0000 | [diff] [blame] | 2645 | if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2646 | AddMethodCandidate(M, DeclAccessPair::make(M, AS_public), RD, ThisTy, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2647 | Classification, llvm::makeArrayRef(&Arg, NumArgs), |
| 2648 | OCS, true); |
Alexis Hunt | 080709f | 2011-06-23 00:26:20 +0000 | [diff] [blame] | 2649 | else |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2650 | AddOverloadCandidate(M, DeclAccessPair::make(M, AS_public), |
| 2651 | llvm::makeArrayRef(&Arg, NumArgs), OCS, true); |
Alexis Hunt | 2949f02 | 2011-06-22 02:58:46 +0000 | [diff] [blame] | 2652 | } else if (FunctionTemplateDecl *Tmpl = |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2653 | dyn_cast<FunctionTemplateDecl>(Cand)) { |
Alexis Hunt | 080709f | 2011-06-23 00:26:20 +0000 | [diff] [blame] | 2654 | if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) |
| 2655 | AddMethodTemplateCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2656 | RD, nullptr, ThisTy, Classification, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2657 | llvm::makeArrayRef(&Arg, NumArgs), |
Alexis Hunt | 080709f | 2011-06-23 00:26:20 +0000 | [diff] [blame] | 2658 | OCS, true); |
| 2659 | else |
| 2660 | AddTemplateOverloadCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2661 | nullptr, llvm::makeArrayRef(&Arg, NumArgs), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2662 | OCS, true); |
Alexis Hunt | 1da3928 | 2011-06-24 02:11:39 +0000 | [diff] [blame] | 2663 | } else { |
| 2664 | assert(isa<UsingDecl>(Cand) && "illegal Kind of operator = Decl"); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | OverloadCandidateSet::iterator Best; |
| 2669 | switch (OCS.BestViableFunction(*this, SourceLocation(), Best)) { |
| 2670 | case OR_Success: |
| 2671 | Result->setMethod(cast<CXXMethodDecl>(Best->Function)); |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2672 | Result->setKind(SpecialMemberOverloadResult::Success); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2673 | break; |
| 2674 | |
| 2675 | case OR_Deleted: |
| 2676 | Result->setMethod(cast<CXXMethodDecl>(Best->Function)); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2677 | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | |
| 2680 | case OR_Ambiguous: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2681 | Result->setMethod(nullptr); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2682 | Result->setKind(SpecialMemberOverloadResult::Ambiguous); |
| 2683 | break; |
| 2684 | |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2685 | case OR_No_Viable_Function: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2686 | Result->setMethod(nullptr); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2687 | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2688 | break; |
| 2689 | } |
| 2690 | |
| 2691 | return Result; |
| 2692 | } |
| 2693 | |
| 2694 | /// \brief Look up the default constructor for the given class. |
| 2695 | CXXConstructorDecl *Sema::LookupDefaultConstructor(CXXRecordDecl *Class) { |
Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 2696 | SpecialMemberOverloadResult *Result = |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2697 | LookupSpecialMember(Class, CXXDefaultConstructor, false, false, false, |
| 2698 | false, false); |
| 2699 | |
| 2700 | return cast_or_null<CXXConstructorDecl>(Result->getMethod()); |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 2703 | /// \brief Look up the copying constructor for the given class. |
| 2704 | CXXConstructorDecl *Sema::LookupCopyingConstructor(CXXRecordDecl *Class, |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2705 | unsigned Quals) { |
Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 2706 | assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
| 2707 | "non-const, non-volatile qualifiers for copy ctor arg"); |
| 2708 | SpecialMemberOverloadResult *Result = |
| 2709 | LookupSpecialMember(Class, CXXCopyConstructor, Quals & Qualifiers::Const, |
| 2710 | Quals & Qualifiers::Volatile, false, false, false); |
| 2711 | |
Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 2712 | return cast_or_null<CXXConstructorDecl>(Result->getMethod()); |
| 2713 | } |
| 2714 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2715 | /// \brief Look up the moving constructor for the given class. |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 2716 | CXXConstructorDecl *Sema::LookupMovingConstructor(CXXRecordDecl *Class, |
| 2717 | unsigned Quals) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2718 | SpecialMemberOverloadResult *Result = |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 2719 | LookupSpecialMember(Class, CXXMoveConstructor, Quals & Qualifiers::Const, |
| 2720 | Quals & Qualifiers::Volatile, false, false, false); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2721 | |
| 2722 | return cast_or_null<CXXConstructorDecl>(Result->getMethod()); |
| 2723 | } |
| 2724 | |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2725 | /// \brief Look up the constructors for the given class. |
| 2726 | DeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) { |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 2727 | // If the implicit constructors have not yet been declared, do so now. |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 2728 | if (CanDeclareSpecialMemberFunction(Class)) { |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 2729 | if (Class->needsImplicitDefaultConstructor()) |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2730 | DeclareImplicitDefaultConstructor(Class); |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 2731 | if (Class->needsImplicitCopyConstructor()) |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2732 | DeclareImplicitCopyConstructor(Class); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2733 | if (getLangOpts().CPlusPlus11 && Class->needsImplicitMoveConstructor()) |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2734 | DeclareImplicitMoveConstructor(Class); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2735 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2736 | |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2737 | CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class)); |
| 2738 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T); |
| 2739 | return Class->lookup(Name); |
| 2740 | } |
| 2741 | |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 2742 | /// \brief Look up the copying assignment operator for the given class. |
| 2743 | CXXMethodDecl *Sema::LookupCopyingAssignment(CXXRecordDecl *Class, |
| 2744 | unsigned Quals, bool RValueThis, |
Richard Smith | 83c478d | 2012-04-20 18:46:14 +0000 | [diff] [blame] | 2745 | unsigned ThisQuals) { |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 2746 | assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
| 2747 | "non-const, non-volatile qualifiers for copy assignment arg"); |
| 2748 | assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
| 2749 | "non-const, non-volatile qualifiers for copy assignment this"); |
| 2750 | SpecialMemberOverloadResult *Result = |
| 2751 | LookupSpecialMember(Class, CXXCopyAssignment, Quals & Qualifiers::Const, |
| 2752 | Quals & Qualifiers::Volatile, RValueThis, |
| 2753 | ThisQuals & Qualifiers::Const, |
| 2754 | ThisQuals & Qualifiers::Volatile); |
| 2755 | |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 2756 | return Result->getMethod(); |
| 2757 | } |
| 2758 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2759 | /// \brief Look up the moving assignment operator for the given class. |
| 2760 | CXXMethodDecl *Sema::LookupMovingAssignment(CXXRecordDecl *Class, |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 2761 | unsigned Quals, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2762 | bool RValueThis, |
| 2763 | unsigned ThisQuals) { |
| 2764 | assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && |
| 2765 | "non-const, non-volatile qualifiers for copy assignment this"); |
| 2766 | SpecialMemberOverloadResult *Result = |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 2767 | LookupSpecialMember(Class, CXXMoveAssignment, Quals & Qualifiers::Const, |
| 2768 | Quals & Qualifiers::Volatile, RValueThis, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2769 | ThisQuals & Qualifiers::Const, |
| 2770 | ThisQuals & Qualifiers::Volatile); |
| 2771 | |
| 2772 | return Result->getMethod(); |
| 2773 | } |
| 2774 | |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2775 | /// \brief Look for the destructor of the given class. |
| 2776 | /// |
Alexis Hunt | 967ea7c | 2011-06-03 21:10:40 +0000 | [diff] [blame] | 2777 | /// During semantic analysis, this routine should be used in lieu of |
| 2778 | /// CXXRecordDecl::getDestructor(). |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2779 | /// |
| 2780 | /// \returns The destructor for this class. |
| 2781 | CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) { |
Alexis Hunt | 4ac55e3 | 2011-06-04 04:32:43 +0000 | [diff] [blame] | 2782 | return cast<CXXDestructorDecl>(LookupSpecialMember(Class, CXXDestructor, |
| 2783 | false, false, false, |
| 2784 | false, false)->getMethod()); |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2787 | /// LookupLiteralOperator - Determine which literal operator should be used for |
| 2788 | /// a user-defined literal, per C++11 [lex.ext]. |
| 2789 | /// |
| 2790 | /// Normal overload resolution is not used to select which literal operator to |
| 2791 | /// call for a user-defined literal. Look up the provided literal operator name, |
| 2792 | /// and filter the results to the appropriate set for the given argument types. |
| 2793 | Sema::LiteralOperatorLookupResult |
| 2794 | Sema::LookupLiteralOperator(Scope *S, LookupResult &R, |
| 2795 | ArrayRef<QualType> ArgTys, |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2796 | bool AllowRaw, bool AllowTemplate, |
| 2797 | bool AllowStringTemplate) { |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2798 | LookupName(R, S); |
| 2799 | assert(R.getResultKind() != LookupResult::Ambiguous && |
| 2800 | "literal operator lookup can't be ambiguous"); |
| 2801 | |
| 2802 | // Filter the lookup results appropriately. |
| 2803 | LookupResult::Filter F = R.makeFilter(); |
| 2804 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2805 | bool FoundRaw = false; |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2806 | bool FoundTemplate = false; |
| 2807 | bool FoundStringTemplate = false; |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2808 | bool FoundExactMatch = false; |
| 2809 | |
| 2810 | while (F.hasNext()) { |
| 2811 | Decl *D = F.next(); |
| 2812 | if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) |
| 2813 | D = USD->getTargetDecl(); |
| 2814 | |
Douglas Gregor | c197057 | 2013-04-10 05:18:00 +0000 | [diff] [blame] | 2815 | // If the declaration we found is invalid, skip it. |
| 2816 | if (D->isInvalidDecl()) { |
| 2817 | F.erase(); |
| 2818 | continue; |
| 2819 | } |
| 2820 | |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2821 | bool IsRaw = false; |
| 2822 | bool IsTemplate = false; |
| 2823 | bool IsStringTemplate = false; |
| 2824 | bool IsExactMatch = false; |
| 2825 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2826 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 2827 | if (FD->getNumParams() == 1 && |
| 2828 | FD->getParamDecl(0)->getType()->getAs<PointerType>()) |
| 2829 | IsRaw = true; |
Richard Smith | 550de45 | 2013-01-15 07:12:59 +0000 | [diff] [blame] | 2830 | else if (FD->getNumParams() == ArgTys.size()) { |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2831 | IsExactMatch = true; |
| 2832 | for (unsigned ArgIdx = 0; ArgIdx != ArgTys.size(); ++ArgIdx) { |
| 2833 | QualType ParamTy = FD->getParamDecl(ArgIdx)->getType(); |
| 2834 | if (!Context.hasSameUnqualifiedType(ArgTys[ArgIdx], ParamTy)) { |
| 2835 | IsExactMatch = false; |
| 2836 | break; |
| 2837 | } |
| 2838 | } |
| 2839 | } |
| 2840 | } |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2841 | if (FunctionTemplateDecl *FD = dyn_cast<FunctionTemplateDecl>(D)) { |
| 2842 | TemplateParameterList *Params = FD->getTemplateParameters(); |
| 2843 | if (Params->size() == 1) |
| 2844 | IsTemplate = true; |
| 2845 | else |
| 2846 | IsStringTemplate = true; |
| 2847 | } |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2848 | |
| 2849 | if (IsExactMatch) { |
| 2850 | FoundExactMatch = true; |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2851 | AllowRaw = false; |
| 2852 | AllowTemplate = false; |
| 2853 | AllowStringTemplate = false; |
| 2854 | if (FoundRaw || FoundTemplate || FoundStringTemplate) { |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2855 | // Go through again and remove the raw and template decls we've |
| 2856 | // already found. |
| 2857 | F.restart(); |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2858 | FoundRaw = FoundTemplate = FoundStringTemplate = false; |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2859 | } |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2860 | } else if (AllowRaw && IsRaw) { |
| 2861 | FoundRaw = true; |
| 2862 | } else if (AllowTemplate && IsTemplate) { |
| 2863 | FoundTemplate = true; |
| 2864 | } else if (AllowStringTemplate && IsStringTemplate) { |
| 2865 | FoundStringTemplate = true; |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2866 | } else { |
| 2867 | F.erase(); |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | F.done(); |
| 2872 | |
| 2873 | // C++11 [lex.ext]p3, p4: If S contains a literal operator with a matching |
| 2874 | // parameter type, that is used in preference to a raw literal operator |
| 2875 | // or literal operator template. |
| 2876 | if (FoundExactMatch) |
| 2877 | return LOLR_Cooked; |
| 2878 | |
| 2879 | // C++11 [lex.ext]p3, p4: S shall contain a raw literal operator or a literal |
| 2880 | // operator template, but not both. |
| 2881 | if (FoundRaw && FoundTemplate) { |
| 2882 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2883 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 2884 | NoteOverloadCandidate((*I)->getUnderlyingDecl()->getAsFunction()); |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2885 | return LOLR_Error; |
| 2886 | } |
| 2887 | |
| 2888 | if (FoundRaw) |
| 2889 | return LOLR_Raw; |
| 2890 | |
| 2891 | if (FoundTemplate) |
| 2892 | return LOLR_Template; |
| 2893 | |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2894 | if (FoundStringTemplate) |
| 2895 | return LOLR_StringTemplate; |
| 2896 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2897 | // Didn't find anything we could use. |
| 2898 | Diag(R.getNameLoc(), diag::err_ovl_no_viable_literal_operator) |
| 2899 | << R.getLookupName() << (int)ArgTys.size() << ArgTys[0] |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 2900 | << (ArgTys.size() == 2 ? ArgTys[1] : QualType()) << AllowRaw |
| 2901 | << (AllowTemplate || AllowStringTemplate); |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2902 | return LOLR_Error; |
| 2903 | } |
| 2904 | |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2905 | void ADLResult::insert(NamedDecl *New) { |
| 2906 | NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; |
| 2907 | |
| 2908 | // If we haven't yet seen a decl for this key, or the last decl |
| 2909 | // was exactly this one, we're done. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2910 | if (Old == nullptr || Old == New) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2911 | Old = New; |
| 2912 | return; |
| 2913 | } |
| 2914 | |
| 2915 | // Otherwise, decide which is a more recent redeclaration. |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2916 | FunctionDecl *OldFD = Old->getAsFunction(); |
| 2917 | FunctionDecl *NewFD = New->getAsFunction(); |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2918 | |
| 2919 | FunctionDecl *Cursor = NewFD; |
| 2920 | while (true) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2921 | Cursor = Cursor->getPreviousDecl(); |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2922 | |
| 2923 | // If we got to the end without finding OldFD, OldFD is the newer |
| 2924 | // declaration; leave things as they are. |
| 2925 | if (!Cursor) return; |
| 2926 | |
| 2927 | // If we do find OldFD, then NewFD is newer. |
| 2928 | if (Cursor == OldFD) break; |
| 2929 | |
| 2930 | // Otherwise, keep looking. |
| 2931 | } |
| 2932 | |
| 2933 | Old = New; |
| 2934 | } |
| 2935 | |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 2936 | void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc, |
| 2937 | ArrayRef<Expr *> Args, ADLResult &Result) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2938 | // Find all of the associated namespaces and classes based on the |
| 2939 | // arguments we have. |
| 2940 | AssociatedNamespaceSet AssociatedNamespaces; |
| 2941 | AssociatedClassSet AssociatedClasses; |
John McCall | 7d8b041 | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 2942 | FindAssociatedClassesAndNamespaces(Loc, Args, |
John McCall | c7e8e79 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 2943 | AssociatedNamespaces, |
| 2944 | AssociatedClasses); |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2945 | |
| 2946 | // C++ [basic.lookup.argdep]p3: |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2947 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 2948 | // and let Y be the lookup set produced by argument dependent |
| 2949 | // lookup (defined as follows). If X contains [...] then Y is |
| 2950 | // empty. Otherwise Y is the set of declarations found in the |
| 2951 | // namespaces associated with the argument types as described |
| 2952 | // below. The set of declarations found by the lookup of the name |
| 2953 | // is the union of X and Y. |
| 2954 | // |
| 2955 | // Here, we compute Y and add its members to the overloaded |
| 2956 | // candidate set. |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2957 | for (auto *NS : AssociatedNamespaces) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2958 | // When considering an associated namespace, the lookup is the |
| 2959 | // same as the lookup performed when the associated namespace is |
| 2960 | // used as a qualifier (3.4.3.2) except that: |
| 2961 | // |
| 2962 | // -- Any using-directives in the associated namespace are |
| 2963 | // ignored. |
| 2964 | // |
John McCall | c7e8e79 | 2009-08-07 22:18:02 +0000 | [diff] [blame] | 2965 | // -- Any namespace-scope friend functions declared in |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 2966 | // associated classes are visible within their respective |
| 2967 | // namespaces even if they are not visible during an ordinary |
| 2968 | // lookup (11.4). |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 2969 | DeclContext::lookup_result R = NS->lookup(Name); |
| 2970 | for (auto *D : R) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2971 | // If the only declaration here is an ordinary friend, consider |
| 2972 | // it only if it was declared in an associated classes. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2973 | if ((D->getIdentifierNamespace() & Decl::IDNS_Ordinary) == 0) { |
| 2974 | // If it's neither ordinarily visible nor a friend, we can't find it. |
| 2975 | if ((D->getIdentifierNamespace() & Decl::IDNS_OrdinaryFriend) == 0) |
| 2976 | continue; |
| 2977 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2978 | bool DeclaredInAssociatedClass = false; |
| 2979 | for (Decl *DI = D; DI; DI = DI->getPreviousDecl()) { |
| 2980 | DeclContext *LexDC = DI->getLexicalDeclContext(); |
| 2981 | if (isa<CXXRecordDecl>(LexDC) && |
| 2982 | AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) { |
| 2983 | DeclaredInAssociatedClass = true; |
| 2984 | break; |
| 2985 | } |
| 2986 | } |
| 2987 | if (!DeclaredInAssociatedClass) |
John McCall | d1e9d83 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 2988 | continue; |
| 2989 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 2991 | if (isa<UsingShadowDecl>(D)) |
| 2992 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2993 | |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 2994 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 2995 | continue; |
| 2996 | |
| 2997 | Result.insert(D); |
Douglas Gregor | 6127ca4 | 2009-06-23 20:14:09 +0000 | [diff] [blame] | 2998 | } |
| 2999 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3000 | } |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3001 | |
| 3002 | //---------------------------------------------------------------------------- |
| 3003 | // Search for all visible declarations. |
| 3004 | //---------------------------------------------------------------------------- |
| 3005 | VisibleDeclConsumer::~VisibleDeclConsumer() { } |
| 3006 | |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3007 | bool VisibleDeclConsumer::includeHiddenDecls() const { return false; } |
| 3008 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3009 | namespace { |
| 3010 | |
| 3011 | class ShadowContextRAII; |
| 3012 | |
| 3013 | class VisibleDeclsRecord { |
| 3014 | public: |
| 3015 | /// \brief An entry in the shadow map, which is optimized to store a |
| 3016 | /// single declaration (the common case) but can also store a list |
| 3017 | /// of declarations. |
Chris Lattner | 83cfc7c | 2011-07-18 01:54:02 +0000 | [diff] [blame] | 3018 | typedef llvm::TinyPtrVector<NamedDecl*> ShadowMapEntry; |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3019 | |
| 3020 | private: |
| 3021 | /// \brief A mapping from declaration names to the declarations that have |
| 3022 | /// this name within a particular scope. |
| 3023 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
| 3024 | |
| 3025 | /// \brief A list of shadow maps, which is used to model name hiding. |
| 3026 | std::list<ShadowMap> ShadowMaps; |
| 3027 | |
| 3028 | /// \brief The declaration contexts we have already visited. |
| 3029 | llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; |
| 3030 | |
| 3031 | friend class ShadowContextRAII; |
| 3032 | |
| 3033 | public: |
| 3034 | /// \brief Determine whether we have already visited this context |
| 3035 | /// (and, if not, note that we are going to visit that context now). |
| 3036 | bool visitedContext(DeclContext *Ctx) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3037 | return !VisitedContexts.insert(Ctx).second; |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3040 | bool alreadyVisitedContext(DeclContext *Ctx) { |
| 3041 | return VisitedContexts.count(Ctx); |
| 3042 | } |
| 3043 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3044 | /// \brief Determine whether the given declaration is hidden in the |
| 3045 | /// current scope. |
| 3046 | /// |
| 3047 | /// \returns the declaration that hides the given declaration, or |
| 3048 | /// NULL if no such declaration exists. |
| 3049 | NamedDecl *checkHidden(NamedDecl *ND); |
| 3050 | |
| 3051 | /// \brief Add a declaration to the current shadow map. |
Chris Lattner | 83cfc7c | 2011-07-18 01:54:02 +0000 | [diff] [blame] | 3052 | void add(NamedDecl *ND) { |
| 3053 | ShadowMaps.back()[ND->getDeclName()].push_back(ND); |
| 3054 | } |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3055 | }; |
| 3056 | |
| 3057 | /// \brief RAII object that records when we've entered a shadow context. |
| 3058 | class ShadowContextRAII { |
| 3059 | VisibleDeclsRecord &Visible; |
| 3060 | |
| 3061 | typedef VisibleDeclsRecord::ShadowMap ShadowMap; |
| 3062 | |
| 3063 | public: |
| 3064 | ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame^] | 3065 | Visible.ShadowMaps.emplace_back(); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | ~ShadowContextRAII() { |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3069 | Visible.ShadowMaps.pop_back(); |
| 3070 | } |
| 3071 | }; |
| 3072 | |
| 3073 | } // end anonymous namespace |
| 3074 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3075 | NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { |
Douglas Gregor | 0235c42 | 2010-01-14 00:06:47 +0000 | [diff] [blame] | 3076 | // Look through using declarations. |
| 3077 | ND = ND->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3078 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3079 | unsigned IDNS = ND->getIdentifierNamespace(); |
| 3080 | std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); |
| 3081 | for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); |
| 3082 | SM != SMEnd; ++SM) { |
| 3083 | ShadowMap::iterator Pos = SM->find(ND->getDeclName()); |
| 3084 | if (Pos == SM->end()) |
| 3085 | continue; |
| 3086 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 3087 | for (auto *D : Pos->second) { |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3088 | // A tag declaration does not hide a non-tag declaration. |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 3089 | if (D->hasTagIdentifierNamespace() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3090 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3091 | Decl::IDNS_ObjCProtocol))) |
| 3092 | continue; |
| 3093 | |
| 3094 | // Protocols are in distinct namespaces from everything else. |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 3095 | if (((D->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3096 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 3097 | D->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3098 | continue; |
| 3099 | |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3100 | // Functions and function templates in the same scope overload |
| 3101 | // rather than hide. FIXME: Look for hiding based on function |
| 3102 | // signatures! |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 3103 | if (D->getUnderlyingDecl()->isFunctionOrFunctionTemplate() && |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 3104 | ND->getUnderlyingDecl()->isFunctionOrFunctionTemplate() && |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3105 | SM == ShadowMaps.rbegin()) |
Douglas Gregor | 200c99d | 2010-01-14 03:35:48 +0000 | [diff] [blame] | 3106 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3108 | // We've found a declaration that hides this one. |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 3109 | return D; |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3113 | return nullptr; |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | static void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result, |
| 3117 | bool QualifiedNameLookup, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3118 | bool InBaseClass, |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3119 | VisibleDeclConsumer &Consumer, |
| 3120 | VisibleDeclsRecord &Visited) { |
Douglas Gregor | 0c8a172 | 2010-02-04 23:42:48 +0000 | [diff] [blame] | 3121 | if (!Ctx) |
| 3122 | return; |
| 3123 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3124 | // Make sure we don't visit the same context twice. |
| 3125 | if (Visited.visitedContext(Ctx->getPrimaryContext())) |
| 3126 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3127 | |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 3128 | // Outside C++, lookup results for the TU live on identifiers. |
| 3129 | if (isa<TranslationUnitDecl>(Ctx) && |
| 3130 | !Result.getSema().getLangOpts().CPlusPlus) { |
| 3131 | auto &S = Result.getSema(); |
| 3132 | auto &Idents = S.Context.Idents; |
| 3133 | |
| 3134 | // Ensure all external identifiers are in the identifier table. |
| 3135 | if (IdentifierInfoLookup *External = Idents.getExternalIdentifierLookup()) { |
| 3136 | std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers()); |
| 3137 | for (StringRef Name = Iter->Next(); !Name.empty(); Name = Iter->Next()) |
| 3138 | Idents.get(Name); |
| 3139 | } |
| 3140 | |
| 3141 | // Walk all lookup results in the TU for each identifier. |
| 3142 | for (const auto &Ident : Idents) { |
| 3143 | for (auto I = S.IdResolver.begin(Ident.getValue()), |
| 3144 | E = S.IdResolver.end(); |
| 3145 | I != E; ++I) { |
| 3146 | if (S.IdResolver.isDeclInScope(*I, Ctx)) { |
| 3147 | if (NamedDecl *ND = Result.getAcceptableDecl(*I)) { |
| 3148 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); |
| 3149 | Visited.add(ND); |
| 3150 | } |
| 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | return; |
| 3156 | } |
| 3157 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 3158 | if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) |
| 3159 | Result.getSema().ForceDeclarationOfImplicitMembers(Class); |
| 3160 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3161 | // Enumerate all of the results in this context. |
Richard Trieu | 20abd6b | 2015-04-15 03:48:48 +0000 | [diff] [blame] | 3162 | for (DeclContextLookupResult R : Ctx->lookups()) { |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 3163 | for (auto *D : R) { |
| 3164 | if (auto *ND = Result.getAcceptableDecl(D)) { |
| 3165 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); |
| 3166 | Visited.add(ND); |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 3167 | } |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | // Traverse using directives for qualified name lookup. |
| 3172 | if (QualifiedNameLookup) { |
| 3173 | ShadowContextRAII Shadow(Visited); |
Aaron Ballman | 804a7fb | 2014-03-17 17:14:12 +0000 | [diff] [blame] | 3174 | for (auto I : Ctx->using_directives()) { |
Aaron Ballman | 63ab760 | 2014-03-07 13:44:44 +0000 | [diff] [blame] | 3175 | LookupVisibleDecls(I->getNominatedNamespace(), Result, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3176 | QualifiedNameLookup, InBaseClass, Consumer, Visited); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3180 | // Traverse the contexts of inherited C++ classes. |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3181 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3182 | if (!Record->hasDefinition()) |
| 3183 | return; |
| 3184 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3185 | for (const auto &B : Record->bases()) { |
| 3186 | QualType BaseType = B.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3187 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3188 | // Don't look into dependent bases, because name lookup can't look |
| 3189 | // there anyway. |
| 3190 | if (BaseType->isDependentType()) |
| 3191 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3192 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3193 | const RecordType *Record = BaseType->getAs<RecordType>(); |
| 3194 | if (!Record) |
| 3195 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3196 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3197 | // FIXME: It would be nice to be able to determine whether referencing |
| 3198 | // a particular member would be ambiguous. For example, given |
| 3199 | // |
| 3200 | // struct A { int member; }; |
| 3201 | // struct B { int member; }; |
| 3202 | // struct C : A, B { }; |
| 3203 | // |
| 3204 | // void f(C *c) { c->### } |
| 3205 | // |
| 3206 | // accessing 'member' would result in an ambiguity. However, we |
| 3207 | // could be smart enough to qualify the member with the base |
| 3208 | // class, e.g., |
| 3209 | // |
| 3210 | // c->B::member |
| 3211 | // |
| 3212 | // or |
| 3213 | // |
| 3214 | // c->A::member |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3215 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3216 | // Find results in this base class (and its bases). |
| 3217 | ShadowContextRAII Shadow(Visited); |
| 3218 | LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3219 | true, Consumer, Visited); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3220 | } |
| 3221 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3222 | |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3223 | // Traverse the contexts of Objective-C classes. |
| 3224 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { |
| 3225 | // Traverse categories. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 3226 | for (auto *Cat : IFace->visible_categories()) { |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3227 | ShadowContextRAII Shadow(Visited); |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 3228 | LookupVisibleDecls(Cat, Result, QualifiedNameLookup, false, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3229 | Consumer, Visited); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | // Traverse protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3233 | for (auto *I : IFace->all_referenced_protocols()) { |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3234 | ShadowContextRAII Shadow(Visited); |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3235 | LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3236 | Visited); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | // Traverse the superclass. |
| 3240 | if (IFace->getSuperClass()) { |
| 3241 | ShadowContextRAII Shadow(Visited); |
| 3242 | LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3243 | true, Consumer, Visited); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3244 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3245 | |
Douglas Gregor | 0b59e80 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 3246 | // If there is an implementation, traverse it. We do this to find |
| 3247 | // synthesized ivars. |
| 3248 | if (IFace->getImplementation()) { |
| 3249 | ShadowContextRAII Shadow(Visited); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3250 | LookupVisibleDecls(IFace->getImplementation(), Result, |
Nick Lewycky | 13668f2 | 2012-04-03 20:26:45 +0000 | [diff] [blame] | 3251 | QualifiedNameLookup, InBaseClass, Consumer, Visited); |
Douglas Gregor | 0b59e80 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 3252 | } |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3253 | } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3254 | for (auto *I : Protocol->protocols()) { |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3255 | ShadowContextRAII Shadow(Visited); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3256 | LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3257 | Visited); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3258 | } |
| 3259 | } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3260 | for (auto *I : Category->protocols()) { |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3261 | ShadowContextRAII Shadow(Visited); |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3262 | LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3263 | Visited); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3264 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3265 | |
Douglas Gregor | 0b59e80 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 3266 | // If there is an implementation, traverse it. |
| 3267 | if (Category->getImplementation()) { |
| 3268 | ShadowContextRAII Shadow(Visited); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3269 | LookupVisibleDecls(Category->getImplementation(), Result, |
Douglas Gregor | 0b59e80 | 2010-04-19 18:02:19 +0000 | [diff] [blame] | 3270 | QualifiedNameLookup, true, Consumer, Visited); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3271 | } |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3272 | } |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | static void LookupVisibleDecls(Scope *S, LookupResult &Result, |
| 3276 | UnqualUsingDirectiveSet &UDirs, |
| 3277 | VisibleDeclConsumer &Consumer, |
| 3278 | VisibleDeclsRecord &Visited) { |
| 3279 | if (!S) |
| 3280 | return; |
| 3281 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3282 | if (!S->getEntity() || |
| 3283 | (!S->getParent() && |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 3284 | !Visited.alreadyVisitedContext(S->getEntity())) || |
| 3285 | (S->getEntity())->isFunctionOrMethod()) { |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3286 | FindLocalExternScope FindLocals(Result); |
Douglas Gregor | 712dcfe | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 3287 | // Walk through the declarations in this Scope. |
Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 3288 | for (auto *D : S->decls()) { |
| 3289 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 4a81456 | 2011-12-14 16:03:29 +0000 | [diff] [blame] | 3290 | if ((ND = Result.getAcceptableDecl(ND))) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3291 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), nullptr, false); |
Douglas Gregor | 712dcfe | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 3292 | Visited.add(ND); |
| 3293 | } |
| 3294 | } |
| 3295 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 3297 | // FIXME: C++ [temp.local]p8 |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3298 | DeclContext *Entity = nullptr; |
Douglas Gregor | 4f24863 | 2010-01-01 17:44:25 +0000 | [diff] [blame] | 3299 | if (S->getEntity()) { |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3300 | // Look into this scope's declaration context, along with any of its |
| 3301 | // parent lookup contexts (e.g., enclosing classes), up to the point |
| 3302 | // where we hit the context stored in the next outer scope. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 3303 | Entity = S->getEntity(); |
Douglas Gregor | 6623006 | 2010-03-15 14:33:29 +0000 | [diff] [blame] | 3304 | DeclContext *OuterCtx = findOuterContext(S).first; // FIXME |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3305 | |
Douglas Gregor | ea16606 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 3306 | for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3307 | Ctx = Ctx->getLookupParent()) { |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3308 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { |
| 3309 | if (Method->isInstanceMethod()) { |
| 3310 | // For instance methods, look for ivars in the method's interface. |
| 3311 | LookupResult IvarResult(Result.getSema(), Result.getLookupName(), |
| 3312 | Result.getNameLoc(), Sema::LookupMemberName); |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 3313 | if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3314 | LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false, |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3315 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 3316 | } |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | // We've already performed all of the name lookup that we need |
| 3320 | // to for Objective-C methods; the next context will be the |
| 3321 | // outer scope. |
| 3322 | break; |
| 3323 | } |
| 3324 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3325 | if (Ctx->isFunctionOrMethod()) |
| 3326 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3327 | |
| 3328 | LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3329 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3330 | } |
| 3331 | } else if (!S->getParent()) { |
| 3332 | // Look into the translation unit scope. We walk through the translation |
| 3333 | // unit's declaration context, because the Scope itself won't have all of |
| 3334 | // the declarations if we loaded a precompiled header. |
| 3335 | // FIXME: We would like the translation unit's Scope object to point to the |
| 3336 | // translation unit, so we don't need this special "if" branch. However, |
| 3337 | // doing so would force the normal C++ name-lookup code to look into the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3338 | // translation unit decl when the IdentifierInfo chains would suffice. |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3339 | // Once we fix that problem (which is part of a more general "don't look |
Douglas Gregor | 712dcfe | 2010-01-07 00:31:29 +0000 | [diff] [blame] | 3340 | // in DeclContexts unless we have to" optimization), we can eliminate this. |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3341 | Entity = Result.getSema().Context.getTranslationUnitDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3342 | LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3343 | /*InBaseClass=*/false, Consumer, Visited); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3346 | if (Entity) { |
| 3347 | // Lookup visible declarations in any namespaces found by using |
| 3348 | // directives. |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 3349 | for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(Entity)) |
| 3350 | LookupVisibleDecls(const_cast<DeclContext *>(UUE.getNominatedNamespace()), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3351 | Result, /*QualifiedNameLookup=*/false, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3352 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | // Lookup names in the parent scope. |
| 3356 | ShadowContextRAII Shadow(Visited); |
| 3357 | LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited); |
| 3358 | } |
| 3359 | |
| 3360 | void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3361 | VisibleDeclConsumer &Consumer, |
| 3362 | bool IncludeGlobalScope) { |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3363 | // Determine the set of using directives available during |
| 3364 | // unqualified name lookup. |
| 3365 | Scope *Initial = S; |
| 3366 | UnqualUsingDirectiveSet UDirs; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3367 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3368 | // Find the first namespace or translation-unit scope. |
| 3369 | while (S && !isNamespaceOrTranslationUnitScope(S)) |
| 3370 | S = S->getParent(); |
| 3371 | |
| 3372 | UDirs.visitScopeChain(Initial, S); |
| 3373 | } |
| 3374 | UDirs.done(); |
| 3375 | |
| 3376 | // Look for visible declarations. |
| 3377 | LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3378 | Result.setAllowHidden(Consumer.includeHiddenDecls()); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3379 | VisibleDeclsRecord Visited; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3380 | if (!IncludeGlobalScope) |
| 3381 | Visited.visitedContext(Context.getTranslationUnitDecl()); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3382 | ShadowContextRAII Shadow(Visited); |
| 3383 | ::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited); |
| 3384 | } |
| 3385 | |
| 3386 | void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3387 | VisibleDeclConsumer &Consumer, |
| 3388 | bool IncludeGlobalScope) { |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3389 | LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3390 | Result.setAllowHidden(Consumer.includeHiddenDecls()); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3391 | VisibleDeclsRecord Visited; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3392 | if (!IncludeGlobalScope) |
| 3393 | Visited.visitedContext(Context.getTranslationUnitDecl()); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3394 | ShadowContextRAII Shadow(Visited); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3395 | ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3396 | /*InBaseClass=*/false, Consumer, Visited); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 3399 | /// LookupOrCreateLabel - Do a name lookup of a label with the specified name. |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3400 | /// If GnuLabelLoc is a valid source location, then this is a definition |
| 3401 | /// of an __label__ label name, otherwise it is a normal label definition |
| 3402 | /// or use. |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 3403 | LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc, |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3404 | SourceLocation GnuLabelLoc) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 3405 | // Do a lookup to see if we have a label with this name already. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3406 | NamedDecl *Res = nullptr; |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3407 | |
| 3408 | if (GnuLabelLoc.isValid()) { |
| 3409 | // Local label definitions always shadow existing labels. |
| 3410 | Res = LabelDecl::Create(Context, CurContext, Loc, II, GnuLabelLoc); |
| 3411 | Scope *S = CurScope; |
| 3412 | PushOnScopeChains(Res, S, true); |
| 3413 | return cast<LabelDecl>(Res); |
| 3414 | } |
| 3415 | |
| 3416 | // Not a GNU local label. |
| 3417 | Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration); |
| 3418 | // If we found a label, check to see if it is in the same context as us. |
| 3419 | // When in a Block, we don't want to reuse a label in an enclosing function. |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 3420 | if (Res && Res->getDeclContext() != CurContext) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3421 | Res = nullptr; |
| 3422 | if (!Res) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 3423 | // If not forward referenced or defined already, create the backing decl. |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3424 | Res = LabelDecl::Create(Context, CurContext, Loc, II); |
| 3425 | Scope *S = CurScope->getFnParent(); |
Chris Lattner | 9ba479b | 2011-02-18 21:16:39 +0000 | [diff] [blame] | 3426 | assert(S && "Not in a function?"); |
| 3427 | PushOnScopeChains(Res, S, true); |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 3428 | } |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 3429 | return cast<LabelDecl>(Res); |
| 3430 | } |
| 3431 | |
| 3432 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3433 | // Typo correction |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 3434 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3435 | |
Kaelyn Takata | 0fc7519 | 2014-06-11 18:06:56 +0000 | [diff] [blame] | 3436 | static bool isCandidateViable(CorrectionCandidateCallback &CCC, |
| 3437 | TypoCorrection &Candidate) { |
| 3438 | Candidate.setCallbackDistance(CCC.RankCandidate(Candidate)); |
| 3439 | return Candidate.getEditDistance(false) != TypoCorrection::InvalidDistance; |
| 3440 | } |
| 3441 | |
| 3442 | static void LookupPotentialTypoResult(Sema &SemaRef, |
| 3443 | LookupResult &Res, |
| 3444 | IdentifierInfo *Name, |
| 3445 | Scope *S, CXXScopeSpec *SS, |
| 3446 | DeclContext *MemberContext, |
| 3447 | bool EnteringContext, |
| 3448 | bool isObjCIvarLookup, |
| 3449 | bool FindHidden); |
| 3450 | |
Kaelyn Takata | 9ab7fb6 | 2014-10-27 18:07:40 +0000 | [diff] [blame] | 3451 | /// \brief Check whether the declarations found for a typo correction are |
| 3452 | /// visible, and if none of them are, convert the correction to an 'import |
| 3453 | /// a module' correction. |
| 3454 | static void checkCorrectionVisibility(Sema &SemaRef, TypoCorrection &TC) { |
| 3455 | if (TC.begin() == TC.end()) |
| 3456 | return; |
| 3457 | |
| 3458 | TypoCorrection::decl_iterator DI = TC.begin(), DE = TC.end(); |
| 3459 | |
| 3460 | for (/**/; DI != DE; ++DI) |
| 3461 | if (!LookupResult::isVisible(SemaRef, *DI)) |
| 3462 | break; |
| 3463 | // Nothing to do if all decls are visible. |
| 3464 | if (DI == DE) |
| 3465 | return; |
| 3466 | |
| 3467 | llvm::SmallVector<NamedDecl*, 4> NewDecls(TC.begin(), DI); |
| 3468 | bool AnyVisibleDecls = !NewDecls.empty(); |
| 3469 | |
| 3470 | for (/**/; DI != DE; ++DI) { |
| 3471 | NamedDecl *VisibleDecl = *DI; |
| 3472 | if (!LookupResult::isVisible(SemaRef, *DI)) |
| 3473 | VisibleDecl = findAcceptableDecl(SemaRef, *DI); |
| 3474 | |
| 3475 | if (VisibleDecl) { |
| 3476 | if (!AnyVisibleDecls) { |
| 3477 | // Found a visible decl, discard all hidden ones. |
| 3478 | AnyVisibleDecls = true; |
| 3479 | NewDecls.clear(); |
| 3480 | } |
| 3481 | NewDecls.push_back(VisibleDecl); |
| 3482 | } else if (!AnyVisibleDecls && !(*DI)->isModulePrivate()) |
| 3483 | NewDecls.push_back(*DI); |
| 3484 | } |
| 3485 | |
| 3486 | if (NewDecls.empty()) |
| 3487 | TC = TypoCorrection(); |
| 3488 | else { |
| 3489 | TC.setCorrectionDecls(NewDecls); |
| 3490 | TC.setRequiresImport(!AnyVisibleDecls); |
| 3491 | } |
| 3492 | } |
| 3493 | |
Kaelyn Takata | 0fc7519 | 2014-06-11 18:06:56 +0000 | [diff] [blame] | 3494 | // Fill the supplied vector with the IdentifierInfo pointers for each piece of |
| 3495 | // the given NestedNameSpecifier (i.e. given a NestedNameSpecifier "foo::bar::", |
| 3496 | // fill the vector with the IdentifierInfo pointers for "foo" and "bar"). |
| 3497 | static void getNestedNameSpecifierIdentifiers( |
| 3498 | NestedNameSpecifier *NNS, |
| 3499 | SmallVectorImpl<const IdentifierInfo*> &Identifiers) { |
| 3500 | if (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 3501 | getNestedNameSpecifierIdentifiers(Prefix, Identifiers); |
| 3502 | else |
| 3503 | Identifiers.clear(); |
| 3504 | |
| 3505 | const IdentifierInfo *II = nullptr; |
| 3506 | |
| 3507 | switch (NNS->getKind()) { |
| 3508 | case NestedNameSpecifier::Identifier: |
| 3509 | II = NNS->getAsIdentifier(); |
| 3510 | break; |
| 3511 | |
| 3512 | case NestedNameSpecifier::Namespace: |
| 3513 | if (NNS->getAsNamespace()->isAnonymousNamespace()) |
| 3514 | return; |
| 3515 | II = NNS->getAsNamespace()->getIdentifier(); |
| 3516 | break; |
| 3517 | |
| 3518 | case NestedNameSpecifier::NamespaceAlias: |
| 3519 | II = NNS->getAsNamespaceAlias()->getIdentifier(); |
| 3520 | break; |
| 3521 | |
| 3522 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3523 | case NestedNameSpecifier::TypeSpec: |
| 3524 | II = QualType(NNS->getAsType(), 0).getBaseTypeIdentifier(); |
| 3525 | break; |
| 3526 | |
| 3527 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 3528 | case NestedNameSpecifier::Super: |
Kaelyn Takata | 0fc7519 | 2014-06-11 18:06:56 +0000 | [diff] [blame] | 3529 | return; |
| 3530 | } |
| 3531 | |
| 3532 | if (II) |
| 3533 | Identifiers.push_back(II); |
| 3534 | } |
| 3535 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3536 | void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 3537 | DeclContext *Ctx, bool InBaseClass) { |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3538 | // Don't consider hidden names for typo correction. |
| 3539 | if (Hiding) |
| 3540 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3541 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 3542 | // Only consider entities with identifiers for names, ignoring |
| 3543 | // special names (constructors, overloaded operators, selectors, |
| 3544 | // etc.). |
| 3545 | IdentifierInfo *Name = ND->getIdentifier(); |
| 3546 | if (!Name) |
| 3547 | return; |
| 3548 | |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3549 | // Only consider visible declarations and declarations from modules with |
| 3550 | // names that exactly match. |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3551 | if (!LookupResult::isVisible(SemaRef, ND) && Name != Typo && |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3552 | !findAcceptableDecl(SemaRef, ND)) |
| 3553 | return; |
| 3554 | |
Douglas Gregor | 57756ea | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 3555 | FoundName(Name->getName()); |
| 3556 | } |
| 3557 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3558 | void TypoCorrectionConsumer::FoundName(StringRef Name) { |
Kaelyn Uhrain | 653ff24 | 2013-10-02 18:26:35 +0000 | [diff] [blame] | 3559 | // Compute the edit distance between the typo and the name of this |
| 3560 | // entity, and add the identifier to the list of results. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3561 | addName(Name, nullptr); |
Kaelyn Uhrain | 653ff24 | 2013-10-02 18:26:35 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | void TypoCorrectionConsumer::addKeywordResult(StringRef Keyword) { |
| 3565 | // Compute the edit distance between the typo and this keyword, |
| 3566 | // and add the keyword to the list of results. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3567 | addName(Keyword, nullptr, nullptr, true); |
Kaelyn Uhrain | 653ff24 | 2013-10-02 18:26:35 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | void TypoCorrectionConsumer::addName(StringRef Name, NamedDecl *ND, |
| 3571 | NestedNameSpecifier *NNS, bool isKeyword) { |
Douglas Gregor | 93910a5 | 2010-10-19 19:39:10 +0000 | [diff] [blame] | 3572 | // Use a simple length-based heuristic to determine the minimum possible |
| 3573 | // edit distance. If the minimum isn't good enough, bail out early. |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3574 | StringRef TypoStr = Typo->getName(); |
| 3575 | unsigned MinED = abs((int)Name.size() - (int)TypoStr.size()); |
| 3576 | if (MinED && TypoStr.size() / MinED < 3) |
Douglas Gregor | 93910a5 | 2010-10-19 19:39:10 +0000 | [diff] [blame] | 3577 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3578 | |
Douglas Gregor | c1fb15e | 2010-10-19 22:14:33 +0000 | [diff] [blame] | 3579 | // Compute an upper bound on the allowable edit distance, so that the |
| 3580 | // edit-distance algorithm can short-circuit. |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3581 | unsigned UpperBound = (TypoStr.size() + 2) / 3 + 1; |
| 3582 | unsigned ED = TypoStr.edit_distance(Name, true, UpperBound); |
Kaelyn Uhrain | 653ff24 | 2013-10-02 18:26:35 +0000 | [diff] [blame] | 3583 | if (ED >= UpperBound) return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | |
Kaelyn Uhrain | 653ff24 | 2013-10-02 18:26:35 +0000 | [diff] [blame] | 3585 | TypoCorrection TC(&SemaRef.Context.Idents.get(Name), ND, NNS, ED); |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 3586 | if (isKeyword) TC.makeKeyword(); |
Kaelyn Takata | 0a31302 | 2014-11-20 22:06:26 +0000 | [diff] [blame] | 3587 | TC.setCorrectionRange(nullptr, Result.getLookupNameInfo()); |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 3588 | addCorrection(TC); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
Kaelyn Takata | d2287c3 | 2014-10-27 18:07:13 +0000 | [diff] [blame] | 3591 | static const unsigned MaxTypoDistanceResultSets = 5; |
| 3592 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3593 | void TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) { |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3594 | StringRef TypoStr = Typo->getName(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3595 | StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName(); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3596 | |
| 3597 | // For very short typos, ignore potential corrections that have a different |
| 3598 | // base identifier from the typo or which have a normalized edit distance |
| 3599 | // longer than the typo itself. |
| 3600 | if (TypoStr.size() < 3 && |
| 3601 | (Name != TypoStr || Correction.getEditDistance(true) > TypoStr.size())) |
| 3602 | return; |
| 3603 | |
| 3604 | // If the correction is resolved but is not viable, ignore it. |
Kaelyn Takata | 9ab7fb6 | 2014-10-27 18:07:40 +0000 | [diff] [blame] | 3605 | if (Correction.isResolved()) { |
| 3606 | checkCorrectionVisibility(SemaRef, Correction); |
| 3607 | if (!Correction || !isCandidateViable(*CorrectionValidator, Correction)) |
| 3608 | return; |
| 3609 | } |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3610 | |
Kaelyn Uhrain | ba896f1 | 2012-06-01 18:11:16 +0000 | [diff] [blame] | 3611 | TypoResultList &CList = |
| 3612 | CorrectionResults[Correction.getEditDistance(false)][Name]; |
Chandler Carruth | 7d85c9b | 2011-06-28 22:48:40 +0000 | [diff] [blame] | 3613 | |
Kaelyn Uhrain | ba896f1 | 2012-06-01 18:11:16 +0000 | [diff] [blame] | 3614 | if (!CList.empty() && !CList.back().isResolved()) |
| 3615 | CList.pop_back(); |
| 3616 | if (NamedDecl *NewND = Correction.getCorrectionDecl()) { |
| 3617 | std::string CorrectionStr = Correction.getAsString(SemaRef.getLangOpts()); |
| 3618 | for (TypoResultList::iterator RI = CList.begin(), RIEnd = CList.end(); |
| 3619 | RI != RIEnd; ++RI) { |
| 3620 | // If the Correction refers to a decl already in the result list, |
| 3621 | // replace the existing result if the string representation of Correction |
| 3622 | // comes before the current result alphabetically, then stop as there is |
| 3623 | // nothing more to be done to add Correction to the candidate set. |
| 3624 | if (RI->getCorrectionDecl() == NewND) { |
| 3625 | if (CorrectionStr < RI->getAsString(SemaRef.getLangOpts())) |
| 3626 | *RI = Correction; |
| 3627 | return; |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | if (CList.empty() || Correction.isResolved()) |
| 3632 | CList.push_back(Correction); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3633 | |
Kaelyn Uhrain | 34fab55 | 2012-05-31 23:32:58 +0000 | [diff] [blame] | 3634 | while (CorrectionResults.size() > MaxTypoDistanceResultSets) |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3635 | CorrectionResults.erase(std::prev(CorrectionResults.end())); |
| 3636 | } |
| 3637 | |
| 3638 | void TypoCorrectionConsumer::addNamespaces( |
| 3639 | const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces) { |
| 3640 | SearchNamespaces = true; |
| 3641 | |
| 3642 | for (auto KNPair : KnownNamespaces) |
| 3643 | Namespaces.addNameSpecifier(KNPair.first); |
| 3644 | |
| 3645 | bool SSIsTemplate = false; |
| 3646 | if (NestedNameSpecifier *NNS = |
| 3647 | (SS && SS->isValid()) ? SS->getScopeRep() : nullptr) { |
| 3648 | if (const Type *T = NNS->getAsType()) |
| 3649 | SSIsTemplate = T->getTypeClass() == Type::TemplateSpecialization; |
| 3650 | } |
| 3651 | for (const auto *TI : SemaRef.getASTContext().types()) { |
| 3652 | if (CXXRecordDecl *CD = TI->getAsCXXRecordDecl()) { |
| 3653 | CD = CD->getCanonicalDecl(); |
| 3654 | if (!CD->isDependentType() && !CD->isAnonymousStructOrUnion() && |
| 3655 | !CD->isUnion() && CD->getIdentifier() && |
| 3656 | (SSIsTemplate || !isa<ClassTemplateSpecializationDecl>(CD)) && |
| 3657 | (CD->isBeingDefined() || CD->isCompleteDefinition())) |
| 3658 | Namespaces.addNameSpecifier(CD); |
| 3659 | } |
| 3660 | } |
| 3661 | } |
| 3662 | |
Kaelyn Takata | 0d6a3ed | 2014-10-27 18:07:34 +0000 | [diff] [blame] | 3663 | const TypoCorrection &TypoCorrectionConsumer::getNextCorrection() { |
| 3664 | if (++CurrentTCIndex < ValidatedCorrections.size()) |
| 3665 | return ValidatedCorrections[CurrentTCIndex]; |
| 3666 | |
| 3667 | CurrentTCIndex = ValidatedCorrections.size(); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3668 | while (!CorrectionResults.empty()) { |
| 3669 | auto DI = CorrectionResults.begin(); |
| 3670 | if (DI->second.empty()) { |
| 3671 | CorrectionResults.erase(DI); |
| 3672 | continue; |
| 3673 | } |
| 3674 | |
| 3675 | auto RI = DI->second.begin(); |
| 3676 | if (RI->second.empty()) { |
| 3677 | DI->second.erase(RI); |
| 3678 | performQualifiedLookups(); |
| 3679 | continue; |
| 3680 | } |
| 3681 | |
| 3682 | TypoCorrection TC = RI->second.pop_back_val(); |
Kaelyn Takata | 9ab7fb6 | 2014-10-27 18:07:40 +0000 | [diff] [blame] | 3683 | if (TC.isResolved() || TC.requiresImport() || resolveCorrection(TC)) { |
Kaelyn Takata | 0d6a3ed | 2014-10-27 18:07:34 +0000 | [diff] [blame] | 3684 | ValidatedCorrections.push_back(TC); |
| 3685 | return ValidatedCorrections[CurrentTCIndex]; |
| 3686 | } |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3687 | } |
Kaelyn Takata | 0d6a3ed | 2014-10-27 18:07:34 +0000 | [diff] [blame] | 3688 | return ValidatedCorrections[0]; // The empty correction. |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | bool TypoCorrectionConsumer::resolveCorrection(TypoCorrection &Candidate) { |
| 3692 | IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo(); |
| 3693 | DeclContext *TempMemberContext = MemberContext; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 3694 | CXXScopeSpec *TempSS = SS.get(); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3695 | retry_lookup: |
| 3696 | LookupPotentialTypoResult(SemaRef, Result, Name, S, TempSS, TempMemberContext, |
| 3697 | EnteringContext, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 3698 | CorrectionValidator->IsObjCIvarLookup, |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3699 | Name == Typo && !Candidate.WillReplaceSpecifier()); |
| 3700 | switch (Result.getResultKind()) { |
| 3701 | case LookupResult::NotFound: |
| 3702 | case LookupResult::NotFoundInCurrentInstantiation: |
| 3703 | case LookupResult::FoundUnresolvedValue: |
| 3704 | if (TempSS) { |
| 3705 | // Immediately retry the lookup without the given CXXScopeSpec |
| 3706 | TempSS = nullptr; |
| 3707 | Candidate.WillReplaceSpecifier(true); |
| 3708 | goto retry_lookup; |
| 3709 | } |
| 3710 | if (TempMemberContext) { |
| 3711 | if (SS && !TempSS) |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 3712 | TempSS = SS.get(); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3713 | TempMemberContext = nullptr; |
| 3714 | goto retry_lookup; |
| 3715 | } |
| 3716 | if (SearchNamespaces) |
| 3717 | QualifiedResults.push_back(Candidate); |
| 3718 | break; |
| 3719 | |
| 3720 | case LookupResult::Ambiguous: |
| 3721 | // We don't deal with ambiguities. |
| 3722 | break; |
| 3723 | |
| 3724 | case LookupResult::Found: |
| 3725 | case LookupResult::FoundOverloaded: |
| 3726 | // Store all of the Decls for overloaded symbols |
| 3727 | for (auto *TRD : Result) |
| 3728 | Candidate.addCorrectionDecl(TRD); |
Kaelyn Takata | 9ab7fb6 | 2014-10-27 18:07:40 +0000 | [diff] [blame] | 3729 | checkCorrectionVisibility(SemaRef, Candidate); |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 3730 | if (!isCandidateViable(*CorrectionValidator, Candidate)) { |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3731 | if (SearchNamespaces) |
| 3732 | QualifiedResults.push_back(Candidate); |
| 3733 | break; |
| 3734 | } |
Kaelyn Takata | 20deb1d | 2015-01-28 00:46:09 +0000 | [diff] [blame] | 3735 | Candidate.setCorrectionRange(SS.get(), Result.getLookupNameInfo()); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3736 | return true; |
| 3737 | } |
| 3738 | return false; |
| 3739 | } |
| 3740 | |
| 3741 | void TypoCorrectionConsumer::performQualifiedLookups() { |
| 3742 | unsigned TypoLen = Typo->getName().size(); |
| 3743 | for (auto QR : QualifiedResults) { |
| 3744 | for (auto NSI : Namespaces) { |
| 3745 | DeclContext *Ctx = NSI.DeclCtx; |
| 3746 | const Type *NSType = NSI.NameSpecifier->getAsType(); |
| 3747 | |
| 3748 | // If the current NestedNameSpecifier refers to a class and the |
| 3749 | // current correction candidate is the name of that class, then skip |
| 3750 | // it as it is unlikely a qualified version of the class' constructor |
| 3751 | // is an appropriate correction. |
| 3752 | if (CXXRecordDecl *NSDecl = NSType ? NSType->getAsCXXRecordDecl() : 0) { |
| 3753 | if (NSDecl->getIdentifier() == QR.getCorrectionAsIdentifierInfo()) |
| 3754 | continue; |
| 3755 | } |
| 3756 | |
| 3757 | TypoCorrection TC(QR); |
| 3758 | TC.ClearCorrectionDecls(); |
| 3759 | TC.setCorrectionSpecifier(NSI.NameSpecifier); |
| 3760 | TC.setQualifierDistance(NSI.EditDistance); |
| 3761 | TC.setCallbackDistance(0); // Reset the callback distance |
| 3762 | |
| 3763 | // If the current correction candidate and namespace combination are |
| 3764 | // too far away from the original typo based on the normalized edit |
| 3765 | // distance, then skip performing a qualified name lookup. |
| 3766 | unsigned TmpED = TC.getEditDistance(true); |
| 3767 | if (QR.getCorrectionAsIdentifierInfo() != Typo && TmpED && |
| 3768 | TypoLen / TmpED < 3) |
| 3769 | continue; |
| 3770 | |
| 3771 | Result.clear(); |
| 3772 | Result.setLookupName(QR.getCorrectionAsIdentifierInfo()); |
| 3773 | if (!SemaRef.LookupQualifiedName(Result, Ctx)) |
| 3774 | continue; |
| 3775 | |
| 3776 | // Any corrections added below will be validated in subsequent |
| 3777 | // iterations of the main while() loop over the Consumer's contents. |
| 3778 | switch (Result.getResultKind()) { |
| 3779 | case LookupResult::Found: |
| 3780 | case LookupResult::FoundOverloaded: { |
| 3781 | if (SS && SS->isValid()) { |
| 3782 | std::string NewQualified = TC.getAsString(SemaRef.getLangOpts()); |
| 3783 | std::string OldQualified; |
| 3784 | llvm::raw_string_ostream OldOStream(OldQualified); |
| 3785 | SS->getScopeRep()->print(OldOStream, SemaRef.getPrintingPolicy()); |
| 3786 | OldOStream << Typo->getName(); |
| 3787 | // If correction candidate would be an identical written qualified |
| 3788 | // identifer, then the existing CXXScopeSpec probably included a |
| 3789 | // typedef that didn't get accounted for properly. |
| 3790 | if (OldOStream.str() == NewQualified) |
| 3791 | break; |
| 3792 | } |
| 3793 | for (LookupResult::iterator TRD = Result.begin(), TRDEnd = Result.end(); |
| 3794 | TRD != TRDEnd; ++TRD) { |
| 3795 | if (SemaRef.CheckMemberAccess(TC.getCorrectionRange().getBegin(), |
| 3796 | NSType ? NSType->getAsCXXRecordDecl() |
| 3797 | : nullptr, |
| 3798 | TRD.getPair()) == Sema::AR_accessible) |
| 3799 | TC.addCorrectionDecl(*TRD); |
| 3800 | } |
Kaelyn Takata | 0a31302 | 2014-11-20 22:06:26 +0000 | [diff] [blame] | 3801 | if (TC.isResolved()) { |
| 3802 | TC.setCorrectionRange(SS.get(), Result.getLookupNameInfo()); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3803 | addCorrection(TC); |
Kaelyn Takata | 0a31302 | 2014-11-20 22:06:26 +0000 | [diff] [blame] | 3804 | } |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 3805 | break; |
| 3806 | } |
| 3807 | case LookupResult::NotFound: |
| 3808 | case LookupResult::NotFoundInCurrentInstantiation: |
| 3809 | case LookupResult::Ambiguous: |
| 3810 | case LookupResult::FoundUnresolvedValue: |
| 3811 | break; |
| 3812 | } |
| 3813 | } |
| 3814 | } |
| 3815 | QualifiedResults.clear(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Kaelyn Takata | cd7c3a9 | 2014-06-11 18:33:46 +0000 | [diff] [blame] | 3818 | TypoCorrectionConsumer::NamespaceSpecifierSet::NamespaceSpecifierSet( |
| 3819 | ASTContext &Context, DeclContext *CurContext, CXXScopeSpec *CurScopeSpec) |
Benjamin Kramer | 1553727 | 2015-03-13 16:10:42 +0000 | [diff] [blame] | 3820 | : Context(Context), CurContextChain(buildContextChain(CurContext)) { |
Kaelyn Takata | cd7c3a9 | 2014-06-11 18:33:46 +0000 | [diff] [blame] | 3821 | if (NestedNameSpecifier *NNS = |
| 3822 | CurScopeSpec ? CurScopeSpec->getScopeRep() : nullptr) { |
| 3823 | llvm::raw_string_ostream SpecifierOStream(CurNameSpecifier); |
| 3824 | NNS->print(SpecifierOStream, Context.getPrintingPolicy()); |
| 3825 | |
| 3826 | getNestedNameSpecifierIdentifiers(NNS, CurNameSpecifierIdentifiers); |
| 3827 | } |
| 3828 | // Build the list of identifiers that would be used for an absolute |
| 3829 | // (from the global context) NestedNameSpecifier referring to the current |
| 3830 | // context. |
| 3831 | for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(), |
| 3832 | CEnd = CurContextChain.rend(); |
| 3833 | C != CEnd; ++C) { |
| 3834 | if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) |
| 3835 | CurContextIdentifiers.push_back(ND->getIdentifier()); |
| 3836 | } |
| 3837 | |
| 3838 | // Add the global context as a NestedNameSpecifier |
Hans Wennborg | 6601013 | 2014-06-11 21:24:13 +0000 | [diff] [blame] | 3839 | SpecifierInfo SI = {cast<DeclContext>(Context.getTranslationUnitDecl()), |
| 3840 | NestedNameSpecifier::GlobalSpecifier(Context), 1}; |
| 3841 | DistanceMap[1].push_back(SI); |
Kaelyn Takata | cd7c3a9 | 2014-06-11 18:33:46 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
Kaelyn Takata | 7dcc0e6 | 2014-06-11 18:07:08 +0000 | [diff] [blame] | 3844 | auto TypoCorrectionConsumer::NamespaceSpecifierSet::buildContextChain( |
| 3845 | DeclContext *Start) -> DeclContextList { |
Nick Lewycky | 0d9b319 | 2013-04-08 21:55:21 +0000 | [diff] [blame] | 3846 | assert(Start && "Building a context chain from a null context"); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3847 | DeclContextList Chain; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3848 | for (DeclContext *DC = Start->getPrimaryContext(); DC != nullptr; |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3849 | DC = DC->getLookupParent()) { |
| 3850 | NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(DC); |
| 3851 | if (!DC->isInlineNamespace() && !DC->isTransparentContext() && |
| 3852 | !(ND && ND->isAnonymousNamespace())) |
| 3853 | Chain.push_back(DC->getPrimaryContext()); |
| 3854 | } |
| 3855 | return Chain; |
| 3856 | } |
| 3857 | |
Kaelyn Takata | a5bdbc8 | 2014-06-11 18:07:05 +0000 | [diff] [blame] | 3858 | unsigned |
| 3859 | TypoCorrectionConsumer::NamespaceSpecifierSet::buildNestedNameSpecifier( |
| 3860 | DeclContextList &DeclChain, NestedNameSpecifier *&NNS) { |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3861 | unsigned NumSpecifiers = 0; |
| 3862 | for (DeclContextList::reverse_iterator C = DeclChain.rbegin(), |
| 3863 | CEnd = DeclChain.rend(); |
| 3864 | C != CEnd; ++C) { |
| 3865 | if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) { |
| 3866 | NNS = NestedNameSpecifier::Create(Context, NNS, ND); |
| 3867 | ++NumSpecifiers; |
| 3868 | } else if (RecordDecl *RD = dyn_cast_or_null<RecordDecl>(*C)) { |
| 3869 | NNS = NestedNameSpecifier::Create(Context, NNS, RD->isTemplateDecl(), |
| 3870 | RD->getTypeForDecl()); |
| 3871 | ++NumSpecifiers; |
| 3872 | } |
| 3873 | } |
| 3874 | return NumSpecifiers; |
| 3875 | } |
| 3876 | |
Kaelyn Takata | a5bdbc8 | 2014-06-11 18:07:05 +0000 | [diff] [blame] | 3877 | void TypoCorrectionConsumer::NamespaceSpecifierSet::addNameSpecifier( |
| 3878 | DeclContext *Ctx) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3879 | NestedNameSpecifier *NNS = nullptr; |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3880 | unsigned NumSpecifiers = 0; |
Kaelyn Takata | 0fc7519 | 2014-06-11 18:06:56 +0000 | [diff] [blame] | 3881 | DeclContextList NamespaceDeclChain(buildContextChain(Ctx)); |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3882 | DeclContextList FullNamespaceDeclChain(NamespaceDeclChain); |
| 3883 | |
| 3884 | // Eliminate common elements from the two DeclContext chains. |
| 3885 | for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(), |
| 3886 | CEnd = CurContextChain.rend(); |
| 3887 | C != CEnd && !NamespaceDeclChain.empty() && |
| 3888 | NamespaceDeclChain.back() == *C; ++C) { |
| 3889 | NamespaceDeclChain.pop_back(); |
| 3890 | } |
| 3891 | |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3892 | // Build the NestedNameSpecifier from what is left of the NamespaceDeclChain |
Kaelyn Takata | a5bdbc8 | 2014-06-11 18:07:05 +0000 | [diff] [blame] | 3893 | NumSpecifiers = buildNestedNameSpecifier(NamespaceDeclChain, NNS); |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3894 | |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3895 | // Add an explicit leading '::' specifier if needed. |
| 3896 | if (NamespaceDeclChain.empty()) { |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3897 | // Rebuild the NestedNameSpecifier as a globally-qualified specifier. |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3898 | NNS = NestedNameSpecifier::GlobalSpecifier(Context); |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3899 | NumSpecifiers = |
Kaelyn Takata | a5bdbc8 | 2014-06-11 18:07:05 +0000 | [diff] [blame] | 3900 | buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); |
Kaelyn Uhrain | 653ff24 | 2013-10-02 18:26:35 +0000 | [diff] [blame] | 3901 | } else if (NamedDecl *ND = |
| 3902 | dyn_cast_or_null<NamedDecl>(NamespaceDeclChain.back())) { |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3903 | IdentifierInfo *Name = ND->getIdentifier(); |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3904 | bool SameNameSpecifier = false; |
| 3905 | if (std::find(CurNameSpecifierIdentifiers.begin(), |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3906 | CurNameSpecifierIdentifiers.end(), |
| 3907 | Name) != CurNameSpecifierIdentifiers.end()) { |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3908 | std::string NewNameSpecifier; |
| 3909 | llvm::raw_string_ostream SpecifierOStream(NewNameSpecifier); |
| 3910 | SmallVector<const IdentifierInfo *, 4> NewNameSpecifierIdentifiers; |
| 3911 | getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); |
| 3912 | NNS->print(SpecifierOStream, Context.getPrintingPolicy()); |
| 3913 | SpecifierOStream.flush(); |
| 3914 | SameNameSpecifier = NewNameSpecifier == CurNameSpecifier; |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3915 | } |
Kaelyn Uhrain | f7b63e3 | 2013-10-19 00:04:52 +0000 | [diff] [blame] | 3916 | if (SameNameSpecifier || |
| 3917 | std::find(CurContextIdentifiers.begin(), CurContextIdentifiers.end(), |
| 3918 | Name) != CurContextIdentifiers.end()) { |
| 3919 | // Rebuild the NestedNameSpecifier as a globally-qualified specifier. |
| 3920 | NNS = NestedNameSpecifier::GlobalSpecifier(Context); |
| 3921 | NumSpecifiers = |
Kaelyn Takata | a5bdbc8 | 2014-06-11 18:07:05 +0000 | [diff] [blame] | 3922 | buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | // If the built NestedNameSpecifier would be replacing an existing |
| 3927 | // NestedNameSpecifier, use the number of component identifiers that |
| 3928 | // would need to be changed as the edit distance instead of the number |
| 3929 | // of components in the built NestedNameSpecifier. |
| 3930 | if (NNS && !CurNameSpecifierIdentifiers.empty()) { |
| 3931 | SmallVector<const IdentifierInfo*, 4> NewNameSpecifierIdentifiers; |
| 3932 | getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); |
| 3933 | NumSpecifiers = llvm::ComputeEditDistance( |
Craig Topper | 8c2a2a0 | 2014-08-30 16:55:39 +0000 | [diff] [blame] | 3934 | llvm::makeArrayRef(CurNameSpecifierIdentifiers), |
| 3935 | llvm::makeArrayRef(NewNameSpecifierIdentifiers)); |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Hans Wennborg | 6601013 | 2014-06-11 21:24:13 +0000 | [diff] [blame] | 3938 | SpecifierInfo SI = {Ctx, NNS, NumSpecifiers}; |
| 3939 | DistanceMap[NumSpecifiers].push_back(SI); |
Kaelyn Uhrain | 95995be | 2013-09-26 19:10:29 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3942 | /// \brief Perform name lookup for a possible result for typo correction. |
| 3943 | static void LookupPotentialTypoResult(Sema &SemaRef, |
| 3944 | LookupResult &Res, |
| 3945 | IdentifierInfo *Name, |
| 3946 | Scope *S, CXXScopeSpec *SS, |
| 3947 | DeclContext *MemberContext, |
| 3948 | bool EnteringContext, |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3949 | bool isObjCIvarLookup, |
| 3950 | bool FindHidden) { |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3951 | Res.suppressDiagnostics(); |
| 3952 | Res.clear(); |
| 3953 | Res.setLookupName(Name); |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 3954 | Res.setAllowHidden(FindHidden); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3955 | if (MemberContext) { |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3956 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) { |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 3957 | if (isObjCIvarLookup) { |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3958 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) { |
| 3959 | Res.addDecl(Ivar); |
| 3960 | Res.resolveKind(); |
| 3961 | return; |
| 3962 | } |
| 3963 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3964 | |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3965 | if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration(Name)) { |
| 3966 | Res.addDecl(Prop); |
| 3967 | Res.resolveKind(); |
| 3968 | return; |
| 3969 | } |
| 3970 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3971 | |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3972 | SemaRef.LookupQualifiedName(Res, MemberContext); |
| 3973 | return; |
| 3974 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3975 | |
| 3976 | SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3977 | EnteringContext); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3978 | |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3979 | // Fake ivar lookup; this should really be part of |
| 3980 | // LookupParsedName. |
| 3981 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 3982 | if (Method->isInstanceMethod() && Method->getClassInterface() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3983 | (Res.empty() || |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3984 | (Res.isSingleResult() && |
| 3985 | Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3986 | if (ObjCIvarDecl *IV |
Douglas Gregor | d507d77 | 2010-10-20 03:06:34 +0000 | [diff] [blame] | 3987 | = Method->getClassInterface()->lookupInstanceVariable(Name)) { |
| 3988 | Res.addDecl(IV); |
| 3989 | Res.resolveKind(); |
| 3990 | } |
| 3991 | } |
| 3992 | } |
| 3993 | } |
| 3994 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3995 | /// \brief Add keywords to the consumer as possible typo corrections. |
| 3996 | static void AddKeywordsToConsumer(Sema &SemaRef, |
| 3997 | TypoCorrectionConsumer &Consumer, |
Richard Smith | b3a1df0 | 2012-06-08 21:35:42 +0000 | [diff] [blame] | 3998 | Scope *S, CorrectionCandidateCallback &CCC, |
| 3999 | bool AfterNestedNameSpecifier) { |
| 4000 | if (AfterNestedNameSpecifier) { |
| 4001 | // For 'X::', we know exactly which keywords can appear next. |
| 4002 | Consumer.addKeywordResult("template"); |
| 4003 | if (CCC.WantExpressionKeywords) |
| 4004 | Consumer.addKeywordResult("operator"); |
| 4005 | return; |
| 4006 | } |
| 4007 | |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 4008 | if (CCC.WantObjCSuper) |
| 4009 | Consumer.addKeywordResult("super"); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4010 | |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 4011 | if (CCC.WantTypeSpecifiers) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4012 | // Add type-specifier keywords to the set of results. |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 4013 | static const char *const CTypeSpecs[] = { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4014 | "char", "const", "double", "enum", "float", "int", "long", "short", |
Douglas Gregor | 3b22a88 | 2011-07-01 21:27:45 +0000 | [diff] [blame] | 4015 | "signed", "struct", "union", "unsigned", "void", "volatile", |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4016 | "_Complex", "_Imaginary", |
| 4017 | // storage-specifiers as well |
| 4018 | "extern", "inline", "static", "typedef" |
| 4019 | }; |
| 4020 | |
Craig Topper | e5ce831 | 2013-07-15 03:38:40 +0000 | [diff] [blame] | 4021 | const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4022 | for (unsigned I = 0; I != NumCTypeSpecs; ++I) |
| 4023 | Consumer.addKeywordResult(CTypeSpecs[I]); |
| 4024 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4025 | if (SemaRef.getLangOpts().C99) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4026 | Consumer.addKeywordResult("restrict"); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4027 | if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4028 | Consumer.addKeywordResult("bool"); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4029 | else if (SemaRef.getLangOpts().C99) |
Douglas Gregor | 3b22a88 | 2011-07-01 21:27:45 +0000 | [diff] [blame] | 4030 | Consumer.addKeywordResult("_Bool"); |
| 4031 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4032 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4033 | Consumer.addKeywordResult("class"); |
| 4034 | Consumer.addKeywordResult("typename"); |
| 4035 | Consumer.addKeywordResult("wchar_t"); |
| 4036 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4037 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4038 | Consumer.addKeywordResult("char16_t"); |
| 4039 | Consumer.addKeywordResult("char32_t"); |
| 4040 | Consumer.addKeywordResult("constexpr"); |
| 4041 | Consumer.addKeywordResult("decltype"); |
| 4042 | Consumer.addKeywordResult("thread_local"); |
| 4043 | } |
| 4044 | } |
| 4045 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4046 | if (SemaRef.getLangOpts().GNUMode) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4047 | Consumer.addKeywordResult("typeof"); |
Kaelyn Takata | b04846b | 2014-07-28 18:14:02 +0000 | [diff] [blame] | 4048 | } else if (CCC.WantFunctionLikeCasts) { |
| 4049 | static const char *const CastableTypeSpecs[] = { |
| 4050 | "char", "double", "float", "int", "long", "short", |
| 4051 | "signed", "unsigned", "void" |
| 4052 | }; |
| 4053 | for (auto *kw : CastableTypeSpecs) |
| 4054 | Consumer.addKeywordResult(kw); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4057 | if (CCC.WantCXXNamedCasts && SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4058 | Consumer.addKeywordResult("const_cast"); |
| 4059 | Consumer.addKeywordResult("dynamic_cast"); |
| 4060 | Consumer.addKeywordResult("reinterpret_cast"); |
| 4061 | Consumer.addKeywordResult("static_cast"); |
| 4062 | } |
| 4063 | |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 4064 | if (CCC.WantExpressionKeywords) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4065 | Consumer.addKeywordResult("sizeof"); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4066 | if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4067 | Consumer.addKeywordResult("false"); |
| 4068 | Consumer.addKeywordResult("true"); |
| 4069 | } |
| 4070 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4071 | if (SemaRef.getLangOpts().CPlusPlus) { |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 4072 | static const char *const CXXExprs[] = { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4073 | "delete", "new", "operator", "throw", "typeid" |
| 4074 | }; |
Craig Topper | e5ce831 | 2013-07-15 03:38:40 +0000 | [diff] [blame] | 4075 | const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4076 | for (unsigned I = 0; I != NumCXXExprs; ++I) |
| 4077 | Consumer.addKeywordResult(CXXExprs[I]); |
| 4078 | |
| 4079 | if (isa<CXXMethodDecl>(SemaRef.CurContext) && |
| 4080 | cast<CXXMethodDecl>(SemaRef.CurContext)->isInstance()) |
| 4081 | Consumer.addKeywordResult("this"); |
| 4082 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4083 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4084 | Consumer.addKeywordResult("alignof"); |
| 4085 | Consumer.addKeywordResult("nullptr"); |
| 4086 | } |
| 4087 | } |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 4088 | |
| 4089 | if (SemaRef.getLangOpts().C11) { |
| 4090 | // FIXME: We should not suggest _Alignof if the alignof macro |
| 4091 | // is present. |
| 4092 | Consumer.addKeywordResult("_Alignof"); |
| 4093 | } |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 4096 | if (CCC.WantRemainingKeywords) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4097 | if (SemaRef.getCurFunctionOrMethodDecl() || SemaRef.getCurBlock()) { |
| 4098 | // Statements. |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 4099 | static const char *const CStmts[] = { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4100 | "do", "else", "for", "goto", "if", "return", "switch", "while" }; |
Craig Topper | e5ce831 | 2013-07-15 03:38:40 +0000 | [diff] [blame] | 4101 | const unsigned NumCStmts = llvm::array_lengthof(CStmts); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4102 | for (unsigned I = 0; I != NumCStmts; ++I) |
| 4103 | Consumer.addKeywordResult(CStmts[I]); |
| 4104 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4105 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4106 | Consumer.addKeywordResult("catch"); |
| 4107 | Consumer.addKeywordResult("try"); |
| 4108 | } |
| 4109 | |
| 4110 | if (S && S->getBreakParent()) |
| 4111 | Consumer.addKeywordResult("break"); |
| 4112 | |
| 4113 | if (S && S->getContinueParent()) |
| 4114 | Consumer.addKeywordResult("continue"); |
| 4115 | |
| 4116 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
| 4117 | Consumer.addKeywordResult("case"); |
| 4118 | Consumer.addKeywordResult("default"); |
| 4119 | } |
| 4120 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4121 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4122 | Consumer.addKeywordResult("namespace"); |
| 4123 | Consumer.addKeywordResult("template"); |
| 4124 | } |
| 4125 | |
| 4126 | if (S && S->isClassScope()) { |
| 4127 | Consumer.addKeywordResult("explicit"); |
| 4128 | Consumer.addKeywordResult("friend"); |
| 4129 | Consumer.addKeywordResult("mutable"); |
| 4130 | Consumer.addKeywordResult("private"); |
| 4131 | Consumer.addKeywordResult("protected"); |
| 4132 | Consumer.addKeywordResult("public"); |
| 4133 | Consumer.addKeywordResult("virtual"); |
| 4134 | } |
| 4135 | } |
| 4136 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4137 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4138 | Consumer.addKeywordResult("using"); |
| 4139 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4140 | if (SemaRef.getLangOpts().CPlusPlus11) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4141 | Consumer.addKeywordResult("static_assert"); |
| 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4146 | std::unique_ptr<TypoCorrectionConsumer> Sema::makeTypoCorrectionConsumer( |
| 4147 | const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind, |
| 4148 | Scope *S, CXXScopeSpec *SS, |
| 4149 | std::unique_ptr<CorrectionCandidateCallback> CCC, |
| 4150 | DeclContext *MemberContext, bool EnteringContext, |
Nick Lewycky | 2465326 | 2014-12-16 21:39:02 +0000 | [diff] [blame] | 4151 | const ObjCObjectPointerType *OPT, bool ErrorRecovery) { |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4152 | |
| 4153 | if (Diags.hasFatalErrorOccurred() || !getLangOpts().SpellChecking || |
| 4154 | DisableTypoCorrection) |
| 4155 | return nullptr; |
| 4156 | |
| 4157 | // In Microsoft mode, don't perform typo correction in a template member |
| 4158 | // function dependent context because it interferes with the "lookup into |
| 4159 | // dependent bases of class templates" feature. |
| 4160 | if (getLangOpts().MSVCCompat && CurContext->isDependentContext() && |
| 4161 | isa<CXXMethodDecl>(CurContext)) |
| 4162 | return nullptr; |
| 4163 | |
| 4164 | // We only attempt to correct typos for identifiers. |
| 4165 | IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); |
| 4166 | if (!Typo) |
| 4167 | return nullptr; |
| 4168 | |
| 4169 | // If the scope specifier itself was invalid, don't try to correct |
| 4170 | // typos. |
| 4171 | if (SS && SS->isInvalid()) |
| 4172 | return nullptr; |
| 4173 | |
| 4174 | // Never try to correct typos during template deduction or |
| 4175 | // instantiation. |
| 4176 | if (!ActiveTemplateInstantiations.empty()) |
| 4177 | return nullptr; |
| 4178 | |
| 4179 | // Don't try to correct 'super'. |
| 4180 | if (S && S->isInObjcMethodScope() && Typo == getSuperIdentifier()) |
| 4181 | return nullptr; |
| 4182 | |
| 4183 | // Abort if typo correction already failed for this specific typo. |
| 4184 | IdentifierSourceLocations::iterator locs = TypoCorrectionFailures.find(Typo); |
| 4185 | if (locs != TypoCorrectionFailures.end() && |
| 4186 | locs->second.count(TypoName.getLoc())) |
| 4187 | return nullptr; |
| 4188 | |
| 4189 | // Don't try to correct the identifier "vector" when in AltiVec mode. |
| 4190 | // TODO: Figure out why typo correction misbehaves in this case, fix it, and |
| 4191 | // remove this workaround. |
| 4192 | if (getLangOpts().AltiVec && Typo->isStr("vector")) |
| 4193 | return nullptr; |
| 4194 | |
Nick Lewycky | 2465326 | 2014-12-16 21:39:02 +0000 | [diff] [blame] | 4195 | // Provide a stop gap for files that are just seriously broken. Trying |
| 4196 | // to correct all typos can turn into a HUGE performance penalty, causing |
| 4197 | // some files to take minutes to get rejected by the parser. |
| 4198 | unsigned Limit = getDiagnostics().getDiagnosticOptions().SpellCheckingLimit; |
| 4199 | if (Limit && TyposCorrected >= Limit) |
| 4200 | return nullptr; |
| 4201 | ++TyposCorrected; |
| 4202 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4203 | // If we're handling a missing symbol error, using modules, and the |
| 4204 | // special search all modules option is used, look for a missing import. |
| 4205 | if (ErrorRecovery && getLangOpts().Modules && |
| 4206 | getLangOpts().ModulesSearchAll) { |
| 4207 | // The following has the side effect of loading the missing module. |
| 4208 | getModuleLoader().lookupMissingImports(Typo->getName(), |
| 4209 | TypoName.getLocStart()); |
| 4210 | } |
| 4211 | |
| 4212 | CorrectionCandidateCallback &CCCRef = *CCC; |
| 4213 | auto Consumer = llvm::make_unique<TypoCorrectionConsumer>( |
| 4214 | *this, TypoName, LookupKind, S, SS, std::move(CCC), MemberContext, |
| 4215 | EnteringContext); |
| 4216 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4217 | // Perform name lookup to find visible, similarly-named entities. |
Nick Lewycky | 2465326 | 2014-12-16 21:39:02 +0000 | [diff] [blame] | 4218 | bool IsUnqualifiedLookup = false; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4219 | DeclContext *QualifiedDC = MemberContext; |
| 4220 | if (MemberContext) { |
| 4221 | LookupVisibleDecls(MemberContext, LookupKind, *Consumer); |
| 4222 | |
| 4223 | // Look in qualified interfaces. |
| 4224 | if (OPT) { |
| 4225 | for (auto *I : OPT->quals()) |
| 4226 | LookupVisibleDecls(I, LookupKind, *Consumer); |
| 4227 | } |
| 4228 | } else if (SS && SS->isSet()) { |
| 4229 | QualifiedDC = computeDeclContext(*SS, EnteringContext); |
| 4230 | if (!QualifiedDC) |
| 4231 | return nullptr; |
| 4232 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4233 | LookupVisibleDecls(QualifiedDC, LookupKind, *Consumer); |
| 4234 | } else { |
| 4235 | IsUnqualifiedLookup = true; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | // Determine whether we are going to search in the various namespaces for |
| 4239 | // corrections. |
| 4240 | bool SearchNamespaces |
| 4241 | = getLangOpts().CPlusPlus && |
| 4242 | (IsUnqualifiedLookup || (SS && SS->isSet())); |
| 4243 | |
| 4244 | if (IsUnqualifiedLookup || SearchNamespaces) { |
| 4245 | // For unqualified lookup, look through all of the names that we have |
| 4246 | // seen in this translation unit. |
| 4247 | // FIXME: Re-add the ability to skip very unlikely potential corrections. |
| 4248 | for (const auto &I : Context.Idents) |
| 4249 | Consumer->FoundName(I.getKey()); |
| 4250 | |
| 4251 | // Walk through identifiers in external identifier sources. |
| 4252 | // FIXME: Re-add the ability to skip very unlikely potential corrections. |
| 4253 | if (IdentifierInfoLookup *External |
| 4254 | = Context.Idents.getExternalIdentifierLookup()) { |
| 4255 | std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers()); |
| 4256 | do { |
| 4257 | StringRef Name = Iter->Next(); |
| 4258 | if (Name.empty()) |
| 4259 | break; |
| 4260 | |
| 4261 | Consumer->FoundName(Name); |
| 4262 | } while (true); |
| 4263 | } |
| 4264 | } |
| 4265 | |
| 4266 | AddKeywordsToConsumer(*this, *Consumer, S, CCCRef, SS && SS->isNotEmpty()); |
| 4267 | |
| 4268 | // Build the NestedNameSpecifiers for the KnownNamespaces, if we're going |
| 4269 | // to search those namespaces. |
| 4270 | if (SearchNamespaces) { |
| 4271 | // Load any externally-known namespaces. |
| 4272 | if (ExternalSource && !LoadedExternalKnownNamespaces) { |
| 4273 | SmallVector<NamespaceDecl *, 4> ExternalKnownNamespaces; |
| 4274 | LoadedExternalKnownNamespaces = true; |
| 4275 | ExternalSource->ReadKnownNamespaces(ExternalKnownNamespaces); |
| 4276 | for (auto *N : ExternalKnownNamespaces) |
| 4277 | KnownNamespaces[N] = true; |
| 4278 | } |
| 4279 | |
| 4280 | Consumer->addNamespaces(KnownNamespaces); |
| 4281 | } |
| 4282 | |
| 4283 | return Consumer; |
| 4284 | } |
| 4285 | |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 4286 | /// \brief Try to "correct" a typo in the source code by finding |
| 4287 | /// visible declarations whose names are similar to the name that was |
| 4288 | /// present in the source code. |
| 4289 | /// |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4290 | /// \param TypoName the \c DeclarationNameInfo structure that contains |
| 4291 | /// the name that was present in the source code along with its location. |
| 4292 | /// |
| 4293 | /// \param LookupKind the name-lookup criteria used to search for the name. |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 4294 | /// |
| 4295 | /// \param S the scope in which name lookup occurs. |
| 4296 | /// |
| 4297 | /// \param SS the nested-name-specifier that precedes the name we're |
| 4298 | /// looking for, if present. |
| 4299 | /// |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 4300 | /// \param CCC A CorrectionCandidateCallback object that provides further |
| 4301 | /// validation of typo correction candidates. It also provides flags for |
| 4302 | /// determining the set of keywords permitted. |
| 4303 | /// |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 4304 | /// \param MemberContext if non-NULL, the context in which to look for |
| 4305 | /// a member access expression. |
| 4306 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4307 | /// \param EnteringContext whether we're entering the context described by |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 4308 | /// the nested-name-specifier SS. |
| 4309 | /// |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 4310 | /// \param OPT when non-NULL, the search for visible declarations will |
| 4311 | /// also walk the protocols in the qualified interfaces of \p OPT. |
| 4312 | /// |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4313 | /// \returns a \c TypoCorrection containing the corrected name if the typo |
| 4314 | /// along with information such as the \c NamedDecl where the corrected name |
| 4315 | /// was declared, and any additional \c NestedNameSpecifier needed to access |
| 4316 | /// it (C++ only). The \c TypoCorrection is empty if there is no correction. |
| 4317 | TypoCorrection Sema::CorrectTypo(const DeclarationNameInfo &TypoName, |
| 4318 | Sema::LookupNameKind LookupKind, |
| 4319 | Scope *S, CXXScopeSpec *SS, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 4320 | std::unique_ptr<CorrectionCandidateCallback> CCC, |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 4321 | CorrectTypoKind Mode, |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4322 | DeclContext *MemberContext, |
| 4323 | bool EnteringContext, |
Kaelyn Uhrain | 0e23844 | 2013-09-27 19:40:08 +0000 | [diff] [blame] | 4324 | const ObjCObjectPointerType *OPT, |
| 4325 | bool RecordFailure) { |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 4326 | assert(CCC && "CorrectTypo requires a CorrectionCandidateCallback"); |
| 4327 | |
Kaelyn Uhrain | f0aabda | 2013-08-12 19:54:38 +0000 | [diff] [blame] | 4328 | // Always let the ExternalSource have the first chance at correction, even |
| 4329 | // if we would otherwise have given up. |
| 4330 | if (ExternalSource) { |
| 4331 | if (TypoCorrection Correction = ExternalSource->CorrectTypo( |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 4332 | TypoName, LookupKind, S, SS, *CCC, MemberContext, EnteringContext, OPT)) |
Kaelyn Uhrain | f0aabda | 2013-08-12 19:54:38 +0000 | [diff] [blame] | 4333 | return Correction; |
| 4334 | } |
| 4335 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4336 | // Ugly hack equivalent to CTC == CTC_ObjCMessageReceiver; |
| 4337 | // WantObjCSuper is only true for CTC_ObjCMessageReceiver and for |
| 4338 | // some instances of CTC_Unknown, while WantRemainingKeywords is true |
| 4339 | // for CTC_Unknown but not for CTC_ObjCMessageReceiver. |
| 4340 | bool ObjCMessageReceiver = CCC->WantObjCSuper && !CCC->WantRemainingKeywords; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4341 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4342 | IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4343 | auto Consumer = makeTypoCorrectionConsumer( |
| 4344 | TypoName, LookupKind, S, SS, std::move(CCC), MemberContext, |
Nick Lewycky | 2465326 | 2014-12-16 21:39:02 +0000 | [diff] [blame] | 4345 | EnteringContext, OPT, Mode == CTK_ErrorRecovery); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 4346 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4347 | if (!Consumer) |
| 4348 | return TypoCorrection(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 4350 | // If we haven't found anything, we're done. |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4351 | if (Consumer->empty()) |
Nick Lewycky | 2465326 | 2014-12-16 21:39:02 +0000 | [diff] [blame] | 4352 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 4353 | |
Kaelyn Uhrain | 493ea63 | 2012-06-06 20:54:51 +0000 | [diff] [blame] | 4354 | // Make sure the best edit distance (prior to adding any namespace qualifiers) |
| 4355 | // is not more that about a third of the length of the typo's identifier. |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4356 | unsigned ED = Consumer->getBestEditDistance(true); |
| 4357 | unsigned TypoLen = Typo->getName().size(); |
Kaelyn Uhrain | 653ff24 | 2013-10-02 18:26:35 +0000 | [diff] [blame] | 4358 | if (ED > 0 && TypoLen / ED < 3) |
Nick Lewycky | 2465326 | 2014-12-16 21:39:02 +0000 | [diff] [blame] | 4359 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4360 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4361 | TypoCorrection BestTC = Consumer->getNextCorrection(); |
| 4362 | TypoCorrection SecondBestTC = Consumer->getNextCorrection(); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 4363 | if (!BestTC) |
Kaelyn Uhrain | 0e23844 | 2013-09-27 19:40:08 +0000 | [diff] [blame] | 4364 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4365 | |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 4366 | ED = BestTC.getEditDistance(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4367 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4368 | if (TypoLen >= 3 && ED > 0 && TypoLen / ED < 3) { |
Kaelyn Uhrain | cb7a040 | 2012-01-23 20:18:59 +0000 | [diff] [blame] | 4369 | // If this was an unqualified lookup and we believe the callback |
| 4370 | // object wouldn't have filtered out possible corrections, note |
| 4371 | // that no correction was found. |
Nick Lewycky | 2465326 | 2014-12-16 21:39:02 +0000 | [diff] [blame] | 4372 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Douglas Gregor | 0afa7f6 | 2010-10-14 20:34:08 +0000 | [diff] [blame] | 4375 | // If only a single name remains, return that result. |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 4376 | if (!SecondBestTC || |
| 4377 | SecondBestTC.getEditDistance(false) > BestTC.getEditDistance(false)) { |
| 4378 | const TypoCorrection &Result = BestTC; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4379 | |
Douglas Gregor | 2a1d72d | 2010-10-26 17:18:00 +0000 | [diff] [blame] | 4380 | // Don't correct to a keyword that's the same as the typo; the keyword |
| 4381 | // wasn't actually in scope. |
Kaelyn Uhrain | 0e23844 | 2013-09-27 19:40:08 +0000 | [diff] [blame] | 4382 | if (ED == 0 && Result.isKeyword()) |
| 4383 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4384 | |
David Blaikie | 04ea41c | 2012-10-12 20:00:44 +0000 | [diff] [blame] | 4385 | TypoCorrection TC = Result; |
| 4386 | TC.setCorrectionRange(SS, TypoName); |
Kaelyn Takata | a95ebc6 | 2014-06-17 23:47:29 +0000 | [diff] [blame] | 4387 | checkCorrectionVisibility(*this, TC); |
David Blaikie | 04ea41c | 2012-10-12 20:00:44 +0000 | [diff] [blame] | 4388 | return TC; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4389 | } else if (SecondBestTC && ObjCMessageReceiver) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4390 | // Prefer 'super' when we're completing in a message-receiver |
| 4391 | // context. |
| 4392 | |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 4393 | if (BestTC.getCorrection().getAsString() != "super") { |
| 4394 | if (SecondBestTC.getCorrection().getAsString() == "super") |
| 4395 | BestTC = SecondBestTC; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4396 | else if ((*Consumer)["super"].front().isKeyword()) |
| 4397 | BestTC = (*Consumer)["super"].front(); |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 4398 | } |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4399 | // Don't correct to a keyword that's the same as the typo; the keyword |
| 4400 | // wasn't actually in scope. |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 4401 | if (BestTC.getEditDistance() == 0 || |
| 4402 | BestTC.getCorrection().getAsString() != "super") |
Kaelyn Uhrain | 0e23844 | 2013-09-27 19:40:08 +0000 | [diff] [blame] | 4403 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4404 | |
Kaelyn Takata | 68fdd59 | 2014-06-11 18:07:01 +0000 | [diff] [blame] | 4405 | BestTC.setCorrectionRange(SS, TypoName); |
| 4406 | return BestTC; |
Douglas Gregor | af9eb59 | 2010-10-15 13:35:25 +0000 | [diff] [blame] | 4407 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | |
Kaelyn Takata | 73429fd | 2014-06-10 21:03:49 +0000 | [diff] [blame] | 4409 | // Record the failure's location if needed and return an empty correction. If |
| 4410 | // this was an unqualified lookup and we believe the callback object did not |
| 4411 | // filter out possible corrections, also cache the failure for the typo. |
Kaelyn Takata | ae9e97c | 2015-01-16 22:11:04 +0000 | [diff] [blame] | 4412 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure && !SecondBestTC); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4415 | /// \brief Try to "correct" a typo in the source code by finding |
| 4416 | /// visible declarations whose names are similar to the name that was |
| 4417 | /// present in the source code. |
| 4418 | /// |
| 4419 | /// \param TypoName the \c DeclarationNameInfo structure that contains |
| 4420 | /// the name that was present in the source code along with its location. |
| 4421 | /// |
| 4422 | /// \param LookupKind the name-lookup criteria used to search for the name. |
| 4423 | /// |
| 4424 | /// \param S the scope in which name lookup occurs. |
| 4425 | /// |
| 4426 | /// \param SS the nested-name-specifier that precedes the name we're |
| 4427 | /// looking for, if present. |
| 4428 | /// |
| 4429 | /// \param CCC A CorrectionCandidateCallback object that provides further |
| 4430 | /// validation of typo correction candidates. It also provides flags for |
| 4431 | /// determining the set of keywords permitted. |
| 4432 | /// |
| 4433 | /// \param TDG A TypoDiagnosticGenerator functor that will be used to print |
| 4434 | /// diagnostics when the actual typo correction is attempted. |
| 4435 | /// |
Kaelyn Takata | 8363f04 | 2014-10-27 18:07:42 +0000 | [diff] [blame] | 4436 | /// \param TRC A TypoRecoveryCallback functor that will be used to build an |
| 4437 | /// Expr from a typo correction candidate. |
| 4438 | /// |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4439 | /// \param MemberContext if non-NULL, the context in which to look for |
| 4440 | /// a member access expression. |
| 4441 | /// |
| 4442 | /// \param EnteringContext whether we're entering the context described by |
| 4443 | /// the nested-name-specifier SS. |
| 4444 | /// |
| 4445 | /// \param OPT when non-NULL, the search for visible declarations will |
| 4446 | /// also walk the protocols in the qualified interfaces of \p OPT. |
| 4447 | /// |
| 4448 | /// \returns a new \c TypoExpr that will later be replaced in the AST with an |
| 4449 | /// Expr representing the result of performing typo correction, or nullptr if |
| 4450 | /// typo correction is not possible. If nullptr is returned, no diagnostics will |
| 4451 | /// be emitted and it is the responsibility of the caller to emit any that are |
| 4452 | /// needed. |
| 4453 | TypoExpr *Sema::CorrectTypoDelayed( |
| 4454 | const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind, |
| 4455 | Scope *S, CXXScopeSpec *SS, |
| 4456 | std::unique_ptr<CorrectionCandidateCallback> CCC, |
Kaelyn Takata | 8363f04 | 2014-10-27 18:07:42 +0000 | [diff] [blame] | 4457 | TypoDiagnosticGenerator TDG, TypoRecoveryCallback TRC, CorrectTypoKind Mode, |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4458 | DeclContext *MemberContext, bool EnteringContext, |
| 4459 | const ObjCObjectPointerType *OPT) { |
| 4460 | assert(CCC && "CorrectTypoDelayed requires a CorrectionCandidateCallback"); |
| 4461 | |
| 4462 | TypoCorrection Empty; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4463 | auto Consumer = makeTypoCorrectionConsumer( |
| 4464 | TypoName, LookupKind, S, SS, std::move(CCC), MemberContext, |
Kaelyn Takata | ae9e97c | 2015-01-16 22:11:04 +0000 | [diff] [blame] | 4465 | EnteringContext, OPT, Mode == CTK_ErrorRecovery); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4466 | |
| 4467 | if (!Consumer || Consumer->empty()) |
| 4468 | return nullptr; |
| 4469 | |
| 4470 | // Make sure the best edit distance (prior to adding any namespace qualifiers) |
| 4471 | // is not more that about a third of the length of the typo's identifier. |
| 4472 | unsigned ED = Consumer->getBestEditDistance(true); |
| 4473 | IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); |
| 4474 | if (ED > 0 && Typo->getName().size() / ED < 3) |
| 4475 | return nullptr; |
| 4476 | |
| 4477 | ExprEvalContexts.back().NumTypos++; |
Kaelyn Takata | 8363f04 | 2014-10-27 18:07:42 +0000 | [diff] [blame] | 4478 | return createDelayedTypo(std::move(Consumer), std::move(TDG), std::move(TRC)); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 4481 | void TypoCorrection::addCorrectionDecl(NamedDecl *CDecl) { |
| 4482 | if (!CDecl) return; |
| 4483 | |
| 4484 | if (isKeyword()) |
| 4485 | CorrectionDecls.clear(); |
| 4486 | |
Kaelyn Uhrain | f60b55a | 2012-11-19 18:49:53 +0000 | [diff] [blame] | 4487 | CorrectionDecls.push_back(CDecl->getUnderlyingDecl()); |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 4488 | |
| 4489 | if (!CorrectionName) |
| 4490 | CorrectionName = CDecl->getDeclName(); |
| 4491 | } |
| 4492 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4493 | std::string TypoCorrection::getAsString(const LangOptions &LO) const { |
| 4494 | if (CorrectionNameSpec) { |
| 4495 | std::string tmpBuffer; |
| 4496 | llvm::raw_string_ostream PrefixOStream(tmpBuffer); |
| 4497 | CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO)); |
David Blaikie | d4da872 | 2013-05-14 21:04:00 +0000 | [diff] [blame] | 4498 | PrefixOStream << CorrectionName; |
Benjamin Kramer | 73faad6 | 2012-04-14 08:26:28 +0000 | [diff] [blame] | 4499 | return PrefixOStream.str(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | return CorrectionName.getAsString(); |
Douglas Gregor | 2d43530 | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 4503 | } |
Kaelyn Uhrain | 989b7ca | 2013-04-03 16:59:49 +0000 | [diff] [blame] | 4504 | |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 4505 | bool CorrectionCandidateCallback::ValidateCandidate( |
| 4506 | const TypoCorrection &candidate) { |
Kaelyn Uhrain | 989b7ca | 2013-04-03 16:59:49 +0000 | [diff] [blame] | 4507 | if (!candidate.isResolved()) |
| 4508 | return true; |
| 4509 | |
| 4510 | if (candidate.isKeyword()) |
| 4511 | return WantTypeSpecifiers || WantExpressionKeywords || WantCXXNamedCasts || |
| 4512 | WantRemainingKeywords || WantObjCSuper; |
| 4513 | |
Nick Lewycky | 9ea8efa | 2014-06-23 22:57:51 +0000 | [diff] [blame] | 4514 | bool HasNonType = false; |
| 4515 | bool HasStaticMethod = false; |
| 4516 | bool HasNonStaticMethod = false; |
| 4517 | for (Decl *D : candidate) { |
| 4518 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) |
| 4519 | D = FTD->getTemplatedDecl(); |
| 4520 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 4521 | if (Method->isStatic()) |
| 4522 | HasStaticMethod = true; |
| 4523 | else |
| 4524 | HasNonStaticMethod = true; |
| 4525 | } |
| 4526 | if (!isa<TypeDecl>(D)) |
| 4527 | HasNonType = true; |
Kaelyn Uhrain | 989b7ca | 2013-04-03 16:59:49 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
Nick Lewycky | 9ea8efa | 2014-06-23 22:57:51 +0000 | [diff] [blame] | 4530 | if (IsAddressOfOperand && HasNonStaticMethod && !HasStaticMethod && |
| 4531 | !candidate.getCorrectionSpecifier()) |
| 4532 | return false; |
| 4533 | |
| 4534 | return WantTypeSpecifiers || HasNonType; |
Kaelyn Uhrain | 989b7ca | 2013-04-03 16:59:49 +0000 | [diff] [blame] | 4535 | } |
Kaelyn Uhrain | 53e7219 | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 4536 | |
| 4537 | FunctionCallFilterCCC::FunctionCallFilterCCC(Sema &SemaRef, unsigned NumArgs, |
Kaelyn Uhrain | b4b1475 | 2014-02-28 18:12:42 +0000 | [diff] [blame] | 4538 | bool HasExplicitTemplateArgs, |
Kaelyn Takata | fb271f0 | 2014-04-04 22:16:30 +0000 | [diff] [blame] | 4539 | MemberExpr *ME) |
Kaelyn Uhrain | b4b1475 | 2014-02-28 18:12:42 +0000 | [diff] [blame] | 4540 | : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs), |
Kaelyn Takata | fb271f0 | 2014-04-04 22:16:30 +0000 | [diff] [blame] | 4541 | CurContext(SemaRef.CurContext), MemberFn(ME) { |
Kaelyn Takata | b04846b | 2014-07-28 18:14:02 +0000 | [diff] [blame] | 4542 | WantTypeSpecifiers = false; |
| 4543 | WantFunctionLikeCasts = SemaRef.getLangOpts().CPlusPlus && NumArgs == 1; |
Kaelyn Uhrain | 53e7219 | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 4544 | WantRemainingKeywords = false; |
| 4545 | } |
| 4546 | |
| 4547 | bool FunctionCallFilterCCC::ValidateCandidate(const TypoCorrection &candidate) { |
| 4548 | if (!candidate.getCorrectionDecl()) |
| 4549 | return candidate.isKeyword(); |
| 4550 | |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 4551 | for (auto *C : candidate) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4552 | FunctionDecl *FD = nullptr; |
Aaron Ballman | 1e606f4 | 2014-07-15 22:03:49 +0000 | [diff] [blame] | 4553 | NamedDecl *ND = C->getUnderlyingDecl(); |
Kaelyn Uhrain | 53e7219 | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 4554 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4555 | FD = FTD->getTemplatedDecl(); |
| 4556 | if (!HasExplicitTemplateArgs && !FD) { |
| 4557 | if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { |
| 4558 | // If the Decl is neither a function nor a template function, |
| 4559 | // determine if it is a pointer or reference to a function. If so, |
| 4560 | // check against the number of arguments expected for the pointee. |
| 4561 | QualType ValType = cast<ValueDecl>(ND)->getType(); |
| 4562 | if (ValType->isAnyPointerType() || ValType->isReferenceType()) |
| 4563 | ValType = ValType->getPointeeType(); |
| 4564 | if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4565 | if (FPT->getNumParams() == NumArgs) |
Kaelyn Uhrain | 53e7219 | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 4566 | return true; |
| 4567 | } |
| 4568 | } |
Kaelyn Uhrain | b4b1475 | 2014-02-28 18:12:42 +0000 | [diff] [blame] | 4569 | |
| 4570 | // Skip the current candidate if it is not a FunctionDecl or does not accept |
| 4571 | // the current number of arguments. |
| 4572 | if (!FD || !(FD->getNumParams() >= NumArgs && |
| 4573 | FD->getMinRequiredArguments() <= NumArgs)) |
| 4574 | continue; |
| 4575 | |
Kaelyn Takata | fb271f0 | 2014-04-04 22:16:30 +0000 | [diff] [blame] | 4576 | // If the current candidate is a non-static C++ method, skip the candidate |
| 4577 | // unless the method being corrected--or the current DeclContext, if the |
| 4578 | // function being corrected is not a method--is a method in the same class |
| 4579 | // or a descendent class of the candidate's parent class. |
Kaelyn Uhrain | b4b1475 | 2014-02-28 18:12:42 +0000 | [diff] [blame] | 4580 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Kaelyn Takata | fb271f0 | 2014-04-04 22:16:30 +0000 | [diff] [blame] | 4581 | if (MemberFn || !MD->isStatic()) { |
| 4582 | CXXMethodDecl *CurMD = |
| 4583 | MemberFn |
| 4584 | ? dyn_cast_or_null<CXXMethodDecl>(MemberFn->getMemberDecl()) |
| 4585 | : dyn_cast_or_null<CXXMethodDecl>(CurContext); |
Kaelyn Uhrain | b4b1475 | 2014-02-28 18:12:42 +0000 | [diff] [blame] | 4586 | CXXRecordDecl *CurRD = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4587 | CurMD ? CurMD->getParent()->getCanonicalDecl() : nullptr; |
Kaelyn Uhrain | b4b1475 | 2014-02-28 18:12:42 +0000 | [diff] [blame] | 4588 | CXXRecordDecl *RD = MD->getParent()->getCanonicalDecl(); |
| 4589 | if (!CurRD || (CurRD != RD && !CurRD->isDerivedFrom(RD))) |
| 4590 | continue; |
| 4591 | } |
| 4592 | } |
| 4593 | return true; |
Kaelyn Uhrain | 53e7219 | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 4594 | } |
| 4595 | return false; |
| 4596 | } |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 4597 | |
| 4598 | void Sema::diagnoseTypo(const TypoCorrection &Correction, |
| 4599 | const PartialDiagnostic &TypoDiag, |
| 4600 | bool ErrorRecovery) { |
| 4601 | diagnoseTypo(Correction, TypoDiag, PDiag(diag::note_previous_decl), |
| 4602 | ErrorRecovery); |
| 4603 | } |
| 4604 | |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4605 | /// Find which declaration we should import to provide the definition of |
| 4606 | /// the given declaration. |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4607 | static NamedDecl *getDefinitionToImport(NamedDecl *D) { |
| 4608 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4609 | return VD->getDefinition(); |
| 4610 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4611 | return FD->isDefined(FD) ? const_cast<FunctionDecl*>(FD) : nullptr; |
| 4612 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4613 | return TD->getDefinition(); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4614 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4615 | return ID->getDefinition(); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4616 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4617 | return PD->getDefinition(); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4618 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4619 | return getDefinitionToImport(TD->getTemplatedDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4620 | return nullptr; |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 4623 | /// \brief Diagnose a successfully-corrected typo. Separated from the correction |
| 4624 | /// itself to allow external validation of the result, etc. |
| 4625 | /// |
| 4626 | /// \param Correction The result of performing typo correction. |
| 4627 | /// \param TypoDiag The diagnostic to produce. This will have the corrected |
| 4628 | /// string added to it (and usually also a fixit). |
| 4629 | /// \param PrevNote A note to use when indicating the location of the entity to |
| 4630 | /// which we are correcting. Will have the correction string added to it. |
| 4631 | /// \param ErrorRecovery If \c true (the default), the caller is going to |
| 4632 | /// recover from the typo as if the corrected string had been typed. |
| 4633 | /// In this case, \c PDiag must be an error, and we will attach a fixit |
| 4634 | /// to it. |
| 4635 | void Sema::diagnoseTypo(const TypoCorrection &Correction, |
| 4636 | const PartialDiagnostic &TypoDiag, |
| 4637 | const PartialDiagnostic &PrevNote, |
| 4638 | bool ErrorRecovery) { |
| 4639 | std::string CorrectedStr = Correction.getAsString(getLangOpts()); |
| 4640 | std::string CorrectedQuotedStr = Correction.getQuoted(getLangOpts()); |
| 4641 | FixItHint FixTypo = FixItHint::CreateReplacement( |
| 4642 | Correction.getCorrectionRange(), CorrectedStr); |
| 4643 | |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4644 | // Maybe we're just missing a module import. |
| 4645 | if (Correction.requiresImport()) { |
| 4646 | NamedDecl *Decl = Correction.getCorrectionDecl(); |
| 4647 | assert(Decl && "import required but no declaration to import"); |
| 4648 | |
| 4649 | // Suggest importing a module providing the definition of this entity, if |
| 4650 | // possible. |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4651 | NamedDecl *Def = getDefinitionToImport(Decl); |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4652 | if (!Def) |
| 4653 | Def = Decl; |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4654 | Module *Owner = getOwningModule(Def); |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4655 | assert(Owner && "definition of hidden declaration is not in a module"); |
| 4656 | |
| 4657 | Diag(Correction.getCorrectionRange().getBegin(), |
| 4658 | diag::err_module_private_declaration) |
| 4659 | << Def << Owner->getFullModuleName(); |
| 4660 | Diag(Def->getLocation(), diag::note_previous_declaration); |
| 4661 | |
| 4662 | // Recover by implicitly importing this module. |
Richard Smith | 3d23c42 | 2014-05-07 02:25:43 +0000 | [diff] [blame] | 4663 | if (ErrorRecovery) |
| 4664 | createImplicitModuleImportForErrorRecovery( |
| 4665 | Correction.getCorrectionRange().getBegin(), Owner); |
Richard Smith | e156254d | 2013-08-20 20:35:18 +0000 | [diff] [blame] | 4666 | return; |
| 4667 | } |
| 4668 | |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 4669 | Diag(Correction.getCorrectionRange().getBegin(), TypoDiag) |
| 4670 | << CorrectedQuotedStr << (ErrorRecovery ? FixTypo : FixItHint()); |
| 4671 | |
| 4672 | NamedDecl *ChosenDecl = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4673 | Correction.isKeyword() ? nullptr : Correction.getCorrectionDecl(); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 4674 | if (PrevNote.getDiagID() && ChosenDecl) |
| 4675 | Diag(ChosenDecl->getLocation(), PrevNote) |
| 4676 | << CorrectedQuotedStr << (ErrorRecovery ? FixItHint() : FixTypo); |
| 4677 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4678 | |
Kaelyn Takata | 8363f04 | 2014-10-27 18:07:42 +0000 | [diff] [blame] | 4679 | TypoExpr *Sema::createDelayedTypo(std::unique_ptr<TypoCorrectionConsumer> TCC, |
| 4680 | TypoDiagnosticGenerator TDG, |
| 4681 | TypoRecoveryCallback TRC) { |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4682 | assert(TCC && "createDelayedTypo requires a valid TypoCorrectionConsumer"); |
| 4683 | auto TE = new (Context) TypoExpr(Context.DependentTy); |
| 4684 | auto &State = DelayedTypos[TE]; |
| 4685 | State.Consumer = std::move(TCC); |
| 4686 | State.DiagHandler = std::move(TDG); |
Kaelyn Takata | 8363f04 | 2014-10-27 18:07:42 +0000 | [diff] [blame] | 4687 | State.RecoveryHandler = std::move(TRC); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 4688 | return TE; |
| 4689 | } |
| 4690 | |
| 4691 | const Sema::TypoExprState &Sema::getTypoExprState(TypoExpr *TE) const { |
| 4692 | auto Entry = DelayedTypos.find(TE); |
| 4693 | assert(Entry != DelayedTypos.end() && |
| 4694 | "Failed to get the state for a TypoExpr!"); |
| 4695 | return Entry->second; |
| 4696 | } |
| 4697 | |
| 4698 | void Sema::clearDelayedTypo(TypoExpr *TE) { |
| 4699 | DelayedTypos.erase(TE); |
| 4700 | } |