John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1 | //===--- Lookup.h - Classes for name lookup ---------------------*- C++ -*-===// |
| 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 defines the LookupResult class, which is integral to |
| 11 | // Sema's name-lookup subsystem. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_SEMA_LOOKUP_H |
| 16 | #define LLVM_CLANG_SEMA_LOOKUP_H |
| 17 | |
| 18 | #include "Sema.h" |
| 19 | |
| 20 | namespace clang { |
| 21 | |
| 22 | /// @brief Represents the results of name lookup. |
| 23 | /// |
| 24 | /// An instance of the LookupResult class captures the results of a |
| 25 | /// single name lookup, which can return no result (nothing found), |
| 26 | /// a single declaration, a set of overloaded functions, or an |
| 27 | /// ambiguity. Use the getKind() method to determine which of these |
| 28 | /// results occurred for a given lookup. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 29 | class LookupResult { |
| 30 | public: |
| 31 | enum LookupResultKind { |
| 32 | /// @brief No entity found met the criteria. |
| 33 | NotFound = 0, |
| 34 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 35 | /// @brief No entity found met the criteria within the current |
| 36 | /// instantiation,, but there were dependent base classes of the |
| 37 | /// current instantiation that could not be searched. |
| 38 | NotFoundInCurrentInstantiation, |
| 39 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 40 | /// @brief Name lookup found a single declaration that met the |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 41 | /// criteria. getFoundDecl() will return this declaration. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 42 | Found, |
| 43 | |
| 44 | /// @brief Name lookup found a set of overloaded functions that |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 45 | /// met the criteria. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 46 | FoundOverloaded, |
| 47 | |
| 48 | /// @brief Name lookup found an unresolvable value declaration |
| 49 | /// and cannot yet complete. This only happens in C++ dependent |
| 50 | /// contexts with dependent using declarations. |
| 51 | FoundUnresolvedValue, |
| 52 | |
| 53 | /// @brief Name lookup results in an ambiguity; use |
| 54 | /// getAmbiguityKind to figure out what kind of ambiguity |
| 55 | /// we have. |
| 56 | Ambiguous |
| 57 | }; |
| 58 | |
| 59 | enum AmbiguityKind { |
| 60 | /// Name lookup results in an ambiguity because multiple |
| 61 | /// entities that meet the lookup criteria were found in |
| 62 | /// subobjects of different types. For example: |
| 63 | /// @code |
| 64 | /// struct A { void f(int); } |
| 65 | /// struct B { void f(double); } |
| 66 | /// struct C : A, B { }; |
| 67 | /// void test(C c) { |
| 68 | /// c.f(0); // error: A::f and B::f come from subobjects of different |
| 69 | /// // types. overload resolution is not performed. |
| 70 | /// } |
| 71 | /// @endcode |
| 72 | AmbiguousBaseSubobjectTypes, |
| 73 | |
| 74 | /// Name lookup results in an ambiguity because multiple |
| 75 | /// nonstatic entities that meet the lookup criteria were found |
| 76 | /// in different subobjects of the same type. For example: |
| 77 | /// @code |
| 78 | /// struct A { int x; }; |
| 79 | /// struct B : A { }; |
| 80 | /// struct C : A { }; |
| 81 | /// struct D : B, C { }; |
| 82 | /// int test(D d) { |
| 83 | /// return d.x; // error: 'x' is found in two A subobjects (of B and C) |
| 84 | /// } |
| 85 | /// @endcode |
| 86 | AmbiguousBaseSubobjects, |
| 87 | |
| 88 | /// Name lookup results in an ambiguity because multiple definitions |
| 89 | /// of entity that meet the lookup criteria were found in different |
| 90 | /// declaration contexts. |
| 91 | /// @code |
| 92 | /// namespace A { |
| 93 | /// int i; |
| 94 | /// namespace B { int i; } |
| 95 | /// int test() { |
| 96 | /// using namespace B; |
| 97 | /// return i; // error 'i' is found in namespace A and A::B |
| 98 | /// } |
| 99 | /// } |
| 100 | /// @endcode |
| 101 | AmbiguousReference, |
| 102 | |
| 103 | /// Name lookup results in an ambiguity because an entity with a |
| 104 | /// tag name was hidden by an entity with an ordinary name from |
| 105 | /// a different context. |
| 106 | /// @code |
| 107 | /// namespace A { struct Foo {}; } |
| 108 | /// namespace B { void Foo(); } |
| 109 | /// namespace C { |
| 110 | /// using namespace A; |
| 111 | /// using namespace B; |
| 112 | /// } |
| 113 | /// void test() { |
| 114 | /// C::Foo(); // error: tag 'A::Foo' is hidden by an object in a |
| 115 | /// // different namespace |
| 116 | /// } |
| 117 | /// @endcode |
| 118 | AmbiguousTagHiding |
| 119 | }; |
| 120 | |
| 121 | /// A little identifier for flagging temporary lookup results. |
| 122 | enum TemporaryToken { |
| 123 | Temporary |
| 124 | }; |
| 125 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 126 | typedef UnresolvedSetImpl::iterator iterator; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 127 | typedef bool (*ResultFilter)(NamedDecl*, unsigned IDNS); |
| 128 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 129 | LookupResult(Sema &SemaRef, DeclarationName Name, SourceLocation NameLoc, |
| 130 | Sema::LookupNameKind LookupKind, |
| 131 | Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration) |
| 132 | : ResultKind(NotFound), |
| 133 | Paths(0), |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 134 | NamingClass(0), |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 135 | SemaRef(SemaRef), |
| 136 | Name(Name), |
| 137 | NameLoc(NameLoc), |
| 138 | LookupKind(LookupKind), |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 139 | IsAcceptableFn(0), |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 140 | IDNS(0), |
| 141 | Redecl(Redecl != Sema::NotForRedeclaration), |
| 142 | HideTags(true), |
| 143 | Diagnose(Redecl == Sema::NotForRedeclaration) |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 144 | { |
| 145 | configure(); |
| 146 | } |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 147 | |
| 148 | /// Creates a temporary lookup result, initializing its core data |
| 149 | /// using the information from another result. Diagnostics are always |
| 150 | /// disabled. |
| 151 | LookupResult(TemporaryToken _, const LookupResult &Other) |
| 152 | : ResultKind(NotFound), |
| 153 | Paths(0), |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 154 | NamingClass(0), |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 155 | SemaRef(Other.SemaRef), |
| 156 | Name(Other.Name), |
| 157 | NameLoc(Other.NameLoc), |
| 158 | LookupKind(Other.LookupKind), |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 159 | IsAcceptableFn(Other.IsAcceptableFn), |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 160 | IDNS(Other.IDNS), |
| 161 | Redecl(Other.Redecl), |
| 162 | HideTags(Other.HideTags), |
| 163 | Diagnose(false) |
| 164 | {} |
| 165 | |
| 166 | ~LookupResult() { |
| 167 | if (Diagnose) diagnose(); |
| 168 | if (Paths) deletePaths(Paths); |
| 169 | } |
| 170 | |
| 171 | /// Gets the name to look up. |
| 172 | DeclarationName getLookupName() const { |
| 173 | return Name; |
| 174 | } |
| 175 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 176 | /// \brief Sets the name to look up. |
| 177 | void setLookupName(DeclarationName Name) { |
| 178 | this->Name = Name; |
| 179 | } |
| 180 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 181 | /// Gets the kind of lookup to perform. |
| 182 | Sema::LookupNameKind getLookupKind() const { |
| 183 | return LookupKind; |
| 184 | } |
| 185 | |
| 186 | /// True if this lookup is just looking for an existing declaration. |
| 187 | bool isForRedeclaration() const { |
| 188 | return Redecl; |
| 189 | } |
| 190 | |
| 191 | /// Sets whether tag declarations should be hidden by non-tag |
| 192 | /// declarations during resolution. The default is true. |
| 193 | void setHideTags(bool Hide) { |
| 194 | HideTags = Hide; |
| 195 | } |
| 196 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 197 | bool isAmbiguous() const { |
| 198 | return getResultKind() == Ambiguous; |
| 199 | } |
| 200 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 201 | /// Determines if this names a single result which is not an |
| 202 | /// unresolved value using decl. If so, it is safe to call |
| 203 | /// getFoundDecl(). |
| 204 | bool isSingleResult() const { |
| 205 | return getResultKind() == Found; |
| 206 | } |
| 207 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 208 | /// Determines if the results are overloaded. |
| 209 | bool isOverloadedResult() const { |
| 210 | return getResultKind() == FoundOverloaded; |
| 211 | } |
| 212 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 213 | bool isUnresolvableResult() const { |
| 214 | return getResultKind() == FoundUnresolvedValue; |
| 215 | } |
| 216 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 217 | LookupResultKind getResultKind() const { |
| 218 | sanity(); |
| 219 | return ResultKind; |
| 220 | } |
| 221 | |
| 222 | AmbiguityKind getAmbiguityKind() const { |
| 223 | assert(isAmbiguous()); |
| 224 | return Ambiguity; |
| 225 | } |
| 226 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 227 | const UnresolvedSetImpl &asUnresolvedSet() const { |
| 228 | return Decls; |
| 229 | } |
| 230 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 231 | iterator begin() const { return iterator(Decls.begin()); } |
| 232 | iterator end() const { return iterator(Decls.end()); } |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 233 | |
| 234 | /// \brief Return true if no decls were found |
| 235 | bool empty() const { return Decls.empty(); } |
| 236 | |
| 237 | /// \brief Return the base paths structure that's associated with |
| 238 | /// these results, or null if none is. |
| 239 | CXXBasePaths *getBasePaths() const { |
| 240 | return Paths; |
| 241 | } |
| 242 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 243 | /// \brief Tests whether the given declaration is acceptable. |
| 244 | bool isAcceptableDecl(NamedDecl *D) const { |
| 245 | assert(IsAcceptableFn); |
| 246 | return IsAcceptableFn(D, IDNS); |
| 247 | } |
| 248 | |
| 249 | /// \brief Returns the identifier namespace mask for this lookup. |
| 250 | unsigned getIdentifierNamespace() const { |
| 251 | return IDNS; |
| 252 | } |
| 253 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 254 | /// \brief Returns whether these results arose from performing a |
| 255 | /// lookup into a class. |
| 256 | bool isClassLookup() const { |
| 257 | return NamingClass != 0; |
| 258 | } |
| 259 | |
| 260 | /// \brief Returns the 'naming class' for this lookup, i.e. the |
| 261 | /// class which was looked into to find these results. |
| 262 | /// |
| 263 | /// C++0x [class.access.base]p5: |
| 264 | /// The access to a member is affected by the class in which the |
| 265 | /// member is named. This naming class is the class in which the |
| 266 | /// member name was looked up and found. [Note: this class can be |
| 267 | /// explicit, e.g., when a qualified-id is used, or implicit, |
| 268 | /// e.g., when a class member access operator (5.2.5) is used |
| 269 | /// (including cases where an implicit "this->" is added). If both |
| 270 | /// a class member access operator and a qualified-id are used to |
| 271 | /// name the member (as in p->T::m), the class naming the member |
| 272 | /// is the class named by the nested-name-specifier of the |
| 273 | /// qualified-id (that is, T). -- end note ] |
| 274 | /// |
| 275 | /// This is set by the lookup routines when they find results in a class. |
| 276 | CXXRecordDecl *getNamingClass() const { |
| 277 | return NamingClass; |
| 278 | } |
| 279 | |
| 280 | /// \brief Sets the 'naming class' for this lookup. |
| 281 | void setNamingClass(CXXRecordDecl *Record) { |
| 282 | NamingClass = Record; |
| 283 | } |
| 284 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 285 | /// \brief Add a declaration to these results with its natural access. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 286 | /// Does not test the acceptance criteria. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 287 | void addDecl(NamedDecl *D) { |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 288 | addDecl(D, D->getAccess()); |
| 289 | } |
| 290 | |
| 291 | /// \brief Add a declaration to these results with the given access. |
| 292 | /// Does not test the acceptance criteria. |
| 293 | void addDecl(NamedDecl *D, AccessSpecifier AS) { |
| 294 | Decls.addDecl(D, AS); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 295 | ResultKind = Found; |
| 296 | } |
| 297 | |
| 298 | /// \brief Add all the declarations from another set of lookup |
| 299 | /// results. |
| 300 | void addAllDecls(const LookupResult &Other) { |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 301 | Decls.append(Other.Decls.begin(), Other.Decls.end()); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 302 | ResultKind = Found; |
| 303 | } |
| 304 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 305 | /// \brief Determine whether no result was found because we could not |
| 306 | /// search into dependent base classes of the current instantiation. |
| 307 | bool wasNotFoundInCurrentInstantiation() const { |
| 308 | return ResultKind == NotFoundInCurrentInstantiation; |
| 309 | } |
| 310 | |
| 311 | /// \brief Note that while no result was found in the current instantiation, |
| 312 | /// there were dependent base classes that could not be searched. |
| 313 | void setNotFoundInCurrentInstantiation() { |
| 314 | assert(ResultKind == NotFound && Decls.empty()); |
| 315 | ResultKind = NotFoundInCurrentInstantiation; |
| 316 | } |
| 317 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 318 | /// \brief Resolves the result kind of the lookup, possibly hiding |
| 319 | /// decls. |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 320 | /// |
| 321 | /// This should be called in any environment where lookup might |
| 322 | /// generate multiple lookup results. |
| 323 | void resolveKind(); |
| 324 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 325 | /// \brief Re-resolves the result kind of the lookup after a set of |
| 326 | /// removals has been performed. |
| 327 | void resolveKindAfterFilter() { |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 328 | if (Decls.empty()) { |
| 329 | if (ResultKind != NotFoundInCurrentInstantiation) |
| 330 | ResultKind = NotFound; |
| 331 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 332 | ResultKind = Found; |
| 333 | resolveKind(); |
| 334 | } |
| 335 | } |
| 336 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 337 | template <class DeclClass> |
| 338 | DeclClass *getAsSingle() const { |
| 339 | if (getResultKind() != Found) return 0; |
| 340 | return dyn_cast<DeclClass>(getFoundDecl()); |
| 341 | } |
| 342 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 343 | /// \brief Fetch the unique decl found by this lookup. Asserts |
| 344 | /// that one was found. |
| 345 | /// |
| 346 | /// This is intended for users who have examined the result kind |
| 347 | /// and are certain that there is only one result. |
| 348 | NamedDecl *getFoundDecl() const { |
| 349 | assert(getResultKind() == Found |
| 350 | && "getFoundDecl called on non-unique result"); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 351 | return (*begin())->getUnderlyingDecl(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 352 | } |
| 353 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 354 | /// Fetches a representative decl. Useful for lazy diagnostics. |
| 355 | NamedDecl *getRepresentativeDecl() const { |
| 356 | assert(!Decls.empty() && "cannot get representative of empty set"); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 357 | return *begin(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 358 | } |
| 359 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 360 | /// \brief Asks if the result is a single tag decl. |
| 361 | bool isSingleTagDecl() const { |
| 362 | return getResultKind() == Found && isa<TagDecl>(getFoundDecl()); |
| 363 | } |
| 364 | |
| 365 | /// \brief Make these results show that the name was found in |
| 366 | /// base classes of different types. |
| 367 | /// |
| 368 | /// The given paths object is copied and invalidated. |
| 369 | void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P); |
| 370 | |
| 371 | /// \brief Make these results show that the name was found in |
| 372 | /// distinct base classes of the same type. |
| 373 | /// |
| 374 | /// The given paths object is copied and invalidated. |
| 375 | void setAmbiguousBaseSubobjects(CXXBasePaths &P); |
| 376 | |
| 377 | /// \brief Make these results show that the name was found in |
| 378 | /// different contexts and a tag decl was hidden by an ordinary |
| 379 | /// decl in a different context. |
| 380 | void setAmbiguousQualifiedTagHiding() { |
| 381 | setAmbiguous(AmbiguousTagHiding); |
| 382 | } |
| 383 | |
| 384 | /// \brief Clears out any current state. |
| 385 | void clear() { |
| 386 | ResultKind = NotFound; |
| 387 | Decls.clear(); |
| 388 | if (Paths) deletePaths(Paths); |
| 389 | Paths = NULL; |
| 390 | } |
| 391 | |
| 392 | /// \brief Clears out any current state and re-initializes for a |
| 393 | /// different kind of lookup. |
| 394 | void clear(Sema::LookupNameKind Kind) { |
| 395 | clear(); |
| 396 | LookupKind = Kind; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 397 | configure(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 398 | } |
| 399 | |
John McCall | 9c86b51 | 2010-03-25 21:28:06 +0000 | [diff] [blame^] | 400 | /// \brief Change this lookup's redeclaration kind. |
| 401 | void setRedeclarationKind(Sema::RedeclarationKind RK) { |
| 402 | Redecl = RK; |
| 403 | configure(); |
| 404 | } |
| 405 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 406 | void print(llvm::raw_ostream &); |
| 407 | |
| 408 | /// Suppress the diagnostics that would normally fire because of this |
| 409 | /// lookup. This happens during (e.g.) redeclaration lookups. |
| 410 | void suppressDiagnostics() { |
| 411 | Diagnose = false; |
| 412 | } |
| 413 | |
| 414 | /// Sets a 'context' source range. |
| 415 | void setContextRange(SourceRange SR) { |
| 416 | NameContextRange = SR; |
| 417 | } |
| 418 | |
| 419 | /// Gets the source range of the context of this name; for C++ |
| 420 | /// qualified lookups, this is the source range of the scope |
| 421 | /// specifier. |
| 422 | SourceRange getContextRange() const { |
| 423 | return NameContextRange; |
| 424 | } |
| 425 | |
| 426 | /// Gets the location of the identifier. This isn't always defined: |
| 427 | /// sometimes we're doing lookups on synthesized names. |
| 428 | SourceLocation getNameLoc() const { |
| 429 | return NameLoc; |
| 430 | } |
| 431 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 432 | /// \brief Get the Sema object that this lookup result is searching |
| 433 | /// with. |
| 434 | Sema &getSema() const { return SemaRef; } |
| 435 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 436 | /// A class for iterating through a result set and possibly |
| 437 | /// filtering out results. The results returned are possibly |
| 438 | /// sugared. |
| 439 | class Filter { |
| 440 | LookupResult &Results; |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 441 | LookupResult::iterator I; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 442 | bool Changed; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 443 | #ifndef NDEBUG |
| 444 | bool CalledDone; |
| 445 | #endif |
| 446 | |
| 447 | friend class LookupResult; |
| 448 | Filter(LookupResult &Results) |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 449 | : Results(Results), I(Results.begin()), Changed(false) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 450 | #ifndef NDEBUG |
| 451 | , CalledDone(false) |
| 452 | #endif |
| 453 | {} |
| 454 | |
| 455 | public: |
| 456 | #ifndef NDEBUG |
| 457 | ~Filter() { |
| 458 | assert(CalledDone && |
| 459 | "LookupResult::Filter destroyed without done() call"); |
| 460 | } |
| 461 | #endif |
| 462 | |
| 463 | bool hasNext() const { |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 464 | return I != Results.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | NamedDecl *next() { |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 468 | assert(I != Results.end() && "next() called on empty filter"); |
| 469 | return *I++; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /// Erase the last element returned from this iterator. |
| 473 | void erase() { |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 474 | Results.Decls.erase(--I); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 475 | Changed = true; |
| 476 | } |
| 477 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 478 | /// Replaces the current entry with the given one, preserving the |
| 479 | /// access bits. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 480 | void replace(NamedDecl *D) { |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 481 | Results.Decls.replace(I-1, D); |
| 482 | Changed = true; |
| 483 | } |
| 484 | |
| 485 | /// Replaces the current entry with the given one. |
| 486 | void replace(NamedDecl *D, AccessSpecifier AS) { |
| 487 | Results.Decls.replace(I-1, D, AS); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 488 | Changed = true; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void done() { |
| 492 | #ifndef NDEBUG |
| 493 | assert(!CalledDone && "done() called twice"); |
| 494 | CalledDone = true; |
| 495 | #endif |
| 496 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 497 | if (Changed) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 498 | Results.resolveKindAfterFilter(); |
| 499 | } |
| 500 | }; |
| 501 | |
| 502 | /// Create a filter for this result set. |
| 503 | Filter makeFilter() { |
| 504 | return Filter(*this); |
| 505 | } |
| 506 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 507 | private: |
| 508 | void diagnose() { |
| 509 | if (isAmbiguous()) |
| 510 | SemaRef.DiagnoseAmbiguousLookup(*this); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 511 | else if (isClassLookup() && SemaRef.getLangOptions().AccessControl) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 512 | SemaRef.CheckLookupAccess(*this); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void setAmbiguous(AmbiguityKind AK) { |
| 516 | ResultKind = Ambiguous; |
| 517 | Ambiguity = AK; |
| 518 | } |
| 519 | |
| 520 | void addDeclsFromBasePaths(const CXXBasePaths &P); |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 521 | void configure(); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 522 | |
| 523 | // Sanity checks. |
| 524 | void sanity() const { |
| 525 | assert(ResultKind != NotFound || Decls.size() == 0); |
| 526 | assert(ResultKind != Found || Decls.size() == 1); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 527 | assert(ResultKind != FoundOverloaded || Decls.size() > 1 || |
John McCall | 97d9e21 | 2009-11-22 20:57:36 +0000 | [diff] [blame] | 528 | (Decls.size() == 1 && |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 529 | isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 530 | assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved()); |
| 531 | assert(ResultKind != Ambiguous || Decls.size() > 1 || |
| 532 | (Decls.size() == 1 && Ambiguity == AmbiguousBaseSubobjects)); |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 533 | assert((Paths != NULL) == (ResultKind == Ambiguous && |
| 534 | (Ambiguity == AmbiguousBaseSubobjectTypes || |
| 535 | Ambiguity == AmbiguousBaseSubobjects))); |
| 536 | } |
| 537 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 538 | bool sanityCheckUnresolved() const { |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 539 | for (iterator I = begin(), E = end(); I != E; ++I) |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 540 | if (isa<UnresolvedUsingValueDecl>(*I)) |
| 541 | return true; |
| 542 | return false; |
| 543 | } |
| 544 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 545 | static void deletePaths(CXXBasePaths *); |
| 546 | |
| 547 | // Results. |
| 548 | LookupResultKind ResultKind; |
| 549 | AmbiguityKind Ambiguity; // ill-defined unless ambiguous |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 550 | UnresolvedSet<8> Decls; |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 551 | CXXBasePaths *Paths; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 552 | CXXRecordDecl *NamingClass; |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 553 | |
| 554 | // Parameters. |
| 555 | Sema &SemaRef; |
| 556 | DeclarationName Name; |
| 557 | SourceLocation NameLoc; |
| 558 | SourceRange NameContextRange; |
| 559 | Sema::LookupNameKind LookupKind; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 560 | ResultFilter IsAcceptableFn; // set by configure() |
| 561 | unsigned IDNS; // set by configure() |
| 562 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 563 | bool Redecl; |
| 564 | |
| 565 | /// \brief True if tag declarations should be hidden if non-tags |
| 566 | /// are present |
| 567 | bool HideTags; |
| 568 | |
| 569 | bool Diagnose; |
| 570 | }; |
| 571 | |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 572 | /// \brief Consumes visible declarations found when searching for |
| 573 | /// all visible names within a given scope or context. |
| 574 | /// |
| 575 | /// This abstract class is meant to be subclassed by clients of \c |
| 576 | /// Sema::LookupVisibleDecls(), each of which should override the \c |
| 577 | /// FoundDecl() function to process declarations as they are found. |
| 578 | class VisibleDeclConsumer { |
| 579 | public: |
| 580 | /// \brief Destroys the visible declaration consumer. |
| 581 | virtual ~VisibleDeclConsumer(); |
| 582 | |
| 583 | /// \brief Invoked each time \p Sema::LookupVisibleDecls() finds a |
| 584 | /// declaration visible from the current scope or context. |
| 585 | /// |
| 586 | /// \param ND the declaration found. |
| 587 | /// |
| 588 | /// \param Hiding a declaration that hides the declaration \p ND, |
| 589 | /// or NULL if no such declaration exists. |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 590 | /// |
| 591 | /// \param InBaseClass whether this declaration was found in base |
| 592 | /// class of the context we searched. |
| 593 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, |
| 594 | bool InBaseClass) = 0; |
Douglas Gregor | 546be3c | 2009-12-30 17:04:44 +0000 | [diff] [blame] | 595 | }; |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 596 | |
| 597 | /// \brief A class for storing results from argument-dependent lookup. |
| 598 | class ADLResult { |
| 599 | private: |
| 600 | /// A map from canonical decls to the 'most recent' decl. |
| 601 | llvm::DenseMap<NamedDecl*, NamedDecl*> Decls; |
| 602 | |
| 603 | public: |
| 604 | /// Adds a new ADL candidate to this map. |
| 605 | void insert(NamedDecl *D); |
| 606 | |
| 607 | /// Removes any data associated with a given decl. |
| 608 | void erase(NamedDecl *D) { |
| 609 | Decls.erase(cast<NamedDecl>(D->getCanonicalDecl())); |
| 610 | } |
| 611 | |
| 612 | class iterator { |
| 613 | typedef llvm::DenseMap<NamedDecl*,NamedDecl*>::iterator inner_iterator; |
| 614 | inner_iterator iter; |
| 615 | |
| 616 | friend class ADLResult; |
| 617 | iterator(const inner_iterator &iter) : iter(iter) {} |
| 618 | public: |
| 619 | iterator() {} |
| 620 | |
| 621 | iterator &operator++() { ++iter; return *this; } |
| 622 | iterator operator++(int) { return iterator(iter++); } |
| 623 | |
| 624 | NamedDecl *operator*() const { return iter->second; } |
| 625 | |
| 626 | bool operator==(const iterator &other) const { return iter == other.iter; } |
| 627 | bool operator!=(const iterator &other) const { return iter != other.iter; } |
| 628 | }; |
| 629 | |
| 630 | iterator begin() { return iterator(Decls.begin()); } |
| 631 | iterator end() { return iterator(Decls.end()); } |
| 632 | }; |
| 633 | |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | #endif |