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