Douglas Gregor | 78d7013 | 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 | //===----------------------------------------------------------------------===// |
| 14 | #include "Sema.h" |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
| 22 | #include "clang/Basic/LangOptions.h" |
| 23 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 25 | #include <set> |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 26 | #include <vector> |
| 27 | #include <iterator> |
| 28 | #include <utility> |
| 29 | #include <algorithm> |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 33 | typedef llvm::SmallVector<UsingDirectiveDecl*, 4> UsingDirectivesTy; |
| 34 | typedef llvm::DenseSet<NamespaceDecl*> NamespaceSet; |
| 35 | typedef llvm::SmallVector<Sema::LookupResult, 3> LookupResultsTy; |
| 36 | |
| 37 | /// UsingDirAncestorCompare - Implements strict weak ordering of |
| 38 | /// UsingDirectives. It orders them by address of its common ancestor. |
| 39 | struct UsingDirAncestorCompare { |
| 40 | |
| 41 | /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext. |
| 42 | bool operator () (UsingDirectiveDecl *U, const DeclContext *Ctx) const { |
| 43 | return U->getCommonAncestor() < Ctx; |
| 44 | } |
| 45 | |
| 46 | /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext. |
| 47 | bool operator () (const DeclContext *Ctx, UsingDirectiveDecl *U) const { |
| 48 | return Ctx < U->getCommonAncestor(); |
| 49 | } |
| 50 | |
| 51 | /// @brief Compares UsingDirectiveDecl common ancestors. |
| 52 | bool operator () (UsingDirectiveDecl *U1, UsingDirectiveDecl *U2) const { |
| 53 | return U1->getCommonAncestor() < U2->getCommonAncestor(); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | /// AddNamespaceUsingDirectives - Adds all UsingDirectiveDecl's to heap UDirs |
| 58 | /// (ordered by common ancestors), found in namespace NS, |
| 59 | /// including all found (recursively) in their nominated namespaces. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 60 | void AddNamespaceUsingDirectives(ASTContext &Context, |
| 61 | DeclContext *NS, |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 62 | UsingDirectivesTy &UDirs, |
| 63 | NamespaceSet &Visited) { |
| 64 | DeclContext::udir_iterator I, End; |
| 65 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 66 | for (llvm::tie(I, End) = NS->getUsingDirectives(Context); I !=End; ++I) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 67 | UDirs.push_back(*I); |
| 68 | std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare()); |
| 69 | NamespaceDecl *Nominated = (*I)->getNominatedNamespace(); |
| 70 | if (Visited.insert(Nominated).second) |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 71 | AddNamespaceUsingDirectives(Context, Nominated, UDirs, /*ref*/ Visited); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | /// AddScopeUsingDirectives - Adds all UsingDirectiveDecl's found in Scope S, |
| 76 | /// including all found in the namespaces they nominate. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 77 | static void AddScopeUsingDirectives(ASTContext &Context, Scope *S, |
| 78 | UsingDirectivesTy &UDirs) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 79 | NamespaceSet VisitedNS; |
| 80 | |
| 81 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 82 | |
| 83 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Ctx)) |
| 84 | VisitedNS.insert(NS); |
| 85 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 86 | AddNamespaceUsingDirectives(Context, Ctx, UDirs, /*ref*/ VisitedNS); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 87 | |
| 88 | } else { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 89 | Scope::udir_iterator I = S->using_directives_begin(), |
| 90 | End = S->using_directives_end(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 91 | |
| 92 | for (; I != End; ++I) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 93 | UsingDirectiveDecl *UD = I->getAs<UsingDirectiveDecl>(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 94 | UDirs.push_back(UD); |
| 95 | std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare()); |
| 96 | |
| 97 | NamespaceDecl *Nominated = UD->getNominatedNamespace(); |
| 98 | if (!VisitedNS.count(Nominated)) { |
| 99 | VisitedNS.insert(Nominated); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 100 | AddNamespaceUsingDirectives(Context, Nominated, UDirs, |
| 101 | /*ref*/ VisitedNS); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 107 | /// MaybeConstructOverloadSet - Name lookup has determined that the |
| 108 | /// elements in [I, IEnd) have the name that we are looking for, and |
| 109 | /// *I is a match for the namespace. This routine returns an |
| 110 | /// appropriate Decl for name lookup, which may either be *I or an |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 111 | /// OverloadedFunctionDecl that represents the overloaded functions in |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 112 | /// [I, IEnd). |
| 113 | /// |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 114 | /// The existance of this routine is temporary; users of LookupResult |
| 115 | /// should be able to handle multiple results, to deal with cases of |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 116 | /// ambiguity and overloaded functions without needing to create a |
| 117 | /// Decl node. |
| 118 | template<typename DeclIterator> |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 119 | static NamedDecl * |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 120 | MaybeConstructOverloadSet(ASTContext &Context, |
| 121 | DeclIterator I, DeclIterator IEnd) { |
| 122 | assert(I != IEnd && "Iterator range cannot be empty"); |
| 123 | assert(!isa<OverloadedFunctionDecl>(*I) && |
| 124 | "Cannot have an overloaded function"); |
| 125 | |
| 126 | if (isa<FunctionDecl>(*I)) { |
| 127 | // If we found a function, there might be more functions. If |
| 128 | // so, collect them into an overload set. |
| 129 | DeclIterator Last = I; |
| 130 | OverloadedFunctionDecl *Ovl = 0; |
| 131 | for (++Last; Last != IEnd && isa<FunctionDecl>(*Last); ++Last) { |
| 132 | if (!Ovl) { |
| 133 | // FIXME: We leak this overload set. Eventually, we want to |
| 134 | // stop building the declarations for these overload sets, so |
| 135 | // there will be nothing to leak. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 136 | Ovl = OverloadedFunctionDecl::Create(Context, (*I)->getDeclContext(), |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 137 | (*I)->getDeclName()); |
| 138 | Ovl->addOverload(cast<FunctionDecl>(*I)); |
| 139 | } |
| 140 | Ovl->addOverload(cast<FunctionDecl>(*Last)); |
| 141 | } |
| 142 | |
| 143 | // If we had more than one function, we built an overload |
| 144 | // set. Return it. |
| 145 | if (Ovl) |
| 146 | return Ovl; |
| 147 | } |
| 148 | |
| 149 | return *I; |
| 150 | } |
| 151 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 152 | /// Merges together multiple LookupResults dealing with duplicated Decl's. |
| 153 | static Sema::LookupResult |
| 154 | MergeLookupResults(ASTContext &Context, LookupResultsTy &Results) { |
| 155 | typedef Sema::LookupResult LResult; |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 156 | typedef llvm::SmallPtrSet<NamedDecl*, 4> DeclsSetTy; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 157 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 158 | // Remove duplicated Decl pointing at same Decl, by storing them in |
| 159 | // associative collection. This might be case for code like: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 160 | // |
| 161 | // namespace A { int i; } |
| 162 | // namespace B { using namespace A; } |
| 163 | // namespace C { using namespace A; } |
| 164 | // |
| 165 | // void foo() { |
| 166 | // using namespace B; |
| 167 | // using namespace C; |
| 168 | // ++i; // finds A::i, from both namespace B and C at global scope |
| 169 | // } |
| 170 | // |
| 171 | // C++ [namespace.qual].p3: |
| 172 | // The same declaration found more than once is not an ambiguity |
| 173 | // (because it is still a unique declaration). |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 174 | DeclsSetTy FoundDecls; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 176 | // Counter of tag names, and functions for resolving ambiguity |
| 177 | // and name hiding. |
| 178 | std::size_t TagNames = 0, Functions = 0, OrdinaryNonFunc = 0; |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 180 | LookupResultsTy::iterator I = Results.begin(), End = Results.end(); |
| 181 | |
| 182 | // No name lookup results, return early. |
| 183 | if (I == End) return LResult::CreateLookupResult(Context, 0); |
| 184 | |
| 185 | // Keep track of the tag declaration we found. We only use this if |
| 186 | // we find a single tag declaration. |
| 187 | TagDecl *TagFound = 0; |
| 188 | |
| 189 | for (; I != End; ++I) { |
| 190 | switch (I->getKind()) { |
| 191 | case LResult::NotFound: |
| 192 | assert(false && |
| 193 | "Should be always successful name lookup result here."); |
| 194 | break; |
| 195 | |
| 196 | case LResult::AmbiguousReference: |
| 197 | case LResult::AmbiguousBaseSubobjectTypes: |
| 198 | case LResult::AmbiguousBaseSubobjects: |
| 199 | assert(false && "Shouldn't get ambiguous lookup here."); |
| 200 | break; |
| 201 | |
| 202 | case LResult::Found: { |
| 203 | NamedDecl *ND = I->getAsDecl(); |
| 204 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) { |
| 205 | TagFound = Context.getCanonicalDecl(TD); |
| 206 | TagNames += FoundDecls.insert(TagFound)? 1 : 0; |
| 207 | } else if (isa<FunctionDecl>(ND)) |
| 208 | Functions += FoundDecls.insert(ND)? 1 : 0; |
| 209 | else |
| 210 | FoundDecls.insert(ND); |
| 211 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 214 | case LResult::FoundOverloaded: |
| 215 | for (LResult::iterator FI = I->begin(), FEnd = I->end(); FI != FEnd; ++FI) |
| 216 | Functions += FoundDecls.insert(*FI)? 1 : 0; |
| 217 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 218 | } |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 219 | } |
| 220 | OrdinaryNonFunc = FoundDecls.size() - TagNames - Functions; |
| 221 | bool Ambiguous = false, NameHidesTags = false; |
| 222 | |
| 223 | if (FoundDecls.size() == 1) { |
| 224 | // 1) Exactly one result. |
| 225 | } else if (TagNames > 1) { |
| 226 | // 2) Multiple tag names (even though they may be hidden by an |
| 227 | // object name). |
| 228 | Ambiguous = true; |
| 229 | } else if (FoundDecls.size() - TagNames == 1) { |
| 230 | // 3) Ordinary name hides (optional) tag. |
| 231 | NameHidesTags = TagFound; |
| 232 | } else if (Functions) { |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 233 | // C++ [basic.lookup].p1: |
| 234 | // ... Name lookup may associate more than one declaration with |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 235 | // a name if it finds the name to be a function name; the declarations |
| 236 | // are said to form a set of overloaded functions (13.1). |
| 237 | // Overload resolution (13.3) takes place after name lookup has succeeded. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 238 | // |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 239 | if (!OrdinaryNonFunc) { |
| 240 | // 4) Functions hide tag names. |
| 241 | NameHidesTags = TagFound; |
| 242 | } else { |
| 243 | // 5) Functions + ordinary names. |
| 244 | Ambiguous = true; |
| 245 | } |
| 246 | } else { |
| 247 | // 6) Multiple non-tag names |
| 248 | Ambiguous = true; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 251 | if (Ambiguous) |
| 252 | return LResult::CreateLookupResult(Context, |
| 253 | FoundDecls.begin(), FoundDecls.size()); |
| 254 | if (NameHidesTags) { |
| 255 | // There's only one tag, TagFound. Remove it. |
| 256 | assert(TagFound && FoundDecls.count(TagFound) && "No tag name found?"); |
| 257 | FoundDecls.erase(TagFound); |
| 258 | } |
| 259 | |
| 260 | // Return successful name lookup result. |
| 261 | return LResult::CreateLookupResult(Context, |
| 262 | MaybeConstructOverloadSet(Context, |
| 263 | FoundDecls.begin(), |
| 264 | FoundDecls.end())); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Retrieve the set of identifier namespaces that correspond to a |
| 268 | // specific kind of name lookup. |
| 269 | inline unsigned |
| 270 | getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind, |
| 271 | bool CPlusPlus) { |
| 272 | unsigned IDNS = 0; |
| 273 | switch (NameKind) { |
| 274 | case Sema::LookupOrdinaryName: |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 275 | case Sema::LookupOperatorName: |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 276 | case Sema::LookupRedeclarationWithLinkage: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 277 | IDNS = Decl::IDNS_Ordinary; |
| 278 | if (CPlusPlus) |
| 279 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member; |
| 280 | break; |
| 281 | |
| 282 | case Sema::LookupTagName: |
| 283 | IDNS = Decl::IDNS_Tag; |
| 284 | break; |
| 285 | |
| 286 | case Sema::LookupMemberName: |
| 287 | IDNS = Decl::IDNS_Member; |
| 288 | if (CPlusPlus) |
| 289 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
| 290 | break; |
| 291 | |
| 292 | case Sema::LookupNestedNameSpecifierName: |
| 293 | case Sema::LookupNamespaceName: |
| 294 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member; |
| 295 | break; |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | afd5eb3 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 297 | case Sema::LookupObjCProtocolName: |
| 298 | IDNS = Decl::IDNS_ObjCProtocol; |
| 299 | break; |
| 300 | |
| 301 | case Sema::LookupObjCImplementationName: |
| 302 | IDNS = Decl::IDNS_ObjCImplementation; |
| 303 | break; |
| 304 | |
| 305 | case Sema::LookupObjCCategoryImplName: |
| 306 | IDNS = Decl::IDNS_ObjCCategoryImpl; |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 307 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 308 | } |
| 309 | return IDNS; |
| 310 | } |
| 311 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 312 | Sema::LookupResult |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 313 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, NamedDecl *D) { |
Douglas Gregor | 54713b6 | 2009-04-24 02:57:34 +0000 | [diff] [blame^] | 314 | if (ObjCCompatibleAliasDecl *Alias |
| 315 | = dyn_cast_or_null<ObjCCompatibleAliasDecl>(D)) |
| 316 | D = Alias->getClassInterface(); |
| 317 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 318 | LookupResult Result; |
| 319 | Result.StoredKind = (D && isa<OverloadedFunctionDecl>(D))? |
| 320 | OverloadedDeclSingleDecl : SingleDecl; |
| 321 | Result.First = reinterpret_cast<uintptr_t>(D); |
| 322 | Result.Last = 0; |
| 323 | Result.Context = &Context; |
| 324 | return Result; |
| 325 | } |
| 326 | |
Douglas Gregor | 6beddfe | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 327 | /// @brief Moves the name-lookup results from Other to this LookupResult. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 328 | Sema::LookupResult |
| 329 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 330 | IdentifierResolver::iterator F, |
| 331 | IdentifierResolver::iterator L) { |
| 332 | LookupResult Result; |
| 333 | Result.Context = &Context; |
| 334 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 335 | if (F != L && isa<FunctionDecl>(*F)) { |
| 336 | IdentifierResolver::iterator Next = F; |
| 337 | ++Next; |
| 338 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 339 | Result.StoredKind = OverloadedDeclFromIdResolver; |
| 340 | Result.First = F.getAsOpaqueValue(); |
| 341 | Result.Last = L.getAsOpaqueValue(); |
| 342 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
Douglas Gregor | 54713b6 | 2009-04-24 02:57:34 +0000 | [diff] [blame^] | 345 | |
| 346 | Decl *D = *F; |
| 347 | if (ObjCCompatibleAliasDecl *Alias |
| 348 | = dyn_cast_or_null<ObjCCompatibleAliasDecl>(D)) |
| 349 | D = Alias->getClassInterface(); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 350 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 351 | Result.StoredKind = SingleDecl; |
Douglas Gregor | 54713b6 | 2009-04-24 02:57:34 +0000 | [diff] [blame^] | 352 | Result.First = reinterpret_cast<uintptr_t>(D); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 353 | Result.Last = 0; |
| 354 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 357 | Sema::LookupResult |
| 358 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 359 | DeclContext::lookup_iterator F, |
| 360 | DeclContext::lookup_iterator L) { |
| 361 | LookupResult Result; |
| 362 | Result.Context = &Context; |
| 363 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 364 | if (F != L && isa<FunctionDecl>(*F)) { |
| 365 | DeclContext::lookup_iterator Next = F; |
| 366 | ++Next; |
| 367 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 368 | Result.StoredKind = OverloadedDeclFromDeclContext; |
| 369 | Result.First = reinterpret_cast<uintptr_t>(F); |
| 370 | Result.Last = reinterpret_cast<uintptr_t>(L); |
| 371 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
Douglas Gregor | 54713b6 | 2009-04-24 02:57:34 +0000 | [diff] [blame^] | 374 | |
| 375 | Decl *D = *F; |
| 376 | if (ObjCCompatibleAliasDecl *Alias |
| 377 | = dyn_cast_or_null<ObjCCompatibleAliasDecl>(D)) |
| 378 | D = Alias->getClassInterface(); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 380 | Result.StoredKind = SingleDecl; |
Douglas Gregor | 54713b6 | 2009-04-24 02:57:34 +0000 | [diff] [blame^] | 381 | Result.First = reinterpret_cast<uintptr_t>(D); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 382 | Result.Last = 0; |
| 383 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 386 | /// @brief Determine the result of name lookup. |
| 387 | Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const { |
| 388 | switch (StoredKind) { |
| 389 | case SingleDecl: |
| 390 | return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound; |
| 391 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 392 | case OverloadedDeclSingleDecl: |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 393 | case OverloadedDeclFromIdResolver: |
| 394 | case OverloadedDeclFromDeclContext: |
| 395 | return FoundOverloaded; |
| 396 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 397 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 398 | return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 399 | |
| 400 | case AmbiguousLookupStoresDecls: |
| 401 | return AmbiguousReference; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 404 | // We can't ever get here. |
| 405 | return NotFound; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /// @brief Converts the result of name lookup into a single (possible |
| 409 | /// NULL) pointer to a declaration. |
| 410 | /// |
| 411 | /// The resulting declaration will either be the declaration we found |
| 412 | /// (if only a single declaration was found), an |
| 413 | /// OverloadedFunctionDecl (if an overloaded function was found), or |
| 414 | /// NULL (if no declaration was found). This conversion must not be |
| 415 | /// used anywhere where name lookup could result in an ambiguity. |
| 416 | /// |
| 417 | /// The OverloadedFunctionDecl conversion is meant as a stop-gap |
| 418 | /// solution, since it causes the OverloadedFunctionDecl to be |
| 419 | /// leaked. FIXME: Eventually, there will be a better way to iterate |
| 420 | /// over the set of overloaded functions returned by name lookup. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 421 | NamedDecl *Sema::LookupResult::getAsDecl() const { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 422 | switch (StoredKind) { |
| 423 | case SingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 424 | return reinterpret_cast<NamedDecl *>(First); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 425 | |
| 426 | case OverloadedDeclFromIdResolver: |
| 427 | return MaybeConstructOverloadSet(*Context, |
| 428 | IdentifierResolver::iterator::getFromOpaqueValue(First), |
| 429 | IdentifierResolver::iterator::getFromOpaqueValue(Last)); |
| 430 | |
| 431 | case OverloadedDeclFromDeclContext: |
| 432 | return MaybeConstructOverloadSet(*Context, |
| 433 | reinterpret_cast<DeclContext::lookup_iterator>(First), |
| 434 | reinterpret_cast<DeclContext::lookup_iterator>(Last)); |
| 435 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 436 | case OverloadedDeclSingleDecl: |
| 437 | return reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 438 | |
| 439 | case AmbiguousLookupStoresDecls: |
| 440 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 441 | assert(false && |
| 442 | "Name lookup returned an ambiguity that could not be handled"); |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 449 | /// @brief Retrieves the BasePaths structure describing an ambiguous |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 450 | /// name lookup, or null. |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 451 | BasePaths *Sema::LookupResult::getBasePaths() const { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 452 | if (StoredKind == AmbiguousLookupStoresBasePaths) |
| 453 | return reinterpret_cast<BasePaths *>(First); |
| 454 | return 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 457 | Sema::LookupResult::iterator::reference |
| 458 | Sema::LookupResult::iterator::operator*() const { |
| 459 | switch (Result->StoredKind) { |
| 460 | case SingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 461 | return reinterpret_cast<NamedDecl*>(Current); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 463 | case OverloadedDeclSingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 464 | return *reinterpret_cast<NamedDecl**>(Current); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 466 | case OverloadedDeclFromIdResolver: |
| 467 | return *IdentifierResolver::iterator::getFromOpaqueValue(Current); |
| 468 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 469 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 470 | if (Result->Last) |
| 471 | return *reinterpret_cast<NamedDecl**>(Current); |
| 472 | |
| 473 | // Fall through to handle the DeclContext::lookup_iterator we're |
| 474 | // storing. |
| 475 | |
| 476 | case OverloadedDeclFromDeclContext: |
| 477 | case AmbiguousLookupStoresDecls: |
| 478 | return *reinterpret_cast<DeclContext::lookup_iterator>(Current); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | Sema::LookupResult::iterator& Sema::LookupResult::iterator::operator++() { |
| 485 | switch (Result->StoredKind) { |
| 486 | case SingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 487 | Current = reinterpret_cast<uintptr_t>((NamedDecl*)0); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 488 | break; |
| 489 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 490 | case OverloadedDeclSingleDecl: { |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 491 | NamedDecl ** I = reinterpret_cast<NamedDecl**>(Current); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 492 | ++I; |
| 493 | Current = reinterpret_cast<uintptr_t>(I); |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 494 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 497 | case OverloadedDeclFromIdResolver: { |
| 498 | IdentifierResolver::iterator I |
| 499 | = IdentifierResolver::iterator::getFromOpaqueValue(Current); |
| 500 | ++I; |
| 501 | Current = I.getAsOpaqueValue(); |
| 502 | break; |
| 503 | } |
| 504 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 505 | case AmbiguousLookupStoresBasePaths: |
| 506 | if (Result->Last) { |
| 507 | NamedDecl ** I = reinterpret_cast<NamedDecl**>(Current); |
| 508 | ++I; |
| 509 | Current = reinterpret_cast<uintptr_t>(I); |
| 510 | break; |
| 511 | } |
| 512 | // Fall through to handle the DeclContext::lookup_iterator we're |
| 513 | // storing. |
| 514 | |
| 515 | case OverloadedDeclFromDeclContext: |
| 516 | case AmbiguousLookupStoresDecls: { |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 517 | DeclContext::lookup_iterator I |
| 518 | = reinterpret_cast<DeclContext::lookup_iterator>(Current); |
| 519 | ++I; |
| 520 | Current = reinterpret_cast<uintptr_t>(I); |
| 521 | break; |
| 522 | } |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return *this; |
| 526 | } |
| 527 | |
| 528 | Sema::LookupResult::iterator Sema::LookupResult::begin() { |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 529 | switch (StoredKind) { |
| 530 | case SingleDecl: |
| 531 | case OverloadedDeclFromIdResolver: |
| 532 | case OverloadedDeclFromDeclContext: |
| 533 | case AmbiguousLookupStoresDecls: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 534 | return iterator(this, First); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 535 | |
| 536 | case OverloadedDeclSingleDecl: { |
| 537 | OverloadedFunctionDecl * Ovl = |
| 538 | reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 539 | return iterator(this, |
| 540 | reinterpret_cast<uintptr_t>(&(*Ovl->function_begin()))); |
| 541 | } |
| 542 | |
| 543 | case AmbiguousLookupStoresBasePaths: |
| 544 | if (Last) |
| 545 | return iterator(this, |
| 546 | reinterpret_cast<uintptr_t>(getBasePaths()->found_decls_begin())); |
| 547 | else |
| 548 | return iterator(this, |
| 549 | reinterpret_cast<uintptr_t>(getBasePaths()->front().Decls.first)); |
| 550 | } |
| 551 | |
| 552 | // Required to suppress GCC warning. |
| 553 | return iterator(); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | Sema::LookupResult::iterator Sema::LookupResult::end() { |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 557 | switch (StoredKind) { |
| 558 | case SingleDecl: |
| 559 | case OverloadedDeclFromIdResolver: |
| 560 | case OverloadedDeclFromDeclContext: |
| 561 | case AmbiguousLookupStoresDecls: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 562 | return iterator(this, Last); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 563 | |
| 564 | case OverloadedDeclSingleDecl: { |
| 565 | OverloadedFunctionDecl * Ovl = |
| 566 | reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 567 | return iterator(this, |
| 568 | reinterpret_cast<uintptr_t>(&(*Ovl->function_end()))); |
| 569 | } |
| 570 | |
| 571 | case AmbiguousLookupStoresBasePaths: |
| 572 | if (Last) |
| 573 | return iterator(this, |
| 574 | reinterpret_cast<uintptr_t>(getBasePaths()->found_decls_end())); |
| 575 | else |
| 576 | return iterator(this, reinterpret_cast<uintptr_t>( |
| 577 | getBasePaths()->front().Decls.second)); |
| 578 | } |
| 579 | |
| 580 | // Required to suppress GCC warning. |
| 581 | return iterator(); |
| 582 | } |
| 583 | |
| 584 | void Sema::LookupResult::Destroy() { |
| 585 | if (BasePaths *Paths = getBasePaths()) |
| 586 | delete Paths; |
| 587 | else if (getKind() == AmbiguousReference) |
| 588 | delete[] reinterpret_cast<NamedDecl **>(First); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 591 | static void |
| 592 | CppNamespaceLookup(ASTContext &Context, DeclContext *NS, |
| 593 | DeclarationName Name, Sema::LookupNameKind NameKind, |
| 594 | unsigned IDNS, LookupResultsTy &Results, |
| 595 | UsingDirectivesTy *UDirs = 0) { |
| 596 | |
| 597 | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); |
| 598 | |
| 599 | // Perform qualified name lookup into the LookupCtx. |
| 600 | DeclContext::lookup_iterator I, E; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 601 | for (llvm::tie(I, E) = NS->lookup(Context, Name); I != E; ++I) |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 602 | if (Sema::isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 603 | Results.push_back(Sema::LookupResult::CreateLookupResult(Context, I, E)); |
| 604 | break; |
| 605 | } |
| 606 | |
| 607 | if (UDirs) { |
| 608 | // For each UsingDirectiveDecl, which common ancestor is equal |
| 609 | // to NS, we preform qualified name lookup into namespace nominated by it. |
| 610 | UsingDirectivesTy::const_iterator UI, UEnd; |
| 611 | llvm::tie(UI, UEnd) = |
| 612 | std::equal_range(UDirs->begin(), UDirs->end(), NS, |
| 613 | UsingDirAncestorCompare()); |
| 614 | |
| 615 | for (; UI != UEnd; ++UI) |
| 616 | CppNamespaceLookup(Context, (*UI)->getNominatedNamespace(), |
| 617 | Name, NameKind, IDNS, Results); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | static bool isNamespaceOrTranslationUnitScope(Scope *S) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 622 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 623 | return Ctx->isFileContext(); |
| 624 | return false; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 625 | } |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 627 | std::pair<bool, Sema::LookupResult> |
| 628 | Sema::CppLookupName(Scope *S, DeclarationName Name, |
| 629 | LookupNameKind NameKind, bool RedeclarationOnly) { |
| 630 | assert(getLangOptions().CPlusPlus && |
| 631 | "Can perform only C++ lookup"); |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 632 | unsigned IDNS |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 633 | = getIdentifierNamespacesFromLookupNameKind(NameKind, /*CPlusPlus*/ true); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 634 | Scope *Initial = S; |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 635 | DeclContext *OutOfLineCtx = 0; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 636 | IdentifierResolver::iterator |
| 637 | I = IdResolver.begin(Name), |
| 638 | IEnd = IdResolver.end(); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 639 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 640 | // First we lookup local scope. |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 641 | // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 642 | // ...During unqualified name lookup (3.4.1), the names appear as if |
| 643 | // they were declared in the nearest enclosing namespace which contains |
| 644 | // both the using-directive and the nominated namespace. |
| 645 | // [Note: in this context, “contains” means “contains directly or |
| 646 | // indirectly”. |
| 647 | // |
| 648 | // For example: |
| 649 | // namespace A { int i; } |
| 650 | // void foo() { |
| 651 | // int i; |
| 652 | // { |
| 653 | // using namespace A; |
| 654 | // ++i; // finds local 'i', A::i appears at global scope |
| 655 | // } |
| 656 | // } |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 657 | // |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 658 | for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 659 | // Check whether the IdResolver has anything in this scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 660 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 661 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 662 | // We found something. Look for anything else in our scope |
| 663 | // with this same name and in an acceptable identifier |
| 664 | // namespace, so that we can construct an overload set if we |
| 665 | // need to. |
| 666 | IdentifierResolver::iterator LastI = I; |
| 667 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 668 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | LookupResult Result = |
| 672 | LookupResult::CreateLookupResult(Context, I, LastI); |
| 673 | return std::make_pair(true, Result); |
| 674 | } |
| 675 | } |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 676 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 677 | LookupResult R; |
| 678 | // Perform member lookup into struct. |
| 679 | // FIXME: In some cases, we know that every name that could be |
| 680 | // found by this qualified name lookup will also be on the |
| 681 | // identifier chain. For example, inside a class without any |
| 682 | // base classes, we never need to perform qualified lookup |
| 683 | // because all of the members are on top of the identifier |
| 684 | // chain. |
Douglas Gregor | d3c7859 | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 685 | if (isa<RecordDecl>(Ctx)) { |
| 686 | R = LookupQualifiedName(Ctx, Name, NameKind, RedeclarationOnly); |
| 687 | if (R || RedeclarationOnly) |
| 688 | return std::make_pair(true, R); |
| 689 | } |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 690 | if (Ctx->getParent() != Ctx->getLexicalParent()) { |
| 691 | // It is out of line defined C++ method or struct, we continue |
| 692 | // doing name lookup in parent context. Once we will find namespace |
| 693 | // or translation-unit we save it for possible checking |
| 694 | // using-directives later. |
| 695 | for (OutOfLineCtx = Ctx; OutOfLineCtx && !OutOfLineCtx->isFileContext(); |
| 696 | OutOfLineCtx = OutOfLineCtx->getParent()) { |
Douglas Gregor | d3c7859 | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 697 | R = LookupQualifiedName(OutOfLineCtx, Name, NameKind, RedeclarationOnly); |
| 698 | if (R || RedeclarationOnly) |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 699 | return std::make_pair(true, R); |
| 700 | } |
| 701 | } |
| 702 | } |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 703 | } |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 705 | // Collect UsingDirectiveDecls in all scopes, and recursively all |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 706 | // nominated namespaces by those using-directives. |
| 707 | // UsingDirectives are pushed to heap, in common ancestor pointer |
| 708 | // value order. |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 709 | // FIXME: Cache this sorted list in Scope structure, and DeclContext, |
| 710 | // so we don't build it for each lookup! |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 711 | UsingDirectivesTy UDirs; |
| 712 | for (Scope *SC = Initial; SC; SC = SC->getParent()) |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 713 | if (SC->getFlags() & Scope::DeclScope) |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 714 | AddScopeUsingDirectives(Context, SC, UDirs); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 715 | |
| 716 | // Sort heapified UsingDirectiveDecls. |
| 717 | std::sort_heap(UDirs.begin(), UDirs.end()); |
| 718 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 719 | // Lookup namespace scope, and global scope. |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 720 | // Unqualified name lookup in C++ requires looking into scopes |
| 721 | // that aren't strictly lexical, and therefore we walk through the |
| 722 | // context as well as walking through the scopes. |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 723 | |
| 724 | LookupResultsTy LookupResults; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 725 | assert((!OutOfLineCtx || OutOfLineCtx->isFileContext()) && |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 726 | "We should have been looking only at file context here already."); |
| 727 | bool LookedInCtx = false; |
| 728 | LookupResult Result; |
| 729 | while (OutOfLineCtx && |
| 730 | OutOfLineCtx != S->getEntity() && |
| 731 | OutOfLineCtx->isNamespace()) { |
| 732 | LookedInCtx = true; |
| 733 | |
| 734 | // Look into context considering using-directives. |
| 735 | CppNamespaceLookup(Context, OutOfLineCtx, Name, NameKind, IDNS, |
| 736 | LookupResults, &UDirs); |
| 737 | |
| 738 | if ((Result = MergeLookupResults(Context, LookupResults)) || |
| 739 | (RedeclarationOnly && !OutOfLineCtx->isTransparentContext())) |
| 740 | return std::make_pair(true, Result); |
| 741 | |
| 742 | OutOfLineCtx = OutOfLineCtx->getParent(); |
| 743 | } |
| 744 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 745 | for (; S; S = S->getParent()) { |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 746 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 747 | assert(Ctx && Ctx->isFileContext() && |
| 748 | "We should have been looking only at file context here already."); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 749 | |
| 750 | // Check whether the IdResolver has anything in this scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 751 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 752 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 753 | // We found something. Look for anything else in our scope |
| 754 | // with this same name and in an acceptable identifier |
| 755 | // namespace, so that we can construct an overload set if we |
| 756 | // need to. |
| 757 | IdentifierResolver::iterator LastI = I; |
| 758 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 759 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | |
| 763 | // We store name lookup result, and continue trying to look into |
| 764 | // associated context, and maybe namespaces nominated by |
| 765 | // using-directives. |
| 766 | LookupResults.push_back( |
| 767 | LookupResult::CreateLookupResult(Context, I, LastI)); |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 772 | LookedInCtx = true; |
| 773 | // Look into context considering using-directives. |
| 774 | CppNamespaceLookup(Context, Ctx, Name, NameKind, IDNS, |
| 775 | LookupResults, &UDirs); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 776 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 777 | if ((Result = MergeLookupResults(Context, LookupResults)) || |
| 778 | (RedeclarationOnly && !Ctx->isTransparentContext())) |
| 779 | return std::make_pair(true, Result); |
| 780 | } |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 782 | if (!(LookedInCtx || LookupResults.empty())) { |
| 783 | // We didn't Performed lookup in Scope entity, so we return |
| 784 | // result form IdentifierResolver. |
| 785 | assert((LookupResults.size() == 1) && "Wrong size!"); |
| 786 | return std::make_pair(true, LookupResults.front()); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 787 | } |
| 788 | return std::make_pair(false, LookupResult()); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 791 | /// @brief Perform unqualified name lookup starting from a given |
| 792 | /// scope. |
| 793 | /// |
| 794 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 795 | /// used to find names within the current scope. For example, 'x' in |
| 796 | /// @code |
| 797 | /// int x; |
| 798 | /// int f() { |
| 799 | /// return x; // unqualified name look finds 'x' in the global scope |
| 800 | /// } |
| 801 | /// @endcode |
| 802 | /// |
| 803 | /// Different lookup criteria can find different names. For example, a |
| 804 | /// particular scope can have both a struct and a function of the same |
| 805 | /// name, and each can be found by certain lookup criteria. For more |
| 806 | /// information about lookup criteria, see the documentation for the |
| 807 | /// class LookupCriteria. |
| 808 | /// |
| 809 | /// @param S The scope from which unqualified name lookup will |
| 810 | /// begin. If the lookup criteria permits, name lookup may also search |
| 811 | /// in the parent scopes. |
| 812 | /// |
| 813 | /// @param Name The name of the entity that we are searching for. |
| 814 | /// |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 815 | /// @param Loc If provided, the source location where we're performing |
| 816 | /// name lookup. At present, this is only used to produce diagnostics when |
| 817 | /// C library functions (like "malloc") are implicitly declared. |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 818 | /// |
| 819 | /// @returns The result of name lookup, which includes zero or more |
| 820 | /// declarations and possibly additional information used to diagnose |
| 821 | /// ambiguities. |
| 822 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 823 | Sema::LookupName(Scope *S, DeclarationName Name, LookupNameKind NameKind, |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 824 | bool RedeclarationOnly, bool AllowBuiltinCreation, |
| 825 | SourceLocation Loc) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 826 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 827 | |
| 828 | if (!getLangOptions().CPlusPlus) { |
| 829 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 830 | // search in the declarations attached to the name. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 831 | unsigned IDNS = 0; |
| 832 | switch (NameKind) { |
| 833 | case Sema::LookupOrdinaryName: |
| 834 | IDNS = Decl::IDNS_Ordinary; |
| 835 | break; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 836 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 837 | case Sema::LookupTagName: |
| 838 | IDNS = Decl::IDNS_Tag; |
| 839 | break; |
| 840 | |
| 841 | case Sema::LookupMemberName: |
| 842 | IDNS = Decl::IDNS_Member; |
| 843 | break; |
| 844 | |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 845 | case Sema::LookupOperatorName: |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 846 | case Sema::LookupNestedNameSpecifierName: |
| 847 | case Sema::LookupNamespaceName: |
| 848 | assert(false && "C does not perform these kinds of name lookup"); |
| 849 | break; |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 850 | |
| 851 | case Sema::LookupRedeclarationWithLinkage: |
| 852 | // Find the nearest non-transparent declaration scope. |
| 853 | while (!(S->getFlags() & Scope::DeclScope) || |
| 854 | (S->getEntity() && |
| 855 | static_cast<DeclContext *>(S->getEntity()) |
| 856 | ->isTransparentContext())) |
| 857 | S = S->getParent(); |
| 858 | IDNS = Decl::IDNS_Ordinary; |
| 859 | break; |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 860 | |
Douglas Gregor | afd5eb3 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 861 | case Sema::LookupObjCProtocolName: |
| 862 | IDNS = Decl::IDNS_ObjCProtocol; |
| 863 | break; |
| 864 | |
| 865 | case Sema::LookupObjCImplementationName: |
| 866 | IDNS = Decl::IDNS_ObjCImplementation; |
| 867 | break; |
| 868 | |
| 869 | case Sema::LookupObjCCategoryImplName: |
| 870 | IDNS = Decl::IDNS_ObjCCategoryImpl; |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 871 | break; |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 874 | // Scan up the scope chain looking for a decl that matches this |
| 875 | // identifier that is in the appropriate namespace. This search |
| 876 | // should not take long, as shadowing of names is uncommon, and |
| 877 | // deep shadowing is extremely uncommon. |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 878 | bool LeftStartingScope = false; |
| 879 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 880 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
| 881 | IEnd = IdResolver.end(); |
| 882 | I != IEnd; ++I) |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 883 | if ((*I)->isInIdentifierNamespace(IDNS)) { |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 884 | if (NameKind == LookupRedeclarationWithLinkage) { |
| 885 | // Determine whether this (or a previous) declaration is |
| 886 | // out-of-scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 887 | if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 888 | LeftStartingScope = true; |
| 889 | |
| 890 | // If we found something outside of our starting scope that |
| 891 | // does not have linkage, skip it. |
| 892 | if (LeftStartingScope && !((*I)->hasLinkage())) |
| 893 | continue; |
| 894 | } |
| 895 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 896 | if ((*I)->getAttr<OverloadableAttr>()) { |
| 897 | // If this declaration has the "overloadable" attribute, we |
| 898 | // might have a set of overloaded functions. |
| 899 | |
| 900 | // Figure out what scope the identifier is in. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 901 | while (!(S->getFlags() & Scope::DeclScope) || |
| 902 | !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 903 | S = S->getParent(); |
| 904 | |
| 905 | // Find the last declaration in this scope (with the same |
| 906 | // name, naturally). |
| 907 | IdentifierResolver::iterator LastI = I; |
| 908 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 909 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 910 | break; |
| 911 | } |
| 912 | |
| 913 | return LookupResult::CreateLookupResult(Context, I, LastI); |
| 914 | } |
| 915 | |
| 916 | // We have a single lookup result. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 917 | return LookupResult::CreateLookupResult(Context, *I); |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 918 | } |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 919 | } else { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 920 | // Perform C++ unqualified name lookup. |
| 921 | std::pair<bool, LookupResult> MaybeResult = |
| 922 | CppLookupName(S, Name, NameKind, RedeclarationOnly); |
| 923 | if (MaybeResult.first) |
| 924 | return MaybeResult.second; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | // If we didn't find a use of this identifier, and if the identifier |
| 928 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 929 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 930 | if (NameKind == LookupOrdinaryName || |
| 931 | NameKind == LookupRedeclarationWithLinkage) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 932 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 933 | if (II && AllowBuiltinCreation) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 934 | // If this is a builtin on this (or all) targets, create the decl. |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 935 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 936 | // In C++, we don't have any predefined library functions like |
| 937 | // 'malloc'. Instead, we'll just error. |
| 938 | if (getLangOptions().CPlusPlus && |
| 939 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 940 | return LookupResult::CreateLookupResult(Context, 0); |
| 941 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 942 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 943 | LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 944 | S, RedeclarationOnly, Loc)); |
| 945 | } |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 946 | } |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 947 | } |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 948 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /// @brief Perform qualified name lookup into a given context. |
| 952 | /// |
| 953 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 954 | /// names when the context of those names is explicit specified, e.g., |
| 955 | /// "std::vector" or "x->member". |
| 956 | /// |
| 957 | /// Different lookup criteria can find different names. For example, a |
| 958 | /// particular scope can have both a struct and a function of the same |
| 959 | /// name, and each can be found by certain lookup criteria. For more |
| 960 | /// information about lookup criteria, see the documentation for the |
| 961 | /// class LookupCriteria. |
| 962 | /// |
| 963 | /// @param LookupCtx The context in which qualified name lookup will |
| 964 | /// search. If the lookup criteria permits, name lookup may also search |
| 965 | /// in the parent contexts or (for C++ classes) base classes. |
| 966 | /// |
| 967 | /// @param Name The name of the entity that we are searching for. |
| 968 | /// |
| 969 | /// @param Criteria The criteria that this routine will use to |
| 970 | /// determine which names are visible and which names will be |
| 971 | /// found. Note that name lookup will find a name that is visible by |
| 972 | /// the given criteria, but the entity itself may not be semantically |
| 973 | /// correct or even the kind of entity expected based on the |
| 974 | /// lookup. For example, searching for a nested-name-specifier name |
| 975 | /// might result in an EnumDecl, which is visible but is not permitted |
| 976 | /// as a nested-name-specifier in C++03. |
| 977 | /// |
| 978 | /// @returns The result of name lookup, which includes zero or more |
| 979 | /// declarations and possibly additional information used to diagnose |
| 980 | /// ambiguities. |
| 981 | Sema::LookupResult |
| 982 | Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name, |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 983 | LookupNameKind NameKind, bool RedeclarationOnly) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 984 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
| 985 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 986 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 987 | |
| 988 | // If we're performing qualified name lookup (e.g., lookup into a |
| 989 | // struct), find fields as part of ordinary name lookup. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 990 | unsigned IDNS |
| 991 | = getIdentifierNamespacesFromLookupNameKind(NameKind, |
| 992 | getLangOptions().CPlusPlus); |
| 993 | if (NameKind == LookupOrdinaryName) |
| 994 | IDNS |= Decl::IDNS_Member; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 995 | |
| 996 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 997 | DeclContext::lookup_iterator I, E; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 998 | for (llvm::tie(I, E) = LookupCtx->lookup(Context, Name); I != E; ++I) |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 999 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1000 | return LookupResult::CreateLookupResult(Context, I, E); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1001 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1002 | // If this isn't a C++ class or we aren't allowed to look into base |
| 1003 | // classes, we're done. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1004 | if (RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1005 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1006 | |
| 1007 | // Perform lookup into our base classes. |
| 1008 | BasePaths Paths; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1009 | Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx))); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Look for this member in our base classes |
| 1012 | if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1013 | MemberLookupCriteria(Name, NameKind, IDNS), Paths)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1014 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1015 | |
| 1016 | // C++ [class.member.lookup]p2: |
| 1017 | // [...] If the resulting set of declarations are not all from |
| 1018 | // sub-objects of the same type, or the set has a nonstatic member |
| 1019 | // and includes members from distinct sub-objects, there is an |
| 1020 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 1021 | // the result of the lookup. |
| 1022 | // FIXME: support using declarations! |
| 1023 | QualType SubobjectType; |
Daniel Dunbar | ddebeca | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1024 | int SubobjectNumber = 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1025 | for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
| 1026 | Path != PathEnd; ++Path) { |
| 1027 | const BasePathElement &PathElement = Path->back(); |
| 1028 | |
| 1029 | // Determine whether we're looking at a distinct sub-object or not. |
| 1030 | if (SubobjectType.isNull()) { |
| 1031 | // This is the first subobject we've looked at. Record it's type. |
| 1032 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 1033 | SubobjectNumber = PathElement.SubobjectNumber; |
| 1034 | } else if (SubobjectType |
| 1035 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 1036 | // We found members of the given name in two subobjects of |
| 1037 | // different types. This lookup is ambiguous. |
| 1038 | BasePaths *PathsOnHeap = new BasePaths; |
| 1039 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1040 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, true); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1041 | } else if (SubobjectNumber != PathElement.SubobjectNumber) { |
| 1042 | // We have a different subobject of the same type. |
| 1043 | |
| 1044 | // C++ [class.member.lookup]p5: |
| 1045 | // A static member, a nested type or an enumerator defined in |
| 1046 | // a base class T can unambiguously be found even if an object |
| 1047 | // has more than one base class subobject of type T. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1048 | Decl *FirstDecl = *Path->Decls.first; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1049 | if (isa<VarDecl>(FirstDecl) || |
| 1050 | isa<TypeDecl>(FirstDecl) || |
| 1051 | isa<EnumConstantDecl>(FirstDecl)) |
| 1052 | continue; |
| 1053 | |
| 1054 | if (isa<CXXMethodDecl>(FirstDecl)) { |
| 1055 | // Determine whether all of the methods are static. |
| 1056 | bool AllMethodsAreStatic = true; |
| 1057 | for (DeclContext::lookup_iterator Func = Path->Decls.first; |
| 1058 | Func != Path->Decls.second; ++Func) { |
| 1059 | if (!isa<CXXMethodDecl>(*Func)) { |
| 1060 | assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); |
| 1061 | break; |
| 1062 | } |
| 1063 | |
| 1064 | if (!cast<CXXMethodDecl>(*Func)->isStatic()) { |
| 1065 | AllMethodsAreStatic = false; |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (AllMethodsAreStatic) |
| 1071 | continue; |
| 1072 | } |
| 1073 | |
| 1074 | // We have found a nonstatic member name in multiple, distinct |
| 1075 | // subobjects. Name lookup is ambiguous. |
| 1076 | BasePaths *PathsOnHeap = new BasePaths; |
| 1077 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1078 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | // Lookup in a base class succeeded; return these results. |
| 1083 | |
| 1084 | // If we found a function declaration, return an overload set. |
| 1085 | if (isa<FunctionDecl>(*Paths.front().Decls.first)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1086 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1087 | Paths.front().Decls.first, Paths.front().Decls.second); |
| 1088 | |
| 1089 | // We found a non-function declaration; return a single declaration. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1090 | return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /// @brief Performs name lookup for a name that was parsed in the |
| 1094 | /// source code, and may contain a C++ scope specifier. |
| 1095 | /// |
| 1096 | /// This routine is a convenience routine meant to be called from |
| 1097 | /// contexts that receive a name and an optional C++ scope specifier |
| 1098 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 1099 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 1100 | /// respectively) on the given name and return those results. |
| 1101 | /// |
| 1102 | /// @param S The scope from which unqualified name lookup will |
| 1103 | /// begin. |
| 1104 | /// |
| 1105 | /// @param SS An optional C++ scope-specified, e.g., "::N::M". |
| 1106 | /// |
| 1107 | /// @param Name The name of the entity that name lookup will |
| 1108 | /// search for. |
| 1109 | /// |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1110 | /// @param Loc If provided, the source location where we're performing |
| 1111 | /// name lookup. At present, this is only used to produce diagnostics when |
| 1112 | /// C library functions (like "malloc") are implicitly declared. |
| 1113 | /// |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1114 | /// @returns The result of qualified or unqualified name lookup. |
| 1115 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1116 | Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS, |
| 1117 | DeclarationName Name, LookupNameKind NameKind, |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1118 | bool RedeclarationOnly, bool AllowBuiltinCreation, |
| 1119 | SourceLocation Loc) { |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1120 | if (SS) { |
Douglas Gregor | 6e7c27c | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 1121 | if (SS->isInvalid() || RequireCompleteDeclContext(*SS)) |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1122 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1124 | if (SS->isSet()) { |
| 1125 | return LookupQualifiedName(computeDeclContext(*SS), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1126 | Name, NameKind, RedeclarationOnly); |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1127 | } |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1130 | return LookupName(S, Name, NameKind, RedeclarationOnly, |
| 1131 | AllowBuiltinCreation, Loc); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1135 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 1136 | /// from name lookup. |
| 1137 | /// |
| 1138 | /// @param Result The ambiguous name lookup result. |
| 1139 | /// |
| 1140 | /// @param Name The name of the entity that name lookup was |
| 1141 | /// searching for. |
| 1142 | /// |
| 1143 | /// @param NameLoc The location of the name within the source code. |
| 1144 | /// |
| 1145 | /// @param LookupRange A source range that provides more |
| 1146 | /// source-location information concerning the lookup itself. For |
| 1147 | /// example, this range might highlight a nested-name-specifier that |
| 1148 | /// precedes the name. |
| 1149 | /// |
| 1150 | /// @returns true |
| 1151 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name, |
| 1152 | SourceLocation NameLoc, |
| 1153 | SourceRange LookupRange) { |
| 1154 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 1155 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1156 | if (BasePaths *Paths = Result.getBasePaths()) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1157 | if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) { |
| 1158 | QualType SubobjectType = Paths->front().back().Base->getType(); |
| 1159 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 1160 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 1161 | << LookupRange; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1162 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1163 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1164 | while (isa<CXXMethodDecl>(*Found) && |
| 1165 | cast<CXXMethodDecl>(*Found)->isStatic()) |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1166 | ++Found; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1167 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1168 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
| 1169 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1170 | Result.Destroy(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes && |
| 1175 | "Unhandled form of name lookup ambiguity"); |
| 1176 | |
| 1177 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 1178 | << Name << LookupRange; |
| 1179 | |
| 1180 | std::set<Decl *> DeclsPrinted; |
| 1181 | for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end(); |
| 1182 | Path != PathEnd; ++Path) { |
| 1183 | Decl *D = *Path->Decls.first; |
| 1184 | if (DeclsPrinted.insert(D).second) |
| 1185 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 1186 | } |
| 1187 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1188 | Result.Destroy(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1189 | return true; |
| 1190 | } else if (Result.getKind() == LookupResult::AmbiguousReference) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1191 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
| 1192 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1193 | NamedDecl **DI = reinterpret_cast<NamedDecl **>(Result.First), |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1194 | **DEnd = reinterpret_cast<NamedDecl **>(Result.Last); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | b02f5f2 | 2009-02-03 21:29:32 +0000 | [diff] [blame] | 1196 | for (; DI != DEnd; ++DI) |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1197 | Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1199 | Result.Destroy(); |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1200 | return true; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1203 | assert(false && "Unhandled form of name lookup ambiguity"); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1205 | // We can't reach here. |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1206 | return true; |
| 1207 | } |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1208 | |
| 1209 | // \brief Add the associated classes and namespaces for |
| 1210 | // argument-dependent lookup with an argument of class type |
| 1211 | // (C++ [basic.lookup.koenig]p2). |
| 1212 | static void |
| 1213 | addAssociatedClassesAndNamespaces(CXXRecordDecl *Class, |
| 1214 | ASTContext &Context, |
| 1215 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
| 1216 | Sema::AssociatedClassSet &AssociatedClasses) { |
| 1217 | // C++ [basic.lookup.koenig]p2: |
| 1218 | // [...] |
| 1219 | // -- If T is a class type (including unions), its associated |
| 1220 | // classes are: the class itself; the class of which it is a |
| 1221 | // member, if any; and its direct and indirect base |
| 1222 | // classes. Its associated namespaces are the namespaces in |
| 1223 | // which its associated classes are defined. |
| 1224 | |
| 1225 | // Add the class of which it is a member, if any. |
| 1226 | DeclContext *Ctx = Class->getDeclContext(); |
| 1227 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1228 | AssociatedClasses.insert(EnclosingClass); |
| 1229 | |
| 1230 | // Add the associated namespace for this class. |
| 1231 | while (Ctx->isRecord()) |
| 1232 | Ctx = Ctx->getParent(); |
| 1233 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1234 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1235 | |
| 1236 | // Add the class itself. If we've already seen this class, we don't |
| 1237 | // need to visit base classes. |
| 1238 | if (!AssociatedClasses.insert(Class)) |
| 1239 | return; |
| 1240 | |
| 1241 | // FIXME: Handle class template specializations |
| 1242 | |
| 1243 | // Add direct and indirect base classes along with their associated |
| 1244 | // namespaces. |
| 1245 | llvm::SmallVector<CXXRecordDecl *, 32> Bases; |
| 1246 | Bases.push_back(Class); |
| 1247 | while (!Bases.empty()) { |
| 1248 | // Pop this class off the stack. |
| 1249 | Class = Bases.back(); |
| 1250 | Bases.pop_back(); |
| 1251 | |
| 1252 | // Visit the base classes. |
| 1253 | for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), |
| 1254 | BaseEnd = Class->bases_end(); |
| 1255 | Base != BaseEnd; ++Base) { |
| 1256 | const RecordType *BaseType = Base->getType()->getAsRecordType(); |
| 1257 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 1258 | if (AssociatedClasses.insert(BaseDecl)) { |
| 1259 | // Find the associated namespace for this base class. |
| 1260 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
| 1261 | while (BaseCtx->isRecord()) |
| 1262 | BaseCtx = BaseCtx->getParent(); |
| 1263 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(BaseCtx)) |
| 1264 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1265 | |
| 1266 | // Make sure we visit the bases of this base class. |
| 1267 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
| 1268 | Bases.push_back(BaseDecl); |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | // \brief Add the associated classes and namespaces for |
| 1275 | // argument-dependent lookup with an argument of type T |
| 1276 | // (C++ [basic.lookup.koenig]p2). |
| 1277 | static void |
| 1278 | addAssociatedClassesAndNamespaces(QualType T, |
| 1279 | ASTContext &Context, |
| 1280 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
| 1281 | Sema::AssociatedClassSet &AssociatedClasses) { |
| 1282 | // C++ [basic.lookup.koenig]p2: |
| 1283 | // |
| 1284 | // For each argument type T in the function call, there is a set |
| 1285 | // of zero or more associated namespaces and a set of zero or more |
| 1286 | // associated classes to be considered. The sets of namespaces and |
| 1287 | // classes is determined entirely by the types of the function |
| 1288 | // arguments (and the namespace of any template template |
| 1289 | // argument). Typedef names and using-declarations used to specify |
| 1290 | // the types do not contribute to this set. The sets of namespaces |
| 1291 | // and classes are determined in the following way: |
| 1292 | T = Context.getCanonicalType(T).getUnqualifiedType(); |
| 1293 | |
| 1294 | // -- If T is a pointer to U or an array of U, its associated |
| 1295 | // namespaces and classes are those associated with U. |
| 1296 | // |
| 1297 | // We handle this by unwrapping pointer and array types immediately, |
| 1298 | // to avoid unnecessary recursion. |
| 1299 | while (true) { |
| 1300 | if (const PointerType *Ptr = T->getAsPointerType()) |
| 1301 | T = Ptr->getPointeeType(); |
| 1302 | else if (const ArrayType *Ptr = Context.getAsArrayType(T)) |
| 1303 | T = Ptr->getElementType(); |
| 1304 | else |
| 1305 | break; |
| 1306 | } |
| 1307 | |
| 1308 | // -- If T is a fundamental type, its associated sets of |
| 1309 | // namespaces and classes are both empty. |
| 1310 | if (T->getAsBuiltinType()) |
| 1311 | return; |
| 1312 | |
| 1313 | // -- If T is a class type (including unions), its associated |
| 1314 | // classes are: the class itself; the class of which it is a |
| 1315 | // member, if any; and its direct and indirect base |
| 1316 | // classes. Its associated namespaces are the namespaces in |
| 1317 | // which its associated classes are defined. |
Douglas Gregor | 2e04759 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1318 | if (const RecordType *ClassType = T->getAsRecordType()) |
| 1319 | if (CXXRecordDecl *ClassDecl |
| 1320 | = dyn_cast<CXXRecordDecl>(ClassType->getDecl())) { |
| 1321 | addAssociatedClassesAndNamespaces(ClassDecl, Context, |
| 1322 | AssociatedNamespaces, |
| 1323 | AssociatedClasses); |
| 1324 | return; |
| 1325 | } |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1326 | |
| 1327 | // -- If T is an enumeration type, its associated namespace is |
| 1328 | // the namespace in which it is defined. If it is class |
| 1329 | // member, its associated class is the member’s class; else |
| 1330 | // it has no associated class. |
| 1331 | if (const EnumType *EnumT = T->getAsEnumType()) { |
| 1332 | EnumDecl *Enum = EnumT->getDecl(); |
| 1333 | |
| 1334 | DeclContext *Ctx = Enum->getDeclContext(); |
| 1335 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1336 | AssociatedClasses.insert(EnclosingClass); |
| 1337 | |
| 1338 | // Add the associated namespace for this class. |
| 1339 | while (Ctx->isRecord()) |
| 1340 | Ctx = Ctx->getParent(); |
| 1341 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1342 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1343 | |
| 1344 | return; |
| 1345 | } |
| 1346 | |
| 1347 | // -- If T is a function type, its associated namespaces and |
| 1348 | // classes are those associated with the function parameter |
| 1349 | // types and those associated with the return type. |
| 1350 | if (const FunctionType *FunctionType = T->getAsFunctionType()) { |
| 1351 | // Return type |
| 1352 | addAssociatedClassesAndNamespaces(FunctionType->getResultType(), |
| 1353 | Context, |
| 1354 | AssociatedNamespaces, AssociatedClasses); |
| 1355 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1356 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FunctionType); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1357 | if (!Proto) |
| 1358 | return; |
| 1359 | |
| 1360 | // Argument types |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1361 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1362 | ArgEnd = Proto->arg_type_end(); |
| 1363 | Arg != ArgEnd; ++Arg) |
| 1364 | addAssociatedClassesAndNamespaces(*Arg, Context, |
| 1365 | AssociatedNamespaces, AssociatedClasses); |
| 1366 | |
| 1367 | return; |
| 1368 | } |
| 1369 | |
| 1370 | // -- If T is a pointer to a member function of a class X, its |
| 1371 | // associated namespaces and classes are those associated |
| 1372 | // with the function parameter types and return type, |
| 1373 | // together with those associated with X. |
| 1374 | // |
| 1375 | // -- If T is a pointer to a data member of class X, its |
| 1376 | // associated namespaces and classes are those associated |
| 1377 | // with the member type together with those associated with |
| 1378 | // X. |
| 1379 | if (const MemberPointerType *MemberPtr = T->getAsMemberPointerType()) { |
| 1380 | // Handle the type that the pointer to member points to. |
| 1381 | addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(), |
| 1382 | Context, |
| 1383 | AssociatedNamespaces, AssociatedClasses); |
| 1384 | |
| 1385 | // Handle the class type into which this points. |
| 1386 | if (const RecordType *Class = MemberPtr->getClass()->getAsRecordType()) |
| 1387 | addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()), |
| 1388 | Context, |
| 1389 | AssociatedNamespaces, AssociatedClasses); |
| 1390 | |
| 1391 | return; |
| 1392 | } |
| 1393 | |
| 1394 | // FIXME: What about block pointers? |
| 1395 | // FIXME: What about Objective-C message sends? |
| 1396 | } |
| 1397 | |
| 1398 | /// \brief Find the associated classes and namespaces for |
| 1399 | /// argument-dependent lookup for a call with the given set of |
| 1400 | /// arguments. |
| 1401 | /// |
| 1402 | /// This routine computes the sets of associated classes and associated |
| 1403 | /// namespaces searched by argument-dependent lookup |
| 1404 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
| 1405 | void |
| 1406 | Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, |
| 1407 | AssociatedNamespaceSet &AssociatedNamespaces, |
| 1408 | AssociatedClassSet &AssociatedClasses) { |
| 1409 | AssociatedNamespaces.clear(); |
| 1410 | AssociatedClasses.clear(); |
| 1411 | |
| 1412 | // C++ [basic.lookup.koenig]p2: |
| 1413 | // For each argument type T in the function call, there is a set |
| 1414 | // of zero or more associated namespaces and a set of zero or more |
| 1415 | // associated classes to be considered. The sets of namespaces and |
| 1416 | // classes is determined entirely by the types of the function |
| 1417 | // arguments (and the namespace of any template template |
| 1418 | // argument). |
| 1419 | for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) { |
| 1420 | Expr *Arg = Args[ArgIdx]; |
| 1421 | |
| 1422 | if (Arg->getType() != Context.OverloadTy) { |
| 1423 | addAssociatedClassesAndNamespaces(Arg->getType(), Context, |
| 1424 | AssociatedNamespaces, AssociatedClasses); |
| 1425 | continue; |
| 1426 | } |
| 1427 | |
| 1428 | // [...] In addition, if the argument is the name or address of a |
| 1429 | // set of overloaded functions and/or function templates, its |
| 1430 | // associated classes and namespaces are the union of those |
| 1431 | // associated with each of the members of the set: the namespace |
| 1432 | // in which the function or function template is defined and the |
| 1433 | // classes and namespaces associated with its (non-dependent) |
| 1434 | // parameter types and return type. |
| 1435 | DeclRefExpr *DRE = 0; |
| 1436 | if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) { |
| 1437 | if (unaryOp->getOpcode() == UnaryOperator::AddrOf) |
| 1438 | DRE = dyn_cast<DeclRefExpr>(unaryOp->getSubExpr()); |
| 1439 | } else |
| 1440 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 1441 | if (!DRE) |
| 1442 | continue; |
| 1443 | |
| 1444 | OverloadedFunctionDecl *Ovl |
| 1445 | = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl()); |
| 1446 | if (!Ovl) |
| 1447 | continue; |
| 1448 | |
| 1449 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 1450 | FuncEnd = Ovl->function_end(); |
| 1451 | Func != FuncEnd; ++Func) { |
| 1452 | FunctionDecl *FDecl = cast<FunctionDecl>(*Func); |
| 1453 | |
| 1454 | // Add the namespace in which this function was defined. Note |
| 1455 | // that, if this is a member function, we do *not* consider the |
| 1456 | // enclosing namespace of its class. |
| 1457 | DeclContext *Ctx = FDecl->getDeclContext(); |
| 1458 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1459 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1460 | |
| 1461 | // Add the classes and namespaces associated with the parameter |
| 1462 | // types and return type of this function. |
| 1463 | addAssociatedClassesAndNamespaces(FDecl->getType(), Context, |
| 1464 | AssociatedNamespaces, AssociatedClasses); |
| 1465 | } |
| 1466 | } |
| 1467 | } |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1468 | |
| 1469 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 1470 | /// an acceptable non-member overloaded operator for a call whose |
| 1471 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 1472 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 1473 | /// enumeration types. |
| 1474 | static bool |
| 1475 | IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, |
| 1476 | QualType T1, QualType T2, |
| 1477 | ASTContext &Context) { |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1478 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
| 1479 | return true; |
| 1480 | |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1481 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 1482 | return true; |
| 1483 | |
| 1484 | const FunctionProtoType *Proto = Fn->getType()->getAsFunctionProtoType(); |
| 1485 | if (Proto->getNumArgs() < 1) |
| 1486 | return false; |
| 1487 | |
| 1488 | if (T1->isEnumeralType()) { |
| 1489 | QualType ArgType = Proto->getArgType(0).getNonReferenceType(); |
| 1490 | if (Context.getCanonicalType(T1).getUnqualifiedType() |
| 1491 | == Context.getCanonicalType(ArgType).getUnqualifiedType()) |
| 1492 | return true; |
| 1493 | } |
| 1494 | |
| 1495 | if (Proto->getNumArgs() < 2) |
| 1496 | return false; |
| 1497 | |
| 1498 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 1499 | QualType ArgType = Proto->getArgType(1).getNonReferenceType(); |
| 1500 | if (Context.getCanonicalType(T2).getUnqualifiedType() |
| 1501 | == Context.getCanonicalType(ArgType).getUnqualifiedType()) |
| 1502 | return true; |
| 1503 | } |
| 1504 | |
| 1505 | return false; |
| 1506 | } |
| 1507 | |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1508 | /// \brief Find the protocol with the given name, if any. |
| 1509 | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II) { |
Douglas Gregor | afd5eb3 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 1510 | Decl *D = LookupName(TUScope, II, LookupObjCProtocolName).getAsDecl(); |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1511 | return cast_or_null<ObjCProtocolDecl>(D); |
| 1512 | } |
| 1513 | |
Douglas Gregor | afd5eb3 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 1514 | /// \brief Find the Objective-C implementation with the given name, if |
| 1515 | /// any. |
| 1516 | ObjCImplementationDecl *Sema::LookupObjCImplementation(IdentifierInfo *II) { |
| 1517 | Decl *D = LookupName(TUScope, II, LookupObjCImplementationName).getAsDecl(); |
| 1518 | return cast_or_null<ObjCImplementationDecl>(D); |
| 1519 | } |
| 1520 | |
| 1521 | /// \brief Find the Objective-C category implementation with the given |
| 1522 | /// name, if any. |
| 1523 | ObjCCategoryImplDecl *Sema::LookupObjCCategoryImpl(IdentifierInfo *II) { |
| 1524 | Decl *D = LookupName(TUScope, II, LookupObjCCategoryImplName).getAsDecl(); |
| 1525 | return cast_or_null<ObjCCategoryImplDecl>(D); |
| 1526 | } |
| 1527 | |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1528 | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, |
| 1529 | QualType T1, QualType T2, |
| 1530 | FunctionSet &Functions) { |
| 1531 | // C++ [over.match.oper]p3: |
| 1532 | // -- The set of non-member candidates is the result of the |
| 1533 | // unqualified lookup of operator@ in the context of the |
| 1534 | // expression according to the usual rules for name lookup in |
| 1535 | // unqualified function calls (3.4.2) except that all member |
| 1536 | // functions are ignored. However, if no operand has a class |
| 1537 | // type, only those non-member functions in the lookup set |
| 1538 | // that have a first parameter of type T1 or “reference to |
| 1539 | // (possibly cv-qualified) T1”, when T1 is an enumeration |
| 1540 | // type, or (if there is a right operand) a second parameter |
| 1541 | // of type T2 or “reference to (possibly cv-qualified) T2”, |
| 1542 | // when T2 is an enumeration type, are candidate functions. |
| 1543 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 1544 | LookupResult Operators = LookupName(S, OpName, LookupOperatorName); |
| 1545 | |
| 1546 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
| 1547 | |
| 1548 | if (!Operators) |
| 1549 | return; |
| 1550 | |
| 1551 | for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end(); |
| 1552 | Op != OpEnd; ++Op) { |
| 1553 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Op)) |
| 1554 | if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) |
| 1555 | Functions.insert(FD); // FIXME: canonical FD |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | void Sema::ArgumentDependentLookup(DeclarationName Name, |
| 1560 | Expr **Args, unsigned NumArgs, |
| 1561 | FunctionSet &Functions) { |
| 1562 | // Find all of the associated namespaces and classes based on the |
| 1563 | // arguments we have. |
| 1564 | AssociatedNamespaceSet AssociatedNamespaces; |
| 1565 | AssociatedClassSet AssociatedClasses; |
| 1566 | FindAssociatedClassesAndNamespaces(Args, NumArgs, |
| 1567 | AssociatedNamespaces, AssociatedClasses); |
| 1568 | |
| 1569 | // C++ [basic.lookup.argdep]p3: |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1570 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 1571 | // and let Y be the lookup set produced by argument dependent |
| 1572 | // lookup (defined as follows). If X contains [...] then Y is |
| 1573 | // empty. Otherwise Y is the set of declarations found in the |
| 1574 | // namespaces associated with the argument types as described |
| 1575 | // below. The set of declarations found by the lookup of the name |
| 1576 | // is the union of X and Y. |
| 1577 | // |
| 1578 | // Here, we compute Y and add its members to the overloaded |
| 1579 | // candidate set. |
| 1580 | for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), |
| 1581 | NSEnd = AssociatedNamespaces.end(); |
| 1582 | NS != NSEnd; ++NS) { |
| 1583 | // When considering an associated namespace, the lookup is the |
| 1584 | // same as the lookup performed when the associated namespace is |
| 1585 | // used as a qualifier (3.4.3.2) except that: |
| 1586 | // |
| 1587 | // -- Any using-directives in the associated namespace are |
| 1588 | // ignored. |
| 1589 | // |
| 1590 | // -- FIXME: Any namespace-scope friend functions declared in |
| 1591 | // associated classes are visible within their respective |
| 1592 | // namespaces even if they are not visible during an ordinary |
| 1593 | // lookup (11.4). |
| 1594 | DeclContext::lookup_iterator I, E; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1595 | for (llvm::tie(I, E) = (*NS)->lookup(Context, Name); I != E; ++I) { |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1596 | FunctionDecl *Func = dyn_cast<FunctionDecl>(*I); |
| 1597 | if (!Func) |
| 1598 | break; |
| 1599 | |
| 1600 | Functions.insert(Func); |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | |